examples/peripherals/twai/cybergear/README.md
| Supported Targets | ESP32 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-H21 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
|---|
This example demonstrates how to control XiaoMi CyberGear motors 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.
[!NOTE] For simplicity, this console example can only control one motor.
Supported Targets table above)The TWAI bus connection requires a transceiver to convert between the ESP32's TWAI signals and the TWAI bus differential signals.
ESP32 Pin Transceiver CyberGear Motor
--------- ----------- ---------------
GPIO TX ---> CTX
GPIO RX <--- CRX
GND ---> GND GND <--- Separate power supply
VCC(3.3V) ---> VCC(3.3V) VCC (24V) <--- Separate power supply
CAN_H <--> CAN_H
CAN_L <--> CAN_L
--------- ------------ ---------------
Before project configuration and build, be sure to set the correct chip target using idf.py set-target <chip_name>.
idf.py set-target esp32
idf.py menuconfig
In the Example Configuration menu:
TWAI TX GPIO number - GPIO pin for TWAI TXTWAI RX GPIO number - GPIO pin for TWAI RXESP Node ID - TWAI node identifierMotor ID - Motor identifierMotor bitrate - Motor bitrateBuild the project and flash it to the board, then run monitor tool to view serial output:
idf.py build
idf.py -p PORT flash monitor
(Replace PORT with the name of the serial port to use.)
(To exit the serial monitor, type Ctrl-].)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
I (330) Cybergear: CyberGear node started, TX: GPIO0, RX: GPIO2
I (340) Cybergear: CyberGear console started
Type 'help' to get the list of commands.
Use UP/DOWN arrows to navigate through command history.
Press TAB when typing command name to auto-complete.
I (350) main_task: Returned from app_main()
cybergear>
The example provides an interactive console with the following commands:
# Enable motor
enable
# Disable motor and clear fault
disable
# Set control mode (0:Motion, 1:Location, 2:Speed, 3:Current)
mode <mode>
# Motion control
motion -t <torque> -l <location> -s <speed> -p <kp> -d <kd>
# Set motor speed (rad/s)
speed -s <speed>
# Set motor location (rad)
loc -s <location>
# Set motor current (A)
current -s <current>
# Set current location as zero
zero
# Set motor ID
setid <id>
# Get motor ID
getid
# Show motor status
status
cybergear> enable # Enable motor
cybergear> mode 1 # Set location control mode
cybergear> loc -s 1.57 # Set location to π/2 rad
cybergear> status # Check motor status
cybergear> mode 2 # Set speed control mode
cybergear> speed -s 5.0 # Set speed to 5 rad/s
cybergear> disable # Disable motor
Command setid: The motor ID can not be changed when the motor is running, please disable the motor and set motor id.
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.
The status command shows detailed motor information:
===== Motor Status =====
Motor ID: 127
Fault Status: 0
State: 2
Location: 0.125000
Speed: 2.000000
Torque: 0.300000
Temperature: 36.500000
========================
If Motor not responding
For any technical queries, please open an issue on GitHub. We will get back to you soon.