ROS学习手记9 - 编写编译和检验Service Node

  • 格式:docx
  • 大小:26.46 KB
  • 文档页数:6

ROS学习笔记9 - 编写编译和检验Service NodeWriting a Service NodeHere we'll create the service ("add_two_ints_server") node which will receive two ints and return the sum.Change directories to your beginner_tutorials package you created in your catkin workspace previous tutorials:cd ~/catkin_ws/src/beginner_tutorialsPlease make sure you have followed the directions in the previous tutorial for creating the service needed in this tutorial, creating the AddTwoInts.srv(be sure to choose the right version of build tool you're using at the top of wiki page in the link).The CodeCreate the src/add_two_ints_server.cpp file within thebeginner_tutorials package and paste the following inside it:切换行号显示#include "ros/ros.h"#include "beginner_tutorials/AddTwoInts.h"bool add(beginner_tutorials::AddTwoInts::Request &req,beginner_tutorials::AddTwoInts::Response &res){res.sum = req.a + req.b;ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b); ROS_INFO("sending back response: [%ld]", (long int)res.sum);return true;}int main(int argc, char **argv){ros::init(argc, argv, "add_two_ints_server");ros::NodeHandle n;ros::ServiceServer service = n.advertiseService("add_two_ints", add); ROS_INFO("Ready to add two ints.");ros::spin();return 0;}The Code ExplainedNow, let's break the code down.切换行号显示#include "ros/ros.h"#include "beginner_tutorials/AddTwoInts.h"beginner_tutorials/AddTwoInts.h is the header file generated from the srv file that we created earlier. 位置:~/catkin_ws/include/beginner_tutorials切换行号显示bool add(beginner_tutorials::AddTwoInts::Request &req,beginner_tutorials::AddTwoInts::Response &res)This function provides the service for adding two ints, it takes in the request and response type defined in the srv file(AddTwoInts.srv) and returns a boolean.切换行号显示{res.sum = req.a + req.b;ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b); ROS_INFO("sending back response: [%ld]", (long int)res.sum);return true;}Here the two ints are added and stored in the response. Then some information about the request and response are logged. Finally the service returns true when it is complete.切换行号显示ros::ServiceServer service = n.advertiseService("add_two_ints", add);Here the service is created and advertised over ROS.对比一下前一节publisher nodes的创建:ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter" , 1000); //topic name “chatter”, bufferSize:1000对比一下后面client相关声明语句:ros::ServiceClient client =n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");This creates a client for the add_two_ints service. Theros::ServiceClient object is used to call the service later on.Writing the Client NodeThe CodeCreate the src/add_two_ints_client.cpp file within thebeginner_tutorials package and paste the following inside it:切换行号显示#include "ros/ros.h"#include "beginner_tutorials/AddTwoInts.h"#include <cstdlib>int main(int argc, char **argv){ros::init(argc, argv, "add_two_ints_client");if (argc != 3){ROS_INFO("usage: add_two_ints_client X Y");return 1;}ros::NodeHandle n;ros::ServiceClient client =n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");beginner_tutorials::AddTwoInts srv;srv.request.a = atoll(argv[1]);srv.request.b = atoll(argv[2]);if (client.call(srv)){ROS_INFO("Sum: %ld", (long int)srv.response.sum);}else{ROS_ERROR("Failed to call service add_two_ints");return 1;}return 0;}The Code ExplainedNow, let's break the code down.切换行号显示ros::ServiceClient client =n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");This creates a client for the add_two_ints service. The ros::ServiceClient object is used to call the service later on.切换行号显示beginner_tutorials::AddTwoInts srv;srv.request.a = atoll(argv[1]);srv.request.b = atoll(argv[2]);Here we instantiate列举 an autogenerated service class, and assign values into its request member. A service class contains two members, request and response. It also contains two class definitions, Request and Response.切换行号显示if (client.call(srv))This actually calls the service. Since service calls are blocking, it will return once the call is done. If the service call succeeded, call() will return true and the value in srv.response will be valid. If the call did not succeed, call() will return false and the value in srv.response will be invalid.Building your nodesAgain edit the beginner_tutorials CMakeLists.txt located at~/catkin_ws/src/beginner_tutorials/CMakeLists.txt and add the following at the end:https:///ros/catkin_tutorials/master/create_package_srv client/catkin_ws/src/beginner_tutorials/CMakeLists.txt切换行号显示add_executable(add_two_ints_server src/add_two_ints_server.cpp)target_link_libraries(add_two_ints_server ${catkin_LIBRARIES})add_dependencies(add_two_ints_server beginner_tutorials_gencpp)add_executable(add_two_ints_client src/add_two_ints_client.cpp)target_link_libraries(add_two_ints_client ${catkin_LIBRARIES})add_dependencies(add_two_ints_client beginner_tutorials_gencpp)This will create two executables, add_two_ints_server andadd_two_ints_client, which by default will go into package directory of your devel space, located by default at~/catkin_ws/devel/lib/share/<package name>. You can invoke executables directly or you can use rosrun to invoke调用 them. They are not placed in '<prefix>/bin' because that would pollute the PATH when installing your package to the system. If you wish for your executable to be on the PATH at installation time, you can setup an install target, see:catkin/CMakeLists.txtFor more detailed discription of the CMakeLists.txt file see:catkin/CMakeLists.txtNow run catkin_make:# In your catkin workspacecd ~/catkin_wscatkin_makeIf your build fails for some reason:make sure you have followed the directions in the previous tutorial: creating the AddTwoInts.srv.Now that you have written a simple service and client, let's examine the simple service and client.Examining the Simple Service and ClientRunning the ServiceLet's start by running the service:$ rosrun beginner_tutorials add_two_ints_server (C++)$ rosrun beginner_tutorials add_two_ints_server.py (Python)You should see something similar to:Ready to add two ints.Running the ClientNow let's run the client with the necessary arguments:$ rosrun beginner_tutorials add_two_ints_client 1 3 (C++)$ rosrun beginner_tutorials add_two_ints_client.py 1 3 (Python) You should see something similar to:Requesting 1+31 + 3 = 4Now that you've successfully run your first server and client, let's learn how to record and play back data.Further examples on Service and Client nodesIf you want to investigate further and get a hands-on example, you can get one here. A simple Client and Service combination shows the use of custom message types. The Service node is written in C++ while the Client is available in C++, Python and LISP.。