当前位置:网站首页>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
边栏推荐
- Development record of primary sensitive word detection
- Activity supports multi window display
- Wechat payment iframe sub page has no response
- 2022 group programming ladder simulation l2-1 blind box packaging line (25 points)
- Code forces round # 784 (DIV. 4) solution (First AK CF (XD)
- "Visual programming" test paper
- Problem C: Hanoi Tower III
- Applet - more than two pieces of folding and expansion logic
- Commonly used classes
- Applet - WXS
猜你喜欢
Create virtual machine
Flink customizes the application of sink side sinkfunction
. net 5 Web custom middleware implementation returns the default picture
Paddlepaddle does not support arm64 architecture.
Seekbar custom style details
Code forces round # 784 (DIV. 4) solution (First AK CF (XD)
Learn about I / O flow and file operations
将编译安装的mysql加入PATH环境变量
Design and implementation of redis (3): persistence strategy RDB, AOF
Test questions and some space wars
随机推荐
JS calculates the display date according to the time
Use the thread factory to set the thread name in the thread pool
AWS from entry to actual combat: creating accounts
Identificateur, mot - clé, type de données
Wechat applet canvas draws a simple asymptotic color of the dashboard
Instructions for fastmock
MySQL zip installation tutorial
Problem C: Hanoi Tower III
L3-011 直捣黄龙 (30 分)
When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
Vs studio modifies C language scanf and other errors
Design and implementation of redis (2): how to handle expired keys
Unity Basics
C interface
Oracle JDK vs OpenJDK
Three types of cyclic structure
Idea debug debugging tutorial
Problem C: realize Joseph Ring with linked list
QT dynamic translation of Chinese and English languages
Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)