ESP-IDF and MAVLink UART communication problem

53 views Asked by At

I'm trying to move from the Arduino framework to ESP-IDF. I'm attempting to rewrite my code in ESP-IDF, and I'm encountering an issue. This is my code:

#define UART_BUF_SIZE 1024

void read_uart_task(void *pvParameters)
{
   
 // Create a buffer to store received data
    uint8_t data[UART_BUF_SIZE];
    // Create a MAVLink message and status structures
    mavlink_message_t msg;
    mavlink_status_t status;

    while (1)
    {
        
        memset(data, 0, sizeof(data));
        int len = uart_read_bytes(UART_MAV, data, UART_BUF_SIZE, 20 / portTICK_PERIOD_MS);
         
        if (len > 0)
        {
            gpio_set_level(LED_PIN, !gpio_get_level(LED_PIN));
            for (int i = 0; i < len; i++)  mavlink_parse_char(MAVLINK_COMM_0, data[i], &msg, &status);
                 
            switch (msg.msgid)
            {
                case MAVLINK_MSG_ID_HEARTBEAT:
                {
                    mavlink_heartbeat_t heartbeat;
                    mavlink_msg_heartbeat_decode(&msg, &heartbeat);
                    // Access the decoded fields
                    uint8_t type = heartbeat.type;
                    uint8_t autopilot = heartbeat.autopilot;
                    uint8_t base_mode = heartbeat.base_mode;
                    uint32_t custom_mode = heartbeat.custom_mode;
                    uint8_t system_status = heartbeat.system_status;

                    char output[512];  // Adjust the size accordingly
                    sprintf(output, "Heartbeat Data:\nType: %d\nAutopilot: %d\nBase Mode: %d\nCustom Mode: %lu\nSystem Status: %d \n\r",
                            type, autopilot, base_mode, custom_mode, system_status);
                    send_data_over_uart(output);
                     

                }
                break;

            default:

                break;
            }
        }
        
        vTaskDelay(200/portTICK_PERIOD_MS);
    }
  
}

And the output of this code is shown below, and I'm sure it's not correct because it erratically changes, and the given values don't make ANY sense. Moreover, using my previous code with Arduino.h works perfectly on the same hardware. I would be extremely happy if anybody could tell me anything about it.

Heartbeat Data:
Type: 0
Autopilot: 24
Base Mode: 30
Custom Mode: 0
System Status: 6

Heartbeat Data:
Type: 0
Autopilot: 0
Base Mode: 0
Custom Mode: 101095582
System Status: 0

Heartbeat Data:
Type: 0
Autopilot: 0
Base Mode: 0
Custom Mode: 101095582
System Status: 0

I've tried messing with variable types, changing UART parameters, and lowering the baud rate.

1

There are 1 answers

0
iggaru On

I'd try the following:

1.- Build your project with Print registers and halt (CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT) option instead of Print registers and reboot (CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT) which is default option. This way, you can capture errors from RTOS. see: Panic Handler section

2.- Then open a terminal in order to receive error messages. If RTOS is panic-ing, you'll know.

3.- Are you giving enough stack when you vTaskCreate read_uart_task? I can see some arrays there that perhaps are consuming too much stack.