当前位置:网站首页>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()
边栏推荐
- 第17周自由入侵 指针练习--输出最大值
- 即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
- Redisdesktopmanager removes the upgrade prompt
- 兆骑科创海外高层次人才引进平台,创业赛事活动路演
- PIP installation module, error
- CentOS installs docker and MySQL and redis environments
- Hardware development and market industry
- Week 17 free intrusion pointer exercise - output maximum
- Cross Site Request Forgery (CSRF)
- 跨站脚本攻击(XSS)
猜你喜欢

RedisDesktopManager去除升级提示

【集训Day1】 Dwarves line up

二层管理型交换机如何设置IP
![Cloud rendering volume cloud [theoretical basis and implementation scheme]](/img/38/0e97d6f015f3cb51e872a8d3ce584a.png)
Cloud rendering volume cloud [theoretical basis and implementation scheme]

Asemi rectifier bridge kbpc3510, kbpc3510 package, kbpc3510 application

带你熟悉云网络的“电话簿”:DNS
2.1.2 synchronization always fails

树形dp问题

Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming

JS 闭包 模拟私有变量 面试题 立即执行函数IIFE
随机推荐
3、 Topic communication: create your own information format
Spark统一内存划分
Brief introduction to CUDA image construction
来吧开发者!不只为了 20 万奖金,试试用最好的“积木”来一场头脑风暴吧!
国际大咖 VS 本土开源新星 | ApacheCon Asia 主题演讲议程全览
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
[training Day1] Dwaves line up
PIP installation module, error
股票公司开户万一免五这是真的安全靠谱的吗
(25) top level menu of blender source code analysis blender menu
The user experience center of Analysys Qianfan bank was established to help upgrade the user experience of the banking industry
[metauniverse OMI theory] analyze Web3 risk challenges and build Web3 ecological security
6、 Common commands of ROS (I): rosnode, rostopic, rosmsg
AI sky covering DL multilayer perceptron
Cloud rendering volume cloud [theoretical basis and implementation scheme]
性能调优bug层出不穷?这3份文档轻松搞定JVM调优
Hosts this file has been set to read-only solution
AI zhetianchuan DL regression and classification
Asemi rectifier bridge kbpc2510, kbpc2510 parameters, kbpc2510 specifications
基本的SELECT语句