Note: This tutorial assumes that you have completed the previous tutorials: ROS tutorials.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Defining Custom Messages

Description: This tutorial will show you how to define your own custom message data types using the ROS Message Description Language.

Tutorial Level:

Next Tutorial: Using a C++ class in Python

Generating Messages

Generating a message is easy. Simply place a .msg file inside the msg directory in a package. Please follow previous tutorial about creating .msg files (don't forget to choose build system type at the top of the page there).

Including or Importing Messages

C++

Messages are put into a namespace that matches the name of the package. ie.

   1 #include <std_msgs/String.h>
   2 
   3 std_msgs::String msg;

Python

   1 from std_msgs.msg import String
   2 
   3 msg = String()

Dependencies

If you are using the new custom message defined in a different package, remember to add:

to manifest.xml:

<depend package="name_of_package_containing_custom_msg"/>

to package.xml:

<build_depend>name_of_package_containing_custom_msg</build_depend>
<run_depend>name_of_package_containing_custom_msg</run_depend>

<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>

and you will need to add this to your CMakeList.txt:

find_package(message_generation)
catkin_package(CATKIN_DEPENDS message_runtime)
add_message_files(FILES your_msg_file.msg)

If you are building C++ nodes which use your new messages, you will also need to declare a dependency between your node and your message, as described in the catkin msg/srv build documentation

The ROSNodeTutorialPython tutorial shows an example of the previously described talker and listener tutorials using a custom message, with implementations in C++ and Python.

Wiki: es/ROS/Tutoriales/DefiningCustomMessages (last edited 2021-02-16 16:59:59 by JuanEduardoRiva)