OP-1900 added deceleration check to autotakeoff failsafe
[librepilot.git] / flight / modules / PathFollower / inc / vtollandfsm.h
blob4c582a00aa86b6526d3541e715f9c5959f1ed296
1 /**
2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
4 * @{
5 * @addtogroup PathFollower FSM
6 * @brief Executes landing sequence via an FSM
7 * @{
9 * @file vtollandfsm.h
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
11 * @brief Executes FSM for landing sequence
13 * @see The GNU Public License (GPL) Version 3
15 *****************************************************************************/
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #ifndef VTOLLANDFSM_H
32 #define VTOLLANDFSM_H
34 extern "C" {
35 #include "statusvtolland.h"
37 #include "pathfollowerfsm.h"
39 // Landing state machine
40 typedef enum {
41 LAND_STATE_INACTIVE = 0, // Inactive state is the initialised state on startup
42 LAND_STATE_INIT_ALTHOLD, // Initiate altitude hold before starting descent
43 LAND_STATE_WTG_FOR_DESCENTRATE, // Waiting for attainment of landing descent rate
44 LAND_STATE_AT_DESCENTRATE, // Landing descent rate achieved
45 LAND_STATE_WTG_FOR_GROUNDEFFECT, // Waiting for group effect to be detected
46 LAND_STATE_GROUNDEFFECT, // Ground effect detected
47 LAND_STATE_THRUSTDOWN, // Thrust down sequence
48 LAND_STATE_THRUSTOFF, // Thrust is now off
49 LAND_STATE_DISARMED, // Disarmed
50 LAND_STATE_SIZE
51 } PathFollowerFSM_LandState_T;
53 class VtolLandFSM : public PathFollowerFSM {
54 private:
55 static VtolLandFSM *p_inst;
56 VtolLandFSM();
58 public:
59 static VtolLandFSM *instance()
61 if (!p_inst) {
62 p_inst = new VtolLandFSM();
64 return p_inst;
66 int32_t Initialize(VtolPathFollowerSettingsData *vtolPathFollowerSettings,
67 PathDesiredData *pathDesired,
68 FlightStatusData *flightStatus);
69 void Inactive(void);
70 void Activate(void);
71 void Update(void);
72 void BoundThrust(float &ulow, float &uhigh);
73 PathFollowerFSMState_T GetCurrentState(void);
74 void ConstrainStabiDesired(StabilizationDesiredData *stabDesired);
75 float BoundVelocityDown(float);
76 void CheckPidScaler(pid_scaler *scaler);
78 protected:
80 // FSM instance data type
81 typedef struct {
82 StatusVtolLandData fsmLandStatus;
83 StatusVtolLandStateOptions currentState;
84 TakeOffLocationData takeOffLocation;
85 uint32_t stateRunCount;
86 uint32_t stateTimeoutCount;
87 float sum1;
88 float sum2;
89 float expectedLandPositionNorth;
90 float expectedLandPositionEast;
91 float thrustLimit;
92 float boundThrustMin;
93 float boundThrustMax;
94 uint8_t observationCount;
95 uint8_t observation2Count;
96 uint8_t flZeroStabiHorizontal;
97 uint8_t flConstrainThrust;
98 uint8_t flLowAltitude;
99 uint8_t flAltitudeHold;
100 } VtolLandFSMData_T;
102 // FSM state structure
103 typedef struct {
104 void(VtolLandFSM::*setup) (void); // Called to initialise the state
105 void(VtolLandFSM::*run) (uint8_t); // Run the event detection code for a state
106 } PathFollowerFSM_LandStateHandler_T;
108 // Private variables
109 VtolLandFSMData_T *mLandData;
110 VtolPathFollowerSettingsData *vtolPathFollowerSettings;
111 PathDesiredData *pathDesired;
112 FlightStatusData *flightStatus;
114 void setup_inactive(void);
115 void setup_init_althold(void);
116 void setup_wtg_for_descentrate(void);
117 void setup_at_descentrate(void);
118 void setup_wtg_for_groundeffect(void);
119 void run_init_althold(uint8_t);
120 void run_wtg_for_descentrate(uint8_t);
121 void run_at_descentrate(uint8_t);
122 void run_wtg_for_groundeffect(uint8_t);
123 void setup_groundeffect(void);
124 void run_groundeffect(uint8_t);
125 void setup_thrustdown(void);
126 void run_thrustdown(uint8_t);
127 void setup_thrustoff(void);
128 void run_thrustoff(uint8_t);
129 void setup_disarmed(void);
130 void run_disarmed(uint8_t);
131 void initFSM(void);
132 void setState(StatusVtolLandStateOptions newState, StatusVtolLandStateExitReasonOptions reason);
133 int32_t runState();
134 int32_t runAlways();
135 void updateVtolLandFSMStatus();
136 void assessAltitude(void);
138 void setStateTimeout(int32_t count);
140 static PathFollowerFSM_LandStateHandler_T sLandStateTable[LAND_STATE_SIZE];
143 #endif // VTOLLANDFSM_H