申明:ROS学习参考了古月居老师的Blibli视频,强烈推荐大家看视频学习,本博客仅记录自己的学习经历和心得,欢迎大家一起讨论!
1 ROS服务模型
本节,利用Service模型。通过请求和应答的方式传递用户信息,服务名称为(/show_person),数据类型为learning_service::Person,均为用户自定义
如何自定义一个服务数据类型?
- 在srv文件夹下定义Person.srv文件
string name
uint8 age
uint8 sex
uint8 unknown = 0
uint8 male = 1
uint8 female = 2
—
string result
注意:“—”前面的是Request数据,后面的是Response数据
- 在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)
- 编译生成相关文件
cd ~/catkin_ws #进入ros工作空间根目录下
catkin_make
编译生成的3个头文件存储在catkin_ws/devel/include/learning_service文件夹下
查看一下Trigger的数据结构:
rossrv show std_srvs/Trigger
2 C++程序实现
在learning_service下的src文件夹下新建.cpp文件或.py文件,需要在此learning_service/src文件夹下运行:
2.1 Client文件编写
(1)创建C++文件
touch person_client.cpp
(2)编写.cpp文件内容
/**
* 该例程将请求/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;
// 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
ros::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;
};
(3)配置CMakeLists.txt编译规则
add_executable(person_client src/person_client.cpp)
target_link_libraries(person_client ${catkin_LIBRARIES})
add_dependencies(person_client ${PROJECT_NAME}_gencpp) //和动态生成的头文件进行依赖
添加上述代码到文件对应位置:
(4)进入catkin_ws 进行可执行文件 person_client的编译,运行
cd ~/catkin_ws
catkin_make#编译
2.2 Server文件编写
(1) 创建.cpp文件
touch person_server.cpp
(2)编写.py文件内容
/**
* 该例程将执行/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,注册回调函数personCallback
ros::ServiceServer person_service = n.advertiseService("/show_person", personCallback);
// 循环等待回调函数
ROS_INFO("Ready to show person informtion.");
ros::spin();
return 0;
}
(3) 配置CMakeLists.txt 编译规则
add_executable(person_server src/person_server.cpp)
target_link_libraries(person_server ${catkin_LIBRARIES})
add_dependencies(person_server ${PROJECT_NAME}_gencpp)
(4) 进行编译,生成person_server可执行文件
cd ~/catkin_ws
catkin_make
2.3 程序运行
roscore
rosrun learning_service person_client
rosrun learning_service person_server
3 python程序实现
3.1 Client文件编写
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将请求/show_person服务,服务数据类型learning_service::Person
import sys
import rospy
from learning_service.srv import Person, PersonRequest
def person_client():
# ROS节点初始化
rospy.init_node('person_client')
# 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
rospy.wait_for_service('/show_person')
try:
person_client = rospy.ServiceProxy('/show_person', Person)
# 请求服务调用,输入请求数据
response = person_client("Tom", 20, PersonRequest.male)
return response.result
except rospy.ServiceException, e:
print "Service call failed: %s"%e
if __name__ == "__main__":
#服务调用并显示调用结果
print "Show person result : %s" %(person_client())
3.2 Server文件编写
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将执行/show_person服务,服务数据类型learning_service::Person
import rospy
from learning_service.srv import Person, PersonResponse
def 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,注册回调函数personCallback
s = rospy.Service('/show_person', Person, personCallback)
# 循环等待回调函数
print "Ready to show person informtion."
rospy.spin()
if __name__ == "__main__":
person_server()
3.3 程序实现
roscore
rosrun learning_service person_client.py
rosrun learning_service person_server.py
本讲完,接下来会持续更新!