TWAI/CAN am I using the functions correctly?
Posted: Sun Sep 12, 2021 6:29 pm
Hello!
I've been trying to communicate with a VESC motor controller using the TWAI on the ESP32 for a while now, i've based some code on the examples and picked what i thought was neccesary. Im able to add the config and filter (ACCEPT_ALL), install and start the driver without any errors. Then i just start a loop to read the the rx buffer with twai_receive(&message, pdMS_TO_TICKS(50)).
Im able to receive and print one message out of five that the VESC is sending. But then it seems like i only read the same message over and over. Can anyone confirm that I am using this code correctly? My final implementation will not look like this, but should this work to read the rx buffer? Seems like it does not remove the read message from the buffer after they have been read, but i have probably missed something.
I have run the TWAI Self Test Example which seemed to be working, so i think the hardware setup is fine also both the ID and the DATA that is being reported is something that could be expected, so the communication seems to work so far. Also if i power off the sending VESC, the communication stops, and when i power it on again it resumes printing to terminal, so that also seems to work.
Note: Ive already successfully communicated with a VESC with a Teensy and Arduino framework, so i know that this should or could work.
What am I missing? Any help is greatly appreciated!
Board: LOLIN32 V1.0.0 WROOM-32
ESP-IDF: Version 4.3.1
CAN transceiver: SN65HVD230
My code:
Output from terminal:
I've been trying to communicate with a VESC motor controller using the TWAI on the ESP32 for a while now, i've based some code on the examples and picked what i thought was neccesary. Im able to add the config and filter (ACCEPT_ALL), install and start the driver without any errors. Then i just start a loop to read the the rx buffer with twai_receive(&message, pdMS_TO_TICKS(50)).
Im able to receive and print one message out of five that the VESC is sending. But then it seems like i only read the same message over and over. Can anyone confirm that I am using this code correctly? My final implementation will not look like this, but should this work to read the rx buffer? Seems like it does not remove the read message from the buffer after they have been read, but i have probably missed something.
I have run the TWAI Self Test Example which seemed to be working, so i think the hardware setup is fine also both the ID and the DATA that is being reported is something that could be expected, so the communication seems to work so far. Also if i power off the sending VESC, the communication stops, and when i power it on again it resumes printing to terminal, so that also seems to work.
Note: Ive already successfully communicated with a VESC with a Teensy and Arduino framework, so i know that this should or could work.
What am I missing? Any help is greatly appreciated!
Board: LOLIN32 V1.0.0 WROOM-32
ESP-IDF: Version 4.3.1
CAN transceiver: SN65HVD230
My code:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_err.h"
#include "driver/twai.h"
//func declaration
void readTwai(uint32_t *counter);
void app_main()
{
//Initialize configuration structures using macro initializers
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_21, GPIO_NUM_22, TWAI_MODE_NORMAL);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
//Install TWAI driver
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK)
{
printf("Driver installed\n");
}
else
{
printf("Failed to install driver\n");
return;
}
//Start TWAI driver
if (twai_start() == ESP_OK)
{
printf("Driver started\n");
}
else
{
printf("Failed to start driver\n");
return;
}
uint32_t counter = 0;
while (1)
{
readTwai(&counter);
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void readTwai(uint32_t *counter)
{
twai_message_t message;
twai_status_info_t status;
twai_get_status_info(&status);
printf("buf before reading: %d\t", status.msgs_to_rx);
if (twai_receive(&message, pdMS_TO_TICKS(50)) == ESP_OK)
{
printf("buf after reading: %d\t", status.msgs_to_rx);
printf("Counter value: %d ", *counter);
printf("ID is %d data:[", message.identifier);
if (!(message.rtr))
{
for (int i = 0; i < message.data_length_code; i++)
{
printf("%d,", message.data[i]);
}
printf("]\n");
}
}
*counter += 1;
}
Output from terminal:
Code: Select all
buf before reading: 5 buf after reading: 5 Counter value: 4773 ID is 2356 data:[255,255,255,253,0,0,0,0,]
buf before reading: 5 buf after reading: 5 Counter value: 4774 ID is 2356 data:[255,255,255,253,0,0,0,0,]
buf before reading: 5 buf after reading: 5 Counter value: 4775 ID is 2356 data:[255,255,255,253,0,0,0,0,]
buf before reading: 5 buf after reading: 5 Counter value: 4776 ID is 2356 data:[255,255,255,253,0,0,0,0,]
buf before reading: 5 buf after reading: 5 Counter value: 4777 ID is 2356 data:[255,255,255,253,0,0,0,0,]
buf before reading: 5 buf after reading: 5 Counter value: 4778 ID is 2356 data:[255,255,255,253,0,0,0,0,]
buf before reading: 5 buf after reading: 5 Counter value: 4779 ID is 2356 data:[255,255,255,253,0,0,0,0,]
buf before reading: 5 buf after reading: 5 Counter value: 4780 ID is 2356 data:[255,255,255,253,0,0,0,0,]