Skip to content

Commit

Permalink
#495 Reworked scheduling in task
Browse files Browse the repository at this point in the history
  • Loading branch information
krichardsson committed Nov 3, 2019
1 parent 821e249 commit a500cf3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/config/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ to exclude the API function. */
#define F2T(X) ((unsigned int)((configTICK_RATE_HZ/(X))))
#define T2M(X) ((unsigned int)(X))

// Seconds to OS ticks
#define S2T(X) ((portTickType)((X) * configTICK_RATE_HZ))
#define T2S(X) ((X) / (float)configTICK_RATE_HZ)


// DEBUG SECTION
#define configUSE_APPLICATION_TASK_TAG 1
Expand Down
21 changes: 13 additions & 8 deletions src/modules/src/estimator_kalman.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,17 @@ static void kalmanTask(void* parameters) {
systemWaitStart();

uint32_t lastPrediction = xTaskGetTickCount();
uint32_t nextPrediction = xTaskGetTickCount();
uint32_t lastPNUpdate = xTaskGetTickCount();
uint32_t lastBaroUpdate = xTaskGetTickCount();
uint32_t nextBaroUpdate = xTaskGetTickCount();
uint32_t nextStatsUpdate = 0;

while (true) {
// Wake up when it is time for next prediction, unless we get queued meassurements
uint32_t timeout = (configTICK_RATE_HZ / PREDICT_RATE) - (xTaskGetTickCount() - lastPrediction);
xSemaphoreTake(measurementQueueSemaphore, timeout / portTICK_PERIOD_MS);
uint32_t maxSleepTics = nextPrediction - xTaskGetTickCount();
if (maxSleepTics > 0) {
xSemaphoreTake(measurementQueueSemaphore, maxSleepTics);
}

// If the client triggers an estimator reset via parameter update
if (coreData.resetEstimation) {
Expand All @@ -314,27 +317,29 @@ static void kalmanTask(void* parameters) {
#endif

// Run the system dynamics to predict the state forward.
if ((osTick - lastPrediction) >= configTICK_RATE_HZ / PREDICT_RATE) { // update at the PREDICT_RATE
float dt = (float)(osTick - lastPrediction) / configTICK_RATE_HZ;
if (osTick >= nextPrediction) { // update at the PREDICT_RATE
float dt = T2S(osTick - lastPrediction);
if (predictStateForward(osTick, dt)) {
lastPrediction = osTick;
doneUpdate = true;
statsCntInc(&statsPredictions);
}

nextPrediction = osTick + S2T(1.0f / PREDICT_RATE);
}

/**
* Add process noise every loop, rather than every prediction
*/
kalmanCoreAddProcessNoise(&coreData, (float)(osTick - lastPNUpdate) / configTICK_RATE_HZ);
kalmanCoreAddProcessNoise(&coreData, T2S(osTick - lastPNUpdate));
lastPNUpdate = osTick;

/**
* Update the state estimate with the barometer measurements
*/
// Accumulate the barometer measurements
if (useBaroUpdate) {
if ((osTick - lastBaroUpdate) >= configTICK_RATE_HZ/BARO_RATE // update at BARO_RATE
if (osTick > nextBaroUpdate // update at BARO_RATE
&& baroAccumulatorCount > 0)
{
xSemaphoreTake(dataMutex, portMAX_DELAY);
Expand All @@ -345,7 +350,7 @@ static void kalmanTask(void* parameters) {

kalmanCoreUpdateWithBaro(&coreData, baroAslAverage, quadIsFlying);

lastBaroUpdate = osTick;
nextBaroUpdate = osTick + S2T(1.0f / BARO_RATE);
doneUpdate = true;

statsCntInc(&statsBaroUpdates);
Expand Down

0 comments on commit a500cf3

Please sign in to comment.