PX4 Firmware
PX4 Autopilot Software http://px4.io
FlightTask.cpp
Go to the documentation of this file.
1 #include "FlightTask.hpp"
2 #include <mathlib/mathlib.h>
3 #include <lib/ecl/geo/geo.h>
4 
5 constexpr uint64_t FlightTask::_timeout;
6 // First index of empty_setpoint corresponds to time-stamp and requires a finite number.
7 const vehicle_local_position_setpoint_s FlightTask::empty_setpoint = {0, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, {NAN, NAN, NAN}, {NAN, NAN, NAN}, {NAN, NAN, NAN}, {}};
8 
9 const vehicle_constraints_s FlightTask::empty_constraints = {0, NAN, NAN, NAN, NAN, NAN, NAN, NAN, false, {}};
10 const landing_gear_s FlightTask::empty_landing_gear_default_keep = {0, landing_gear_s::GEAR_KEEP, {}};
11 
13 {
19  return true;
20 }
21 
23 {
25 }
26 
28 {
33 
36 
39  return true;
40 }
41 
43 {
49 }
50 
52 {
53  // Check if a reset event has happened
57  }
58 
62  }
63 
67  }
68 
72  }
73 
75  float delta_psi = matrix::Eulerf(matrix::Quatf(_sub_attitude.get().delta_q_reset)).psi();
76  _ekfResetHandlerHeading(delta_psi);
78  }
79 }
80 
82 {
83  /* fill position setpoint message */
84  vehicle_local_position_setpoint_s vehicle_local_position_setpoint;
85  vehicle_local_position_setpoint.timestamp = hrt_absolute_time();
86 
87  vehicle_local_position_setpoint.x = _position_setpoint(0);
88  vehicle_local_position_setpoint.y = _position_setpoint(1);
89  vehicle_local_position_setpoint.z = _position_setpoint(2);
90 
91  vehicle_local_position_setpoint.vx = _velocity_setpoint(0);
92  vehicle_local_position_setpoint.vy = _velocity_setpoint(1);
93  vehicle_local_position_setpoint.vz = _velocity_setpoint(2);
94 
95  _acceleration_setpoint.copyTo(vehicle_local_position_setpoint.acceleration);
96  _jerk_setpoint.copyTo(vehicle_local_position_setpoint.jerk);
97  _thrust_setpoint.copyTo(vehicle_local_position_setpoint.thrust);
98  vehicle_local_position_setpoint.yaw = _yaw_setpoint;
99  vehicle_local_position_setpoint.yawspeed = _yawspeed_setpoint;
100 
101  return vehicle_local_position_setpoint;
102 }
103 
105 {
109  _jerk_setpoint.setAll(NAN);
112 }
113 
115 {
116  _position.setAll(NAN);
117  _velocity.setAll(NAN);
118  _yaw = NAN;
119  _dist_to_bottom = NAN;
120 
122  // yaw
124  }
125 
126  // Only use vehicle-local-position topic fields if the topic is received within a certain timestamp
128 
129  // position
133  }
134 
137  }
138 
139  // velocity
143  }
144 
147  }
148 
149  // distance to bottom
151  && PX4_ISFINITE(_sub_vehicle_local_position.get().dist_bottom)) {
153  }
154 
155  // global frame reference coordinates to enable conversions
159  }
160  }
161 }
162 
164 {
165  _constraints.speed_xy = _param_mpc_xy_vel_max.get();
166  _constraints.speed_up = _param_mpc_z_vel_max_up.get();
167  _constraints.speed_down = _param_mpc_z_vel_max_dn.get();
168  _constraints.tilt = math::radians(_param_mpc_tiltmax_air.get());
171  _constraints.want_takeoff = false;
172 }
173 
175 {
176  // position setpoint above the minimum altitude
177  bool position_triggered_takeoff = false;
178 
179  if (PX4_ISFINITE(_position_setpoint(2))) {
180  // minimal altitude either 20cm or what is necessary for correct estimation e.g. optical flow
181  float min_altitude = 0.2f;
182  const float min_distance_to_ground = _sub_vehicle_local_position.get().hagl_min;
183 
184  if (PX4_ISFINITE(min_distance_to_ground)) {
185  min_altitude = min_distance_to_ground + 0.05f;
186  }
187 
188  position_triggered_takeoff = _position_setpoint(2) < (_position(2) - min_altitude);
189  }
190 
191  // upwards velocity setpoint
192  bool velocity_triggered_takeoff = false;
193 
194  if (PX4_ISFINITE(_velocity_setpoint(2))) {
195  velocity_triggered_takeoff = _velocity_setpoint(2) < -0.3f;
196  }
197 
198  return position_triggered_takeoff || velocity_triggered_takeoff;
199 }
matrix::Vector3f _acceleration_setpoint
Definition: FlightTask.hpp:222
matrix::Vector3f _velocity
current vehicle velocity
Definition: FlightTask.hpp:208
int globallocalconverter_init(double lat_0, double lon_0, float alt_0, uint64_t timestamp)
Initialize the global mapping between global position (spherical) and local position (NED)...
Definition: geo.cpp:211
static const vehicle_local_position_setpoint_s empty_setpoint
Empty setpoint.
Definition: FlightTask.hpp:139
float _yawspeed_setpoint
Definition: FlightTask.hpp:226
hrt_abstime _time_stamp_activate
time stamp when task was activated
Definition: FlightTask.hpp:202
void _checkEkfResetCounters()
Definition: FlightTask.cpp:51
float _yaw_setpoint
Definition: FlightTask.hpp:225
virtual bool activate(vehicle_local_position_setpoint_s last_setpoint)
Call once on the event where you switch to the task.
Definition: FlightTask.cpp:12
virtual void _setDefaultConstraints()
Set constraints to default values.
Definition: FlightTask.cpp:163
Definition of geo / math functions to perform geodesic calculations.
float _deltatime
passed time in seconds since the task was last updated
Definition: FlightTask.hpp:201
static const landing_gear_s empty_landing_gear_default_keep
default landing gear state
Definition: FlightTask.hpp:150
virtual void _ekfResetHandlerVelocityXY()
Definition: FlightTask.hpp:193
virtual void _ekfResetHandlerPositionXY()
Definition: FlightTask.hpp:192
virtual void _ekfResetHandlerHeading(float delta_psi)
Definition: FlightTask.hpp:196
hrt_abstime _time_stamp_current
time stamp at the beginning of the current task update
Definition: FlightTask.hpp:203
void copyTo(Type dst[M *N]) const
Definition: Matrix.hpp:115
matrix::Vector3f _position_setpoint
Setpoints which the position controller has to execute.
Definition: FlightTask.hpp:220
Quaternion class.
Definition: Dcm.hpp:24
float _dist_to_bottom
current height above ground level
Definition: FlightTask.hpp:210
static constexpr uint64_t _timeout
maximal time in us before a loop or data times out
Definition: FlightTask.hpp:199
virtual void _ekfResetHandlerVelocityZ()
Definition: FlightTask.hpp:195
landing_gear_s _gear
Definition: FlightTask.hpp:246
matrix::Vector3f _jerk_setpoint
Definition: FlightTask.hpp:223
matrix::Vector3f _thrust_setpoint
Definition: FlightTask.hpp:224
Abstract base class for different advanced flight tasks like orbit, follow me, ...
virtual void _ekfResetHandlerPositionZ()
Definition: FlightTask.hpp:194
struct FlightTask::@63 _reset_counters
constexpr T radians(T degrees)
Definition: Limits.hpp:85
virtual bool updateInitialize()
Call before activate() or update() to initialize time and input data.
Definition: FlightTask.cpp:27
Euler< float > Eulerf
Definition: Euler.hpp:156
matrix::Vector3f _position
current vehicle position
Definition: FlightTask.hpp:207
float _time
passed time in seconds since the task was activated
Definition: FlightTask.hpp:200
matrix::Vector3f _velocity_setpoint
Definition: FlightTask.hpp:221
uORB::SubscriptionData< vehicle_local_position_s > _sub_vehicle_local_position
Definition: FlightTask.hpp:171
virtual void reActivate()
Call this to reset an active Flight Task.
Definition: FlightTask.cpp:22
vehicle_constraints_s _constraints
Vehicle constraints.
Definition: FlightTask.hpp:244
constexpr _Tp min(_Tp a, _Tp b)
Definition: Limits.hpp:54
const T & get() const
virtual bool _checkTakeoff()
Determine when to trigger a takeoff (ignored in flight)
Definition: FlightTask.cpp:174
void _evaluateVehicleLocalPosition()
Check and update local position.
Definition: FlightTask.cpp:114
void _resetSetpoints()
Reset all setpoints to NAN.
Definition: FlightTask.cpp:104
const vehicle_local_position_setpoint_s getPositionSetpoint()
Get the output data.
Definition: FlightTask.cpp:81
float _yaw
current vehicle yaw heading
Definition: FlightTask.hpp:209
uORB::SubscriptionData< vehicle_attitude_s > _sub_attitude
Definition: FlightTask.hpp:172
static const vehicle_constraints_s empty_constraints
Empty constraints.
Definition: FlightTask.hpp:145
void _initEkfResetCounters()
Monitor the EKF reset counters and call the appropriate handling functions in case of a reset event...
Definition: FlightTask.cpp:42
hrt_abstime _time_stamp_last
time stamp when task was last updated
Definition: FlightTask.hpp:204
void setAll(Type val)
Definition: Matrix.hpp:426
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).