From 70e8b5326d68e283c99696e4a7f971c1eb5bb186 Mon Sep 17 00:00:00 2001 From: "jerome.bedier" Date: Tue, 18 Nov 2025 14:03:53 +0100 Subject: [PATCH] added the cmd_pub.cpp --- ros_midterm/src/cmd_pub.cpp | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ros_midterm/src/cmd_pub.cpp diff --git a/ros_midterm/src/cmd_pub.cpp b/ros_midterm/src/cmd_pub.cpp new file mode 100644 index 0000000..00b180f --- /dev/null +++ b/ros_midterm/src/cmd_pub.cpp @@ -0,0 +1,42 @@ +#include "rclcpp/rclcpp.hpp" +#include "geometry_msgs/msg/twist.hpp" +#include + +class CmdPublisher : public rclcpp::Node +{ +public: + CmdPublisher() : Node("cmd_pub"), gen_(rd_()), dis_(-2.0, 2.0) + { + publisher_ = this->create_publisher("/cmd_vel", 10); + timer_ = this->create_wall_timer( + std::chrono::seconds(1), + std::bind(&CmdPublisher::timer_callback, this)); + + RCLCPP_INFO(this->get_logger(), "CMD Publisher node started"); + } + +private: + void timer_callback() + { + auto message = geometry_msgs::msg::Twist(); + message.angular.z = dis_(gen_); + + RCLCPP_INFO(this->get_logger(), "Publishing Angular Velocity Z: %.4f rad/s", + message.angular.z); + publisher_->publish(message); + } + + rclcpp::Publisher::SharedPtr publisher_; + rclcpp::TimerBase::SharedPtr timer_; + std::random_device rd_; + std::mt19937 gen_; + std::uniform_real_distribution<> dis_; +}; + +int main(int argc, char * argv[]) +{ + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +}