Here’s a clear example using turtlesim in ROS 2 that demonstrates both Publish/Subscribe and Service/Clientcommunication, along with an explanation of their differences.

1. Publish/Subscribe Example with Turtlesim

Command-line demonstration:

`bash*# Start turtlesim* ros2 run turtlesim turtlesim_node

# Publish a velocity command to move the turtle forward ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}"`

2. Service/Client Example with Turtlesim

Command-line demonstration:

bash*# Call the /spawn service to create a new turtle at (x=2, y=2, theta=0.2)* ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: ''}"

Comparison Table

Pattern Example Command/Node Direction Continuous? Use Case
Publish/Subscribe ros2 topic pub /turtle1/cmd_vel ... One-way Yes Move the turtle continuously
Service/Client ros2 service call /spawn ... Two-way (request/response) No Spawn a new turtle (on demand)

Summary of Differences