当前位置:网站首页>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
边栏推荐
- Software testing process
- Leetcode punch in diary day 01
- Supersocket is Used in net5 - command
- Several common methods of multithreading
- 2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
- File upload vulnerability summary and upload labs shooting range documentary
- Use of rotary selector wheelpicker
- Unity basics 2
- Instructions for fastmock
- Visual programming - Experiment 2
猜你喜欢
The art of concurrent programming (3): an in-depth understanding of the principle of synchronized
Opencv4 QR code recognition test
浅学一下I/O流和File类文件操作
Unity basics 2
7-1 introduction to finance
Applet - canvas drawing Poster
MySQL is completely uninstalled and MySQL service is cleaned up
. net 5 Web custom middleware implementation returns the default picture
变量、常量、运算符
Unity games and related interview questions
随机推荐
According to the category information and coordinate information of the XML file, the category area corresponding to the image is pulled out and stored in the folder.
Section 1 array and slicing in Chapter 6
Concepts of objects and classes
Un aperçu des flux d'E / s et des opérations de fichiers de classe de fichiers
Visual programming - Experiment 1
The art of concurrent programming (6): explain the principle of reentrantlock in detail
VS Studio 修改C语言scanf等报错
(valid for personal testing) compilation guide of paddedetection on Jetson
Definition format of array
If statement format flow
Openvino only supports Intel CPUs of generation 6 and above
Batch download of files ---- compressed and then downloaded
Wechat payment iframe sub page has no response
集合之List接口
Commonly used classes
MySQL query specifies that a row is sorted to the first row
標識符、關鍵字、數據類型
L3-011 direct attack Huanglong (30 points)
Use the thread factory to set the thread name in the thread pool
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)