Skip to content

Commit b7d1767

Browse files
authored
Drive the real arm through the carrier board (#45)
The ground station drives the real arm through the carrier board, the UNO R4 WiFi between the laptop's USB and the drives' CAN bus. drive_interface:=slcan opens it with slcan_bus.py, writes each tick's frames at once, reopens the port if the USB link drops or the carrier goes quiet, and shows the carrier's status and e-stop in a can.carrier row. A frame the carrier refuses goes out again on the next tick, and no more than 16 frames go unanswered, the size of the carrier's queue, so every answer matches its frame. Shutdown writes the stops at once and waits for their answers. arm_drives.yaml uses the CAD gear ratios (26:1 on the base, shoulder and elbow, 3:1 on the wrist motors) at CAN IDs 1-5 and 1 Mbit/s. mks_setup scans the bus, sets a drive's CAN ID and bit rate, and repeats the F7h broadcast stop until the carrier takes it. The controller link reports loss, jitter, gaps and round trip from ping and pong packets. scripts/macos.sh gains flash, drives and arm. The four-node vcan step now fails unless the test binary reports passed tests and no skips: this googletest has no --gtest_fail_if_skipped, which printed its help and exited 0 without running a test.
1 parent 8177ba7 commit b7d1767

36 files changed

Lines changed: 1915 additions & 156 deletions

‎.github/workflows/can_drives.yml‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ jobs:
3636
waybionic-can-drives python3 -m pytest -v -p no:cacheprovider
3737
src/waybionic_ground_station/waybionic_teleop/test/test_can_drives.py
3838
39-
# --gtest_fail_if_skipped turns a missing vcan0 into a red job instead of 11 silent skips.
39+
# The binary skips its cases when vcan0 is missing, so a skip, or no passed tests at all,
40+
# fails the job. This googletest has no --gtest_fail_if_skipped: an unknown flag prints
41+
# the help and exits 0 without running anything.
4042
- name: Run the four-node vcan integration test
41-
run: >
42-
docker run --rm --network host waybionic-can-drives
43-
/waybionic_ws/build/waybionic_can/test_vcan_four_node
44-
--gtest_fail_if_skipped
43+
run: |
44+
set -o pipefail
45+
docker run --rm --network host waybionic-can-drives \
46+
/waybionic_ws/build/waybionic_can/test_vcan_four_node | tee four_node.txt
47+
grep -Eq '^\[ PASSED \] [1-9][0-9]* tests?\.' four_node.txt
48+
! grep -q 'SKIPPED' four_node.txt

‎BuildInstructions.md‎

Lines changed: 129 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ can report exit code `-2`, indicating the requested SIGINT interruption.
296296

297297
`teleop:=true` drives the arm with a controller through simulated CAN drives;
298298
nothing is sent to hardware unless you choose a drive bus, as described in
299-
[Real MKS drives over CAN](#real-mks-drives-over-can). The controller mapping is in
300-
`waybionic_teleop/config/xbox_teleop.yaml`, and the placeholder joint-to-drive map
301-
(MKS SERVO42D/57D CAN IDs, gear ratios and the wrist differential) is in
299+
[Real arm](#real-arm-mks-drives-over-can). The controller mapping is in
300+
`waybionic_teleop/config/xbox_teleop.yaml`, and the joint-to-drive map (MKS
301+
SERVO42D/57D CAN IDs, the 26:1 and 3:1 gear ratios and the wrist differential) is in
302302
`waybionic_teleop/config/arm_drives.yaml`.
303303

304304
**Windows (Docker):** connect the controller, then start the teleop version of the
@@ -443,7 +443,7 @@ zero, including any cable or gear stop.
443443
In simulation, a zero-velocity joint command (including B and controller timeout)
444444
sends an MKS F5 frame with zero speed and zero acceleration to each moving drive.
445445
An out-of-range encoder target stops all drives instead of updating only part of
446-
the arm. The placeholder drive speed is capped at 300 RPM; the MKS manual warns
446+
the arm. The drive speed is capped at 600 RPM (`max_rpm`); the MKS manual warns
447447
against immediate software stops above 1000 RPM. Each drive also runs no faster
448448
than 1.5 times its commanded speed plus 1 RPM, and teleop never advances its
449449
targets by more than two updates at once, so after a pause the drives do not catch
@@ -504,34 +504,111 @@ Press **Ctrl+C** in its window to stop.
504504

505505
**Linux:** `ros2 launch waybionic_bringup ground_station.launch.py teleop:=true autoplay:=true`.
506506

507-
## Real MKS Drives over CAN
507+
## Real Arm (MKS Drives over CAN)
508508

509-
`drive_interface` sends the same frames to real MKS SERVO42D/57D drives through a
510-
[python-can](https://python-can.readthedocs.io/) interface. The drive map in
511-
`waybionic_teleop/config/arm_drives.yaml` is still a placeholder: check each CAN ID,
512-
gear ratio and direction before the first powered test, and start with the motors
513-
unloaded. Set every drive to the bitrate in that file (1 Mbit/s; the MKS default is
514-
500 kbit/s). On a real bus every drive joint needs a URDF limit before the host holds or
515-
moves anything (`unmodeled_joints` applies to simulation only), so remove the placeholder
516-
`tool` drive from the map, or give its joint a limit in the URDF.
509+
The controller plugs into the computer, not the Arduino. The computer runs the teleop,
510+
the Cartesian moves (inverse kinematics), the joint limits and collision checks, the
511+
RViz twin and the monitor, then sends MKS frames through the carrier: a UNO R4 WiFi with
512+
a TJA1051T transceiver running `waybionic_can/arduino/carrier_bridge`. The carrier only
513+
passes frames between USB and the CAN bus, and reports its own status.
517514

518-
The computer running ROS needs the USB CAN adapter. Docker Desktop on Windows and
519-
macOS cannot reach USB devices, so use native ROS on Linux or macOS (RoboStack), or
520-
Docker on Linux with `--network host`.
515+
```text
516+
controller -> Mac (ROS 2) -> USB, 1 Mbaud -> carrier -> CAN, 1 Mbit/s -> 5 MKS drives
517+
```
521518

522-
Linux, SocketCAN adapter (candleLight firmware):
519+
`waybionic_teleop/config/arm_drives.yaml` maps the joints to the drives: CAN IDs 1-5
520+
(base yaw, shoulder, elbow, wrist left, wrist right), 26:1 cycloidal reducers on the
521+
first three joints and 3:1 belts on the two wrist motors. Its `tool` drive (CAN ID 6) is
522+
a placeholder with no hardware behind it. On a real bus every drive joint needs a URDF
523+
limit before the host holds or moves anything (`unmodeled_joints` applies to simulation
524+
only), and the host waits for every drive in the list, so remove `tool` from `drives`
525+
before driving the real arm. Start with the motors unloaded, and keep the physical E-stop
526+
within reach.
523527

524-
```bash
525-
sudo ip link set can0 up type can bitrate 1000000
526-
ros2 launch waybionic_bringup ground_station.launch.py teleop:=true drive_interface:=socketcan drive_channel:=can0
528+
Before the drives are first powered, Mechanical confirms each joint's signed travel from
529+
the zero pose against the URDF limits, including any cable or gear stop, and someone
530+
shows that the E-stop cuts power to the drives.
531+
532+
The computer running ROS needs the carrier or USB CAN adapter. Docker Desktop on Windows
533+
and macOS cannot reach USB devices, so use native ROS on macOS (RoboStack, below) or
534+
Linux, or Docker on Linux with `--network host` for a SocketCAN adapter.
535+
536+
### On a Mac
537+
538+
Do the [macOS setup](#macos-apple-silicon) first. Then, from the repository root:
539+
540+
1. Flash the carrier once (and after carrier firmware changes). The firmware comes with
541+
PR #40 (`waybionic_can`); until that is merged, `flash` stops and says so. Plug in
542+
only the carrier, so its port is the only `/dev/cu.usbmodem*`; otherwise set
543+
`WAYBIONIC_CARRIER=/dev/cu.usbmodem...`.
544+
545+
```bash
546+
brew install arduino-cli
547+
./scripts/macos.sh flash
548+
```
549+
550+
2. Set up each drive once, alone on the bus: give it its CAN ID (they all ship as 1),
551+
then switch it to 1 Mbit/s. The carrier keeps its first bit rate until it resets, so
552+
replug it, connect every drive, and check they all answer.
553+
554+
```bash
555+
./scripts/macos.sh drives set-id 1 3 # for example the elbow
556+
./scripts/macos.sh drives set-bitrate 3 1000000
557+
# after every drive is switched: replug the carrier
558+
./scripts/macos.sh drives --bitrate 1000000 scan
559+
```
560+
561+
3. With `tool` removed from `drives` in `arm_drives.yaml`, put the arm in the zero pose
562+
(upright, as RViz shows it before anything moves) and start the ground station. A
563+
controller plugged into the Mac (USB or Bluetooth) drives the arm and its twin
564+
together:
565+
566+
```bash
567+
./scripts/macos.sh arm
568+
```
569+
570+
4. In a second Terminal, zero the drives with teleop still disabled, then press Start on
571+
the controller:
572+
573+
```bash
574+
./scripts/macos.sh run ros2 service call /sim_arm_drives/zero std_srvs/srv/Trigger
575+
```
576+
577+
5. Check each joint's direction at the slowest speed in the `upper` group. If a drive
578+
turns the wrong way, negate its `factors` in `arm_drives.yaml` and rebuild. Then move
579+
each joint a known angle, such as 10 degrees in RViz, and measure it on the arm; a
580+
different angle means its `gear_ratio` in `arm_drives.yaml` is wrong.
581+
582+
**Remote operator.** The controller can stay with an operator on another computer. On
583+
the operator's Windows computer, run the controller bridge pointed at the Mac's address:
584+
585+
```powershell
586+
cd waybionic_teleop
587+
python -m waybionic_teleop.xinput_bridge --host 192.168.1.20
527588
```
528589

529-
macOS or Linux, serial-line (slcan) adapter:
590+
On the Mac, listen on the network:
530591

531592
```bash
532-
ros2 launch waybionic_bringup ground_station.launch.py teleop:=true drive_interface:=slcan drive_channel:=/dev/tty.usbmodem1101
593+
./scripts/macos.sh arm joy_source:=udp joy_udp_bind:=0.0.0.0
533594
```
534595

596+
The receiver takes controller packets from any computer that can reach its port, so do
597+
this only on a private network.
598+
599+
### The engineering monitor
600+
601+
With `teleop:=true` the monitor shows live rows:
602+
603+
| Row | Shows |
604+
| --- | --- |
605+
| `controller.link` | Remote controller only: packet rate, loss, jitter, longest gap and network round trip; warns past 2% loss, a 100 ms gap or a 50 ms round trip |
606+
| `teleop.input` | Whether controller input is arriving; teleop disables itself after 0.5 s without it |
607+
| `arm.command_gate` | Whether the drives take teleop's commands, or why they stopped |
608+
| `can.carrier` | The carrier's USB link (reopened every second if unplugged), E-stop and supply (once wired), CAN controller errors, and frames the carrier refused |
609+
| `can.bus` | Frames per second and bus load |
610+
| `drive.*`, `arm.*` | Each drive's reply state and encoder, each joint against its target, and whether the arm is zeroed |
611+
535612
**Zeroing.** The encoders count from where the drives were powered on, so the host
536613
publishes no joint states and moves nothing until the arm is zeroed. Put the arm in
537614
the zero pose (the pose RViz shows before anything moves), leave teleop disabled, and
@@ -542,16 +619,37 @@ ros2 service call /sim_arm_drives/zero std_srvs/srv/Trigger
542619
```
543620

544621
Then press Start. Real drives take commands through the same gate as the simulated ones.
545-
If a drive stops answering, for example because the E-stop cut its power, or its encoder
546-
count jumps further than `max_rpm` allows between two readings, as after a brief power
547-
loss, every drive stops and the arm must be zeroed again. The same happens when a drive
548-
reports a failed move (as when its stall protection releases the motor) or an end-limit
549-
stop, or stays more than `following_error_counts` from its target for
550-
`following_error_ticks` ticks; both are provisional values in `arm_drives.yaml`. If the
551-
joint commands stop for 0.5 s, every drive stops where it is, and if the host stops, the
622+
If a drive stops answering, for example because the E-stop cut its power or the carrier
623+
was unplugged, or its encoder count jumps further than `max_rpm` allows between two
624+
readings, as after a brief power loss, every drive stops and the arm must be zeroed
625+
again. The same happens when a drive reports a failed move (as when its stall protection
626+
releases the motor) or an end-limit stop, or stays more than `following_error_counts` from
627+
its target for `following_error_ticks` ticks; both are provisional values in
628+
`arm_drives.yaml`. Once Electrical wires the carrier's E-stop sense, pressing the E-stop
629+
also stops every drive at once, and Start is refused until it is released. If the joint
630+
commands stop for 0.5 s, every drive stops where it is, and if the host stops, the
552631
drives' heartbeat stops them. A stop frame the adapter refuses is sent again on every
553-
tick, and Start is refused until it has gone out. After any stop, disable teleop and press
554-
Start again.
632+
tick, and Start is refused until it has gone out. The carrier answers a frame it cannot
633+
pass on yet with BELL; the host sends a refused move, stop or setup frame again on the
634+
next tick, and a refused zero while the zero service waits. After any stop, disable
635+
teleop and press Start again.
636+
637+
### On Linux
638+
639+
With the carrier, set up the drives with `mks_setup` (the commands in step 2 above), then
640+
launch:
641+
642+
```bash
643+
ros2 run waybionic_teleop mks_setup --channel /dev/ttyACM0 scan
644+
ros2 launch waybionic_bringup ground_station.launch.py teleop:=true drive_interface:=slcan drive_channel:=/dev/ttyACM0
645+
```
646+
647+
A SocketCAN adapter (candleLight firmware):
648+
649+
```bash
650+
sudo ip link set can0 up type can bitrate 1000000
651+
ros2 launch waybionic_bringup ground_station.launch.py teleop:=true drive_interface:=socketcan drive_channel:=can0
652+
```
555653

556654
**Without hardware.** `mks_drive_sim` answers on a CAN interface the way the drives
557655
do, so the host can be tested end to end over a virtual CAN interface on Linux:

‎robostack.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ dependencies:
1212
- ros-jazzy-rviz-imu-plugin
1313
- ros-jazzy-xacro
1414
- ros-jazzy-joint-state-publisher-gui
15+
# A controller plugged into the Mac (joy_source:=device).
16+
- ros-jazzy-joy
1517
- colcon-common-extensions
1618
- compilers
1719
- cmake
@@ -20,6 +22,8 @@ dependencies:
2022
- ninja
2123
- pytest <9
2224
- python-can
25+
# The carrier's USB serial link to the real arm.
26+
- pyserial
2327
# colcon still relies on the legacy `develop` and `test` commands removed by
2428
# newer setuptools. Keep Python ROS packages editable and testable on macOS.
2529
- setuptools <72

‎scripts/macos.sh‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ Commands:
2020
setup Create/update the RoboStack environment and build the workspace
2121
build Build the workspace
2222
launch Launch the ground station visualization
23+
flash Compile and upload carrier_bridge to the UNO R4 WiFi (needs arduino-cli)
24+
drives Set up the MKS drives through the carrier, e.g. drives scan (see mks_setup)
25+
arm Drive the real arm: controller on this Mac, carrier on USB
2326
run Run any command inside the ROS workspace
27+
28+
The carrier is found as the only /dev/cu.usbmodem* port; set WAYBIONIC_CARRIER to choose.
2429
EOF
2530
}
2631

@@ -133,6 +138,41 @@ run_workspace() {
133138
' _ "$ROOT" "$@"
134139
}
135140

141+
find_carrier() {
142+
local ports
143+
144+
if [[ -n "${WAYBIONIC_CARRIER:-}" ]]; then
145+
[[ -e "$WAYBIONIC_CARRIER" ]] || fail "WAYBIONIC_CARRIER=$WAYBIONIC_CARRIER does not exist"
146+
CARRIER="$WAYBIONIC_CARRIER"
147+
return
148+
fi
149+
shopt -s nullglob
150+
ports=(/dev/cu.usbmodem*)
151+
shopt -u nullglob
152+
case "${#ports[@]}" in
153+
0) fail "no /dev/cu.usbmodem* port: plug in the carrier (UNO R4 WiFi)" ;;
154+
1) CARRIER="${ports[0]}" ;;
155+
*) fail "several ports (${ports[*]}); set WAYBIONIC_CARRIER to the carrier's" ;;
156+
esac
157+
}
158+
159+
flash_carrier() {
160+
local fqbn="arduino:renesas_uno:unor4wifi"
161+
local sketch="$ROOT/waybionic_can/arduino/carrier_bridge"
162+
local library="$ROOT/waybionic_can/arduino/libraries/WaybionicCan"
163+
164+
[[ -f "$sketch/carrier_bridge.ino" ]] \
165+
|| fail "carrier_bridge is not in this checkout yet: it comes with PR #40 (waybionic_can)"
166+
command -v arduino-cli >/dev/null 2>&1 \
167+
|| fail "install arduino-cli first: brew install arduino-cli"
168+
find_carrier
169+
arduino-cli core update-index
170+
arduino-cli core install arduino:renesas_uno
171+
arduino-cli compile --fqbn "$fqbn" --library "$library" "$sketch"
172+
arduino-cli upload --fqbn "$fqbn" --port "$CARRIER" "$sketch"
173+
echo "Flashed carrier_bridge to $CARRIER"
174+
}
175+
136176
command_name="${1:-help}"
137177
if [[ $# -gt 0 ]]; then
138178
shift
@@ -158,6 +198,24 @@ case "$command_name" in
158198
run_workspace env RMW_IMPLEMENTATION="${RMW_IMPLEMENTATION:-rmw_cyclonedds_cpp}" \
159199
ros2 launch waybionic_bringup ground_station.launch.py "$@"
160200
;;
201+
flash)
202+
check_host
203+
flash_carrier
204+
;;
205+
drives)
206+
[[ $# -gt 0 ]] || fail "drives needs an mks_setup command, such as scan"
207+
prepare
208+
find_carrier
209+
run_workspace ros2 run waybionic_teleop mks_setup --channel "$CARRIER" "$@"
210+
;;
211+
arm)
212+
prepare
213+
find_carrier
214+
echo "Carrier: $CARRIER"
215+
run_workspace env RMW_IMPLEMENTATION="${RMW_IMPLEMENTATION:-rmw_cyclonedds_cpp}" \
216+
ros2 launch waybionic_bringup ground_station.launch.py teleop:=true \
217+
joy_source:=device drive_interface:=slcan drive_channel:="$CARRIER" "$@"
218+
;;
161219
run)
162220
[[ $# -gt 0 ]] || fail "run requires a command"
163221
prepare

‎waybionic_bringup/launch/ground_station.launch.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,13 @@ def generate_launch_description():
172172

173173
drive_interface_arg = DeclareLaunchArgument(
174174
'drive_interface', default_value='sim',
175-
description='Drive bus: sim for simulated drives, or a python-can interface such as '
176-
'socketcan or slcan for the real MKS drives')
175+
description='Drive bus: sim for simulated drives, slcan for the WayBionic carrier on '
176+
'a USB serial port, or a python-can interface such as socketcan')
177177

178178
drive_channel_arg = DeclareLaunchArgument(
179179
'drive_channel', default_value='',
180-
description='CAN channel for a real drive bus, such as can0 or /dev/ttyACM0')
180+
description='The carrier serial port or CAN channel, such as /dev/cu.usbmodem1101, '
181+
'/dev/ttyACM0 or can0')
181182

182183
joy_source_arg = DeclareLaunchArgument(
183184
'joy_source', default_value='device',

‎waybionic_bringup/test/test_teleop_launch.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,17 @@ def hold(seconds, *pressed, **axes):
137137
sequence += 1
138138
sender.sendto(gamepad.pack(sequence, packet_axes, buttons), ('127.0.0.1', PORT))
139139
rclpy.spin_once(node, timeout_sec=0.02)
140-
try:
141-
rumbles.append(gamepad.unpack_rumble(sender.recv(64)))
142-
except BlockingIOError:
143-
pass
140+
while True:
141+
try:
142+
data, station = sender.recvfrom(64)
143+
except BlockingIOError:
144+
break
145+
# Answer the station's pings as the bridges do.
146+
answer = gamepad.pong(data)
147+
if answer is not None:
148+
sender.sendto(answer, station)
149+
else:
150+
rumbles.append(gamepad.unpack_rumble(data))
144151

145152
def group():
146153
# Only a report sent after the last press counts.
@@ -174,6 +181,8 @@ def group():
174181
self.assertTrue(rumbles, 'the controller never rumbled')
175182
self.assertGreater(rumbles[0], 0.0)
176183
self.assertEqual(rumbles[-1], 0.0)
184+
link = {value.key: value.value for value in diagnostics['controller.link'].values}
185+
self.assertNotEqual(link['round_trip_ms'], '', 'the link was never timed')
177186
finally:
178187
hold(0.2, 'b')
179188
sender.close()

‎waybionic_can/CMakeLists.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ if(BUILD_TESTING)
7676
endif()
7777
endforeach()
7878

79-
# Skips itself when the vcan interface is missing. The privileged can_drives.yml job
80-
# runs this binary with --gtest_fail_if_skipped so a missing vcan0 cannot pass CI.
79+
# Skips itself when the vcan interface is missing. The can_drives.yml job runs this binary
80+
# with vcan0 and fails if any case skips, so a missing vcan0 cannot pass CI.
8181
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
8282
ament_add_gtest(test_vcan_four_node tests/integration/test_vcan_four_node.cpp TIMEOUT 240)
8383
if(TARGET test_vcan_four_node)

‎waybionic_can/README.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,9 @@ Here is what an exchange looks like. Receivers 1 and 3 log `ignored (addressed t
163163

164164
## Hardware status and TODOs
165165

166-
- **Set CAN ID and bitrate:** 8Bh and 8Ah are not implemented in the shared C++ subset. F7h
167-
emergency stop is implemented from MKS manual section 9.2.3. The Python `EMERGENCY_STOP`
168-
helper matches [PR #45](https://github.com/Waybionic/waybionic_ground_station/pull/45); if
169-
that PR lands first, keep main's copy of `emergency_stop()` and the F7h `sim_drives.py` branch.
166+
- **Set CAN ID and bitrate:** 8Bh and 8Ah are not implemented in the shared C++ subset; the
167+
Python `mks_can.py` has them for `mks_setup`. F7h emergency stop is implemented from MKS
168+
manual section 9.2.3.
170169
- **Broadcast ID 0 and other MKS commands:** not implemented in the C++ bench subset.
171170
- **Physical receiver actuator:** the bench receivers use `SoftwareActuator`. A hobby-servo `IActuator` would need a defined mapping from MKS axis counts to angles and of what READ_ENCODER reports.
172171
- **Carrier telemetry hardware:** status ID `0x7F0` reports unknown E-stop/supply plus CAN error

0 commit comments

Comments
 (0)