diff --git a/components/sx127x_driver/CMakeLists.txt b/components/sx127x_driver/CMakeLists.txt index 0b90858..15ac65e 100644 --- a/components/sx127x_driver/CMakeLists.txt +++ b/components/sx127x_driver/CMakeLists.txt @@ -1,7 +1,9 @@ set(COMPONENT_SRCS "sx127x_driver.c" - "sx127x_registers.c") + "sx127x_registers.c" + "sx127x_pb.c") set(COMPONENT_ADD_INCLUDEDIRS ".") +set(COMPONENT_REQUIRES "nanopb") register_component() diff --git a/components/sx127x_driver/Kconfig.projbuild b/components/sx127x_driver/Kconfig.projbuild index 255827b..aae3363 100644 --- a/components/sx127x_driver/Kconfig.projbuild +++ b/components/sx127x_driver/Kconfig.projbuild @@ -38,4 +38,8 @@ config SX127X_SPI_DMA_CHAN There are two DMA channels that can be used by the ESP32 SPI driver. One which is not in use must be picked (1 or 2). Set to 0 to disable SPI DMA. +config SX127X_USE_NANOPB + bool "Use nanopb" + default true + endmenu diff --git a/components/sx127x_driver/sx127x_driver.c b/components/sx127x_driver/sx127x_driver.c index 4aafb04..a57f859 100644 --- a/components/sx127x_driver/sx127x_driver.c +++ b/components/sx127x_driver/sx127x_driver.c @@ -7,11 +7,6 @@ const char *SX127X_TAG = "sx127x"; -typedef struct sx127x_packet { - char * data; - size_t data_len; -} sx127x_packet_t; - sx127x_config_t sx127x_config_default() { sx127x_config_t config = SX127X_CONFIG_DEFAULT; return config; diff --git a/components/sx127x_driver/sx127x_driver.h b/components/sx127x_driver/sx127x_driver.h index 9c7a8ab..203652d 100644 --- a/components/sx127x_driver/sx127x_driver.h +++ b/components/sx127x_driver/sx127x_driver.h @@ -100,3 +100,5 @@ esp_err_t sx127x_recv_packet(sx127x_hndl hndl, sx127x_rx_packet_t *packet_out, QueueHandle_t sx127x_get_recv_queue(sx127x_hndl hndl); void sx127x_packet_rx_free(sx127x_rx_packet_t *packet); + +#include "sx127x_pb.h" diff --git a/components/sx127x_driver/sx127x_internal.h b/components/sx127x_driver/sx127x_internal.h index 3e9c9b1..e23d0ce 100644 --- a/components/sx127x_driver/sx127x_internal.h +++ b/components/sx127x_driver/sx127x_internal.h @@ -69,6 +69,11 @@ typedef enum sx127x_task_state { SX127X_TASK_STOPPING = 2, } sx127x_task_state_t; +typedef struct sx127x_packet { + char * data; + size_t data_len; +} sx127x_packet_t; + typedef struct sx127x { sx127x_config_t config; spi_device_handle_t device_handle; diff --git a/components/sx127x_driver/sx127x_pb.c b/components/sx127x_driver/sx127x_pb.c new file mode 100644 index 0000000..0af1535 --- /dev/null +++ b/components/sx127x_driver/sx127x_pb.c @@ -0,0 +1,36 @@ +#include "sx127x_pb.h" +#include "sx127x_internal.h" + +#include "sdkconfig.h" + +#include + +#ifdef CONFIG_SX127X_USE_NANOPB + +esp_err_t sx127x_send_packet_pb(sx127x_hndl hndl, const pb_field_t fields[], + void *src_struct, TickType_t ticks_to_wait) { + SX127X_CHECK(atomic_load(&hndl->task_state) == SX127X_TASK_RUNNING, + "task not running", ESP_ERR_INVALID_STATE); + + size_t data_len; + bool pb_ret = pb_get_encoded_size(&data_len, fields, src_struct); + SX127X_CHECK(pb_ret == true, "pb_get_encoded_size fail", ESP_ERR_INVALID_ARG); + SX127X_CHECK(data_len < SX127_MAX_PACKET_LEN, "packet len too long: %d", + ESP_ERR_INVALID_ARG, data_len); + + pb_byte_t *data = heap_caps_malloc( + data_len, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT | MALLOC_CAP_DMA); + SX127X_CHECK(data != NULL, "malloc error", ESP_ERR_NO_MEM); + pb_ostream_t ostream = pb_ostream_from_buffer(data, data_len); + pb_encode(&ostream, fields, src_struct); + ESP_LOGV(SX127X_TAG, "encoded pb message %d bytes", data_len); + + sx127x_packet_t packet; + packet.data_len = data_len; + packet.data = (char *)data; + BaseType_t pdRet = xQueueSend(hndl->tx_packet_queue, &packet, ticks_to_wait); + SX127X_CHECK(pdRet == pdTRUE, "tx queue full", ESP_ERR_TIMEOUT); + return ESP_OK; +} + +#endif diff --git a/components/sx127x_driver/sx127x_pb.h b/components/sx127x_driver/sx127x_pb.h new file mode 100644 index 0000000..f323944 --- /dev/null +++ b/components/sx127x_driver/sx127x_pb.h @@ -0,0 +1,14 @@ +#pragma once + +#include "sdkconfig.h" + +#ifdef CONFIG_SX127X_USE_NANOPB + +#include "sx127x_driver.h" + +#include + +esp_err_t sx127x_send_packet_pb(sx127x_hndl hndl, const pb_field_t fields[], + void *src_struct, TickType_t ticks_to_wait); + +#endif diff --git a/main/messages.proto b/main/messages.proto index 8e41f87..0e86b83 100644 --- a/main/messages.proto +++ b/main/messages.proto @@ -1,5 +1,7 @@ syntax = "proto3"; +package uas.ugv; + enum UGV_State { IDLE = 0; AQUIRING = 1; diff --git a/main/ugv_comms.c b/main/ugv_comms.c index 6fa3e4c..6bad373 100644 --- a/main/ugv_comms.c +++ b/main/ugv_comms.c @@ -2,10 +2,14 @@ #include "ugv_config.h" #include +#include +#include + +#include "messages.pb.h" static const char *TAG = "ugv_comms"; -static void ugv_comms_task(void *arg); +static void ugv_comms_task(void *arg); static uint16_t packet_num; void ugv_comms_init() { @@ -40,15 +44,22 @@ void ugv_comms_init() { packet_num = 0; - xTaskCreate(ugv_comms_task, "ugv_comms", 2 * 1024, NULL, 2, &ugv_comms_task_hndl); + xTaskCreate(ugv_comms_task, "ugv_comms", 2 * 1024, NULL, 2, + &ugv_comms_task_hndl); } void ugv_comms_task(void *params) { - char tx_buf[20]; - const size_t tx_buf_len = (sizeof(tx_buf) / sizeof(tx_buf[0])); - TickType_t send_period = pdMS_TO_TICKS(2000); - TickType_t current_time = xTaskGetTickCount(); - TickType_t next_send = current_time + send_period; + TickType_t send_period = pdMS_TO_TICKS(2000); + TickType_t current_time = xTaskGetTickCount(); + TickType_t next_send = current_time + send_period; + + uas_ugv_UGV_Message ugv_message = uas_ugv_UGV_Message_init_default; + ugv_message.which_ugv_message = uas_ugv_UGV_Message_status_tag; + ugv_message.ugv_message.status.location.fix_quality = 0; + ugv_message.ugv_message.status.location.latitude = 43.65; + ugv_message.ugv_message.status.location.longitude = -116.20; + ugv_message.ugv_message.status.location.altitude = 2730; + ugv_message.ugv_message.status.state = uas_ugv_UGV_State_IDLE; while (true) { TickType_t delay_ticks = next_send - current_time; vTaskDelay(delay_ticks); @@ -56,19 +67,14 @@ void ugv_comms_task(void *params) { if (current_time < next_send) { continue; } - int written_bytes = - snprintf(tx_buf, tx_buf_len, "hello world %d", packet_num); - if (written_bytes < 0) { - ESP_LOGE(TAG, "snprintf error: %d", written_bytes); - continue; - } packet_num++; - esp_err_t ret = sx127x_send_packet(lora, tx_buf, written_bytes, - 0); // 0 means error if queue full + esp_err_t ret = + sx127x_send_packet_pb(lora, uas_ugv_UGV_Message_fields, &ugv_message, + 0); // 0 means error if queue full if (ret != ESP_OK) { ESP_LOGE(TAG, "error sending packet: %d", ret); } else { - ESP_LOGI(TAG, "lora wrote %d bytes", written_bytes); + ESP_LOGI(TAG, "lora wrote UGV_Message packet"); } current_time = xTaskGetTickCount(); diff --git a/main/ugv_main.c b/main/ugv_main.c index 0034a7e..62cb229 100644 --- a/main/ugv_main.c +++ b/main/ugv_main.c @@ -9,9 +9,6 @@ #include "u8g2_esp32_hal.h" #include "ugv_comms.h" #include "ugv_config.h" -#include "messages.pb.h" - -#include static const char *TAG = "ugv_main";