ROS Noetic入门笔记(一)在ubuntu20.04中安装ROS Noetic并简单测试
ROS Noetic入门笔记(二)ROS Noetic创建工作空间和功能包
ROS Noetic入门笔记(三)ROS Noetic中的常用命令及工具
ROS Noetic入门笔记(四)使用rqt_console和roslaunch
ROS Noetic入门笔记(五)创建ROS话题消息(msg)和服务数据(srv
ROS Noetic入门笔记(六)使用C++编写简单的Publisher与Suscriber
ROS Noetic入门笔记(七)使用Python编写简单的Publisher与Suscriber
ROS Noetic入门笔记(八)运行Publisher与Suscriber
ROS Noetic入门笔记(九)使用C++编写简单的Server与Client
ROS Noetic入门笔记(十)使用Python编写简单的Server与Client
ROS Noetic入门笔记(十一)使用rosbag实现数据记录与回放
文章目录
1.创建Server
(1)使用roscd跳转到beginner_tutorials功能包目录
先设置环境变量
$ cd ~/catkin_ws
$ source ./devel/setup.bash
然后roscd到beginner_tutorials功能包
$ roscd beginner_tutorials
(2)在src目录下创建add_two_ints_server.cpp文件并粘贴以下代码
#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;
}
(3)add_two_ints_server.cpp代码解释
#include "ros/ros.h"
#include "beginner_tutorials/AddTwoInts.h"
beginner_tutorials/AddTwoInts.h头文件是我们在ROS Noetic入门笔记(五)创建ROS话题消息(msg)和服务数据(srv)中创建服务数据类型的描述文件AddTwoInts.srv时自动生成的
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;
}
这里两个变量相加并将结果放入应答数据中,然后log一些request 和response相关内容,执行完返回true
ros::ServiceServer service = n.advertiseService("add_two_ints", add);
创建一个名为add_two_ints的Server,注册回调函数add
2.创建Client
(1)在src目录下创建add_two_ints_client.cpp文件并粘贴以下代码到文件中
#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;
}
(2)add_two_ints_client.cpp代码解释
ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");
创建一个add_two_ints的client实例,指定服务类型为beginner_tutorials::AddTwoInts
beginner_tutorials::AddTwoInts srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);
这里实例化一个服务数据类型的变量,包含2个成员,request和response
if (client.call(srv))
调用服务,服务调用为阻塞式的,调用成功call() 会返回true,同时srv.response中的值有效,调用不成功call() 会返回false,同时srv.response中的值无效
3.编译功能包
编辑CMakeLists.txt ,文件位于~/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)
这将创建两个可执行文件,add_two_ints_server和add_two_ints_client,默认在~/catkin_ws/devel/lib/beginner_tutorials文件夹下,你可以直接调用可执行文件或使用rosrun调用
修改完成后,回到工作空间的根目录下开始编译
$ cd ~/catkin_ws
$ catkin_make
如果编译失败了,请确认一下是否在ROS Noetic入门笔记(五)创建ROS话题消息(msg)和服务数据(srv)中创建了AddTwoInts.srv
4.运行roscore
roscore
5.运行Server
记得进入工作空间先source
$ source ./devel/setup.bash
$ rosrun beginner_tutorials add_two_ints_server
6.运行Client
记得进入工作空间先source
$ source ./devel/setup.bash
$ rosrun beginner_tutorials add_two_ints_client 1 3
上一篇:ROS Noetic入门笔记(八)运行Publisher与Suscriber
下一篇:ROS Noetic入门笔记(十)使用Python编写简单的Server与Client