-
Notifications
You must be signed in to change notification settings - Fork 2
new_interface_tutorial
This tutorial is meant to aid the developer create new custom interfaces in the ROS2 program for specific topics.
- ROS2 custom interfaces tutorial documentation (Crystal distro)
- Set of packages with common ROS2 interfaces
- ROS2 Field Types for interfaces (Galactic Distro)
- ROS - Robot Operating System
- ROS2 - New version of ROS that will be used
- Interface / Msg - The content which is sent throught ROS2 topics or any other type of communication channel
It is essential you create an independent package for the purpose of the tutorial. (Note: the project may already include one, you are not supposed to create multiple packages for new interfaces)
ros2 pkg create --build-type ament_cmake tutorial_interfacestutorial_interfaces is the name of the newly introduced package. Note that it is a CMake package since there is no way to generate a .msg or .srv file in a pure Python package.
It is good practice to keep .msg and .srv files in their own directories within a package. So we must create the respective folders:
mkdir msg
mkdir srvIn the tutorial_interfaces/msg directory you just created, make a new file called Num.msg with one line of code declaring its data structure:
int64 num
This will be our custom message, and it transfers a single 64-bit integer called num.
There is a set of other primitive types with the respective Python & C++ conversions in the Links section.
If by any chance you wish to implement an interface which depends on other created interfaces, you may do it like so:
geometry_msgs/Point point
Here we are transferring a Point derived from the geometry_msgs package.
If we take a look at its definition, we conclude it is the same as writing the following piece of code:
# This contains the position of a point in free space
float64 x
float64 y
float64 zBecause the interfaces rely on rosidl_default_generators for generating language-specific code, you need to declare a dependency on it. Add the following lines to package.xml:
<build_depend>rosidl_default_generators</build_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>As you may know from other ROS2 experiments, if we use any external interface, we need to create a specific dependency for it. In the case we presented before, we would need to include the following line:
<depend>geometry_msgs</depend>To convert the interfaces you defined into language-specific code (like C++ and Python) so that they can be used in those languages, add the following lines to CMakeLists.txt:
find_package(rosidl_default_generators REQUIRED)
# Add any new custom interface here
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Num.msg"
)
ament_export_dependencies(rosidl_default_runtime)In addition to the above lines, in case we are using dependent definitions, we must add the correspondent find_package statement and add it to the rosidl_generate_interfaces like so:
find_package(geometry_msgs REQUIRED)
# Add any new custom interface here
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Num.msg"
DEPENDENCIES geometry_msgs
)Now that all the parts of your custom interfaces package are in place, you can build the package:
colcon build --packages-select tutorial_interfacesIn a new terminal, run the following command from within your workspace to source it:
. install/setup.bashNow you can confirm that your interface creation worked by using the command:
ros2 interface show tutorial_interfaces/msg/NumWith the desired display:
int64 num
The new interfaces can be used now in a real Python/C++ package. You may do so by importing the respective .msg file:
Python:
from tutorial_interfaces.msg import NumC++:
#include "tutorial_interfaces/msg/num.hpp"And not forget to complete the package.xml / CMakeLists.txt files.
The .srv files may be used to create custom service messages. However, the focus of this tutorial is on topic-related messaging, so we will not cover any further steps.
You may follow the first link which also guides this type of communication.