CTRL+shift+a 注释
获取话题 rostopic list
获取消息类型-rostopic info /turtle1/pose
type:
发布者:
订阅者:
查看消息具体格式:rosmsg info turtlesim/Pose
x,y 坐标
theta 朝向
float32 linear_velocity 线速度
float32 angular_velocity 角速度
1.输入命令形式-获取目标位姿
rostopic echo /turtle1/pose
2.c++实现
1.将乌龟节点并联起来
在在功能包plumbing_test目录下新建文件夹launch 并新建文件start_turtle.launch
2.在之前 话题发布实操 所创建的功能包中补充turtlesim
3.在功能包plumbing_test目录src下新建test02_sub_pose.cpp
#include"ros/ros.h"
#include "turtlesim/Pose.h"
/*
xuqiu :订阅小乌龟的位姿,并输出到控制台(打印)
实现流程:
1.包含头文件
2.初始化 ROS 节点
3.创建 ROS 句柄
4.创建订阅者对象
5.回调函数处理订阅的数据
6.spin
*/
//处理订阅到的数据
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("/turtle1/pose",100,doPose);
// 5.回调函数处理订阅的数据
// 6.spin
ros::spin();
return 0;
}
4.配置文件
5.编译
第一个命令行:
cd demo03_ws
catkin_make
roslaunch plumbing_test start_turtle.launch
第二个命令行:
cd demo03_ws
catkin_make
source ./devel/setup.bash
rosrun plumbing_test test02_sub_pose