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 <blar.huang@gmail.com>
This commit is contained in:
blar authored and GitHub committed 2026-11-16 12:17:04 -05:00
1 parent 99a3048735
commit 7e4b18611a
6 files changed
+30 -19

No files matched your search

@@ -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);
}
@@ -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};
@@ -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 {
@@ -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);
}
@@ -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};
@@ -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 {