|
| 1 | +| Supported Targets | ESP32 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-H21 | ESP32-P4 | ESP32-S2 | ESP32-S3 | |
| 2 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | |
| 3 | + |
| 4 | +# CyberGear Motor Control Example |
| 5 | + |
| 6 | +This example demonstrates how to control [XiaoMi CyberGear motors](https://www.mi.com/cyber-gear) using ESP32's TWAI interface. It provides a complete console interface for motor control with support for enable/disable, mode setting, speed and position control, and real-time status monitoring. |
| 7 | + |
| 8 | +> [!NOTE] |
| 9 | +> For simplicity, this console example can only control one motor. |
| 10 | +
|
| 11 | +## How to use example |
| 12 | + |
| 13 | +### Hardware Required |
| 14 | + |
| 15 | +* A development board with any supported Espressif SOC chip (see `Supported Targets` table above) |
| 16 | +* XiaoMi CyberGear motor |
| 17 | +* CAN transceiver (e.g., SN65HVD230) |
| 18 | +* Connecting wires |
| 19 | + |
| 20 | +### Hardware Connection |
| 21 | + |
| 22 | +The CAN bus connection requires a CAN transceiver to convert between the ESP32's TWAI signals and the CAN bus differential signals. |
| 23 | + |
| 24 | +``` |
| 25 | +ESP32 Pin CAN Transceiver CyberGear Motor |
| 26 | +--------- --------------- --------------- |
| 27 | +GPIO TX ---> CTX |
| 28 | +GPIO RX <--- CRX |
| 29 | +GND ---> GND GND <--- Separate power supply |
| 30 | +VCC(3.3V) ---> VCC(3.3V) VCC (24V) <--- Separate power supply |
| 31 | + CAN_H <--> CAN_H |
| 32 | + CAN_L <--> CAN_L |
| 33 | +--------- --------------- --------------- |
| 34 | +``` |
| 35 | + |
| 36 | +### Configure the project |
| 37 | + |
| 38 | +Before project configuration and build, be sure to set the correct chip target using `idf.py set-target <chip_name>`. |
| 39 | + |
| 40 | +```bash |
| 41 | +idf.py set-target esp32 |
| 42 | +idf.py menuconfig |
| 43 | +``` |
| 44 | + |
| 45 | +In the `Example Configuration` menu: |
| 46 | + |
| 47 | +* Set `TWAI TX GPIO number` - GPIO pin for TWAI TX |
| 48 | +* Set `TWAI RX GPIO number` - GPIO pin for TWAI RX |
| 49 | +* Set `ESP Node ID` - TWAI node identifier |
| 50 | +* Set `Motor ID` - Motor identifier |
| 51 | +* Set `Motor bitrate` - Motor bitrate |
| 52 | + |
| 53 | +### Build and Flash |
| 54 | + |
| 55 | +Build the project and flash it to the board, then run monitor tool to view serial output: |
| 56 | + |
| 57 | +```bash |
| 58 | +idf.py build |
| 59 | +idf.py -p PORT flash monitor |
| 60 | +``` |
| 61 | + |
| 62 | +(Replace PORT with the name of the serial port to use.) |
| 63 | + |
| 64 | +(To exit the serial monitor, type ``Ctrl-]``.) |
| 65 | + |
| 66 | +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. |
| 67 | + |
| 68 | +## Example Output |
| 69 | + |
| 70 | +``` |
| 71 | +I (330) Cybergear: CyberGear node started, TX: GPIO0, RX: GPIO2 |
| 72 | +I (340) Cybergear: CyberGear console started |
| 73 | +
|
| 74 | +Type 'help' to get the list of commands. |
| 75 | +Use UP/DOWN arrows to navigate through command history. |
| 76 | +Press TAB when typing command name to auto-complete. |
| 77 | +I (350) main_task: Returned from app_main() |
| 78 | +cybergear> |
| 79 | +``` |
| 80 | + |
| 81 | +## Console Commands |
| 82 | + |
| 83 | +The example provides an interactive console with the following commands: |
| 84 | + |
| 85 | +### Control Commands |
| 86 | + |
| 87 | +```bash |
| 88 | +# Enable motor |
| 89 | +enable |
| 90 | + |
| 91 | +# Disable motor and clear fault |
| 92 | +disable |
| 93 | + |
| 94 | +# Set control mode (0:Motion, 1:Location, 2:Speed, 3:Current) |
| 95 | +mode <mode> |
| 96 | + |
| 97 | +# Motion control |
| 98 | +motion -t <torque> -l <location> -s <speed> -p <kp> -d <kd> |
| 99 | + |
| 100 | +# Set motor speed (rad/s) |
| 101 | +speed -s <speed> |
| 102 | + |
| 103 | +# Set motor location (rad) |
| 104 | +loc -s <location> |
| 105 | + |
| 106 | +# Set motor current (A) |
| 107 | +current -s <current> |
| 108 | + |
| 109 | +# Set current location as zero |
| 110 | +zero |
| 111 | + |
| 112 | +# Set motor ID |
| 113 | +setid <id> |
| 114 | + |
| 115 | +# Get motor ID |
| 116 | +getid |
| 117 | + |
| 118 | +# Show motor status |
| 119 | +status |
| 120 | + |
| 121 | +``` |
| 122 | + |
| 123 | +### Usage Examples |
| 124 | + |
| 125 | +```bash |
| 126 | +cybergear> enable # Enable motor |
| 127 | +cybergear> mode 1 # Set location control mode |
| 128 | +cybergear> loc -s 1.57 # Set location to π/2 rad |
| 129 | +cybergear> status # Check motor status |
| 130 | +cybergear> mode 2 # Set speed control mode |
| 131 | +cybergear> speed -s 5.0 # Set speed to 5 rad/s |
| 132 | +cybergear> disable # Disable motor |
| 133 | +``` |
| 134 | + |
| 135 | +### Note |
| 136 | + |
| 137 | +Command **setid**: The motor ID can not be changed when the motor is running, please disable the motor and set motor id. |
| 138 | +Command **getid**: If you forget the motor ID you set previously, you can run getid cmd first. And then power off the motor and power it on again. Then it will broadcast its motor ID. |
| 139 | + |
| 140 | +### Technical Specifications |
| 141 | + |
| 142 | +- **TWAI Bitrate**: 125 Kbps, 250 Kbps, 500 Kbps or 1 Mbps (default 1 Mbps) |
| 143 | +- **Position Range**: ±12.5 rad |
| 144 | +- **Speed Range**: ±30.0 rad/s |
| 145 | +- **Torque Range**: ±12.0 Nm |
| 146 | +- **Current Range**: 0-23.0 A |
| 147 | + |
| 148 | + |
| 149 | +### Motor Status Display |
| 150 | + |
| 151 | +The `status` command shows detailed motor information: |
| 152 | + |
| 153 | +``` |
| 154 | +===== Motor Status ===== |
| 155 | +Motor ID: 127 |
| 156 | +Fault Status: 0 |
| 157 | +State: 2 |
| 158 | +Location: 0.125000 |
| 159 | +Speed: 2.000000 |
| 160 | +Torque: 0.300000 |
| 161 | +Temperature: 36.500000 |
| 162 | +======================== |
| 163 | +``` |
| 164 | + |
| 165 | +## Troubleshooting |
| 166 | + |
| 167 | +**If Motor not responding** |
| 168 | + |
| 169 | + - Check CAN transceiver connections |
| 170 | + - Verify motor ID settings are correct |
| 171 | + - Confirm CAN bus bitrate matches (1Mbps) |
| 172 | + - Check motor power supply |
| 173 | + |
| 174 | +For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon. |
0 commit comments