More standard types of messages have been defined in Ros, and you can customize your own message types with these standard types of messages. This is useful in complex data transfers, such as when a node interacts with a server, it is possible to transfer multiple parameters to the server and return the corresponding results. In order to ensure the completeness of the example, each step will be detailed.
The basic idea is similar to the example of creating talker and listener, with the following steps:
- Create a Workspace workspace (similar to a VS solution, to manage many projects);
- Set up Package packages (similar to the project under VS);
- Create MSG and SRV files;
- Write service node and client node code;
- Compile with Rosmake (Catkin_make is also possible, but slightly different, please refer to another blog post for Ros Knowledge (3));
- Run with Rosrun;
1.1. Create a working space
Before you start a specific job, create a workspace and set the environment variable to ~/.BASHRC for the workspace, and if you want to see the existing spatial path, you can use the query command
Echo $ROS _package_path
You will see the following information:
/home/horsetail/dev/rosbook:/home/horsetail/catkin_ws/src:/opt/ros/jade/share:/opt/ros/jade/stacks
The creation space here is actually the first to create a folder, and then set the path of the folder to the environment variable ~/.BASHRC. For example we create a directory ~/dev/rosbook as a workspace here.
First execute the command:
$ cd ~mkdir-p dev/rosbook
Then add the created path to the environment variable and execute the following command:
"Export Ros_package_path=~/dev/rosbook:${ros_package_path}" >> ~/. bashrc$. ~/.BASHRC
In this way, we have completed the configuration of the workspace, note: When Ros is installed, the environment variables of ROS must be added to the ~/.BASHRC. There is also a need to put Ros. The next step is to create the package under this space.
1.2. Create a Package
The package can be created manually, but it is cumbersome and, for convenience, it is best to use the ROSCREATE-PKG command-line tool, which has the following format:
roscreate-pkg [Package_name] [depend1] [Depend2] [Depend3] ...
The command line contains the name of the package to be created, dependent on the package.
In our example, create a new package called Mypacakge1, with the following command:
$ cd ~/dev/rosbook$ roscreate-pkg mypackage1 std_msgs roscpp rospy
After a while, the following message pops up, indicating that the creation was successful:
Created Package directory/home/horsetail/dev/rosbook/mypackage1created include directory/home/horsetail/dev/rosbook/mypackage1/include/ mypackage1created cpp Source Directory/home/horsetail /dev/rosbook/mypackage1/file/home /horsetail/dev/rosbook/mypackage1/makefilecreated Package file/home/horsetail/dev/rosbook/mypackage1/ manifest.xmlcreated package file/home/horsetail/dev/rosbook/mypackage1/cmakelists.txtcreated package file/home/horsetail/dev/rosbook/mypackage1/mainpage.doxplease edit Mypackage1/manifest.xml and mainpage.dox to finish creating your package /span>
All right, this completes the creation of the package, we found that in the Mypackage1 directory there is a src folder, we are next to the web here to add the source program.
1.3. creating MSG and SRV files
First, under the Mypackage1 Feature Pack, create a msg folder and create a new file in it mypackage_msg1.msg, which will customize the type of message in this file, adding the following code to the file:
Int32 Aint32 Bint32 C
Now edit CMakeList.txt, remove # from the #rosbuild_genmsg () line, and then use the Rosmake command to compile the Feature pack:
$ rosmake Mypackage1
To check for correctness, use the rosmsg command:
$ rosmsg Show MYPACKAGE1/MYPACKAGE1_MSG1
If you see the same content as the file, the instructions are compiled correctly.
Now create a new SRV file, create an SRV folder under the Mypackage1 folder, create a new file Mypackage1_srv1.srv under the SRV folder, and add the following code:
int32 Aint32 Bint32 C---sum
Edit CMakeList.txt, remove # from the #rosbuild_genmsg () line, and then compile the feature pack with the Rosmake command to verify correctness with a single command:
$ rossrv Show Mypackage1/mypackage1_srv1
If you see the same content as the file, the instructions are compiled correctly.
1.4. Writing service node and client node code
Next build the code to validate the request response in the service, create a new file Example_srv_request.cpp under MYPACKAGE1/SRC, and add the following code:
#include"Ros/ros.h"#include"Mypackage1/mypackage_srv1.h"BOOLAdd (Chapter2_tutorials::chapter2_srv1::request &req, Chapter2_tutorials::chapter2_srv1::response&Res) {Res.sum= req. A + req. B +req. C Ros_info ("request:a=%ld, B=%ld c=%ld", (int) Req. A, (int) Req. B, (int) req. C); Ros_info ("Sending back response: [%ld]", (int) Res.sum); Returntrue;}intMainintargcChar**argv) {Ros::init (argc, argv,"Add_3_ints_server"); Ros::nodehandle N; Ros::serviceserver Service= N.advertiseservice ("add_3_ints", add); Ros_info ("Ready to Add 3 ints."); Ros::spin (); Return0;}
To create a new file Example_srv_respone.cpp under MYPACKAGE1/SRC, add the following code:
#include"Ros/ros.h"#include"Mypackage1/mypackage_srv1.h"#include<cstdlib>intMainintargcChar**argv) {Ros::init (argc, argv,"add_3_ints_client"); if(ARGC! =4) {Ros_info ("usage:add_3_ints_client A B C"); Return1; } ros::nodehandle N; Ros::serviceclient Client= N.serviceclient<chapter2_tutorials::chapter2_srv1> ("add_3_ints"); CHAPTER2_TUTORIALS::CHAPTER2_SRV1 SRV; SRV.REQUEST.A= Atoll (argv[1]); srv.request.b= Atoll (argv[2]); Srv.request.c= Atoll (argv[3]); if(Client.call (SRV)) {Ros_info ("Sum:%ld", (Long int) Srv.response.sum); } Else{ros_error ("Failed to call service add_two_ints"); Return1; } return0;}
Next, create the code to validate the message delivery, create a new file Example_talker_msg.cpp under MYPACKAGE1/SRC, and add the following code:
#include"Ros/ros.h"#include"Mypackage1/mypackage1_msg1.h"#include<sstream>intMainintargcChar**argv) {Ros::init (argc, argv,"Example_talker_msg"); Ros::nodehandle N; Ros::P ublisher Pub= N.advertise<chapter2_tutorials::chapter2_msg1> ("message", +); Ros::rate Loop_rate (Ten); while(Ros::ok ()) {CHAPTER2_TUTORIALS::CHAPTER2_MSG1 msg; Msg. A=1; Msg. B=2; Msg. C=3; Pub.publish (msg); Ros::spinonce (); Loop_rate.Sleep(); } return0;}
To create a new file Example_listener_msg.cpp under MYPACKAGE1/SRC, add the following code:
#include"Ros/ros.h"#include"Mypackage1/mypackage1_msg1.h"void Messagecallback (const chapter2_tutorials::chapter2_msg1::constptr&msg) {Ros_info ("I heard: [%d] [%d] [%d]", Msg->a, Msg->b, msg->C);}intMainintargcChar**argv) {Ros::init (argc, argv,"Example_listener_msg"); Ros::nodehandle N; Ros::subscriber Sub= N.subscribe ("message", +, Messagecallback); Ros::spin (); Return0;}
Well, this completes the test code for the service and the message.
1.5. compiling with the Rosmake
Next, tell the compiler how to find these two files. You need to open mypackage1/cmakelists.txt and add two lines of command at the end of the file:
Rosbuild_add_executable (example_srv_request src/example_srv_request. CPP) rosbuild_add_executable (example_srv_respone src/example_srv_respone. CPP ) rosbuild_add_executable (example_talker_msg src/example_talker_msg.cpp) rosbuild_add_ Executable (example_listener_msg src/example_listener_msg.cpp)
The structure of the file after the addition is this:
Cmake_minimum_required (VERSION2.4.6) include ($ENV {ros_root}/core/rosbuild/rosbuild.cmake) # Set the build type. Options are:# Coverage:W/debug Symbols,W/O optimization,W/code-coverage# Debug:W/debug Symbols,W/o optimization# Release:W/o debug Symbols,W/optimization# Relwithdebinfo:W/debug Symbols,W/optimization# Minsizerel:W/o debug Symbols,W/optimization, stripped Binaries#set (Ros_build_type relwithdebinfo) rosbuild_init () #set The default path forBuilt executables to the"bin"directoryset (Executable_output_path ${project_source_dir}/bin) #set the default path forBuilt libraries to the"Lib"directoryset (Library_output_path ${project_source_dir}/Lib) #uncommentifYou have defined messagesrosbuild_genmsg () #uncommentifYou have defined servicesrosbuild_gensrv () #common commands forBuilding C + +Executables and Libraries
Rosbuild_add_executable (example_srv_request src/example_srv_request. CPP) rosbuild_add_executable (Example_srv_respone src/example_srv_respone. CPP)rosbuild_add_executable (example_talker_msg src/example_talker_msg.cpp) rosbuild_add_executable ( Example_listener_msg src/example_listener_msg.cpp)
This MYPACKAGE1 package is compiled with the Rosmake command. Execute the following command:
$ rosmake Mypackage1
Output the following information:
[Email protected]:~$ roscore ... logging to/home/horsetail/.ros/log/6eae5b9c-628d-11e5-8bd7-3859f9722953/roslaunch-horsetail-book-6447. logchecking Log DirectoryForDisk usage. This could take awhile. Press Ctrl-C to InterruptDone checking logFile disk usage. Usage is <1gb.started roslaunch Server http://Horsetail-book:44362/ros_comm version1.11.13summary========parameters */rosdistro:jade */rosversion: 1.11. 13nodesauto-starting New Masterprocess[master]: Started with PID [6459]ros_ Master_uri=http://horsetail-book:11311/
... (The content is too long, omitted)
[Rosmake] Results: [Rosmake] Built 26 packages with the Span style= "color: #800080;" >0 failures. [Rosmake] Summary output to directory [Rosmake]/home/horsetail/.ros/rosmake/rosmake_output- 20150924-164014
Wow, the compilation passed, you notice actually also compiles with Catkin, the amount. Let's run for a bit.
1.4. Operation
First open a new terminal, initiate the initialization of ROS, execute the command:
$ roscore
First verify the service request and the corresponding function, you need to execute the following commands in separate windows:
example_talker_msg example_listener_msg
You can see the window requesting listener, which displays the following information:
[INFO] [1443154332.742277621]: I heard:[1][2][3][INFO] [1443164722.557755739]: I heard:[1][2][3 ][INFO] [1443164744.557858055]: I heard:[1][2][3]
Okay, now that the service SRV and message msg validation is complete.
1.5. Source code
Finally, attach Source: mypackage1.tar.gz (This is the example of the second chapter of the ROS Robotics program, which contains examples of messages and services)
Resources
[1]. Aaron Martinez Enrique Fern Andez, Ros robot program Design [B], p14-42, 2014.
Ros Knowledge (5)----Examples of messages and services