1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-19 14:25:20 +03:00

de-duplicate failsafe code. extract failsafe code to seperate files.

remove dependency on board.h and mw.h on a few files.

Moved rx configuration paramaters into rxConfig in order to remove the
dependency on config_t from the sbus rx code  - sumd/spektrum to follow.

Avoided use of YAW/PITCH/ROLL in some files since those constants are
from an unrelated enum type.

Replaced some magic numbers with constants to remove comments and
improve code readability.

Note, due to the tight coupling and global variable usage it was
difficult to create a smaller commit.
This commit is contained in:
Dominic Clifton 2014-04-17 23:02:22 +01:00
parent d7eb416aa9
commit 0f02e12f40
26 changed files with 606 additions and 437 deletions

73
src/failsafe.c Normal file
View file

@ -0,0 +1,73 @@
#include <stdbool.h>
#include <stdint.h>
#include "rx_common.h"
#include "drivers/serial_common.h" // FIXME this file should not have a dependency on serial ports, see core_t from runtime_config.h
#include "flight_mixer.h" // FIXME this file should not have a dependency on the flight mixer, see config_t, servoParam_t, etc from config_storage.h
#include "axis.h" // FIXME this file should not have a dependency axis
#include "flight_common.h" // FIXME this file should not have a dependency on the flight common, see config_t from config_storage.h
#include "sensors_common.h" // FIXME this file should not have a dependency on the sensors, see sensor_align_e from config_storage.h
#include "boardalignment.h" // FIXME this file should not have a dependency on board alignment
#include "battery.h" // FIXME this file should not have a dependency on battery, see batteryConfig_t from config_storage.h
#include "runtime_config.h"
#include "config.h"
#include "config_storage.h"
int16_t failsafeCnt = 0;
int16_t failsafeEvents = 0;
bool isFailsafeIdle(void)
{
return failsafeCnt == 0;
}
bool hasFailsafeTimerElapsed(void)
{
return failsafeCnt > (5 * cfg.failsafe_delay);
}
bool shouldFailsafeForceLanding(bool armed)
{
return hasFailsafeTimerElapsed() && armed;
}
bool shouldFailsafeHaveCausedLandingByNow(void)
{
return failsafeCnt > 5 * (cfg.failsafe_delay + cfg.failsafe_off_delay);
}
void failsafeAvoidRearm(void)
{
mwDisarm(); // This will prevent the automatic rearm if failsafe shuts it down and prevents
f.OK_TO_ARM = 0; // to restart accidently by just reconnect to the tx - you will have to switch off first to rearm
}
void updateFailsafeState(void)
{
uint8_t i;
if (!feature(FEATURE_FAILSAFE)) {
return;
}
if (hasFailsafeTimerElapsed()) {
if (shouldFailsafeForceLanding(f.ARMED)) { // Stabilize, and set Throttle to specified level
for (i = 0; i < 3; i++) {
rcData[i] = mcfg.rxConfig.midrc; // after specified guard time after RC signal is lost (in 0.1sec)
}
rcData[THROTTLE] = cfg.failsafe_throttle;
failsafeEvents++;
}
if (shouldFailsafeHaveCausedLandingByNow() || !f.ARMED) {
failsafeAvoidRearm();
}
}
failsafeCnt++;
}