Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / flight / failsafe.h
blob547d74b8cda1c87f3b93681fefd11a2a9c0ff12a
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #pragma once
23 #include "pg/pg.h"
25 #define FAILSAFE_POWER_ON_DELAY_US (1000 * 1000 * 5)
26 #define MILLIS_PER_TENTH_SECOND 100
27 #define MILLIS_PER_SECOND 1000
28 #define PERIOD_OF_1_SECONDS 1 * MILLIS_PER_SECOND
29 #define PERIOD_OF_3_SECONDS 3 * MILLIS_PER_SECOND
30 #define PERIOD_OF_30_SECONDS 30 * MILLIS_PER_SECOND
31 #define PERIOD_RXDATA_FAILURE 200 // millis
32 #define PERIOD_RXDATA_RECOVERY 200 // millis
35 typedef struct failsafeConfig_s {
36 uint16_t failsafe_throttle; // Throttle level used for landing - specify value between 1000..2000 (pwm pulse width for slightly below hover). center throttle = 1500.
37 uint16_t failsafe_throttle_low_delay; // Time throttle stick must have been below 'min_check' to "JustDisarm" instead of "full failsafe procedure".
38 uint8_t failsafe_delay; // Guard time for failsafe activation after signal lost. 1 step = 0.1sec - 1sec in example (10)
39 uint8_t failsafe_off_delay; // Time for Landing before motors stop in 0.1sec. 1 step = 0.1sec - 20sec in example (200)
40 uint8_t failsafe_switch_mode; // failsafe switch action is 0: stage1 (identical to rc link loss), 1: disarms instantly, 2: stage2
41 uint8_t failsafe_procedure; // selected full failsafe procedure is 0: auto-landing, 1: Drop it
42 uint16_t failsafe_recovery_delay; // Time (in 0.1sec) of valid rx data (plus 200ms) needed to allow recovering from failsafe procedure
43 uint8_t failsafe_stick_threshold; // Stick deflection percentage to exit GPS Rescue procedure
44 } failsafeConfig_t;
46 PG_DECLARE(failsafeConfig_t, failsafeConfig);
48 typedef enum {
49 FAILSAFE_IDLE = 0,
50 FAILSAFE_RX_LOSS_DETECTED,
51 FAILSAFE_LANDING,
52 FAILSAFE_LANDED,
53 FAILSAFE_RX_LOSS_MONITORING,
54 FAILSAFE_RX_LOSS_RECOVERED,
55 FAILSAFE_GPS_RESCUE
56 } failsafePhase_e;
58 typedef enum {
59 FAILSAFE_RXLINK_DOWN = 0,
60 FAILSAFE_RXLINK_UP
61 } failsafeRxLinkState_e;
63 typedef enum {
64 FAILSAFE_PROCEDURE_AUTO_LANDING = 0,
65 FAILSAFE_PROCEDURE_DROP_IT,
66 #ifdef USE_GPS_RESCUE
67 FAILSAFE_PROCEDURE_GPS_RESCUE,
68 #endif
69 FAILSAFE_PROCEDURE_COUNT // must be last
70 } failsafeProcedure_e;
72 extern const char * const failsafeProcedureNames[FAILSAFE_PROCEDURE_COUNT];
74 typedef enum {
75 FAILSAFE_SWITCH_MODE_STAGE1 = 0,
76 FAILSAFE_SWITCH_MODE_KILL,
77 FAILSAFE_SWITCH_MODE_STAGE2
78 } failsafeSwitchMode_e;
80 typedef struct failsafeState_s {
81 int16_t events;
82 bool monitoring;
83 bool active;
84 uint32_t rxDataFailurePeriod;
85 uint32_t rxDataRecoveryPeriod;
86 uint32_t validRxDataReceivedAt;
87 uint32_t validRxDataFailedAt;
88 uint32_t throttleLowPeriod; // throttle stick must have been below 'min_check' for this period
89 uint32_t landingShouldBeFinishedAt;
90 uint32_t receivingRxDataPeriod; // period for the required period of valid rxData
91 uint32_t receivingRxDataPeriodPreset; // preset for the required period of valid rxData
92 failsafePhase_e phase;
93 failsafeRxLinkState_e rxLinkState;
94 } failsafeState_t;
96 void failsafeInit(void);
97 void failsafeReset(void);
99 void failsafeStartMonitoring(void);
100 void failsafeUpdateState(void);
102 failsafePhase_e failsafePhase(void);
103 bool failsafeIsMonitoring(void);
104 bool failsafeIsActive(void);
105 bool failsafeIsReceivingRxData(void);
106 void failsafeOnRxSuspend(uint32_t suspendPeriod);
107 void failsafeOnRxResume(void);
109 void failsafeOnValidDataReceived(void);
110 void failsafeOnValidDataFailed(void);