Welcome to RDK S600 Developer Kit repository
10.ROS2 custom interface message
10. ROS2 Custom Interface Messages
Note: The accompanying reference video uses the ROS Humble version, for reference only
1. Communication Interface Overview
In the ROS system, whether it's topics, services, or actions, an important concept is used - the communication interface.
Communication is not one person talking to themselves, but two or more people exchanging ideas. What is being exchanged? To make it easy for everyone to understand, we can define a standard structure for the transmitted data. This is the communication interface.
Interfaces can reduce dependencies between programs, making it easy for us to use others' code and for others to use our code. This is the core goal of ROS - reducing redundant work.

ROS has three commonly used communication mechanisms: topics, services, and actions. Through the interfaces defined by each communication type, various nodes can be organically connected.
2. Creating Custom Interface Process
The process of creating custom interface messages is similar to writing executable programs in packages. The main steps are:
Create an interface package;
Create and edit .msg files, .srv files, .action files;
Edit configuration files;
Build;
Test.
3. Create Action Communication Custom Interface
In the [9. ROS2 Action Communication Server Implementation] example, we have already demonstrated the complete process of creating an action communication interface. You can review it first. We won't repeat it here.
4. Create Topic Communication Custom Interface
In the [9. ROS2 Action Communication Server Implementation] course, we have already created the custom interface package. Create a msg folder under the pkg_interfaces package, and create a Person.msg file in the msg folder. Enter the following content in the file:
string nameint32 agefloat64 height
Add the following configuration in package.xml and CMakeLists.txt:
CMakeLists.txt:
xxxxxxxxxxrosidl_generate_interfaces(${PROJECT_NAME} "action/Progress.action" "msg/Person.msg")package.xml:
xxxxxxxxxx<buildtool_depend>rosidl_default_generators</buildtool_depend><exec_depend>rosidl_default_runtime</exec_depend><depend>action_msgs</depend><member_of_group>rosidl_interface_packages</member_of_group>
Navigate to the current workspace in the terminal and build the package:
xxxxxxxxxxcolcon build --packages-select pkg_interfaces
Test if the interface is working correctly
First, refresh environment variables:
xxxxxxxxxxsource install/setup.bashView interface type:
xxxxxxxxxxros2 interface show pkg_interfaces/msg/PersonUnder normal circumstances, the terminal will output content consistent with the Person.msg file.

5. Create Service Communication Custom Interface
In the [9. ROS2 Action Communication Server Implementation] course, we have already created the custom interface package. Create an srv folder under the pkg_interfaces package, and create an Add.srv file in the srv folder. Enter the following content in the file:
xxxxxxxxxxint32 num1int32 num2---int32 sum

Add the following configuration in package.xml and CMakeLists.txt:
CMakeLists.txt:
xxxxxxxxxxrosidl_generate_interfaces(${PROJECT_NAME} "action/Progress.action" "msg/Person.msg" "srv/Add.srv")package.xml:
xxxxxxxxxx<buildtool_depend>rosidl_default_generators</buildtool_depend><exec_depend>rosidl_default_runtime</exec_depend><depend>action_msgs</depend><member_of_group>rosidl_interface_packages</member_of_group>

Navigate to the current workspace in the terminal and build the package:
xxxxxxxxxxcd ~/yahboomcar_ros2_ws/yahboomcar_wscolcon build --packages-select pkg_interfacessource install/setup.bash
Test:
xxxxxxxxxxros2 interface show pkg_interfaces/srv/Add
Under normal circumstances, the terminal will output content consistent with the Person.msg file.
