Skip to content

Commit

Permalink
v3.0.1
Browse files Browse the repository at this point in the history
fix: fix datagram format

feat: add rate limiter to sender

fix: optimize log output

fix: ota update writes OTA_HOST to NVS if provided

fix: simplify ota update guard

fix: de-activate marker during ota

fix: try to fix corrupted NVS by adding a guard variable

fix: increase UART buffer len

feat: add submodule rfmarkit-interface-ng
  • Loading branch information
davidliyutong committed May 12, 2024
1 parent de85f15 commit 630718a
Show file tree
Hide file tree
Showing 22 changed files with 145 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "third_party/rfmarkit-interface-ng"]
path = third_party/rfmarkit-interface-ng
url = https://github.com/robotflow-initiative/rfmarkit-interface-ng
92 changes: 83 additions & 9 deletions components/apps/data_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,42 @@
#define CONFIG_UDP_RETRY 3

typedef struct {
uint8_t addr;
hi229_dgram_t dgram;
char device_id[12];
uint8_t checksum;
uint8_t addr;
uint32_t id; /* user defined ID */
float acc[3]; /* acceleration */
float gyr[3]; /* angular velocity */
float mag[3]; /* magnetic field */
float eul[3]; /* attitude: eular angle */
float quat[4]; /* attitude: quaternion */
float pressure; /* air pressure */
uint32_t timestamp;
int64_t time_us;
int64_t tsf_time_us;
uint32_t seq;
int32_t uart_buffer_len;
char device_id[12];
uint8_t checksum;
} marker_packet_t;

static const char *TAG = "app_data_client";
//static marker_packet_t dummy_packet = {
// .addr = 100,
// .id = 101,
// .acc = {1, 1, 1},
// .gyr = {2, 2, 2},
// .mag = {3, 3, 3},
// .eul = {4, 4, 4},
// .quat = {5, 5, 5, 5},
// .pressure = 6,
// .timestamp = 7,
// .time_us = 8,
// .tsf_time_us = 9,
// .seq = 10,
// .uart_buffer_len = 11,
// .device_id = "abcdabcdabcd",
// .checksum = 12
//};

static const char *TAG = "app.data_client ";

/**
* @brief Compute checksum of the data
Expand All @@ -40,7 +69,32 @@ static uint8_t compute_checksum(const uint8_t *data, size_t len) {
return sum;
}

_Noreturn void app_data_client(void *pvParameters) {
static void tag_packet(marker_packet_t * pkt, imu_dgram_t * imu_data) {
pkt->id = imu_data->imu[0].id;
memcpy(pkt->acc, imu_data->imu[0].acc, sizeof(pkt->acc));
memcpy(pkt->gyr, imu_data->imu[0].gyr, sizeof(pkt->gyr));
memcpy(pkt->mag, imu_data->imu[0].mag, sizeof(pkt->mag));
memcpy(pkt->eul, imu_data->imu[0].eul, sizeof(pkt->eul));
memcpy(pkt->quat, imu_data->imu[0].quat, sizeof(pkt->quat));
pkt->pressure = imu_data->imu[0].pressure;
pkt->timestamp = imu_data->imu[0].timestamp;
pkt->time_us = imu_data->time_us;
pkt->tsf_time_us = imu_data->tsf_time_us;
pkt->seq = imu_data->seq;
pkt->uart_buffer_len = imu_data->uart_buffer_len;
pkt->checksum = 0;

}

static void IRAM_ATTR read_timer_cb_handler(void *arg) {
ESP_LOGD(TAG, "timer_cb_handler called");
const char signal = 0;
xQueueSendFromISR((QueueHandle_t) arg, &signal, NULL);
}

static QueueHandle_t read_signal_queue = NULL;

_Noreturn void app_data_client(void *pvParameters) {
ESP_LOGI(TAG, "app_data_client started");

/** Get a ring buffer pointer **/
Expand All @@ -54,6 +108,18 @@ _Noreturn void app_data_client(void *pvParameters) {
udp_socket_t client = {0};
esp_err_t err;

/** Limit the read FPS **/
if (read_signal_queue == NULL) read_signal_queue = xQueueCreate(128, sizeof(char));
esp_timer_handle_t read_timer;
const esp_timer_create_args_t blink_timer_args = {
.callback = &read_timer_cb_handler,
/** name is optional, but may help identify the timer when debugging */
.name = "timeout",
.skip_unhandled_events = true,
.arg = read_signal_queue
};
ESP_ERROR_CHECK(esp_timer_create(&blink_timer_args, &read_timer));

while (1) {
/** Wait for the system to be active **/
if (!g_mcu.state.active || !g_mcu.state.ntp_synced || !g_mcu.state.discovery_completed) {
Expand All @@ -69,13 +135,18 @@ _Noreturn void app_data_client(void *pvParameters) {
goto handle_error;
}
}
os_delay_ms(500); // wait for uart_monitor to start
os_delay_ms(100); // wait for uart_monitor to start

int64_t curr_index = 1;
int64_t confirm_index = -1;

esp_timer_start_periodic(read_timer, 1000000 / g_mcu.target_fps);

imu_dgram_t imu_reading = {0};
char signal = 0;
while (g_mcu.state.active) {
/** If the queue is empty, sleep for a while **/
err = ring_buf_peek(serial_buf, &pkt.dgram, curr_index, &confirm_index);
err = ring_buf_peek(serial_buf, &imu_reading, curr_index, &confirm_index);

/** The ring buffer is empty or not ready **/
if (err != ESP_OK) {
Expand All @@ -85,7 +156,8 @@ _Noreturn void app_data_client(void *pvParameters) {
curr_index++; // increment the index

/** imu_reading is available **/
pkt.checksum = compute_checksum((uint8_t *) &pkt, sizeof(pkt) - sizeof(pkt.checksum));
tag_packet(&pkt, &imu_reading);
pkt.checksum = 0;

/** According to the document, udp send may fail because of memory issue, this is normal **/
for (int i = 0; i < CONFIG_UDP_RETRY; ++i) {
Expand All @@ -105,6 +177,8 @@ _Noreturn void app_data_client(void *pvParameters) {
/** If the packet is not sent, abort **/
if (err != ESP_OK) goto handle_error;

/** get the signal from the queue **/
xQueueReceive(read_signal_queue, &signal, portMAX_DELAY);
}

handle_error:
Expand Down
3 changes: 2 additions & 1 deletion components/apps/system_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

ESP_EVENT_DEFINE_BASE(SYSTEM_EVENTS);

static const char *TAG = "app_system_loop";
static const char *TAG = "app.system_loop ";

static void discovery_timer_cb(TimerHandle_t xTimer) {
/** Send discovery packets if Wi-Fi connected **/
Expand Down Expand Up @@ -104,6 +104,7 @@ _Noreturn void app_system_loop(void *pvParameters) {
if (bits & EV_SYS_OTA_TRIGGERED_BIT) {
ESP_LOGI(TAG, "OTA triggered");
esp_event_post_to(g_mcu.system_loop, SYSTEM_EVENTS, SYSTEM_OTA_EVENT, NULL, 0, portMAX_DELAY);
sys_set_operation_mode(false);
sys_ota_perform();
}

Expand Down
4 changes: 2 additions & 2 deletions components/apps/uart_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "imu.h"
#include "sys.h"

static const char *TAG = "app_uart_monitor";
static const char *TAG = "app.uart_monitor";

/**
* @brief Compute and set timestamp with us resolution
Expand All @@ -29,7 +29,7 @@ static void IRAM_ATTR tag_time_us(imu_dgram_t *imu_data) {
static void IRAM_ATTR tag_buffer_len(imu_dgram_t *imu_data) {
size_t uart_buffer_len = 0;
uart_get_buffered_data_len(g_imu.port, &uart_buffer_len);
imu_data->uart_buffer_len = (int) uart_buffer_len;
imu_data->uart_buffer_len = (int32_t) uart_buffer_len;
}

_Noreturn void app_uart_monitor(void *pvParameters) {
Expand Down
2 changes: 1 addition & 1 deletion components/ble/ble_srv.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static int blehr_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t blehr_addr_type;
static uint16_t conn_handle;

static const char *TAG = "ble.srv";
static const char *TAG = "ble.srv ";


static void blehr_advertise(void) {
Expand Down
2 changes: 1 addition & 1 deletion components/ble/gatt_srv.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "ble_srv.h"
#include "sys.h"

static const char *TAG = "ble.gatt_srv";
static const char *TAG = "ble.gatt_srv ";


static int gatt_svr_chr_wifi_info(
Expand Down
5 changes: 3 additions & 2 deletions components/blink/blink.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static int s_blink_idx; // Used in interrupt
blink_config_t g_blink_cfg;
static esp_timer_handle_t s_blink_timer;

static const char *TAG = "blink";
static const char *TAG = "blink ";

/**
* Function to get bit from byte array
Expand Down Expand Up @@ -200,7 +200,8 @@ static void blink_prepare_pattern() {
const esp_timer_create_args_t blink_timer_args = {
.callback = &blink_timeout,
/** name is optional, but may help identify the timer when debugging */
.name = "timeout"
.name = "timeout",
.skip_unhandled_events = true,
};

ESP_ERROR_CHECK(esp_timer_create(&blink_timer_args, &s_blink_timer));
Expand Down
2 changes: 1 addition & 1 deletion components/rest_controller/rest_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef struct rest_server_context {
char buffer[CONFIG_USER_CTX_BUFSIZE];
} rest_server_context_t;

static const char *TAG = "sys.controller";
static const char *TAG = "sys.rest ";

/**
----------------------------------------------------------------------------------
Expand Down
20 changes: 17 additions & 3 deletions components/rest_controller/rest_controller_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "blink.h"
#include "imu.h"

static const char *TAG = "rest_ctr_hdl";
static const char *TAG = "sys.rest.hdl ";

#define CONFIG_PARAM_VALUE_MAX_LEN 32
#define CONFIG_WEBSOCKET_FRAM_MAX_LEN 64
Expand Down Expand Up @@ -158,6 +158,8 @@ esp_err_t system_upgrade_handler(httpd_req_t *req) {
cJSON *root = cJSON_CreateObject();

char ota_host[CONFIG_PARAM_VALUE_MAX_LEN + 1] = {0};
mcu_var_t *p_var = NULL;
esp_err_t err;

switch (req->method) {
case HTTP_GET:
Expand All @@ -170,10 +172,21 @@ esp_err_t system_upgrade_handler(httpd_req_t *req) {

if (strlen(ota_host) > 0) {
strcpy(g_mcu.ota_host, ota_host);
p_var = sys_find_var(CONFIG_NVS_OTA_HOST_NAME, strlen(CONFIG_NVS_OTA_HOST_NAME));
if (p_var != NULL) {
err = sys_set_nvs_var(p_var, ota_host);
if (err != ESP_OK) {
cJSON_AddStringToObject(root, "status", "cannot set ota_host");
} else {
cJSON_AddStringToObject(root, "status", "ok");
}
} else {
cJSON_AddStringToObject(root, "status", "invalid name");
}
} else {
cJSON_AddStringToObject(root, "status", "ok");
}

set_sys_event(EV_SYS_OTA_TRIGGERED);
cJSON_AddStringToObject(root, "status", "ok");
break;
default:
cJSON_AddStringToObject(root, "status", "invalid method");
Expand Down Expand Up @@ -234,6 +247,7 @@ esp_err_t system_power_mgmt_handler(httpd_req_t *req) {
} else {
cJSON_AddStringToObject(root, "status", "invalid mode value");
}
break;
default:
cJSON_AddStringToObject(root, "status", "invalid method");
}
Expand Down
2 changes: 1 addition & 1 deletion components/sys/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static button_state_t button_state = IDLE;
static uint32_t press_timestamp;
static uint32_t release_timestamp;

static const char *TAG = "sys.button";
static const char *TAG = "sys.button ";

/**
* @brief Button ISR handler
Expand Down
2 changes: 1 addition & 1 deletion components/sys/discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static char discovery_msg_buffer[] = "\xe5\xe5\xe5\xe5\xe5\x01\x00""000000000000
static SemaphoreHandle_t sync_mutex = NULL;
static udp_socket_t client = {0};

static const char *TAG = "sys.discovery";
static const char *TAG = "sys.discovery ";

/**
* @brief Prepare discovery message buffer ONCE
Expand Down
2 changes: 1 addition & 1 deletion components/sys/led_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

static SemaphoreHandle_t sync_mutex = NULL;

static const char *TAG = "sys.led_status";
static const char *TAG = "sys.led_status ";

/**
* 1. Fast Flash When Wi-Fi is not connected
Expand Down
11 changes: 8 additions & 3 deletions components/sys/nvs_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "sys.h"
#include "settings.h"

static const char *TAG = "sys.nvs_mgmt";
static const char *TAG = "sys.nvs_mgmt ";

/** MCU vars **/
mcu_var_t g_mcu_vars[] = {
Expand All @@ -24,8 +24,8 @@ mcu_var_t g_mcu_vars[] = {
{.name = CONFIG_NVS_NTP_HOST_NAME, .type=VAR_STR},
{.name = CONFIG_NVS_TEST_NAME, .type =VAR_INT32},
{.name = CONFIG_NVS_IMU_BAUD_NAME, .type=VAR_INT32},
{.name = CONFIG_NVS_SEQ_NAME, .type=VAR_INT32}

{.name = CONFIG_NVS_SEQ_NAME, .type=VAR_INT32},
{.name = CONFIG_NVS_TARGET_FPS_NAME, .type=VAR_INT32},
};

static void sys_load_nvs_configuration(void);
Expand Down Expand Up @@ -186,18 +186,23 @@ static void sys_load_nvs_configuration() {
os_delay_ms(100);

char value_buffer[CONFIG_VAR_STR_MAX_LEN];
char _protect = 0;
sys_load_int32_conf(CONFIG_NVS_TEST_NAME, _protect, 0); // FIXME: a workaround to protect the following variable from getting corrupted
sys_load_str_conf(CONFIG_NVS_WIFI_SSID_NAME, g_mcu.wifi_ssid, CONFIG_WIFI_SSID);
sys_load_str_conf(CONFIG_NVS_WIFI_PSK_NAME, g_mcu.wifi_psk, CONFIG_WIFI_PSK);
sys_load_str_conf(CONFIG_NVS_DATA_HOST_NAME, g_mcu.data_host_ip_addr, CONFIG_DATA_HOST_IP_ADDR);
sys_load_str_conf(CONFIG_NVS_OTA_HOST_NAME, g_mcu.ota_host, CONFIG_OTA_HOST);
sys_load_str_conf(CONFIG_NVS_NTP_HOST_NAME, g_mcu.ntp_host_ip_addr, CONFIG_NTP_HOST_IP_ADDR);
sys_load_int32_conf(CONFIG_NVS_IMU_BAUD_NAME, g_mcu.imu_baud, CONFIG_IMU_BAUD);
sys_load_int32_conf(CONFIG_NVS_SEQ_NAME, g_mcu.seq, 0);
sys_load_int32_conf(CONFIG_NVS_TARGET_FPS_NAME, g_mcu.target_fps, CONFIG_TARGET_FPS);


/** Temporary Fix NVS invalid variable **/
g_mcu.imu_baud = MIN(g_mcu.imu_baud, 921600);
g_mcu.seq = MIN(g_mcu.seq, 255);
g_mcu.seq = MAX(g_mcu.seq, 0);
g_mcu.target_fps = MIN(g_mcu.target_fps, 200);

/** Macro Expansion Reference, DO NOT REMOVE **/
/**
Expand Down
2 changes: 1 addition & 1 deletion components/sys/power_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static SemaphoreHandle_t sync_mutex = NULL;
RTC_DATA_ATTR power_mgmt_ctx_t g_power_mgmt_ctx;
static TimerHandle_t power_save_timer = NULL;

static const char *TAG = "sys.pm";
static const char *TAG = "sys.pm ";

static void power_mgmt_handle_transition(TimerHandle_t xTimer);

Expand Down
12 changes: 11 additions & 1 deletion components/sys/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/** MCU structure **/
mcu_t g_mcu = {0};

static const char *TAG = "sys";
static const char *TAG = "sys ";

static esp_err_t esp_ota_event_handler(esp_http_client_event_t *evt) {
switch (evt->event_id) {
Expand Down Expand Up @@ -96,6 +96,7 @@ static bool sys_ota_uncommitted() {
**/
void sys_ota_guard() {
bool ota_uncommitted = sys_ota_uncommitted();
#ifdef CONFIG_OTA_GUARD_ENABLED
while (1) {
if (ota_uncommitted) {
if (g_mcu.state.wifi_connected) {
Expand All @@ -116,6 +117,15 @@ void sys_ota_guard() {
break;
}
}
#else
// TODO: sometimes the device cannot connect to Wi-Fi due to corrupted NVS
os_delay_ms(3000); // wait for the system to be ready)
if (ota_uncommitted) {
esp_ota_mark_app_valid_cancel_rollback();
ESP_LOGI(TAG, "[ota] firmware is working fine, cancel rollback");
esp_ota_mark_app_valid_cancel_rollback(); // cancel rollback
}
#endif
}

/**
Expand Down
1 change: 1 addition & 0 deletions components/sys/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ typedef struct {
int32_t use_hamming;
int32_t imu_baud;
int32_t seq;
int32_t target_fps;
} mcu_t;

typedef enum {
Expand Down
Loading

0 comments on commit 630718a

Please sign in to comment.