39 #include <mathlib/mathlib.h> 47 if (!PX4_ISFINITE(_yaw_setpoint)) {
49 _generateHeadingAlongTrack();
52 if (_param_mpc_yaw_mode.get() == 4 && !_yaw_sp_aligned) {
54 if (!_position_locked) {
55 _velocity_setpoint.setAll(0.
f);
56 _position_setpoint = _position;
57 _position_locked =
true;
61 _position_locked =
false;
62 _generateAltitudeSetpoints();
63 _generateXYsetpoints();
71 _speed_at_target = 0.0f;
73 if (
Vector2f(&(_target - _next_wp)(0)).length() > 0.001
f &&
74 (
Vector2f(&(_target - _prev_wp)(0)).length() > _target_acceptance_radius)) {
77 Vector2f(&(_target - _prev_wp)(0)).unit_or_zero()
78 *
Vector2f(&(_target - _next_wp)(0)).unit_or_zero()
87 Vector2f prev_to_dest(_target - _prev_wp);
88 _compute_heading_from_2D_vector(_yaw_setpoint, prev_to_dest);
95 Vector2f pos_sp_to_dest(_target - _position_setpoint);
96 const bool has_reached_altitude = fabsf(_target(2) - _position(2)) < _param_nav_mc_alt_rad.get();
98 if ((_speed_at_target < 0.001
f && pos_sp_to_dest.
length() < _target_acceptance_radius) ||
99 (!has_reached_altitude && pos_sp_to_dest.
length() < _target_acceptance_radius)) {
102 _position_setpoint(0) = _target(0);
103 _position_setpoint(1) = _target(1);
104 _velocity_setpoint(0) = _velocity_setpoint(1) = 0.0f;
109 _speed_at_target =
math::max(_speed_at_target, 0.5
f);
113 Vector2f prev_to_pos(_position - _prev_wp);
114 Vector2f closest_pt =
Vector2f(_prev_wp) + u_prev_to_dest * (prev_to_pos * u_prev_to_dest);
115 Vector2f closest_to_dest(_target - _position);
116 Vector2f prev_to_dest(_target - _prev_wp);
117 float speed_sp_track = _mc_cruise_speed;
118 float speed_sp_prev_track =
math::max(
Vector2f(_velocity_setpoint) * u_prev_to_dest, 0.0
f);
122 float target_threshold = 1.5f * _mc_cruise_speed;
123 float speed_threshold = _mc_cruise_speed;
124 const float threshold_max = target_threshold;
126 if (target_threshold > 0.5
f * prev_to_dest.
length()) {
128 target_threshold = 0.5f * prev_to_dest.
length();
132 if (threshold_max > _target_acceptance_radius) {
133 float m = (_mc_cruise_speed - _speed_at_target) / (threshold_max - _target_acceptance_radius);
134 speed_threshold = m * (target_threshold - _target_acceptance_radius) + _speed_at_target;
138 if (closest_to_dest.length() < target_threshold) {
142 if (!has_reached_altitude) {
144 _speed_at_target = 0.0f;
147 float acceptance_radius = _target_acceptance_radius;
149 if (_speed_at_target < 0.01
f) {
151 acceptance_radius = 0.0f;
154 if ((target_threshold - acceptance_radius) >=
SIGMA_NORM) {
157 float m = (speed_threshold - _speed_at_target) / (target_threshold - acceptance_radius);
158 speed_sp_track = m * (closest_to_dest.length() - acceptance_radius) + _speed_at_target;
161 speed_sp_track = _speed_at_target;
167 if ((speed_sp_prev_track < speed_sp_track)
168 && (speed_sp_track * speed_sp_prev_track > 0.0f)
169 && (speed_sp_prev_track > _speed_at_target)) {
170 speed_sp_track = speed_sp_prev_track;
176 float acc_track = (speed_sp_track - speed_sp_prev_track) / _deltatime;
179 float acc_max = (_yaw_sp_aligned) ? _param_mpc_acc_hor.get() : 0.5f;
181 if (acc_track > acc_max) {
183 speed_sp_track = acc_max * _deltatime + speed_sp_prev_track;
187 speed_sp_track =
math::min(speed_threshold, speed_sp_track);
192 _position_setpoint(0) = closest_pt(0);
193 _position_setpoint(1) = closest_pt(1);
194 Vector2f velocity_sp_xy = u_prev_to_dest * speed_sp_track;
195 _velocity_setpoint(0) = velocity_sp_xy(0);
196 _velocity_setpoint(1) = velocity_sp_xy(1);
203 const float dist = fabsf(_target(2) - _prev_wp(2));
206 if ((dist >
SIGMA_NORM) && (fabsf(_position(2) - _target(2)) > 0.1
f)) {
209 const float dist_to_prev = fabsf(_position(2) - _prev_wp(2));
210 const float dist_to_target = fabsf(_target(2) - _position(2));
213 const bool flying_upward = _target(2) < _position(2);
218 _param_mpc_land_alt2.get(), _param_mpc_land_alt1.get(),
219 _param_mpc_land_speed.get(), _constraints.speed_down);
223 float speed_sp = (flying_upward) ? _constraints.speed_up : vel_limit;
227 float target_threshold = speed_sp * 1.5f;
231 const bool is_2_target_threshold = dist >= 2.0f * target_threshold;
232 const float min_vel = 0.2f;
233 const float slope = (speed_sp - min_vel) / (target_threshold);
235 if (!is_2_target_threshold) {
238 target_threshold = dist * 0.5f;
240 speed_sp = slope * (target_threshold) + min_vel;
244 if (dist_to_target < target_threshold) {
246 speed_sp = slope * dist_to_target + min_vel;
248 }
else if (dist_to_prev < target_threshold) {
251 const float acc = (speed_sp - fabsf(_velocity_setpoint(2))) / _deltatime;
252 const float acc_max = (flying_upward) ? (_param_mpc_acc_up_max.get() * 0.5f) : (_param_mpc_acc_down_max.get() * 0.5f);
255 speed_sp = acc_max * _deltatime + fabsf(_velocity_setpoint(2));
260 if (speed_sp < 0.0
f) {
261 PX4_WARN(
"speed cannot be smaller than 0");
266 _velocity_setpoint(2) = (flying_upward) ? -speed_sp : speed_sp;
267 _position_setpoint(2) = NAN;
272 _velocity_setpoint(2) = 0.0f;
273 _position_setpoint(2) = _target(2);
constexpr _Tp constrain(_Tp val, _Tp min_val, _Tp max_val)
Flight task for autonomous, gps driven mode.
static constexpr float SIGMA_NORM
void _setSpeedAtTarget()
Sets desiered speed at target.
void _generateAltitudeSetpoints()
Generate velocity and position setpoints for following line along z.
void _generateSetpoints() override
Generate setpoints along line.
void _generateXYsetpoints()
Generate velocity and position setpoints for following line along xy.
const T gradual(const T &value, const T &x_low, const T &x_high, const T &y_low, const T &y_high)
void _generateHeadingAlongTrack()
Generates heading along track.
const T expontialFromLimits(const T &X_in, const T &Y_min, const T &Y_mid, const T &Y_max)
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Vector2< float > Vector2f
constexpr _Tp min(_Tp a, _Tp b)
constexpr _Tp max(_Tp a, _Tp b)