Article Directory
1. Create msg and srv
Create msg and srv folders at the same level as the src of the package, and create your own messages and services in them. Such as:
my_msg.msg
Header header
string child_frame_id
geometry_msgs/PoseWithCovariance pose
geometry_msgs/TwistWithCovariance twist
my_srv.srv
Note: Above the dotted line is the request (request), below the dotted line is the corresponding (response)
int64 a
int64 b
---
int64 sum
2. Configure msg and srv
- CmakeList.txt configuration
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
message_generation
)
add_message_files(FILES my_msg.msg)
add_service_files(FILES my_srv.srv)
generate_messages(DEPENDENCIES std_msgs)
catkin_package()
include_directories(include ${
catkin_INCLUDE_DIRS})
- package.xml placement
In fact, if you add message_generation when creating the package, you don’t need to add the following configuration. Similarly, cmakeList.txt does not need to add message_generation. I added it here to avoid forgetting to add it.
<build_depend>message_generation</build_depend>
<build_export_depend>message_generation</build_export_depend>
<exec_depend>message_generation</exec_depend>
3. Use service
The usage of common messages is the same as the messages created by yourself. Messages have been used in publishers and subscribers, so I won't repeat them here. Let's start talking about services.
Directly paste the code and own comments:
- service side
#include "ros/ros.h"
#include "test_service/my_srv.h"//创建srv的头文件
bool add(test_service::my_srv::Request &req, test_service::my_srv::Response &res)//求和服务
{
res.sum = req.a + req.b;//求和服务,相应的值存入response里面
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, "node_client");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("add_two_ints", add);//建立service,并在ROS内发布出来,这里发布的是求和
ROS_INFO("Ready to add two ints.");
ros::spin();
return 0;
}
- client side
#include "ros/ros.h"
#include "test_service/my_srv.h"
#include <cstdlib>
int main(int argc, char **argv)
{
ros::init(argc, argv, "node_server");
if (argc != 3)
{
ROS_INFO("usage: add_two_ints_client X Y");
return 1;
}
ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<test_service::my_srv>("add_two_ints");//创建client
test_service::my_srv srv;
srv.request.a = atoll(argv[1]);//利用终端传入两个参数
srv.request.b = atoll(argv[2]);
if (client.call(srv))//调用service,调用成功返回true
{
ROS_INFO("Sum: %ld", (long int)srv.response.sum);
}
else
{
ROS_ERROR("Failed to call service add_two_ints");
return 1;
}
return 0;
}
- Configuration add
add_executable(node_server src/server_main.cpp)
target_link_libraries(node_server ${
catkin_LIBRARIES})
add_dependencies(node_server ${
${
PROJECT_NAME}_EXPORTED_TARGETS} ${
catkin_EXPORTED_TARGETS})
add_executable(node_client src/client_main.cpp)
target_link_libraries(node_client ${
catkin_LIBRARIES})
add_dependencies(node_client ${
${
PROJECT_NAME}_EXPORTED_TARGETS} ${
catkin_EXPORTED_TARGETS})