ROS 2 Custom Interfaces
Creating Custom Interface
In ROS 2, a custom interface defines unique message, service, or action types tailored to specific application needs. It is important because it enables precise communication between nodes, ensuring data is structured and interpreted correctly for specialized robotics tasks.
Making Package
To create a custom interface it is best to make a dedicated package for all the interfaces that are needed in that project. Go to the workspace and navigate to the src
folder.
Once the package is create open the package folder with visual studio code and delete the include
directory and the src
directory. Next open the packages.xml
file and delete all the lines that have the <depend>{somthing}</depend>
. Finally, add the following highlighted lines.
Now that the packages.xml
is one the CMakeLists.txt
file needs to be changed. Go through and delete everything so it looks like this and add the lines that are highlighted.
Lines 13 and 14 are the custom interfaces that you created and want to build. Look at the next two sections to learn about how to create these files with the message and server configuration.
Creating a Custom Message
To create a custom message navigate to the package that was created in the [[#Making Package]] section. Here we will create a folder in this package called msg
and inside this folder we will put all of are message configs. For example lets create a message for a color sensor. This message might contain the the red, green, and blue value from the sensor.
ColorSensor.msg | |
---|---|
Now all you have to do add it to the CMakeLists.txt
file and run colcon build
.
Creating a Custom Service
To create a custom service navigate to the package that was created in the [[#Making Package]] section. Here we will create a folder in this package called srv
and inside this folder we will put all of are service configs. For example lets create a service for a rectangular area calculator. This service might have a length and width for a request and as a response just an area.
CalculateArea.srv | |
---|---|
Now all you have to do add it to the CMakeLists.txt
file and run colcon build
.
Using a Custom Interface
To use a custom interface in a Python or C++ program import it or include it just like normal. If you have problems with vscode not recognizing it in the C++ programs you will have to edit the .vscode/c_cpp_properties.json
file and add it to the include path.