This relay package is a generic ROS2 C++ node that subscribes to a topic of any message type and republishes the messages to another topic. It is fully parameterized, allowing configurable input_topic, output_topic, qos_depth and message_type with a flexibility allows forwarding messages of any type without rebuilding.
The generate_parameter_library (Link) is used to generate C++ header files from the provided YAML files, allowing each parameter to be accessed via a generated C++ struct in code.
Features
- Uses generic ROS2 publishers and subscriptions, with message types determined at runtime.
- Each nodes parameters are converted to header files with a C++ struct with specified parameters using
generate_parameter_library. - Forwards messages as raw serialized data, with no deserialization,
keeping memory usage low. - Quick setup by simply specifying topics and message type via parameters.
- Efficient forwarding of any ROS2 message type without code changes.
Limitations
- No compile-time type checking, type mismatches will only fail at runtime, maybe could be make sense as it is just bridge!
- Only forwards serialized messages, cannot inspect or manipulate message contents.
relay/ # Package
├── include/
│ └── relay/
│ ├── relay_node.hpp # Common relay node header, with generic message type pub and sub
│ └── relay_runner.hpp # Node runner header, to run the node either single or multi-thread
│
├── launch/
│ └── relay_nodes.launch.py # Launch files for starting single, or multiple nodes
│
├── media # Mock Example for testing...
│
├── params/
│ ├── imu_relay_parameters.yaml # IMU parameters
│ ├── pose_relay_parameters.yaml # Pose parameters
│ └── twist_relay_parameters.yaml # Twist parameters
│
├── src/
│ ├── imu_relay_node.cpp # IMU relay node
│ ├── pose_relay_node.cpp # Pose relay node
│ └── twist_relay_node.cpp # Twist relay node
│
├── CMakeLists.txt # Build configuration
├── package.xml # Package metadata
├── README.MD cd ~/your_ROS2_ws/src
# Install generate_parameter_library
git clone https://github.com/picknikrobotics/generate_parameter_library.git
# clone the package..
git clone git@github.com:Go-Ab1/_relay_.git
# Go to the workspace root
cd ../
# Install any missing dependencies
rosdep install -i --from-path src --rosdistro jazzy -y
# Build only this package
colcon build --packages-select relay
source install/setup.bashYou can launch one or more relay nodes using the launch file. Just put the node name(s) that needed to be run as a parameter:
- To launch a single node, use the
node_nameargument. - To launch multiple nodes at once, use the
node_namesargument with a comma-separated list.
As an Example-:
# Launch a single relay node (Eg. pose_relay)
ros2 launch relay relay_nodes.launch.py node_name:=pose_relay
# Launch two relay nodes (Eg. pose_relay and imu_relay)
ros2 launch relay relay_nodes.launch.py node_names:=pose_relay,imu_relay
# Launch three relay nodes (pose_relay, imu_relay, and twist_relay)
ros2 launch relay relay_nodes.launch.py node_names:=pose_relay,imu_relay,twist_relayIn the current implementation, the launch file only uses the default parameters defined in the YAML files, which are compiled into the generated struct.
The format of the paratemetes are followed as in the (generate_parameter_library)
imu_relay:
input_topic:
type: string
default_value: "/imu_input"
description: "Input topic for IMU data"
output_topic:
type: string
default_value: "/imu_desired_output"
description: "Output topic for relayed IMU data"
message_type:
type: string
default_value: "sensor_msgs/msg/Imu"
description: "Message type used for publishing and subscribing"
qos_depth:
type: int
default_value: 10
description: "QoS depth for publisher and subscriber"The topics, and queue size can be customized for each relay node, also can be extended to any additional node with different message type.
The generated header(libraries) struct files can inspected with replacing with the ros2 workspace,
ls ~/your_ros2_ws/build/relay/include Alternatively, run a relay node directly without a launch file. This gives full control over parameters, the node name should be used from the CMakelist.txt when run the node and it overrides the parameters at runtime.
Example for Pose relay:
ros2 run relay pose_relay_node \
--ros-args \
-p input_topic:=/pose_input \
-p output_topic:=/pose_desired_topic \
-p qos_depth:=10 \
-p message_type:=geometry_msgs/msg/PoseStamped \Open a terminal and manually publish (
input pose topic) to check if the node is forwarding messages to the desired output topic:
ros2 topic pub /pose_input geometry_msgs/msg/PoseStamped "{
header: {stamp: {sec: 0, nanosec: 0}, frame_id: 'map'},
pose: {position: {x: 1.0, y: 0.0, z: 0.0}, orientation: {x: 0.0, y: 0.0, z: 0.0, w: 1.0}}
}"Open a new terminal and subscribe to the output topic:
ros2 topic echo /pose_desired_topicThe followinf image shows
rqt graphwith publishers and subscribers for confirming messages flow correctly from input to output topics.
To Do:
It would be useful to add parameters directly in the launch file, allowing dynamic overrides at
runtime. While each node already supports parameter overriding when run individually using --ros-args -p =, integrating this feature into the launch file would provide a more seamless way to configuremultiple nodesat once.
References:
Goitom
