PX4 Firmware
PX4 Autopilot Software http://px4.io
FlightTaskAutoMapper2.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file FlightAutoMapper2.cpp
36  */
37 
39 #include <mathlib/mathlib.h>
40 
41 using namespace matrix;
42 
44 {
45  bool ret = FlightTaskAuto::activate(last_setpoint);
46  _reset();
47  return ret;
48 }
49 
51 {
52  // always reset constraints because they might change depending on the type
53  _setDefaultConstraints();
54 
55  _updateAltitudeAboveGround();
56 
57  // The only time a thrust set-point is sent out is during
58  // idle. Hence, reset thrust set-point to NAN in case the
59  // vehicle exits idle.
60 
61  if (_type_previous == WaypointType::idle) {
62  _thrust_setpoint = Vector3f(NAN, NAN, NAN);
63  }
64 
65  // during mission and reposition, raise the landing gears but only
66  // if altitude is high enough
67  if (_highEnoughForLandingGear()) {
68  _gear.landing_gear = landing_gear_s::GEAR_UP;
69  }
70 
71  switch (_type) {
72  case WaypointType::idle:
73  _prepareIdleSetpoints();
74  break;
75 
76  case WaypointType::land:
77  _prepareLandSetpoints();
78  break;
79 
81 
82  /* fallthrought */
84  _preparePositionSetpoints();
85  break;
86 
88  _prepareTakeoffSetpoints();
89  break;
90 
92  _prepareVelocitySetpoints();
93  break;
94 
95  default:
96  _preparePositionSetpoints();
97  break;
98  }
99 
100  if (_param_com_obs_avoid.get()) {
101  _obstacle_avoidance.updateAvoidanceDesiredSetpoints(_position_setpoint, _velocity_setpoint, (int)_type);
102  _obstacle_avoidance.injectAvoidanceSetpoints(_position_setpoint, _velocity_setpoint, _yaw_setpoint,
103  _yawspeed_setpoint);
104  }
105 
106 
107  _generateSetpoints();
108 
109  // update previous type
110  _type_previous = _type;
111 
112  return true;
113 }
114 
116 {
117  // Set setpoints equal current state.
118  _velocity_setpoint = _velocity;
119  _position_setpoint = _position;
120 }
121 
123 {
124  // Send zero thrust setpoint
125  _position_setpoint = Vector3f(NAN, NAN, NAN); // Don't require any position/velocity setpoints
126  _velocity_setpoint = Vector3f(NAN, NAN, NAN);
127  _thrust_setpoint.zero();
128 }
129 
131 {
132  float land_speed = _getLandSpeed();
133 
134  // Keep xy-position and go down with landspeed
135  _position_setpoint = Vector3f(_target(0), _target(1), NAN);
136  _velocity_setpoint = Vector3f(Vector3f(NAN, NAN, land_speed));
137 
138  // set constraints
139  _constraints.tilt = math::radians(_param_mpc_tiltmax_lnd.get());
140  _gear.landing_gear = landing_gear_s::GEAR_DOWN;
141 }
142 
144 {
145  // Takeoff is completely defined by target position
146  _position_setpoint = _target;
147  const float speed_tko = (_alt_above_ground > _param_mpc_land_alt1.get()) ? _constraints.speed_up :
148  _param_mpc_tko_speed.get();
149  _velocity_setpoint = Vector3f(NAN, NAN, -speed_tko); // Limit the maximum vertical speed
150 
151  _gear.landing_gear = landing_gear_s::GEAR_DOWN;
152 }
153 
155 {
156  // XY Velocity waypoint
157  // TODO : Rewiew that. What is the expected behavior?
158  _position_setpoint = Vector3f(NAN, NAN, _position(2));
159  Vector2f vel_sp_xy = Vector2f(_velocity).unit_or_zero() * _mc_cruise_speed;
160  _velocity_setpoint = Vector3f(vel_sp_xy(0), vel_sp_xy(1), NAN);
161 }
162 
164 {
165  // Simple waypoint navigation: go to xyz target, with standard limitations
166  _position_setpoint = _target;
167  _velocity_setpoint = Vector3f(NAN, NAN, NAN); // No special velocity limitations
168 }
169 
171 {
172  // Altitude above ground is by default just the negation of the current local position in D-direction.
173  _alt_above_ground = -_position(2);
174 
175  if (PX4_ISFINITE(_dist_to_bottom)) {
176  // We have a valid distance to ground measurement
177  _alt_above_ground = _dist_to_bottom;
178 
179  } else if (_sub_home_position.get().valid_alt) {
180  // if home position is set, then altitude above ground is relative to the home position
181  _alt_above_ground = -_position(2) + _sub_home_position.get().z;
182  }
183 }
184 
186 {
187  FlightTaskAuto::updateParams();
188 
189  // make sure that alt1 is above alt2
190  _param_mpc_land_alt1.set(math::max(_param_mpc_land_alt1.get(), _param_mpc_land_alt2.get()));
191 }
192 
194 {
195  // return true if altitude is above two meters
196  return _alt_above_ground > 2.0f;
197 }
198 
200 {
201  bool rc_assist_enabled = _param_mpc_land_rc_help.get();
202  bool rc_is_valid = !_sub_vehicle_status.get().rc_signal_lost;
203 
204  float throttle = 0.5f;
205 
206  if (rc_is_valid && rc_assist_enabled) {
207  throttle = _sub_manual_control_setpoint.get().z;
208  }
209 
210  float speed = 0;
211 
212  if (_alt_above_ground > _param_mpc_land_alt1.get()) {
213  speed = _constraints.speed_down;
214 
215  } else {
216  float land_speed = _param_mpc_land_speed.get();
217  float head_room = _constraints.speed_down - land_speed;
218 
219  speed = land_speed + 2 * (0.5f - throttle) * head_room;
220 
221  // Allow minimum assisted land speed to be half of parameter
222  if (speed < land_speed * 0.5f) {
223  speed = land_speed * 0.5f;
224  }
225  }
226 
227  return speed;
228 }
bool activate(vehicle_local_position_setpoint_s last_setpoint) override
Call once on the event where you switch to the task.
Abstract Flight task which generates local setpoints based on the triplet type.
float _getLandSpeed()
Returns landing descent speed.
void _reset()
Resets member variables to current vehicle state.
constexpr T radians(T degrees)
Definition: Limits.hpp:85
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
Vector2< float > Vector2f
Definition: Vector2.hpp:69
bool update() override
To be called regularly in the control loop cycle to execute the task.
Vector3< float > Vector3f
Definition: Vector3.hpp:136
void updateParams() override
See ModuleParam class.
bool activate(vehicle_local_position_setpoint_s last_setpoint) override
Call once on the event where you switch to the task.
bool _highEnoughForLandingGear()
Checks if gears can be lowered.
constexpr _Tp max(_Tp a, _Tp b)
Definition: Limits.hpp:60
void _updateAltitudeAboveGround()
Computes altitude above ground based on sensors available.