Python API Guide¶
Use the Python API when you want to script repeatable workflows, integrate with your own control loop, or build higher-level application logic.
Quick Start¶
import time
from damiao_motor import DaMiaoController
controller = DaMiaoController(channel="can0", bustype="socketcan")
motor = controller.add_motor(motor_id=0x01, feedback_id=0x00, motor_type="4340")
motor.enable()
motor.ensure_control_mode("MIT")
motor.send_cmd_mit(
target_position=0.0,
target_velocity=0.0,
stiffness=3.0,
damping=0.5,
feedforward_torque=0.0,
)
time.sleep(0.05) # allow background polling to receive feedback
print(motor.get_states())
motor.disable()
controller.shutdown()
Typical Workflow¶
- Create a
DaMiaoController. - Add one or more motors with
add_motor(...). - Enable motor(s) and call
ensure_control_mode(...)for the intended command mode. -
Send one mode-specific command:
- MIT mode:
send_cmd_mit(...) - POS_VEL mode:
send_cmd_pos_vel(...) - VEL mode:
send_cmd_vel(...) - FORCE_POS mode:
send_cmd_force_pos(...)
- MIT mode:
-
Read the latest state with
get_states().- These states are polled automatically by the controller in background after
add_motor(...).
- These states are polled automatically by the controller in background after
-
Disable motors and call
shutdown().
Feedback Handling Routine¶
When a motor is added to DaMiaoController, feedback handling is automatic.
add_motor(...)registers the motor and starts background polling.- The controller thread repeatedly calls
poll_feedback(). poll_feedback()drains pending frames with non-blockingrecv(timeout=0), extracts logical ID fromD[0], and dispatches frames toprocess_feedback_frame(...)on the matched motor.- The motor decoder updates runtime state (
status,pos,vel,torq,t_mos,t_rotor) and processes register replies if present. - Your code reads a snapshot via
get_states().
In normal use you do not need to call poll_feedback() manually.
Call shutdown() before exit to stop the polling thread cleanly.
Register Write vs Store¶
- Write: update runtime register values in RAM with
write_register(...). - Store: persist runtime values to flash with
store_parameters().
For semantics and register list, see Registers.