This driver uses Espressif's TWAI (Two-Wire Automotive Interface) to communicate with Xiaomi CyberGear motors. It is based on the library Xiaomi_CyberGear_Arduino and has been ported to ESP-IDF.
The component is independent of the CAN driver. Configure cybergear_config_t::send
with an adapter that sends one extended CAN frame; send_context is passed through to
that adapter. Incoming frames are routed by the application and passed to
cybergear_process_message() as cybergear_message_t.
This library does not log or print errors; instead, it propagates all internal errors.
Consequently, every relevant function returns an esp_err_t value that should be
handled. During development, ESP_ERROR_CHECK(...) is helpful. However, because
these motors are powerful, errors should be resolved or the motors stopped by an
external emergency mechanism.
The motor provides a list of faults and warnings. If a fault is active, the motor sends a fault feedback frame. Since the implementation has not been fully tested and not every fault has occurred yet, the following list indicates whether a fault has been tested successfully:
-
overload -
uncalibrated -
over_current_phase_a -
over_current_phase_b -
over_current_phase_c -
over_voltage -
under_voltage -
driver_chip -
over_temperature -
magnetic_code_failure -
hall_coded_faults
-
over_temperature
idf.py add-dependency "cybergear-robotics/cybergear"- Create an example project.
idf.py create-project-from-example "cybergear-robotics/cybergear:position_test"- Go to the example directory, for example
position_test.cd position_test - Set the ESP target.
idf.py set-target esp32 - Configure CAN TX/RX in the
CyberGear Examplemenu.idf.py menuconfig - Build, flash, and monitor the project.
idf.py build flash monitor
If a fault occurs, the alarm can be cleared with
cybergear_stop_and_clear_faults, which corresponds to CMD_RESET with the
fault-clear flag set.
Register an on_error callback with twai_node_register_event_callbacks() to observe
transmission failures. These can have multiple reasons:
-
Too many messages are being sent, and the CAN TX queue is too small. Increasing the queue length may help:
twai_onchip_node_config_t node_config = { .io_cfg.tx = TX, .io_cfg.rx = RX, .bit_timing.bitrate = 1000000, .tx_queue_depth = 50, }; -
Another task, possibly an interrupt handler, may need to run periodically for CAN messages to continue being sent. This requires further investigation.