ros21讲学习-订阅话题
Posted polipolu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ros21讲学习-订阅话题相关的知识,希望对你有一定的参考价值。
代码:
/*********************************************************************** Copyright 2020 GuYueHome (www.guyuehome.com). ***********************************************************************/ /** * 该例程将订阅/turtle1/pose话题,消息类型turtlesim::Pose */ #include <ros/ros.h> #include "turtlesim/Pose.h"//消息类型 // 接收到订阅的消息后,会进入消息回调函数 void poseCallback(const turtlesim::Pose::ConstPtr& msg) { // 将接收到的消息打印出来 ROS_INFO("Turtle pose: x:%0.6f, y:%0.6f", msg->x, msg->y); } int main(int argc, char **argv) { // 初始化ROS节点 ros::init(argc, argv, "pose_subscriber"); // 创建节点句柄 ros::NodeHandle n; // 创建一个Subscriber,订阅名为/turtle1/pose的topic,注册回调函数poseCallback,消息队列为10 ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10, poseCallback); // 循环等待回调函数 ros::spin(); return 0; }
编译代码,包的cmakelist中写入
add_executable(pose_subscriber src/pose_subscriber.cpp)
target_link_libraries(pose_subscriber ${catkin_LIBRARIES})
编译
cd ~/workspace/catkin_ws
catkin_make
source ~/workspace/catkin_ws/devel/setup.bash
执行:
终端1: roscore 终端2: rosrun turtlesim turtlesim_node 终端3: rosrun learning_topic velocity_publisher
终端4: rosrun learning_topic pose_subscriber
以上是关于ros21讲学习-订阅话题的主要内容,如果未能解决你的问题,请参考以下文章