您当前的位置: 首页 > 

我什么都布吉岛

暂无认证

  • 1浏览

    0关注

    292博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ROS入门21讲笔记(三)代码形式写一个订阅者向发布者订阅消息

我什么都布吉岛 发布时间:2022-03-27 00:27:27 ,浏览量:1

其实发布程序和订阅程序没有什么不同。

#mermaid-svg-Yx2OuGOiJi1jDyoO {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-Yx2OuGOiJi1jDyoO .error-icon{fill:#552222;}#mermaid-svg-Yx2OuGOiJi1jDyoO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Yx2OuGOiJi1jDyoO .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-Yx2OuGOiJi1jDyoO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Yx2OuGOiJi1jDyoO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Yx2OuGOiJi1jDyoO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Yx2OuGOiJi1jDyoO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Yx2OuGOiJi1jDyoO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Yx2OuGOiJi1jDyoO .marker.cross{stroke:#333333;}#mermaid-svg-Yx2OuGOiJi1jDyoO svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Yx2OuGOiJi1jDyoO .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Yx2OuGOiJi1jDyoO .cluster-label text{fill:#333;}#mermaid-svg-Yx2OuGOiJi1jDyoO .cluster-label span{color:#333;}#mermaid-svg-Yx2OuGOiJi1jDyoO .label text,#mermaid-svg-Yx2OuGOiJi1jDyoO span{fill:#333;color:#333;}#mermaid-svg-Yx2OuGOiJi1jDyoO .node rect,#mermaid-svg-Yx2OuGOiJi1jDyoO .node circle,#mermaid-svg-Yx2OuGOiJi1jDyoO .node ellipse,#mermaid-svg-Yx2OuGOiJi1jDyoO .node polygon,#mermaid-svg-Yx2OuGOiJi1jDyoO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Yx2OuGOiJi1jDyoO .node .label{text-align:center;}#mermaid-svg-Yx2OuGOiJi1jDyoO .node.clickable{cursor:pointer;}#mermaid-svg-Yx2OuGOiJi1jDyoO .arrowheadPath{fill:#333333;}#mermaid-svg-Yx2OuGOiJi1jDyoO .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Yx2OuGOiJi1jDyoO .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Yx2OuGOiJi1jDyoO .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-Yx2OuGOiJi1jDyoO .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-Yx2OuGOiJi1jDyoO .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Yx2OuGOiJi1jDyoO .cluster text{fill:#333;}#mermaid-svg-Yx2OuGOiJi1jDyoO .cluster span{color:#333;}#mermaid-svg-Yx2OuGOiJi1jDyoO div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Yx2OuGOiJi1jDyoO :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
开始
创建package
编写订阅者代码
修改CMakeLists.txt并编译
结束

这里我们将订阅者和发布者都放在一个工作空间内,同时让订阅者与发布者处于不同的package中。

Step1 创建一个package

一个工作空间可以有多个package,在工作空间的源文件目录src中直接调用catkin_create_pkg创建一个package:

catkin_create_pkg  receieve-turlepose roscpp rospy std_msgs geometry_msgs turtlesim
Step2 书写接受话题的代码
#include 
#include "turtlesim/Pose.h"

void postCallback(const turtlesim::Pose::ConstPtr &msg) //回调函数,参数是turtlesim::Pose::ConstPtr
{
	ROS_INFO("Turtle pose:x:%0.6, y:%0.6",msg->x,msg->y);//注意回调时间应该尽可能短
}

int main(int argc,char **argv)
{
	ros::init(argc,argv,"GetSubInfo");//初始化这个节点
	ros::NodeHandle n;				//订阅和发布都需要用到这个句柄
	
	ros::Subscriber getSub=n.subscribe("/turtle1/pose",10,postCallback);//最后一个参数是一个回调函数
	
	ros::spin();//循环等待方法,阻塞以查看队列是否有新消息进来,如果有就调用postCallback
	
	return 0;//因为前面在循环等待,所以一般不会执行到这一句
}
Final:编译并运行

和上一节一样,需要修改对应的CMakeLists.txt。最后在WorkSpace根目录下进行编译catkin_make,如无意外将会显示: 在这里插入图片描述

运行结果如下,打印的就是海龟的姿态了: 在这里插入图片描述

关注
打赏
1658157489
查看更多评论
立即登录/注册

微信扫码登录

0.0398s