static void stabilizerTask(void* param) { uint32_t tick = 0; uint32_t lastWakeTime; vTaskSetApplicationTaskTag(0, (void*)TASK_STABILIZER_ID_NBR); //Wait for the system to be fully started to start stabilization loop systemWaitStart(); // Wait for sensors to be calibrated lastWakeTime = xTaskGetTickCount (); while(!sensorsAreCalibrated()) { vTaskDelayUntil(&lastWakeTime, F2T(RATE_MAIN_LOOP)); } while(1) { vTaskDelayUntil(&lastWakeTime, F2T(RATE_MAIN_LOOP)); #ifdef ESTIMATOR_TYPE_kalman stateEstimatorUpdate(&state, &sensorData, &control); #else sensorsAcquire(&sensorData, tick); stateEstimator(&state, &sensorData, tick); #endif commanderGetSetpoint(&setpoint, &state); sitAwUpdateSetpoint(&setpoint, &sensorData, &state); stateController(&control, &sensorData, &state, &setpoint, tick); powerDistribution(&control); tick++; } }
void estimatorComplementary(state_t *state, sensorData_t *sensorData, control_t *control, const uint32_t tick) { sensorsAcquire(sensorData, tick); // Read sensors at full rate (1000Hz) if (RATE_DO_EXECUTE(ATTITUDE_UPDATE_RATE, tick)) { sensfusion6UpdateQ(sensorData->gyro.x, sensorData->gyro.y, sensorData->gyro.z, sensorData->acc.x, sensorData->acc.y, sensorData->acc.z, ATTITUDE_UPDATE_DT); // Save attitude, adjusted for the legacy CF2 body coordinate system sensfusion6GetEulerRPY(&state->attitude.roll, &state->attitude.pitch, &state->attitude.yaw); // Save quaternion, hopefully one day this could be used in a better controller. // Note that this is not adjusted for the legacy coordinate system sensfusion6GetQuaternion( &state->attitudeQuaternion.x, &state->attitudeQuaternion.y, &state->attitudeQuaternion.z, &state->attitudeQuaternion.w); state->acc.z = sensfusion6GetAccZWithoutGravity(sensorData->acc.x, sensorData->acc.y, sensorData->acc.z); positionUpdateVelocity(state->acc.z, ATTITUDE_UPDATE_DT); } if (RATE_DO_EXECUTE(POS_UPDATE_RATE, tick)) { // If position sensor data is preset, pass it throught // FIXME: The position sensor shall be used as an input of the estimator if (sensorData->position.timestamp) { state->position = sensorData->position; } else { positionEstimate(state, sensorData, POS_UPDATE_DT, tick); } } }