赞
踩
实现过程
客户端(Client)产生请求的节点,发布请求,服务端(Server)做出操作并给客户端回馈
ros服务端与客户端通信https://download.csdn.net/download/m0_56451176/86398723?spm=1001.2014.3001.5503
实现目的:通过客户端请求海龟仿真器产生第二子海龟
- cd ~/catkin_ws/src
- catkin_create_pkg learning_service roscpp rospy std_msgs geometry_msgs turtlesim
- cd learning_service/src
- touch turtle_spawn.cpp
- gedit turtle_spawn.cpp
turtle_spawn.cpp内容如下:
- //该例程将请求/spawn服务,服务数据类型turtlesim::Spawn
- #include <ros/ros.h>
- #include <turtlesim/Spawn.h>
- int main(int argc, char** argv)
- {
- // 初始化ROS节点
- ros::init(argc, argv, "turtle_spawn");
-
- // 创建节点句柄
- ros::NodeHandle node;
-
- // 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
- ros::service::waitForService("/spawn"); //若找不到服务则一直等待
- ros::ServiceClient add_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");
-
- // 初始化turtlesim::Spawn的请求数据
- turtlesim::Spawn srv;
- srv.request.x = 2.0;
- srv.request.y = 2.0;
- srv.request.name = "turtle2";
-
- // 请求服务调用
- ROS_INFO("Call service to spwan turtle[x:%0.6f, y:%0.6f, name:%s]", srv.request.x, srv.request.y, srv.request.name.c_str());
- add_turtle.call(srv); //发出请求,等待反馈
-
- // 显示服务调用结果
- ROS_INFO("Spwan turtle successfully [name:%s]", srv.response.name.c_str());
- return 0;
- };
在build区域中添加:
- add_executable(turtle_spawn src/turtle_spawn.cpp)
- target_link_libraries(turtle_spawn ${catkin_LIBRARIES})
- cd ~/catkin_ws
- catkin_make
启动ROS核心
roscore
启动海龟仿真器
rosrun turtlesim turtlesim_node
运行客户端
- source ~/catkin_ws/devel/setup.bash
- rosrun learning_service turtle_spawn
效果为产生第二只海龟
实现目的:控制海龟运动
- cd ~/catkin_ws/src/learning_service/src
- touch turtle_command_server.cpp
- gedit turtle_command_server.cpp
turtle_command_server.cpp内容如下:
- //该例程将执行/turtle_command服务,服务数据类型std_srvs/Trigger
- #include <ros/ros.h>
- #include <geometry_msgs/Twist.h>
- #include <std_srvs/Trigger.h>
- ros::Publisher turtle_vel_pub;
- bool pubCommand = false; //是否在运动的标志,作为开关
-
- // service回调函数,输入参数req,输出参数res
- bool commandCallback(std_srvs::Trigger::Request &req,std_srvs::Trigger::Response &res)
- {
- pubCommand = !pubCommand;
-
- // 显示请求数据
- ROS_INFO("Publish turtle velocity command [%s]", pubCommand==true?"Yes":"No");
-
- // 设置反馈数据
- res.success = true; //在服务中显示输出
- res.message = "Change turtle command state!";
- return true;
- }
-
- int main(int argc, char **argv)
- {
- // ROS节点初始化
- ros::init(argc, argv, "turtle_command_server");
-
- // 创建节点句柄
- ros::NodeHandle n;
-
- // 创建一个名为/turtle_command的server,注册回调函数commandCallback
- ros::ServiceServer command_service = n.advertiseService("/turtle_command", commandCallback);
-
- // 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
- turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);
-
- // 循环等待回调函数
- ROS_INFO("Ready to receive turtle command.");
-
- // 设置循环的频率
- ros::Rate loop_rate(10);
- while(ros::ok())
- {
- // 查看一次回调函数队列
- ros::spinOnce();
-
- // 如果标志为true,则发布速度指令
- if(pubCommand)
- {
- geometry_msgs::Twist vel_msg;
- vel_msg.linear.x = 0.5;
- vel_msg.angular.z = 0.2;
- turtle_vel_pub.publish(vel_msg);
- }
- //按照循环频率延时
- loop_rate.sleep();
- }
- return 0;
- }
在build区域中添加:
- add_executable(turtle_command_server src/turtle_command_server.cpp)
- target_link_libraries(turtle_command_server ${catkin_LIBRARIES})
- cd ~/catkin_ws
- catkin_make
启动ROS核心
roscore
启动海龟仿真器
rosrun turtlesim turtlesim_node
运行服务端
- source ~/catkin_ws/devel/setup.bash
- rosrun learning_service turtle_command_server
运行turtle_command服务
rosservice call /turtle_command "{}"
- cd ~/catkin_ws/src/learning_service
- mkdir srv
- cd srv
- touch Person.srv
- gedit 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>
- find_package(...message_generation)
-
- #依赖,在messages,services栏中加
- add_service_files(FILES Person.srv)
- generate_messages(DEPENDENCIES std_msgs)
-
- #依赖若被注释要取消
- catkin_package(...message_runtime)
- cd ~/catkin_ws
- catkin_make
此时在devel/include/功能包目录下会生成srv文件定义的整体头文件,三横线前在request头文件,三横线下在response头文件,其信息即为在srv中定义的内容,应用时引入Person.h即可
- cd ~/catkin_ws/src/learning_service/src
- touch person_client.cpp
- gedit person_client.cpp
person_client.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服务后,创建一个服务客户端,连接名为/show_person的service
- ros::service::waitForService("/show_person");
- ros::ServiceClient person_client = node.serviceClient<learning_service::Person>("/show_person");
- // 初始化learning_service::Person的请求数据
- learning_service::Person srv; //定义Person的对象
- 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;
- }
在服务功能包的src目录下创建person_server.cpp
- cd ~/catkin_ws/src/learning_service/src
- touch person_server.cpp
- gedit person_server.cpp
person_server.cpp内容如下
- //该例程将执行/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;
- }
- 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_ws
- catkin_make
roscore
- source ~/catkin_ws/devel/setup.bash
- rosrun learning_service person_server
- source ~/catkin_ws/devel/setup.bash
- rosrun learning_service person_client
注:spinOnce()执行后还会执行之后的程序,而spin()不会
先打开服务端,再开客户端,服务端可直接反馈
若服务端未开,则在waitForService函数等待服务端,当服务端可用即可请求并反馈
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。