51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Read RobStride wheel motor parameters over CAN (python-can, socketcan).
|
|
|
|
Prerequisites: can0 up at 1 Mbit/s, e.g.:
|
|
sudo ip link set can0 up type can bitrate 1000000
|
|
|
|
Usage:
|
|
python3 robstride_can_probe.py [motor_ids...]
|
|
"""
|
|
import struct
|
|
import sys
|
|
import can
|
|
|
|
HOST_ID = 0xAA
|
|
MSG_TYPE_READ_PARAM = 0x11
|
|
|
|
# RobStride parameter addresses of interest
|
|
PARAM_RUN_MODE = 0x7005
|
|
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
|
|
host_id = msg.arbitration_id & 0xFF
|
|
param_id = struct.unpack('<H', msg.data[0:2])[0]
|
|
value = struct.unpack('<f', msg.data[4:8])[0]
|
|
return motor_id, host_id, param_id, value
|
|
|
|
def main():
|
|
motor_ids = [int(a) for a in sys.argv[1:]] or [1, 2, 3, 4]
|
|
bus = can.interface.Bus(channel='can0', interface='socketcan')
|
|
try:
|
|
for mid in motor_ids:
|
|
bus.send(read_param_request(mid, PARAM_MECHPOS))
|
|
resp = bus.recv(timeout=0.5)
|
|
if resp is None:
|
|
print(f"Motor {mid}: no response")
|
|
continue
|
|
motor_id, host_id, param_id, value = decode_response(resp)
|
|
print(f"Motor {mid}: mechpos = {value:.4f} rad "
|
|
f"(param_id=0x{param_id:04X}, host_id=0x{host_id:02X})")
|
|
finally:
|
|
bus.shutdown()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|