| Supported Targets | ESP32 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | | ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # _VESC CAN Basic Reception Example_ This example demonstrates the minimal setup required to receive VESC status messages over CAN using the ESP-IDF VESC-CAN driver component. It shows how to: - Configure and create an on-chip TWAI node - Apply a mask filter to accept only VESC status frames - Register the component's receive callback to populate a `vesc_data_t` struct - Enable the controller ## Configuration This example uses the following Kconfig option from the component: | Setting | Description | Default | | ------------------------------- | ------------------ | -------- | | `CONFIG_VESC_CAN_BUS_FREQUENCY` | CAN bitrate (kHz) | `1000` | Modify this using idf.py menuconfig → VESC CAN Configuration. The TWAI pins are set directly in `app_main` via `node_config.io_cfg`, defaulting to GPIO 4 (TX) and GPIO 2 (RX). ## Expected Behavior On boot, the ESP creates the TWAI node, installs the filter, registers the callback, and enables the controller. You should see: ```bash I (xxx) TWAI: Controller instance started, and callback added for data reception from VESC. ``` Once a VESC on the bus begins sending status messages, `vesc_can_on_rx_cb` populates the `motor` struct from the ISR. Note that this example only sets up reception; it does not print the decoded values. If no frames arrive, check wiring, confirm the bitrate matches the VESC's CAN setting, and verify that CAN status messages are enabled in VESC Tool under App Settings → General → CAN Status Messages Rate. ## Filter Notes The mask filter in this example accepts extended IDs of the form `0x01x`: | Field | Value | Effect | | ------ | ------- | ------------------------------------------------- | | `id` | `0x010` | Target ID pattern | | `mask` | `0x000` | Upper 7 bits must match, lower 4 bits are ignored | Adjust or remove this filter depending on which VESC IDs and status messages you want to accept. ## Wiring | ESP Pin | CAN Transceiver Pin | Notes | | ------------ | ------------------- | ------------------------------------------ | | `GPIO_NUM_4` | TXD | TWAI transmit | | `GPIO_NUM_2` | RXD | TWAI receive | | 3.3V | VCC | Power (check transceiver voltage rating) | | GND | GND | Common ground with VESC | | — | CANH / CANL | To VESC CAN bus, 120Ω termination at ends | A CAN transceiver (SN65HVD230, TJA1051, or similar) is required between the ESP and the VESC. The ESP cannot drive the differential bus directly. ## Build and Run ```bash idf.py set-target <chip> idf.py build flash monitor ``` ## Notes - Requires ESP-IDF ≥ 5.5 for the node-based TWAI driver (`esp_twai.h` / `esp_twai_onchip.h`). - Set `flags.enable_listen_only = true` if the node should only observe the bus and never transmit. - The receive callback runs in ISR context. Keep any added processing short and defer heavy work to a task. - This example only performs reception setup. For sending commands (duty, current, RPM, position), see other examples.
To create a project from this example, run:
idf.py create-project-from-example "adithya-187326/vesc-can=1.0.1:feedback-receive"