环境:Ubuntu18.04,ROS版本:melodic
工作空间包括src(代码空间)、build(编译空间)、devel(开发空间)、install(安装空间)
mkdir -p ~/sor_ws/src
cd ~/sor_ws/src
catkin_init_workspace
cd ~/sor_ws
catkin_make
编译成功
src目录下生成build和devel文件
此时还差install文件夹,再执catkin_make install指令
catkin_make install
cd ~/sor_ws/src
catkin_create_pkg learning_service std_msgs rospy roscpp geometry_msgs turtlesim
在learning_service下创建srv文件夹
进入srv文件夹,右键在终端打开,输入touch Person.srv,生成Person.srv文件
双击打开,输入
string name
uint8 age
uint8 sex
uint8 unknown = 0
uint8 male = 1
uint8 female = 2
---
string result
string result作用是给客户端反馈消息是否传送成功
在learning_service下双击打开package.xml文件,添加依赖
message_generation
message_runtime
在learning_service下双击打开CMakeLists.txt文件,添加编译选项
(1)添加message_generation
(2)添加
add_message_files(
FILES
Person.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
(3)去掉CATKIN_DEPANDS前面的#,添加 message_runtime
cd ~/sor_ws
catkin_make
在/sor_ws/devel/include/learning_service路径下生成三个.h文件
cd ~/sor_ws/src/learning_service/src
touch person_client.cpp
sudo gedit person_client.cpp
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/
/**
* 该例程将请求/show_person服务,服务数据类型learning_service::Person
*/
#include
#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("/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;
};
cd ~/sor_ws/src/learning_service/src
touch person_server.cpp
sudo gedit person_server.cpp
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/
/**
* 该例程将执行/show_person服务,服务数据类型learning_service::Person
*/
#include
#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;
}
在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 ~/sor_ws
catkin_make
打开新终端,输入roscore
再打开一个新终端,设置环境变量,防止找不到可执行文件,再运行订阅者指令
source ~/sor_ws/devel/setup.bash
rosrun learning_service person_server
再打开一个新终端,运行客户端指令
source ~/sor_ws/devel/setup.bash
rosrun learning_service person_client
上一篇:ROS入门学习笔记|话题发布与订阅
学习资料来自:【古月居】古月·ROS入门21讲