TWAI/CAN am I using the functions correctly?

nhedlund
Posts: 7
Joined: Sun Sep 12, 2021 3:11 pm

TWAI/CAN am I using the functions correctly?

Postby nhedlund » 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:

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,]

nhedlund
Posts: 7
Joined: Sun Sep 12, 2021 3:11 pm

Re: TWAI/CAN am I using the functions correctly?

Postby nhedlund » Sat Sep 18, 2021 10:58 am

Ok, so by the number of answers here, im guessing that there is not that many people that uses twai/can.. :D

Anyway, i hooked up a picoscope to look at the canbus, and i could see that the esp32/transeiver never acknowledge the message (sets the ack field to zero). Im not sure if the ACK is something that is sent by the esp, or if its implemented in hw in the transeiver. I think that if the frame is not acked, the sender will just keep transmitting it until it is.

Im using the SN65HVD230 transeiver that i bought from aliexpress, and from what iv'e seen on the web there seem to be people having troubles with fakes or just bad batches(?). Maybe this is my issue, i have bought a 5v version mcp2515 that i've used in the past, i'll just have to level shift before hooking up to the esp io pins..

20210914_111705.jpg
20210914_111705.jpg (6.13 MiB) Viewed 6256 times

If anyone has had this issue, please let me know what the solution for you was. :)

nhedlund
Posts: 7
Joined: Sun Sep 12, 2021 3:11 pm

Re: TWAI/CAN am I using the functions correctly?

Postby nhedlund » Wed Sep 22, 2021 6:45 am

Hi again, if anyone else has this issue my problem seemed to be related to faulty SN65HVD230 transceivers that did not ACK the frames.
I changed my setup to a MCP2515 transceiver and a logic level shifter and the above code worked. So if you are having issues, maybe it's the transceiver, i bought mine on aliexpress so for all i know they might be fakes or a bad batch..

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: TWAI/CAN am I using the functions correctly?

Postby ESP_Dazz » Wed Sep 22, 2021 8:32 am

@nhedlund Seems like it's not the first time users have run into fake VP230 transceivers. Leaving the following to a related post in case anyone runs into the same issue (images included in discussion thread).

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 104 guests