import time
from motorbridge import Controller
def scan_damiao(channel: str, start_id: int, end_id: int) -> list[dict]:
"""Scan for Damiao motors and return list of discovered motors."""
found = []
for motor_id in range(start_id, end_id + 1):
# Calculate expected feedback_id
feedback_id = 0x10 + (motor_id & 0x0F)
ctrl = Controller(channel)
try:
motor = ctrl.add_damiao_motor(motor_id, feedback_id, "4340P")
try:
# Try to read ESC_ID register
esc_id = motor.get_register_u32(8, timeout_ms=100)
mst_id = motor.get_register_u32(7, timeout_ms=100)
found.append({
"motor_id": motor_id,
"feedback_id": feedback_id,
"esc_id": esc_id,
"mst_id": mst_id
})
print(f"[hit] motor_id=0x{motor_id:02X} esc=0x{esc_id:X} mst=0x{mst_id:X}")
except Exception:
print(f"[.. ] motor_id=0x{motor_id:02X} no reply")
finally:
motor.close()
except Exception as e:
print(f"[err] motor_id=0x{motor_id:02X} {e}")
finally:
ctrl.close_bus()
ctrl.close()
return found
# Run scan
motors = scan_damiao("can0", 1, 20)
print(f"\nFound {len(motors)} motor(s)")
for m in motors:
print(f" motor_id=0x{m['motor_id']:02X} feedback_id=0x{m['feedback_id']:02X}")