ROS2_custom_interfaces/src/custom_interfaces/Readme.md

54 lines
2.3 KiB
Markdown

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](https://docs.ros.org/en/foxy/Tutorials/Writing-A-Simple-Py-Service-And-Client.html).
## 2. CMakeLists.txt
Edit the `CMakeLists.txt` file by adding the newly created *.msg or *.srv files to this part:
```cmake
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](https://egit.irs.uni-stuttgart.de/RoverLehre/ROS2_pubsub/src/branch/master/non-ros-files/bashrc_overlay.bash)).
## 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).
```xml
<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:
```python
from custom_interfaces.msg import CustomMsg1
```
* Services:
```python
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.