Algorithms, Blockchain and Cloud

ROS Topics, Services, and Actions Explained with Clear Examples


It is said that the next ten years will be the era of Robotics with AI (Artificial Intelligence).

“ROS – Robot Operating System The Robot Operating System (ROS) is a set of software libraries and tools that help you build robot applications. From drivers to state-of-the-art algorithms, and with powerful developer tools, ROS has what you need for your next robotics project. And it’s all open source.”

Introduction to ROS and ROS 2

ROS: Robot Operating System

  • ROS (Robot Operating System) is a flexible framework for writing robot software. It provides tools, libraries, and conventions to simplify the task of creating complex and robust robot behavior across a wide variety of robotic platforms.
  • ROS 2 is the next-generation version of ROS, built to address limitations in real-time performance, security, and multi-platform support. It uses DDS (Data Distribution Service) under the hood for scalable and reliable communication.

For more information, you can visit: ros.org

Core Communication Concepts in ROS

  • Topics – Used for streaming data between nodes asynchronously.
  • Services – Used for synchronous request/response communication.
  • Actions – Used for long-running, goal-oriented tasks with feedback and the ability to cancel.

1. Topics

What Are Topics?

  • Topics provide a publish/subscribe communication model.
  • Ideal for sensor data, continuous streams, or event broadcasting.
  • Many publishers and many subscribers can use the same topic.

Key Characteristics

Pattern Publish/Subscribe
Direction One-way (Unidirectional)
Communication Asynchronous
Use Case Streaming sensor data (e.g. images, laser scans)

Example

# Publisher  
pub = rospy.Publisher('/chatter', String, queue_size=10)  
pub.publish("Hello World!")  

# Subscriber  
def callback(msg):  
    rospy.loginfo(msg.data)  

sub = rospy.Subscriber('/chatter', String, callback)

2. Services

What Are Services?

  • Services use a client-server model for request/response communication.
  • Synchronous and blocking — the client waits for the response.
  • Best used for tasks that require a definite reply, like configuring a component.

Key Characteristics

Pattern Request/Response
Direction Bidirectional
Communication Synchronous
Use Case Querying a device status, turning a component on/off

Example

# Client  
rospy.wait_for_service('/add_two_ints')  
add_two_ints = rospy.ServiceProxy('/add_two_ints', AddTwoInts)  
resp = add_two_ints(3, 5)  

# Server  
def handle_add_two_ints(req):  
    return req.a + req.b  

srv = rospy.Service('/add_two_ints', AddTwoInts, handle_add_two_ints)

3. Actions

What Are Actions?

  • Actions are used for long-running tasks that can be monitored and cancelled.
  • They provide feedback and result messages along with goal status.
  • Great for navigation, manipulation, or any goal-driven process.

Key Characteristics

Pattern Goal/Feedback/Result
Direction Bidirectional
Communication Asynchronous with feedback
Use Case Move a robot arm, navigate to a location

Example

# Client  
client = actionlib.SimpleActionClient('move_base', MoveBaseAction)  
client.wait_for_server()  
goal = MoveBaseGoal()  
client.send_goal(goal)  
client.wait_for_result()  

# Server  
def execute_cb(goal):  
    # Execute goal  
    feedback = MoveBaseFeedback()  
    result = MoveBaseResult()  
    server.set_succeeded(result)  

server = actionlib.SimpleActionServer('move_base', MoveBaseAction, execute_cb, False)  
server.start()

Difference Between Action and Service in ROS

Feature Service Action
Pattern Request / Response Goal / Feedback / Result
Synchronous? Yes (blocking) No (non-blocking)
Duration Short Long-running
Feedback Support No Yes
Cancelable No Yes
Use Case Quick query or configuration Long task with progress, like navigation

Analogy

  • Service = like calling a function and waiting for the result
  • Action = like starting a job in the background and checking progress

Conclusion

  • Use Topics for fast, continuous data streams with no need for response.
  • Use Services when you need a quick answer to a request.
  • Use Actions when you need to send a goal and get progress updates or be able to cancel it later.

ROS 2 Note:

  • All three patterns (Topics, Services, Actions) are supported and enhanced in ROS 2 with DDS for better reliability and performance.

Robot Operating System (ROS)

–EOF (The Ultimate Computing & Technology Blog) —

1076 words
Last Post: C++: lvalue, rvalue and rvalue references
Next Post: C vs C++: Understanding the restrict Keyword and Its Role in Compiler Optimization

The Permanent URL is: ROS Topics, Services, and Actions Explained with Clear Examples (AMP Version)

Exit mobile version