赞
踩
主要代码:
//topic-pub
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
publisher_ = nodePtr_->create_publisher<std_msgs::msg::String>("topic", 10);
publisher_->publish(msg);
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);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。