Skip to content
  • Home
  • About the Blog
  • About the Author
  • Sitemap

Abdur Rosyid's Blog

Just a few notes on mechanical engineering and robotics

Creating Custom Messages

July 8, 2021 by Abdur Rosyid

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:

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:

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:

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:

std_msgs
message_generation
std_msgs
message_runtime

Step 2. Add in CMakeLists.txt:

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).

Post navigation

Previous Post:

ROS Parameters

Next Post:

ROS Bag

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • STEM 101
  • Robotics
  • Kinematics
  • Dynamics
  • Control
  • Robot Operating System (ROS)
  • Robot Operating System (ROS2)
  • Software Development
  • Mechanics of Materials
  • Finite Element Analysis
  • Fluid Mechanics
  • Thermodynamics

Recent Posts

  • Pull Request on Github
  • Basics of Git and Github
  • Conda vs Docker
  • A Conda Cheat Sheet
  • Installing NVIDIA GPU Driver on Ubuntu

Archives

  • June 2025
  • July 2021
  • June 2021
  • March 2021
  • September 2020
  • April 2020
  • January 2015
  • April 2014
  • March 2014
  • March 2012
  • February 2012
  • June 2011
  • March 2008
© 2026 Abdur Rosyid's Blog | WordPress Theme by Superbthemes