Python SDK Reference
This is the comprehensive reference for themotorbridge Python SDK, covering installation, the Controller and Motor classes, vendor-specific APIs, state structures, and error handling.
Chinese version: reference.zh-CN.md
Table of Contents
- Installation
- Channel Format
- Controller Class
- Motor Class
- State and Data Structures
- Damiao-Specific API
- RobStride-Specific API
- Error Handling
- Unified Mode Mapping
- Example Programs
1. Installation
1.1 pip Install (Recommended)
motor_abi shared library and the ws_gateway binary for your platform.
After install, the gateway binary is typically at:
.../site-packages/motorbridge/bin/ws_gateway (or ws_gateway.exe on Windows).
Gateway launch command (added to PATH by pip):
- Keep loopback bind (
127.0.0.1) for local usage. - If you bind to non-loopback addresses (
0.0.0.0or host IP), exportMOTORBRIDGE_WS_TOKENbefore launch. - Clients must pass the token in
x-motorbridge-tokenorAuthorization: Bearer ....
1.2 Local Build from Source
Build the ABI shared library first:1.3 Local Wheel Build
Linux / macOS:1.4 Library Search Path
The SDK searches for themotor_abi shared library in the following order:
MOTORBRIDGE_LIBenvironment variable (if set).motorbridge/lib/inside the installed package.target/release/relative to the repository root (4 levels up fromabi.py).target/release/relative to the current working directory.- System library search via
ctypes.util.find_library("motor_abi").
AbiLoadError is raised.
2. Channel Format
For channel compatibility details (PCAN, slcan, CAN-FD, Damiao Serial Bridge), see channel-compat.md. Summary:- Linux SocketCAN: use interface names directly:
can0,can1,slcan0. Do not append@bitrate(e.g.,can0@1000000is invalid on Linux). - USB-serial CAN adapters: bring up
slcan0first: - CAN-FD: use
Controller.from_socketcanfd(channel)in the Python SDK, or--transport socketcanfdin the CLI. Required for Hexfellow motors. - Damiao Serial Bridge: use
Controller.from_dm_serial(serial_port, baud). Only available for Damiao motors. - Windows (PCAN):
can0/can1map toPCAN_USBBUS1/PCAN_USBBUS2. The@bitratesuffix is supported (e.g.,can0@1000000).
3. Controller Class
TheController manages a CAN bus (or serial bridge) and the motors attached to it.
3.1 Constructors
Controller(channel: str = "can0")
Creates a controller using standard SocketCAN (or PCAN on Windows).
CallError if the bus cannot be opened.
Controller.from_socketcanfd(channel: str = "can0")
Creates a controller using CAN-FD transport. Required for Hexfellow motors.
Controller.from_dm_serial(serial_port: str = "/dev/ttyACM0", baud: int = 921600)
Creates a controller using the Damiao serial bridge. Only works with Damiao motors.
3.2 Lifecycle Methods
| Method | Signature | Description |
|---|---|---|
close | close() -> None | Releases the Python-side handle. Call after shutdown. |
shutdown | shutdown() -> None | Shuts down the controller and performs cleanup (including device processing). Recommended for normal exit. |
close_bus | close_bus() -> None | Closes only the bus layer. For special manual lifecycle management. Does not replace shutdown. |
| Context manager | __enter__() -> Controller, __exit__(exc_type, exc, tb) -> None | On exit, calls shutdown() then close(). |
3.3 Broadcast Control
| Method | Signature | Description |
|---|---|---|
enable_all | enable_all() -> None | Broadcast enable to all motors added to this controller. Recommended to wait 100—300 ms after calling. |
disable_all | disable_all() -> None | Broadcast disable to all motors. Call before ending your program. |
poll_feedback_once | poll_feedback_once() -> None | Manually consumes one receive queue cycle and updates the state cache. Useful with request_feedback for an immediate refresh. |
3.4 Adding Motors
| Method | Signature | Description |
|---|---|---|
add_damiao_motor | add_damiao_motor(motor_id: int, feedback_id: int, model: str) -> Motor | Adds a Damiao motor. model examples: "4310", "4340P". |
add_myactuator_motor | add_myactuator_motor(motor_id: int, feedback_id: int, model: str) -> Motor | Adds a MyActuator motor. model example: "X8". |
add_robstride_motor | add_robstride_motor(motor_id: int, feedback_id: int, model: str) -> Motor | Adds a RobStride motor. model example: "rs-06". Default feedback ID: 0xFD. |
add_hexfellow_motor | add_hexfellow_motor(motor_id: int, feedback_id: int, model: str) -> Motor | Adds a Hexfellow motor. Requires CAN-FD transport. |
add_hightorque_motor | add_hightorque_motor(motor_id: int, feedback_id: int, model: str) -> Motor | Adds a HighTorque motor. |
add_*_motor methods raise CallError if the motor cannot be created.
3.5 Controller Method Reference Table
| Method | Purpose | Typical Sequence | Parameter Guidance |
|---|---|---|---|
Controller(channel) | Standard CAN controller | add_*_motor after | channel e.g. can0 |
Controller.from_dm_serial(port, baud) | Damiao serial bridge | add_damiao_motor after | port e.g. /dev/ttyACM0; baud typically 921600 |
Controller.from_socketcanfd(channel) | CAN-FD controller | add_hexfellow_motor after | channel e.g. can0 |
add_damiao_motor(mid, fid, model) | Bind a Damiao motor handle | enable_all / ensure_mode / send_* after | motor_id commonly 0x01--0x7F; feedback_id commonly motor_id + 0x10; model e.g. 4310/4340P |
enable_all() | Broadcast enable | Before ensure_mode | Wait 100—300 ms after |
disable_all() | Broadcast disable | End of program | No constraints |
poll_feedback_once() | Manual feedback refresh | With request_feedback | Not required when background polling is active |
shutdown() | Full controller shutdown | Normal exit | Also called by __exit__ |
close_bus() | Close bus layer only | Special manual lifecycle | Does not replace shutdown |
close() | Release Python handle | After shutdown | Also called by __exit__ |
4. Motor Class
TheMotor class represents a single motor attached to a controller. All motors (regardless of vendor) share the same base API.
4.1 Lifecycle
| Method | Signature | Description |
|---|---|---|
enable | enable() -> None | Enables the motor. Wait 100—300 ms after calling. |
disable | disable() -> None | Disables the motor. Call before ending control. |
close | close() -> None | Releases the motor handle. |
4.2 Mode Switching
ensure_mode(mode: Mode, timeout_ms: int = 1000) -> None
Switches the motor to the specified control mode and verifies the switch completed within timeout_ms.
Parameters:
mode: one ofMode.MIT,Mode.POS_VEL,Mode.VEL,Mode.FORCE_POS.timeout_ms: verification timeout in milliseconds.- Standard CAN: 150—300 ms recommended.
- dm-serial: 200—500 ms recommended (commonly 300).
4.3 Control Commands
All control commands assume the motor is enabled and the correct mode has been set viaensure_mode.
send_mit(pos, vel, kp, kd, tau) -> None
MIT (impedance) mode control. Five-parameter hybrid control.
| Parameter | Type | Unit | Description |
|---|---|---|---|
pos | float | rad | Target position |
vel | float | rad/s | Target velocity |
kp | float | dimensionless | Position stiffness gain (>= 0) |
kd | float | dimensionless | Velocity damping gain (>= 0) |
tau | float | Nm | Feedforward torque |
kp=2--10, kd=0.05--0.5, tau=0.1--1.0.
send_pos_vel(pos, vlim) -> None
Position control with velocity limit.
| Parameter | Type | Unit | Description |
|---|---|---|---|
pos | float | rad | Target position |
vlim | float | rad/s | Velocity limit (> 0, start from 0.5—2.0) |
send_vel(vel) -> None
Pure velocity control.
| Parameter | Type | Unit | Description |
|---|---|---|---|
vel | float | rad/s | Target velocity (start from small values) |
send_force_pos(pos, vlim, ratio) -> None
Position control with torque limit ratio.
| Parameter | Type | Unit | Description |
|---|---|---|---|
pos | float | rad | Target position |
vlim | float | rad/s | Velocity limit (> 0) |
ratio | float | 0—1 | Torque/current limit ratio (commonly 0.1—0.5). This is not precise constant-torque control. |
4.4 Feedback and State
request_feedback() -> None
Actively requests one frame of feedback from the motor.
poll_feedback_once() (on Controller)
Consumes one receive cycle and updates the internal state cache. Use together with request_feedback and get_state for the freshest readings.
get_state() -> MotorState | None
Returns the most recently cached motor state. Returns None if no feedback has been received yet. For a more reliable reading, call request_feedback() + poll_feedback_once() before get_state().
4.5 Maintenance Operations
| Method | Signature | Description |
|---|---|---|
clear_error | clear_error() -> None | Clears the motor fault state. Call before enable/ensure_mode during maintenance. |
set_zero_position | set_zero_position() -> None | Sets the current position as the zero-point reference. This does not move the motor to position 0. The core applies a fixed 20 ms settle time internally; there is no ms parameter in the Python API. Project rule: always call disable() before set_zero_position(). |
set_can_timeout_ms | set_can_timeout_ms(timeout_ms: int) -> None | Sets the CAN timeout parameter on the motor. timeout_ms > 0, commonly 100—2000. |
store_parameters | store_parameters() -> None | Persists current register values to non-volatile storage. Call after confirming register values are correct. |
4.6 Register Access (Generic)
These methods work for all motor types that support register-based parameter access.| Method | Signature | Description |
|---|---|---|
write_register_f32 | write_register_f32(rid: int, value: float) -> None | Writes a float value to the specified register. |
write_register_u32 | write_register_u32(rid: int, value: int) -> None | Writes an unsigned 32-bit integer value to the specified register. |
get_register_f32 | get_register_f32(rid: int, timeout_ms: int = 1000) -> float | Reads a float register. timeout_ms commonly 300—1000. |
get_register_u32 | get_register_u32(rid: int, timeout_ms: int = 1000) -> int | Reads an unsigned 32-bit integer register. timeout_ms commonly 300—1000. |
4.7 Motor Method Reference Table
| Method | Purpose | Typical Sequence | Parameter Guidance |
|---|---|---|---|
enable() | Single motor enable | After clear_error, before control | Wait 100—300 ms |
disable() | Single motor disable | End of control flow | No constraints |
clear_error() | Clear fault state | Before enable/ensure_mode | Recommended in maintenance |
set_zero_position() | Set current position as zero | Must disable first | No ms param; core uses fixed 20 ms |
ensure_mode(mode, timeout_ms) | Switch + verify control mode | Before send_* | Standard CAN: 150—300; dm-serial: 200—500 |
send_mit(pos, vel, kp, kd, tau) | MIT impedance control | After ensure_mode(Mode.MIT) | pos rad; vel rad/s; kp, kd >= 0; tau Nm |
send_pos_vel(pos, vlim) | Position + velocity limit | After ensure_mode(Mode.POS_VEL) | pos rad; vlim > 0 |
send_vel(vel) | Pure velocity | After ensure_mode(Mode.VEL) | vel rad/s |
send_force_pos(pos, vlim, ratio) | Position + torque limit | After ensure_mode(Mode.FORCE_POS) | ratio 0—1 |
request_feedback() | Request one feedback frame | Before poll_feedback_once + get_state | For fresh readings |
get_state() | Read cached state | After feedback | May return None |
set_can_timeout_ms(ms) | Set CAN timeout parameter | Maintenance flow | ms > 0 |
store_parameters() | Persist registers to flash | After confirmed register writes | Irreversible per session |
write_register_f32(rid, value) | Write f32 register | Optional store_parameters after | rid must be writable f32 |
write_register_u32(rid, value) | Write u32 register | Optional store_parameters after | rid must be writable u32 |
get_register_f32(rid, timeout_ms) | Read f32 register | Diagnostics / verification | timeout_ms 300—1000 |
get_register_u32(rid, timeout_ms) | Read u32 register | Diagnostics / verification | timeout_ms 300—1000 |
close() | Release motor handle | End of lifecycle | --- |
5. State and Data Structures
5.1 Mode (Enum)
5.2 MotorState (Dataclass)
Returned by motor.get_state(). This is a frozen dataclass.
MotorState does not contain the current control mode. To read the current mode, query the appropriate register (e.g., Damiao RID_CTRL_MODE = 10).
5.3 RegisterSpec (Dataclass)
Used for Damiao register metadata lookups.
6. Damiao-Specific API
6.1 Damiao Register Constants
Importable directly from the package:6.2 Damiao Register Reference Table
| RID | Variable | Type | Access | Range | Description |
|---|---|---|---|---|---|
| 0 | UV_Value | f32 | RW | (10.0, 3.4E38] | Under-voltage protection value |
| 1 | KT_Value | f32 | RW | [0.0, 3.4E38] | Torque coefficient |
| 2 | OT_Value | f32 | RW | [80.0, 200) | Over-temperature protection value |
| 3 | OC_Value | f32 | RW | (0.0, 1.0) | Over-current protection value |
| 4 | ACC | f32 | RW | (0.0, 3.4E38) | Acceleration |
| 5 | DEC | f32 | RW | [-3.4E38, 0.0) | Deceleration |
| 6 | MAX_SPD | f32 | RW | (0.0, 3.4E38] | Maximum speed |
| 7 | MST_ID | u32 | RW | [0, 0x7FF] | Feedback ID |
| 8 | ESC_ID | u32 | RW | [0, 0x7FF] | Receive ID |
| 9 | TIMEOUT | u32 | RW | [0, 2^32-1] | Timeout alarm time |
| 10 | CTRL_MODE | u32 | RW | [1, 4] | Control mode |
| 21 | PMAX | f32 | RW | (0.0, 3.4E38] | Position mapping range |
| 22 | VMAX | f32 | RW | (0.0, 3.4E38] | Speed mapping range |
| 23 | TMAX | f32 | RW | (0.0, 3.4E38] | Torque mapping range |
| 24 | I_BW | f32 | RW | [100.0, 10000.0] | Current loop control bandwidth |
| 25 | KP_ASR | f32 | RW | [0.0, 3.4E38] | Speed loop Kp |
| 26 | KI_ASR | f32 | RW | [0.0, 3.4E38] | Speed loop Ki |
| 27 | KP_APR | f32 | RW | [0.0, 3.4E38] | Position loop Kp |
| 28 | KI_APR | f32 | RW | [0.0, 3.4E38] | Position loop Ki |
| 29 | OV_Value | f32 | RW | TBD | Over-voltage protection value |
| 30 | GREF | f32 | RW | (0.0, 1.0] | Gear torque efficiency |
| 31 | Deta | f32 | RW | [1.0, 30.0] | Speed loop damping coefficient |
| 32 | V_BW | f32 | RW | (0.0, 500.0) | Speed loop filter bandwidth |
| 33 | IQ_c1 | f32 | RW | [100.0, 10000.0] | Current loop enhancement coefficient |
| 34 | VL_c1 | f32 | RW | (0.0, 10000.0] | Speed loop enhancement coefficient |
| 35 | can_br | u32 | RW | [0, 4] | CAN baud rate code |
6.3 High-Impact Register IDs
These registers have the most significant effect on motor behavior:21 PMAX,22 VMAX,23 TMAX(range limits)25 KP_ASR,26 KI_ASR,27 KP_APR,28 KI_APR(PID gains)4 ACC,5 DEC,6 MAX_SPD,9 TIMEOUT(motion limits)
6.4 Protection Register IDs
0 UV_Value(under-voltage)2 OT_Value(over-temperature)3 OC_Value(over-current)29 OV_Value(over-voltage)
6.5 Damiao Parameter Read/Write (Dedicated API)
In addition to the genericget_register_*/write_register_* methods, Damiao motors have dedicated parameter access methods:
damiao_get_param_f32(param_id: int, timeout_ms: int = 1000) -> float
Reads a Damiao float parameter by index.
damiao_get_param_u32(param_id: int, timeout_ms: int = 1000) -> int
Reads a Damiao unsigned 32-bit integer parameter by index.
damiao_write_param_f32(param_id: int, value: float) -> None
Writes a Damiao float parameter by index.
damiao_write_param_u32(param_id: int, value: int) -> None
Writes a Damiao unsigned 32-bit integer parameter by index.
6.6 Recommended Tuning Workflow
- Read old value:
get_register_f32(rid)orget_register_u32(rid). - Write new value:
write_register_f32(rid, value)orwrite_register_u32(rid, value). - Read back to verify.
- Persist:
store_parameters().
6.7 Built-in Register Metadata Usage
6.8 Damiao Control Modes in Detail
MIT (Mode.MIT)
Full-parameter hybrid impedance control. Method:send_mit(pos, vel, kp, kd, tau)
Parameters:
pos: target position (rad)vel: target velocity (rad/s)kp: position stiffness gain (dimensionless)kd: velocity damping gain (dimensionless)tau: feedforward torque (Nm)
kp=2--10, kd=0.05--0.5, tau=0.1--1.0.
POS_VEL (Mode.POS_VEL)
Position control with velocity limit. Method:send_pos_vel(pos, vlim)
Parameters:
pos: target position (rad)vlim: velocity limit (rad/s)
VEL (Mode.VEL)
Pure velocity control. Method:send_vel(vel)
Parameters:
vel: target velocity (rad/s)
FORCE_POS (Mode.FORCE_POS)
Position control with torque/current limit. Method:send_force_pos(pos, vlim, ratio)
Parameters:
pos: target position (rad)vlim: velocity limit (rad/s)ratio: torque limit ratio (0—1). This is not precise constant-torque control.
6.9 Damiao set_zero_position() Details
Command Semantics
set_zero_position() sets the current mechanical position as the zero-point reference. It does not move the motor to position 0. The underlying protocol sends the Damiao zero-set command frame (FF FF FF FF FF FF FF FE).
Internal Timing
The Python API signature isset_zero_position() -> None with no ms parameter. The core layer applies a fixed 20 ms settle time internally.
Project Rule (Important)
In this project, you must calldisable() before set_zero_position() for Damiao motors, especially over dm-serial. This significantly reduces the risk of register read timeouts after zeroing.
Recommended sequence:
disable()(ordisable_all())set_zero_position()enable()(orenable_all())ensure_mode(...)send_*()control
Error Recovery After Failure
Ifset_zero_position or subsequent operations fail:
disable()->clear_error()->enable()-> retryensure_mode().- If still failing, scan to confirm the motor is online with the expected IDs.
- If feedback frames arrive but register reads consistently fail, consider power-cycling the device.
6.10 Standard Execution Flow for Damiao
- Create controller and bind motor:
Controller(...)+add_damiao_motor(...). - Enable:
ctrl.enable_all(). - Switch/verify control mode:
motor.ensure_mode(...). - Send commands:
send_mit/send_pos_vel/send_vel/send_force_pos. - Read feedback as needed:
request_feedback()+poll_feedback_once()+get_state(). - Disable at end:
motor.disable()orctrl.disable_all().
6.11 Damiao CLI Commands
Scan
MIT Control
Maintenance
7. RobStride-Specific API
7.1 Adding a RobStride Motor
0xFD (runtime can fallback probe 0xFF/0xFE).
7.2 robstride_ping
motor_id + feedback_id. Returns (device_id, responder_id).
7.3 robstride_set_device_id
store_parameters() afterward to persist.
7.4 RobStride Parameter Read
RobStride provides typed parameter read methods:| Method | Signature | Description |
|---|---|---|
robstride_get_param_i8 | robstride_get_param_i8(param_id: int, timeout_ms: int = 1000) -> int | Read signed 8-bit parameter |
robstride_get_param_u8 | robstride_get_param_u8(param_id: int, timeout_ms: int = 1000) -> int | Read unsigned 8-bit parameter |
robstride_get_param_u16 | robstride_get_param_u16(param_id: int, timeout_ms: int = 1000) -> int | Read unsigned 16-bit parameter |
robstride_get_param_u32 | robstride_get_param_u32(param_id: int, timeout_ms: int = 1000) -> int | Read unsigned 32-bit parameter |
robstride_get_param_f32 | robstride_get_param_f32(param_id: int, timeout_ms: int = 1000) -> float | Read float parameter |
0x7005run_mode0x7016loc_ref0x7017limit_spd0x700Aspd_ref0x7019mechPos0x701BmechVel
7.5 RobStride Parameter Write
| Method | Signature | Description |
|---|---|---|
robstride_write_param_i8 | robstride_write_param_i8(param_id: int, value: int) -> None | Write signed 8-bit parameter |
robstride_write_param_u8 | robstride_write_param_u8(param_id: int, value: int) -> None | Write unsigned 8-bit parameter |
robstride_write_param_u16 | robstride_write_param_u16(param_id: int, value: int) -> None | Write unsigned 16-bit parameter |
robstride_write_param_u32 | robstride_write_param_u32(param_id: int, value: int) -> None | Write unsigned 32-bit parameter |
robstride_write_param_f32 | robstride_write_param_f32(param_id: int, value: float) -> None | Write float parameter |
7.6 RobStride Mode Mapping
| Unified Mode | RobStride Native |
|---|---|
Mode.MIT | Native MIT impedance control frame (pos/vel/kp/kd/tau all active) |
Mode.POS_VEL | Maps to native Position: run_mode=1, loc_ref(0x7016), limit_spd(0x7017) |
Mode.VEL | Native Velocity: run_mode=2, spd_ref(0x700A) |
Mode.FORCE_POS | Unsupported |
robstride_write_param_*), not a dedicated unified mode.
7.7 RobStride Scan (Python SDK)
7.8 RobStride Zero-Set Sequence
8. Error Handling
8.1 Exception Hierarchy
8.2 Exception Classes
MotorBridgeError
Base exception for all motorbridge Python SDK errors. Inherits from RuntimeError.
AbiLoadError
Raised when the libmotor_abi shared library cannot be found or loaded. The error message lists all searched paths.
CallError
Raised when an ABI call returns a non-zero status code. The error message includes the operation name and the underlying error text from the ABI layer.
8.3 Common Error Patterns
8.4 Error Recovery for Damiao
If a motor enters an error state:disable()->clear_error()->enable()-> retryensure_mode().- If still failing, scan to confirm the motor is online with the expected IDs.
- If feedback frames arrive but register reads consistently fail, consider power-cycling the device.
9. Unified Mode Mapping
| Unified Mode | Damiao | RobStride | Hexfellow | MyActuator | HighTorque |
|---|---|---|---|---|---|
Mode.MIT | Native MIT | Native MIT | Native MIT (mode 5) | Unsupported | Maps to native pos+vel+tqe |
Mode.POS_VEL | Native POS_VEL | Native Position (run_mode=1 + limit_spd(0x7017) + loc_ref(0x7016)) | Native POS_VEL (mode 1) | Position setpoint flow | Maps to native pos+vel+tqe |
Mode.VEL | Native VEL | Native Velocity | Unsupported | Native velocity setpoint | Native velocity command |
Mode.FORCE_POS | Native FORCE_POS | Unsupported | Unsupported | Unsupported | Maps to native pos+vel+tqe |
10. Example Programs
Themotorbridge package ships with example scripts in bindings/python/examples/.
Damiao Examples
| Script | Description |
|---|---|
python_wrapper_demo.py | Minimal MIT closed-loop demo |
full_modes_demo.py | Unified entry for all four Damiao modes |
scan_ids_demo.py | Damiao ID scanning |
pos_ctrl_demo.py | Single position target |
multi_motor_ctrl_demo.py | Multi-motor control with per-step SDK timing |
mit_pos_switch_demo.py | Multi-motor mode switching test (MIT -> POS_VEL) |
pos_repl_demo.py | Interactive position control |
pid_register_tune_demo.py | Register tuning |
damiao_maintenance_demo.py | Maintenance: clear_error, set_zero, timeout, feedback |
damiao_register_rw_demo.py | Register f32/u32 read/write + store |
damiao_dm_serial_demo.py | Damiao serial bridge transport demo |
dm_serial_mode_switch_200_demo.py | dm-serial continuous 4-mode switching (configurable loops per mode) |
dm_serial_status_like_cli_demo.py | dm-serial query current mode + key registers + live state |
dm_serial_leader_monitor_demo.py | dm-serial leader monitor (full state reporting + startup enable-all) |
RobStride Examples
| Script | Description |
|---|---|
robstride_wrapper_demo.py | Ping, velocity, position, MIT demos |