1. We need to create a Server and Client, CLIent sends two int variables that need to be added, and the Server returns the added value after the addition
2. Create a srv type file named AddTwoInts.srv under the directory catkin_ws/src/learning_sun/srv, and enter:
int64 a
int64 b
---
int64 sum
3. After writing, you need to compile and connect pcakage.xml and add dependent files:
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
4. After modifying the package.xml file, we continue to modify the CMakeList.txt file and add the last piece of code above catkin_package: Note that this piece of
code must be placed before generate_messages() before it can be compiled
add_service_files(
FILES
AddTwoInts.srv
)
5. Then we create a cpp type file named server.cpp in the /catkin_ws/learning_sun/src/ directory, and enter the code:
#include "ros/ros.h"
//这里使用的头文件是learning_sun/AddTwoInts.h,改头文件根据我们之间创建的服务数据类型的描述文件AddTwoInts.srv自动生成
#include "learning_sun/AddTwoInts.h"
//service回调函数,输入参数req,输出参数res
bool add(learning_sun::AddTwoInts::Request &req,
learning_sun::AddTwoInts::Response &res)
{
//将参数中的请求数据相加,结果放入应答变量中
res.sum = req.a + req.b;
ROS_INFO("request: x=%1d,y=%1d" ,(long int)req.a,(long int)req.b);
ROS_INFO("sending back response: [%1d]" ,(long int)res.sum);
return true;
}
int main(int argc, char **argv)
{
//ROS节点初始化,创建节点名称
ros::init(argc, argv, "add_two_ints_server");
//创建节点句柄
ros::NodeHandle n;
//创建一个名为add_two_ints的server,注册回调函数add()
ros::ServiceServer service = n.advertiseService("add_two_ints",add);
//循环等待回调函数
ROS_INFO("Ready to add two ints!");
ros::spin();
return 0;
}
6. After writing the server, we also need a client Client. We create a cpp type file named Clinet.cpp in the src directory and enter the code:
#include <cstdlib>
#include "ros/ros.h"
#include "learning_sun/AddTwoInts.h"
int main(int argc, char **argv)
{
//ROS节点初始化
ros::init(argc, argv, "add_two_ints_client");
//从终端命令行获取两个加数,if(argc != 3)的意思是引导用户输入不超过三个参数,即输入两个参数
if(argc != 3)
{
ROS_INFO("usage: add_two_ints_client X and Y");
return 1;
}
//创建节点句柄
ros::NodeHandle n;
/*
*功能:创建一个add_two_ints的Client实例,指定服务类型为learning_sun::AddTwoInts
*/
ros::ServiceClient client = n.serviceClient<learning_sun::AddTwoInts>("add_two_ints");
/*
*功能:实例化一个服务类型数据的变量,该变量包含两个成员:request与response,
* 将节点运行时输入的两个参数作为需要相加的两个整数型存储到变量中
*/
learning_sun::AddTwoInts srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);
//进行服务调用,如果调用过程会发生阻塞,如果调用成功则返回ture,调用失败则返回false.srv.response则不可用
if(client.call(srv))
{
ROS_INFO("Sum: %1d", (long int)srv.response.sum);
}
else
{
ROS_ERROR("Failed to call service add_two_ints");
return 1;
}
return 0;
}
7. After the Client is written, we need to compile these two files, we need to modify the CMakeList.txt file, add or modify the following code in the file:
add_executable(server src/server.cpp)
add_executable(client src/client.cpp)
add_dependencies(server ${PROJECT_NAME}_generate_messages_cpp)
add_dependencies(client ${PROJECT_NAME}_generate_messages_cpp)
target_link_libraries(server ${catkin_LIBRARIES} )
target_link_libraries(client ${catkin_LIBRARIES} )
8. After the modification, return to the /catkin_ws directory, enter the command catkin_make
to compile, and the compilation is complete when the progress reaches 100%.
9. Open the terminal, enter roscore to enter the ROS system, open the second terminal and run the rosrun learning_sun server
command to run the server server file, and then open the third terminal, enter and rosrun learning_sun client 3 5
run the client file, (note that two parameters must be entered here), and the output result:
[ INFO] [1568978929.997013419]: Sum: 13
10. The experiment is now complete