diff --git a/src/lib/motion_planning/PositionSmoothing.cpp b/src/lib/motion_planning/PositionSmoothing.cpp index f0ac796b1d9..80f048e73ab 100644 --- a/src/lib/motion_planning/PositionSmoothing.cpp +++ b/src/lib/motion_planning/PositionSmoothing.cpp @@ -220,17 +220,11 @@ const Vector3f PositionSmoothing::_generateVelocitySetpoint(const Vector3f &posi _max_speed_previous = xy_speed; } - // Compute XY and Z velocity components independently to avoid - // Z clamping from reducing XY speed (and vice versa) - const Vector3f pos_to_dest = crossing_point - pos_traj; - const Vector2f u_pos_to_dest_xy = Vector2f(pos_to_dest).unit_or_zero(); - const float z_sign = matrix::sign(pos_to_dest(2)); - - Vector3f vel_sp_constrained{ - u_pos_to_dest_xy(0) *xy_speed, - u_pos_to_dest_xy(1) *xy_speed, - z_sign *z_speed - }; + // Scale one 3D direction to fit both limits, so the slower axis limits the other + // one and a leg that changes altitude is flown as a straight line + Vector3f vel_sp_constrained = u_pos_traj_to_dest * sqrtf(xy_speed * xy_speed + z_speed * z_speed); + math::trajectory::clampToXYNorm(vel_sp_constrained, xy_speed, 0.5f); + math::trajectory::clampToZNorm(vel_sp_constrained, z_speed, 0.5f); for (int i = 0; i < 3; i++) { // If available, use the existing velocity as a feedforward, otherwise replace it diff --git a/src/lib/motion_planning/PositionSmoothingTest.cpp b/src/lib/motion_planning/PositionSmoothingTest.cpp index 79b255c5513..2263b61a81f 100644 --- a/src/lib/motion_planning/PositionSmoothingTest.cpp +++ b/src/lib/motion_planning/PositionSmoothingTest.cpp @@ -246,3 +246,72 @@ TEST_F(PositionSmoothingTest, doesNotDriftPastUnreachedWaypoint) // Without the fix the look-ahead marches down the extended leg and this grows unbounded. EXPECT_LT(max_distance_past_target, 10.f) << "Vehicle drifted too far past the unreached waypoint\n"; } + +class PositionSmoothingStraightLegTest : public ::testing::Test +{ +public: + // A descending leg: 100 m horizontal, 50 m down (NED), 6 m/s XY, 1.5 m/s Z + static constexpr float XY_SPEED = 6.f; + static constexpr float Z_SPEED = 1.5f; + const Vector3f START{0.f, 0.f, 0.f}; + const Vector3f TARGET{100.f, 0.f, 50.f}; + + PositionSmoothing _position_smoothing; + + PositionSmoothingStraightLegTest() + { + _position_smoothing.setMaxJerk(MAX_JERK); + _position_smoothing.setMaxAcceleration({MAX_ACCELERATION, MAX_ACCELERATION, MAX_ACCELERATION}); + _position_smoothing.setMaxVelocity({XY_SPEED, XY_SPEED, Z_SPEED}); + _position_smoothing.setMaxAllowedHorizontalError(MAX_ALLOWED_HOR_ERR); + _position_smoothing.setVerticalAcceptanceRadius(VERTICAL_ACCEPTANCE_RADIUS); + _position_smoothing.setCruiseSpeed(XY_SPEED); + _position_smoothing.setHorizontalTrajectoryGain(HORIZONTAL_TRAJECTORY_GAIN); + _position_smoothing.setTargetAcceptanceRadius(TARGET_ACCEPTANCE_RADIUS); + + _position_smoothing.reset({0.f, 0.f, 0.f}, {0.f, 0.f, 0.f}, START); + } + + PositionSmoothing::PositionSmoothingSetpoints firstSetpoint() + { + Vector3f waypoints[3] = {START, TARGET, TARGET}; + PositionSmoothing::PositionSmoothingSetpoints out; + _position_smoothing.generateSetpoints(START, waypoints, Vector3f{0.f, 0.f, 0.f}, 0.02f, false, out); + return out; + } + + // Fly the leg and return the largest vertical distance from the straight line + float flyLegMaxAltitudeError() + { + Vector3f waypoints[3] = {START, TARGET, TARGET}; + Vector3f position = START; + PositionSmoothing::PositionSmoothingSetpoints out; + const float slope = (TARGET(2) - START(2)) / (TARGET - START).xy().norm(); + float max_error = 0.f; + + for (int i = 0; i < 5000 && (TARGET - position).norm() > 0.1f; i++) { + _position_smoothing.generateSetpoints(position, waypoints, Vector3f{0.f, 0.f, 0.f}, 0.02f, false, out); + position = out.position; + const float z_on_line = START(2) + slope * (position - START).xy().norm(); + max_error = fmaxf(max_error, fabsf(position(2) - z_on_line)); + } + + EXPECT_LT((TARGET - position).norm(), 0.1f) << "Did not reach the target\n"; + return max_error; + } +}; + +TEST_F(PositionSmoothingStraightLegTest, velocityPointsAlongTheLeg) +{ + const Vector3f vel = firstSetpoint().unsmoothed_velocity; + + // Parallel to the leg: vz / |vxy| equals the leg slope, the slower Z axis limits XY + EXPECT_NEAR(vel(2) / vel.xy().norm(), 0.5f, 1e-3f); + EXPECT_NEAR(vel(2), Z_SPEED, 1e-3f); + EXPECT_NEAR(vel.xy().norm(), 2.f * Z_SPEED, 1e-3f); +} + +TEST_F(PositionSmoothingStraightLegTest, fliesAStraightLeg) +{ + EXPECT_LT(flyLegMaxAltitudeError(), 1.f); +} diff --git a/src/lib/motion_planning/TrajectoryConstraints.hpp b/src/lib/motion_planning/TrajectoryConstraints.hpp index 6deaadc949c..d84e9d2bdd1 100644 --- a/src/lib/motion_planning/TrajectoryConstraints.hpp +++ b/src/lib/motion_planning/TrajectoryConstraints.hpp @@ -124,5 +124,55 @@ float computeXYSpeedFromWaypoints(const Vector3f waypoints[N], const VehicleDyna return max_speed; } +/* + * Constrain the 3D vector given a maximum XY norm + * If the XY norm of the 3D vector is larger than the maximum norm, the whole vector + * is scaled down to respect the constraint. + * If the maximum norm is small (defined by the "accuracy" parameter), + * only the XY components are scaled down to avoid affecting + * Z in case of numerical issues + */ +inline void clampToXYNorm(Vector3f &target, float max_xy_norm, float accuracy = FLT_EPSILON) +{ + const float xynorm = target.xy().norm(); + const float scale_factor = (xynorm > FLT_EPSILON) + ? max_xy_norm / xynorm + : 1.f; + + if (scale_factor < 1.f) { + if (max_xy_norm < accuracy && xynorm < accuracy) { + target.xy() = Vector2f(target) * scale_factor; + + } else { + target *= scale_factor; + } + } +} + +/* + * Constrain the 3D vector given a maximum Z norm + * If the Z component of the 3D vector is larger than the maximum norm, the whole vector + * is scaled down to respect the constraint. + * If the maximum norm is small (defined by the "accuracy parameter), + * only the Z component is scaled down to avoid affecting + * XY in case of numerical issues + */ +inline void clampToZNorm(Vector3f &target, float max_z_norm, float accuracy = FLT_EPSILON) +{ + const float znorm = fabs(target(2)); + const float scale_factor = (znorm > FLT_EPSILON) + ? max_z_norm / znorm + : 1.f; + + if (scale_factor < 1.f) { + if (max_z_norm < accuracy && znorm < accuracy) { + target(2) *= scale_factor; + + } else { + target *= scale_factor; + } + } +} + } } diff --git a/src/lib/motion_planning/TrajectoryConstraintsTest.cpp b/src/lib/motion_planning/TrajectoryConstraintsTest.cpp index 842655619a9..f59302ddf2d 100644 --- a/src/lib/motion_planning/TrajectoryConstraintsTest.cpp +++ b/src/lib/motion_planning/TrajectoryConstraintsTest.cpp @@ -218,3 +218,147 @@ TEST_F(TrajectoryConstraintsTest, test10AngleCloseNext) EXPECT_LT(close_speed, normal_speed); } + +TEST(TrajectoryConstraintsClamp, clampToXYNormNoEffectLarge) +{ + // GIVEN: a short vector + Vector3f vec(1, 2, 3); + + // WHEN: we clamp it on XY with a long cutoff + clampToXYNorm(vec, 1000.f); + + // THEN: it shouldn't change + EXPECT_EQ(vec, Vector3f(1, 2, 3)); +} + +TEST(TrajectoryConstraintsClamp, clampToZNormNoEffect) +{ + // GIVEN: a short vector + Vector3f vec(1, 2, 3); + + // WHEN: we clamp it on XY with a long cutoff + clampToZNorm(vec, 1000.f); + + // THEN: it shouldn't change + EXPECT_EQ(vec, Vector3f(1, 2, 3)); +} + +TEST(TrajectoryConstraintsClamp, clampToXYNormNoEffectExact) +{ + // GIVEN: a vector + Vector3f vec(3, 4, 1); + + // WHEN: we clamp it on XY with exact cutoff + clampToXYNorm(vec, 5.f); + + // THEN: it shouldn't change + EXPECT_EQ(vec, Vector3f(3, 4, 1)); +} + +TEST(TrajectoryConstraintsClamp, clampToZNormNoEffectExact) +{ + // GIVEN: a vector + Vector3f vec(3, 4, -1); + + // WHEN: we clamp it on Z with exact cutoff + clampToZNorm(vec, 1.f); + + // THEN: it shouldn't change + EXPECT_EQ(vec, Vector3f(3, 4, -1)); +} + +TEST(TrajectoryConstraintsClamp, clampToXYNormHalf) +{ + // GIVEN: a vector + Vector3f vec(3, 4, 1); + + // WHEN: we clamp it on XY with half hypot length + clampToXYNorm(vec, 2.5f); + + // THEN: it should be half length + EXPECT_TRUE(vec == Vector3f(1.5f, 2.f, 0.5f)); +} + +TEST(TrajectoryConstraintsClamp, clampToZNormHalf) +{ + // GIVEN: a vector + Vector3f vec(3, 4, 10); + + // WHEN: we clamp it on Z with half length + clampToZNorm(vec, 5.f); + + // THEN: it should be half length + EXPECT_TRUE(vec == Vector3f(1.5f, 2.f, 5.f)); +} + +TEST(TrajectoryConstraintsClamp, clampToXYNormZero) +{ + // GIVEN: a vector + Vector3f vec(3, 4, 1); + + // WHEN: we clamp it on XY with half hypot length + clampToXYNorm(vec, 0.f); + + // THEN: it should be 0 + EXPECT_TRUE(vec == Vector3f(0.f, 0.f, 0.f)); +} + +TEST(TrajectoryConstraintsClamp, clampToZNormZero) +{ + // GIVEN: a vector + Vector3f vec(3, 4, 1); + + // WHEN: we clamp it on Z with half hypot length + clampToZNorm(vec, 0.f); + + // THEN: it should be 0 + EXPECT_TRUE(vec == Vector3f(0.f, 0.f, 0.f)); +} + +TEST(TrajectoryConstraintsClamp, clampToXYNormVecZero) +{ + // GIVEN: a vector + Vector3f vec(0, 0, 0); + + // WHEN: we clamp it on XY + clampToXYNorm(vec, 1.f); + + // THEN: it should be 0 still + EXPECT_TRUE(vec == Vector3f(0.f, 0.f, 0.f)); +} + +TEST(TrajectoryConstraintsClamp, clampToZNormVecZero) +{ + // GIVEN: a vector + Vector3f vec(0, 0, 0); + + // WHEN: we clamp it on Z + clampToZNorm(vec, 1.f); + + // THEN: it should be 0 still + EXPECT_TRUE(vec == Vector3f(0.f, 0.f, 0.f)); +} + +TEST(TrajectoryConstraintsClamp, clampToXYNormVecZeroToZero) +{ + // GIVEN: a vector + Vector3f vec(0, 0, 0); + + // WHEN: we clamp it on XY + clampToXYNorm(vec, 0.f); + + // THEN: it should be 0 still + EXPECT_TRUE(vec == Vector3f(0.f, 0.f, 0.f)); +} + +TEST(TrajectoryConstraintsClamp, clampToZNormVecZeroToZero) +{ + // GIVEN: a vector + Vector3f vec(0, 0, 0); + + // WHEN: we clamp it on XY + clampToZNorm(vec, 0.f); + + // THEN: it should be 0 still + EXPECT_TRUE(vec == Vector3f(0.f, 0.f, 0.f)); +}