Creating a ROS msg and srv

From Lofaro Lab Wiki
Jump to: navigation, search

Creating a ROS msg and srv

  • msg: msg files are simple text files that describe the fields of a ROS message. They are used to generate source code for messages in different languages.
  • srv: an srv file describes a service. It is composed of two parts: a request and a response.

Creating a msg

1) Move into the beginner_tutorials folder, create a msg directory, and create a new msg file:

  cd ~/catkin_ws/src/beginner_tutorials
   mkdir msg
   echo "int64 num" > msg/Num.msg

1b) Use rosmsg show [message type] to make sure ROS sees the new msg:

  rosmsg show beginner_tutorials/Num

Or

  rosmsg show Num

The message int64 num will display. The second option is if you do not have the name of the package. If int64 num does not display, see step 6b in Using rqt_console and roslaunch and try again.

Creating a srv

1) Move into your beginner_tutorials folder and create a src folder:

  roscd beginner_tutorials
   mkdir srv

2) Use roscp [package_name] [file_to_copy_path] [copy_path] to copy files from one folder to another:

  roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

This will copy a premade srv file into the srv folder.

2b) Use rossrv show <service type> to make sure ROS sees the new srv:

  rossrv show beginner_tutorials/AddTwoInts

The message with two int64 a and b will show, then a '---' line, followed by an int64 sum.

Further

1) Once you have made the msg and srv, you will need to rebuild the package:

  rosmake beginner_tutorials