#!/usr/bin/env python3 """Briefly spin all 4 RobStride wheel motors "forward" at low speed, for visual validation. Replicates robstride_driver's raw CAN protocol (Enable/MotionControl/Stop frames) directly, since the ros2_control stack isn't built in this environment yet. Uses the RS01 model's official encode/decode limits (position +/-4pi rad, velocity +/-44 rad/s, effort +/-17 Nm, kp 0..500, kd 0..5) so the velocity encoding matches what robstride_ros2 itself would send. Per the rotation test, raw motor velocity sign for "forward" is mirrored left vs right (mirrored motor mounting): left motors (CAN ID 1,3) need a NEGATIVE raw velocity target, right motors (CAN ID 2,4) need POSITIVE - matching the xacro's direction=-1/+1 split. ALWAYS sends Stop to all motors on exit (including Ctrl-C / exceptions). Usage: python3 robstride_spin_test.py [speed_rad_s] [duration_sec] """ import struct import sys import time import can HOST_ID = 0xAA TYPE_MOTION_CONTROL = 1 TYPE_ENABLE = 3 TYPE_STOP = 4 # RS01 model encode/decode limits (robstride_ros2's motor_profiles for model="RS01") POSITION_MIN, POSITION_MAX = -4 * 3.141592653589793, 4 * 3.141592653589793 VELOCITY_MIN, VELOCITY_MAX = -44.0, 44.0 EFFORT_MIN, EFFORT_MAX = -17.0, 17.0 KP_MAX = 500.0 KD_MAX = 5.0 # CAN ID -> raw velocity sign needed for physical "forward" (see agv_can_serial_hw_notes.md) MOTOR_SIGNS = {1: -1.0, 2: 1.0, 3: -1.0, 4: 1.0} def encode_u16(value, minimum, maximum): value = max(minimum, min(maximum, value)) normalized = (value - minimum) / (maximum - minimum) return int(round(normalized * 65535.0)) def decode_u16(raw, minimum, maximum): return minimum + (raw / 65535.0) * (maximum - minimum) def put_be16(buf, offset, value): buf[offset] = (value >> 8) & 0xFF buf[offset + 1] = value & 0xFF def make_enable(motor_id): arb_id = (TYPE_ENABLE << 24) | (HOST_ID << 8) | motor_id return can.Message(arbitration_id=arb_id, data=bytes(8), is_extended_id=True) def make_stop(motor_id, clear_fault=False): arb_id = (TYPE_STOP << 24) | (HOST_ID << 8) | motor_id data = bytearray(8) data[0] = 1 if clear_fault else 0 return can.Message(arbitration_id=arb_id, data=bytes(data), is_extended_id=True) def make_motion_command(motor_id, position, velocity, effort, kp, kd): effort_raw = encode_u16(effort, EFFORT_MIN, EFFORT_MAX) arb_id = (TYPE_MOTION_CONTROL << 24) | (effort_raw << 8) | motor_id data = bytearray(8) put_be16(data, 0, encode_u16(position, POSITION_MIN, POSITION_MAX)) put_be16(data, 2, encode_u16(velocity, VELOCITY_MIN, VELOCITY_MAX)) put_be16(data, 4, encode_u16(kp, 0.0, KP_MAX)) put_be16(data, 6, encode_u16(kd, 0.0, KD_MAX)) return can.Message(arbitration_id=arb_id, data=bytes(data), is_extended_id=True) def decode_feedback(msg): if msg is None or not msg.is_extended_id or len(msg.data) != 8: return None msg_type = (msg.arbitration_id >> 24) & 0x1F if msg_type != 2: # kTypeFeedback return None area = (msg.arbitration_id >> 8) & 0xFFFF motor_id = area & 0xFF position = decode_u16((msg.data[0] << 8) | msg.data[1], POSITION_MIN, POSITION_MAX) velocity = decode_u16((msg.data[2] << 8) | msg.data[3], VELOCITY_MIN, VELOCITY_MAX) return motor_id, position, velocity def main(): args = sys.argv[1:] speed = float(args[0]) if len(args) > 0 else 2.0 duration = float(args[1]) if len(args) > 1 else 4.0 motor_ids = [1, 2, 3, 4] kd = 0.8 bus = can.interface.Bus(channel='can0', interface='socketcan') try: print(f"Enabling motors {motor_ids}...") for mid in motor_ids: bus.send(make_enable(mid)) time.sleep(0.1) print(f"Spinning forward at {speed:.1f} rad/s for {duration:.1f}s " f"(Ctrl-C to stop early)...") control_period = 0.05 # 50ms, well inside the 200ms default CAN watchdog start = time.time() while time.time() - start < duration: for mid in motor_ids: target_vel = MOTOR_SIGNS[mid] * speed bus.send(make_motion_command(mid, 0.0, target_vel, 0.0, 0.0, kd)) resp = bus.recv(timeout=control_period) fb = decode_feedback(resp) if fb: motor_id, position, velocity = fb print(f" motor {motor_id}: velocity={velocity:+.2f} rad/s " f"position={position:+.2f} rad") print("Ramping down to zero velocity...") ramp_start = time.time() while time.time() - ramp_start < 1.0: for mid in motor_ids: bus.send(make_motion_command(mid, 0.0, 0.0, 0.0, 0.0, kd)) time.sleep(control_period) except KeyboardInterrupt: print("Interrupted - stopping.") finally: print("Sending Stop to all motors.") for mid in motor_ids: bus.send(make_stop(mid)) bus.shutdown() if __name__ == "__main__": main()