40 #include <mathlib/mathlib.h> 48 _sub_home_position.update();
51 return ret && PX4_ISFINITE(_position(2)) && PX4_ISFINITE(_velocity(2)) && PX4_ISFINITE(_yaw);
58 _yawspeed_setpoint = 0.0f;
60 _position_setpoint(2) = _position(2);
61 _velocity_setpoint(2) = 0.0f;
62 _setDefaultConstraints();
64 _constraints.tilt =
math::radians(_param_mpc_man_tilt_max.get());
66 if (PX4_ISFINITE(_sub_vehicle_local_position.get().hagl_min)) {
67 _constraints.min_distance_to_ground = _sub_vehicle_local_position.get().hagl_min;
70 _constraints.min_distance_to_ground = -INFINITY;
73 if (PX4_ISFINITE(_sub_vehicle_local_position.get().hagl_max)) {
74 _constraints.max_distance_to_ground = _sub_vehicle_local_position.get().hagl_max;
77 _constraints.max_distance_to_ground = INFINITY;
80 _max_speed_up = _constraints.speed_up;
81 _min_speed_down = _constraints.speed_down;
89 const float yawspeed_target = _sticks_expo(3) *
math::radians(_param_mpc_man_y_max.get());
90 _yawspeed_setpoint = _applyYawspeedFilter(yawspeed_target);
93 const float vel_max_z = (_sticks(2) > 0.0f) ? _constraints.speed_down : _constraints.speed_up;
94 _velocity_setpoint(2) = vel_max_z * _sticks_expo(2);
99 const float den =
math::max(_param_mpc_man_y_tau.get() + _deltatime, 0.001f);
100 const float alpha = _deltatime / den;
101 _yawspeed_filter_state = (1.f - alpha) * _yawspeed_filter_state + alpha * yawspeed_target;
102 return _yawspeed_filter_state;
111 const bool apply_brake = fabsf(_sticks_expo(2)) <=
FLT_EPSILON;
114 const bool stopped = (_param_mpc_hold_max_z.get() <
FLT_EPSILON || fabsf(_velocity(2)) < _param_mpc_hold_max_z.get());
118 if (_param_mpc_alt_mode.get() == 2) {
120 float spd_xy =
Vector2f(_velocity).length();
123 float stick_xy =
Vector2f(&_sticks_expo(0)).length();
124 bool stick_input = stick_xy > 0.001f;
127 bool too_fast = spd_xy > _param_mpc_hold_max_xy.get();
129 if (stick_input || too_fast || !PX4_ISFINITE(_dist_to_bottom)) {
131 _terrain_hold =
false;
132 _terrain_follow =
false;
135 if (PX4_ISFINITE(_dist_to_ground_lock) && PX4_ISFINITE(_dist_to_bottom)) {
136 _position_setpoint(2) = _position(2) + (_dist_to_ground_lock - _dist_to_bottom);
139 _position_setpoint(2) = _position(2);
144 bool not_moving = spd_xy < 0.5f * _param_mpc_hold_max_xy.get();
146 if (!stick_input && not_moving && PX4_ISFINITE(_dist_to_bottom)) {
148 _terrain_hold =
true;
149 _terrain_follow =
true;
152 if (PX4_ISFINITE(_position_setpoint(2))) {
153 _dist_to_ground_lock = _dist_to_bottom + (_position_setpoint(2) - _position(2));
160 if ((_param_mpc_alt_mode.get() == 1 || _terrain_follow) && PX4_ISFINITE(_dist_to_bottom)) {
162 _terrainFollowing(apply_brake, stopped);
164 _respectMaxAltitude();
169 if (apply_brake && stopped && !PX4_ISFINITE(_position_setpoint(2))) {
171 _position_setpoint(2) = _position(2);
175 if (PX4_ISFINITE(_dist_to_bottom) && _dist_to_bottom < _constraints.min_distance_to_ground) {
176 _terrainFollowing(apply_brake, stopped);
179 _dist_to_ground_lock = NAN;
182 }
else if (PX4_ISFINITE(_position_setpoint(2)) && apply_brake) {
185 if (_sub_vehicle_local_position.get().z_reset_counter != _reset_counter) {
186 _position_setpoint(2) = _position(2);
187 _reset_counter = _sub_vehicle_local_position.get().z_reset_counter;
192 _position_setpoint(2) = NAN;
194 _respectMaxAltitude();
201 const bool respectAlt = PX4_ISFINITE(_dist_to_bottom)
202 && _dist_to_bottom < _constraints.min_distance_to_ground;
207 _position_setpoint(2) = _position(2)
208 - (_constraints.min_distance_to_ground - _dist_to_bottom);
214 if (apply_brake && stopped && !PX4_ISFINITE(_dist_to_ground_lock)) {
218 _position_setpoint(2) = _position(2);
220 _respectMinAltitude();
222 _dist_to_ground_lock = _dist_to_bottom - (_position_setpoint(2) - _position(2));
224 }
else if (apply_brake && PX4_ISFINITE(_dist_to_ground_lock)) {
228 const float delta_distance_to_ground = _dist_to_ground_lock - _dist_to_bottom;
230 _position_setpoint(2) = _position(2) - delta_distance_to_ground;
234 _dist_to_ground_lock = _position_setpoint(2) = NAN;
240 if (PX4_ISFINITE(_dist_to_bottom)) {
244 if (PX4_ISFINITE(_constraints.max_distance_to_ground)) {
245 _constraints.speed_up =
math::constrain(_param_mpc_z_p.get() * (_constraints.max_distance_to_ground - _dist_to_bottom),
246 -_min_speed_down, _max_speed_up);
249 _constraints.speed_up = _max_speed_up;
253 if (_dist_to_bottom > _constraints.max_distance_to_ground) {
255 const float delta_distance_to_max = _dist_to_bottom - _constraints.max_distance_to_ground;
257 _position_setpoint(2) = _position(2) + delta_distance_to_max;
259 _constraints.speed_down =
math::min(_min_speed_down, 0.7
f);
262 _constraints.speed_down = _min_speed_down;
270 float dist_to_ground = NAN;
273 if (PX4_ISFINITE(_dist_to_bottom)) {
274 dist_to_ground = _dist_to_bottom;
276 }
else if (_sub_home_position.get().valid_alt) {
277 dist_to_ground = -(_position(2) - _sub_home_position.get().z);
281 if (PX4_ISFINITE(dist_to_ground)) {
283 _param_mpc_land_alt2.get(), _param_mpc_land_alt1.get(),
284 _param_mpc_land_speed.get(), _constraints.speed_down);
286 _param_mpc_land_alt2.get(), _param_mpc_land_alt1.get(),
287 _param_mpc_tko_speed.get(), _constraints.speed_up);
288 _velocity_setpoint(2) =
math::constrain(_velocity_setpoint(2), -limit_up, limit_down);
294 float yaw_rotate = PX4_ISFINITE(_yaw_setpoint) ? _yaw_setpoint : _yaw;
310 bool FlightTaskManualAltitude::_isYawInput()
317 return fabsf(_yawspeed_setpoint) > 0.001f;
329 if (!PX4_ISFINITE(_yaw_setpoint)) {
330 _yaw_setpoint = _yaw;
337 if (PX4_ISFINITE(_yaw_setpoint)) {
338 _yaw_setpoint += delta_psi;
344 _updateHeadingSetpoints();
352 _rotateIntoHeadingFrame(sp);
358 _thrust_setpoint(0) = sp(0);
359 _thrust_setpoint(1) = sp(1);
360 _thrust_setpoint(2) = NAN;
362 _updateAltitudeLock();
363 _respectGroundSlowdown();
369 return _sticks(2) < -0.3f;
376 _constraints.want_takeoff = _checkTakeoff();
constexpr _Tp constrain(_Tp val, _Tp min_val, _Tp max_val)
bool _checkTakeoff() override
Determine when to trigger a takeoff (ignored in flight)
virtual bool activate(vehicle_local_position_setpoint_s last_setpoint)
Call once on the event where you switch to the task.
bool updateInitialize() override
Call before activate() or update() to initialize time and input data.
bool update() override
To be called regularly in the control loop cycle to execute the task.
DEFINE_PARAMETERS_CUSTOM_PARENT(FlightTaskManual,(ParamFloat< px4::params::MPC_HOLD_MAX_Z >) _param_mpc_hold_max_z,(ParamInt< px4::params::MPC_ALT_MODE >) _param_mpc_alt_mode,(ParamFloat< px4::params::MPC_HOLD_MAX_XY >) _param_mpc_hold_max_xy,(ParamFloat< px4::params::MPC_Z_P >) _param_mpc_z_p,(ParamFloat< px4::params::MPC_MAN_Y_MAX >) _param_mpc_man_y_max,(ParamFloat< px4::params::MPC_MAN_Y_TAU >) _param_mpc_man_y_tau,(ParamFloat< px4::params::MPC_MAN_TILT_MAX >) _param_mpc_man_tilt_max,(ParamFloat< px4::params::MPC_LAND_ALT1 >) _param_mpc_land_alt1,(ParamFloat< px4::params::MPC_LAND_ALT2 >) _param_mpc_land_alt2,(ParamFloat< px4::params::MPC_LAND_SPEED >) _param_mpc_land_speed,(ParamFloat< px4::params::MPC_TKO_SPEED >) _param_mpc_tko_speed) private void _unlockYaw()
void _respectMinAltitude()
Minimum Altitude during range sensor operation.
const T gradual(const T &value, const T &x_low, const T &x_high, const T &y_low, const T &y_high)
void _rotateIntoHeadingFrame(matrix::Vector2f &vec)
rotates vector into local frame
void _updateAltitudeLock()
Check and sets for position lock.
void _ekfResetHandlerHeading(float delta_psi) override
adjust heading setpoint in case of EKF reset event
constexpr T radians(T degrees)
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Vector2< float > Vector2f
void _updateHeadingSetpoints()
sets yaw or yaw speed
void _respectGroundSlowdown()
Sets downwards velocity constraint based on the distance to ground.
virtual void _updateSetpoints()
updates all setpoints
constexpr _Tp min(_Tp a, _Tp b)
float _applyYawspeedFilter(float yawspeed_target)
Filter between stick input and yaw setpoint.
bool activate(vehicle_local_position_setpoint_s last_setpoint) override
Call once on the event where you switch to the task.
bool updateInitialize() override
Call before activate() or update() to initialize time and input data.
Vector3< float > Vector3f
void _respectMaxAltitude()
constexpr _Tp max(_Tp a, _Tp b)
void _terrainFollowing(bool apply_brake, bool stopped)
Terrain following.
virtual void _scaleSticks()
scales sticks to velocity in z