1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-16 21:05:35 +03:00

Tidy RX failsafe code

This commit is contained in:
Martin Budden 2017-03-24 10:39:34 +00:00
parent 52c629751c
commit 5cd6627fb2
2 changed files with 27 additions and 72 deletions

View file

@ -79,10 +79,8 @@ static bool rxDataProcessingRequired = false;
static bool auxiliaryProcessingRequired = false;
static bool rxSignalReceived = false;
static bool rxSignalReceivedNotDataDriven = false;
static bool rxFlightChannelsValid = false;
static bool rxIsInFailsafeMode = true;
static bool rxIsInFailsafeModeNotDataDriven = true;
static uint8_t rxChannelCount;
static timeUs_t rxNextUpdateAtUs = 0;
@ -213,34 +211,12 @@ static bool nullProcessFrame(const rxRuntimeConfig_t *rxRuntimeConfig)
return true;
}
#define REQUIRED_CHANNEL_MASK 0x0F // first 4 channels
static uint8_t validFlightChannelMask;
STATIC_UNIT_TESTED void rxResetFlightChannelStatus(void) {
validFlightChannelMask = REQUIRED_CHANNEL_MASK;
}
STATIC_UNIT_TESTED bool rxHaveValidFlightChannels(void)
{
return (validFlightChannelMask == REQUIRED_CHANNEL_MASK);
}
STATIC_UNIT_TESTED bool isPulseValid(uint16_t pulseDuration)
{
return pulseDuration >= rxConfig()->rx_min_usec &&
pulseDuration <= rxConfig()->rx_max_usec;
}
// pulse duration is in micro seconds (usec)
STATIC_UNIT_TESTED void rxUpdateFlightChannelStatus(uint8_t channel, bool valid)
{
if (channel < NON_AUX_CHANNEL_COUNT && !valid) {
// if signal is invalid - mark channel as BAD
validFlightChannelMask &= ~(1 << channel);
}
}
#ifdef USE_SERIAL_RX
bool serialRxInit(const rxConfig_t *rxConfig, rxRuntimeConfig_t *rxRuntimeConfig)
{
@ -396,11 +372,6 @@ bool rxAreFlightChannelsValid(void)
return rxFlightChannelsValid;
}
static bool isRxDataDriven(void)
{
return !(feature(FEATURE_RX_PARALLEL_PWM | FEATURE_RX_PPM));
}
void suspendRxSignal(void)
{
suspendRxSignalUntil = micros() + SKIP_RC_ON_SUSPEND_PERIOD;
@ -422,22 +393,21 @@ bool rxUpdateCheck(timeUs_t currentTimeUs, timeDelta_t currentDeltaTime)
if (rxSignalReceived) {
if (currentTimeUs >= needRxSignalBefore) {
rxSignalReceived = false;
rxSignalReceivedNotDataDriven = false;
}
}
#if defined(USE_PWM) || defined(USE_PPM)
if (feature(FEATURE_RX_PPM)) {
if (isPPMDataBeingReceived()) {
rxSignalReceivedNotDataDriven = true;
rxIsInFailsafeModeNotDataDriven = false;
rxSignalReceived = true;
rxIsInFailsafeMode = false;
needRxSignalBefore = currentTimeUs + needRxSignalMaxDelayUs;
resetPPMDataReceivedState();
}
} else if (feature(FEATURE_RX_PARALLEL_PWM)) {
if (isPWMDataBeingReceived()) {
rxSignalReceivedNotDataDriven = true;
rxIsInFailsafeModeNotDataDriven = false;
rxSignalReceived = true;
rxIsInFailsafeMode = false;
needRxSignalBefore = currentTimeUs + needRxSignalMaxDelayUs;
}
} else
@ -546,20 +516,11 @@ static void readRxChannelsApplyRanges(void)
}
}
static void detectAndApplySignalLossBehaviour(timeUs_t currentTimeUs)
static void detectAndApplySignalLossBehaviour(void)
{
bool useValueFromRx = true;
const bool rxIsDataDriven = isRxDataDriven();
const uint32_t currentTimeMs = currentTimeUs / 1000;
const uint32_t currentTimeMs = millis();
if (!rxIsDataDriven) {
rxSignalReceived = rxSignalReceivedNotDataDriven;
rxIsInFailsafeMode = rxIsInFailsafeModeNotDataDriven;
}
if (!rxSignalReceived || rxIsInFailsafeMode) {
useValueFromRx = false;
}
const bool useValueFromRx = rxSignalReceived && !rxIsInFailsafeMode;
#ifdef DEBUG_RX_SIGNAL_LOSS
debug[0] = rxSignalReceived;
@ -567,41 +528,37 @@ static void detectAndApplySignalLossBehaviour(timeUs_t currentTimeUs)
debug[2] = rxRuntimeConfig.rcReadRawFn(&rxRuntimeConfig, 0);
#endif
rxResetFlightChannelStatus();
rxFlightChannelsValid = true;
for (int channel = 0; channel < rxChannelCount; channel++) {
uint16_t sample = rcRaw[channel];
uint16_t sample = (useValueFromRx) ? rcRaw[channel] : PPM_RCVR_TIMEOUT;
const bool validPulse = useValueFromRx && isPulseValid(sample);
bool validPulse = isPulseValid(sample);
if (!validPulse) {
if (currentTimeMs < rcInvalidPulsPeriod[channel]) {
sample = rcData[channel]; // hold channel for MAX_INVALID_PULS_TIME
if (validPulse) {
rcInvalidPulsPeriod[channel] = currentTimeMs + MAX_INVALID_PULS_TIME;
} else {
if (cmp32(currentTimeMs, rcInvalidPulsPeriod[channel]) < 0) {
continue; // skip to next channel to hold channel value MAX_INVALID_PULS_TIME
} else {
sample = getRxfailValue(channel); // after that apply rxfail value
rxUpdateFlightChannelStatus(channel, validPulse);
if (channel < NON_AUX_CHANNEL_COUNT) {
rxFlightChannelsValid = false;
}
}
} else {
rcInvalidPulsPeriod[channel] = currentTimeMs + MAX_INVALID_PULS_TIME;
}
if (rxIsDataDriven) {
rcData[channel] = sample;
} else {
if (feature(FEATURE_RX_PARALLEL_PWM | FEATURE_RX_PPM)) {
// smooth output for PWM and PPM
rcData[channel] = calculateChannelMovingAverage(channel, sample);
} else {
rcData[channel] = sample;
}
}
rxFlightChannelsValid = rxHaveValidFlightChannels();
if ((rxFlightChannelsValid) && !IS_RC_MODE_ACTIVE(BOXFAILSAFE)) {
if (rxFlightChannelsValid && !IS_RC_MODE_ACTIVE(BOXFAILSAFE)) {
failsafeOnValidDataReceived();
} else {
rxIsInFailsafeMode = rxIsInFailsafeModeNotDataDriven = true;
rxIsInFailsafeMode = true;
failsafeOnValidDataFailed();
for (int channel = 0; channel < rxChannelCount; channel++) {
rcData[channel] = getRxfailValue(channel);
}
@ -635,7 +592,7 @@ bool calculateRxChannelsAndUpdateFailsafe(timeUs_t currentTimeUs)
}
readRxChannelsApplyRanges();
detectAndApplySignalLossBehaviour(currentTimeUs);
detectAndApplySignalLossBehaviour();
rcSampleIndex++;

View file

@ -35,10 +35,7 @@ extern "C" {
boxBitmask_t rcModeActivationMask;
void rxResetFlightChannelStatus(void);
bool rxHaveValidFlightChannels(void);
bool isPulseValid(uint16_t pulseDuration);
void rxUpdateFlightChannelStatus(uint8_t channel, uint16_t pulseDuration);
PG_RESET_TEMPLATE(featureConfig_t, featureConfig,
.enabledFeatures = 0
@ -57,7 +54,8 @@ typedef struct testData_s {
static testData_t testData;
TEST(RxTest, TestValidFlightChannelsLowArm)
#if 0 //!! valid pulse handling has changed so these test now test removed functions
TEST(RxTest, TestValidFlightChannels)
{
// given
memset(&testData, 0, sizeof(testData));
@ -189,7 +187,7 @@ TEST(RxTest, TestInvalidFlightChannels)
EXPECT_FALSE(rxHaveValidFlightChannels());
}
}
#endif
// STUBS