当前位置:   article > 正文

ROS2学习之路之话题发布(Publisher)_ros2 publisher 发布源码分析、

ros2 publisher 发布源码分析、

主要代码:

//topic-pub
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
publisher_ = nodePtr_->create_publisher<std_msgs::msg::String>("topic", 10);
publisher_->publish(msg);
  • 1
  • 2
  • 3
  • 4

eg.1


#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/int8.hpp>
#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;


class MinimalPublisher
{
  private:
    rclcpp::Node::SharedPtr nodePtr_;
    rclcpp::TimerBase::SharedPtr timer_;
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
    size_t count_;
  public:
    MinimalPublisher(rclcpp::Node::SharedPtr& nodePtr):nodePtr_(nodePtr),count_(0)
    {
      publisher_ = nodePtr_->create_publisher<std_msgs::msg::String>("topic", 10);
      timer_ = nodePtr_->create_wall_timer(500ms, std::bind(&MinimalPublisher::timer_callback, this));
    }
    void timer_callback()
    {
      auto message = std_msgs::msg::String();
      message.data = "Hello, world! " + std::to_string(count_++);
      RCLCPP_INFO(nodePtr_->get_logger(), "Publishing: '%s'", message.data.c_str());
      publisher_->publish(message);
    }

};

int main(int argc, char** argv) {
  rclcpp::init(argc, argv);

  auto nodePtr = rclcpp::Node::make_shared("mpac_points_filter");
  auto heartBeatPubPtr = nodePtr->create_publisher<std_msgs::msg::Int8>("heartBeat", 10);

  std_msgs::msg::Int8 heartBeatData;
  heartBeatData.data = 6;  //6-filter node
  auto heartBeatTimer = nodePtr->create_wall_timer(std::chrono::seconds(1), [heartBeatPubPtr, heartBeatData]() { heartBeatPubPtr->publish(heartBeatData); });
  
  MinimalPublisher vc(nodePtr);

  rclcpp::spin(nodePtr);
  rclcpp::shutdown();

  return 0;
}

//topic-pub
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
publisher_ = nodePtr_->create_publisher<std_msgs::msg::String>("topic", 10);
publisher_->publish(message);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号