@@ -81,11 +81,8 @@ void FlightTaskManualAltitude::_updateConstraintsFromEstimator()
81
81
_min_distance_to_ground = -INFINITY;
82
82
}
83
83
84
- if (PX4_ISFINITE (_sub_vehicle_local_position.get ().hagl_max )) {
85
- _max_distance_to_ground = _sub_vehicle_local_position.get ().hagl_max ;
86
-
87
- } else {
88
- _max_distance_to_ground = INFINITY;
84
+ if (!PX4_ISFINITE (_max_distance_to_ground) && PX4_ISFINITE (_sub_vehicle_local_position.get ().hagl_max_z )) {
85
+ _max_distance_to_ground = _sub_vehicle_local_position.get ().hagl_max_z ;
89
86
}
90
87
}
91
88
@@ -154,8 +151,6 @@ void FlightTaskManualAltitude::_updateAltitudeLock()
154
151
if ((_param_mpc_alt_mode.get () == 1 || _terrain_hold) && PX4_ISFINITE (_dist_to_bottom)) {
155
152
// terrain following
156
153
_terrainFollowing (apply_brake, stopped);
157
- // respect maximum altitude
158
- _respectMaxAltitude ();
159
154
160
155
} else {
161
156
// normal mode where height is dependent on local frame
@@ -185,9 +180,10 @@ void FlightTaskManualAltitude::_updateAltitudeLock()
185
180
// user demands velocity change
186
181
_position_setpoint (2 ) = NAN;
187
182
// ensure that maximum altitude is respected
188
- _respectMaxAltitude ();
189
183
}
190
184
}
185
+
186
+ _respectMaxAltitude ();
191
187
}
192
188
193
189
void FlightTaskManualAltitude::_respectMinAltitude ()
@@ -229,29 +225,20 @@ void FlightTaskManualAltitude::_respectMaxAltitude()
229
225
{
230
226
if (PX4_ISFINITE (_dist_to_bottom)) {
231
227
232
- // if there is a valid maximum distance to ground, linearly increase speed limit with distance
233
- // below the maximum, preserving control loop vertical position error gain.
234
- // TODO: manipulate the velocity setpoint instead of tweaking the saturation of the controller
228
+ float vel_constrained = _param_mpc_z_p.get () * (_max_distance_to_ground - _dist_to_bottom);
229
+
235
230
if (PX4_ISFINITE (_max_distance_to_ground)) {
236
- _constraints.speed_up = math::constrain (_param_mpc_z_p.get () * (_max_distance_to_ground - _dist_to_bottom),
237
- -_param_mpc_z_vel_max_dn.get (), _param_mpc_z_vel_max_up.get ());
231
+ _constraints.speed_up = math::constrain (vel_constrained, -_param_mpc_z_vel_max_dn.get (), _param_mpc_z_vel_max_up.get ());
238
232
239
233
} else {
240
234
_constraints.speed_up = _param_mpc_z_vel_max_up.get ();
241
235
}
242
236
243
- // if distance to bottom exceeded maximum distance, slowly approach maximum distance
244
- if (_dist_to_bottom > _max_distance_to_ground) {
245
- // difference between current distance to ground and maximum distance to ground
246
- const float delta_distance_to_max = _dist_to_bottom - _max_distance_to_ground;
247
- // set position setpoint to maximum distance to ground
248
- _position_setpoint (2 ) = _position (2 ) + delta_distance_to_max;
249
- // limit speed downwards to 0.7m/s
250
- _constraints.speed_down = math::min (_param_mpc_z_vel_max_dn.get (), 0 .7f );
251
-
252
- } else {
253
- _constraints.speed_down = _param_mpc_z_vel_max_dn.get ();
237
+ if (_dist_to_bottom > _max_distance_to_ground && !(_sticks.getThrottleZeroCenteredExpo () < FLT_EPSILON)) {
238
+ _velocity_setpoint (2 ) = math::constrain (-vel_constrained, 0 .f , _param_mpc_z_vel_max_dn.get ());
254
239
}
240
+
241
+ _constraints.speed_down = _param_mpc_z_vel_max_dn.get ();
255
242
}
256
243
}
257
244
@@ -306,6 +293,7 @@ bool FlightTaskManualAltitude::update()
306
293
_scaleSticks ();
307
294
_updateSetpoints ();
308
295
_constraints.want_takeoff = _checkTakeoff ();
296
+ _max_distance_to_ground = INFINITY;
309
297
310
298
return ret;
311
299
}
0 commit comments