Skip to content

Commit b39507b

Browse files
committed
feat(twai): add cybergear example
1 parent 47a659c commit b39507b

File tree

14 files changed

+1357
-0
lines changed

14 files changed

+1357
-0
lines changed

docs/en/api-reference/peripherals/twai.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ Application Examples
375375
- :example:`peripherals/twai/twai_utils` demonstrates how to use the TWAI (Two-Wire Automotive Interface) APIs to create a command-line interface for TWAI bus communication, supporting frame transmission/reception, filtering, monitoring, and both classic and FD formats for testing and debugging TWAI networks.
376376
- :example:`peripherals/twai/twai_error_recovery` demonstrates how to recover nodes from the bus-off state and resume communication, as well as bus error reporting, node state changes, and other event information.
377377
- :example:`peripherals/twai/twai_network` using 2 nodes with different roles: transmitting and listening, demonstrates how to use the driver for single and bulk data transmission, as well as configure filters to receive these data.
378+
- :example:`peripherals/twai/cybergear` demonstrates how to control XiaoMi CyberGear motors via TWAI interface.
378379

379380
API Reference
380381
-------------

docs/zh_CN/api-reference/peripherals/twai.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ TWAI控制器能够检测由于总线干扰产生的/损坏的不符合帧格式
375375
- :example:`peripherals/twai/twai_utils` 演示了如何使用 TWAI(Two-Wire Automotive Interface,双线汽车接口)API 创建一个命令行工具,用于 TWAI 总线通信,支持帧的发送/接收、过滤、监控,以及经典和 FD 格式,以便测试和调试 TWAI 网络。
376376
- :example:`peripherals/twai/twai_error_recovery` 演示了总线错误上报,节点状态变化等事件信息,以及如何从离线状态恢复节点并重新进行通信。
377377
- :example:`peripherals/twai/twai_network` 通过发送、监听, 2 个不同角色的节点,演示了如何使用驱动程序进行单次的和大量的数据发送,以及配置过滤器以接收这些数据。
378+
- :example:`peripherals/twai/cybergear` 演示了如何通过 TWAI 接口控制 XiaoMi CyberGear 电机。
378379

379380
API 参考
380381
--------

examples/peripherals/.build-test-rules.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,12 @@ examples/peripherals/touch_sensor/touch_sens_sleep:
503503
depends_components:
504504
- esp_driver_touch_sens
505505

506+
examples/peripherals/twai/cybergear:
507+
disable:
508+
- if: SOC_TWAI_SUPPORTED != 1
509+
depends_components:
510+
- esp_driver_twai
511+
506512
examples/peripherals/twai/twai_error_recovery:
507513
disable:
508514
- if: SOC_TWAI_SUPPORTED != 1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
7+
idf_build_set_property(MINIMAL_BUILD ON)
8+
project(cybergear_example)
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
idf_component_register(SRCS "cybergear_example_main.c" "cybergear.c" "cybergear_console.c"
2+
REQUIRES console esp_driver_twai
3+
INCLUDE_DIRS "."
4+
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
menu "Example Configuration"
2+
3+
orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"
4+
5+
config EXAMPLE_TWAI_TX_GPIO_NUM
6+
int "TX GPIO number"
7+
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
8+
default 0
9+
help
10+
This option selects the GPIO pin used for the TWAI TX signal. Connect the
11+
TWAI TX signal to your transceiver.
12+
13+
config EXAMPLE_TWAI_RX_GPIO_NUM
14+
int "RX GPIO number"
15+
range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX
16+
default 2
17+
help
18+
This option selects the GPIO pin used for the TWAI RX signal. Connect the
19+
TWAI RX signal to your transceiver.
20+
21+
config EXAMPLE_TWAI_ESP_NODE_ID
22+
int "TWAI ESP Node ID"
23+
range 0 256
24+
default 254
25+
help
26+
This option selects the ESP Node ID of the ESP Controller.
27+
28+
config EXAMPLE_TWAI_MOTOR_ID
29+
int "TWAI CyberGear motor ID"
30+
range 0 256
31+
default 127
32+
help
33+
This option selects the motor ID of the CyberGear. 127 is the factory value.
34+
Check the README if you don't know how to get the motor ID.
35+
36+
choice EXAMPLE_TWAI_MOTOR_BITRATE_CHOICE
37+
prompt "TWAI CyberGear motor bitrate"
38+
default EXAMPLE_TWAI_MOTOR_BITRATE_1M
39+
help
40+
This option selects the bitrate of the CyberGear.
41+
42+
config EXAMPLE_TWAI_MOTOR_BITRATE_1M
43+
bool "1M"
44+
config EXAMPLE_TWAI_MOTOR_BITRATE_500K
45+
bool "500K"
46+
config EXAMPLE_TWAI_MOTOR_BITRATE_250K
47+
bool "250K"
48+
config EXAMPLE_TWAI_MOTOR_BITRATE_125K
49+
bool "125K"
50+
endchoice
51+
52+
config EXAMPLE_TWAI_MOTOR_BITRATE
53+
int
54+
default 1000000 if EXAMPLE_TWAI_MOTOR_BITRATE_1M
55+
default 500000 if EXAMPLE_TWAI_MOTOR_BITRATE_500K
56+
default 250000 if EXAMPLE_TWAI_MOTOR_BITRATE_250K
57+
default 125000 if EXAMPLE_TWAI_MOTOR_BITRATE_125K
58+
59+
endmenu

0 commit comments

Comments
 (0)