Skip to content

Commit

Permalink
#495 Added deinit functionality to estimators to allow the kalman fil…
Browse files Browse the repository at this point in the history
…ter task to sleep when not used
  • Loading branch information
krichardsson committed Nov 4, 2019
1 parent ab60210 commit 880ba37
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/modules/interface/estimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef enum {

void stateEstimatorInit(StateEstimatorType estimator);
bool stateEstimatorTest(void);
void stateEstimatorSwitchTo(StateEstimatorType estimator);
void stateEstimator(state_t *state, sensorData_t *sensors, control_t *control, const uint32_t tick);
StateEstimatorType getStateEstimator(void);
const char* stateEstimatorGetName();
Expand Down
1 change: 1 addition & 0 deletions src/modules/interface/estimator_kalman.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "stabilizer_types.h"

void estimatorKalmanInit(void);
void estimatorKalmanDeinit();
bool estimatorKalmanTest(void);
void estimatorKalman(state_t *state, sensorData_t *sensors, control_t *control, const uint32_t tick);

Expand Down
42 changes: 31 additions & 11 deletions src/modules/src/estimator.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#define DEFAULT_ESTIMATOR complementaryEstimator
static StateEstimatorType currentEstimator = anyEstimator;

static void initEstimator();
static void initEstimator(const StateEstimatorType estimator);
static void deinitEstimator(const StateEstimatorType estimator);

typedef struct {
void (*init)(void);
void (*deinit)(void);
bool (*test)(void);
void (*update)(state_t *state, sensorData_t *sensors, control_t *control, const uint32_t tick);
const char* name;
Expand All @@ -30,9 +32,10 @@ typedef struct {

static EstimatorFcns estimatorFunctions[] = {
{
.init = 0,
.test = 0,
.update = 0,
.init = NOT_IMPLEMENTED,
.deinit = NOT_IMPLEMENTED,
.test = NOT_IMPLEMENTED,
.update = NOT_IMPLEMENTED,
.name = "None",
.estimatorEnqueueTDOA = NOT_IMPLEMENTED,
.estimatorEnqueuePosition = NOT_IMPLEMENTED,
Expand All @@ -45,6 +48,7 @@ static EstimatorFcns estimatorFunctions[] = {
}, // Any estimator
{
.init = estimatorComplementaryInit,
.deinit = NOT_IMPLEMENTED,
.test = estimatorComplementaryTest,
.update = estimatorComplementary,
.name = "Complementary",
Expand All @@ -59,6 +63,7 @@ static EstimatorFcns estimatorFunctions[] = {
},
{
.init = estimatorKalmanInit,
.deinit = estimatorKalmanDeinit,
.test = estimatorKalmanTest,
.update = estimatorKalman,
.name = "Kalman",
Expand All @@ -75,23 +80,30 @@ static EstimatorFcns estimatorFunctions[] = {


void stateEstimatorInit(StateEstimatorType estimator) {
stateEstimatorSwitchTo(estimator);
}

void stateEstimatorSwitchTo(StateEstimatorType estimator) {
if (estimator < 0 || estimator >= StateEstimatorTypeCount) {
return;
}

currentEstimator = estimator;
StateEstimatorType newEstimator = estimator;

if (anyEstimator == currentEstimator) {
currentEstimator = DEFAULT_ESTIMATOR;
if (anyEstimator == newEstimator) {
newEstimator = DEFAULT_ESTIMATOR;
}

StateEstimatorType forcedEstimator = ESTIMATOR_NAME;
if (forcedEstimator != anyEstimator) {
DEBUG_PRINT("Estimator type forced\n");
currentEstimator = forcedEstimator;
newEstimator = forcedEstimator;
}

initEstimator();
initEstimator(newEstimator);
StateEstimatorType previousEstimator = currentEstimator;
currentEstimator = newEstimator;
deinitEstimator(previousEstimator);

DEBUG_PRINT("Using %s (%d) estimator\n", stateEstimatorGetName(), currentEstimator);
}
Expand All @@ -100,8 +112,16 @@ StateEstimatorType getStateEstimator(void) {
return currentEstimator;
}

static void initEstimator() {
estimatorFunctions[currentEstimator].init();
static void initEstimator(const StateEstimatorType estimator) {
if (estimatorFunctions[estimator].init) {
estimatorFunctions[estimator].init();
}
}

static void deinitEstimator(const StateEstimatorType estimator) {
if (estimatorFunctions[estimator].deinit) {
estimatorFunctions[estimator].deinit();
}
}

bool stateEstimatorTest(void) {
Expand Down
21 changes: 21 additions & 0 deletions src/modules/src/estimator_kalman.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ static kalmanCoreData_t coreData;
*/

static bool isInit = false;
static volatile bool isEstimatorActive = false;

static Axis3f accAccumulator;
static float thrustAccumulator;
static Axis3f gyroAccumulator;
Expand All @@ -234,6 +236,7 @@ static statsCntRate_t statsFinalize;
static statsCntRate_t statsUMeasurementAppended;
static statsCntRate_t statsUMeasurementNotAppended;
static void updateStats(uint32_t now_ms);
static void blockWhileInactive();

#ifdef KALMAN_USE_BARO_UPDATE
static const bool useBaroUpdate = true;
Expand Down Expand Up @@ -261,6 +264,7 @@ static bool updateQueuedMeasurments(const Axis3f *gyro);

// --------------------------------------------------

// Called one time during system startup
void estimatorKalmanTaskInit() {
distDataQueue = xQueueCreate(DIST_QUEUE_LENGTH, sizeof(distanceMeasurement_t));
posDataQueue = xQueueCreate(POS_QUEUE_LENGTH, sizeof(positionMeasurement_t));
Expand All @@ -284,6 +288,7 @@ void estimatorKalmanTaskInit() {
statsCntReset(&statsUMeasurementAppended, now_ms);
statsCntReset(&statsUMeasurementNotAppended, now_ms);

isEstimatorActive = false;
xTaskCreate(kalmanTask, KALMAN_TASK_NAME, 3 * configMINIMAL_STACK_SIZE, NULL, KALMAN_TASK_PRI, NULL);

isInit = true;
Expand All @@ -303,6 +308,8 @@ static void kalmanTask(void* parameters) {
uint32_t nextStatsUpdate = 0;

while (true) {
blockWhileInactive();

// Wake up when it is time for next prediction, unless we get queued meassurements
uint32_t maxSleepTics = nextPrediction - xTaskGetTickCount();
if (maxSleepTics > 0) {
Expand Down Expand Up @@ -407,6 +414,12 @@ static void kalmanTask(void* parameters) {
}
}

static void blockWhileInactive() {
while(! isEstimatorActive) {
vTaskDelay(M2T(300));
}
}

static void updateStats(uint32_t now_ms) {
statsCntRate(&statsUpdates, now_ms);
statsCntRate(&statsPredictions, now_ms);
Expand Down Expand Up @@ -576,6 +589,7 @@ static bool updateQueuedMeasurments(const Axis3f *gyro) {
return doneUpdate;
}

// Called when this estimator is activated
void estimatorKalmanInit(void) {
xQueueReset(distDataQueue);
xQueueReset(posDataQueue);
Expand All @@ -597,6 +611,13 @@ void estimatorKalmanInit(void) {
xSemaphoreGive(dataMutex);

kalmanCoreInit(&coreData);

isEstimatorActive = true;
}

// Called when another estimator is activated, and this estimator no longer is the active estimator
void estimatorKalmanDeinit(void) {
isEstimatorActive = false;
}

static bool appendMeasurement(xQueueHandle queue, void *measurement)
Expand Down
4 changes: 2 additions & 2 deletions src/modules/src/stabilizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ static void stabilizerTask(void* param)
} else {
// allow to update estimator dynamically
if (getStateEstimator() != estimatorType) {
stateEstimatorInit(estimatorType);
stateEstimatorSwitchTo(estimatorType);
estimatorType = getStateEstimator();
}
// allow to update controller dynamically
Expand All @@ -269,7 +269,7 @@ static void stabilizerTask(void* param)

stateEstimator(&state, &sensorData, &control, tick);
compressState();

commanderGetSetpoint(&setpoint, &state);
compressSetpoint();

Expand Down

0 comments on commit 880ba37

Please sign in to comment.