Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ROS 2 example

examples/ros2/ uses r2r Rust nodes to carry predsync frames over a best-effort topic. The nodes use the Rust core and generated telemetry wrappers; ROS 2 owns only the transport.

# codec self-test; does not require ROS 2
pixi run examples-ros2

# with a supported ROS 2 installation sourced
source /opt/ros/jazzy/setup.bash
pixi run examples-ros2-build
cargo run --manifest-path examples/ros2/Cargo.toml --features ros2 --bin talker
# in another shell
cargo run --manifest-path examples/ros2/Cargo.toml --features ros2 --bin listener

r2r builds Rust message types from the sourced ROS 2 installation, so no colcon package is needed. The example limits generation to std_msgs with IDL_PACKAGE_FILTER. The ros2 feature enables the r2r dependencies; the default feature set keeps the codec library testable without ROS 2.

The codec helper (examples/ros2/src/lib.rs) owns one encoder and one decoder, converts between generated Rust values and byte payloads, and is tested by round-tripping three frames and dropping a rejected frame.

The nodes publish and subscribe std_msgs.msg.UInt8MultiArray with:

  • BestEffort reliability;
  • volatile durability;
  • a queue depth of 5.

Best effort is appropriate because predsync already handles loss and does not benefit from ROS 2 retransmission. Each message contains exactly one predsync frame; Decoder::feed is never asked to find boundaries inside a stream.

The talker stops on Ctrl-C and prints median, mean and a 95% bootstrap confidence interval for predicted fields per frame, byte savings and percentage size reduction. A maximum/mean message-size line precedes the overall figure. The baseline is a fresh encoder at the same tick, i.e. the cost of sending every true value every time.

If you have several publishers, create one decoder per publisher topic or include a sender identity in your own message wrapper. The message ID in a predsync frame identifies a message type, not a sender.