feat(rover): compensate the drift angle in reverse

The course controllers each converted the course setpoint to a heading
setpoint on their own, and ackermann and differential rovers ignored the
drift angle in reverse because it was only estimated driving forwards.

calcHeadingSetpoint() is the inverse of the drift angle estimate, with the
expected direction of travel as an optional parameter, so one tested
function serves forwards, reverse and the mecanum thrust direction.
Ackermann and differential rovers now pass pi in reverse to both the
estimator and the helper, so the drift angle is estimated and compensated
in either direction of travel.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Michael Fritsche <frimi999@gmail.com>
This commit is contained in:
Michael Fritsche committed 2026-11-24 09:42:35 +01:00
1 parent 63db4326d7
commit 5cbf11ca93
9 files changed
+121 -41

No files matched your search

+1 -1
View File
@@ -100,7 +100,7 @@ The speed controller uses the following structure:
The feed forward mapping is done by interpolating the speed setpoint from [-[RO_MAX_THR_SPEED](../advanced_config/parameter_reference.md#RO_MAX_THR_SPEED), [RO_MAX_THR_SPEED](../advanced_config/parameter_reference.md#RO_MAX_THR_SPEED)] to [-1, 1].
The course controller turns the course setpoint and speed into speed and attitude setpoints.
For ackermann and differential rovers the heading is aligned with the course setpoint.
For ackermann and differential rovers the heading is aligned with the course setpoint (opposite to it when driving in reverse).
Therefore the course setpoint, corrected by the estimated [drift angle](../concept/navigation_terminology.md#drift-angle), is sent as a yaw setpoint to the [yaw controller](attitude_tuning.md#attitude-controller-structure-info-only) and the speed setpoint is always defined in body x direction.
For mecanum vehicles, the course setpoint and heading are decoupled.
@@ -34,6 +34,7 @@
#include "DriftAngleEstimator.hpp"
#include <mathlib/mathlib.h>
#include <matrix/math.hpp>
void DriftAngleEstimator::update(const vehicle_local_position_s &vehicle_local_position, const float expected_direction)
{
@@ -65,3 +66,8 @@ void DriftAngleEstimator::update(const vehicle_local_position_s &vehicle_local_p
_timestamp = vehicle_local_position.timestamp;
}
float calcHeadingSetpoint(const float course_setpoint, const float drift_angle, const float expected_direction)
{
return matrix::wrap_pi(course_setpoint - expected_direction - drift_angle);
}
@@ -81,3 +81,13 @@ private:
bool _valid{false};
hrt_abstime _timestamp{0};
};
/**
* Heading setpoint that moves the vehicle over ground along the course setpoint, the inverse of the drift angle estimate:
* course = heading + expected direction of travel + drift angle.
* @param course_setpoint [rad] [@frame NED] Intended direction of motion over ground
* @param drift_angle [rad] Drift angle relative to the expected direction of travel, see DriftAngleEstimator
* @param expected_direction [rad] [@frame Body] Expected direction of travel, 0 forwards, pi/2 right, pi backwards
* @return [rad] [@range -pi, pi] Heading setpoint
*/
float calcHeadingSetpoint(float course_setpoint, float drift_angle, float expected_direction = 0.f);
@@ -276,3 +276,57 @@ TEST(DriftAngleEstimator, reset)
// THEN: the drift angle is zero
EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f);
}
TEST(calcHeadingSetpoint, forwardsSubtractsDriftAngle)
{
// GIVEN: a course setpoint to the east and a drift angle of 0.1 rad, the course to the right of the heading
// WHEN: driving forwards
const float heading_setpoint = calcHeadingSetpoint(M_PI_2_F, 0.1f);
// THEN: the heading setpoint points left of the course setpoint by the drift angle
EXPECT_NEAR(heading_setpoint, M_PI_2_F - 0.1f, 1e-6f);
}
TEST(calcHeadingSetpoint, reversePointsAwayAndSubtractsDriftAngle)
{
// GIVEN: a course setpoint to the north east and a drift angle of 0.1 rad
// WHEN: driving in reverse
const float heading_setpoint = calcHeadingSetpoint(M_PI_4_F, 0.1f, M_PI_F);
// THEN: the heading setpoint points to the south west, corrected by the drift angle and wrapped into [-pi, pi]
EXPECT_NEAR(heading_setpoint, M_PI_4_F - M_PI_F - 0.1f, 1e-6f);
}
TEST(calcHeadingSetpoint, wrapsAcrossPi)
{
// GIVEN: a course setpoint to the south and a drift angle to the left
// WHEN: driving forwards
const float heading_setpoint = calcHeadingSetpoint(M_PI_F, -0.1f);
// THEN: the heading setpoint is wrapped into [-pi, pi]
EXPECT_NEAR(heading_setpoint, -M_PI_F + 0.1f, 1e-6f);
}
TEST(calcHeadingSetpoint, sidewaysDirectionOfTravel)
{
// GIVEN: a course setpoint to the north east and no drift
// WHEN: the vehicle is expected to move to its right
const float heading_setpoint = calcHeadingSetpoint(M_PI_4_F, 0.f, M_PI_2_F);
// THEN: the vehicle heads north west, so its right points along the course setpoint
EXPECT_NEAR(heading_setpoint, -M_PI_4_F, 1e-6f);
}
TEST(calcHeadingSetpoint, invertsDriftAngleEstimate)
{
// GIVEN: a drift angle estimated driving in reverse, heading north and moving south with a drift to the west
DriftAngleEstimator drift_angle_estimator;
drift_angle_estimator.update(localPosition(-2.f, -0.2f, 0.f, 1_s), M_PI_F);
const float course = atan2f(-0.2f, -2.f);
// WHEN: the heading setpoint is calculated to hold that course in reverse
const float heading_setpoint = calcHeadingSetpoint(course, drift_angle_estimator.getDriftAngle(), M_PI_F);
// THEN: it is the heading the drift angle was estimated at
EXPECT_NEAR(heading_setpoint, 0.f, 1e-5f);
}
@@ -66,21 +66,12 @@ void AckermannCourseControl::updateCourseControl()
const hrt_abstime timestamp = hrt_absolute_time();
float speed_setpoint = _speed_setpoint;
// Without a speed setpoint the course is held with whatever thrust is commanded, the thrust sign gives the direction of travel
const bool reverse = PX4_ISFINITE(speed_setpoint) ? speed_setpoint < -FLT_EPSILON : _thrust_setpoint_x < -FLT_EPSILON;
float yaw_setpoint = _vehicle_yaw; // No course: keep the current heading
if (PX4_ISFINITE(_course_setpoint)) {
if (reverse) {
// Driving in reverse, the rover points away from its direction of travel.
// The drift angle is only estimated driving forwards, so it is not compensated here.
yaw_setpoint = matrix::wrap_pi(_course_setpoint + M_PI_F);
} else {
// Driving forwards, the heading setpoint is the course setpoint corrected by the drift angle, like the fixed wing crabs into the wind
yaw_setpoint = matrix::wrap_pi(_course_setpoint - _drift_angle_estimator.getDriftAngle());
}
// The heading setpoint is the course setpoint corrected by the drift angle, like the fixed wing crabs into the wind.
// In reverse the rover points away from its direction of travel.
yaw_setpoint = calcHeadingSetpoint(_course_setpoint, _drift_angle_estimator.getDriftAngle(), expectedDirection());
}
surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{};
@@ -116,12 +107,6 @@ void AckermannCourseControl::updateSubscriptions()
_vehicle_yaw = matrix::Eulerf(matrix::Quatf(vehicle_attitude.q)).psi();
}
if (_vehicle_local_position_sub.updated()) {
vehicle_local_position_s vehicle_local_position{};
_vehicle_local_position_sub.copy(&vehicle_local_position);
_drift_angle_estimator.update(vehicle_local_position);
}
if (_surface_vehicle_thrust_setpoint_sub.updated()) {
surface_vehicle_thrust_setpoint_s surface_vehicle_thrust_setpoint{};
_surface_vehicle_thrust_setpoint_sub.copy(&surface_vehicle_thrust_setpoint);
@@ -134,6 +119,20 @@ void AckermannCourseControl::updateSubscriptions()
_course_setpoint = surface_vehicle_course_setpoint.course;
_speed_setpoint = surface_vehicle_course_setpoint.speed;
}
// After the setpoints, so the drift angle is estimated for the current direction of travel
if (_vehicle_local_position_sub.updated()) {
vehicle_local_position_s vehicle_local_position{};
_vehicle_local_position_sub.copy(&vehicle_local_position);
_drift_angle_estimator.update(vehicle_local_position, expectedDirection());
}
}
float AckermannCourseControl::expectedDirection() const
{
// Without a speed setpoint the course is held with whatever thrust is commanded, the thrust sign gives the direction of travel
const bool reverse = PX4_ISFINITE(_speed_setpoint) ? _speed_setpoint < -FLT_EPSILON : _thrust_setpoint_x < -FLT_EPSILON;
return reverse ? M_PI_F : 0.f;
}
bool AckermannCourseControl::runSanityChecks()
@@ -57,7 +57,7 @@
* @brief Class for ackermann course control.
*
* Sits between the position controller and the attitude and speed controllers: turns a course over ground into a heading
* setpoint (drift compensation forwards, pointing away from the course in reverse) and passes the speed on, reduced
* setpoint (drift compensation, pointing away from the course in reverse) and passes the speed on, reduced
* for large heading errors. Without a speed setpoint it commands the heading for the thrust
* commanded elsewhere and deactivates the speed controller (NaN speed setpoint).
* The last valid course setpoint is controlled until a NaN course is received. With a NaN course and a speed the current
@@ -103,6 +103,12 @@ private:
*/
void updateSubscriptions();
/**
* @brief Expected direction of travel in body frame, from the sign of the speed setpoint, or of the thrust without one.
* @return [rad] pi in reverse, 0 otherwise
*/
float expectedDirection() const;
// uORB subscriptions
uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)};
uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)};
@@ -64,21 +64,12 @@ void DifferentialCourseControl::updateCourseControl()
const hrt_abstime timestamp = hrt_absolute_time();
float speed_setpoint = _speed_setpoint;
// Without a speed setpoint the course is held with whatever thrust is commanded, the thrust sign gives the direction of travel
const bool reverse = PX4_ISFINITE(speed_setpoint) ? speed_setpoint < -FLT_EPSILON : _thrust_setpoint_x < -FLT_EPSILON;
float yaw_setpoint = _vehicle_yaw; // No course: keep the current heading
if (PX4_ISFINITE(_course_setpoint)) {
if (reverse) {
// Driving in reverse, the rover points away from its direction of travel.
// The drift angle is only estimated driving forwards, so it is not compensated here.
yaw_setpoint = matrix::wrap_pi(_course_setpoint + M_PI_F);
} else {
// Driving forwards, the heading setpoint is the course setpoint corrected by the drift angle, like the fixed wing crabs into the wind
yaw_setpoint = matrix::wrap_pi(_course_setpoint - _drift_angle_estimator.getDriftAngle());
}
// The heading setpoint is the course setpoint corrected by the drift angle, like the fixed wing crabs into the wind.
// In reverse the rover points away from its direction of travel.
yaw_setpoint = calcHeadingSetpoint(_course_setpoint, _drift_angle_estimator.getDriftAngle(), expectedDirection());
const float heading_error = fabsf(matrix::wrap_pi(yaw_setpoint - _vehicle_yaw));
@@ -128,12 +119,6 @@ void DifferentialCourseControl::updateSubscriptions()
_vehicle_yaw = matrix::Eulerf(matrix::Quatf(vehicle_attitude.q)).psi();
}
if (_vehicle_local_position_sub.updated()) {
vehicle_local_position_s vehicle_local_position{};
_vehicle_local_position_sub.copy(&vehicle_local_position);
_drift_angle_estimator.update(vehicle_local_position);
}
if (_surface_vehicle_thrust_setpoint_sub.updated()) {
surface_vehicle_thrust_setpoint_s surface_vehicle_thrust_setpoint{};
_surface_vehicle_thrust_setpoint_sub.copy(&surface_vehicle_thrust_setpoint);
@@ -146,6 +131,20 @@ void DifferentialCourseControl::updateSubscriptions()
_course_setpoint = surface_vehicle_course_setpoint.course;
_speed_setpoint = surface_vehicle_course_setpoint.speed;
}
// After the setpoints, so the drift angle is estimated for the current direction of travel
if (_vehicle_local_position_sub.updated()) {
vehicle_local_position_s vehicle_local_position{};
_vehicle_local_position_sub.copy(&vehicle_local_position);
_drift_angle_estimator.update(vehicle_local_position, expectedDirection());
}
}
float DifferentialCourseControl::expectedDirection() const
{
// Without a speed setpoint the course is held with whatever thrust is commanded, the thrust sign gives the direction of travel
const bool reverse = PX4_ISFINITE(_speed_setpoint) ? _speed_setpoint < -FLT_EPSILON : _thrust_setpoint_x < -FLT_EPSILON;
return reverse ? M_PI_F : 0.f;
}
bool DifferentialCourseControl::runSanityChecks()
@@ -65,7 +65,7 @@ enum class DrivingState {
* @brief Class for differential course control.
*
* Sits between the position controller and the attitude and speed controllers: turns a course over ground into a heading
* setpoint (drift compensation forwards, pointing away from the course in reverse) and passes the speed on, reduced
* setpoint (drift compensation, pointing away from the course in reverse) and passes the speed on, reduced
* for large heading errors and zero while turning on the spot. Without a speed setpoint it commands the heading for the thrust
* commanded elsewhere and deactivates the speed controller (NaN speed setpoint). The last valid course setpoint is controlled until a NaN course is received.
*/
@@ -109,6 +109,12 @@ private:
*/
void updateSubscriptions();
/**
* @brief Expected direction of travel in body frame, from the sign of the speed setpoint, or of the thrust without one.
* @return [rad] pi in reverse, 0 otherwise
*/
float expectedDirection() const;
// uORB subscriptions
uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)};
uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)};
@@ -79,8 +79,8 @@ void MecanumCourseControl::updateCourseControl()
// The drift angle is estimated relative to the thrust direction, so it compensates the slip in any direction of travel.
surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{};
surface_vehicle_attitude_setpoint.timestamp = timestamp;
surface_vehicle_attitude_setpoint.yaw_setpoint = matrix::wrap_pi(_course_setpoint - _thrust_direction
- _drift_angle_estimator.getDriftAngle());
surface_vehicle_attitude_setpoint.yaw_setpoint = calcHeadingSetpoint(_course_setpoint,
_drift_angle_estimator.getDriftAngle(), _thrust_direction);
_surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint);
// NaN deactivates the speed controller, the thrust then comes from elsewhere