diff --git a/CMakeLists.txt b/CMakeLists.txt index a7709b5..7f640a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,7 @@ endfunction() custom_executable(rclcpp_1431) custom_executable(rclcpp_1454) custom_executable(rclcpp_1455) +custom_executable(rclcpp_1487) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 6.0) diff --git a/src/rclcpp_1487.cpp b/src/rclcpp_1487.cpp new file mode 100644 index 0000000..063d456 --- /dev/null +++ b/src/rclcpp_1487.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include + +#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("topic", 10); + timer_ = this->create_wall_timer( + 1ms, std::bind(&MinimalPublisher::timer_callback, this)); // 1kHz + } + + private: + void timer_callback() + { + auto message = std_msgs::msg::String(); + message.data = "Hello, world! " + std::to_string(count_++); + // RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str()); + publisher_->publish(message); + } + rclcpp::TimerBase::SharedPtr timer_; + rclcpp::Publisher::SharedPtr publisher_; + size_t count_; + }; + + int main(int argc, char * argv[]) + { + rclcpp::init(argc, argv); + + rclcpp::executors::MultiThreadedExecutor exe; + // rclcpp::executors::SingleThreadedExecutor exe; + + std::shared_ptr pub = std::make_shared(); + exe.add_node(pub->get_node_base_interface()); + + exe.spin(); + rclcpp::shutdown(); + return 0; + } \ No newline at end of file