ESP32 to ODrive Communication using CAN interface

Hi
I want to communication between esp32 and ODrive using CAN interface. However, I can’t control my motor for velocity control at ODrive .
Can someone help me ? Thanks a lot!

My config and code:

odrv0.axis0.config.can_node_id = 3
odrv0.axis1.config.can_node_id = 1
odrv0.can.set_baud_rate(500000)

#include <CAN.h>

void setup() {
    Serial.begin(115200);
    while (!Serial)
        ;

    Serial.println("CAN Sender");

    // start the CAN bus at 500 kbps
    if (!CAN.begin(500E3)) {
        Serial.println("Starting CAN failed!");
        while (1)
            ;
    }
}
// ID for ODrive
int canID = (0x003 << 5) + 0x00D;
// velocity set point
int setV = 20;
// CAN message
unsigned char stmp[6] = {setV, (setV >> 8), (setV >> 16), (setV >> 24), 0, 0};

void loop() {
    CAN.beginPacket(canID);
    CAN.write(stmp, 6);
    CAN.endPacket();

    delay(5000);

    // try to parse packet
    int packetSize = CAN.parsePacket();

    if (packetSize) {
        // received a packet
        Serial.print("Received ");

        if (CAN.packetExtended()) {
            Serial.print("extended ");
        }

        if (CAN.packetRtr()) {
            // Remote transmission request, packet contains no data
            Serial.print("RTR ");
        }

        Serial.print("packet with id 0x");
        Serial.print(CAN.packetId(), HEX);

        if (CAN.packetRtr()) {
            Serial.print(" and requested length ");
            Serial.println(CAN.packetDlc());
        } else {
            Serial.print(" and length ");
            Serial.println(packetSize);

            // only print packet data for non-RTR packets
            while (CAN.available()) {
                Serial.print((char)CAN.read());
            }
            Serial.println();
        }
        delay(5000);
        Serial.println();
    }
}

Serial monitor:

1 Like

Please note that the values for that message must be IEEE754 floating point, little-endian. https://docs.odriverobotics.com/can-protocol

Try

float vel = 20;
uint8_t stmp[6] = {0};
memcpy(stmp, &vel, sizeof(vel));

@Wetmelon
Thank you !
I solved the problem in your way!
:grinning:

hi ı want too use odrive with esp32 and canbus, ı read the heartbeat message but ı cant send message, ı try your code but just write can sender, my wiring is true, can you help me please

How do we do the hardware connection of odrive, ESP32, CAN SN65HVD234, and BLDC motor D6374 150kV ???