当前位置:网站首页>ROS series (4): ROS communication mechanism series (4): topic communication practice
ROS series (4): ROS communication mechanism series (4): topic communication practice
2022-04-23 03:37:00 【Yi Lanjun】
The practice of topic communication is mainly through ROS Built in turtlesim Case study , Combined with the introduction of ROS Command to get the node 、 topic of conversation 、 The control of tortoise movement is realized by topic news .
1 Topic Publishing
Requirements describe : The tortoise motion control is realized by coding , Let the little turtle do circular motion .
Implementation analysis :
- Realize the control of the tortoise , There are two key nodes (Node): One is the tortoise motion display node turtlesim_node, The other is the keyboard control node , The two communicate through topic communication .
- Before implementing the control node , You need to know the topics and messages used by node control , have access to ros Relevant orders and rqt Calculate the diagram to obtain .
- After knowing the topic and news , adopt C++ or Python Write node motion control , Through the specified topic , Just publish the message according to a certain logic .
Implementation process :
- start-up tutlesim node .
- Through the calculation diagram, combined with ros Command to get topic and message information .
- Coding to achieve motion control node .
- start-up roscore、turtlesim_node And custom control nodes , View the run results .
1.1 start-up tutlesim node
- Start three terminals
- terminal 1 type :roscore
- terminal 2 type :rosrun turtlesim turtlesim_node
- terminal 3 keyboard :rosrun turtlesim turtle_teleop_key

Be careful : The cursor must be focused in the keyboard controlled terminal , Otherwise you can't control the tortoise's movement .
1.2 Topic and message acquisition
1.2.1 Topic acquisition
Check the topic through the calculation chart :
rqt_graph
Or by rostopic List topics :
rostopic list
1.2.2 Information access
Get message type :
rostopic type /turtle1/cmd_vel
output:
geometry_msgs/Twist
Get message format :
rosmsg info geometry_msgs/Twist
output:
geometry_msgs/Vector3 linear ## xyz Linear velocity in direction
float64 x
float64 y
float64 z
geometry_msgs/Vector3 angular ## x Roll on the shaft 、y Pitch and on axis z The angular velocity of yaw on the axis
float64 x
float64 y
float64 z
1.3 Implement the publishing node (C++)
To create a function package, you need to rely on the function package : roscpp rospy std_msgs geometry_msgs
/* To write ROS node , Control the little turtle to draw a circle preparation : 1. obtain topic( It is known that : /turtle1/cmd_vel) 2. Get message type ( It is known that : geometry_msgs/Twist) 3. Before running , Pay attention to start first turtlesim_node node Implementation process : 1. Include header file 2. initialization ROS node 3. Create publisher object 4. Circularly issue motion control messages */
#include "ros/ros.h"
#include "geometry_msgs/Twist.h"
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
// 2. initialization ROS node
ros::init(argc,argv,"control");
ros::NodeHandle nh;
// 3. Create publisher object
ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",1000);
// 4. Circularly issue motion control messages
//4-1. Organize messages
geometry_msgs::Twist msg;
msg.linear.x = 1.0;
msg.linear.y = 0.0;
msg.linear.z = 0.0;
msg.angular.x = 0.0;
msg.angular.y = 0.0;
msg.angular.z = 2.0;
//4-2. Set the transmission frequency
ros::Rate r(10);
//4-3. Cycle to send
while (ros::ok())
{
pub.publish(msg);
ros::spinOnce();
}
return 0;
}
The configuration file is the same as ROS series ( Four ):ROS Communication mechanism series (1): Topic communication Publisher profile in .
1.4 function
Run the same as this article 1.1 start-up tutlesim node .
- First , start-up roscore;
- Then start the tortoise display node ;
- Finally, execute the motion control node .
The running results are as follows :

2 Topic subscription
Requirements describe :
It is known that turtlesim The tortoise display node in , Will release the current turtle's pose ( The coordinates and orientation of the tortoise in the form ), Request control of tortoise movement , And print the current turtle's posture from time to time .
Implementation process :
- adopt ros Command to get topic and message information .
- Coding to achieve pose acquisition node .
- start-up roscore、turtlesim_node 、 Control node and pose subscription node , Control the movement of the tortoise and output the posture of the tortoise .
2.1 Topic and message acquisition
Get the topic :/turtle1/pose:
rostopic list
Get message type :turtlesim/Pose
rostopic type /turtle1/pose
Get message format :
rosmsg info turtlesim/Pose
result :
float32 x
float32 y
float32 theta
float32 linear_velocity
float32 angular_velocity
2.2 Implement the subscription node (C++)
To create a function package, you need to rely on the function package : roscpp rospy std_msgs turtlesim
/* Subscribe to the posture of the little turtle : Get the coordinates of the little turtle in the form from time to time and print preparation : 1. Get topic name /turtle1/pose 2. Get message type turtlesim/Pose 3. Start before operation turtlesim_node And turtle_teleop_key node Implementation process : 1. Include header file 2. initialization ROS node 3. establish ROS Handle 4. Create subscriber object 5. The callback function processes the subscribed data 6.spin */
#include "ros/ros.h"
#include "turtlesim/Pose.h"
void doPose(const turtlesim::Pose::ConstPtr& p){
ROS_INFO(" Turtle pose information :x=%.2f,y=%.2f,theta=%.2f,lv=%.2f,av=%.2f",
p->x,p->y,p->theta,p->linear_velocity,p->angular_velocity
);
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
// 2. initialization ROS node
ros::init(argc,argv,"sub_pose");
// 3. establish ROS Handle
ros::NodeHandle nh;
// 4. Create subscriber object
ros::Subscriber sub = nh.subscribe<turtlesim::Pose>("/turtle1/pose",1000,doPose);
// 5. The callback function processes the subscribed data
// 6.spin
ros::spin();
return 0;
}
The configuration file is the same as ROS series ( Four ):ROS Communication mechanism series (1): Topic communication Subscriber profile in .
2.3 function
- First , start-up roscore;
- Then start the tortoise display node , Execute motion control node ;
- Finally, start the tortoise pose subscription node .
版权声明
本文为[Yi Lanjun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220602231705.html
边栏推荐
- Problem a: face recognition
- Common exceptions
- Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)
- Redis (17) -- redis cache related problem solving
- 集合之List接口
- Unity games and related interview questions
- . net 5 Web custom middleware implementation returns the default picture
- Learn about I / O flow and file operations
- C interface
- Notes sur l'apprentissage profond (Ⅱ) - - Principe et mise en oeuvre de la fonction d'activation
猜你喜欢

Visual programming - Experiment 1

Design and implementation of redis (5): master-slave replication strategy and optimization

When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director

Unity basics 2

A sword is a sword. There is no difference between a wooden sword and a copper sword

Source code and update details of new instance segmentation network panet (path aggregation network for instance segmentation)

Use of rotary selector wheelpicker

Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)

QT dynamic translation of Chinese and English languages

2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
随机推荐
Install PaddlePaddle on ARM
Seekbar custom style details
A sword is a sword. There is no difference between a wooden sword and a copper sword
Visual programming -- how to customize the mouse cursor
QT dynamic translation of Chinese and English languages
Create virtual machine
JS changes the words separated by dashes into camel style
Visual programming - Experiment 1
Flink customizes the application of sink side sinkfunction
Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
Openvino only supports Intel CPUs of generation 6 and above
Supersocket is Use in net5 - startup
Applet - more than two pieces of folding and expansion logic
标识符、关键字、数据类型
[microservices] (x) -- Unified gateway
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
Commonly used classes
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
Notes sur l'apprentissage profond (Ⅱ) - - Principe et mise en oeuvre de la fonction d'activation
Punch in: 4.22 C language chapter - (1) first knowledge of C language - (11) pointer