Robot App Store Developer Program


Knowledge Base

How to create ROS MSG and ROS SRV and how to use them

  By Anna Sandler

In this KB, we are going to have a look at how to create ROS msgs and ROS srvs.
  • Messages are nothing different from normal text files, which contain variables.
  • Services are also text files, contain variables but have a request and a response variables, so something comes in, and a result comes out.

How to create ROS MSG?

First, please start your terminal window and I’ll presume you’ll already have created a ROS package before you started this tutorial.
So, in the terminal window, type ‘roscd PACKAGE_NAME’ to change directory to your package.
Now let’s create a directory called ‘msg’ in the package directory using the command: 'mkdir msg'.
Good – now that we have a folder for our messages, there are two possible ways to create a msg:
  • You can open gedit, and type in your variable details, or;
  • Do this with ‘echo’. We will use this method.

We will choose the second option. (it is Linux after all, command-line is the king!)
Please note that ROS has many different data types; you can use float, or string, or you can even use arrays.
So back to the terminal window, type in: ‘echo “int64 num” > msg/num.msg’
Explanation: ‘int64’ is for the data type, followed by a name of the variable – we called it ‘num’. Then ‘>’ puts this string into msg file in the folder we just created – ‘msg/num.msg’.

Good, now let’s have a look if everything went alright, we can open the file with gedit using ‘gedit msg/num.msg’ and we can see that the text exactly the thing as we just wrote in there using the command above.

Changing CMakeList file

The next thing we have to do is we have to edit our ‘CMakeList.txt’. just type ‘gedit CMakeList.txt’
Close to the top of the file you can see comment: “uncomment if you have defined messages”. So just uncomment the next line: ‘rosbuild genmsg()’ and save the CMakeList.txt file.

It is really easy to use ROS msg – just type in the terminal window ‘rosmsg show PACKAGE_NAME/MSG_NAME’. (you can use tab for auto-complete) press enter, and you will be able to see that your package uses this message, with the data type int64.

How to create ROS SRV?

First we need to create a directory for srv. (please note you have to be in the root directory of your package using ‘pwd’) now type: ‘mkdir srv’.

in this example, we won’t create our own srv, we will copy one from a package that ROS has already installed. So type, ‘roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv’.
Explanation: ‘roscp’ needs the following parameters: the package you want to copy a file from, followed by the filename you want to copy, and then followed by the destination folder and filename.

Now press ‘Enter’ to execute it.

Now again with srv, we have to edit the CMakeList. (just type ‘gedit CMakeList.txt’)
Look for the part it says: ‘uncomment if you have defined services’. Since we have defined services, we will have to uncomment following line ‘rosbuild_gensrv()’ and save this file.

Now we let’s have a quick look if everything went alright. We can change the directory to our srv, type in the terminal window: ‘rossrv show PACKAGE_NAME/AddTwoInts’, and it will show the data in our service file.
You can see in the output that - variables ‘A’ and ‘B’ are the input and, the ‘sum’ is the output.

Next we have to use ‘rosmake’ to build the package.
Type in ‘rosmake PACKAGE_NAME’ and press enter.
Make sure that you have zero (0) failures.

That’s it; we’ve learned how to create ROS messages and ROS services. Wasn’t that hard, right? :-)