From 42231d06813038bb0f67bdb665181f634aefaf4f Mon Sep 17 00:00:00 2001 From: chyoo Date: Wed, 12 Aug 2026 10:17:21 +0900 Subject: [PATCH] fix(nav2): align mkmini gazebo vehicle geometry --- .../src/steering_command_plugin.cpp | 25 ++++++++++++++++--- .../launch/simulator.launch.py | 2 +- .../models/mkmini_gazebo/model.sdf | 24 +++++++++--------- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/mkmini_gazebo_steering_plugin/src/steering_command_plugin.cpp b/src/mkmini_gazebo_steering_plugin/src/steering_command_plugin.cpp index 802ffd2..9d7afe8 100644 --- a/src/mkmini_gazebo_steering_plugin/src/steering_command_plugin.cpp +++ b/src/mkmini_gazebo_steering_plugin/src/steering_command_plugin.cpp @@ -21,6 +21,7 @@ class SteeringCommandPlugin : public gazebo::ModelPlugin public: void Load(gazebo::physics::ModelPtr model, sdf::ElementPtr sdf) override { + model_ = model; left_joint_ = model->GetJoint(sdf->Get("left_joint")); right_joint_ = model->GetJoint(sdf->Get("right_joint")); if (!left_joint_ || !right_joint_) { @@ -28,7 +29,8 @@ public: return; } wheelbase_ = sdf->Get("wheelbase", 0.60).first; - track_width_ = sdf->Get("track_width", 0.72).first; + track_width_ = sdf->Get("track_width", 0.51).first; + steering_speed_ = sdf->Get("steering_speed", 1.2).first; node_ = gazebo_ros::Node::Get(sdf); const auto topic = sdf->Get("topic", "/mkmini/steering_command").first; subscription_ = node_->create_subscription( @@ -54,9 +56,21 @@ private: { std::lock_guard lock(mutex_); if (!has_target_ || !left_joint_ || !right_joint_) return; + + const auto now = model_->GetWorld()->SimTime(); + if (last_update_time_ == gazebo::common::Time::Zero) { + last_update_time_ = now; + return; + } + const auto dt = std::clamp((now - last_update_time_).Double(), 0.0, 0.1); + last_update_time_ = now; const auto [left_angle, right_angle] = AckermannAngles(target_angle_); - left_joint_->SetPosition(0, left_angle); - right_joint_->SetPosition(0, right_angle); + const auto max_step = steering_speed_ * dt; + const auto step_towards = [max_step](double current, double target) { + return current + std::clamp(target - current, -max_step, max_step); + }; + left_joint_->SetPosition(0, step_towards(left_joint_->Position(0), left_angle)); + right_joint_->SetPosition(0, step_towards(right_joint_->Position(0), right_angle)); } std::pair AckermannAngles(double steering_angle) const @@ -69,6 +83,7 @@ private: return direction > 0.0 ? std::make_pair(inner, outer) : std::make_pair(-outer, -inner); } + gazebo::physics::ModelPtr model_; gazebo::physics::JointPtr left_joint_; gazebo::physics::JointPtr right_joint_; gazebo::event::ConnectionPtr update_connection_; @@ -77,8 +92,10 @@ private: rclcpp::Subscription::SharedPtr cmd_vel_subscription_; std::mutex mutex_; double wheelbase_{0.60}; - double track_width_{0.72}; + double track_width_{0.51}; + double steering_speed_{1.2}; double target_angle_{0.0}; + gazebo::common::Time last_update_time_; bool has_target_{false}; }; diff --git a/src/nav2_turtlebot_simulator/launch/simulator.launch.py b/src/nav2_turtlebot_simulator/launch/simulator.launch.py index e74be44..3713ccc 100644 --- a/src/nav2_turtlebot_simulator/launch/simulator.launch.py +++ b/src/nav2_turtlebot_simulator/launch/simulator.launch.py @@ -393,7 +393,7 @@ def launch_gazebo(context): executable='static_transform_publisher', name='mkmini_lidar_tf', arguments=[ - '--x', '0', '--y', '0', '--z', '0.9', + '--x', '0.15', '--y', '0', '--z', '0.578', '--roll', '0', '--pitch', '0', '--yaw', '0', '--frame-id', 'base_link', '--child-frame-id', 'os_lidar', ], diff --git a/src/nav2_turtlebot_simulator/models/mkmini_gazebo/model.sdf b/src/nav2_turtlebot_simulator/models/mkmini_gazebo/model.sdf index 129d046..586beb9 100644 --- a/src/nav2_turtlebot_simulator/models/mkmini_gazebo/model.sdf +++ b/src/nav2_turtlebot_simulator/models/mkmini_gazebo/model.sdf @@ -4,34 +4,34 @@ Its visible front wheels are separate steering links so the Gazebo view follows real left/right Ackermann angles instead of a rotating axle. --> - 0 0 0.16 0 0 0 + 0 0 0.12 0 0 0 38.02.02.63.2 - 0 0 0.41 0 0 01.05 0.70 0.50 - 0 0 0.41 0 0 01.05 0.70 0.500.12 0.20 0.25 10.12 0.35 0.42 1 - 0.56 0 0.68 0 0 00.06 0.42 0.100.95 0.65 0.08 10.95 0.65 0.08 1 + 0 0 0.334 0 0 00.84 0.61 0.407 + 0 0 0.334 0 0 00.84 0.61 0.4070.12 0.20 0.25 10.12 0.35 0.42 1 + 0.42 0 0.515 0 0 00.049 0.366 0.0810.95 0.65 0.08 10.95 0.65 0.08 1 0.30 0 0 0 0 01.00.010.010.01 base_linkfront_fork0 0 1-0.50.5 - 0.30 0 0 0 0 01.20.020.010.020 0 0 1.5708 0 00.140.18 + 0.30 0 0 0 0 01.20.020.010.020 0 0 1.5708 0 00.120.05 front_forkfront_drive_wheel0 1 0 - 0.30 0.36 0 0 0 00.050.00010.00010.00010 0 0 1.5708 0 00.140.100.04 0.04 0.04 10.04 0.04 0.04 1 + 0.30 0.255 0 0 0 00.050.00010.00010.00010 0 0 1.5708 0 00.120.050.04 0.04 0.04 10.04 0.04 0.04 1 base_linkfront_left_visual_wheel0 0 1-0.60.6 - 0.30 -0.36 0 0 0 00.050.00010.00010.00010 0 0 1.5708 0 00.140.100.04 0.04 0.04 10.04 0.04 0.04 1 + 0.30 -0.255 0 0 0 00.050.00010.00010.00010 0 0 1.5708 0 00.120.050.04 0.04 0.04 10.04 0.04 0.04 1 base_linkfront_right_visual_wheel0 0 1-0.60.6 - -0.30 0.36 0 0 0 01.00.020.010.020 0 0 1.5708 0 00.140.100 0 0 1.5708 0 00.140.100.04 0.04 0.04 10.04 0.04 0.04 1 + -0.30 0.255 0 0 0 01.00.020.010.020 0 0 1.5708 0 00.120.050 0 0 1.5708 0 00.120.050.04 0.04 0.04 10.04 0.04 0.04 1 base_linkrear_left_wheel0 1 0 - -0.30 -0.36 0 0 0 01.00.020.010.020 0 0 1.5708 0 00.140.100 0 0 1.5708 0 00.140.100.04 0.04 0.04 10.04 0.04 0.04 1 + -0.30 -0.255 0 0 0 01.00.020.010.020 0 0 1.5708 0 00.120.050 0 0 1.5708 0 00.120.050.04 0.04 0.04 10.04 0.04 0.04 1 base_linkrear_right_wheel0 1 0 - 0 0 0.9 0 0 00.20.0010.0010.0010.0750.100.85 0.12 0.10 10.85 0.12 0.10 1true10true3601-3.141593.1415911000.2018.00.02~/out:=/raw/lidarsensor_msgs/PointCloud2os_lidar + 0.15 0 0.578 0 0 00.20.0010.0010.0010.0610.0810.85 0.12 0.10 10.85 0.12 0.10 1true10true3601-3.141593.1415911000.2018.00.02~/out:=/raw/lidarsensor_msgs/PointCloud2os_lidar base_linkos_lidar - cmd_vel:=/cmd_velodom:=/odomfront_steering_jointfront_drive_jointrear_left_jointrear_right_joint0.720.280.28120.01.50.0truetruetrueodombase_link - /mkmini/steering_commandfront_left_visual_steering_jointfront_right_visual_steering_joint0.600.72 + cmd_vel:=/cmd_velodom:=/odomfront_steering_jointfront_drive_jointrear_left_jointrear_right_joint0.510.240.24120.01.51.2truetruetrueodombase_link + /mkmini/steering_commandfront_left_visual_steering_jointfront_right_visual_steering_joint0.600.511.2