Creating Custom Messages
There are three types of messages in ROS:
- msg : used in topic
- srv : used in service
- action : used in action
The msg message is defined in an .msg file, which looks like this:
1 2 |
data_type variable_1 data_type variable_2 |
One or more .msg files are typically created inside a folder called “msg”.
ROS topic uses .msg message. In this case, you can use either an existing (built-in) .msg or your custom .msg.
The srv message consists of two parts, separated by a line of three dashes. The first part is the request variable(s) whereas the second part is the response variable(s). An .srv file looks like this:
1 2 3 4 5 |
data_type request_variable_1 data_type request_variable_2 --- data_type response_variable_1 data_type response_variable_2 |
One or more .srv files are typically created inside a folder called “srv”.
The action message consists of three parts, separated by two lines of three dashes. The first, second, and third parts are the goal (request) variable(s), the result variable(s), and the feedback variable(s), respectively. An .action file looks like this:
1 2 3 4 5 6 7 8 |
data_type goal_variable_1 data_type goal_variable_2 --- data_type result_variable_1 data_type result_variable_2 --- data_type feedback_variable_1 data_type feedback_variable_2 |
One or more .srv files are typically created inside a folder called “action”.
Steps to create a custom message:
Step 1. Add in package.xml:
1 2 3 4 |
<build_depend>std_msgs</build_depend> <build_depend>message_generation</build_depend> <run_depend>std_msgs</run_depend> <run_depend>message_runtime</run_depend> |
Step 2. Add in CMakeLists.txt:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
find_package(catkin REQUIRED COMPONENTS std_msgs message_generation ) add_message_files(FILES MessageFileName.msg) # used to create message file add_service_files(FILES ServiceFileName.action) # used to create service file add_action_files(FILES ActionFileName.action) # used to create action file generate_messages( DEPENDENCIES std_msgs ) |
Step 3. Create a folder (either “msg”, “srv”, or “action” folder) to contain the custom message.
Step 4. Create the message file (either .msg, .srv, or .action file) as described above.
Step 5. Build using “catkin_make” command.
Best Practice
For a robot, it is generally recommended to create a separate (dedicated) package for the .msg, .srv, and .action files rather than to include the message file(s) in an executable node. It is because when the subscriber node and the publisher node are executed on different computers, both the publisher and subscriber nodes are dependent on the identical message. Therefore unnecessary nodes must be installed if the message file exists in the package. If the message is created as an independent package, the message package can be added to the dependency option, thus eliminating unnecessary dependencies between packages.
To do this, create a dedicated package (named YOUR_ROBOT_msgs) for all your custom message(s), service(s), and action(s).