ROS2/C++ 话题发布和订阅

本文档详细介绍了如何在ROS2中创建功能包,建立发布和接收节点。首先,创建发布节点并编写cpp代码,更新package.xml和CMakelists.txt文件。接着,编译并运行发布节点,然后创建接收节点,同样更新CMakelists.txt。最后,编译运行接收节点,并展示了发布和接收节点在终端上的输出情况。
摘要由CSDN通过智能技术生成

创建功能包

mkdir -p ros_ws/src
cd ros_ws/src
ros2 pkg create --build-type ament_cmake cpp_pubsub

创建发布节点

cd cpp_pubsub/src/
gedit publisher.cpp

cpp文件内容如下:

#include <chrono>
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;

/* This example creates a subclass of Node and uses std::bind() to register a
 * member function as a callback from the timer. */

class MinimalPublisher : public rclcpp::Node
{
   
public:
  MinimalPublisher()
  : Node("minimal_publisher"), count_(0)
  {
   
    publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
    timer_ = this->create_wall_timer(
      500ms, std::bind(&MinimalPublisher::timer_callback, this));
  }

private:
  void timer_callback(
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值