set-commands

Example of the component adithya-187326/vesc-can v1.0.1
| Supported Targets | ESP32 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- |

# _VESC CAN Command Transmission Example_

This example demonstrates how to send motor commands to a VESC over CAN using the ESP-IDF VESC-CAN driver component.

It shows how to:

- Configure and create an on-chip TWAI node
- Send a current command with an off delay using `vesc_set_target_off_delay`
- Continuously send RPM commands using `vesc_set_target` at a fixed rate

## Configuration

This example uses the following Kconfig option from the component:
| Setting | Description | Default |
| ---------------- | ---------------------------- | ------- |
| `CONFIG_VESC_ID` | Target VESC CAN ID (0 - 255) | `0x60` |

Modify this using idf.py menuconfig → VESC CAN Configuration.

The VESC ID must match the value set in VESC Tool under App Settings → General → VESC ID.

The TWAI pins and bitrate are set directly in `app_main` via `node_config`, defaulting to GPIO 4 (TX), GPIO 2 (RX), and 1 Mbit/s. The bitrate must match the VESC's CAN baud rate.

## Expected Behavior

On boot, the ESP creates and enables the TWAI node, then:

```bash
I (xxx) TWAI: Driver instance started
I (xxx) TWAI: Controller instance started
```

The motor is then commanded to 2 A with a 10 second off delay, held for 15 seconds, after which the example enters a loop sending 1200 RPM every 100 ms.

**The motor will spin.** Secure the motor before running this example.

If the motor does not respond, confirm the VESC ID matches, the bitrate matches, and the motor is configured and detected in VESC Tool.

## Timeout Behavior

The VESC stops the motor if no CAN frame is received for longer than its configured timeout (0.5 s by default). This is why the example sends RPM commands in a loop rather than once. The 100 ms period (10 Hz) stays well inside that window; VESC documentation recommends a fixed rate such as 50 Hz for control commands.

The initial current command uses an off delay of 10 seconds, which keeps the current controller running even after the commanded current falls below the minimum.

## Filter Notes

The mask filter 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 |

The filter only affects reception. It is not required for transmission, and this example does not register a receive callback.

## 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`).
- Commands sent outside the VESC's configured limits are truncated to the limit rather than rejected.
- Available targets include `VESC_DUTY`, `VESC_CURRENT`, `VESC_CURRENT_BRAKE`, `VESC_RPM`, `VESC_POSITION`, and the relative and handbrake variants.
- The off delay parameter is only valid for `VESC_CURRENT` and `VESC_CURRENT_REL`.
- This example only performs transmission. For receiving status messages, see other examples.

To create a project from this example, run:

idf.py create-project-from-example "adithya-187326/vesc-can=1.0.1:set-commands"

or download archive (~3.47 KB)