Skip to content

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

  1. Create a DaMiaoController.
  2. Add one or more motors with add_motor(...).
  3. Enable motor(s) and call ensure_control_mode(...) for the intended command mode.
  4. Send one mode-specific command:

  5. Read the latest state with get_states().

    • These states are polled automatically by the controller in background after add_motor(...).
  6. Disable motors and call shutdown().

Feedback Handling Routine

When a motor is added to DaMiaoController, feedback handling is automatic.

  1. add_motor(...) registers the motor and starts background polling.
  2. The controller thread repeatedly calls poll_feedback().
  3. poll_feedback() drains pending frames with non-blocking recv(timeout=0), extracts logical ID from D[0], and dispatches frames to process_feedback_frame(...) on the matched motor.
  4. The motor decoder updates runtime state (status, pos, vel, torq, t_mos, t_rotor) and processes register replies if present.
  5. 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

For semantics and register list, see Registers.

Reference