|
|
@ -14,7 +14,7 @@ namespace ugv { |
|
|
|
namespace comms { |
|
|
|
namespace comms { |
|
|
|
|
|
|
|
|
|
|
|
static const char * TAG = "ugv_comms"; |
|
|
|
static const char * TAG = "ugv_comms"; |
|
|
|
static const TickType_t SEND_PERIOD = pdMS_TO_TICKS(5000); |
|
|
|
static const TickType_t SEND_PERIOD = pdMS_TO_TICKS(15 * 1000); |
|
|
|
|
|
|
|
|
|
|
|
CommsClass::CommsClass() |
|
|
|
CommsClass::CommsClass() |
|
|
|
: last_packet_tick(0), |
|
|
|
: last_packet_tick(0), |
|
|
@ -121,9 +121,7 @@ void CommsClass::RunTask() { |
|
|
|
using messages::UGV_Status; |
|
|
|
using messages::UGV_Status; |
|
|
|
|
|
|
|
|
|
|
|
TickType_t current_tick = xTaskGetTickCount(); |
|
|
|
TickType_t current_tick = xTaskGetTickCount(); |
|
|
|
next_send = current_tick + SEND_PERIOD; |
|
|
|
next_status_send = current_tick; |
|
|
|
|
|
|
|
|
|
|
|
esp_err_t ret; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
sx127x_rx_packet_t rx_packet; |
|
|
|
sx127x_rx_packet_t rx_packet; |
|
|
@ -132,11 +130,8 @@ void CommsClass::RunTask() { |
|
|
|
int rx_len; |
|
|
|
int rx_len; |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
UGV_Message ugv_message; |
|
|
|
|
|
|
|
std::string ugv_message_data; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (true) { |
|
|
|
while (true) { |
|
|
|
TickType_t delay_ticks = next_send - current_tick; |
|
|
|
TickType_t delay_ticks = next_status_send - current_tick; |
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
ret = sx127x_recv_packet(lora, &rx_packet, delay_ticks); |
|
|
|
ret = sx127x_recv_packet(lora, &rx_packet, delay_ticks); |
|
|
@ -160,26 +155,31 @@ void CommsClass::RunTask() { |
|
|
|
|
|
|
|
|
|
|
|
current_tick = xTaskGetTickCount(); |
|
|
|
current_tick = xTaskGetTickCount(); |
|
|
|
|
|
|
|
|
|
|
|
if (current_tick < next_send) { |
|
|
|
if (current_tick < next_status_send) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Lock(); |
|
|
|
SendStatus(); |
|
|
|
ugv_message.mutable_status()->CopyFrom(this->status); |
|
|
|
|
|
|
|
Unlock(); |
|
|
|
|
|
|
|
ugv_message.SerializeToString(&ugv_message_data); |
|
|
|
|
|
|
|
ret = SendPacket(ugv_message_data); |
|
|
|
|
|
|
|
if (ret != ESP_OK) { |
|
|
|
|
|
|
|
ESP_LOGE(TAG, "error sending packet: %d", ret); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
ESP_LOGV(TAG, "lora wrote UGV_Message packet"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current_tick = xTaskGetTickCount(); |
|
|
|
current_tick = xTaskGetTickCount(); |
|
|
|
next_send = current_tick + SEND_PERIOD; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CommsClass::SendStatus() { |
|
|
|
|
|
|
|
Lock(); |
|
|
|
|
|
|
|
status_message.mutable_status()->CopyFrom(this->status); |
|
|
|
|
|
|
|
Unlock(); |
|
|
|
|
|
|
|
status_message.SerializeToString(&status_message_data); |
|
|
|
|
|
|
|
esp_err_t ret = SendPacket(status_message_data); |
|
|
|
|
|
|
|
if (ret != ESP_OK) { |
|
|
|
|
|
|
|
ESP_LOGE(TAG, "error sending packet: %d", ret); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
ESP_LOGV(TAG, "lora wrote UGV_Message packet"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
next_status_send = xTaskGetTickCount() + SEND_PERIOD; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
#ifdef COMMS_SX127X |
|
|
|
void CommsClass::HandlePacket(sx127x_rx_packet_t *rx_packet) { |
|
|
|
void CommsClass::HandlePacket(sx127x_rx_packet_t *rx_packet) { |
|
|
|
ESP_LOGI(TAG, "lora received packet (len %d, rssi: %d, snr: %f): %.*s\n", |
|
|
|
ESP_LOGI(TAG, "lora received packet (len %d, rssi: %d, snr: %f): %.*s\n", |
|
|
@ -240,17 +240,28 @@ void CommsClass::HandleCommand(const messages::GroundCommand &command) { |
|
|
|
ESP_LOGI(TAG, "rx command id %d type %d", command.id(), command.type()); |
|
|
|
ESP_LOGI(TAG, "rx command id %d type %d", command.id(), command.type()); |
|
|
|
using messages::UGV_State; |
|
|
|
using messages::UGV_State; |
|
|
|
|
|
|
|
|
|
|
|
xSemaphoreTake(mutex, pdMS_TO_TICKS(10)); |
|
|
|
|
|
|
|
switch (command.type()) { |
|
|
|
switch (command.type()) { |
|
|
|
case messages::CMD_DISABLE: status.set_state(UGV_State::STATE_IDLE); break; |
|
|
|
case messages::CMD_DISABLE: |
|
|
|
|
|
|
|
xSemaphoreTake(mutex, pdMS_TO_TICKS(10)); |
|
|
|
|
|
|
|
status.set_state(UGV_State::STATE_IDLE); |
|
|
|
|
|
|
|
xSemaphoreGive(mutex); |
|
|
|
|
|
|
|
break; |
|
|
|
case messages::CMD_DRIVE_TO_TARGET: |
|
|
|
case messages::CMD_DRIVE_TO_TARGET: |
|
|
|
|
|
|
|
xSemaphoreTake(mutex, pdMS_TO_TICKS(10)); |
|
|
|
status.set_state(UGV_State::STATE_AQUIRING); |
|
|
|
status.set_state(UGV_State::STATE_AQUIRING); |
|
|
|
|
|
|
|
xSemaphoreGive(mutex); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case messages::CMD_TEST: |
|
|
|
|
|
|
|
xSemaphoreTake(mutex, pdMS_TO_TICKS(10)); |
|
|
|
|
|
|
|
status.set_state(UGV_State::STATE_TEST); |
|
|
|
|
|
|
|
xSemaphoreGive(mutex); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case messages::CMD_TEST: status.set_state(UGV_State::STATE_TEST); break; |
|
|
|
|
|
|
|
case messages::CMD_DRIVE_HEADING: { |
|
|
|
case messages::CMD_DRIVE_HEADING: { |
|
|
|
if (command.has_drive_heading()) { |
|
|
|
if (command.has_drive_heading()) { |
|
|
|
|
|
|
|
xSemaphoreTake(mutex, pdMS_TO_TICKS(10)); |
|
|
|
status.set_state(UGV_State::STATE_DRIVE_HEADING); |
|
|
|
status.set_state(UGV_State::STATE_DRIVE_HEADING); |
|
|
|
this->drive_heading = command.drive_heading(); |
|
|
|
this->drive_heading = command.drive_heading(); |
|
|
|
|
|
|
|
xSemaphoreGive(mutex); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
status.set_state(UGV_State::STATE_IDLE); |
|
|
|
status.set_state(UGV_State::STATE_IDLE); |
|
|
|
ESP_LOGE(TAG, "invalid CMD_DRIVE_HEADING"); |
|
|
|
ESP_LOGE(TAG, "invalid CMD_DRIVE_HEADING"); |
|
|
@ -259,35 +270,42 @@ void CommsClass::HandleCommand(const messages::GroundCommand &command) { |
|
|
|
} |
|
|
|
} |
|
|
|
case messages::CMD_SET_TARGET: { |
|
|
|
case messages::CMD_SET_TARGET: { |
|
|
|
if (command.has_target_location()) { |
|
|
|
if (command.has_target_location()) { |
|
|
|
|
|
|
|
xSemaphoreTake(mutex, pdMS_TO_TICKS(10)); |
|
|
|
if (!new_target) { |
|
|
|
if (!new_target) { |
|
|
|
new_target = new messages::TargetLocation(); |
|
|
|
new_target = new messages::TargetLocation(); |
|
|
|
} |
|
|
|
} |
|
|
|
new_target->CopyFrom(command.target_location()); |
|
|
|
new_target->CopyFrom(command.target_location()); |
|
|
|
|
|
|
|
xSemaphoreGive(mutex); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
case messages::CMD_SET_CONFIG: { |
|
|
|
case messages::CMD_SET_CONFIG: { |
|
|
|
if (command.has_config()) { |
|
|
|
if (command.has_config()) { |
|
|
|
|
|
|
|
xSemaphoreTake(mutex, pdMS_TO_TICKS(10)); |
|
|
|
if (!new_config) { |
|
|
|
if (!new_config) { |
|
|
|
new_config = new config::Config(); |
|
|
|
new_config = new config::Config(); |
|
|
|
} |
|
|
|
} |
|
|
|
new_config->CopyFrom(command.config()); |
|
|
|
new_config->CopyFrom(command.config()); |
|
|
|
|
|
|
|
xSemaphoreGive(mutex); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
case messages::CMD_GET_STATUS: { |
|
|
|
|
|
|
|
SendStatus(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case messages::CMD_PING: { |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
default: |
|
|
|
default: |
|
|
|
ESP_LOGW(TAG, "unhandled command type: %d", command.type()); |
|
|
|
ESP_LOGW(TAG, "unhandled command type: %d", command.type()); |
|
|
|
xSemaphoreGive(mutex); |
|
|
|
|
|
|
|
return; // early return, no ack
|
|
|
|
return; // early return, no ack
|
|
|
|
} |
|
|
|
} |
|
|
|
xSemaphoreGive(mutex); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
messages::UGV_Message ugv_message; |
|
|
|
messages::UGV_Message ugv_message; |
|
|
|
ugv_message.set_command_ack(command.id()); |
|
|
|
ugv_message.set_command_ack(command.id()); |
|
|
|
|
|
|
|
|
|
|
|
SendPacket(ugv_message); |
|
|
|
SendPacket(ugv_message); |
|
|
|
|
|
|
|
|
|
|
|
next_send = xTaskGetTickCount() + pdMS_TO_TICKS(200); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
esp_err_t CommsClass::SendPacket(const uint8_t *data, size_t size) { |
|
|
|
esp_err_t CommsClass::SendPacket(const uint8_t *data, size_t size) { |
|
|
|