目录
一、话题与消息获取
二、代码编写
1、C++
2、python
三、编译运行
一、话题与消息获取
rostopic list
rostopic type /turtle1/pose
rosmsg info turtlesim/Pose
二、代码编写
1、C++
//包含头文件
#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[])
{
//初始化 ROS 节点
ros::init(argc,argv,"sub_pose");
//创建 ROS 句柄
ros::NodeHandle nh;
//创建订阅者对象
ros::Subscriber sub = nh.subscribe<turtlesim::Pose>("/turtle1/pose",1000,doPose);
ros::spin();
return 0;
}
2、python
#! /usr/bin/env python
# -*- coding:UTF-8 -*-
#导包
import rospy
from turtlesim.msg import Pose
def doPose(data):
rospy.loginfo("x=%.2f, y=%.2f,theta=%.2f",data.x,data.y,data.theta)
if __name__ == "__main__":
#初始化 ROS 节点
rospy.init_node("sub_pose_p")
#创建订阅者对象
sub = rospy.Subscriber("/turtle1/pose",Pose,doPose,queue_size=1000)
#回调函数处理订阅的数据
#spin
rospy.spin()