Reference: "ROS Robot Development Practice"
Note: continue to implement communication programming under the function package learning_communication under the catkin_ws workspace
1. Service Communication Model
Second, service programming implementation
Note: The addition listener releases two numbers to the talker, and the talker adds it to the listener
1. Service programming process:
1. Create a server;
2. Create a client;
3. Add compilation options;
4. Run an executable file;
2. Custom service file srv
a. Create the srv directory under learning_communication and the AddTwoInts.srv file under the srv directory
int64 a
int64 b
---
int64 sum
b. Add function package dependency in package.xml
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
c. Add compilation options in CMakeLists.txt
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES learning_communication
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
# DEPENDS system_lib
)
add_service_files(FILES AddTwoInts.srv)
generate_messages(DEPENDENCIES std_msgs)
d. Compilation
Generate an executable file under the catkin_ws workspace:
catkin_make
3. Create a server
a. Create the server.cpp file in the following directory:
b. The server.cpp file code is as follows:
/*
AddTwoInts Server
*/
#include "ros/ros.h"
#include "learning_communication/AddTwoInts.h"
//service回调函数,输入参数req,输出参数res
bool add(learning_communication::AddTwoInts::Request &req,
learning_communication::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节点初始化
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;
}
c. Implementation logic
(1) Initialize the ROS node;
(2) Create a Server instance;
(3) Wait for the service request circularly and enter the callback function;
(4) Complete the service function processing in the callback function and feed back the response data;
4. Create a client
a. Create the client.cpp file in the following directory:
b. The client.cpp file code is as follows:
/*
AddTwoInts Client
*/
#include <cstdlib>
#include "ros/ros.h"
#include "learning_communication/AddTwoInts.h"
int main(int argc, char **argv)
{
//Ros节点初始化
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;
//创建一个client,请求add_two_int service,service消息类型是learning_communication::AddTwoInts
ros::ServiceClient client = n.serviceClient<learning_communication::AddTwoInts>("add_two_ints");
//创建learning_communication::AddTwoInts类型的service消息
learning_communication::AddTwoInts srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);
//发布service请求,等待加法运算的应答结果
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;
}
c. Implementation logic
(1) Initialize ROS node;
(2) Create Client instance;
(3) Publish service request data;
(4) Wait for the response result after Server processing;
5. Add compilation options
Compile method
Compile the code in the CMakeLists.txt file:
(1) Set the code to be compiled and the executable file generated;
(2) Set the link library;
(3) Set the dependency;
Specific operation
Open the CMakeLists.txt file in the learning_communication function package and add the following code:
add_executable(server src/server.cpp)
target_link_libraries(server ${catkin_LIBRARIES})
add_dependencies(server ${PROJRCT_NAME}_gencpp)
add_executable(client src/client.cpp)
target_link_libraries(client ${catkin_LIBRARIES})
add_dependencies(client ${PROJRCT_NAME}_gencpp)
Generate
Generate an executable file under the catkin_ws workspace:
catkin_make
6. Run the executable file
Note that every time you open a new terminal to add environment variables: (if you need to manually add)
source ~/test/catkin_ws/devel/setup.bash
a. Please
open the terminal of ROS Master :
roscore
b. Start the server and
open a new terminal:
rosrun learning_communication server
c. Start the client and
open the terminal:
rosrun learning_communication client 3 4