First test of direct can control on the wheels
This commit is contained in:
1 parent
e317c10f7f
commit
3f12a62044
20 files changed
+1101
-127
No files matched your search
@@ -0,0 +1,50 @@
|
||||
#!/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()
|
||||
@@ -0,0 +1,67 @@
|
||||
#!/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()
|
||||
@@ -0,0 +1,135 @@
|
||||
#!/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()
|
||||
Reference in new issue
Block a user