Chapter 5. ROS 2 Programming Cont.
(Interfaces & custom package)
010243407-63 Robot Operating System Programming
[Link]. Noppadol Pudchuen
Production and Robotics Engineering
King Mongkut’s University of Technology North Bangkok
Topics
• What is ROS2 interfaces?
• Creating custom msg and srv files
• Creating a custom action
• Writing an action server and client (Python)
ROS 2 Interfaces
• ROS application typically communicate through “interfaces” of one of
three types: messages, services and actions.
• In this chapter we will describe the supported types:
• Messages: .msg files are simple text file that describe the fields of a
ROS message. They are used to generate source code for message in
different languages.
• Service: .srv files describe a service. They are composed of two parts:
a request and response. The request and response are message
declarations.
• Action: .action files describe actions. They are composed of three
parts: a goal, a result, and feedback. Each part is a messaged
declaration itself.
ROS 2 Interfaces
• These are built-in-types currently supported:
Example: sensor_msgs/msg/LaserScan
• File: sensor_msgs/msg/[Link]
[Link]
Example: sensor_msgs/msg/LaserScan
• Get ROS 2 sensor message
• Bring up the turtlebot3 world environment.
$ros2 launch turtlebot3_gazebo turtlebot3_world.[Link]
Creating custom msg and srv files
• 1. Create a new package
$ ros2 pkg create –build-type ament_cmake tutorial_interfaces
The .msg and .srv files are required to be placed in directories
called msg and srv respectively. Create the directories in
ros2_ws/src/tutorial_interfaces:
$ mkdir msg srv
• 2. Create custom definitions
2.1 msg definition
In the tutorial_interfaces/msg directory the we create, make a new
file called [Link] with one line of code declaring its data
structure
int64 num
Creating custom msg and srv files
• Also, in the tutorial_interfaces/msg directory, make a new file called
[Link] with the following information:
geometry_msgs/Point center
float64 radius
• 2.2 srv definition
Go back to the tutorial_interfaces/srv that we created, make a new file
named [Link] with the following request and response
structure:
int64 a
int64 b
int64 c
---
int64 sum
Creating custom msg and srv files
• 3. [Link]
To convert the interfaces you defined into language-specific code
(C++ and Python) so that they can used in those languages, add
the following lines to [Link]
find_package(geometry_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)
ros_idl_generate_interfaces(${PROJECT_NAME}
“msg/[Link]”
“msg/[Link]”
“srv/[Link]”
DEPENDENCIES geometry_msgs # Add packages that above message depend on, in this case
geometry_msgs for [Link]
)
Creating custom msg and srv files
• 3. [Link]
Due to the interfaces rely on rosidl_default_generators for generating
language-specific code, you need to declare a build tool
dependencies on it. A rosidl_default_generators is a runtime or
execution-stage dependency, needed to be able to use the interfaces
later.
Add the following lines within the <package> element of [Link]
<depend>geometry_msgs</depend>
<buildtool_depend>rosidl_default_generator</buildtool_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
Creating custom msg and srv files
• 5. Build the tutorial_inferfaces package
$ colcon build –package-select tutorial_interfaces
• 6. Validate our msg and srv creation
In a new terminal, run the following command from your workspace
(ros2_ws) to source it through this following:
$ source install/[Link]
Now you can check that your interface creation worked by using the
ros2 interface show command:
$ ros2 interfaces show tutorial_interfaces/msg/Num
It should return:
int64 num
Creating custom msg and srv files
Creating custom msg and srv files
Creating an action
• 1. Create a new package
$cd ros2_ws/src
$ros2 pkg create action_tutorials_interfaces
• 2. Definition an action
Action are defined in .action file of the following form:
# Request
---
# Result
---
# Feedback
Creating an action
• An action definition is made up of three message definition
separated by ---.
A request message is sent from an action client to an action
server initiating a new goal
A result message is sent from an action server to an action client
when a goal is done.
Feedback messages are periodically sent from ac action server to
an action client with updates about a goal.
Creating an action
• Create an action directory in our ROS 2 package
action_tutorials_interfaces:
$ cd action_tutorials_interfaces
$ mkdir action
Within action directory, create a file called [Link] with
the following contents:
int32 order
---
int32[] sequence
---
int32[] partial_sequence
Creating an action
• 3. Building an action
This is accomplished by adding the following lines of our
[Link] before the ament_package() line, in the
action_tutorials_interfaces:
find_package(rosidl_default_generators REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME}
“action/[Link]”
)
Creating an action
• We must also add the required dependencies to our [Link]
<buildtool_depend>rosidl_default_generator</buildtool_depend>
<depend>action_msgs</depend>
<member_of_group>rosidl_interface_packages</member_of_group>
• We should now be able to build the package containing the
Fibonacci action definition by the following steps:
# Move to the root of the workspace
$ cd ~/ros2_ws
# Build
$ colcon build
• We can check that our action implementation successfully with the
command line tool:
$ ros2 interface show action_tutorials_interfaces/action/Fibonacci
Writing an action server and client (Python)
• 1. Writing an action server
fibonacci_action_server.py
[Link]
ediate/Writing-an-Action-Server-Client/[Link]
Writing an action server and client (Python)
• Try to bring up our action server:
$ python3 fibonacci_action_server.py
• In another terminal, we can use the command line interface to send a
goal:
$ ros2 action send_goal --feedback fibonacci action_tutorials_interfaces/action/Fibonacci “{order: 5}”
Writing an action server and client (Python)
• 2. Writing an action client
fibonacci_action_client.py
[Link]
ediate/Writing-an-Action-Server-Client/[Link]
Writing an action server and client (Python)
• Let’s test our action client and server by running the following
command:
$ python3 fibonacci_action_server.py
Open another terminal, run the action client:
$ python3 fibonacci_action_client.py
• You should see messages printed by the action server as it
successfully executes the goal: