c++创建订阅者和发布者

2023-10-11 06:32
文章标签 c++ 创建 订阅 发布者

本文主要是介绍c++创建订阅者和发布者,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

此文章默认读者已经对ros有了一定的基础,明白ros之间如何进行数据通信,了解ros的文件结构,了解工作空间,功能包的概念。本篇文章参考创客智造上的教程,他们的讲解比我更详细。百度一下创客智造即可。

  订阅者和发布者依托于节点,即订阅者和发布者是在节点中完成的。所以先来说一下节点的定义,步骤可分为:

1.新建代码源文件

2.写代码

3.在cmakelist.txt文件中定义

订阅者和发布者在写代码的步骤中。

 

下面上一段,roswiki上给出的官方demo,然后对demo做讲解。

talker.cpp

#include "ros/ros.h"

#include "std_msgs/String.h"

 

#include <sstream>  //c++里的导包,对于我这种c++半路出家的,就理解为java里的import语句

//值得一说的是第一行语句,include ros/ros.h  ,引入ros的相关头文件,这是所有ros开发的起点

 

/**

 * This tutorial demonstrates simple sending of messages over the ROS system.

 */

int main(int argc, char **argv)

{

  /**

   * The ros::init() function needs to see argc and argv so that it can perform

   * any ROS arguments and name remapping that were provided at the command line.

   * For programmatic remappings you can use a different version of init() which takes

   * remappings directly, but for most command-line programs, passing argc and argv is

   * the easiest way to do it.  The third argument to init() is the name of the node.

   *

   * You must call one of the versions of ros::init() before using any other

   * part of the ROS system.

   */

  ros::init(argc, argv, "talker");    //初始化节点,第一二个参数走main里传进来,第三个参数为节点名称

 

  /**

   * NodeHandle is the main access point to communications with the ROS system.

   * The first NodeHandle constructed will fully initialize this node, and the last

   * NodeHandle destructed will close down the node.

   */

•   ros::NodeHandle n;   //实例化节点

 

 

  /**

   * The advertise() function is how you tell ROS that you want to

   * publish on a given topic name. This invokes a call to the ROS

   * master node, which keeps a registry of who is publishing and who

   * is subscribing. After this advertise() call is made, the master

   * node will notify anyone who is trying to subscribe to this topic name,

   * and they will in turn negotiate a peer-to-peer connection with this

   * node.  advertise() returns a Publisher object which allows you to

   * publish messages on that topic through a call to publish().  Once

   * all copies of the returned Publisher object are destroyed, the topic

   * will be automatically unadvertised.

   *

   * The second parameter to advertise() is the size of the message queue

   * used for publishing messages.  If messages are published more quickly

   * than we can send them, the number here specifies how many messages to

   * buffer up before throwing some away.

   */

  ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);

//定义一个发布者,尖括号内的泛型是ros里的消息格式,后面的参数分别为话题名称和消息队列长度

  ros::Rate loop_rate(10);

//设置循环的频率

 

 

  /**

   * A count of how many messages we have sent. This is used to create

   * a unique string for each message.

   */

  int count = 0;

  while (ros::ok())  //ros::ok表示ros的状态,当程序出问题或者ctrl+c退出时为false

  {

    /**

     * This is a message object. You stuff it with data, and then publish it.

     */

    std_msgs::String msg;  //定义需要发布的数据

 

    std::stringstream ss;

    ss << "hello world " << count;

    msg.data = ss.str();

 

    ROS_INFO("%s", msg.data.c_str());  //ros_info是ros提供的打印信息的方法

 

    /**

     * The publish() function is how you send messages. The parameter

     * is the message object. The type of this object must agree with the type

     * given as a template parameter to the advertise<>() call, as was done

     * in the constructor above.

     */

    chatter_pub.publish(msg);

 

    ros::spinOnce();  

 

loop_rate.sleep();  //类似于线程里的sleep方法

 

    ++count;

  }

  return 0;

}

 

 

 

Listener.cpp

#include "ros/ros.h"

#include "std_msgs/String.h"

 

/**

 * This tutorial demonstrates simple receipt of messages over the ROS system.

 */

//回调函数,用作数据的处理

void chatterCallback(const std_msgs::String::ConstPtr& msg)

{

  ROS_INFO("I heard: [%s]", msg->data.c_str());

}

 

int main(int argc, char **argv)

{

  /**

   * The ros::init() function needs to see argc and argv so that it can perform

   * any ROS arguments and name remapping that were provided at the command line.

   * For programmatic remappings you can use a different version of init() which takes

   * remappings directly, but for most command-line programs, passing argc and argv is

   * the easiest way to do it.  The third argument to init() is the name of the node.

   *

   * You must call one of the versions of ros::init() before using any other

   * part of the ROS system.

   */

  ros::init(argc, argv, "listener");

 

  /**

   * NodeHandle is the main access point to communications with the ROS system.

   * The first NodeHandle constructed will fully initialize this node, and the last

   * NodeHandle destructed will close down the node.

   */

  ros::NodeHandle n;

 

  /**

   * The subscribe() call is how you tell ROS that you want to receive messages

   * on a given topic.  This invokes a call to the ROS

   * master node, which keeps a registry of who is publishing and who

   * is subscribing.  Messages are passed to a callback function, here

   * called chatterCallback.  subscribe() returns a Subscriber object that you

   * must hold on to until you want to unsubscribe.  When all copies of the Subscriber

   * object go out of scope, this callback will automatically be unsubscribed from

   * this topic.

   *

   * The second parameter to the subscribe() function is the size of the message

   * queue.  If messages are arriving faster than they are being processed, this

   * is the number of messages that will be buffered up before beginning to throw

   * away the oldest ones.

   */

  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback); 

//实例化订阅者对象,第一个参数为订阅的话题名称,第二个参数为消息队列长度

   第三个参数为回调函数,即接收到数据后要执行的方法

 

  /**

   * ros::spin() will enter a loop, pumping callbacks.  With this version, all

   * callbacks will be called from within this thread (the main one).  ros::spin()

   * will exit when Ctrl-C is pressed, or the node is shutdown by the master.

   */

  ros::spin();

 

  return 0;

}

 

相比较来说,话题的订阅者比发布者更复杂一点,发布者只关注将数据发布出去,订阅者更关注数据接收到之后如何做处理,当然并不是一个节点只能定义一个发布者或者订阅者,一个发布者发布的数据可以是本话题里订阅处理后的数据,最重要的还是让整个系统条理清晰。 节点和话题的命名最好做到结构清晰,让人望文知义。

代码写好之后,接下来就是编译,并且发布到ros系统中。Ros工程使用cmake构建,所以ros的编译要编写cmakelist.txt,然后使用ros的编译命令catkin_make进行编译。所以如果要进行ros更层次的开发,了解一下cmakelist的语法也是必须的。

首先找到功能包根目录下的cmakelist.txt文件,添加如下代码:

 

include_directories(include ${catkin_INCLUDE_DIRS})

 

add_executable(talker src/talker.cpp)

#定义增加的可执行文件和源码位置

target_link_libraries(talker ${catkin_LIBRARIES})

#定义连接库

add_dependencies(talker beginner_tutorials_generate_messages_cpp)

#定义依赖

add_executable(listener src/listener.cpp)

target_link_libraries(listener ${catkin_LIBRARIES})

add_dependencies(listener beginner_tutorials_generate_messages_cpp)

 

cmakelist文件编写好之后,要进入工作空间的根目录,进行编译:

在工作空间根目录下运行命令catkin_make,若不报错则成功

 

 

验证一下;

终端1:roscore启动ros

终端2:rosrun 包名(此处为你的包名) talker

终端3:rosrun 包名 listener

观察屏幕打印信息

这篇关于c++创建订阅者和发布者的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/186227

相关文章

C++ HTTP框架推荐(特点及优势)

《C++HTTP框架推荐(特点及优势)》:本文主要介绍C++HTTP框架推荐的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Crow2. Drogon3. Pistache4. cpp-httplib5. Beast (Boos

C++类和对象之初始化列表的使用方式

《C++类和对象之初始化列表的使用方式》:本文主要介绍C++类和对象之初始化列表的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C++初始化列表详解:性能优化与正确实践什么是初始化列表?初始化列表的三大核心作用1. 性能优化:避免不必要的赋值操作2. 强

C++迭代器失效的避坑指南

《C++迭代器失效的避坑指南》在C++中,迭代器(iterator)是一种类似指针的对象,用于遍历STL容器(如vector、list、map等),迭代器失效是指在对容器进行某些操作后... 目录1. 什么是迭代器失效?2. 哪些操作会导致迭代器失效?2.1 vector 的插入操作(push_back,

Java 如何创建和使用ExecutorService

《Java如何创建和使用ExecutorService》ExecutorService是Java中用来管理和执行多线程任务的一种高级工具,可以有效地管理线程的生命周期和任务的执行过程,特别是在需要处... 目录一、什么是ExecutorService?二、ExecutorService的核心功能三、如何创建

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文