How to create custom message types in ROS

Pubblicato il
Fiorella Sibona

Fiorella Sibona

Roboticist Wannabe

Hi everyone! Today I will put together informations which I have gathered while I was trying to create a new message type with ROS for my thesis project. This may be your situation when you need a basic message type which aims at simplifying your applications: indeed some ROS standard message types are way too complex for the simple use you need to fit with. I hope this post can summarize and speed up the custom message creation operation. Let’s get started!


Creating the .msg file

I assume you have your catkin workspace (created following this tutorial) and you have created your ROS package as explained here.

RMK: it is a good habit to create a specific package to define your messages, e.g., create a custom_msgs package.

First of all, from command line, enter the package folder exploiting the roscd ROS command:

roscd custom_msgs

Once in the package folder, create a new folder called msg, such that the custom messages contained in it will be automatically recognized at build time:

mkdir msg
cd msg

Create your new message definition file by directly specifying its content and saving it in a .msg file; in my case, I needed to have a simple array of integer data which I have called the Servo_Array type.

echo "uint16[] data" > msg/Servo_Array.msg

To check whether your message definition file has been correctly saved you can simply check its content:

cat Servo_Array.msg

Triggering the message generation

In order to instruct the catkin build operation to generate the newly defined messages, we have to edit the package.xml and CmakeLists.txt package files as follows:

  • Open package.xml, and make sure these two lines are in it and uncomment them:
  • Open CmakeLists.txt, add message_generation to the list of COMPONENTS as follows:
find_package(catkin REQUIRED COMPONENTS
  • Export the message runtime dependency:
  CATKIN_DEPENDS message_runtime ...
  • Then uncomment the following lines (remove #) and replace Message*.msg with your .msg file (in my case Servo_Array.msg):
  • Finally uncomment these lines:

RMK: if you have more than one custom message to add, just create the relative .msg files and add it when a .msg file has to be added in the CmakeLists.txt file (as specified above).

Re-building your package

Now that we have created some new messages, we need to make our package again: #In your catkin workspace

roscd custom_msgs
cd ../..

RMK: suppose you are writing Python scripts within a package called, for example, my_package: to import the custom message in your script you’ll just need the line from custom_msgs.msg import Motors_Array. Note that Python scripts are usually contained in a my_package/scripts folder.

Most informations in this post have been retrieved from here.

Custom Messages and Rosserial Arduino

In case you need to use your custom message within your serial node on Arduino, you just need to copy your custom_msgs package in the ros_lib folder (Arduino_sketches_folder/libraries/ros_lib/). After re-opening the Arduino editor, you can refer the new message in your sketch with #include <custom_msgs/Motors_Array.h>.

Bye! :hibiscus:

Fiorella Sibona

Fiorella Sibona

Roboticist Wannabe

comments powered by Disqus
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora