diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 1c4296baa3a..ce145ec3d01 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -50,6 +50,7 @@ add_subdirectory(crc EXCLUDE_FROM_ALL) add_subdirectory(crypto EXCLUDE_FROM_ALL) add_subdirectory(dataman_client EXCLUDE_FROM_ALL) add_subdirectory(dijkstra EXCLUDE_FROM_ALL) +add_subdirectory(drift_angle_estimator EXCLUDE_FROM_ALL) add_subdirectory(drivers EXCLUDE_FROM_ALL) add_subdirectory(failure_injection EXCLUDE_FROM_ALL) add_subdirectory(field_sensor_bias_estimator EXCLUDE_FROM_ALL) diff --git a/src/lib/drift_angle_estimator/CMakeLists.txt b/src/lib/drift_angle_estimator/CMakeLists.txt new file mode 100644 index 00000000000..ba58ed99ca1 --- /dev/null +++ b/src/lib/drift_angle_estimator/CMakeLists.txt @@ -0,0 +1,39 @@ +############################################################################ +# +# Copyright (c) 2026 PX4 Development Team. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name PX4 nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +px4_add_library(drift_angle_estimator + DriftAngleEstimator.cpp + DriftAngleEstimator.hpp +) + +px4_add_unit_gtest(SRC DriftAngleEstimatorTest.cpp LINKLIBS drift_angle_estimator) diff --git a/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp b/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp new file mode 100644 index 00000000000..8df367a5e1e --- /dev/null +++ b/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include "DriftAngleEstimator.hpp" + +#include + +void DriftAngleEstimator::update(const vehicle_local_position_s &vehicle_local_position, const float expected_direction) +{ + 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); + + // 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)) { + return; + } + + const float drift_angle = math::constrain(atan2f(speed_across, speed_along), -MAX_DRIFT_ANGLE, MAX_DRIFT_ANGLE); + + if (!_valid) { + _filter.reset(drift_angle); + _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)); + } + + _timestamp = vehicle_local_position.timestamp; +} diff --git a/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp b/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp new file mode 100644 index 00000000000..83c21702409 --- /dev/null +++ b/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp @@ -0,0 +1,83 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file DriftAngleEstimator.hpp + * + * Estimates the drift angle of a surface vehicle from its ground velocity. + */ + +#pragma once + +#include +#include +#include + +/** + * Estimates the drift angle, the angle from the expected direction of travel to the course over ground. + * The expected direction of travel is given in body frame, forwards by default. An omnidirectional vehicle passes + * 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. + */ +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, + * 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 + */ + void update(const vehicle_local_position_s &vehicle_local_position, float expected_direction = 0.f); + + /** + * @return Drift angle [rad], 0 until estimated since the last reset + */ + float getDriftAngle() const { return _valid ? _filter.getState() : 0.f; } + + 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 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 + +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}; + 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 new file mode 100644 index 00000000000..d4c56a26f65 --- /dev/null +++ b/src/lib/drift_angle_estimator/DriftAngleEstimatorTest.cpp @@ -0,0 +1,278 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * Test code for the DriftAngleEstimator + * Run this test only using "make tests TESTFILTER=DriftAngleEstimator" + */ + +#include +#include "DriftAngleEstimator.hpp" + +using namespace time_literals; + +static vehicle_local_position_s localPosition(const float vx, const float vy, const float heading, + const hrt_abstime timestamp) +{ + vehicle_local_position_s vehicle_local_position{}; + vehicle_local_position.timestamp = timestamp; + vehicle_local_position.v_xy_valid = true; + vehicle_local_position.vx = vx; + vehicle_local_position.vy = vy; + vehicle_local_position.heading = heading; + vehicle_local_position.evh = 0.1f; + return vehicle_local_position; +} + +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(); + + // THEN: the drift angle is zero + EXPECT_FLOAT_EQ(drift_angle, 0.f); +} + +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 + 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); +} + +TEST(DriftAngleEstimator, ignoresReverse) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the rover drives backwards + 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); +} + +TEST(DriftAngleEstimator, ignoresInvalidVelocity) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the horizontal velocity is flagged invalid + vehicle_local_position_s no_velocity = localPosition(2.f, 0.5f, 0.f, 1_s); + no_velocity.v_xy_valid = false; + drift_angle_estimator.update(no_velocity); + + // THEN: the drift angle stays zero + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); +} + +TEST(DriftAngleEstimator, ignoresNanHeading) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the heading is NaN + 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); +} + +TEST(DriftAngleEstimator, ignoresUncertainVelocity) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the speed is below the velocity uncertainty + vehicle_local_position_s uncertain = localPosition(2.f, 0.5f, 0.f, 1_s); + uncertain.evh = 1.f; + 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); +} + +TEST(DriftAngleEstimator, ignoresNanExpectedDirection) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the expected direction of travel is NaN + 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); +} + +TEST(DriftAngleEstimator, ignoresMovementAcrossExpectedDirection) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // WHEN: the vehicle is expected to move to its right but moves forwards + 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); +} + +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 + 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); +} + +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 + 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); +} + +TEST(DriftAngleEstimator, noDriftAlongDiagonalExpectedDirection) +{ + // GIVEN: a new drift angle estimator + DriftAngleEstimator drift_angle_estimator; + + // 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); +} + +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); + hrt_abstime timestamp = 1_s; + + // WHEN: a new drift 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)); +} + +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); + hrt_abstime timestamp = 1_s; + + // WHEN: a new drift 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); +} + +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); + + // WHEN: it is reset + drift_angle_estimator.reset(); + + // THEN: the drift angle is zero + EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); +} diff --git a/src/lib/rover_control/RoverControl.cpp b/src/lib/rover_control/RoverControl.cpp index 063eeef19a1..fe1cb612637 100644 --- a/src/lib/rover_control/RoverControl.cpp +++ b/src/lib/rover_control/RoverControl.cpp @@ -308,33 +308,4 @@ float calcWaypointTransitionAngle(Vector2f &prev_wp_ned, Vector2f &curr_wp_ned, return acosf(cosin); } -void DriftAngleEstimator::update(const vehicle_local_position_s &vehicle_local_position) -{ - if (!vehicle_local_position.v_xy_valid || !PX4_ISFINITE(vehicle_local_position.heading)) { - return; - } - - const float heading = vehicle_local_position.heading; - const float forward_speed = vehicle_local_position.vx * cosf(heading) + vehicle_local_position.vy * sinf(heading); - const float lateral_speed = -vehicle_local_position.vx * sinf(heading) + vehicle_local_position.vy * cosf(heading); - - // The direction of a slow or backwards velocity says nothing about the drift angle while driving along the course - if (forward_speed < math::max(MIN_SPEED, EVH_FACTOR * vehicle_local_position.evh)) { - return; - } - - const float drift_angle = math::constrain(atan2f(lateral_speed, forward_speed), -MAX_DRIFT_ANGLE, MAX_DRIFT_ANGLE); - - if (!_valid) { - _filter.reset(drift_angle); - _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)); - } - - _timestamp = vehicle_local_position.timestamp; -} - } // RoverControl diff --git a/src/lib/rover_control/RoverControl.hpp b/src/lib/rover_control/RoverControl.hpp index 435863126cb..9937d52003f 100644 --- a/src/lib/rover_control/RoverControl.hpp +++ b/src/lib/rover_control/RoverControl.hpp @@ -42,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -167,39 +166,4 @@ void globalToLocalSetpointTriplet(Vector2f &curr_wp_ned, Vector2f &prev_wp_ned, */ float calcWaypointTransitionAngle(Vector2f &prev_wp_ned, Vector2f &curr_wp_ned, Vector2f &next_wp_ned); -/** - * Estimates the drift angle, the angle from the heading to the course over ground, while driving forwards. - * Course control subtracts it 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. - */ -class DriftAngleEstimator -{ -public: - /** - * Update the estimate from the angle between the ground velocity and the heading. - * Only updated while driving forwards fast enough for the velocity direction to be meaningful, kept otherwise. - * @param vehicle_local_position Local position with the velocity and heading estimate - */ - void update(const vehicle_local_position_s &vehicle_local_position); - - /** - * @return Drift angle [rad], 0 until estimated since the last reset - */ - float getDriftAngle() const { return _valid ? _filter.getState() : 0.f; } - - void reset() { _valid = false; } - - static constexpr float MIN_SPEED{.5f}; ///< [m/s] Forward speed below which the velocity direction is noise - static constexpr float EVH_FACTOR{3.f}; ///< Factor of the velocity standard deviation the forward speed has to exceed - static constexpr float MAX_DRIFT_ANGLE{M_PI_F / 4.f}; ///< [rad] Limits the heading offset from the course - -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}; - bool _valid{false}; - hrt_abstime _timestamp{0}; -}; - } diff --git a/src/lib/rover_control/RoverControlTest.cpp b/src/lib/rover_control/RoverControlTest.cpp index 3d86a2aef4e..3de357f2f44 100644 --- a/src/lib/rover_control/RoverControlTest.cpp +++ b/src/lib/rover_control/RoverControlTest.cpp @@ -186,182 +186,6 @@ TEST(calcWaypointTransitionAngle, nextEqualsCurrent) EXPECT_FALSE(PX4_ISFINITE(angle)); } -static vehicle_local_position_s localPosition(const float vx, const float vy, const float heading, - const hrt_abstime timestamp) -{ - vehicle_local_position_s vehicle_local_position{}; - vehicle_local_position.timestamp = timestamp; - vehicle_local_position.v_xy_valid = true; - vehicle_local_position.vx = vx; - vehicle_local_position.vy = vy; - vehicle_local_position.heading = heading; - vehicle_local_position.evh = 0.1f; - return vehicle_local_position; -} - -TEST(DriftAngleEstimator, zeroBeforeFirstUpdate) -{ - // GIVEN: a new drift angle estimator - RoverControl::DriftAngleEstimator drift_angle_estimator; - - // WHEN: no update has happened yet - const float drift_angle = drift_angle_estimator.getDriftAngle(); - - // THEN: the drift angle is zero - EXPECT_FLOAT_EQ(drift_angle, 0.f); -} - -TEST(DriftAngleEstimator, ignoresLowSpeed) -{ - // GIVEN: a new drift angle estimator - RoverControl::DriftAngleEstimator drift_angle_estimator; - - // WHEN: the rover moves too slowly for the velocity direction to say anything about the drift - 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); -} - -TEST(DriftAngleEstimator, ignoresReverse) -{ - // GIVEN: a new drift angle estimator - RoverControl::DriftAngleEstimator drift_angle_estimator; - - // WHEN: the rover drives backwards - 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); -} - -TEST(DriftAngleEstimator, ignoresInvalidVelocity) -{ - // GIVEN: a new drift angle estimator - RoverControl::DriftAngleEstimator drift_angle_estimator; - - // WHEN: the horizontal velocity is flagged invalid - vehicle_local_position_s no_velocity = localPosition(2.f, 0.5f, 0.f, 1_s); - no_velocity.v_xy_valid = false; - drift_angle_estimator.update(no_velocity); - - // THEN: the drift angle stays zero - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); -} - -TEST(DriftAngleEstimator, ignoresNanHeading) -{ - // GIVEN: a new drift angle estimator - RoverControl::DriftAngleEstimator drift_angle_estimator; - - // WHEN: the heading is NaN - 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); -} - -TEST(DriftAngleEstimator, ignoresUncertainVelocity) -{ - // GIVEN: a new drift angle estimator - RoverControl::DriftAngleEstimator drift_angle_estimator; - - // WHEN: the forward speed is below the velocity uncertainty - vehicle_local_position_s uncertain = localPosition(2.f, 0.5f, 0.f, 1_s); - uncertain.evh = 1.f; - 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 - RoverControl::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 - RoverControl::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 - RoverControl::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(), RoverControl::DriftAngleEstimator::MAX_DRIFT_ANGLE); -} - -TEST(DriftAngleEstimator, approachesNewDrift) -{ - // GIVEN: a drift angle estimator that has seen the rover drive straight - RoverControl::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); - hrt_abstime timestamp = 1_s; - - // WHEN: a new drift 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)); -} - -TEST(DriftAngleEstimator, convergesToNewDrift) -{ - // GIVEN: a drift angle estimator that has seen the rover drive straight - RoverControl::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); - hrt_abstime timestamp = 1_s; - - // WHEN: a new drift 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); -} - -TEST(DriftAngleEstimator, reset) -{ - // GIVEN: a drift angle estimator with a non-zero drift angle - RoverControl::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); - - // WHEN: it is reset - drift_angle_estimator.reset(); - - // THEN: the drift angle is zero - EXPECT_FLOAT_EQ(drift_angle_estimator.getDriftAngle(), 0.f); -} - // Sticks of a mode 2 transmitter, each at a different value so a swapped axis is caught: // throttle = left vertical, yaw = left horizontal, pitch = right vertical, roll = right horizontal static manual_control_setpoint_s distinctSticks() diff --git a/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp index 896e8767095..2cdd8d2d7b0 100644 --- a/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp +++ b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp @@ -39,6 +39,7 @@ // Library includes #include #include +#include #include #include @@ -113,7 +114,7 @@ private: uORB::Publication _surface_vehicle_speed_setpoint_pub{ORB_ID(surface_vehicle_speed_setpoint)}; // Variables - RoverControl::DriftAngleEstimator _drift_angle_estimator{}; + DriftAngleEstimator _drift_angle_estimator{}; float _course_setpoint{NAN}; // Last received course setpoint, controlled until a NaN course is received float _speed_setpoint{NAN}; // Last received speed setpoint, passed on if finite float _vehicle_yaw{0.f}; diff --git a/src/modules/rover_ackermann/AckermannCourseControl/CMakeLists.txt b/src/modules/rover_ackermann/AckermannCourseControl/CMakeLists.txt index 30cc4f89fe4..a15172cd3f7 100644 --- a/src/modules/rover_ackermann/AckermannCourseControl/CMakeLists.txt +++ b/src/modules/rover_ackermann/AckermannCourseControl/CMakeLists.txt @@ -35,5 +35,5 @@ px4_add_library(AckermannCourseControl AckermannCourseControl.cpp ) -target_link_libraries(AckermannCourseControl PUBLIC rover_control) +target_link_libraries(AckermannCourseControl PUBLIC rover_control drift_angle_estimator) target_include_directories(AckermannCourseControl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/modules/rover_differential/DifferentialCourseControl/CMakeLists.txt b/src/modules/rover_differential/DifferentialCourseControl/CMakeLists.txt index ab76f56fe0a..6bf8be205a5 100644 --- a/src/modules/rover_differential/DifferentialCourseControl/CMakeLists.txt +++ b/src/modules/rover_differential/DifferentialCourseControl/CMakeLists.txt @@ -35,5 +35,5 @@ px4_add_library(DifferentialCourseControl DifferentialCourseControl.cpp ) -target_link_libraries(DifferentialCourseControl PUBLIC rover_control) +target_link_libraries(DifferentialCourseControl PUBLIC rover_control drift_angle_estimator) target_include_directories(DifferentialCourseControl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp index f8464d3fded..7c438bbb23e 100644 --- a/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp +++ b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp @@ -39,6 +39,7 @@ // Library includes #include #include +#include #include #include @@ -119,7 +120,7 @@ private: uORB::Publication _surface_vehicle_speed_setpoint_pub{ORB_ID(surface_vehicle_speed_setpoint)}; // Variables - RoverControl::DriftAngleEstimator _drift_angle_estimator{}; + DriftAngleEstimator _drift_angle_estimator{}; DrivingState _current_state{DrivingState::DRIVING}; float _course_setpoint{NAN}; // Last received course setpoint, controlled until a NaN course is received float _speed_setpoint{NAN}; // Last received speed setpoint, passed on if finite diff --git a/src/modules/rover_mecanum/MecanumCourseControl/CMakeLists.txt b/src/modules/rover_mecanum/MecanumCourseControl/CMakeLists.txt index 77149a5908b..d75a866be56 100644 --- a/src/modules/rover_mecanum/MecanumCourseControl/CMakeLists.txt +++ b/src/modules/rover_mecanum/MecanumCourseControl/CMakeLists.txt @@ -35,5 +35,5 @@ px4_add_library(MecanumCourseControl MecanumCourseControl.cpp ) -target_link_libraries(MecanumCourseControl PUBLIC rover_control) +target_link_libraries(MecanumCourseControl PUBLIC rover_control drift_angle_estimator) target_include_directories(MecanumCourseControl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp index 3b55a8eea06..95a5bf81531 100644 --- a/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp +++ b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp @@ -76,9 +76,11 @@ void MecanumCourseControl::updateCourseControl() // No speed setpoint: the course is held with whatever thrust is commanded. // The heading is turned so that the commanded thrust direction in body frame points along the course, // e.g. thrust to the right drives along the course with the course on the left of the heading. + // 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); + surface_vehicle_attitude_setpoint.yaw_setpoint = matrix::wrap_pi(_course_setpoint - _thrust_direction + - _drift_angle_estimator.getDriftAngle()); _surface_vehicle_attitude_setpoint_pub.publish(surface_vehicle_attitude_setpoint); // NaN deactivates the speed controller, the thrust then comes from elsewhere @@ -114,6 +116,12 @@ void MecanumCourseControl::updateSubscriptions() } } + 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, _thrust_direction); + } + if (_surface_vehicle_course_setpoint_sub.updated()) { surface_vehicle_course_setpoint_s surface_vehicle_course_setpoint{}; _surface_vehicle_course_setpoint_sub.copy(&surface_vehicle_course_setpoint); diff --git a/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.hpp b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.hpp index 9afcc61ba0d..cc1fe843de4 100644 --- a/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.hpp +++ b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.hpp @@ -39,6 +39,7 @@ // Library includes #include #include +#include #include #include @@ -59,8 +60,8 @@ using namespace matrix; * * Sits between the position controller and the speed and attitude controllers. With a speed it turns the course over * ground into a body frame velocity setpoint, independent of the heading. Without a speed it turns the heading so that - * the direction of the thrust commanded elsewhere (body x and y) points along the course, and deactivates the speed - * controller (NaN speed setpoint). The last valid course setpoint is controlled until a + * the direction of the thrust commanded elsewhere (body x and y) points along the course, corrected by the drift angle + * relative to that thrust direction, 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 the speed is passed on along the body x axis, with NaN course and speed * the course controller stops publishing. */ @@ -88,7 +89,7 @@ public: /** * @brief Reset course controller. */ - void reset() { _course_setpoint = NAN; _speed_setpoint = NAN; _thrust_direction = 0.f; } + void reset() { _course_setpoint = NAN; _speed_setpoint = NAN; _thrust_direction = 0.f; _drift_angle_estimator.reset(); } protected: /** @@ -106,6 +107,7 @@ private: // uORB subscriptions uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; + uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)}; uORB::Subscription _surface_vehicle_course_setpoint_sub{ORB_ID(surface_vehicle_course_setpoint)}; uORB::Subscription _surface_vehicle_thrust_setpoint_sub{ORB_ID(surface_vehicle_thrust_setpoint)}; @@ -118,4 +120,5 @@ private: float _course_setpoint{NAN}; // Last received course setpoint, controlled until a NaN course is received float _speed_setpoint{NAN}; // Last received speed setpoint float _thrust_direction{0.f}; // [rad] Direction of the last nonzero thrust setpoint in body frame, 0 forwards, pi/2 right + DriftAngleEstimator _drift_angle_estimator{}; // Drift angle relative to the thrust direction };