ROS小乌龟话题改写--话题发布--话题订阅
创建小乌龟
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
2:图显示:
rqt_graph
test02_sub_pose
3:通过命令显示话题名消息信息
方法一获得:
rostopic info /turtle1/cmd_vel
方法二获得:
rostopic type /turtle1/cmd_vel
获取消息格式
方法一
rosmsg show geometry_msgs/Twist
方法二
rosmsg info geometry_msgs/Twist
设置线速度和角速度
线速度:比如说是小车的前进后退的速度
x:代表前进后退的值
y:平移的速度
z:垂直方向上上下的速度
角速度:比如小汽车转弯的速度
x:翻滚
y:俯仰
z:偏航
线速度只设置:X
角速度只设置:Z
打印话题的消息
rostopic echo /turtle1/cmd_vel
使用命令的方式来实现小乌龟做圆周运动
rtopic pub -r 10 /turtle1/cmd_vel geometry_msgs/Twist "linear:x: 1.0y: 0.0z: 0.0
angular:x: 0.0y: 0.0z: 1.0"
plumbing_test
使用c++来实现小乌龟做圆周运动
写入包的名称以及导入使用的依赖
plumbing_test #包名
roscpp rospy std_msgs geometry_msgs #导入的依赖
/*编写 ROS 节点,控制小乌龟画圆准备工作:1.获取topic(已知: /turtle1/cmd_vel)2.获取消息类型(已知: geometry_msgs/Twist)3.运行前,注意先启动 turtlesim_node 节点实现流程:1.包含头文件2.初始化 ROS 节点3.创建发布者对象4.循环发布运动控制消息
*/#include "ros/ros.h"
#include "geometry_msgs/Twist.h"int main(int argc, char *argv[])
{setlocale(LC_ALL,"");// 2.初始化 ROS 节点ros::init(argc,argv,"control");ros::NodeHandle nh;// 3.创建发布者对象ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",10);// 4.循环发布运动控制消息//4-1.组织消息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.设置发送频率ros::Rate r(10);//4-3.循环发送while (ros::ok()){pub.publish(msg);ros::spinOnce();}return 0;
}
source ./devel/setup.bash
roscore
rosrun turtlesim turtlesim_node
rosrun plumbing_test test01_pub_twist
使用python实现圆周运动
#! /usr/bin/env python
"""编写 ROS 节点,控制小乌龟画圆准备工作:source ./devel/setup.bash
roscore
rosrun turtlesim turtlesim_node
rosrun plumbing_test test01_pub_twist1.获取topic(已知: /turtle1/cmd_vel)2.获取消息类型(已知: geometry_msgs/Twist)3.运行前,注意先启动 turtlesim_node 节点实现流程:1.导包2.初始化 ROS 节点3.创建发布者对象4.循环发布运动控制消息"""import rospy
from geometry_msgs.msg import Twistif __name__ == "__main__":# 2.初始化 ROS 节点rospy.init_node("control_circle_p")# 3.创建发布者对象pub = rospy.Publisher("/turtle1/cmd_vel",Twist,queue_size=10)#queue_size=10这个是队列# 4.循环发布运动控制消息source ./devel/setup.bash
roscore
rosrun turtlesim turtlesim_node
rosrun plumbing_test test01_pub_twistrate = rospy.Rate(10)msg = Twist()msg.linear.x = 1.0msg.linear.y = 0.0msg.linear.z = 0.0msg.angular.x = 0.0msg.angular.y = 0.0msg.angular.z = 0.5while not rospy.is_shutdown():pub.publish(msg)rate.sleep()source ./devel/setup.bash
roscore
rosrun turtlesim turtlesim_node
rosrun plumbing_test test01_pub_twist
chmod +x *.py
source ./devel/setup.bash
roscore
rosrun turtlesim turtlesim_node
rosrun plumbing_test test01_pub_twist_p.py
乌龟话题的订阅,位置信息的显示
创建launch文件
<launch><!--乌龟的GUI--><node pkg="turtlesim" type="turtlesim_node" name="turtle1" output="screen"/><!--键盘控制--><node pkg="turtlesim" type="turtle_teleop_key" name="key" output="screen"/></launch>
启动launch文件
source ./devel/setup.bash roslaunch plumbing_test start_turtle.launch
查看话题
rostopic list
查看位置话题信息
rostopic info /turtle1/pose
获取位置消息格式
rosmsg info turtlesim/Pose
x:x轴
y:y轴
theta:朝向。也就是弧度
linear_velocity:线速度
angular_velocity:角速度
打印乌龟位置信息
rostopic echo /turtle1/pose
使用c++来实现功能
2.获取消息类型 turtlesim/Pose使用的是自定义的消息格式要进行追加
/* 订阅小乌龟的位姿: 时时获取小乌龟在窗体中的坐标并打印准备工作:1.获取话题名称 /turtle1/pose2.获取消息类型 turtlesim/Pose3.运行前启动 turtlesim_node 与 turtle_teleop_key 节点实现流程:1.包含头文件2.初始化 ROS 节点3.创建 ROS 句柄4.创建订阅者对象5.回调函数处理订阅的数据6.spin
*/#include "ros/ros.h"
#include "turtlesim/Pose.h" //位置信息的头文件void doPose(const turtlesim::Pose::ConstPtr& p){ROS_INFO("乌龟位姿信息: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.初始化 ROS 节点ros::init(argc,argv,"sub_pose");// 3.创建 ROS 句柄ros::NodeHandle nh;// 4.创建订阅者对象ros::Subscriber sub = nh.subscribe<turtlesim::Pose>("/turtle1/pose",1000,doPose);// 5.回调函数处理订阅的数据// 6.spinros::spin();return 0;
}
在package.xml进行添加
<build_depend>turtlesim</build_depend>
<exec_depend>turtlesim</exec_depend>
在CMakeLists.txt文件中添加
turtlesim
命令的实现
roslaunch plumbing_test start_turtle.launch
source ./devel/setup.bash
rosrun plumbing_test test02_sub_pose
使用python实现
#! /usr/bin/env python
"""订阅小乌龟的位姿: 时时获取小乌龟在窗体中的坐标并打印准备工作:1.获取话题名称 /turtle1/pose2.获取消息类型 turtlesim/Pose3.运行前启动 turtlesim_node 与 turtle_teleop_key 节点实现流程:1.导包2.初始化 ROS 节点3.创建订阅者对象4.回调函数处理订阅的数据5.spin"""import rospy
from turtlesim.msg import Posedef doPose(data):# rospy.loginfo("乌龟坐标:x=%.2f, y=%.2f,theta=%.2f",data.x,data.y,data.theta)rospy.loginfo("乌龟坐标:(%.2f,%.2f),朝向:%.2f,线速度:%.2f,角速度:%.2f",data.x,data.y,data.theta,data.linear_velocity,data.angular_velocity)if __name__ == "__main__":# 2.初始化 ROS 节点rospy.init_node("sub_pose_p")# 3.创建订阅者对象sub = rospy.Subscriber("/turtle1/pose",Pose,doPose,queue_size=1000)# 4.回调函数处理订阅的数据# 5.spinrospy.spin()
命令实现
roslaunch plumbing_test start_turtle.launch
rosrun plumbing_test test02_sub_pose_p.py