Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 1868 → Rev 1869

/branches/dongfang_FC_rewrite/attitude.c
156,15 → 156,15
************************************************************************/
 
int32_t getAngleEstimateFromAcc(uint8_t axis) {
return GYRO_ACC_FACTOR * (int32_t)filteredAcc[axis];
return GYRO_ACC_FACTOR * (int32_t) filteredAcc[axis];
}
 
void setStaticAttitudeAngles(void) {
#ifdef ATTITUDE_USE_ACC_SENSORS
angle[PITCH] = getAngleEstimateFromAcc(PITCH);
angle[ROLL] = getAngleEstimateFromAcc(ROLL);
angle[PITCH] = getAngleEstimateFromAcc(PITCH);
angle[ROLL] = getAngleEstimateFromAcc(ROLL);
#else
angle[PITCH] = angle[ROLL] = 0;
angle[PITCH] = angle[ROLL] = 0;
#endif
}
 
172,26 → 172,26
* Neutral Readings
************************************************************************/
void attitude_setNeutral(void) {
// Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway.
dynamicParams.AxisCoupling1 = dynamicParams.AxisCoupling2 = 0;
// Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway.
dynamicParams.AxisCoupling1 = dynamicParams.AxisCoupling2 = 0;
 
driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0;
correctionSum[PITCH] = correctionSum[ROLL] = 0;
driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0;
correctionSum[PITCH] = correctionSum[ROLL] = 0;
 
// Calibrate hardware.
analog_calibrate();
// Calibrate hardware.
analog_calibrate();
 
// reset gyro integrals to acc guessing
setStaticAttitudeAngles();
yawAngleDiff = 0;
// reset gyro integrals to acc guessing
setStaticAttitudeAngles();
yawAngleDiff = 0;
 
// update compass course to current heading
compassCourse = compassHeading;
// update compass course to current heading
compassCourse = compassHeading;
 
// Inititialize YawGyroIntegral value with current compass heading
yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW;
// Inititialize YawGyroIntegral value with current compass heading
yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW;
 
// Servo_On(); //enable servo output
// Servo_On(); //enable servo output
}
 
/************************************************************************
201,22 → 201,24
* The rate variable end up in a range of about [-1024, 1023].
*************************************************************************/
void getAnalogData(void) {
uint8_t axis;
uint8_t axis;
 
for (axis = PITCH; axis <= ROLL; axis++) {
rate_PID[axis] = gyro_PID[axis] / HIRES_GYRO_INTEGRATION_FACTOR + driftComp[axis];
rate_ATT[axis] = gyro_ATT[axis] / HIRES_GYRO_INTEGRATION_FACTOR + driftComp[axis];
differential[axis] = gyroD[axis];
averageAcc[axis] += acc[axis];
}
for (axis = PITCH; axis <= ROLL; axis++) {
rate_PID[axis] = gyro_PID[axis] / HIRES_GYRO_INTEGRATION_FACTOR
+ driftComp[axis];
rate_ATT[axis] = gyro_ATT[axis] / HIRES_GYRO_INTEGRATION_FACTOR
+ driftComp[axis];
differential[axis] = gyroD[axis];
averageAcc[axis] += acc[axis];
}
 
averageAccCount++;
yawRate = yawGyro + driftCompYaw;
averageAccCount++;
yawRate = yawGyro + driftCompYaw;
 
// We are done reading variables from the analog module.
// Interrupt-driven sensor reading may restart.
analogDataReady = 0;
analog_start();
// We are done reading variables from the analog module.
// Interrupt-driven sensor reading may restart.
analogDataReady = 0;
analog_start();
}
 
/*
226,60 → 228,61
* changed accordingly.
*/
void trigAxisCoupling(void) {
int16_t cospitch = int_cos(angle[PITCH]);
int16_t cosroll = int_cos(angle[ROLL]);
int16_t sinroll = int_sin(angle[ROLL]);
J5HIGH;
int16_t cospitch = int_cos(angle[PITCH]);
int16_t cosroll = int_cos(angle[ROLL]);
int16_t sinroll = int_sin(angle[ROLL]);
 
ACRate[PITCH] = (((int32_t) rate_ATT[PITCH] * cosroll - (int32_t) yawRate
* sinroll) >> MATH_UNIT_FACTOR_LOG);
ACRate[PITCH] = (((int32_t) rate_ATT[PITCH] * cosroll - (int32_t) yawRate
* sinroll) >> MATH_UNIT_FACTOR_LOG);
 
ACRate[ROLL] = rate_ATT[ROLL] +
(((((int32_t)rate_ATT[PITCH] * sinroll + (int32_t)yawRate * cosroll)
>> MATH_UNIT_FACTOR_LOG)
* int_tan(angle[PITCH])) >> MATH_UNIT_FACTOR_LOG);
ACRate[ROLL] = rate_ATT[ROLL] + (((((int32_t) rate_ATT[PITCH] * sinroll
+ (int32_t) yawRate * cosroll) >> MATH_UNIT_FACTOR_LOG) * int_tan(
angle[PITCH])) >> MATH_UNIT_FACTOR_LOG);
 
ACYawRate = ((int32_t) rate_ATT[PITCH] * sinroll) / cospitch
+ ((int32_t) yawRate * cosroll) / cospitch;
ACYawRate = ((int32_t) rate_ATT[PITCH] * sinroll) / cospitch
+ ((int32_t) yawRate * cosroll) / cospitch;
}
 
// 480 usec with axis coupling - almost no time without.
void integrate(void) {
// First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate.
uint8_t axis;
if (!looping && (staticParams.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) {
// The rotary rate limiter bit is abused for selecting axis coupling algorithm instead.
trigAxisCoupling();
} else {
ACRate[PITCH] = rate_ATT[PITCH];
ACRate[ROLL] = rate_ATT[ROLL];
ACYawRate = yawRate;
}
// First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate.
uint8_t axis;
if (!looping && (staticParams.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) {
// The rotary rate limiter bit is abused for selecting axis coupling algorithm instead.
trigAxisCoupling();
} else {
ACRate[PITCH] = rate_ATT[PITCH];
ACRate[ROLL] = rate_ATT[ROLL];
ACYawRate = yawRate;
}
 
/*
* Yaw
* Calculate yaw gyro integral (~ to rotation angle)
* Limit yawGyroHeading proportional to 0 deg to 360 deg
*/
yawGyroHeading += ACYawRate;
yawAngleDiff += yawRate;
/*
* Yaw
* Calculate yaw gyro integral (~ to rotation angle)
* Limit yawGyroHeading proportional to 0 deg to 360 deg
*/
yawGyroHeading += ACYawRate;
yawAngleDiff += yawRate;
 
if (yawGyroHeading >= YAWOVER360) {
yawGyroHeading -= YAWOVER360; // 360 deg. wrap
} else if (yawGyroHeading < 0) {
yawGyroHeading += YAWOVER360;
}
if (yawGyroHeading >= YAWOVER360) {
yawGyroHeading -= YAWOVER360; // 360 deg. wrap
} else if (yawGyroHeading < 0) {
yawGyroHeading += YAWOVER360;
}
 
/*
* Pitch axis integration and range boundary wrap.
*/
for (axis = PITCH; axis <= ROLL; axis++) {
angle[axis] += ACRate[axis];
if (angle[axis] > PITCHROLLOVER180) {
angle[axis] -= PITCHROLLOVER360;
} else if (angle[axis] <= -PITCHROLLOVER180) {
angle[axis] += PITCHROLLOVER360;
}
}
/*
* Pitch axis integration and range boundary wrap.
*/
for (axis = PITCH; axis <= ROLL; axis++) {
angle[axis] += ACRate[axis];
if (angle[axis] > PITCHROLLOVER180) {
angle[axis] -= PITCHROLLOVER360;
} else if (angle[axis] <= -PITCHROLLOVER180) {
angle[axis] += PITCHROLLOVER360;
}
}
J5LOW;
}
 
/************************************************************************
290,58 → 293,58
* That should only be necessary with drifty gyros like ENC-03.
************************************************************************/
void correctIntegralsByAcc0thOrder(void) {
// TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities
// are less than ....., or reintroduce Kalman.
// Well actually the Z axis acc. check is not so silly.
uint8_t axis;
int32_t temp;
if (!looping && acc[Z] >= -dynamicParams.UserParams[7] && acc[Z]
<= dynamicParams.UserParams[7]) {
DebugOut.Digital[0] |= DEBUG_ACC0THORDER;
// TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities
// are less than ....., or reintroduce Kalman.
// Well actually the Z axis acc. check is not so silly.
uint8_t axis;
int32_t temp;
if (!looping && acc[Z] >= -dynamicParams.UserParams[7] && acc[Z]
<= dynamicParams.UserParams[7]) {
DebugOut.Digital[0] |= DEBUG_ACC0THORDER;
 
uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!!
uint8_t debugFullWeight = 1;
int32_t accDerived;
uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!!
uint8_t debugFullWeight = 1;
int32_t accDerived;
 
if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active
permilleAcc /= 2;
debugFullWeight = 0;
}
if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active
permilleAcc /= 2;
debugFullWeight = 0;
}
 
if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands
permilleAcc /= 2;
debugFullWeight = 0;
}
if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands
permilleAcc /= 2;
debugFullWeight = 0;
}
 
if (debugFullWeight)
DebugOut.Digital[1] |= DEBUG_ACC0THORDER;
else
DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER;
if (debugFullWeight)
DebugOut.Digital[1] |= DEBUG_ACC0THORDER;
else
DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER;
 
/*
* Add to each sum: The amount by which the angle is changed just below.
*/
for (axis = PITCH; axis <= ROLL; axis++) {
accDerived = getAngleEstimateFromAcc(axis);
DebugOut.Analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL;
/*
* Add to each sum: The amount by which the angle is changed just below.
*/
for (axis = PITCH; axis <= ROLL; axis++) {
accDerived = getAngleEstimateFromAcc(axis);
DebugOut.Analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL;
 
// 1000 * the correction amount that will be added to the gyro angle in next line.
temp = angle[axis]; //(permilleAcc * (accDerived - angle[axis])) / 1000;
angle[axis] = ((int32_t) (1000L - permilleAcc) * temp
+ (int32_t) permilleAcc * accDerived) / 1000L;
correctionSum[axis] += angle[axis] - temp;
}
} else {
DebugOut.Digital[0] &= ~DEBUG_ACC0THORDER;
DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER;
DebugOut.Analog[9] = 0;
DebugOut.Analog[10] = 0;
// 1000 * the correction amount that will be added to the gyro angle in next line.
temp = angle[axis]; //(permilleAcc * (accDerived - angle[axis])) / 1000;
angle[axis] = ((int32_t) (1000L - permilleAcc) * temp
+ (int32_t) permilleAcc * accDerived) / 1000L;
correctionSum[axis] += angle[axis] - temp;
}
} else {
DebugOut.Digital[0] &= ~DEBUG_ACC0THORDER;
DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER;
DebugOut.Analog[9] = 0;
DebugOut.Analog[10] = 0;
 
DebugOut.Analog[16] = 0;
DebugOut.Analog[17] = 0;
// experiment: Kill drift compensation updates when not flying smooth.
correctionSum[PITCH] = correctionSum[ROLL] = 0;
}
DebugOut.Analog[16] = 0;
DebugOut.Analog[17] = 0;
// experiment: Kill drift compensation updates when not flying smooth.
correctionSum[PITCH] = correctionSum[ROLL] = 0;
}
}
 
/************************************************************************
357,25 → 360,26
// 2 times / sec. = 488/2
#define DRIFTCORRECTION_TIME 256L
void driftCorrection(void) {
static int16_t timer = DRIFTCORRECTION_TIME;
int16_t deltaCorrection;
uint8_t axis;
if (!--timer) {
timer = DRIFTCORRECTION_TIME;
for (axis = PITCH; axis <= ROLL; axis++) {
// Take the sum of corrections applied, add it to delta
deltaCorrection = (correctionSum[axis] + DRIFTCORRECTION_TIME / 2) / DRIFTCORRECTION_TIME;
// Add the delta to the compensation. So positive delta means, gyro should have higher value.
driftComp[axis] += deltaCorrection / staticParams.GyroAccTrim;
CHECK_MIN_MAX(driftComp[axis], -staticParams.DriftComp, staticParams.DriftComp);
// DebugOut.Analog[11 + axis] = correctionSum[axis];
DebugOut.Analog[16 + axis] = correctionSum[axis];
DebugOut.Analog[18 + axis] = deltaCorrection / staticParams.GyroAccTrim;
DebugOut.Analog[28 + axis] = driftComp[axis];
static int16_t timer = DRIFTCORRECTION_TIME;
int16_t deltaCorrection;
uint8_t axis;
if (!--timer) {
timer = DRIFTCORRECTION_TIME;
for (axis = PITCH; axis <= ROLL; axis++) {
// Take the sum of corrections applied, add it to delta
deltaCorrection = (correctionSum[axis] + DRIFTCORRECTION_TIME / 2)
/ DRIFTCORRECTION_TIME;
// Add the delta to the compensation. So positive delta means, gyro should have higher value.
driftComp[axis] += deltaCorrection / staticParams.GyroAccTrim;
CHECK_MIN_MAX(driftComp[axis], -staticParams.DriftComp, staticParams.DriftComp);
// DebugOut.Analog[11 + axis] = correctionSum[axis];
DebugOut.Analog[16 + axis] = correctionSum[axis];
DebugOut.Analog[18 + axis] = deltaCorrection / staticParams.GyroAccTrim;
DebugOut.Analog[28 + axis] = driftComp[axis];
 
correctionSum[axis] = 0;
}
}
correctionSum[axis] = 0;
}
}
}
 
/************************************************************************
382,102 → 386,101
* Main procedure.
************************************************************************/
void calculateFlightAttitude(void) {
// part1: 550 usec.
// part1a: 550 usec.
// part1b: 60 usec.
getAnalogData();
// end part1b
integrate();
// end part1a
// part1: 550 usec.
// part1a: 550 usec.
// part1b: 60 usec.
getAnalogData();
// end part1b
integrate();
// end part1a
 
DebugOut.Analog[6] = stronglyFilteredAcc[PITCH];
DebugOut.Analog[7] = stronglyFilteredAcc[ROLL];
DebugOut.Analog[8] = stronglyFilteredAcc[Z];
 
DebugOut.Analog[6] = ACRate[PITCH];
DebugOut.Analog[7] = ACRate[ROLL];
DebugOut.Analog[8] = ACYawRate;
DebugOut.Analog[3] = rate_PID[PITCH];
DebugOut.Analog[4] = rate_PID[ROLL];
DebugOut.Analog[5] = yawRate;
 
DebugOut.Analog[3] = rate_PID[PITCH];
DebugOut.Analog[4] = rate_PID[ROLL];
DebugOut.Analog[5] = yawRate;
 
#ifdef ATTITUDE_USE_ACC_SENSORS
correctIntegralsByAcc0thOrder();
driftCorrection();
correctIntegralsByAcc0thOrder();
driftCorrection();
#endif
// end part1
// end part1
}
 
void updateCompass(void) {
int16_t w, v, r, correction, error;
int16_t w, v, r, correction, error;
 
if (compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) {
if (controlMixer_testCompassCalState()) {
compassCalState++;
if (compassCalState < 5)
beepNumber(compassCalState);
else
beep(1000);
}
} else {
// get maximum attitude angle
w = abs(angle[PITCH] / 512);
v = abs(angle[ROLL] / 512);
if (v > w)
w = v;
correction = w / 8 + 1;
// calculate the deviation of the yaw gyro heading and the compass heading
if (compassHeading < 0)
error = 0; // disable yaw drift compensation if compass heading is undefined
else if (abs(yawRate) > 128) { // spinning fast
error = 0;
} else {
// compassHeading - yawGyroHeading, on a -180..179 deg interval.
error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW))
% 360) - 180;
}
if (!ignoreCompassTimer && w < 25) {
yawGyroDrift += error;
// Basically this gets set if we are in "fix" mode, and when starting.
if (updateCompassCourse) {
beep(200);
yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW;
compassCourse = compassHeading; //(int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW);
updateCompassCourse = 0;
}
}
yawGyroHeading += (error * 8) / correction;
if (compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) {
if (controlMixer_testCompassCalState()) {
compassCalState++;
if (compassCalState < 5)
beepNumber(compassCalState);
else
beep(1000);
}
} else {
// get maximum attitude angle
w = abs(angle[PITCH] / 512);
v = abs(angle[ROLL] / 512);
if (v > w)
w = v;
correction = w / 8 + 1;
// calculate the deviation of the yaw gyro heading and the compass heading
if (compassHeading < 0)
error = 0; // disable yaw drift compensation if compass heading is undefined
else if (abs(yawRate) > 128) { // spinning fast
error = 0;
} else {
// compassHeading - yawGyroHeading, on a -180..179 deg interval.
error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW))
% 360) - 180;
}
if (!ignoreCompassTimer && w < 25) {
yawGyroDrift += error;
// Basically this gets set if we are in "fix" mode, and when starting.
if (updateCompassCourse) {
beep(200);
yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW;
compassCourse = compassHeading; //(int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW);
updateCompassCourse = 0;
}
}
yawGyroHeading += (error * 8) / correction;
 
/*
w = (w * dynamicParams.CompassYawEffect) / 32;
w = dynamicParams.CompassYawEffect - w;
*/
w = dynamicParams.CompassYawEffect - (w * dynamicParams.CompassYawEffect)
/ 32;
/*
w = (w * dynamicParams.CompassYawEffect) / 32;
w = dynamicParams.CompassYawEffect - w;
*/
w = dynamicParams.CompassYawEffect - (w * dynamicParams.CompassYawEffect)
/ 32;
 
// As readable formula:
// w = dynamicParams.CompassYawEffect * (1-w/32);
// As readable formula:
// w = dynamicParams.CompassYawEffect * (1-w/32);
 
if (w >= 0) { // maxAttitudeAngle < 32
if (!ignoreCompassTimer) {
v = 64 + (maxControl[PITCH] + maxControl[ROLL]) / 8;
// yawGyroHeading - compassCourse on a -180..179 degree interval.
r
= ((540 + yawGyroHeading / GYRO_DEG_FACTOR_YAW - compassCourse)
% 360) - 180;
v = (r * w) / v; // align to compass course
// limit yaw rate
w = 3 * dynamicParams.CompassYawEffect;
if (v > w)
v = w;
else if (v < -w)
v = -w;
yawAngleDiff += v;
} else { // wait a while
ignoreCompassTimer--;
}
} else { // ignore compass at extreme attitudes for a while
ignoreCompassTimer = 500;
}
}
if (w >= 0) { // maxAttitudeAngle < 32
if (!ignoreCompassTimer) {
v = 64 + (maxControl[PITCH] + maxControl[ROLL]) / 8;
// yawGyroHeading - compassCourse on a -180..179 degree interval.
r
= ((540 + yawGyroHeading / GYRO_DEG_FACTOR_YAW - compassCourse)
% 360) - 180;
v = (r * w) / v; // align to compass course
// limit yaw rate
w = 3 * dynamicParams.CompassYawEffect;
if (v > w)
v = w;
else if (v < -w)
v = -w;
yawAngleDiff += v;
} else { // wait a while
ignoreCompassTimer--;
}
} else { // ignore compass at extreme attitudes for a while
ignoreCompassTimer = 500;
}
}
}
 
/*