diff --git a/docs/en/config_rover/velocity_tuning.md b/docs/en/config_rover/velocity_tuning.md index 0160c5f63d6..b6d8c7b4721 100644 --- a/docs/en/config_rover/velocity_tuning.md +++ b/docs/en/config_rover/velocity_tuning.md @@ -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. diff --git a/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp b/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp index 8df367a5e1e..1cec7b72003 100644 --- a/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp +++ b/src/lib/drift_angle_estimator/DriftAngleEstimator.cpp @@ -34,6 +34,7 @@ #include "DriftAngleEstimator.hpp" #include +#include 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); +} diff --git a/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp b/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp index 83c21702409..e9dbf96533c 100644 --- a/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp +++ b/src/lib/drift_angle_estimator/DriftAngleEstimator.hpp @@ -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); diff --git a/src/lib/drift_angle_estimator/DriftAngleEstimatorTest.cpp b/src/lib/drift_angle_estimator/DriftAngleEstimatorTest.cpp index d4c56a26f65..00025a8902c 100644 --- a/src/lib/drift_angle_estimator/DriftAngleEstimatorTest.cpp +++ b/src/lib/drift_angle_estimator/DriftAngleEstimatorTest.cpp @@ -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); +} diff --git a/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp index d4ed34c424b..66a87e66b2c 100644 --- a/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp +++ b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.cpp @@ -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() diff --git a/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp index 2cdd8d2d7b0..7a1234c9d5a 100644 --- a/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp +++ b/src/modules/rover_ackermann/AckermannCourseControl/AckermannCourseControl.hpp @@ -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)}; diff --git a/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp index e2a9d531a10..7f2ccacc47f 100644 --- a/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp +++ b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.cpp @@ -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() diff --git a/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp index 7c438bbb23e..cb2a49b1bc0 100644 --- a/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp +++ b/src/modules/rover_differential/DifferentialCourseControl/DifferentialCourseControl.hpp @@ -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)}; diff --git a/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp index 95a5bf81531..e45f31ac313 100644 --- a/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp +++ b/src/modules/rover_mecanum/MecanumCourseControl/MecanumCourseControl.cpp @@ -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