ROS 学习笔记(三):自定义服务数据srv+Server+Client 示例运行
一、自定义服务数据:
1.向功能包添加自定义服务文件(AddTwoInts.srv)
cd ~/catkin_ws/src/mypackage
mkdir srv
cd srv
2.生成并编辑自定义服务文件:
gedit AddTwoInts.srv
添加内容:
int64 a
int64 b
---
int64 sum
3.修改添加~/catkin_ws/src/mypackage/package.xml 文件中内容
...<build_depend>message_generation</build_depend><exec_depend>message_runtime</exec_depend>
...
4.修改添加~/catkin_ws/src/mypackage/CMakeList.txt 文件中内容
(message_generation 同时针对话题消息和服务消息类型描述文件生成相应的代码)
// 添加生成依赖的功能包
find_package(catkin REQUIRED COMPONENTS
...
message_generation
)// 添加需要编译的.srv文件
add_service_files(FILESAddTwoInts.srv
)
5.编译后查看服务.srv
cd ~/catkin_ws
rosservice list
注意:对于AddTwoInts.h 是 AddTwoInts.srv文件在编译后自动生成的
二、Server+Client 示例:
1,~/catkin_ws/mypackage/src 目录中,编写俩个源文件server.cpp和client.cpp
(编译器编写或文本编写)
server.cpp 源码:
#include "ros/ros.h"
#include "mypackage/AddTwoInts.h"// 回调函数部分(实现服务功能)
bool add(mypackage::AddTwoInts::Request &req,mypackage::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 nh;// 创建名为 "add_two_ints" server 实例示例:ros::ServiceServer service = nh.advertiseService("add_two_ints", add);ROS_INFO("Ready to add two ints!");ros::spin();return 0;
}
client.cpp 源码:
#include "ros/ros.h"
#include "mypackage/AddTwoInts.h"
#include <cstdlib>int main(int argc, char **argv)
{// ROS节点初始化ros::init(argc, argv, "add_two_ints_client");// 终端获取两个数if (argc != 3){ROS_INFO("usage: rosrun package_name executable_name arguments");return 1;}// 创建节点句柄ros::NodeHandle nh;// 创建一个client,请求 "add_two_ints" service :ros::ServiceClient client = nh.serviceClient<mypackage::AddTwoInts>("add_two_ints");mypackage::AddTwoInts srv;srv.request.a = atol(argv[1]);srv.request.b = atol(argv[2]);// 发布service 请求,等待应答if (client.call(srv)){ROS_INFO("with the given [%ld] and [%ld],the sum is: [%ld]", srv.request.a, srv.request.b, (long int)srv.response.sum);}else{ROS_ERROR("Failed to call service: add_two_ints");return 1;}return 0;
}
2 编译功能包源码(catkin_make方式编译)
打开 CMakeList.txt 文件:
1)设置头文件相对位置,默认为功能包的所在目录
2)生成可执行文件 talker 和 listener
3)设置链接库(系统或第三方库函数)此处并没有使用其他库
4)设置依赖
5) 生成server和client 两个可执行文件于 ~/catkin_ws/devel/lib/mypackage 中
CMakeList.txt 添加修改代码如下:
include_directories(include ${catkin_INCLUDE_DIRS})add_executable(server src/server.cpp)
add_dependencies(server ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(server ${catkin_LIBRARIES})add_executable(client src/client.cpp)
add_dependencies(client ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(client ${catkin_LIBRARIES})
3.终端工作空间根目录 ~/catkin_ws 开始编译
cd ~/catkin_ws
catkin_make
source devel/setup.bash // 已在于终端环境变量文件中设置的无需重新设置
三、直接运行 server client
1.新建终端,运行ros master
cd ~/catkin_ws
roscore
2.新建终端,运行服务器节点 server
rosrun mypackage server
3.新建终端,运行客户端节点 client
rosrun mypackage client 100 222
4.结果显示:
[INFO] [146235634.23453542]:with the given [100] and [222],the sum is: [322]
四、创建launch启动文件运行
1.创建launch文件夹,创建启动文件 mypackage.launch
cd catkin_ws/src/mypackage/
mkdir launch
cd launch/
touch mypackage.launch
mypackage.launch文件内容,设置同时启动服务节点:
<launch><node pkg="mypackage" type="server" name="add_two_ints_server" output="screen"/>
</launch>
2.运行launch文件并查看消息(默认会运行roscore)
roslaunch mypackage mypackage.launch
3.新建终端,运行client 执行文件输入两个整型参数:
rosrun mypackage client 100 100
4.结果显示:
[INFO] [146235634.23453542]:with the given [100] and [100],the sum is: [200]