ROS2 Advanced Interfaces and Middleware

TF2 Coordinate Transformation

Why TF2 matters

TF2 manages coordinate relationships between robot frames over time, such as map, odom, base_link, and camera_link.

Typical use cases:

  • Transform sensor data into a planning frame
  • Align camera/lidar data with robot base
  • Keep localization, navigation, and visualization consistent

Core concepts

  • Frame: A named coordinate system.
  • Transform: Translation + rotation between two frames.
  • TF tree: A connected graph of frame relationships.
  • Static transform: Fixed transform published once.
  • Dynamic transform: Time-varying transform updated continuously.

Minimal TF2 commands

  1. Start ROS 2:
bash
ros2 daemon stop
ros2 daemon start
  1. Publish a static transform:
bash
ros2 run tf2_ros static_transform_publisher 0.2 0.0 0.3 0 0 0 base_link camera_link
  1. Inspect the transform:
bash
ros2 run tf2_ros tf2_echo base_link camera_link
  1. Visualize frame tree:
bash
ros2 run tf2_tools view_frames

Custom Interface Messages

Overview

ROS 2 allows you to define custom interfaces:

  • .msg for topic messages
  • .srv for services
  • .action for long-running tasks

Example .msg

msg/RobotStatus.msg:

text
string mode
float32 battery
bool emergency_stop

Build integration checklist

  • Add rosidl_default_generators in package.xml and CMakeLists.txt.
  • Register interface files via rosidl_generate_interfaces(...).
  • Build with colcon build.
  • Source workspace before use.

Parameter Service Cases

Parameter basics

Parameters configure node behavior at runtime.

Common operations:

bash
ros2 param list
ros2 param get /node_name parameter_name
ros2 param set /node_name parameter_name value

YAML loading

Example params.yaml:

yaml
my_node:
  ros__parameters:
    use_sim_time: false
    rate_hz: 10.0

Run with parameters:

bash
ros2 run my_pkg my_node --ros-args --params-file params.yaml

Meta-package

A meta-package groups related packages for distribution and dependency management. It usually contains no executable code.

Typical use:

  • Organize a project stack
  • Install multiple related packages together

Distributed Communication

Multi-machine prerequisites

  • Machines can reach each other on the same network.
  • Time is synchronized (NTP recommended).
  • Same ROS_DOMAIN_ID is configured.

Quick checks

bash
echo $ROS_DOMAIN_ID
ros2 node list
ros2 topic list

If discovery fails, check firewall and multicast settings.


DDS

Role of DDS in ROS 2

DDS is the underlying middleware used by ROS 2 for discovery and message transport.

Common implementations

  • Cyclone DDS
  • Fast DDS
  • Connext DDS
bash
export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp

Then restart terminals and source workspace.


Time-related API

ROS 2 supports system time and simulated time.

  • Use node clocks in code (node.get_clock() in Python).
  • Enable simulation time with parameter:
bash
ros2 param set /node_name use_sim_time true

Use use_sim_time whenever replaying bags or running simulation.


Common Command Tools

Frequently used CLI commands:

bash
ros2 node list
ros2 topic list
ros2 topic echo /topic_name
ros2 service list
ros2 action list
ros2 interface list

These commands are the fastest way to debug graph connectivity.