Updated and Validated
[betaflight.git] / src / main / flight / failsafe.h
blob31c5b01b7e1133d481f946662e399527169cbc6c
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 10 // millis
32 #define PERIOD_RXDATA_RECOVERY 200 // millis
34 typedef struct failsafeConfig_s {
35 uint16_t failsafe_throttle; // Throttle level used for landing - specify value between 1000..2000 (pwm pulse width for slightly below hover). center throttle = 1500.
36 uint16_t failsafe_throttle_low_delay; // Time throttle stick must have been below 'min_check' to "JustDisarm" instead of "full failsafe procedure".
37 uint8_t failsafe_delay; // Guard time for failsafe activation after signal lost. 1 step = 0.1sec - 1sec in example (10)
38 uint8_t failsafe_off_delay; // Time for Landing before motors stop in 0.1sec. 1 step = 0.1sec - 20sec in example (200)
39 uint8_t failsafe_switch_mode; // failsafe switch action is 0: Stage 1, 1: Disarms instantly, 2: Stage 2
40 uint8_t failsafe_procedure; // selected full failsafe procedure is 0: auto-landing, 1: Drop it
41 uint16_t failsafe_recovery_delay; // Time (in 0.1sec) of valid rx data (min 200ms PERIOD_RXDATA_RECOVERY) to allow recovering from failsafe procedure
42 uint8_t failsafe_stick_threshold; // Stick deflection percentage to exit GPS Rescue procedure
43 } failsafeConfig_t;
45 PG_DECLARE(failsafeConfig_t, failsafeConfig);
47 typedef enum {
48 FAILSAFE_IDLE = 0,
49 FAILSAFE_RX_LOSS_DETECTED,
50 FAILSAFE_LANDING,
51 FAILSAFE_LANDED,
52 FAILSAFE_RX_LOSS_MONITORING,
53 FAILSAFE_RX_LOSS_RECOVERED,
54 FAILSAFE_GPS_RESCUE
55 } failsafePhase_e;
57 typedef enum {
58 FAILSAFE_RXLINK_DOWN = 0,
59 FAILSAFE_RXLINK_UP
60 } failsafeRxLinkState_e;
62 typedef enum {
63 FAILSAFE_PROCEDURE_AUTO_LANDING = 0,
64 FAILSAFE_PROCEDURE_DROP_IT,
65 #ifdef USE_GPS_RESCUE
66 FAILSAFE_PROCEDURE_GPS_RESCUE,
67 #endif
68 FAILSAFE_PROCEDURE_COUNT // must be last
69 } failsafeProcedure_e;
71 extern const char * const failsafeProcedureNames[FAILSAFE_PROCEDURE_COUNT];
73 typedef enum {
74 FAILSAFE_SWITCH_MODE_STAGE1 = 0,
75 FAILSAFE_SWITCH_MODE_KILL,
76 FAILSAFE_SWITCH_MODE_STAGE2
77 } failsafeSwitchMode_e;
79 typedef struct failsafeState_s {
80 int16_t events;
81 bool monitoring;
82 bool active;
83 uint32_t rxDataFailurePeriod;
84 uint32_t rxDataRecoveryPeriod;
85 uint32_t validRxDataReceivedAt;
86 uint32_t validRxDataFailedAt;
87 uint32_t throttleLowPeriod; // throttle stick must have been below 'min_check' for this period
88 uint32_t landingShouldBeFinishedAt;
89 uint32_t receivingRxDataPeriod; // period for the required period of valid rxData
90 uint32_t receivingRxDataPeriodPreset; // preset for the required period of valid rxData
91 failsafePhase_e phase;
92 failsafeRxLinkState_e rxLinkState;
93 } failsafeState_t;
95 void failsafeInit(void);
96 void failsafeReset(void);
98 void failsafeStartMonitoring(void);
99 void failsafeUpdateState(void);
101 failsafePhase_e failsafePhase(void);
102 bool failsafeIsMonitoring(void);
103 bool failsafeIsActive(void);
104 bool failsafeIsReceivingRxData(void);
105 void failsafeCheckDataFailurePeriod(void);
106 void failsafeOnRxSuspend(uint32_t suspendPeriod);
107 void failsafeOnRxResume(void);
108 void failsafeOnValidDataReceived(void);
109 void failsafeOnValidDataFailed(void);