Files
agv_pro_ros2/scripts/hw_debug/robstride_rotation_watch.py
T

68 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Continuously poll mechpos for all 4 wheel motors and report per-ID deltas.
Used to identify which CAN ID (1-4) corresponds to which physical wheel corner:
run this while a human rotates one wheel at a time and watch which ID's mechpos
changes.
Usage:
python3 robstride_rotation_watch.py [duration_sec] [motor_ids...]
"""
import struct
import sys
import time
import can
HOST_ID = 0xAA
MSG_TYPE_READ_PARAM = 0x11
PARAM_MECHPOS = 0x7019
def read_param_request(motor_id, param_id):
arb_id = (MSG_TYPE_READ_PARAM << 24) | (HOST_ID << 8) | motor_id
data = struct.pack('<HHI', param_id, 0, 0)
return can.Message(arbitration_id=arb_id, data=data, is_extended_id=True)
def decode_response(msg):
motor_id = (msg.arbitration_id >> 8) & 0xFF
value = struct.unpack('<f', msg.data[4:8])[0]
return motor_id, value
def main():
args = sys.argv[1:]
duration = float(args[0]) if args else 8.0
motor_ids = [int(a) for a in args[1:]] or [1, 2, 3, 4]
bus = can.interface.Bus(channel='can0', interface='socketcan')
baseline = {}
try:
for mid in motor_ids:
bus.send(read_param_request(mid, PARAM_MECHPOS))
resp = bus.recv(timeout=0.5)
if resp is not None:
_, value = decode_response(resp)
baseline[mid] = value
print(f"Baseline: {baseline}")
print(f"Polling for {duration:.0f}s - rotate the wheel now...")
start = time.time()
while time.time() - start < duration:
deltas = {}
for mid in motor_ids:
bus.send(read_param_request(mid, PARAM_MECHPOS))
resp = bus.recv(timeout=0.3)
if resp is None:
continue
_, value = decode_response(resp)
if mid in baseline:
deltas[mid] = value - baseline[mid]
moved = {mid: round(d, 3) for mid, d in deltas.items() if abs(d) > 0.05}
if moved:
print(f"[{time.time() - start:4.1f}s] moved: {moved}")
time.sleep(0.1)
finally:
bus.shutdown()
if __name__ == "__main__":
main()