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
- Start ROS 2:
ros2 daemon stop
ros2 daemon start- Publish a static transform:
ros2 run tf2_ros static_transform_publisher 0.2 0.0 0.3 0 0 0 base_link camera_link- Inspect the transform:
ros2 run tf2_ros tf2_echo base_link camera_link- Visualize frame tree:
ros2 run tf2_tools view_framesCustom Interface Messages
Overview
ROS 2 allows you to define custom interfaces:
.msgfor topic messages.srvfor services.actionfor long-running tasks
Example .msg
msg/RobotStatus.msg:
string mode
float32 battery
bool emergency_stopBuild integration checklist
- Add
rosidl_default_generatorsinpackage.xmlandCMakeLists.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:
ros2 param list
ros2 param get /node_name parameter_name
ros2 param set /node_name parameter_name valueYAML loading
Example params.yaml:
my_node:
ros__parameters:
use_sim_time: false
rate_hz: 10.0Run with parameters:
ros2 run my_pkg my_node --ros-args --params-file params.yamlMeta-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_IDis configured.
Quick checks
echo $ROS_DOMAIN_ID
ros2 node list
ros2 topic listIf 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
Select RMW implementation
export RMW_IMPLEMENTATION=rmw_cyclonedds_cppThen 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:
ros2 param set /node_name use_sim_time trueUse use_sim_time whenever replaying bags or running simulation.
Common Command Tools
Frequently used CLI commands:
ros2 node list
ros2 topic list
ros2 topic echo /topic_name
ros2 service list
ros2 action list
ros2 interface listThese commands are the fastest way to debug graph connectivity.