ROS2_custom_interfaces/src/custom_interfaces
2021-06-28 13:24:24 +02:00
..
msg timestamp working 2021-06-28 13:24:24 +02:00
srv canopen msg/srv added 2021-06-28 12:31:29 +02:00
CMakeLists.txt timestamp working 2021-06-28 13:24:24 +02:00
package.xml timestamp working 2021-06-28 13:24:24 +02:00
Readme.md third commit 2021-05-24 00:51:14 +02:00

Use this ROS2 package to create custom interfaces, eg. topic (.msg) and/or service (.srv) files.
This package must only be used for interface files and no Python scripts.

The following steps describe how to create a new custom topic or service.

1. Create new interface file

Topic *.msg files got into msg directory,
Service *.srv files got into srv directory.

Create these files according to the example on this instructions page.

2. CMakeLists.txt

Edit the CMakeLists.txt file by adding the newly created *.msg or *.srv files to this part:

rosidl_generate_interfaces(${PROJECT_NAME}
  "msg/CustomMsg1.msg"
  "msg/CustomMsg2.msg"
  "srv/CustomSrv1.srv"
  DEPENDENCIES builtin_interfaces
 )

3. Build colcon workspace

Move back to the root of your colcon workspace and rebuild it: colcon build --symlink-install
It should build without any errors. If you encounter errors fix these before continuing.

After a successful build you must close and reopen all terminals in order to source the newly built workspace. For this to happen you must have edited .bashrc and added your current workspace (sudo nano ~/.bashrc and follow this example).

4. Define ROS Package Dependencies

Configure package.xml of the other ROS packages in which you want to use these custom interfaces. This also works for Python scripts from inside the linked package (eg. library module from inside pubsub package).

<depend>std_msgs</depend>
  
<!-- START Custom Dependencies -->
<!-- This is custom for the package you depend on -->
<exec_depend>pubsub_msg</exec_depend>
<!-- END Custom Dependencies -->

5. Import interfaces

Import the newly created interfaces into the Python scripts. These scripts must be located in Python packages inside the same workspace (or another sourced workspace):

  • Topics:
from custom_interfaces.msg import CustomMsg1
  • Services:
from custom_interfaces.srv import CustomSrv1

Now you can work with your custom interface in order to exchange custom information between nodes.

This also works for Python modules stored in the linked ROS packages.