1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-21 15:25:36 +03:00

Use peak tracking as default for task duration estimation rather than a moving average

This commit is contained in:
Steve Evans 2020-08-14 16:42:20 +01:00 committed by Steve Evans
parent bfd597a449
commit 29d221502e
23 changed files with 100 additions and 91 deletions

View file

@ -113,8 +113,11 @@
#include "tasks.h"
// taskUpdateRxMain() has occasional peaks in execution time so normal moving average duration estimation doesn't work
// Decay the estimated max task duration by 1/(1 << RX_TASK_DECAY_SHIFT) on every invocation
#define RX_TASK_DECAY_SHIFT 5
// Add a margin to the task duration estimation
#define RX_TASK_MARGIN 5
#define RX_TASK_MARGIN 1
static void taskMain(timeUs_t currentTimeUs)
{
@ -179,14 +182,13 @@ bool taskUpdateRxMainInProgress()
static void taskUpdateRxMain(timeUs_t currentTimeUs)
{
static timeUs_t rxStateDurationUs[RX_STATE_COUNT];
static timeUs_t rxStateDurationFracUs[RX_STATE_COUNT];
timeUs_t executeTimeUs;
timeUs_t existingDurationUs;
rxState_e oldRxState = rxState;
// Where we are using a state machine call schedulerIgnoreTaskExecRate() for all states bar one
if (rxState != RX_STATE_UPDATE) {
ignoreTaskExecRate();
schedulerIgnoreTaskExecRate();
}
switch (rxState) {
@ -222,22 +224,20 @@ static void taskUpdateRxMain(timeUs_t currentTimeUs)
break;
}
if (getIgnoreTaskExecTime()) {
if (schedulerGetIgnoreTaskExecTime()) {
return;
}
executeTimeUs = micros() - currentTimeUs;
executeTimeUs = micros() - currentTimeUs + RX_TASK_MARGIN;
existingDurationUs = rxStateDurationUs[oldRxState] / TASK_STATS_MOVING_SUM_COUNT;
// If the execution time is higher than expected, double the weight in the moving average
if (executeTimeUs > existingDurationUs) {
rxStateDurationUs[oldRxState] += executeTimeUs - existingDurationUs;
if (executeTimeUs > (rxStateDurationFracUs[oldRxState] >> RX_TASK_DECAY_SHIFT)) {
rxStateDurationFracUs[oldRxState] = executeTimeUs << RX_TASK_DECAY_SHIFT;
} else {
// Slowly decay the max time
rxStateDurationFracUs[oldRxState]--;
}
rxStateDurationUs[oldRxState] += executeTimeUs - existingDurationUs;
schedulerSetNextStateTime((rxStateDurationUs[rxState] / TASK_STATS_MOVING_SUM_COUNT) + RX_TASK_MARGIN);
schedulerSetNextStateTime(rxStateDurationFracUs[rxState] >> RX_TASK_DECAY_SHIFT);
}