PX4 Firmware
PX4 Autopilot Software http://px4.io
FlightTaskManualAltitude.hpp
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 FlightManualAltitude.hpp
36  *
37  * Flight task for manual controlled altitude.
38  */
39 
40 #pragma once
41 
42 #include "FlightTaskManual.hpp"
44 
46 {
47 public:
48  FlightTaskManualAltitude() = default;
49  virtual ~FlightTaskManualAltitude() = default;
50  bool activate(vehicle_local_position_setpoint_s last_setpoint) override;
51  bool updateInitialize() override;
52  bool update() override;
53 
54 protected:
55  void _updateHeadingSetpoints(); /**< sets yaw or yaw speed */
56  void _ekfResetHandlerHeading(float delta_psi) override; /**< adjust heading setpoint in case of EKF reset event */
57  virtual void _updateSetpoints(); /**< updates all setpoints */
58  virtual void _scaleSticks(); /**< scales sticks to velocity in z */
59  bool _checkTakeoff() override;
60 
61  /**
62  * rotates vector into local frame
63  */
65 
66  /**
67  * Check and sets for position lock.
68  * If sticks are at center position, the vehicle
69  * will exit velocity control and enter position control.
70  */
71  void _updateAltitudeLock();
72 
73  DEFINE_PARAMETERS_CUSTOM_PARENT(FlightTaskManual,
74  (ParamFloat<px4::params::MPC_HOLD_MAX_Z>) _param_mpc_hold_max_z,
75  (ParamInt<px4::params::MPC_ALT_MODE>) _param_mpc_alt_mode,
76  (ParamFloat<px4::params::MPC_HOLD_MAX_XY>) _param_mpc_hold_max_xy,
77  (ParamFloat<px4::params::MPC_Z_P>) _param_mpc_z_p, /**< position controller altitude propotional gain */
78  (ParamFloat<px4::params::MPC_MAN_Y_MAX>) _param_mpc_man_y_max, /**< scaling factor from stick to yaw rate */
79  (ParamFloat<px4::params::MPC_MAN_Y_TAU>) _param_mpc_man_y_tau,
80  (ParamFloat<px4::params::MPC_MAN_TILT_MAX>) _param_mpc_man_tilt_max, /**< maximum tilt allowed for manual flight */
81  (ParamFloat<px4::params::MPC_LAND_ALT1>) _param_mpc_land_alt1, /**< altitude at which to start downwards slowdown */
82  (ParamFloat<px4::params::MPC_LAND_ALT2>) _param_mpc_land_alt2, /**< altitude below wich to land with land speed */
83  (ParamFloat<px4::params::MPC_LAND_SPEED>)
84  _param_mpc_land_speed, /**< desired downwards speed when approaching the ground */
85  (ParamFloat<px4::params::MPC_TKO_SPEED>)
86  _param_mpc_tko_speed /**< desired upwards speed when still close to the ground */
87  )
88 private:
89  bool _isYawInput();
90  void _unlockYaw();
91  void _lockYaw();
92 
93  /**
94  * Filter between stick input and yaw setpoint
95  *
96  * @param yawspeed_target yaw setpoint desired by the stick
97  * @return filtered value from independent filter state
98  */
99  float _applyYawspeedFilter(float yawspeed_target);
100 
101  /**
102  * Terrain following.
103  * During terrain following, the position setpoint is adjusted
104  * such that the distance to ground is kept constant.
105  * @param apply_brake is true if user wants to break
106  * @param stopped is true if vehicle has stopped moving in D-direction
107  */
108  void _terrainFollowing(bool apply_brake, bool stopped);
109 
110  /**
111  * Minimum Altitude during range sensor operation.
112  * If a range sensor is used for altitude estimates, for
113  * best operation a minimum altitude is required. The minimum
114  * altitude is only enforced during altitude lock.
115  */
116  void _respectMinAltitude();
117 
118  void _respectMaxAltitude();
119 
120  /**
121  * Sets downwards velocity constraint based on the distance to ground.
122  * To ensure a slowdown to land speed before hitting the ground.
123  */
124  void _respectGroundSlowdown();
125 
127 
128  float _yawspeed_filter_state{}; /**< state of low-pass filter in rad/s */
129  uint8_t _reset_counter = 0; /**< counter for estimator resets in z-direction */
130  float _max_speed_up = 10.0f;
131  float _min_speed_down = 1.0f;
132  bool _terrain_follow{false}; /**< true when the vehicle is following the terrain height */
133  bool _terrain_hold{false}; /**< true when vehicle is controlling height above a static ground position */
134 
135  /**
136  * Distance to ground during terrain following.
137  * If user does not demand velocity change in D-direction and the vehcile
138  * is in terrain-following mode, then height to ground will be locked to
139  * _dist_to_ground_lock.
140  */
141  float _dist_to_ground_lock = NAN;
142 };
bool _checkTakeoff() override
Determine when to trigger a takeoff (ignored in flight)
bool updateInitialize() override
Call before activate() or update() to initialize time and input data.
uint8_t _reset_counter
counter for estimator resets in z-direction
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()
FlightTaskManualAltitude()=default
void _respectMinAltitude()
Minimum Altitude during range sensor operation.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
void _rotateIntoHeadingFrame(matrix::Vector2f &vec)
rotates vector into local frame
virtual ~FlightTaskManualAltitude()=default
void _updateAltitudeLock()
Check and sets for position lock.
void _ekfResetHandlerHeading(float delta_psi) override
adjust heading setpoint in case of EKF reset event
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
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.
uORB::SubscriptionData< home_position_s > _sub_home_position
Linear and exponential map from stick inputs to range -1 and 1.
float _yawspeed_filter_state
state of low-pass filter in rad/s
bool _terrain_hold
true when vehicle is controlling height above a static ground position
void _terrainFollowing(bool apply_brake, bool stopped)
Terrain following.
virtual void _scaleSticks()
scales sticks to velocity in z
float _dist_to_ground_lock
Distance to ground during terrain following.
bool _terrain_follow
true when the vehicle is following the terrain height