static void stabilizerTask(void* param) { uint32_t lastWakeTime; vTaskSetApplicationTaskTag(0, (void*)TASK_STABILIZER_ID_NBR); //Wait for the system to be fully started to start stabilization loop systemWaitStart(); lastWakeTime = xTaskGetTickCount (); while(1) { vTaskDelayUntil(&lastWakeTime, F2T(IMU_UPDATE_FREQ)); // Magnetometer not yet used more then for logging. imu9Read(&gyro, &acc, &mag); if (imu6IsCalibrated()) { commanderGetRPY(&rollDesired, &pitchDesired, &yawDesired); sensfusion6UpdateQ(gyro.x, gyro.y, gyro.z, acc.x, acc.y, acc.z, FUSION_UPDATE_DT); sensfusion6GetEulerRPY(&eulerRollActual, &eulerPitchActual, &eulerYawActual); if (horizonMode) { horizonPID(eulerRollActual, eulerPitchActual, -gyro.z, rollDesired, pitchDesired, yawDesired); } else { ratePID(gyro.x, -gyro.y, -gyro.z, rollDesired, pitchDesired, yawDesired); } controllerGetActuatorOutput(&actuatorRoll, &actuatorPitch, &actuatorYaw); commanderGetThrust(&actuatorThrust); /* Call out before performing thrust updates, if any functions would like to influence the thrust. */ if (armed) { distributePower(actuatorThrust, actuatorRoll, actuatorPitch, actuatorYaw); } else { distributePower(0, 0, 0, 0); controllerResetAllPID(); } } } }
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); } } }
static void stabilizerTask(void* param) { uint32_t attitudeCounter = 0; uint32_t altHoldCounter = 0; uint32_t lastWakeTime; vTaskSetApplicationTaskTag(0, (void*) TASK_STABILIZER_ID_NBR); //Wait for the system to be fully started to start stabilization loop systemWaitStart(); lastWakeTime = xTaskGetTickCount(); while (1) { vTaskDelayUntil(&lastWakeTime, F2T(IMU_UPDATE_FREQ)); // 500Hz // Magnetometer not yet used more then for logging. imu9Read(&gyro, &acc, &mag); if (imu6IsCalibrated()) { commanderGetRPY(&eulerRollDesired, &eulerPitchDesired, &eulerYawDesired); commanderGetRPYType(&rollType, &pitchType, &yawType); // 250HZ if (++attitudeCounter >= ATTITUDE_UPDATE_RATE_DIVIDER) { sensfusion6UpdateQ(gyro.x, gyro.y, gyro.z, acc.x, acc.y, acc.z, FUSION_UPDATE_DT); sensfusion6GetEulerRPY(&eulerRollActual, &eulerPitchActual, &eulerYawActual); accWZ = sensfusion6GetAccZWithoutGravity(acc.x, acc.y, acc.z); accMAG = (acc.x * acc.x) + (acc.y * acc.y) + (acc.z * acc.z); // Estimate speed from acc (drifts) vSpeed += deadband(accWZ, vAccDeadband) * FUSION_UPDATE_DT; controllerCorrectAttitudePID(eulerRollActual, eulerPitchActual, eulerYawActual, eulerRollDesired, eulerPitchDesired, -eulerYawDesired, &rollRateDesired, &pitchRateDesired, &yawRateDesired); attitudeCounter = 0; } // 100HZ if (imuHasBarometer() && (++altHoldCounter >= ALTHOLD_UPDATE_RATE_DIVIDER)) { stabilizerAltHoldUpdate(); altHoldCounter = 0; } if (rollType == RATE) { rollRateDesired = eulerRollDesired; } if (pitchType == RATE) { pitchRateDesired = eulerPitchDesired; } if (yawType == RATE) { yawRateDesired = -eulerYawDesired; } // TODO: Investigate possibility to subtract gyro drift. controllerCorrectRatePID(gyro.x, -gyro.y, gyro.z, rollRateDesired, pitchRateDesired, yawRateDesired); controllerGetActuatorOutput(&actuatorRoll, &actuatorPitch, &actuatorYaw); if (!altHold || !imuHasBarometer()) { // Use thrust from controller if not in altitude hold mode commanderGetThrust(&actuatorThrust); } else { // Added so thrust can be set to 0 while in altitude hold mode after disconnect commanderWatchdog(); } if (actuatorThrust > 0) { #if defined(TUNE_ROLL) distributePower(actuatorThrust, actuatorRoll, 0, 0); #elif defined(TUNE_PITCH) distributePower(actuatorThrust, 0, actuatorPitch, 0); #elif defined(TUNE_YAW) distributePower(actuatorThrust, 0, 0, -actuatorYaw); #else distributePower(actuatorThrust, actuatorRoll, actuatorPitch, -actuatorYaw); #endif } else { distributePower(0, 0, 0, 0); controllerResetAllPID(); } } } }
static void stabilizerTask(void* param) { uint32_t lastWakeTime; //uint32_t tempTime; uint16_t heartbCounter = 0; uint16_t attitudeCounter = 0; uint16_t altHoldCounter = 0; //uint32_t data[6]; //Wait for the system to be fully started to start stabilization loop systemWaitStart(); lastWakeTime = xTaskGetTickCount (); for( ; ;) { //tempTime = lastWakeTime; vTaskDelayUntil(&lastWakeTime, F2T(IMU_UPDATE_FREQ)); // 500Hz heartbCounter ++; /* if (lastWakeTime < tempTime) { tempTime = (0 - tempTime) + lastWakeTime; } else { tempTime = lastWakeTime - tempTime; } */ while (heartbCounter >= HEART_UPDATE_RATE_DIVIDER) { // 1Hz MAVLINK(mavlink_msg_heartbeat_send(MAVLINK_COMM_0, MAV_TYPE_QUADROTOR, MAV_AUTOPILOT_GENERIC, MAV_MODE_PREFLIGHT, 0, MAV_STATE_STANDBY);) heartbCounter = 0; } imuRead(&gyro, &acc, &mag); if (imu6IsCalibrated()) { // 250HZ if (++attitudeCounter >= ATTITUDE_UPDATE_RATE_DIVIDER) { MahonyAHRSupdateIMU(gyro.y, gyro.x, gyro.z, acc.y, acc.x, acc.z); //filterUpdate_mars(gyro.x, gyro.y, gyro.z, acc.x, acc.y, acc.z,mag.x,mag.y,mag.z); //MahonyAHRSupdate(gyro.x, gyro.y, gyro.z, acc.x, acc.y, acc.z,mag.x,mag.y,mag.z); //MahonyAHRSupdate(gyro.y, gyro.x, gyro.z, acc.y, acc.x, acc.z,mag.y,mag.x,mag.z); //filterUpdate_mars(gyro.x, gyro.y, gyro.z, acc.x, acc.y, acc.z,mag.x,mag.y,mag.z); //MahonyAHRSupdate(gyro.y, gyro.x, gyro.z, acc.y, acc.x, acc.z,mag.y,mag.x,mag.z); sensfusion6GetEulerRPY(&eulerRollActual, &eulerPitchActual, &eulerYawActual); radRollActual = eulerRollActual * M_PI / 180.0f; radPitchActual = eulerPitchActual * M_PI / 180.0f; radYawActual = eulerYawActual * M_PI / 180.0f; //float yh, xh; #define yh (mag.y * cos(radRollActual) - mag.z * sin(radRollActual)) #define xh (mag.x*cos(radPitchActual) + mag.y*sin(radRollActual)*sin(radPitchActual) + mag.z * cos(radRollActual)*sin(radPitchActual)) radYawActual = atan2(-yh,xh); MAVLINK(mavlink_msg_attitude_send(MAVLINK_COMM_0, lastWakeTime, \ radRollActual, radPitchActual, radYawActual, \ gyro.x, gyro.y, gyro.z);) attitudeCounter = 0; }