Skip to main content

Topics and Publishers

Learning Objectives

After completing this chapter, you will be able to:

  • Implement publishers for custom message types in ROS 2
  • Understand Quality of Service (QoS) settings and their impact
  • Design effective message structures for humanoid robot applications
  • Create robust topic-based communication patterns

Introduction

Topics form the backbone of communication in ROS 2, implementing a publish-subscribe pattern that enables loose coupling between nodes. In the context of Physical AI and humanoid robotics, topics are essential for distributing sensor data, robot states, and control commands throughout the system. Understanding how to design and implement effective topic-based communication is crucial for creating responsive and reliable robotic systems.

The publish-subscribe pattern allows multiple nodes to broadcast information (publishers) and consume information (subscribers) without direct knowledge of each other. This architecture is particularly advantageous in complex robotic systems where different components need to share data without tight coupling, enabling modularity and maintainability.

Core Concepts

ROS 2 topics use a Data Distribution Service (DDS) implementation at their core, which provides more sophisticated communication patterns than ROS 1. This includes configurable Quality of Service (QoS) settings that allow fine-tuning of communication characteristics based on requirements like reliability, latency, and bandwidth.

Message Types

Messages are the data structures exchanged between nodes via topics. ROS 2 supports built-in message types as well as custom message definitions. For humanoid robotics applications, messages might represent sensor readings, robot poses, joint states, or command velocities.

Quality of Service (QoS)

QoS settings allow publishers and subscribers to negotiate communication characteristics. These include:

  • Reliability: Best effort vs. reliable delivery
  • Durability: Volatile vs. transient local
  • History: Keep all or keep last N messages
  • Deadline: Delivery deadline for messages
  • Lifespan: How long messages are valid

Publisher-Subscriber Pattern

The pattern involves publishers sending messages to topics and subscribers receiving messages from topics. Multiple publishers and subscribers can interact with the same topic, creating a flexible communication network.

Hands-on Examples

Let's implement custom messages and QoS patterns for humanoid robot communication:

# Custom message file: HumanoidState.msg
# Save as msg/HumanoidState.msg

# Humanoid robot state message
string robot_name
float64 torso_height
float64[3] center_of_mass # x, y, z
float64[28] joint_positions # 28 DOF humanoid
float64[28] joint_velocities
float64[28] joint_efforts
float64[4] orientation # quaternion
float64[3] linear_velocity
float64[3] angular_velocity
bool in_motion
bool in_safe_mode

Expected Output:

QoS Configuration Examples
Sensor Data: QoSProfile(reliability=ReliabilityPolicy.BEST_EFFORT, durability=DurabilityPolicy.VOLATILE, history=HistoryPolicy.KEEP_LAST, depth=5)
Control Commands: QoSProfile(reliability=ReliabilityPolicy.RELIABLE, durability=DurabilityPolicy.VOLATILE, history=HistoryPolicy.KEEP_LAST, depth=3)
Configuration: QoSProfile(reliability=ReliabilityPolicy.RELIABLE, durability=DurabilityPolicy.TRANSIENT_LOCAL, history=HistoryPolicy.KEEP_ALL, depth=10)
Feedback: QoSProfile(reliability=ReliabilityPolicy.RELIABLE, durability=DurabilityPolicy.VOLATILE, history=HistoryPolicy.KEEP_LAST, depth=10)

Exercises

Complete the following exercises to reinforce your understanding:

  1. Custom Message Creation: Create and use a custom message for humanoid robot sensor data

    • Define a custom .msg file for sensor readings
    • Generate the message files with colcon build
    • Implement a publisher that sends sensor data
    • Create a subscriber that processes the sensor data
  2. QoS Optimization: Experiment with different QoS settings for various data types

    • Set up publishers with different QoS profiles
    • Measure the impact on latency and reliability
    • Document which QoS settings work best for different robot data types
    • Create a configuration table for different message types

Common Pitfalls and Solutions

  • Pitfall 1: Using inappropriate QoS settings - Applying reliable QoS to high-frequency sensor data causing network congestion
    • Solution: Use best effort QoS for high-frequency sensor data; reliable QoS for critical commands
  • Pitfall 2: Message type mismatch - Publishers and subscribers using incompatible message structures
    • Solution: Ensure both ends use the same message definition and version
  • Pitfall 3: Insufficient history depth - Setting depth=1 for important messages that may have processing delays
    • Solution: Set appropriate history depth based on message importance and system requirements
  • Pitfall 4: Publishing too frequently - Overwhelming the network with unnecessary data
    • Solution: Optimize publishing frequency based on actual system needs

Summary

  • Topics enable publish-subscribe communication in ROS 2 using DDS
  • QoS settings allow customization of communication characteristics
  • Custom messages are essential for domain-specific robot applications
  • Proper QoS selection is crucial for system performance and reliability
  • Message design affects system modularity and maintainability

Further Reading