diff --git a/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp b/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp index 1cec7b72003..957b102cc2b 100644 --- a/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp +++ b/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp @@ -36,37 +36,66 @@ #include #include -void DriftAngleEstimator::update(const vehicle_local_position_s &vehicle_local_position, const float expected_direction) +void DriftAngleEstimator::update(const vehicle_local_position_s &vehicle_local_position, const float expected_direction, + const float speed_setpoint) { if (!vehicle_local_position.v_xy_valid || !PX4_ISFINITE(vehicle_local_position.heading) || !PX4_ISFINITE(expected_direction)) { return; } - // Ground velocity along and across the expected direction of travel const float direction = vehicle_local_position.heading + expected_direction; - const float speed_along = vehicle_local_position.vx * cosf(direction) + vehicle_local_position.vy * sinf(direction); - const float speed_across = -vehicle_local_position.vx * sinf(direction) + vehicle_local_position.vy * cosf(direction); + const matrix::Vector2f unit_direction{cosf(direction), sinf(direction)}; + const matrix::Vector2f ground_velocity{vehicle_local_position.vx, vehicle_local_position.vy}; + const float measured_speed = ground_velocity.dot(unit_direction); - // The direction of a slow velocity, or one against the expected direction, says nothing about the drift angle - if (speed_along < math::max(MIN_SPEED, EVH_FACTOR * vehicle_local_position.evh)) { + // The velocity of a slow vehicle, or of one moving against the expected direction, is dominated by noise and + // maneuvering, not by the current + if (measured_speed < math::max(MIN_SPEED, EVH_FACTOR * vehicle_local_position.evh)) { return; } - const float drift_angle = math::constrain(atan2f(speed_across, speed_along), -MAX_DRIFT_ANGLE, MAX_DRIFT_ANGLE); + // Without a speed setpoint all of the speed along the expected direction is attributed to the propulsion + const float commanded_speed = PX4_ISFINITE(speed_setpoint) ? fabsf(speed_setpoint) : measured_speed; + + if (commanded_speed < MIN_SPEED) { + return; + } + + const matrix::Vector2f current_velocity = ground_velocity - unit_direction * commanded_speed; if (!_valid) { - _filter.reset(drift_angle); + _current_velocity_filter.reset(current_velocity); _valid = true; } else { // After a pause in the estimation the stale estimate is filtered towards the new one rather than replaced - _filter.update(drift_angle, math::min(vehicle_local_position.timestamp - _timestamp, MAX_DT_US)); + _current_velocity_filter.update(current_velocity, + math::min(vehicle_local_position.timestamp - _timestamp, MAX_DT_US)); } + _measured_speed = measured_speed; _timestamp = vehicle_local_position.timestamp; } +float DriftAngleEstimator::getDriftAngle(const float course_setpoint, const float speed_setpoint) const +{ + const float speed = PX4_ISFINITE(speed_setpoint) ? fabsf(speed_setpoint) : _measured_speed; + + if (!_valid || !PX4_ISFINITE(course_setpoint) || speed < FLT_EPSILON) { + return 0.f; + } + + const matrix::Vector2f current_velocity = _current_velocity_filter.getState(); + // Positive when the current sets the vehicle to the right of the course setpoint + const float current_across = -current_velocity(0) * sinf(course_setpoint) + current_velocity(1) * cosf( + course_setpoint); + // speed * sin(drift_angle) has to cancel the current across the course. Limiting the ratio limits the angle and + // keeps asinf in its domain when the current is faster than the vehicle. + const float max_ratio = sinf(MAX_DRIFT_ANGLE); + return asinf(math::constrain(current_across / speed, -max_ratio, max_ratio)); +} + float calcHeadingSetpoint(const float course_setpoint, const float drift_angle, const float expected_direction) { return matrix::wrap_pi(course_setpoint - expected_direction - drift_angle); diff --git a/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp b/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp index e9dbf96533c..1da10744448 100644 --- a/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp +++ b/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp @@ -41,6 +41,7 @@ #include #include +#include #include /** @@ -49,27 +50,40 @@ * the direction it pushes in, e.g. its thrust direction. * Course control subtracts the drift angle from the course to get the heading setpoint, like the fixed wing corrects * its course setpoint for the wind, so the vehicle moves over ground along the course despite wheel slip or current. + * + * Internally the current of the wind and current triangle is estimated, the part of the ground velocity that the + * commanded speed along the expected direction does not explain (current, wind or wheel slip). It is fixed in NED + * while the vehicle turns, unlike the drift angle, so the drift angle is solved from it for each course setpoint. */ class DriftAngleEstimator { public: /** - * Update the estimate from the angle between the ground velocity and the expected direction of travel. - * Only updated while moving along the expected direction fast enough for the velocity direction to be meaningful, + * Update the current estimate from the ground velocity minus the commanded velocity. + * Only updated while moving along the expected direction fast enough for the velocity to be meaningful, * kept otherwise. * @param vehicle_local_position Local position with the velocity and heading estimate * @param expected_direction [rad] [@frame Body] Expected direction of travel, 0 forwards, pi/2 right, pi backwards + * @param speed_setpoint [m/s] Commanded speed, only its magnitude is used as the speed along the expected direction. + * NaN if the speed is not controlled: the measured speed along the expected direction is used instead, so only + * the current across the expected direction is estimated. */ - void update(const vehicle_local_position_s &vehicle_local_position, float expected_direction = 0.f); + void update(const vehicle_local_position_s &vehicle_local_position, float expected_direction = 0.f, + float speed_setpoint = NAN); /** - * @return Drift angle [rad], 0 until estimated since the last reset + * Solve the wind and current triangle: the offset of the direction of travel whose velocity across the course + * setpoint cancels the current across it. + * @param course_setpoint [rad] [@frame NED] Intended direction of motion over ground + * @param speed_setpoint [m/s] Commanded speed, only its magnitude is used. NaN if the speed is not controlled: + * the speed measured at the last update is used instead. + * @return Drift angle [rad], 0 until estimated since the last reset, without a course setpoint or at standstill */ - float getDriftAngle() const { return _valid ? _filter.getState() : 0.f; } + float getDriftAngle(float course_setpoint, float speed_setpoint = NAN) const; void reset() { _valid = false; } - static constexpr float MIN_SPEED{.5f}; ///< [m/s] Speed along the expected direction below which the velocity direction is noise + static constexpr float MIN_SPEED{.5f}; ///< [m/s] Speed along the expected direction, measured and commanded, below which nothing is estimated static constexpr float EVH_FACTOR{3.f}; ///< Factor of the velocity standard deviation the speed has to exceed static constexpr float MAX_DRIFT_ANGLE{M_PI_F / 4.f}; ///< [rad] Limits the heading offset from the course @@ -77,7 +91,9 @@ private: static constexpr uint64_t TIME_CONSTANT_US{1000000}; ///< Filters the yaw oscillations of the vehicle static constexpr uint64_t MAX_DT_US{100000}; ///< Sample interval limit, as the estimate pauses at low speed - AlphaFilter _filter{TIME_CONSTANT_US}; + // Filtered as a vector in NED: filtering its direction and speed separately wraps at +-pi and biases the speed + AlphaFilter _current_velocity_filter{TIME_CONSTANT_US}; + float _measured_speed{0.f}; ///< [m/s] Speed along the expected direction at the last update, for an uncontrolled speed bool _valid{false}; hrt_abstime _timestamp{0}; }; diff --git a/src/lib/drift_angle_estimator/DriftAngleEstimatorTest.cpp b/src/lib/drift_angle_estimator/DriftAngleEstimatorTest.cpp index 00025a8902c..f5e8f9f09eb 100644 --- a/src/lib/drift_angle_estimator/DriftAngleEstimatorTest.cpp +++ b/src/lib/drift_angle_estimator/DriftAngleEstimatorTest.cpp @@ -54,13 +54,17 @@ static vehicle_local_position_s localPosition(const float vx, const float vy, co return vehicle_local_position; } +// Speed setpoint passed to getDriftAngle() in the tests that expect no estimate, so a zero drift angle shows that +// nothing was estimated rather than that no speed was known +static constexpr float SPEED_SETPOINT{2.f}; + TEST(DriftAngleEstimator, zeroBeforeFirstUpdate) { // GIVEN: a new drift angle estimator DriftAngleEstimator drift_angle_estimator; // WHEN: no update has happened yet - const float drift_angle = drift_angle_estimator.getDriftAngle(); + const float drift_angle = drift_angle_estimator.getDriftAngle(0.f, SPEED_SETPOINT); // THEN: the drift angle is zero EXPECT_FLOAT_EQ(drift_angle, 0.f); @@ -71,11 +75,23 @@ TEST(DriftAngleEstimator, ignoresLowSpeed) // GIVEN: a new drift angle estimator DriftAngleEstimator drift_angle_estimator; - // WHEN: the rover moves too slowly for the velocity direction to say anything about the drift + // WHEN: the rover moves too slowly for the velocity to say anything about the current drift_angle_estimator.update(localPosition(0.3f, 0.1f, 0.f, 1_s)); // THEN: the drift angle stays zero - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f, SPEED_SETPOINT), 0.f); +} + +TEST(DriftAngleEstimator, ignoresLowSpeedSetpoint) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the rover moves fast but only a low speed is commanded + drift_angle_estimator.update(localPosition(2.f, 0.2f, 0.f, 1_s), 0.f, 0.3f); + + // THEN: the drift angle stays zero + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f, SPEED_SETPOINT), 0.f); } TEST(DriftAngleEstimator, ignoresReverse) @@ -87,7 +103,7 @@ TEST(DriftAngleEstimator, ignoresReverse) drift_angle_estimator.update(localPosition(-2.f, 0.5f, 0.f, 1_s)); // THEN: the drift angle stays zero - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f, SPEED_SETPOINT), 0.f); } TEST(DriftAngleEstimator, ignoresInvalidVelocity) @@ -101,7 +117,7 @@ TEST(DriftAngleEstimator, ignoresInvalidVelocity) drift_angle_estimator.update(no_velocity); // THEN: the drift angle stays zero - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f, SPEED_SETPOINT), 0.f); } TEST(DriftAngleEstimator, ignoresNanHeading) @@ -113,7 +129,7 @@ TEST(DriftAngleEstimator, ignoresNanHeading) drift_angle_estimator.update(localPosition(2.f, 0.5f, NAN, 1_s)); // THEN: the drift angle stays zero - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f, SPEED_SETPOINT), 0.f); } TEST(DriftAngleEstimator, ignoresUncertainVelocity) @@ -127,43 +143,7 @@ TEST(DriftAngleEstimator, ignoresUncertainVelocity) drift_angle_estimator.update(uncertain); // THEN: the drift angle stays zero - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); -} - -TEST(DriftAngleEstimator, driftToTheRightHeadingNorth) -{ - // GIVEN: a new drift angle estimator - DriftAngleEstimator drift_angle_estimator; - - // WHEN: the rover heads north and moves north with a drift to the east - drift_angle_estimator.update(localPosition(2.f, 0.2f, 0.f, 1_s)); - - // THEN: the drift angle is the angle from the heading to the course, positive since the course is to the right - EXPECT_NEAR(drift_angle_estimator.getDriftAngle(), atan2f(0.2f, 2.f), 1e-5f); -} - -TEST(DriftAngleEstimator, driftToTheRightHeadingEast) -{ - // GIVEN: a new drift angle estimator - DriftAngleEstimator drift_angle_estimator; - - // WHEN: the rover heads east with the same drift relative to its heading - drift_angle_estimator.update(localPosition(-0.2f, 2.f, M_PI_2_F, 1_s)); - - // THEN: the drift angle is the same as when heading north - EXPECT_NEAR(drift_angle_estimator.getDriftAngle(), atan2f(0.2f, 2.f), 1e-5f); -} - -TEST(DriftAngleEstimator, limited) -{ - // GIVEN: a new drift angle estimator - DriftAngleEstimator drift_angle_estimator; - - // WHEN: the rover moves mostly sideways - drift_angle_estimator.update(localPosition(1.f, 5.f, 0.f, 1_s)); - - // THEN: the drift angle is limited - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), DriftAngleEstimator::MAX_DRIFT_ANGLE); + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f, SPEED_SETPOINT), 0.f); } TEST(DriftAngleEstimator, ignoresNanExpectedDirection) @@ -175,7 +155,7 @@ TEST(DriftAngleEstimator, ignoresNanExpectedDirection) drift_angle_estimator.update(localPosition(2.f, 0.5f, 0.f, 1_s), NAN); // THEN: the drift angle stays zero - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f, SPEED_SETPOINT), 0.f); } TEST(DriftAngleEstimator, ignoresMovementAcrossExpectedDirection) @@ -187,7 +167,70 @@ TEST(DriftAngleEstimator, ignoresMovementAcrossExpectedDirection) drift_angle_estimator.update(localPosition(2.f, 0.f, 0.f, 1_s), M_PI_2_F); // THEN: the drift angle stays zero - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f, SPEED_SETPOINT), 0.f); +} + +TEST(DriftAngleEstimator, zeroWithoutCourseSetpoint) +{ + // GIVEN: a drift angle estimator that has seen a current to the east + DriftAngleEstimator drift_angle_estimator; + drift_angle_estimator.update(localPosition(2.f, 0.2f, 0.f, 1_s), 0.f, 2.f); + + // WHEN: the drift angle is requested without a course setpoint + const float drift_angle = drift_angle_estimator.getDriftAngle(NAN, 2.f); + + // THEN: the drift angle is zero + EXPECT_FLOAT_EQ(drift_angle, 0.f); +} + +TEST(DriftAngleEstimator, zeroAtStandstill) +{ + // GIVEN: a drift angle estimator that has seen a current to the east + DriftAngleEstimator drift_angle_estimator; + drift_angle_estimator.update(localPosition(2.f, 0.2f, 0.f, 1_s), 0.f, 2.f); + + // WHEN: the drift angle is requested for a zero speed setpoint + const float drift_angle = drift_angle_estimator.getDriftAngle(0.f, 0.f); + + // THEN: the drift angle is zero rather than limited + EXPECT_FLOAT_EQ(drift_angle, 0.f); +} + +TEST(DriftAngleEstimator, driftToTheRightHeadingNorth) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the rover heads north at 2 m/s and moves north with a current of 0.2 m/s to the east + drift_angle_estimator.update(localPosition(2.f, 0.2f, 0.f, 1_s)); + + // THEN: holding a course to the north needs a drift angle whose velocity across the course cancels the current, + // positive since the current sets the rover to the right + EXPECT_NEAR(drift_angle_estimator.getDriftAngle(0.f), asinf(0.2f / 2.f), 1e-5f); +} + +TEST(DriftAngleEstimator, driftToTheRightHeadingEast) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the rover heads east with the same current relative to its heading + drift_angle_estimator.update(localPosition(-0.2f, 2.f, M_PI_2_F, 1_s)); + + // THEN: holding a course to the east needs the same drift angle as holding north when heading north + EXPECT_NEAR(drift_angle_estimator.getDriftAngle(M_PI_2_F), asinf(0.2f / 2.f), 1e-5f); +} + +TEST(DriftAngleEstimator, limited) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the rover moves mostly sideways + drift_angle_estimator.update(localPosition(1.f, 5.f, 0.f, 1_s)); + + // THEN: the drift angle is limited + EXPECT_NEAR(drift_angle_estimator.getDriftAngle(0.f), DriftAngleEstimator::MAX_DRIFT_ANGLE, 1e-6f); } TEST(DriftAngleEstimator, driftToTheRightExpectedRight) @@ -195,11 +238,11 @@ TEST(DriftAngleEstimator, driftToTheRightExpectedRight) // GIVEN: a new drift angle estimator DriftAngleEstimator drift_angle_estimator; - // WHEN: the vehicle heads north, is expected to move to its right (east) and moves east with a drift to the south + // WHEN: the vehicle heads north, is expected to move to its right (east) and moves east with a current to the south drift_angle_estimator.update(localPosition(-0.2f, 2.f, 0.f, 1_s), M_PI_2_F); - // THEN: the drift angle is the angle from the expected direction to the course, positive since the course is to the right - EXPECT_NEAR(drift_angle_estimator.getDriftAngle(), atan2f(0.2f, 2.f), 1e-5f); + // THEN: holding a course to the east needs a positive drift angle since the current sets it to the right + EXPECT_NEAR(drift_angle_estimator.getDriftAngle(M_PI_2_F), asinf(0.2f / 2.f), 1e-5f); } TEST(DriftAngleEstimator, driftToTheRightExpectedBackwards) @@ -207,11 +250,11 @@ TEST(DriftAngleEstimator, driftToTheRightExpectedBackwards) // GIVEN: a new drift angle estimator DriftAngleEstimator drift_angle_estimator; - // WHEN: the vehicle heads north, is expected to move backwards (south) and moves south with a drift to the west + // WHEN: the vehicle heads north, is expected to move backwards (south) and moves south with a current to the west drift_angle_estimator.update(localPosition(-2.f, -0.2f, 0.f, 1_s), M_PI_F); - // THEN: the drift angle is positive since the course is to the right of the expected direction - EXPECT_NEAR(drift_angle_estimator.getDriftAngle(), atan2f(0.2f, 2.f), 1e-5f); + // THEN: holding a course to the south needs a positive drift angle since the current sets it to the right + EXPECT_NEAR(drift_angle_estimator.getDriftAngle(M_PI_F), asinf(0.2f / 2.f), 1e-5f); } TEST(DriftAngleEstimator, noDriftAlongDiagonalExpectedDirection) @@ -222,8 +265,60 @@ TEST(DriftAngleEstimator, noDriftAlongDiagonalExpectedDirection) // WHEN: the vehicle heads east, is expected to move forwards left (north east) and moves exactly north east drift_angle_estimator.update(localPosition(2.f, 2.f, M_PI_2_F, 1_s), -M_PI_4_F); - // THEN: there is no drift - EXPECT_NEAR(drift_angle_estimator.getDriftAngle(), 0.f, 1e-5f); + // THEN: there is no drift angle for a course to the north east + EXPECT_NEAR(drift_angle_estimator.getDriftAngle(M_PI_4_F), 0.f, 1e-5f); +} + +TEST(DriftAngleEstimator, currentStaysFixedWhenTheCourseChanges) +{ + // GIVEN: a drift angle estimator that has seen a current of 0.4 m/s to the east while heading north at 2 m/s + DriftAngleEstimator drift_angle_estimator; + drift_angle_estimator.update(localPosition(2.f, 0.4f, 0.f, 1_s), 0.f, 2.f); + + // WHEN: the course setpoint turns to the east, along the current + const float drift_angle = drift_angle_estimator.getDriftAngle(M_PI_2_F, 2.f); + + // THEN: no drift angle is needed, as the current no longer sets the vehicle off the course + EXPECT_NEAR(drift_angle, 0.f, 1e-5f); +} + +TEST(DriftAngleEstimator, currentAlongTheDirectionOfTravel) +{ + // GIVEN: a drift angle estimator that has seen the rover move north at 2.5 m/s while 2 m/s were commanded, + // so a current of 0.5 m/s to the north + DriftAngleEstimator drift_angle_estimator; + drift_angle_estimator.update(localPosition(2.5f, 0.f, 0.f, 1_s), 0.f, 2.f); + + // WHEN: the course setpoint turns to the east + const float drift_angle = drift_angle_estimator.getDriftAngle(M_PI_2_F, 2.f); + + // THEN: the drift angle cancels the current, which now sets the vehicle to the left of the course + EXPECT_NEAR(drift_angle, asinf(-0.5f / 2.f), 1e-5f); +} + +TEST(DriftAngleEstimator, fasterSpeedSetpointNeedsSmallerDriftAngle) +{ + // GIVEN: a drift angle estimator that has seen a current of 0.4 m/s to the east at 2 m/s + DriftAngleEstimator drift_angle_estimator; + drift_angle_estimator.update(localPosition(2.f, 0.4f, 0.f, 1_s), 0.f, 2.f); + + // WHEN: a speed of 4 m/s is commanded along a course to the north + const float drift_angle = drift_angle_estimator.getDriftAngle(0.f, 4.f); + + // THEN: the faster vehicle needs a smaller drift angle to cancel the same current + EXPECT_NEAR(drift_angle, asinf(0.4f / 4.f), 1e-5f); +} + +TEST(DriftAngleEstimator, reverseSpeedSetpointUsesItsMagnitude) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the vehicle heads north and reverses south at a commanded 2 m/s with a current of 0.2 m/s to the west + drift_angle_estimator.update(localPosition(-2.f, -0.2f, 0.f, 1_s), M_PI_F, -2.f); + + // THEN: holding a course to the south needs a positive drift angle since the current sets it to the right + EXPECT_NEAR(drift_angle_estimator.getDriftAngle(M_PI_F, -2.f), asinf(0.2f / 2.f), 1e-5f); } TEST(DriftAngleEstimator, approachesNewDrift) @@ -231,18 +326,18 @@ TEST(DriftAngleEstimator, approachesNewDrift) // GIVEN: a drift angle estimator that has seen the rover drive straight DriftAngleEstimator drift_angle_estimator; drift_angle_estimator.update(localPosition(2.f, 0.f, 0.f, 1_s)); - ASSERT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); + ASSERT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f), 0.f); hrt_abstime timestamp = 1_s; - // WHEN: a new drift appears for 0.1 s + // WHEN: a new current appears for 0.1 s for (int i = 0; i < 10; i++) { timestamp += 10_ms; drift_angle_estimator.update(localPosition(2.f, 0.4f, 0.f, timestamp)); } - // THEN: the drift angle moves towards the new drift without jumping to it - EXPECT_GT(drift_angle_estimator.getDriftAngle(), 0.f); - EXPECT_LT(drift_angle_estimator.getDriftAngle(), atan2f(0.4f, 2.f)); + // THEN: the drift angle moves towards the new current without jumping to it + EXPECT_GT(drift_angle_estimator.getDriftAngle(0.f), 0.f); + EXPECT_LT(drift_angle_estimator.getDriftAngle(0.f), asinf(0.4f / 2.f)); } TEST(DriftAngleEstimator, convergesToNewDrift) @@ -250,17 +345,17 @@ TEST(DriftAngleEstimator, convergesToNewDrift) // GIVEN: a drift angle estimator that has seen the rover drive straight DriftAngleEstimator drift_angle_estimator; drift_angle_estimator.update(localPosition(2.f, 0.f, 0.f, 1_s)); - ASSERT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); + ASSERT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f), 0.f); hrt_abstime timestamp = 1_s; - // WHEN: a new drift persists for 10 s + // WHEN: a new current persists for 10 s for (int i = 0; i < 1000; i++) { timestamp += 10_ms; drift_angle_estimator.update(localPosition(2.f, 0.4f, 0.f, timestamp)); } - // THEN: the drift angle has converged to the new drift - EXPECT_NEAR(drift_angle_estimator.getDriftAngle(), atan2f(0.4f, 2.f), 1e-3f); + // THEN: the drift angle has converged to the one that cancels the new current + EXPECT_NEAR(drift_angle_estimator.getDriftAngle(0.f), asinf(0.4f / 2.f), 1e-3f); } TEST(DriftAngleEstimator, reset) @@ -268,13 +363,13 @@ TEST(DriftAngleEstimator, reset) // GIVEN: a drift angle estimator with a non-zero drift angle DriftAngleEstimator drift_angle_estimator; drift_angle_estimator.update(localPosition(2.f, 0.2f, 0.f, 1_s)); - ASSERT_GT(drift_angle_estimator.getDriftAngle(), 0.f); + ASSERT_GT(drift_angle_estimator.getDriftAngle(0.f), 0.f); // WHEN: it is reset drift_angle_estimator.reset(); // THEN: the drift angle is zero - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(0.f, SPEED_SETPOINT), 0.f); } TEST(calcHeadingSetpoint, forwardsSubtractsDriftAngle) @@ -325,7 +420,7 @@ TEST(calcHeadingSetpoint, invertsDriftAngleEstimate) 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); + const float heading_setpoint = calcHeadingSetpoint(course, drift_angle_estimator.getDriftAngle(course), M_PI_F); // THEN: it is the heading the drift angle was estimated at EXPECT_NEAR(heading_setpoint, 0.f, 1e-5f); diff --git a/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp index 66a87e66b2c..bd13d7bc775 100644 --- a/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp +++ b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp @@ -71,7 +71,8 @@ void AckermannCourseControl::updateCourseControl() if (PX4_ISFINITE(_course_setpoint)) { // 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()); + yaw_setpoint = calcHeadingSetpoint(_course_setpoint, _drift_angle_estimator.getDriftAngle(_course_setpoint, + _speed_setpoint), expectedDirection()); } surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; @@ -120,11 +121,11 @@ void AckermannCourseControl::updateSubscriptions() _speed_setpoint = surface_vehicle_course_setpoint.speed; } - // After the setpoints, so the drift angle is estimated for the current direction of travel + // After the setpoints, so the estimate uses the direction of travel and the speed commanded now 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()); + _drift_angle_estimator.update(vehicle_local_position, expectedDirection(), _speed_setpoint); } } diff --git a/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp index 7f2ccacc47f..3aaa6a4ff0c 100644 --- a/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp +++ b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp @@ -69,7 +69,8 @@ void DifferentialCourseControl::updateCourseControl() if (PX4_ISFINITE(_course_setpoint)) { // 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()); + yaw_setpoint = calcHeadingSetpoint(_course_setpoint, _drift_angle_estimator.getDriftAngle(_course_setpoint, + _speed_setpoint), expectedDirection()); const float heading_error = fabsf(matrix::wrap_pi(yaw_setpoint - _vehicle_yaw)); @@ -132,11 +133,11 @@ void DifferentialCourseControl::updateSubscriptions() _speed_setpoint = surface_vehicle_course_setpoint.speed; } - // After the setpoints, so the drift angle is estimated for the current direction of travel + // After the setpoints, so the estimate uses the direction of travel and the speed commanded now 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()); + _drift_angle_estimator.update(vehicle_local_position, expectedDirection(), _speed_setpoint); } } diff --git a/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp index e45f31ac313..433bbe7bbba 100644 --- a/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp +++ b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp @@ -80,7 +80,7 @@ void MecanumCourseControl::updateCourseControl() surface_vehicle_attitude_setpoint_s surface_vehicle_attitude_setpoint{}; surface_vehicle_attitude_setpoint.timestamp = timestamp; surface_vehicle_attitude_setpoint.yaw_setpoint = calcHeadingSetpoint(_course_setpoint, - _drift_angle_estimator.getDriftAngle(), _thrust_direction); + _drift_angle_estimator.getDriftAngle(_course_setpoint), _thrust_direction); _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); // NaN deactivates the speed controller, the thrust then comes from elsewhere