|
|
@ -4,7 +4,10 @@ |
|
|
|
#include <esp_log.h> |
|
|
|
#include <esp_log.h> |
|
|
|
|
|
|
|
|
|
|
|
#include "messages.pb.h" |
|
|
|
#include "messages.pb.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
#include "sx127x_registers.h" |
|
|
|
#include "sx127x_registers.h" |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
namespace ugv { |
|
|
|
namespace ugv { |
|
|
|
namespace comms { |
|
|
|
namespace comms { |
|
|
@ -24,6 +27,9 @@ CommsClass::CommsClass() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void CommsClass::Init() { |
|
|
|
void CommsClass::Init() { |
|
|
|
|
|
|
|
esp_err_t ret; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
sx127x_config_t lora_config = sx127x_config_default(); |
|
|
|
sx127x_config_t lora_config = sx127x_config_default(); |
|
|
|
lora_config.sck_io_num = (gpio_num_t)LORA_SCK; |
|
|
|
lora_config.sck_io_num = (gpio_num_t)LORA_SCK; |
|
|
|
lora_config.miso_io_num = (gpio_num_t)LORA_MISO; |
|
|
|
lora_config.miso_io_num = (gpio_num_t)LORA_MISO; |
|
|
@ -38,20 +44,31 @@ void CommsClass::Init() { |
|
|
|
lora_config.sync_word = 0x34; |
|
|
|
lora_config.sync_word = 0x34; |
|
|
|
lora_config.crc = SX127X_CRC_ENABLED; |
|
|
|
lora_config.crc = SX127X_CRC_ENABLED; |
|
|
|
|
|
|
|
|
|
|
|
esp_err_t ret; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ret = sx127x_init(&lora_config, &lora); |
|
|
|
ret = sx127x_init(&lora_config, &lora); |
|
|
|
if (ret != ESP_OK) { |
|
|
|
if (ret != ESP_OK) { |
|
|
|
const char *err_name = esp_err_to_name(ret); |
|
|
|
const char *err_name = esp_err_to_name(ret); |
|
|
|
ESP_LOGE(TAG, "LoRa init failed: %s", err_name); |
|
|
|
ESP_LOGE(TAG, "LoRa init failed: %s", err_name); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
ESP_LOGI(TAG, "LoRa initialized"); |
|
|
|
|
|
|
|
ret = sx127x_start(lora); |
|
|
|
ret = sx127x_start(lora); |
|
|
|
if (ret != ESP_OK) { |
|
|
|
if (ret != ESP_OK) { |
|
|
|
ESP_LOGI(TAG, "LoRa start failed: %d", ret); |
|
|
|
ESP_LOGE(TAG, "LoRa start failed: %d", ret); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ESP_LOGI(TAG, "LoRa initialized"); |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
e32::Config lora_config; |
|
|
|
|
|
|
|
lora_config.uart_port = UART_NUM_2; |
|
|
|
|
|
|
|
lora_config.uart_rx_pin = 10; |
|
|
|
|
|
|
|
lora_config.uart_rx_pin = 11; |
|
|
|
|
|
|
|
ret = lora.Init(lora_config); |
|
|
|
|
|
|
|
if (ret != ESP_OK) { |
|
|
|
|
|
|
|
const char *error_name = esp_err_to_name(ret); |
|
|
|
|
|
|
|
ESP_LOGE(TAG, "E32 LoRa Init failed: %s (%d)", error_name, ret); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
packet_len = -1; |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
packet_num = 0; |
|
|
|
packet_num = 0; |
|
|
|
|
|
|
|
|
|
|
@ -67,17 +84,28 @@ void CommsClass::Unlock() { xSemaphoreGive(mutex); } |
|
|
|
|
|
|
|
|
|
|
|
int32_t CommsClass::ReadRssi() { |
|
|
|
int32_t CommsClass::ReadRssi() { |
|
|
|
int32_t rssi; |
|
|
|
int32_t rssi; |
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
sx127x_read_rssi(lora, &rssi); |
|
|
|
sx127x_read_rssi(lora, &rssi); |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
rssi = 0; |
|
|
|
|
|
|
|
#endif |
|
|
|
return rssi; |
|
|
|
return rssi; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint8_t CommsClass::ReadLnaGain() { |
|
|
|
uint8_t CommsClass::ReadLnaGain() { |
|
|
|
uint8_t lna_gain; |
|
|
|
uint8_t lna_gain; |
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
sx127x_read_lna_gain(lora, &lna_gain); |
|
|
|
sx127x_read_lna_gain(lora, &lna_gain); |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
lna_gain = 0; |
|
|
|
|
|
|
|
#endif |
|
|
|
return lna_gain; |
|
|
|
return lna_gain; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void CommsClass::CommsTask(void *arg) { ((CommsClass *)arg)->RunTask(); } |
|
|
|
void CommsClass::CommsTask(void *arg) { |
|
|
|
|
|
|
|
((CommsClass *)arg)->RunTask(); |
|
|
|
|
|
|
|
vTaskDelete(NULL); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void CommsClass::RunTask() { |
|
|
|
void CommsClass::RunTask() { |
|
|
|
using messages::Location; |
|
|
|
using messages::Location; |
|
|
@ -89,8 +117,16 @@ void CommsClass::RunTask() { |
|
|
|
TickType_t current_tick = xTaskGetTickCount(); |
|
|
|
TickType_t current_tick = xTaskGetTickCount(); |
|
|
|
TickType_t next_send = current_tick + send_period; |
|
|
|
TickType_t next_send = current_tick + send_period; |
|
|
|
|
|
|
|
|
|
|
|
esp_err_t ret; |
|
|
|
esp_err_t ret; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
sx127x_rx_packet_t rx_packet; |
|
|
|
sx127x_rx_packet_t rx_packet; |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
static const int rx_buf_len = MAX_PACKET_LEN * 2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t *rx_buf = new uint8_t[rx_buf_len]; |
|
|
|
|
|
|
|
int rx_len; |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
UGV_Message ugv_message; |
|
|
|
UGV_Message ugv_message; |
|
|
|
UGV_Status *status = ugv_message.mutable_status(); |
|
|
|
UGV_Status *status = ugv_message.mutable_status(); |
|
|
@ -103,26 +139,31 @@ void CommsClass::RunTask() { |
|
|
|
|
|
|
|
|
|
|
|
while (true) { |
|
|
|
while (true) { |
|
|
|
TickType_t delay_ticks = next_send - current_tick; |
|
|
|
TickType_t delay_ticks = next_send - current_tick; |
|
|
|
ret = sx127x_recv_packet(lora, &rx_packet, delay_ticks); |
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
|
|
|
|
ret = sx127x_recv_packet(lora, &rx_packet, delay_ticks); |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
rx_len = lora.Read(rx_buf, rx_buf_len, delay_ticks); |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
current_tick = xTaskGetTickCount(); |
|
|
|
current_tick = xTaskGetTickCount(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
if (ret == ESP_OK) { |
|
|
|
if (ret == ESP_OK) { |
|
|
|
HandlePacket(&rx_packet); |
|
|
|
HandlePacket(&rx_packet); |
|
|
|
|
|
|
|
|
|
|
|
sx127x_packet_rx_free(&rx_packet); |
|
|
|
sx127x_packet_rx_free(&rx_packet); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
HandleRxData(rx_buf, rx_len); |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
if (current_tick < next_send) { |
|
|
|
if (current_tick < next_send) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
packet_num++; |
|
|
|
packet_num++; |
|
|
|
auto str = ugv_message.SerializeAsString(); |
|
|
|
ret = SendPacket(ugv_message); |
|
|
|
ret = sx127x_send_packet(lora, str.c_str(), str.size(), |
|
|
|
|
|
|
|
0); // 0 means error if queue full
|
|
|
|
|
|
|
|
// ret = sx127x_send_packet_pb(st->lora, uas_ugv_UGV_Message_fields,
|
|
|
|
|
|
|
|
// &ugv_message,
|
|
|
|
|
|
|
|
// 0); // 0 means error if queue full
|
|
|
|
|
|
|
|
if (ret != ESP_OK) { |
|
|
|
if (ret != ESP_OK) { |
|
|
|
ESP_LOGE(TAG, "error sending packet: %d", ret); |
|
|
|
ESP_LOGE(TAG, "error sending packet: %d", ret); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -132,11 +173,13 @@ void CommsClass::RunTask() { |
|
|
|
current_tick = xTaskGetTickCount(); |
|
|
|
current_tick = xTaskGetTickCount(); |
|
|
|
next_send = current_tick + send_period; |
|
|
|
next_send = current_tick + send_period; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#ifndef COMMS_SX127X |
|
|
|
|
|
|
|
delete[] rx_buf; |
|
|
|
|
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
void CommsClass::HandlePacket(sx127x_rx_packet_t *rx_packet) { |
|
|
|
void CommsClass::HandlePacket(sx127x_rx_packet_t *rx_packet) { |
|
|
|
using messages::GroundMessage; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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", |
|
|
|
rx_packet->data_len, rx_packet->rssi, rx_packet->snr * 0.25f, |
|
|
|
rx_packet->data_len, rx_packet->rssi, rx_packet->snr * 0.25f, |
|
|
|
rx_packet->data_len, rx_packet->data); |
|
|
|
rx_packet->data_len, rx_packet->data); |
|
|
@ -147,10 +190,40 @@ void CommsClass::HandlePacket(sx127x_rx_packet_t *rx_packet) { |
|
|
|
last_packet_snr = rx_packet->snr; |
|
|
|
last_packet_snr = rx_packet->snr; |
|
|
|
xSemaphoreGive(mutex); |
|
|
|
xSemaphoreGive(mutex); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HandlePacket(rx_packet->data, rx_packet->data_len); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
void CommsClass::HandleRxData(const uint8_t *data, int size) { |
|
|
|
|
|
|
|
if (size <= 0) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
int i = 0; |
|
|
|
|
|
|
|
while (i < size) { |
|
|
|
|
|
|
|
if (packet_len <= 0) { |
|
|
|
|
|
|
|
packet_len = data[i++]; |
|
|
|
|
|
|
|
packet_buf.clear(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
int remaining = packet_len - packet_buf.size(); |
|
|
|
|
|
|
|
int available = size - i; |
|
|
|
|
|
|
|
int read = (remaining > available) ? available : remaining; |
|
|
|
|
|
|
|
packet_buf.append((const char *)&data[i], read); |
|
|
|
|
|
|
|
i += read; |
|
|
|
|
|
|
|
if (packet_buf.size() >= packet_len) { |
|
|
|
|
|
|
|
HandlePacket((uint8_t *)packet_buf.c_str(), packet_buf.size()); |
|
|
|
|
|
|
|
packet_buf.clear(); |
|
|
|
|
|
|
|
packet_len = -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CommsClass::HandlePacket(const uint8_t *data, size_t data_size) { |
|
|
|
|
|
|
|
using messages::GroundMessage; |
|
|
|
|
|
|
|
|
|
|
|
GroundMessage ground_message; |
|
|
|
GroundMessage ground_message; |
|
|
|
|
|
|
|
|
|
|
|
bool pb_ret = |
|
|
|
bool pb_ret = ground_message.ParseFromArray(data, data_size); |
|
|
|
ground_message.ParseFromArray(rx_packet->data, rx_packet->data_len); |
|
|
|
|
|
|
|
if (!pb_ret) { |
|
|
|
if (!pb_ret) { |
|
|
|
ESP_LOGE(TAG, "rx invalid protobuf"); |
|
|
|
ESP_LOGE(TAG, "rx invalid protobuf"); |
|
|
|
return; |
|
|
|
return; |
|
|
@ -174,8 +247,30 @@ void CommsClass::HandleCommand(const messages::GroundCommand &command) { |
|
|
|
messages::UGV_Message ugv_message; |
|
|
|
messages::UGV_Message ugv_message; |
|
|
|
ugv_message.set_command_ack(command.id()); |
|
|
|
ugv_message.set_command_ack(command.id()); |
|
|
|
|
|
|
|
|
|
|
|
std::string str = ugv_message.SerializeAsString(); |
|
|
|
SendPacket(ugv_message); |
|
|
|
sx127x_send_packet(lora, str.c_str(), str.size(), 0); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
esp_err_t CommsClass::SendPacket(const char *data, size_t size) { |
|
|
|
|
|
|
|
#ifdef COMMS_SX127X |
|
|
|
|
|
|
|
return sx127x_send_packet(lora, data, size, 0); |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
if (size >= MAX_PACKET_LEN) { |
|
|
|
|
|
|
|
ESP_LOGE(TAG, "SendPacket size too long: %d", size); |
|
|
|
|
|
|
|
return ESP_ERR_INVALID_SIZE; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
uint8_t sz = size; |
|
|
|
|
|
|
|
lora.Write(&sz, sizeof(sz)); |
|
|
|
|
|
|
|
return lora.Write((uint8_t *)data, size); |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
esp_err_t CommsClass::SendPacket(const std::string &str) { |
|
|
|
|
|
|
|
return SendPacket(str.c_str(), str.size()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
esp_err_t CommsClass::SendPacket(const google::protobuf::MessageLite &message) { |
|
|
|
|
|
|
|
std::string str = message.SerializeAsString(); |
|
|
|
|
|
|
|
return SendPacket(str); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} // namespace comms
|
|
|
|
} // namespace comms
|
|
|
|