From 7e4b18611ae479cef368e5ed6f78e125249d34ce Mon Sep 17 00:00:00 2001 From: blar Date: Mon, 16 Nov 2026 09:17:04 -0800 Subject: [PATCH] fix(rover): hold a straight line when reversing in Position mode (#29773) The differential and Ackermann position controllers measured the heading error against the bearing to the target, also when driving in reverse, where the rover has to point away from that bearing. A rover that was lined up to reverse therefore had a heading error of 180 deg. The differential rover spot turned because of it, and since the yaw setpoint is only turned around while the speed setpoint is negative, it kept alternating between turning towards the bearing and away from it. The Ackermann rover applied the RO_SPEED_RED speed reduction for that error. Turn the yaw setpoint around before the heading error is calculated. Position mode stored the bearing of its desired track when the track hold started. Moving the stick from forwards to reverse without resting at zero kept that bearing, so the target stayed in front of the rover, which turned around to back up towards it. Store the heading of the track instead and take the bearing from the sign of the stick, so the rover reverses along the line it drove forwards on. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: blar-tw --- .../AckermannManualMode/AckermannManualMode.cpp | 14 ++++++++------ .../AckermannManualMode/AckermannManualMode.hpp | 2 +- .../AckermannPosControl/AckermannPosControl.cpp | 9 ++++++--- .../DifferentialManualMode.cpp | 14 ++++++++------ .../DifferentialManualMode.hpp | 2 +- .../DifferentialPosControl.cpp | 8 ++++++-- 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/modules/rover_ackermann/AckermannDriveModes/AckermannManualMode/AckermannManualMode.cpp b/src/modules/rover_ackermann/AckermannDriveModes/AckermannManualMode/AckermannManualMode.cpp index a9164032f26..9eb741bf523 100644 --- a/src/modules/rover_ackermann/AckermannDriveModes/AckermannManualMode/AckermannManualMode.cpp +++ b/src/modules/rover_ackermann/AckermannDriveModes/AckermannManualMode/AckermannManualMode.cpp @@ -188,7 +188,7 @@ void AckermannManualMode::position() if (fabsf(sticks.yaw) > FLT_EPSILON || fabsf(speed_setpoint) < FLT_EPSILON) { - _pos_ctl_bearing = NAN; + _pos_ctl_yaw = NAN; // Speed control surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; @@ -222,14 +222,16 @@ void AckermannManualMode::position() _surface_vehicle_position_setpoint_pub.publish(surface_vehicle_position_setpoint); } else { // Follow an endless linear desired track - if (!PX4_ISFINITE(_pos_ctl_bearing)) { - // The bearing points backwards when driving in reverse - _pos_ctl_bearing = speed_setpoint > 0.f ? _vehicle_yaw : matrix::wrap_pi(_vehicle_yaw + M_PI_F); + if (!PX4_ISFINITE(_pos_ctl_yaw)) { + _pos_ctl_yaw = _vehicle_yaw; _pos_ctl_start_position_ned = _curr_pos_ned; } + // The bearing follows the stick: it points backwards when driving in reverse + const float bearing = speed_setpoint > 0.f ? _pos_ctl_yaw : matrix::wrap_pi(_pos_ctl_yaw + M_PI_F); + // Hold the linear desired track through the start position: the target is kept beyond the maximum lookahead, so it is never reached - const Vector2f bearing_direction(cosf(_pos_ctl_bearing), sinf(_pos_ctl_bearing)); + const Vector2f bearing_direction(cosf(bearing), sinf(bearing)); const float distance_along_track = fabsf((_curr_pos_ned - _pos_ctl_start_position_ned) * bearing_direction); const Vector2f target_ned = _pos_ctl_start_position_ned + (distance_along_track + _param_pp_lookahd_max.get()) * bearing_direction; @@ -251,7 +253,7 @@ void AckermannManualMode::position() void AckermannManualMode::reset() { _stab_yaw_setpoint = NAN; - _pos_ctl_bearing = NAN; + _pos_ctl_yaw = NAN; _pos_ctl_start_position_ned = Vector2f(NAN, NAN); _curr_pos_ned = Vector2f(NAN, NAN); } diff --git a/src/modules/rover_ackermann/AckermannDriveModes/AckermannManualMode/AckermannManualMode.hpp b/src/modules/rover_ackermann/AckermannDriveModes/AckermannManualMode/AckermannManualMode.hpp index c4150c08784..1858e67f18a 100644 --- a/src/modules/rover_ackermann/AckermannDriveModes/AckermannManualMode/AckermannManualMode.hpp +++ b/src/modules/rover_ackermann/AckermannDriveModes/AckermannManualMode/AckermannManualMode.hpp @@ -123,7 +123,7 @@ private: Quatf _vehicle_attitude_quaternion{}; Vector2f _pos_ctl_start_position_ned{NAN, NAN}; Vector2f _curr_pos_ned{NAN, NAN}; - float _pos_ctl_bearing{NAN}; // Bearing from the start position to the target, i.e. the direction of the desired track [rad] + float _pos_ctl_yaw{NAN}; // Heading of the rover at the start position, the desired track runs along it [rad] float _stab_yaw_setpoint{NAN}; float _vehicle_yaw{NAN}; float _max_yaw_rate{NAN}; diff --git a/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp b/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp index d1cd5f7fdaa..086551882e1 100644 --- a/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp +++ b/src/modules/rover_ackermann/AckermannPosControl/AckermannPosControl.cpp @@ -96,8 +96,12 @@ void AckermannPosControl::updatePosControl() _pure_pursuit_status_pub.publish(pure_pursuit_status); } + // Driving in reverse, the rover points away from its direction of travel + const float yaw_setpoint = speed_setpoint > -FLT_EPSILON ? bearing_setpoint : matrix::wrap_pi( + bearing_setpoint + M_PI_F); + if (_param_ro_speed_red.get() > FLT_EPSILON) { - const float course_error = fabsf(matrix::wrap_pi(bearing_setpoint - _vehicle_yaw)); + const float course_error = fabsf(matrix::wrap_pi(yaw_setpoint - _vehicle_yaw)); const float speed_reduction = math::constrain(_param_ro_speed_red.get() * math::interpolate(course_error, 0.f, M_PI_F, 0.f, 1.f), 0.f, 1.f); const float max_speed = math::constrain(_param_ro_max_thr_speed.get() * (1.f - speed_reduction), _min_speed, @@ -111,8 +115,7 @@ void AckermannPosControl::updatePosControl() _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; surface_vehicle_attitude_setpoint.timestamp = timestamp; - surface_vehicle_attitude_setpoint.yaw_setpoint = speed_setpoint > -FLT_EPSILON ? bearing_setpoint : matrix::wrap_pi( - bearing_setpoint + M_PI_F); + surface_vehicle_attitude_setpoint.yaw_setpoint = yaw_setpoint; _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); } else { diff --git a/src/modules/rover_differential/DifferentialDriveModes/DifferentialManualMode/DifferentialManualMode.cpp b/src/modules/rover_differential/DifferentialDriveModes/DifferentialManualMode/DifferentialManualMode.cpp index c00251ccf4e..fa40e940ebd 100644 --- a/src/modules/rover_differential/DifferentialDriveModes/DifferentialManualMode/DifferentialManualMode.cpp +++ b/src/modules/rover_differential/DifferentialDriveModes/DifferentialManualMode/DifferentialManualMode.cpp @@ -181,7 +181,7 @@ void DifferentialManualMode::position() if (fabsf(sticks.yaw) > FLT_EPSILON || fabsf(speed_setpoint) < FLT_EPSILON) { - _pos_ctl_bearing = NAN; + _pos_ctl_yaw = NAN; // Speed control surface_vehicle_speed_setpoint_s surface_vehicle_speed_setpoint{}; @@ -214,14 +214,16 @@ void DifferentialManualMode::position() _surface_vehicle_position_setpoint_pub.publish(surface_vehicle_position_setpoint); } else { // Follow an endless linear desired track - if (!PX4_ISFINITE(_pos_ctl_bearing)) { - // The bearing points backwards when driving in reverse - _pos_ctl_bearing = speed_setpoint > 0.f ? _vehicle_yaw : matrix::wrap_pi(_vehicle_yaw + M_PI_F); + if (!PX4_ISFINITE(_pos_ctl_yaw)) { + _pos_ctl_yaw = _vehicle_yaw; _pos_ctl_start_position_ned = _curr_pos_ned; } + // The bearing follows the stick: it points backwards when driving in reverse + const float bearing = speed_setpoint > 0.f ? _pos_ctl_yaw : matrix::wrap_pi(_pos_ctl_yaw + M_PI_F); + // Hold the linear desired track through the start position: the target is kept beyond the maximum lookahead, so it is never reached - const Vector2f bearing_direction(cosf(_pos_ctl_bearing), sinf(_pos_ctl_bearing)); + const Vector2f bearing_direction(cosf(bearing), sinf(bearing)); const float distance_along_track = fabsf((_curr_pos_ned - _pos_ctl_start_position_ned) * bearing_direction); const Vector2f target_ned = _pos_ctl_start_position_ned + (distance_along_track + _param_pp_lookahd_max.get()) * bearing_direction; @@ -243,7 +245,7 @@ void DifferentialManualMode::position() void DifferentialManualMode::reset() { _stab_yaw_setpoint = NAN; - _pos_ctl_bearing = NAN; + _pos_ctl_yaw = NAN; _pos_ctl_start_position_ned = Vector2f(NAN, NAN); _curr_pos_ned = Vector2f(NAN, NAN); } diff --git a/src/modules/rover_differential/DifferentialDriveModes/DifferentialManualMode/DifferentialManualMode.hpp b/src/modules/rover_differential/DifferentialDriveModes/DifferentialManualMode/DifferentialManualMode.hpp index 71313fdde88..f6f106b2fd6 100644 --- a/src/modules/rover_differential/DifferentialDriveModes/DifferentialManualMode/DifferentialManualMode.hpp +++ b/src/modules/rover_differential/DifferentialDriveModes/DifferentialManualMode/DifferentialManualMode.hpp @@ -123,7 +123,7 @@ private: // Variables Vector2f _pos_ctl_start_position_ned{NAN, NAN}; Vector2f _curr_pos_ned{NAN, NAN}; - float _pos_ctl_bearing{NAN}; // Bearing from the start position to the target, i.e. the direction of the desired track [rad] + float _pos_ctl_yaw{NAN}; // Heading of the rover at the start position, the desired track runs along it [rad] float _stab_yaw_setpoint{NAN}; float _vehicle_yaw{NAN}; float _max_yaw_rate{NAN}; diff --git a/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp b/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp index 36cd97810b8..cdd364c5cca 100644 --- a/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp +++ b/src/modules/rover_differential/DifferentialPosControl/DifferentialPosControl.cpp @@ -94,6 +94,11 @@ void DifferentialPosControl::updatePosControl() _pure_pursuit_status_pub.publish(pure_pursuit_status); } + // Driving in reverse, the rover points away from its direction of travel + if (speed_setpoint < -FLT_EPSILON) { + yaw_setpoint = matrix::wrap_pi(yaw_setpoint + M_PI_F); + } + const float heading_error = matrix::wrap_pi(yaw_setpoint - _vehicle_yaw); if (_current_state == DrivingState::DRIVING && fabsf(heading_error) > _param_rd_trans_drv_trn.get()) { @@ -120,8 +125,7 @@ void DifferentialPosControl::updatePosControl() _surface_vehicle_speed_setpoint_pub.publish(surface_vehicle_speed_setpoint); surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; surface_vehicle_attitude_setpoint.timestamp = timestamp; - surface_vehicle_attitude_setpoint.yaw_setpoint = speed_setpoint > -FLT_EPSILON ? yaw_setpoint : matrix::wrap_pi( - yaw_setpoint + M_PI_F); + surface_vehicle_attitude_setpoint.yaw_setpoint = yaw_setpoint; _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); } else {