Firmware
FlightTask.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 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 
42 #pragma once
43 
44 #include <px4_module_params.h>
45 #include <drivers/drv_hrt.h>
46 #include <matrix/matrix/math.hpp>
47 #include <uORB/Subscription.hpp>
48 #include <uORB/topics/landing_gear.h>
49 #include <uORB/topics/vehicle_local_position.h>
50 #include <uORB/topics/vehicle_local_position_setpoint.h>
51 #include <uORB/topics/vehicle_command.h>
52 #include <uORB/topics/vehicle_constraints.h>
53 #include <uORB/topics/vehicle_attitude.h>
54 #include <uORB/topics/vehicle_trajectory_waypoint.h>
56 #include "SubscriptionArray.hpp"
57 
58 class FlightTask : public ModuleParams
59 {
60 public:
61  FlightTask() :
62  ModuleParams(nullptr)
63  {
66  }
67 
68  virtual ~FlightTask() = default;
69 
75  virtual bool initializeSubscriptions(SubscriptionArray &subscription_array);
76 
81  virtual bool activate();
82 
86  virtual void reActivate();
87 
93  virtual bool applyCommandParameters(const vehicle_command_s &command) { return false; }
94 
100  virtual bool updateInitialize();
101 
106  virtual bool update() = 0;
107 
114  virtual bool updateFinalize() { return true; };
115 
120  const vehicle_local_position_setpoint_s getPositionSetpoint();
121 
127  const vehicle_constraints_s &getConstraints() { return _constraints; }
128 
134  const landing_gear_s &getGear() { return _gear; }
135 
140  const vehicle_trajectory_waypoint_s &getAvoidanceWaypoint() { return _desired_waypoint; }
141 
146  static const vehicle_local_position_setpoint_s empty_setpoint;
147 
152  static const vehicle_constraints_s empty_constraints;
153 
157  static const landing_gear_s empty_landing_gear_default_keep;
158 
163  static const vehicle_trajectory_waypoint_s empty_trajectory_waypoint;
164 
169  {
170  updateParams();
171  }
172 
177  virtual void setYawHandler(WeatherVane *ext_yaw_handler) {}
178 
179  void updateVelocityControllerIO(const matrix::Vector3f &vel_sp,
180  const matrix::Vector3f &thrust_sp) {_velocity_setpoint_feedback = vel_sp; _thrust_setpoint_feedback = thrust_sp; }
181 
182 protected:
183 
184  uORB::Subscription<vehicle_local_position_s> *_sub_vehicle_local_position{nullptr};
185  uORB::Subscription<vehicle_attitude_s> *_sub_attitude{nullptr};
191  void _resetSetpoints();
192 
197 
201  virtual void _setDefaultConstraints();
202 
203  /* Time abstraction */
204  static constexpr uint64_t _timeout = 500000;
205  float _time = 0;
206  float _deltatime = 0;
211  /* Current vehicle state */
212  matrix::Vector3f _position;
213  matrix::Vector3f _velocity;
214  float _yaw = 0.f;
215  float _dist_to_bottom = 0.0f;
225  matrix::Vector3f _position_setpoint;
226  matrix::Vector3f _velocity_setpoint;
227  matrix::Vector3f _acceleration_setpoint;
228  matrix::Vector3f _jerk_setpoint;
229  matrix::Vector3f _thrust_setpoint;
230  float _yaw_setpoint;
231  float _yawspeed_setpoint;
232 
233  matrix::Vector3f _velocity_setpoint_feedback;
234  matrix::Vector3f _thrust_setpoint_feedback;
235 
240  vehicle_constraints_s _constraints{};
241 
242  landing_gear_s _gear{};
243 
248  vehicle_trajectory_waypoint_s _desired_waypoint{};
249 
250  DEFINE_PARAMETERS_CUSTOM_PARENT(ModuleParams,
251  (ParamFloat<px4::params::MPC_XY_VEL_MAX>) _param_mpc_xy_vel_max,
252  (ParamFloat<px4::params::MPC_Z_VEL_MAX_DN>) _param_mpc_z_vel_max_dn,
253  (ParamFloat<px4::params::MPC_Z_VEL_MAX_UP>) _param_mpc_z_vel_max_up,
254  (ParamFloat<px4::params::MPC_TILTMAX_AIR>) _param_mpc_tiltmax_air
255  )
256 };
virtual bool update()=0
To be called regularly in the control loop cycle to execute the task.
Definition: WeatherVane.hpp:48
virtual void _setDefaultConstraints()
Set constraints to default values.
Definition: FlightTask.cpp:138
const vehicle_constraints_s & getConstraints()
Get vehicle constraints.
Definition: FlightTask.hpp:127
Definition: FlightTask.hpp:58
static constexpr uint64_t _timeout
maximal time in us before a loop or data times out
Definition: FlightTask.hpp:204
void handleParameterUpdate()
Call this whenever a parameter update notification is received (parameter_update uORB message) ...
Definition: FlightTask.hpp:168
const vehicle_trajectory_waypoint_s & getAvoidanceWaypoint()
Get avoidance desired waypoint.
Definition: FlightTask.hpp:140
virtual bool applyCommandParameters(const vehicle_command_s &command)
To be called to adopt parameters from an arrived vehicle command.
Definition: FlightTask.hpp:93
void _evaluateVehicleLocalPosition()
Check and update local position.
Definition: FlightTask.cpp:89
High-resolution timer with callouts and timekeeping.
virtual bool updateFinalize()
Call after update() to constrain the generated setpoints in order to comply with the constraints of t...
Definition: FlightTask.hpp:114
virtual void setYawHandler(WeatherVane *ext_yaw_handler)
Sets an external yaw handler which can be used by any flight task to implement a different yaw contro...
Definition: FlightTask.hpp:177
float _yaw
current vehicle yaw heading
Definition: FlightTask.hpp:214
static const vehicle_local_position_setpoint_s empty_setpoint
Empty setpoint.
Definition: FlightTask.hpp:146
Simple array that contains a dynamic amount of Subscription<T> instances.
vehicle_trajectory_waypoint_s _desired_waypoint
Desired waypoints.
Definition: FlightTask.hpp:248
static const landing_gear_s empty_landing_gear_default_keep
default landing gear state
Definition: FlightTask.hpp:157
static const vehicle_trajectory_waypoint_s empty_trajectory_waypoint
Empty desired waypoints.
Definition: FlightTask.hpp:163
void _resetSetpoints()
Reset all setpoints to NAN.
Definition: FlightTask.cpp:79
matrix::Vector3f _velocity
current vehicle velocity
Definition: FlightTask.hpp:213
Definition: px4_param.h:318
hrt_abstime _time_stamp_activate
time stamp when task was activated
Definition: FlightTask.hpp:207
float _time
passed time in seconds since the task was activated
Definition: FlightTask.hpp:205
C++ base class for modules/classes using configuration parameters.
Definition: px4_module_params.h:46
float _dist_to_bottom
current height above ground level
Definition: FlightTask.hpp:215
const vehicle_local_position_setpoint_s getPositionSetpoint()
Get the output data.
Definition: FlightTask.cpp:50
vehicle_constraints_s _constraints
Vehicle constraints.
Definition: FlightTask.hpp:240
matrix::Vector3f _position
current vehicle position
Definition: FlightTask.hpp:212
uint8_t _heading_reset_counter
estimator heading reset
Definition: FlightTask.hpp:186
const landing_gear_s & getGear()
Get landing gear position.
Definition: FlightTask.hpp:134
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
float _deltatime
passed time in seconds since the task was last updated
Definition: FlightTask.hpp:206
hrt_abstime _time_stamp_last
time stamp when task was last updated
Definition: FlightTask.hpp:209
virtual bool updateInitialize()
Call before activate() or update() to initialize time and input data.
Definition: FlightTask.cpp:40
virtual bool activate()
Call once on the event where you switch to the task.
Definition: FlightTask.cpp:25
static const vehicle_constraints_s empty_constraints
Empty constraints.
Definition: FlightTask.hpp:152
virtual bool initializeSubscriptions(SubscriptionArray &subscription_array)
Initialize the uORB subscriptions using an array.
Definition: FlightTask.cpp:12
Definition: SubscriptionArray.hpp:46
virtual void reActivate()
Call this to reset an active Flight Task.
Definition: FlightTask.cpp:35
virtual void updateParams()
Call this method whenever the module gets a parameter change notification.
Definition: px4_module_params.h:79
hrt_abstime _time_stamp_current
time stamp at the beginning of the current task update
Definition: FlightTask.hpp:208
matrix::Vector3f _position_setpoint
Setpoints which the position controller has to execute.
Definition: FlightTask.hpp:225