Compare commits
No commits in common. "1b2d482dd286211cede350820b138ee3c650cdac" and "11748e259657047fe259036767eca23ea1ef22b4" have entirely different histories.
1b2d482dd2
...
11748e2596
|
|
@ -5,49 +5,32 @@
|
|||
class CmdPublisher : public rclcpp::Node
|
||||
{
|
||||
public:
|
||||
CmdPublisher() : Node("cmd_pub"), gen_(rd_()), dis_(-1.0, 1.0)
|
||||
CmdPublisher() : Node("cmd_pub"), gen_(rd_()), dis_(-2.0, 2.0)
|
||||
{
|
||||
publisher_ = this->create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", 10);
|
||||
|
||||
// Publish at 10 Hz for continuous movement
|
||||
timer_ = this->create_wall_timer(
|
||||
std::chrono::milliseconds(100),
|
||||
std::chrono::seconds(1),
|
||||
std::bind(&CmdPublisher::timer_callback, this));
|
||||
|
||||
// Generate new random angular velocity every second
|
||||
random_timer_ = this->create_wall_timer(
|
||||
std::chrono::seconds(1),
|
||||
std::bind(&CmdPublisher::generate_random_velocity, this));
|
||||
|
||||
current_angular_z_ = dis_(gen_);
|
||||
RCLCPP_INFO(this->get_logger(), "CMD Publisher node started - Publishing at 10Hz");
|
||||
RCLCPP_INFO(this->get_logger(), "CMD Publisher node started");
|
||||
}
|
||||
|
||||
private:
|
||||
void generate_random_velocity()
|
||||
{
|
||||
current_angular_z_ = dis_(gen_);
|
||||
RCLCPP_INFO(this->get_logger(), "New Random Angular Velocity Z: %.4f rad/s",
|
||||
current_angular_z_);
|
||||
}
|
||||
|
||||
void timer_callback()
|
||||
{
|
||||
auto message = geometry_msgs::msg::Twist();
|
||||
// Add small linear velocity to help TurtleBot3 move
|
||||
message.linear.x = 0.1; // Small forward movement
|
||||
message.angular.z = current_angular_z_;
|
||||
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<geometry_msgs::msg::Twist>::SharedPtr publisher_;
|
||||
rclcpp::TimerBase::SharedPtr timer_;
|
||||
rclcpp::TimerBase::SharedPtr random_timer_;
|
||||
std::random_device rd_;
|
||||
std::mt19937 gen_;
|
||||
std::uniform_real_distribution<> dis_;
|
||||
double current_angular_z_;
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
|
|
|
|||
Loading…
Reference in New Issue