1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-26 17:55:30 +03:00

Prevent bad data being used during early system startup.

This commit is contained in:
Dominic Clifton 2014-06-23 22:08:29 +01:00
parent e30a373c92
commit 2413130c0f
2 changed files with 31 additions and 15 deletions

View file

@ -389,23 +389,23 @@ void executePeriodicTasks(void)
#ifdef MAG #ifdef MAG
case UPDATE_COMPASS_TASK: case UPDATE_COMPASS_TASK:
if (sensors(SENSOR_MAG)) { if (sensors(SENSOR_MAG)) {
updateCompass(&masterConfig.magZero); updateCompass(&masterConfig.magZero);
} }
break; break;
#endif #endif
#ifdef BARO #ifdef BARO
case UPDATE_BARO_TASK: case UPDATE_BARO_TASK:
if (sensors(SENSOR_BARO)) { if (sensors(SENSOR_BARO)) {
baroUpdate(currentTime); baroUpdate(currentTime);
} }
break; break;
case CALCULATE_ALTITUDE_TASK: case CALCULATE_ALTITUDE_TASK:
if (sensors(SENSOR_BARO) && isBaroReady()) { if (sensors(SENSOR_BARO) && isBaroReady()) {
calculateEstimatedAltitude(currentTime); calculateEstimatedAltitude(currentTime);
} }
break; break;
#endif #endif
case UPDATE_GPS_TASK: case UPDATE_GPS_TASK:
@ -426,7 +426,7 @@ void executePeriodicTasks(void)
} }
if (periodicTaskIndex >= PERIODIC_TASK_COUNT) { if (periodicTaskIndex >= PERIODIC_TASK_COUNT) {
periodicTaskIndex = 0; periodicTaskIndex = 0;
} }
} }
@ -515,12 +515,6 @@ void processRx(void)
LED1_OFF; LED1_OFF;
} }
#ifdef BARO
if (sensors(SENSOR_BARO)) {
updateAltHoldState();
}
#endif
#ifdef MAG #ifdef MAG
if (sensors(SENSOR_ACC) || sensors(SENSOR_MAG)) { if (sensors(SENSOR_ACC) || sensors(SENSOR_MAG)) {
if (rcOptions[BOXMAG]) { if (rcOptions[BOXMAG]) {
@ -562,14 +556,24 @@ void processRx(void)
void loop(void) void loop(void)
{ {
static uint32_t loopTime; static uint32_t loopTime;
static bool haveProcessedAnnexCodeOnce = false;
updateRx(); updateRx();
if (shouldProcessRx(currentTime)) { if (shouldProcessRx(currentTime)) {
processRx(); processRx();
#ifdef BARO
// the 'annexCode' initialses rcCommand, updateAltHoldState depends on valid rcCommand data.
if (haveProcessedAnnexCodeOnce) {
if (sensors(SENSOR_BARO)) {
updateAltHoldState();
}
}
#endif
} else { } else {
// not in rc loop // not processing rx this iteration
executePeriodicTasks(); executePeriodicTasks();
} }
currentTime = micros(); currentTime = micros();
@ -584,6 +588,7 @@ void loop(void)
previousTime = currentTime; previousTime = currentTime;
annexCode(); annexCode();
haveProcessedAnnexCodeOnce = true;
updateAutotuneState(); updateAutotuneState();
@ -598,6 +603,7 @@ void loop(void)
if (f.BARO_MODE) { if (f.BARO_MODE) {
updateAltHold(); updateAltHold();
} }
debug[0] = rcCommand[THROTTLE];
} }
#endif #endif

View file

@ -202,11 +202,21 @@ uint16_t calculateNonDataDrivenChannel(uint8_t chan, uint16_t sample)
{ {
static int16_t rcSamples[MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT][PPM_AND_PWM_SAMPLE_COUNT]; static int16_t rcSamples[MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT][PPM_AND_PWM_SAMPLE_COUNT];
static int16_t rcDataMean[MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT]; static int16_t rcDataMean[MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT];
static bool rxSamplesCollected = false;
uint8_t currentSampleIndex = rcSampleIndex % PPM_AND_PWM_SAMPLE_COUNT; uint8_t currentSampleIndex = rcSampleIndex % PPM_AND_PWM_SAMPLE_COUNT;
// update the recent samples and compute the average of them // update the recent samples and compute the average of them
rcSamples[chan][currentSampleIndex] = sample; rcSamples[chan][currentSampleIndex] = sample;
// avoid returning an incorrect average which would otherwise occur before enough samples
if (!rxSamplesCollected) {
if (rcSampleIndex < PPM_AND_PWM_SAMPLE_COUNT) {
return sample;
}
rxSamplesCollected = true;
}
rcDataMean[chan] = 0; rcDataMean[chan] = 0;
uint8_t sampleIndex; uint8_t sampleIndex;