import time
from motorbridge import Controller, Mode
DT_S = 0.02
MOTORS = ["m1", "m2", "m3", "m4"]
with Controller("can0") as ctrl:
motors = {
name: ctrl.add_damiao_motor(i+1, 0x10+i+1, "4340P")
for i, name in enumerate(MOTORS)
}
ctrl.enable_all()
for motor in motors.values():
motor.ensure_mode(Mode.MIT, 1000)
for i in range(1000):
t0 = time.time()
# Request all feedbacks first
for motor in motors.values():
motor.request_feedback()
# Single poll for all (efficient)
ctrl.poll_feedback_once()
# Read all states
states = {name: motor.get_state() for name, motor in motors.items()}
# Send commands based on states
for name, motor in motors.items():
state = states[name]
if state:
# State-based control
target = 0.5 if state.pos < 0.5 else -0.5
else:
target = 0.0
motor.send_mit(target, 0.0, 30.0, 1.0, 0.0)
# Maintain timing
elapsed = time.time() - t0
if elapsed < DT_S:
time.sleep(DT_S - elapsed)