Creating msg and srv files

In this section, we are going to learn how to create msg and srv files for use in our nodes. They are files where we put a specification about the type of data to be transmitted and the values of this data. ROS will use these files to create the necessary code for us to implement the msg and srv files to be used in our nodes.

Let's start with the msg file first.

In the example used in the Building the node section, we created two nodes with a standard type message. Now we are going to learn how to create custom messages with the tools that ROS has.

First, create a new msg folder in our chapter2_tutorials package; create a new chapter2_msg1.msg file and add the following lines:

int32 A 
int32 B 
int32 C 

Now, edit package.xml and remove <!-- --> from the <build_depend>message_generation</build_depend> and <run_depend>message_runtime</run_depend> lines.

Edit CMakeList.txt and add the message_generation line as follows:

find_package(catkin REQUIRED COMPONENTS 
roscpp 
  std_msgs 
  message_generation 
) 

Find the next lines, uncomment and add the name of the new message as follows:

## Generate messages in the 'msg' folder 
add_message_files( 
        FILES 
        chapter2_msg1.msg 
) 
 
## Generate added messages and services with any dependencies 
listed here generate_messages( DEPENDENCIES std_msgs )

And now you can compile using the following lines:

    $ cd ~/dev/catkin_ws/
    $ catkin_make  

To check whether all is OK, you can use the rosmsg command:

    $ rosmsg show chapter2_tutorials/chapter2_msg1  

If you see the same content as that of the chapter2_msg1.msg file, all is OK.

Now we are going to create a srv file. Create a new folder in the chapter2_tutorials folder with the name srv, create a new chapter2_srv1.srv file and add the following lines:

int32 A 
int32 B 
int32 C 
--- 
int32 sum 

To compile the new msg and srv files, you have to uncomment the following lines in the package.xml and CMakeLists.txt files. These lines permit the configuration of the messages and services and tell ROS how and what to build.

First of all, open the package.xml folder from your chapter2_tutorials package as follows:

    $ rosed chapter2_tutorials package.xml  

Search for the following lines and uncomment them:

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

Open CMakeLists.txt using the following command:

    $ rosed chapter2_tutorials CMakeLists.txt  

Find the following lines, uncomment them and complete them with the correct data:

catkin_package( 
  CATKIN_DEPENDS message_runtime 
) 

To generate messages, you need to add the message_generation line in the
find_package section:

find_package(catkin REQUIRED COMPONENTS 
roscpp 
  std_msgs 
message_generation 
) 

Add the names of the message and service files in the add_message_files section, as follows:

## Generate messages in the 'msg' folder 
add_message_files( 
    FILES 
    chapter2_msg1.msg 
) 
 
## Generate services in the 'srv' folder 
add_service_files( 
    FILES 
    chapter2_srv1.srv 
) 

Uncomment the generate_messages section to make sure that the generation of messages and services can be done:

## Generate added messages and services with any dependencies 
listed here generate_messages( DEPENDENCIES std_msgs )

You can test whether all is OK using the rossrv tool as follows:

    $ rossrv show chapter2_tutorials/chapter2_srv1  

If you see the same content as that of the chapter2_srv1.srv file, all is OK.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.248.1