Browse Source

some more organization

ugv_io
Alex Mikhalev 6 years ago
parent
commit
0f06cd68e9
  1. 1
      main/CMakeLists.txt
  2. 77
      main/ugv_comms.c
  3. 11
      main/ugv_comms.h
  4. 13
      main/ugv_config.h
  5. 89
      main/ugv_main.c

1
main/CMakeLists.txt

@ -1,4 +1,5 @@
set(COMPONENT_SRCS "ugv_main.c" set(COMPONENT_SRCS "ugv_main.c"
"ugv_comms.c"
"u8g2_esp32_hal.c") "u8g2_esp32_hal.c")
set(COMPONENT_ADD_INCLUDEDIRS ".") set(COMPONENT_ADD_INCLUDEDIRS ".")
set(COMPONENT_REQUIRES "u8g2" "sx127x_driver") set(COMPONENT_REQUIRES "u8g2" "sx127x_driver")

77
main/ugv_comms.c

@ -0,0 +1,77 @@
#include "ugv_comms.h"
#include "ugv_config.h"
#include <esp_log.h>
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;
}
}

11
main/ugv_comms.h

@ -0,0 +1,11 @@
#pragma once
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "sx127x_driver.h"
sx127x_hndl lora;
TaskHandle_t ugv_comms_task_hndl;
void ugv_comms_init();

13
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

89
main/ugv_main.c

@ -1,40 +1,22 @@
#define U8X8_USE_PINS #define U8X8_USE_PINS
#include <driver/uart.h> #include <driver/uart.h>
#include <esp_log.h> #include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <string.h> #include <string.h>
#include <u8g2.h> #include <u8g2.h>
#include "sx127x_driver.h" #include "sx127x_driver.h"
#include "sx127x_registers.h" #include "sx127x_registers.h"
#include "u8g2_esp32_hal.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; 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; sx127x_hndl lora;
void loraOnReceive(int packetSize); void comms_task(void *params);
void loraTask(void *params);
TaskHandle_t lora_task_hndl;
sx127x_rx_packet_t rx_packet; sx127x_rx_packet_t rx_packet;
@ -53,8 +35,6 @@ void setup_oled(void) {
u8g2_SetPowerSave(&u8g2, false); u8g2_SetPowerSave(&u8g2, false);
} }
#define GPS_UART UART_NUM_1
const size_t GPS_BUF_SIZE = 1024; const size_t GPS_BUF_SIZE = 1024;
const char NMEA_OUTPUT_RMCGGA[] = const char NMEA_OUTPUT_RMCGGA[] =
@ -88,6 +68,7 @@ static void gps_task(void *arg) {
void setup_gps(void) { void setup_gps(void) {
uart_config_t gps_uart_config; uart_config_t gps_uart_config;
esp_err_t ret;
// gps_uart_config.baud_rate = 9600; // gps_uart_config.baud_rate = 9600;
gps_uart_config.baud_rate = 57600; gps_uart_config.baud_rate = 57600;
gps_uart_config.data_bits = UART_DATA_8_BITS; gps_uart_config.data_bits = UART_DATA_8_BITS;
@ -113,76 +94,18 @@ void setup_gps(void) {
void setup(void) { void setup(void) {
ESP_LOGI(TAG, "setup"); ESP_LOGI(TAG, "setup");
esp_err_t ret;
setup_gps(); setup_gps();
setup_oled(); 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)); 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; int32_t lora_rssi;
uint8_t lora_lna_gain; uint8_t lora_lna_gain;
void loop(void) { void loop(void) {
ESP_LOGI(TAG, "loop");
u8g2_FirstPage(&u8g2); u8g2_FirstPage(&u8g2);
sx127x_recv_packet(lora, &rx_packet, 0); sx127x_recv_packet(lora, &rx_packet, 0);
sx127x_read_rssi(lora, &lora_rssi); sx127x_read_rssi(lora, &lora_rssi);

Loading…
Cancel
Save