当前位置:网站首页>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
边栏推荐
- String input problem
- Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
- (valid for personal testing) compilation guide of paddedetection on Jetson
- 深度學習筆記(二)——激活函數原理與實現
- Section 2 map and structure in Chapter 6
- Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)
- Concepts of objects and classes
- 2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
- Seekbar custom style details
- Unity knowledge points (ugui 2)
猜你喜欢
2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
Source code and update details of new instance segmentation network panet (path aggregation network for instance segmentation)
Section 1 array and slicing in Chapter 6
Test questions and some space wars
Punch in: 4.22 C language chapter - (1) first knowledge of C language - (11) pointer
淺學一下I/O流和File類文件操作
A sword is a sword. There is no difference between a wooden sword and a copper sword
Supersocket is Use in net5 - concept
Common auxiliary classes
Opencv4 QR code recognition test
随机推荐
Design and implementation of redis (5): master-slave replication strategy and optimization
7-3 poly width
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
Design and implementation of redis (2): how to handle expired keys
Three types of cyclic structure
Identifier, keyword, data type
Download and configuration of idea
Source code and update details of new instance segmentation network panet (path aggregation network for instance segmentation)
Section 2 map and structure in Chapter 6
Variables, constants, operators
Paddlepaddle model to onnx
Seekbar custom style details
Test questions (2)
Punch in: 4.22 C language chapter - (1) first knowledge of C language - (11) pointer
What to pay attention to when writing the first code
MySQL zip installation tutorial
STM32 advanced timer com event
Unity Basics
Supersocket is Use in net5 - concept
Batch download of files ---- compressed and then downloaded