当前位置:网站首页>8、 Topic communication: topic substitution and monitoring
8、 Topic communication: topic substitution and monitoring
2022-07-26 17:54:00 【Rock magnon】
List of articles
1、 brief introduction
When we run the little turtle case , We can control its operation through the direction key , How can we control its operation through our own code . The method is actually very simple , Because the case of little turtle is through topic communication , Then we can get the topic of the communication through relevant commands , The message types and details contained in the topic , Then set the corresponding topic through the code .
2、 Publisher implementation steps
1. Topic acquisition
- By calculating the graph
rqt_graph - adopt
rostopic list
Return results :rostopic list /rosout /rosout_agg /statistics /turtle1/cmd_vel /turtle1/color_sensor /turtle1/pose
2. Information access
- rostopic type The node name / Topic name
- Return results :
rostopic type /turtle1/cmd_vel geometry_msgs/Twist
3. Get message format
- rosmsg info Message name
- Return results :
rosmsg info geometry_msgs/Twist geometry_msgs/Vector3 linear float64 x float64 y float64 z geometry_msgs/Vector3 angular float64 x float64 y float64 z
among ,linear Represents linear velocity ,angular For angular velocity , In radians . In this project , Linear speed only X Direction , Angular velocity is only yaw angular velocity .
- 1 radian : When the arc length is equal to the radius, the corresponding angle is 1 radian
- Three angles :
- Roll angle : Around the X Shaft rotation
- Pitch angle : Around the Y Shaft rotation
- Yaw angle : Around the Z Shaft rotation
4. Code implementation
- C++
/* The goal is : Through node replacement , Control the movement of the little turtle Node replacement refers to the replacement of the topic corresponding to the topic , So you need to know in advance : 1. Topic name :/turtle1/cmd_vel rqt_graph rostopic list 2. The message type of the topic :geometry_msgs/Twist rostopic type Topic name 3. Specific information of the topic message : geometry_msgs/Vector3 linear float64 x float64 y float64 z geometry_msgs/Vector3 angular float64 x float64 y float64 z rosmsg info Message name */ //1. Include header file #include"ros/ros.h" // Contains header files with corresponding topic message types #include"geometry_msgs/Twist.h" int main(int argc, char *argv[]){ setlocale(LC_ALL,""); //2. initialization ros node ros::init(argc,argv,"turtle_control"); ros::NodeHandle nh; //3. Create publisher object ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",1000); //4. Release control information 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 = 1.0; ros::Rate r(1); while(ros::ok()){ pub.publish(msg); ros::spinOnce(); } return 0; } - Python
#! /usr/bin/env python from re import T from turtle import pu import rospy from geometry_msgs.msg import Twist if __name__ == "__main__": # initialization ros node rospy.init_node("turtle_control_p") # Create publisher object pub = rospy.Publisher("/turtle1/cmd_vel",Twist,queue_size=1000) # Publish the information rate = rospy.Rate(10) msg = Twist() 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 = 1.0 # Circular Publishing rospy.is_shutdown(): Judge whether the node is closed while not rospy.is_shutdown(): pub.publish(msg) rate.sleep()
3、 Subscriber implementation steps
1. Get topics and news
- rostopic list
- Return results :
rostopic list /rosout /rosout_agg /turtle1/cmd_vel /turtle1/color_sensor /turtle1/pose [email protected]:/media/d102/EPAN/Desktop/code_study_ubuntu/rosdemo_05$ rostopic type /turtle1/pose turtlesim/Pose [email protected]:/media/d102/EPAN/Desktop/code_study_ubuntu/rosdemo_05$ rosmsg info turtlesim/Pose float32 x float32 y float32 theta float32 linear_velocity float32 angular_velocity
2. Changing configuration files
In this project , Due to the use of turtlesim Function pack , Therefore, you need to add the function package dependency , modify CMakeLists.txt file :
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
turtlesim
)
3. Code implementation
- C++
/* topic of conversation :/turtle1/pose Message type :turtlesim/Pose */ #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,""); ros::init(argc,argv,"turtle_pose_sub"); ros::NodeHandle nh; ros::Subscriber sub = nh.subscribe<turtlesim::Pose>("/turtle1/pose",1000,doPose); ros::spin(); return 0; } - Python
#! /usr/bin/env python
""" The goal is : Get the location of the little turtle , And print it out topic of conversation :/turtle1/pose Message type :turtlesim/Pose Preparation before operation :turtle_start.launch """
import rospy
from turtlesim.msg import Pose
def doPose(data1):#data1 Is a formal parameter
rospy.loginfo("x=%.2f,y=%.2f,theta=%.2f",data1.x,data1.y,data1.theta)
if __name__ == "__main__":
rospy.init_node("turtle_pose_p")
sub = rospy.Subscriber("/turtle1/pose",Pose,doPose,queue_size=1000)
rospy.spin()
边栏推荐
- JS 函数作用域 变量声明提升 作用域链 不加var的变量,是全局变量
- 兆骑科创海外高层次人才引进平台,创业赛事活动路演
- Interview with celebrities | open source is a double-edged sword for security -- Wei Jianfan, author of the Chinese translation of cathedral and market
- Deep learning experiment: softmax realizes handwritten digit recognition
- AI zhetianchuan DL regression and classification
- 【集训Day2】Torchbearer
- Ascend目标检测与识别-定制自己的AI应用
- 跨站脚本攻击(XSS)
- 2.1.2 同步始终失败
- Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming
猜你喜欢
随机推荐
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
pip安装模块,报错
236. The nearest common ancestor of a binary tree
Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming!
hosts该文件已设置为只读的解决方法
Machine learning by Li Hongyi 2. Regression
2、 Topic communication principle, code implementation
如何组装一个注册中心?
大咖访谈 | 开源对安全是双刃剑——《大教堂与集市》中文译本作者卫剑钒
兆骑科创海外高层次人才引进平台,创业赛事活动路演
来吧开发者!不只为了 20 万奖金,试试用最好的“积木”来一场头脑风暴吧!
树形dp问题
Open source kaggle cat and dog data set -- used in classic CNN classification practice
PIP installation module, error
如何通过学会提问,成为更加优秀的数据科学家
2022河南萌新联赛第(三)场:河南大学
Heavy! The 2022 China open source development blue book was officially released
[training day3] section
第17周自由入侵 指针练习--输出最大值
COSCon'22城市/学校/机构出品人征集令









