Skip to content

Commit fcfa4aa

Browse files
committed
fix(flight_mode_manager): fix terrain following position setpoint
In MPC_ALT_MODE=1 terrain following, the smoothing block was overwriting the parent class's terrain-adjusted position setpoint. Unify terrain hold and terrain following into a single terrain_position flag that syncs the smoothing block to the parent's position, preventing divergence and simplifying transition handling.
1 parent 550b714 commit fcfa4aa

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/modules/flight_mode_manager/tasks/ManualAltitudeSmoothVel/FlightTaskManualAltitudeSmoothVel.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,23 @@ void FlightTaskManualAltitudeSmoothVel::_setOutputState()
107107
_acceleration_setpoint(2) = _smoothing.getCurrentAcceleration();
108108
_velocity_setpoint(2) = _smoothing.getCurrentVelocity();
109109

110-
if (!_terrain_hold) {
111-
if (_terrain_hold_previous) {
112-
// Reset position setpoint to current position when switching from terrain hold to non-terrain hold
110+
const bool terrain_position = (_terrain_hold || _param_mpc_alt_mode.get() == 1)
111+
&& PX4_ISFINITE(_dist_to_bottom)
112+
&& PX4_ISFINITE(_position_setpoint(2));
113+
114+
if (terrain_position) {
115+
// Terrain hold or terrain following: parent class set position from terrain.
116+
// Keep smoothing block synchronized to prevent divergence on transitions.
117+
_smoothing.setCurrentPosition(_position_setpoint(2));
118+
119+
} else {
120+
if (_terrain_position_previous) {
121+
// Transitioning out of terrain mode: reset smoothing to current position
113122
_smoothing.setCurrentPosition(_position(2));
114123
}
115124

116125
_position_setpoint(2) = _smoothing.getCurrentPosition();
117126
}
118127

119-
_terrain_hold_previous = _terrain_hold;
128+
_terrain_position_previous = terrain_position;
120129
}

src/modules/flight_mode_manager/tasks/ManualAltitudeSmoothVel/FlightTaskManualAltitudeSmoothVel.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ class FlightTaskManualAltitudeSmoothVel : public FlightTaskManualAltitude
6969
)
7070

7171
private:
72-
bool _terrain_hold_previous{false}; /**< true when vehicle was controlling height above a static ground position in the previous iteration */
72+
bool _terrain_position_previous{false}; /**< true when parent class was managing position setpoint from terrain data in the previous iteration */
7373
};

0 commit comments

Comments
 (0)