diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 91db78e..9696840 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,4 +1,5 @@ set(COMPONENT_SRCS "ugv_main.c" + "ugv_comms.c" "u8g2_esp32_hal.c") set(COMPONENT_ADD_INCLUDEDIRS ".") set(COMPONENT_REQUIRES "u8g2" "sx127x_driver") diff --git a/main/ugv_comms.c b/main/ugv_comms.c new file mode 100644 index 0000000..6fa3e4c --- /dev/null +++ b/main/ugv_comms.c @@ -0,0 +1,77 @@ +#include "ugv_comms.h" +#include "ugv_config.h" + +#include + +static const char *TAG = "ugv_comms"; + +static void ugv_comms_task(void *arg); +static uint16_t packet_num; + +void ugv_comms_init() { + sx127x_config_t lora_config = SX127X_CONFIG_DEFAULT; + lora_config.sck_io_num = LORA_SCK; + lora_config.miso_io_num = LORA_MISO; + lora_config.mosi_io_num = LORA_MOSI; + lora_config.cs_io_num = LORA_CS; + lora_config.rst_io_num = LORA_RST; + lora_config.irq_io_num = LORA_IRQ; + lora_config.frequency = LORA_FREQ; + lora_config.tx_power = 17; + lora_config.spreading_factor = 12; + lora_config.signal_bandwidth = 10E3; + lora_config.sync_word = 0x34; + lora_config.crc = SX127X_CRC_ENABLED; + + esp_err_t ret; + + ret = sx127x_init(&lora_config, &lora); + if (ret != ESP_OK) { + const char *err_name = esp_err_to_name(ret); + ESP_LOGE(TAG, "LoRa init failed: %s", err_name); + return; + } + ESP_LOGI(TAG, "LoRa initialized"); + ret = sx127x_start(lora); + if (ret != ESP_OK) { + ESP_LOGI(TAG, "LoRa start failed: %d", ret); + return; + } + + packet_num = 0; + + 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; + while (true) { + TickType_t delay_ticks = next_send - current_time; + vTaskDelay(delay_ticks); + current_time = xTaskGetTickCount(); + 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 + if (ret != ESP_OK) { + ESP_LOGE(TAG, "error sending packet: %d", ret); + } else { + ESP_LOGI(TAG, "lora wrote %d bytes", written_bytes); + } + + current_time = xTaskGetTickCount(); + next_send = current_time + send_period; + } +} diff --git a/main/ugv_comms.h b/main/ugv_comms.h new file mode 100644 index 0000000..c44db3f --- /dev/null +++ b/main/ugv_comms.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +#include "sx127x_driver.h" + +sx127x_hndl lora; +TaskHandle_t ugv_comms_task_hndl; + +void ugv_comms_init(); diff --git a/main/ugv_config.h b/main/ugv_config.h new file mode 100644 index 0000000..1ebbf44 --- /dev/null +++ b/main/ugv_config.h @@ -0,0 +1,13 @@ +#define LORA_SCK 5 +#define LORA_MISO 19 +#define LORA_MOSI 27 +#define LORA_CS 18 +#define LORA_RST 14 +#define LORA_IRQ 26 + +#define LORA_FREQ (433E6) + +#define OLED_H 64 +#define OLED_W 128 + +#define GPS_UART UART_NUM_1 diff --git a/main/ugv_main.c b/main/ugv_main.c index 57ac4ab..62cb229 100644 --- a/main/ugv_main.c +++ b/main/ugv_main.c @@ -1,40 +1,22 @@ #define U8X8_USE_PINS #include #include -#include -#include #include #include #include "sx127x_driver.h" #include "sx127x_registers.h" #include "u8g2_esp32_hal.h" +#include "ugv_comms.h" +#include "ugv_config.h" -const char *TAG = "uas-ugv"; +static const char *TAG = "ugv_main"; u8g2_t u8g2; -#define LORA_SCK 5 -#define LORA_MISO 19 -#define LORA_MOSI 27 -#define LORA_CS 18 -#define LORA_RST 14 -#define LORA_IRQ 26 - -#define LORA_FREQ (433E6) -#define LORA_BUF_LEN 64 - -#define OLED_H 64 -#define OLED_W 128 - -uint16_t packet_num; - sx127x_hndl lora; -void loraOnReceive(int packetSize); -void loraTask(void *params); - -TaskHandle_t lora_task_hndl; +void comms_task(void *params); sx127x_rx_packet_t rx_packet; @@ -53,8 +35,6 @@ void setup_oled(void) { u8g2_SetPowerSave(&u8g2, false); } -#define GPS_UART UART_NUM_1 - const size_t GPS_BUF_SIZE = 1024; const char NMEA_OUTPUT_RMCGGA[] = @@ -88,6 +68,7 @@ static void gps_task(void *arg) { void setup_gps(void) { uart_config_t gps_uart_config; + esp_err_t ret; // gps_uart_config.baud_rate = 9600; gps_uart_config.baud_rate = 57600; gps_uart_config.data_bits = UART_DATA_8_BITS; @@ -113,76 +94,18 @@ void setup_gps(void) { void setup(void) { ESP_LOGI(TAG, "setup"); - esp_err_t ret; setup_gps(); setup_oled(); + ugv_comms_init(); - sx127x_config_t lora_config = SX127X_CONFIG_DEFAULT; - lora_config.tx_power = 17; - lora_config.spreading_factor = 12; - lora_config.signal_bandwidth = 10E3; - lora_config.sync_word = 0x34; - lora_config.crc = SX127X_CRC_ENABLED; - - ret = sx127x_init(&lora_config, &lora); - if (ret != ESP_OK) { - const char *err_name = esp_err_to_name(ret); - ESP_LOGE(TAG, "LoRa init failed: %s", err_name); - } else { - ESP_LOGI(TAG, "LoRa initialized"); - ret = sx127x_start(lora); - if (ret != ESP_OK) { - ESP_LOGI(TAG, "LoRa start failed: %d", ret); - } - } memset(&rx_packet, 0, sizeof(rx_packet)); - - packet_num = 0; - - xTaskCreate(loraTask, "loraTask", 1024 * 10, NULL, 2, &lora_task_hndl); -} - -#define XO 10 - -void loraTask(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; - while (true) { - TickType_t delay_ticks = next_send - current_time; - vTaskDelay(delay_ticks); - current_time = xTaskGetTickCount(); - 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 - if (ret != ESP_OK) { - ESP_LOGE(TAG, "error sending packet: %d", ret); - } else { - ESP_LOGI(TAG, "lora wrote %d bytes", written_bytes); - } - - current_time = xTaskGetTickCount(); - next_send = current_time + send_period; - } } int32_t lora_rssi; uint8_t lora_lna_gain; void loop(void) { - ESP_LOGI(TAG, "loop"); u8g2_FirstPage(&u8g2); sx127x_recv_packet(lora, &rx_packet, 0); sx127x_read_rssi(lora, &lora_rssi);