• +4917626969472
  • info@ur-techpartner.de

ROS Basics (05/17) – Topic: Define Message

Robot Operating System(ROS1/ROS2) ROS Programming

ROS Basics – Topic: Message

In this blog we will describe how to define our own message, and how to modify CmakeList.txt and package.xml files for our defined message compilation.


We knew that the topics handle information through messages. In previous articles we have used a message type “std_msgs/Int32” for communicating information between punisher and subscriber node. But ROS provides a lot of different messages. Though it is recommended to use ROS default messages, we can create our own message types whenever required.
Messages are defined in .msg files. The “.msg” files are located inside a “msg” directory of a package.

We can verify it using command line

Open a terminal and run the command

rosmsg show std_msgs/Int32

We can see that the “Int32” message has only one variable named “data” of type int32

The Int32 message comes from the package “std_msgs” as you can notice in previous command. The Int32.msg file is located inside the “msg” directory of “std_msgs” package.

We can have a look using following commands

Run in Terminal

roscd std_msgs

(This will take us into the package “std_msgs”)

Run the command


We can see that there is a directory named “msg” inside the package

Go to the “msg” directory using cd command

cd msg

Now run the command


You can view that there are a lot of “.msg” files inside the “msg” directory. These all are messages which are part of standard messages package named “std_msgs”.

You can open the files and can see what type of variables each file contains.

You can also go to the specific directory in your computer to view these files as they are show in figure below

Similarly, if you want to see all ROS defined messages, you can run the command

rosmsg list

Define Your Own Message

Suppose, we want to publish a data of different message type that is not available by ROS defined messages. In this case, we can define our own message type.

Since messages are defined by a special message definition file (of extension “.msg”), and the files are saved in “msg” directory.

Therefore, we will create the msg directory in our previously created package “ros_basics”

Open a terminal and run following command go to the package “ros_basic”

roscd ros_basics

Run “ls” command to see the contents of the package


There is no “msg” directory so far

Run the command to create a directory named “msg”

mkdir msg

Go into msg directory

cd msg

Now Create a new file named “Age.msg” inside msg directory

Run the command

touch Age.msg

Open the file to modify it

gedit Age.msg

Enter following lines in Age.msg file

float32 years
float32 months
float32 days

The above executed commands are shown in figure below

The file will look like as show in figure below

Save and close the file.

Modify CMakeLists.txt and package.xml files for defined Message Compilation

Now we will have to modify two files “CMakeLists.txt” and “package.xml” so that our customized message can be available to use.

For this go back to “ros_basics” directory

Run command

cd ..

Check the contents of the package using ls command


You can see the file “CMakeLists.txt” and “package.xml” inside the directory

Modify “CMakeLists.txt”

Run following command to modify the file

gedit CMakeLists.txt

The file will be opened as shown in figure below, you will see most of the code lines are commented. You will have to make changes in following four functions. You do not need to write the function names (they are already there), You only need to find them and uncomment them (if they are commented). The four functions are

1. find_package()
2. add_message_files()
3. generate_messages()
4. catkin_package()

The above four functions should be modified like this

find_package(catkin REQUIRED COMPONENTS
message_generation # Add “ message_generation” here, after other packages

Age.msg # Add “Age.msg” here, and uncomment function & parenthesis

) #Uncomment as shown (if commented)

CATKIN_DEPENDS rospy std_msgs message_runtime
) #Add “message_runtime” as shown, there might be other dependencies.

When all four functions are modified accordingly (they should look like as shown above), then save the file and close it.

Modify “package.xml”

Run following command to modify the file

gedit package.xml

You need to find the part of code lines which are starting with the line


In the end of these lines

Add following three lines


You can see it in the figure below

(This way we add build and execution dependencies for our defined messages).

Save and close the file.

Now the remaining last step is to build the catkin_ws workspace.

Go to the catkin_ws directory

Run command

roscd ~/catkin_ws

Run the command


(This will build all packages)

Run the command

source devel/setup.bash

(Don’t forget to execute above command)

Now your defined message is available to use.

You can verify it by running the command

rosmsg show ros_basics/Age

Congratulations! It worked. Now you can use your own defined message type.

All the commands that we executed are shown in figure below

You can also confirm that if your defined message is available in ROS messages list

Run following command

rosmsg list | grep Age

Yes, it is also present in the list as shown in figure below

When We Should Define a New Message Type?

You should make a new message type only when there is no other choice. Since ROS already has a big set of message types and nodes can publish and receive message of same type.

Therefore, for creating a new message type you will have to do a lot of work to make it possible for nodes to transmit the message. Before defining a new message type you should always use the command “rosmsg” to see that if there is something similar that you can use in your program. Existing message types should always be preferred.

Similarly, you should use SI units (meters, kilograms, seconds, etc.) in your messages, because ROS does the same.

No comments