ROS Communication Patterns
ROS provides different patterns that can be used to communicate between ROS Nodes:
- Topics: are used to send continuous data streams like e.g. sensor data. Data can be published on the topic independent of if there are any subscribers listening. Similarly, Nodes can also subscribe to topics independent of if a publisher exists. Topics allow for many to many connections, meaning that multiple nodes can publish and/or subscribe to the same topic. The official tutorial on how to implement a simple publisher/subscriber can be found here. How too create a custom message type can be found here.
- Services: are used when it is important that a message is received by the recipient and a response on the outcome is wanted. Services should only be used for short procedure calls e. g. changing the state of a system, inverse kinematics calculations or triggering a process. The official tutorial on how to implement a simple service server/client can be found here. How to create a custom action can be found here.
- Actions: are similar as services but are used if the triggered event needs more time and the possibility for receiving updates on the process or being able to cancel the process is wanted. An example could be when sending a navigation command. The official tutorial on how to implement an action server/client can be found here. An example which allows the client to cancel the goal can be found here: server, client. How to create a custom action can be found here
- Parameters: are not actually a communication pattern but rather is a storage space for variables. It is not designed for high-performance and therefore mostly used for static variables like configuration parameters. The official guide on how to use parameters in a class can be found here.
Exercises
Create a custom message package
- Copy for the communication_patterns folder from the ros2_students_25 repository into the src folder of your workspace.
- Open a terminal and create a package called communication_patterns_interfaces by using the following commands:
cd ~/ros2_ws/src/communication_patternsros2 pkg create --build-type ament_cmake communication_patterns_interfaces - In the communication_patterns_interfaces package create the following folders: msg, srv and action
Create custom messages
- Create a message type called Position that has three float64 variables with the names: x, y and z
- Create a message type called Orientation that has three float64 variable with the names: alpha, beta and gamma
- Create a message type called Pose that has two variable called position and orientation which have the previously created message types.
- In the
CMakeLists.txtfile make sure to add the following:find_package(rosidl_default_generators REQUIRED) rosidl_generate_interfaces(${PROJECT_NAME} "msg/MSG_TYPE_FILENAME" ) - In the
package.xmlfile make sure to add the following:<buildtool_depend>rosidl_default_generators</buildtool_depend> <exec_depend>rosidl_default_runtime</exec_depend> <member_of_group>rosidl_interface_packages</member_of_group> - Don’t forget to build your workspace!
Create a publisher and subscriber with custom message type
For this exercise you will be working in the file called pose_publisher.py
To test your code use: ros2 launch communication_patterns pose_exercise.launch.py.
Create custom service messages
- Add a service type with the name CheckCollision.srv which has the following request fields: object_position (Position) and object_radius (float64). The response should be bool value named in_collision.
- Add a service type with the name GetRobotFootprint.srv which has the following request field: robot_id (string). The response should contain the following variables: success (bool), robot_position (Position) and footprint_radius (float64).
- Don’t forget to add the service types in the
CMakeLists.txtfile and to build your workspace!
Create a service client
For this exercise you will be working in the file called robot_footprint_client.py
To test your code use: ros2 launch communication_patterns robot_footprint_exercise.launch.py.
Create a service server
For this exercise you will be working in the file called collision_server.py
To test your code use: ros2 launch communication_patterns collision_exercise.launch.py.
Create a custom action message
If you need help with this exercise, here is the link to the official documentation of how to create an action: link.
- Add an action type with the name RobotNavigation.action which has the following fields. For the goal message: goal_pose (Pose). For the result message: success (bool) and final_pose (Pose). And for the feedback message: current_pose (Pose) and distance_to_goal (float64).
- Add the action types in the
CMakeLists.txtfile. - In the
package.xmlfile add the following line:<depend>action_msgs</depend> - Don’t forget to build your workspace.
Create an action server and client
If you need help with this exercise, here is the official documentation of how to create an action server and client: link.
Complete all the TODO’s in the navigation_server.py and navigation_client.py files.
To test your code use: ros2 launch communication_patterns navigation_exercise.launch.py