fix(motion_planning): plan trajectories before integrating them

This commit is contained in:
Matthias Grob committed 2026-11-12 19:56:01 +01:00
1 parent 65bf937b88
commit 85228f2111
8 files changed
+99 -66

No files matched your search

@@ -56,26 +56,16 @@ void ManualVelocitySmoothingXY::resetPositionLock()
void ManualVelocitySmoothingXY::update(float dt, const Vector2f &velocity_target)
{
// Update state
updateTrajectories(dt);
// Lock or unlock position
// Has to be done before _updateTrajDurations()
checkPositionLock(velocity_target);
// Update durations and sync XY
updateTrajDurations(velocity_target);
}
void ManualVelocitySmoothingXY::updateTrajectories(float dt)
{
// Integrate along the plan
for (int i = 0; i < 2; ++i) {
_trajectory[i].updateTraj(dt);
_state.j(i) = _trajectory[i].getCurrentJerk();
_state.a(i) = _trajectory[i].getCurrentAcceleration();
_state.v(i) = _trajectory[i].getCurrentVelocity();
_state.x(i) = _trajectory[i].getCurrentPosition();
}
}
@@ -98,12 +88,12 @@ void ManualVelocitySmoothingXY::checkPositionLock(const Vector2f &velocity_targe
* This is why the previous input of the velocity controller
* is used to set current velocity of the trajectory.
*/
if (_state.v.length() < 0.1f &&
_state.a.length() < .2f &&
if (getCurrentVelocity().length() < 0.1f &&
getCurrentAcceleration().length() < .2f &&
velocity_target.length() <= FLT_EPSILON) {
// Lock position
_position_lock_active = true;
_position_setpoint_locked = _state.x;
_position_setpoint_locked = Vector2f(_trajectory[0].getCurrentPosition(), _trajectory[1].getCurrentPosition());
} else {
// Unlock position
@@ -111,7 +101,6 @@ void ManualVelocitySmoothingXY::checkPositionLock(const Vector2f &velocity_targe
// Start the trajectory at the current velocity setpoint
_trajectory[0].setCurrentVelocity(_velocity_setpoint_feedback(0));
_trajectory[1].setCurrentVelocity(_velocity_setpoint_feedback(1));
_state.v = _velocity_setpoint_feedback;
resetPositionLock();
}
@@ -76,20 +76,18 @@ public:
}
float getMaxVel() const { return _trajectory[0].getMaxVel(); }
Vector2f getCurrentJerk() const { return _state.j; }
Vector2f getCurrentAcceleration() const { return _state.a; }
Vector2f getCurrentJerk() const { return {_trajectory[0].getCurrentJerk(), _trajectory[1].getCurrentJerk()}; }
Vector2f getCurrentAcceleration() const { return {_trajectory[0].getCurrentAcceleration(), _trajectory[1].getCurrentAcceleration()}; }
void setCurrentVelocity(const Vector2f &vel)
{
_state.v = vel;
_trajectory[0].setCurrentVelocity(vel(0));
_trajectory[1].setCurrentVelocity(vel(1));
}
Vector2f getCurrentVelocity() const { return _state.v; }
Vector2f getCurrentVelocity() const { return {_trajectory[0].getCurrentVelocity(), _trajectory[1].getCurrentVelocity()}; }
void setCurrentPosition(const Vector2f &pos)
{
_state.x = pos;
_trajectory[0].setCurrentPosition(pos(0));
_trajectory[1].setCurrentPosition(pos(1));
_position_estimate = pos;
@@ -104,7 +102,6 @@ public:
private:
void resetPositionLock();
void updateTrajectories(float dt);
void checkPositionLock(const Vector2f &velocity_target);
void updateTrajDurations(const Vector2f &velocity_target);
@@ -116,11 +113,4 @@ private:
Vector2f _velocity_setpoint_feedback{};
Vector2f _position_estimate{};
struct {
Vector2f j;
Vector2f a;
Vector2f v;
Vector2f x;
} _state{};
};
@@ -85,3 +85,20 @@ TEST_F(ManualVelocitySmoothingXYTest, getCurrentState)
EXPECT_EQ(v_end, Vector3f(11.f, 13.f, 0.f));
EXPECT_EQ(x_end, Vector3f(0.f, 0.f, 0.f));
}
TEST_F(ManualVelocitySmoothingXYTest, positionLockAfterReset)
{
// GIVEN: A standing still trajectory reset away from the origin
_smoothing.setMaxJerk(4.f);
_smoothing.setMaxAccel(3.f);
_smoothing.setMaxVel(5.f);
const Vector2f position(10.f, -5.f);
_smoothing.reset(Vector2f(), Vector2f(), position);
// WHEN: It's updated without any velocity target
_smoothing.update(0.02f, Vector2f());
// THEN: The position gets locked where it was reset to
EXPECT_FLOAT_EQ(_smoothing.getCurrentPosition()(0), position(0));
EXPECT_FLOAT_EQ(_smoothing.getCurrentPosition()(1), position(1));
}
@@ -51,9 +51,6 @@ void ManualVelocitySmoothingZ::resetPositionLock()
void ManualVelocitySmoothingZ::update(float dt, float velocity_target)
{
// Update state
updateTrajectories(dt);
// Set max accel/vel/jerk
// Has to be done before _updateTrajDurations()
updateTrajConstraints(velocity_target);
@@ -64,16 +61,9 @@ void ManualVelocitySmoothingZ::update(float dt, float velocity_target)
// Update durations
_trajectory.updateDurations(velocity_target);
}
void ManualVelocitySmoothingZ::updateTrajectories(float dt)
{
// Integrate along the plan
_trajectory.updateTraj(dt);
_state.j = _trajectory.getCurrentJerk();
_state.a = _trajectory.getCurrentAcceleration();
_state.v = _trajectory.getCurrentVelocity();
_state.x = _trajectory.getCurrentPosition();
}
void ManualVelocitySmoothingZ::updateTrajConstraints(float velocity_target)
@@ -98,19 +88,18 @@ void ManualVelocitySmoothingZ::checkPositionLock(float velocity_target)
* This is why the previous input of the velocity controller
* is used to set current velocity of the trajectory.
*/
if (fabsf(_state.v) < 0.1f &&
fabsf(_state.a) < .2f &&
if (fabsf(getCurrentVelocity()) < 0.1f &&
fabsf(getCurrentAcceleration()) < .2f &&
fabsf(velocity_target) <= FLT_EPSILON) {
// Lock position
_position_lock_active = true;
_position_setpoint_locked = _state.x;
_position_setpoint_locked = _trajectory.getCurrentPosition();
} else {
// Unlock position
if (_position_lock_active) {
// Start the trajectory at the current velocity setpoint
_trajectory.setCurrentVelocity(_velocity_setpoint_feedback);
_state.v = _velocity_setpoint_feedback;
resetPositionLock();
}
@@ -72,17 +72,12 @@ public:
_max_vel_down = max_vel_down;
}
float getCurrentJerk() const { return _state.j; }
float getCurrentAcceleration() const { return _state.a; }
void setCurrentVelocity(const float vel)
{
_state.v = vel;
_trajectory.setCurrentVelocity(vel);
}
float getCurrentVelocity() const { return _state.v; }
float getCurrentJerk() const { return _trajectory.getCurrentJerk(); }
float getCurrentAcceleration() const { return _trajectory.getCurrentAcceleration(); }
void setCurrentVelocity(const float vel) { _trajectory.setCurrentVelocity(vel); }
float getCurrentVelocity() const { return _trajectory.getCurrentVelocity(); }
void setCurrentPosition(const float pos)
{
_state.x = pos;
_trajectory.setCurrentPosition(pos);
_position_estimate = pos;
@@ -95,7 +90,6 @@ public:
private:
void resetPositionLock();
void updateTrajectories(float dt);
void updateTrajConstraints(float vel_target);
void checkPositionLock(float velocity_target);
void updateTrajDurations(float velocity_target);
@@ -109,13 +103,6 @@ private:
float _velocity_setpoint_feedback{};
float _position_estimate{};
struct {
float j;
float a;
float v;
float x;
} _state{};
float _max_accel_up{0.f};
float _max_accel_down{0.f};
float _max_vel_up{0.f};
@@ -337,6 +337,13 @@ void PositionSmoothing::_generateTrajectory(
time_stretch_z = 1.f - math::constrain(fabsf(drone_to_traj_z) / _max_allowed_vertical_error, 0.f, 1.f);
}
// Plan from the current state before integrating along the plan
for (int i = 0; i < 3; ++i) {
_trajectory[i].updateDurations(velocity_setpoint(i));
}
VelocitySmoothing::timeSynchronization(_trajectory, 3);
// Axis 0,1 (XY) get the horizontal stretch; axis 2 (Z) gets the vertical stretch.
const float axis_stretch[3] = { time_stretch_xy, time_stretch_xy, time_stretch_z };
@@ -347,10 +354,4 @@ void PositionSmoothing::_generateTrajectory(
out_setpoints.velocity(i) = _trajectory[i].getCurrentVelocity();
out_setpoints.position(i) = _trajectory[i].getCurrentPosition();
}
for (int i = 0; i < 3; ++i) {
_trajectory[i].updateDurations(velocity_setpoint(i));
}
VelocitySmoothing::timeSynchronization(_trajectory, 3);
}
@@ -315,3 +315,32 @@ TEST_F(PositionSmoothingStraightLegTest, fliesAStraightLeg)
{
EXPECT_LT(flyLegMaxAltitudeError(), 1.f);
}
TEST_F(PositionSmoothingTest, resetContinuesAcceleration)
{
// GIVEN: A trajectory reset while braking horizontally and climbing
const Vector3f acceleration(-2.5f, 0.f, 0.f);
const Vector3f velocity(2.f, 0.f, -1.f);
const Vector3f position(0.f, 0.f, -2.f);
_position_smoothing.reset(acceleration, velocity, position);
// WHEN: Only an altitude is targeted and the horizontal velocity has to go to zero
const float dt = 0.02f;
PositionSmoothing::PositionSmoothingSetpoints out;
_position_smoothing.generateSetpoints(position, Vector3f(NAN, NAN, -10.f), Vector3f(0.f, 0.f, NAN), dt, false,
out);
// THEN: The braking continues instead of restarting from zero acceleration
EXPECT_NEAR(out.acceleration(0), acceleration(0), MAX_JERK * dt + 1e-4f);
EXPECT_NEAR(out.velocity(0), velocity(0) + acceleration(0) * dt, MAX_JERK * dt * dt);
// AND WHEN: It continues
for (int i = 0; i < 10; i++) {
const Vector3f previous_acceleration = out.acceleration;
_position_smoothing.generateSetpoints(out.position, Vector3f(NAN, NAN, -10.f), Vector3f(0.f, 0.f, NAN), dt,
false, out);
// THEN: The acceleration only changes within the jerk limit
EXPECT_LE(fabsf(out.acceleration(0) - previous_acceleration(0)), MAX_JERK * dt + 1e-4f);
}
}
@@ -60,6 +60,37 @@ TEST(VelocitySmoothingBasicTest, AllZeroCase)
EXPECT_FLOAT_EQ(trajectory.getCurrentPosition(), 0.f);
}
TEST(VelocitySmoothingBasicTest, ResetContinuesAcceleration)
{
// GIVEN: A trajectory that was planned before and is then reset to an accelerating state
VelocitySmoothing trajectory;
trajectory.setMaxJerk(4.f);
trajectory.setMaxAccel(3.f);
trajectory.setMaxVel(12.f);
trajectory.updateDurations(5.f);
trajectory.updateTraj(0.5f);
trajectory.reset(-2.f, 1.f, 0.f);
// WHEN: We plan towards a still standing vehicle and integrate one step
const float dt = 0.02f;
trajectory.updateDurations(0.f);
trajectory.updateTraj(dt);
// THEN: The acceleration only changed within the jerk limit
EXPECT_NEAR(trajectory.getCurrentAcceleration(), -2.f, 4.f * dt + 1e-5f);
EXPECT_NEAR(trajectory.getCurrentVelocity(), 1.f - 2.f * dt, 4.f * dt * dt);
// AND WHEN: It runs to the end
for (int i = 0; i < 200; i++) {
trajectory.updateDurations(0.f);
trajectory.updateTraj(dt);
}
// THEN: It ends up without acceleration
EXPECT_NEAR(trajectory.getCurrentAcceleration(), 0.f, 1e-3f);
EXPECT_NEAR(trajectory.getCurrentVelocity(), 0.f, 1e-3f);
}
TEST(VelocitySmoothingBasicTest, DivisionByZeroComputeT1)
{
// GIVEN: A try to trigger division by zero in computeT1()