素材巴巴 > 程序开发 >

ROS学习(五)客户端与服务端通信

程序开发 2023-09-10 13:21:01

目录

 服务模型

一、实现客户端

1.创建服务功能包

2.创建turtle_spawn.cpp

3.配置CMakeLists.txt

4.编译

5.运行

二、实现服务端

1.创建turtle_command_server.cpp

2.配置CMakeLists.txt

3.编译

4.运行

 三、自定义服务

1.创建srv文件

2.在 package.xml中添加功能依赖包

3.在CMakeLists.txt添加编译选项

4.编译

5.应用此自定义服务


服务模型

实现过程
客户端(Client)产生请求的节点,发布请求,服务端(Server)做出操作并给客户端回馈


ros服务端与客户端通信icon-default.png?t=M666https://download.csdn.net/download/m0_56451176/86398723?spm=1001.2014.3001.5503 

一、实现客户端

实现目的:通过客户端请求海龟仿真器产生第二子海龟

1.创建服务功能包

cd ~/catkin_ws/src
 catkin_create_pkg learning_service roscpp rospy std_msgs geometry_msgs turtlesim
 

2.创建turtle_spawn.cpp

cd learning_service/src
 touch turtle_spawn.cpp
 gedit turtle_spawn.cpp

turtle_spawn.cpp内容如下:

//该例程将请求/spawn服务,服务数据类型turtlesim::Spawn
 #include 
 #include 
 int main(int argc, char** argv)
 {// 初始化ROS节点ros::init(argc, argv, "turtle_spawn");// 创建节点句柄ros::NodeHandle node;// 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的serviceros::service::waitForService("/spawn");		//若找不到服务则一直等待ros::ServiceClient add_turtle = node.serviceClient("/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;
 };
 

3.配置CMakeLists.txt

在build区域中添加:

add_executable(turtle_spawn src/turtle_spawn.cpp)		
 target_link_libraries(turtle_spawn ${catkin_LIBRARIES})	
 

4.编译

cd ~/catkin_ws
 catkin_make

5.运行

启动ROS核心 

roscore

启动海龟仿真器 

rosrun turtlesim turtlesim_node

运行客户端 

source ~/catkin_ws/devel/setup.bash
 rosrun learning_service turtle_spawn
 

效果为产生第二只海龟


二、实现服务端

实现目的:控制海龟运动

1.创建turtle_command_server.cpp

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 
 #include 
 #include 
 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,注册回调函数commandCallbackros::ServiceServer command_service = n.advertiseService("/turtle_command", commandCallback);// 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10turtle_vel_pub = n.advertise("/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;
 }
 

2.配置CMakeLists.txt

在build区域中添加:

add_executable(turtle_command_server src/turtle_command_server.cpp)
 target_link_libraries(turtle_command_server ${catkin_LIBRARIES})
 

3.编译

cd ~/catkin_ws
 catkin_make

4.运行

启动ROS核心 

roscore
 

启动海龟仿真器 

rosrun turtlesim turtlesim_node
 

运行服务端 

source ~/catkin_ws/devel/setup.bash
 rosrun learning_service turtle_command_server
 

运行turtle_command服务

rosservice call /turtle_command "{}"	


 三、自定义服务

1.创建srv文件

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 

2.在 package.xml中添加功能依赖包

在服务功能包的package.xml中添加功能包的编译依赖和运行依赖

message_generation		
 message_runtime			
 

3.在CMakeLists.txt添加编译选项

find_package(...message_generation)#依赖,在messages,services栏中加
 add_service_files(FILES Person.srv)			
 generate_messages(DEPENDENCIES std_msgs)	#依赖若被注释要取消
 catkin_package(...message_runtime)    
 

  

 

4.编译

cd ~/catkin_ws
 catkin_make

此时在devel/include/功能包目录下会生成srv文件定义的整体头文件,三横线前在request头文件,三横线下在response头文件,其信息即为在srv中定义的内容,应用时引入Person.h即可

5.应用此自定义服务

1.在话题功能包的src目录下创建person_client.cpp

cd ~/catkin_ws/src/learning_service/src
 touch person_client.cpp
 gedit person_client.cpp

 person_client.cpp内容如下

//该例程将请求/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服务后,创建一个服务客户端,连接名为/show_person的serviceros::service::waitForService("/show_person");ros::ServiceClient person_client = node.serviceClient("/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;
 }
 

2.创建person_server.cpp

在服务功能包的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 
 #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;
 }
 

3.配置功能包的CMakeLists.txt中的编译规则,build栏中

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)
 

 4.编译

cd ~/catkin_ws
 catkin_make

5.运行

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 © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。