当前位置:网站首页>ROS series (IV): ROS communication mechanism series (5): Service Communication Practice
ROS series (IV): ROS communication mechanism series (5): Service Communication Practice
2022-04-23 03:37:00 【Yi Lanjun】
Requirements describe : Coding implementation direction turtlesim Send a request , Generate a turtle at the specified position on the form of the turtle display node , This is a service request operation .
Result demonstration :
Implementation process :
- adopt ros Command to obtain service and service message information .
- Code the service request node .
- start-up roscore、turtlesim_node 、 Tortoise generates nodes , Generate new turtles .
1 Service name and service message acquisition
Get the topic :/spawn:
rosservice list
Get message type :turtlesim/Spawn:
rosservice type /spawn
Get message format :
rossrv info turtlesim/Spawn
In response to the results :
float32 x
float32 y
float32 theta
string name
---
string name
2 Service client implementation (C++)
To create a function package, you need to rely on the function package : roscpp rospy std_msgs turtlesim
/* Make a little turtle preparation : 1. Service topics /spawn 2. Service message type turtlesim/Spawn 3. Start before operation turtlesim_node node Implementation process : 1. Include header file Need to include turtlesim Resources under the package , Pay attention to package.xml To configure 2. initialization ros node 3. establish ros Handle 4. establish service client 5. Wait for the service to start 6. Send a request 7. Process response */
#include "ros/ros.h"
#include "turtlesim/Spawn.h"
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
// 2. initialization ros node
ros::init(argc,argv,"set_turtle");
// 3. establish ros Handle
ros::NodeHandle nh;
// 4. establish service client
ros::ServiceClient client = nh.serviceClient<turtlesim::Spawn>("/spawn");
// 5. Wait for the service to start
// client.waitForExistence();
ros::service::waitForService("/spawn");
// 6. Send a request
turtlesim::Spawn spawn;
spawn.request.x = 1.0;
spawn.request.y = 1.0;
spawn.request.theta = 1.57;
spawn.request.name = "my_turtle";
bool flag = client.call(spawn);
// 7. Process response results
if (flag)
{
ROS_INFO(" New turtles are born , name :%s",spawn.response.name.c_str());
} else {
ROS_INFO(" Tortoise generation failed !!!");
}
return 0;
}
3 function
- First , start-up roscore;
- Then start the tortoise display node ;
- Finally, start the tortoise to generate the request node ;
- The final execution result is similar to the demonstration result .
版权声明
本文为[Yi Lanjun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220602231664.html
边栏推荐
- Test questions and some space wars
- 2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
- 7-1 introduction to finance
- 对象和类的概念
- Chapter VI, Section III pointer
- Design and implementation of redis (4): what is the event driver of redis
- Design and implementation of redis (6): how redis achieves high availability
- Three types of cyclic structure
- Three column layout (fixed width on both sides in the middle and fixed width on both sides in the middle)
- Learn about I / O flow and file operations
猜你喜欢
Paddlepaddle model to onnx
Detailed explanation on the use of annotation tool via (VGg image annotator) in mask RCNN
Create virtual machine
Redis(17) -- Redis缓存相关问题解决
Section 1 array and slicing in Chapter 6
Supersocket is Use in net5 - startup
Design and implementation of redis (2): how to handle expired keys
常用的辅助类
Code forces round # 784 (DIV. 4) solution (First AK CF (XD)
Unity games and related interview questions
随机推荐
The art of concurrent programming (6): explain the principle of reentrantlock in detail
JS changes the words separated by dashes into camel style
Seekbar custom style details
Key point detection of human hand based on mediapipe
JS takes out the same elements in two arrays
Query stored procedures in PostgreSQL
7-3 poly width
Use of rotary selector wheelpicker
Problem a: face recognition
Download and configuration of idea
Activity supports multi window display
将编译安装的mysql加入PATH环境变量
Vs studio modifies C language scanf and other errors
C set
Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)
Notes sur l'apprentissage profond (Ⅱ) - - Principe et mise en oeuvre de la fonction d'activation
Design and implementation of redis (3): persistence strategy RDB, AOF
Opencv4 QR code recognition test
PyMOL usage
Now is the best time to empower industrial visual inspection with AI