mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-24 00:35:39 +03:00
Track state execution time for OSD, baro, rx and GPS tasks and inform scheduler of next state execution time
This commit is contained in:
parent
a63172cc1f
commit
ab1baccc66
44 changed files with 1392 additions and 721 deletions
|
@ -113,6 +113,9 @@
|
|||
|
||||
#include "tasks.h"
|
||||
|
||||
// Add a margin to the task duration estimation
|
||||
#define RX_TASK_MARGIN 5
|
||||
|
||||
static void taskMain(timeUs_t currentTimeUs)
|
||||
{
|
||||
UNUSED(currentTimeUs);
|
||||
|
@ -159,42 +162,53 @@ static void taskUpdateAccelerometer(timeUs_t currentTimeUs)
|
|||
}
|
||||
#endif
|
||||
|
||||
static enum {
|
||||
CHECK, PROCESS, MODES, UPDATE
|
||||
} rxState = CHECK;
|
||||
typedef enum {
|
||||
RX_STATE_CHECK,
|
||||
RX_STATE_PROCESS,
|
||||
RX_STATE_MODES,
|
||||
RX_STATE_UPDATE,
|
||||
RX_STATE_COUNT
|
||||
} rxState_e;
|
||||
|
||||
static rxState_e rxState = RX_STATE_CHECK;
|
||||
|
||||
bool taskUpdateRxMainInProgress()
|
||||
{
|
||||
return (rxState != CHECK);
|
||||
return (rxState != RX_STATE_CHECK);
|
||||
}
|
||||
|
||||
static void taskUpdateRxMain(timeUs_t currentTimeUs)
|
||||
{
|
||||
// Where we are using a state machine call ignoreTaskStateTime() for all states bar one
|
||||
if (rxState != MODES) {
|
||||
ignoreTaskStateTime();
|
||||
static timeUs_t rxStateDurationUs[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();
|
||||
}
|
||||
|
||||
switch (rxState) {
|
||||
default:
|
||||
case CHECK:
|
||||
rxState = PROCESS;
|
||||
case RX_STATE_CHECK:
|
||||
rxState = RX_STATE_PROCESS;
|
||||
break;
|
||||
|
||||
case PROCESS:
|
||||
case RX_STATE_PROCESS:
|
||||
if (!processRx(currentTimeUs)) {
|
||||
rxState = CHECK;
|
||||
rxState = RX_STATE_CHECK;
|
||||
break;
|
||||
}
|
||||
rxState = MODES;
|
||||
rxState = RX_STATE_MODES;
|
||||
break;
|
||||
|
||||
case MODES:
|
||||
case RX_STATE_MODES:
|
||||
processRxModes(currentTimeUs);
|
||||
rxState = UPDATE;
|
||||
rxState = RX_STATE_UPDATE;
|
||||
break;
|
||||
|
||||
case UPDATE:
|
||||
case RX_STATE_UPDATE:
|
||||
// updateRcCommands sets rcCommand, which is needed by updateAltHoldState and updateSonarAltHoldState
|
||||
updateRcCommands();
|
||||
updateArmingStatus();
|
||||
|
@ -204,9 +218,26 @@ static void taskUpdateRxMain(timeUs_t currentTimeUs)
|
|||
sendRcDataToHid();
|
||||
}
|
||||
#endif
|
||||
rxState = CHECK;
|
||||
rxState = RX_STATE_CHECK;
|
||||
break;
|
||||
}
|
||||
|
||||
if (getIgnoreTaskExecTime()) {
|
||||
return;
|
||||
}
|
||||
|
||||
executeTimeUs = micros() - currentTimeUs;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
rxStateDurationUs[oldRxState] += executeTimeUs - existingDurationUs;
|
||||
|
||||
schedulerSetNextStateTime((rxStateDurationUs[rxState] / TASK_STATS_MOVING_SUM_COUNT) + RX_TASK_MARGIN);
|
||||
}
|
||||
|
||||
|
||||
|
@ -216,7 +247,7 @@ static void taskUpdateBaro(timeUs_t currentTimeUs)
|
|||
UNUSED(currentTimeUs);
|
||||
|
||||
if (sensors(SENSOR_BARO)) {
|
||||
const uint32_t newDeadline = baroUpdate();
|
||||
const uint32_t newDeadline = baroUpdate(currentTimeUs);
|
||||
if (newDeadline != 0) {
|
||||
rescheduleTask(TASK_SELF, newDeadline);
|
||||
}
|
||||
|
@ -370,7 +401,7 @@ void tasksInit(void)
|
|||
#endif
|
||||
|
||||
#ifdef USE_OSD
|
||||
rescheduleTask(TASK_OSD, TASK_PERIOD_HZ(osdConfig()->task_frequency));
|
||||
rescheduleTask(TASK_OSD, TASK_PERIOD_HZ(osdConfig()->framerate_hz));
|
||||
setTaskEnabled(TASK_OSD, featureIsEnabled(FEATURE_OSD) && osdGetDisplayPort(NULL));
|
||||
#endif
|
||||
|
||||
|
@ -478,7 +509,7 @@ task_t tasks[TASK_COUNT] = {
|
|||
#endif
|
||||
|
||||
#ifdef USE_OSD
|
||||
[TASK_OSD] = DEFINE_TASK("OSD", NULL, NULL, osdUpdate, TASK_PERIOD_HZ(OSD_TASK_FREQUENCY_DEFAULT), TASK_PRIORITY_LOW),
|
||||
[TASK_OSD] = DEFINE_TASK("OSD", NULL, osdUpdateCheck, osdUpdate, TASK_PERIOD_HZ(OSD_FRAMERATE_DEFAULT_HZ), TASK_PRIORITY_LOW),
|
||||
#endif
|
||||
|
||||
#ifdef USE_TELEMETRY
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue