目录
- 自定义服务数据
- 定义srv文件
- 在package.xml中添加功能包依赖
- 在CMakeLists.txt添加编译选项
- 编译生成语言相关文件
- 使用
- C++
- 编写程序
- person_server.cpp
- person_client.cpp
- 配置CMakeLists.txt
- 编译并运行
- python
- 编写程序
- person_server.py
- person_client.py
- 运行
平台:华硕 Thinker Edge R 瑞芯微 RK3399Pro
固件版本:Tinker_Edge_R-Debian-Stretch-V1.0.4-20200615
记录自【古月居】古月·ROS入门21讲 | 一学就会的ROS机器人入门教程 —— 古月居GYH P15 15.服务数据的定义与使用
自定义服务数据
定义srv文件
cd ~/catkin_workspace/src/learning_service
mkdir srv
nano ~/catkin_workspace/src/learning_service/srv/Person.srv
string name
uint8 age
uint8 sexuint8 unknown = 0
uint8 male = 1
uint8 female = 2
---
string result
在package.xml中添加功能包依赖
nano ~/catkin_workspace/src/learning_service/package.xml
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
在CMakeLists.txt添加编译选项
...
find_package(... message_generation)
...
add_service_files(FILES Person.srv)
generate_messages(DEPENDENCIES std_msgs)
...
catkin_package(... message_runtime)
...
nano ~/catkin_workspace/src/learning_service/CMakeLists.txt
编译生成语言相关文件
cd ~/catkin_workspace
catkin_make
结果
cd ~/catkin_workspace/devel/include/learning_service
ls
可见已生成如下文件:
使用
C++
编写程序
person_server.cpp
nano ~/catkin_workspace/src/learning_service/src/person_server.cpp
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************//*** 该例程将执行/show_person服务,服务数据类型learning_service::Person*/#include <ros/ros.h>
#include "learning_service/Person.h"// service回调函数,输入参数req,输出参数res
bool personCallback(learning_service::Person::Request &req,learning_service::Person::Response &res)
{// 显示请求数据ROS_INFO("Person: name:%s age:%d sex:%d", req.name.c_str(), req.age, req.sex);// 设置反馈数据res.result = "OK";return true;
}int main(int argc, char **argv)
{// ROS节点初始化ros::init(argc, argv, "person_server");// 创建节点句柄ros::NodeHandle n;// 创建一个名为/show_person的server,注册回调函数personCallbackros::ServiceServer person_service = n.advertiseService("/show_person", personCallback);// 循环等待回调函数ROS_INFO("Ready to show person informtion.");ros::spin();return 0;
}
person_client.cpp
nano ~/catkin_workspace/src/learning_service/src/person_client.cpp
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************//*** 该例程将请求/show_person服务,服务数据类型learning_service::Person*/#include <ros/ros.h>
#include "learning_service/Person.h"int main(int argc, char** argv)
{// 初始化ROS节点ros::init(argc, argv, "person_client");// 创建节点句柄ros::NodeHandle node;// 发现/show_person服务后,创建一个服务客户端,连接名为/show_person的serviceros::service::waitForService("/show_person");ros::ServiceClient person_client = node.serviceClient<learning_service::Person>("/show_person");// 初始化learning_service::Person的请求数据learning_service::Person srv;srv.request.name = "Tom";srv.request.age = 20;srv.request.sex = learning_service::Person::Request::male;// 请求服务调用ROS_INFO("Call service to show person[name:%s, age:%d, sex:%d]", srv.request.name.c_str(), srv.request.age, srv.request.sex);person_client.call(srv);// 显示服务调用结果ROS_INFO("Show person result : %s", srv.response.result.c_str());return 0;
}
配置CMakeLists.txt
nano ~/catkin_workspace/src/learning_service/CMakeLists.txt
添加
add_executable(person_server src/person_server.cpp)
target_link_libraries(person_server ${catkin_LIBRARIES})
add_dependencies(person_server ${PROJECT_NAME}_gencpp)add_executable(person_client src/person_client.cpp)
target_link_libraries(person_client ${catkin_LIBRARIES})
add_dependencies(person_client ${PROJECT_NAME}_gencpp)
- 设置需要编译的代码和生成的可执行文件;
- 设置链接库;
- 添加依赖项。
编译并运行
cd ~/catkin_workspace
catkin_make# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
roscore
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun learning_service person_server
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun learning_service person_client
python
编写程序
person_server.py
nano ~/catkin_workspace/src/learning_service/scripts/person_server.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-########################################################################
#### Copyright 2020 GuYueHome (www.guyuehome.com). ###
######################################################################### 该例程将执行/show_person服务,服务数据类型learning_service::Personimport rospy
from learning_service.srv import Person, PersonResponsedef personCallback(req):# 显示请求数据rospy.loginfo("Person: name:%s age:%d sex:%d", req.name, req.age, req.sex)# 反馈数据return PersonResponse("OK")def person_server():# ROS节点初始化rospy.init_node('person_server')# 创建一个名为/show_person的server,注册回调函数personCallbacks = rospy.Service('/show_person', Person, personCallback)# 循环等待回调函数print "Ready to show person informtion."rospy.spin()if __name__ == "__main__":person_server()
person_client.py
nano ~/catkin_workspace/src/learning_service/scripts/person_client.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-########################################################################
#### Copyright 2020 GuYueHome (www.guyuehome.com). ###
######################################################################### 该例程将请求/show_person服务,服务数据类型learning_service::Personimport sys
import rospy
from learning_service.srv import Person, PersonRequestdef person_client():# ROS节点初始化rospy.init_node('person_client')# 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的servicerospy.wait_for_service('/show_person')try:person_client = rospy.ServiceProxy('/show_person', Person)# 请求服务调用,输入请求数据response = person_client("Tom", 20, PersonRequest.male)return response.resultexcept rospy.ServiceException, e:print "Service call failed: %s"%eif __name__ == "__main__":#服务调用并显示调用结果print "Show person result : %s" %(person_client())
运行
sudo chmod +x ~/catkin_workspace/src/learning_service/scripts/person_server.py
sudo chmod +x ~/catkin_workspace/src/learning_service/scripts/person_client.py# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
roscore
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun learning_service person_server.py
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun learning_service person_client.py