Replaces Orin↔ESP32-S3 BALANCE CAN comms (0x300-0x303 / 0x400-0x401)
with binary serial framing over CH343 USB-CDC at 460800 baud.
Protocol matches bd-wim1 (sl-perception) exactly:
Frame: [0xAA][LEN][TYPE][PAYLOAD][CRC8-SMBUS]
CRC covers LEN+TYPE+PAYLOAD, big-endian multi-byte fields.
Commands (Orin→ESP32): HEARTBEAT/DRIVE/ESTOP/ARM/PID
Telemetry (ESP32→Orin): TELEM_STATUS, TELEM_VESC_LEFT (ID 56),
TELEM_VESC_RIGHT (ID 68), ACK/NACK
VESC CAN TWAI kept for motor control; drive commands from Orin
forwarded to VESCs via SET_RPM. Hardware note: SN65HVD230
rewired from IO43/44 to IO2/IO1 to free IO43/44 for CH343.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
37 lines
1.4 KiB
C
37 lines
1.4 KiB
C
#pragma once
|
||
/* vesc_can.h — VESC CAN TWAI driver for ESP32-S3 BALANCE (bd-66hx)
|
||
*
|
||
* VESC extended CAN ID: (packet_type << 8) | vesc_node_id
|
||
* Physical layer: TWAI peripheral → SN65HVD230 → 500 kbps shared bus
|
||
*/
|
||
|
||
#include <stdint.h>
|
||
#include <stdbool.h>
|
||
#include "freertos/FreeRTOS.h"
|
||
#include "freertos/queue.h"
|
||
|
||
/* ── VESC packet types ── */
|
||
#define VESC_PKT_SET_RPM 3u
|
||
#define VESC_PKT_STATUS 9u /* int32 erpm, int16 I×10, int16 duty×1000 */
|
||
#define VESC_PKT_STATUS_4 16u /* int16 T_fet×10, T_mot×10, I_in×10 */
|
||
#define VESC_PKT_STATUS_5 27u /* int32 tacho, int16 V_in×10 */
|
||
|
||
/* ── VESC telemetry snapshot ── */
|
||
typedef struct {
|
||
int32_t erpm; /* electrical RPM (STATUS) */
|
||
int16_t current_x10; /* phase current A×10 (STATUS) */
|
||
int16_t voltage_x10; /* bus voltage V×10 (STATUS_5) */
|
||
int16_t temp_mot_x10; /* motor temp °C×10 (STATUS_4) */
|
||
uint32_t last_rx_ms; /* esp_timer ms of last STATUS frame */
|
||
} vesc_state_t;
|
||
|
||
/* ── Globals (two VESC nodes: index 0 = VESC_ID_A=56, 1 = VESC_ID_B=68) ── */
|
||
extern vesc_state_t g_vesc[2];
|
||
|
||
/* ── API ── */
|
||
void vesc_can_init(void);
|
||
void vesc_can_send_rpm(uint8_t vesc_id, int32_t erpm);
|
||
|
||
/* RX task — pass tx_queue as arg; forwards STATUS frames to Orin over serial */
|
||
void vesc_can_rx_task(void *arg); /* arg = QueueHandle_t orin_tx_queue */
|