Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 1634 → Rev 1645

/branches/dongfang_FC_rewrite/attitude.c
84,11 → 84,11
* The variables are overwritten at each attitude calculation invocation - the values
* are not preserved or reused.
*/
int16_t pitchRate, rollRate, yawRate;
int16_t rate[2], yawRate;
 
// With different (less) filtering
int16_t pitchRate_PID, rollRate_PID;
int16_t pitchDifferential, rollDifferential;
int16_t rate_PID[2];
int16_t differential[2];
 
/*
* Gyro readings, after performing "axis coupling" - that is, the transfomation
99,13 → 99,13
* The variables are overwritten at each attitude calculation invocation - the values
* are not preserved or reused.
*/
int16_t ACPitchRate, ACRollRate, ACYawRate;
int16_t ACRate[2], ACYawRate;
 
/*
* Gyro integrals. These are the rotation angles of the airframe compared to the
* horizontal plane, yaw relative to yaw at start.
*/
int32_t pitchAngle, rollAngle, yawAngle;
int32_t angle[2], yawAngle;
 
int readingHeight = 0;
 
124,12 → 124,12
#define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L)
#define YAWOVER360 (GYRO_DEG_FACTOR_YAW * 360L)
 
int32_t pitchCorrectionSum = 0, rollCorrectionSum = 0;
int32_t correctionSum[2] = {0,0};
 
/*
* Experiment: Compensating for dynamic-induced gyro biasing.
*/
int16_t dynamicOffsetPitch = 0, dynamicOffsetRoll = 0, dynamicOffsetYaw = 0;
int16_t dynamicOffset[2] = {0,0}, dynamicOffsetYaw = 0;
// int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0;
// int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw;
// int16_t dynamicCalCount;
144,21 → 144,16
* it is hardly worth the trouble.
************************************************************************/
 
int32_t getPitchAngleEstimateFromAcc(void) {
return GYRO_ACC_FACTOR * (int32_t)filteredPitchAxisAcc;
int32_t getAngleEstimateFromAcc(uint8_t axis) {
return GYRO_ACC_FACTOR * (int32_t)filteredAcc[axis];
}
 
int32_t getRollAngleEstimateFromAcc(void) {
return GYRO_ACC_FACTOR * (int32_t)filteredRollAxisAcc;
}
 
void setStaticAttitudeAngles(void) {
#ifdef ATTITUDE_USE_ACC_SENSORS
pitchAngle = getPitchAngleEstimateFromAcc();
rollAngle = getRollAngleEstimateFromAcc();
angle[PITCH] = getAngleEstimateFromAcc(PITCH);
angle[ROLL] = getAngleEstimateFromAcc(ROLL);
#else
pitchAngle = 0;
rollAngle = 0;
angle[PITCH] = angle[ROLL] = 0;
#endif
}
 
169,13 → 164,13
// Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway.
dynamicParams.AxisCoupling1 = dynamicParams.AxisCoupling2 = 0;
 
dynamicOffsetPitch = dynamicOffsetRoll = 0;
dynamicOffset[PITCH] = dynamicOffset[ROLL] = 0;
// Calibrate hardware.
analog_calibrate();
 
// reset gyro readings
pitchRate = rollRate = yawRate = 0;
rate[PITCH] = rate[ROLL] = yawRate = 0;
 
// reset gyro integrals to acc guessing
setStaticAttitudeAngles();
192,70 → 187,26
/************************************************************************
* Get sensor data from the analog module, and release the ADC
* TODO: Ultimately, the analog module could do this (instead of dumping
* the values into variables).
* the values into variables).
* The rate variable end up in a range of about [-1024, 1023].
* When scaled down by CONTROL_SCALING, the interval is about [-256, 256].
*************************************************************************/
void getAnalogData(void) {
// For the differential calculation. Diff. is not supported right now.
// int16_t d2Pitch, d2Roll;
pitchRate_PID = (hiResPitchGyro + dynamicOffsetPitch) / HIRES_GYRO_INTEGRATION_FACTOR;
pitchRate = (filteredHiResPitchGyro + dynamicOffsetPitch) / HIRES_GYRO_INTEGRATION_FACTOR;
pitchDifferential = pitchGyroD;
 
rollRate_PID = (hiResRollGyro + dynamicOffsetRoll) / HIRES_GYRO_INTEGRATION_FACTOR;
rollRate = (filteredHiResRollGyro + dynamicOffsetRoll) / HIRES_GYRO_INTEGRATION_FACTOR;
rollDifferential = rollGyroD;
 
uint8_t axis;
for (axis=PITCH; axis <=ROLL; axis++) {
rate_PID[axis] = (gyro_PID[axis] + dynamicOffset[axis]) / HIRES_GYRO_INTEGRATION_FACTOR;
rate[axis] = (gyro_ATT[axis] + dynamicOffset[axis]) / HIRES_GYRO_INTEGRATION_FACTOR;
differential[axis] = gyroD[axis];
}
yawRate = yawGyro + dynamicOffsetYaw;
 
// We are done reading variables from the analog module. Interrupt-driven sensor reading may restart.
// We are done reading variables from the analog module.
// Interrupt-driven sensor reading may restart.
analogDataReady = 0;
analog_start();
}
 
/************************************************************************
* Axis coupling, H&I Style
* Currently not working (and there is a bug in it,
* which causes unstable flight in heading-hold mode).
************************************************************************/
void H_and_I_axisCoupling(void) {
int32_t tmpl = 0, tmpl2 = 0, tmp13 = 0, tmp14 = 0;
int16_t CouplingNickRoll = 0, CouplingRollNick = 0;
 
tmp13 = (rollRate * pitchAngle) / 2048L;
tmp13 *= dynamicParams.AxisCoupling2; // 65
tmp13 /= 4096L;
CouplingNickRoll = tmp13;
tmp14 = (pitchRate * rollAngle) / 2048L;
tmp14 *= dynamicParams.AxisCoupling2; // 65
tmp14 /= 4096L;
CouplingRollNick = tmp14;
tmp14 -= tmp13;
 
ACYawRate = yawRate + tmp14;
/*
if(!dynamicParams.AxisCouplingYawCorrection) ACYawRate = yawRate - tmp14 / 2; // force yaw
else ACYawRate
*/
tmpl = ((yawRate + tmp14) * pitchAngle) / 2048L;
tmpl *= dynamicParams.AxisCoupling1;
tmpl /= 4096L;
tmpl2 = ((yawRate + tmp14) * rollAngle) / 2048L;
tmpl2 *= dynamicParams.AxisCoupling1;
tmpl2 /= 4096L;
 
// if(abs(yawRate > 64)) {
// if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1;
// }
ACPitchRate = pitchRate - tmpl2 + tmpl / 100L;
ACRollRate = rollRate + tmpl - tmpl2 / 100L;
}
 
/*
* This is the standard flight-style coordinate system transformation
* (from airframe-local axes to a ground-based system). For some reason
263,49 → 214,38
* changed accordingly.
*/
void trigAxisCoupling(void) {
int16_t cospitch = int_cos(pitchAngle);
int16_t cosroll = int_cos(rollAngle);
int16_t sinroll = int_sin(rollAngle);
int16_t tanpitch = int_tan(pitchAngle);
int16_t cospitch = int_cos(angle[PITCH]);
int16_t cosroll = int_cos(angle[ROLL]);
int16_t sinroll = int_sin(angle[ROLL]);
int16_t tanpitch = int_tan(angle[PITCH]);
#define ANTIOVF 1024
ACPitchRate = ((int32_t)pitchRate * cosroll - (int32_t)yawRate * sinroll) / (int32_t)MATH_UNIT_FACTOR;
ACRollRate = rollRate + (((int32_t)pitchRate * sinroll / ANTIOVF * tanpitch + (int32_t)yawRate * int_cos(rollAngle) / ANTIOVF * tanpitch) / ((int32_t)MATH_UNIT_FACTOR / ANTIOVF * MATH_UNIT_FACTOR));
ACYawRate = ((int32_t)pitchRate * sinroll) / cospitch + ((int32_t)yawRate * cosroll) / cospitch;
ACRate[PITCH] = ((int32_t) rate[PITCH] * cosroll - (int32_t)yawRate * sinroll) / (int32_t)MATH_UNIT_FACTOR;
ACRate[ROLL] = rate[ROLL] + (((int32_t)rate[PITCH] * sinroll / ANTIOVF * tanpitch + (int32_t)yawRate * int_cos(angle[ROLL]) / ANTIOVF * tanpitch) / ((int32_t)MATH_UNIT_FACTOR / ANTIOVF * MATH_UNIT_FACTOR));
ACYawRate = ((int32_t) rate[PITCH] * sinroll) / cospitch + ((int32_t)yawRate * cosroll) / cospitch;
}
 
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.
if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER)
trigAxisCoupling();
else
H_and_I_axisCoupling();
trigAxisCoupling();
} else {
ACPitchRate = pitchRate;
ACRollRate = rollRate;
ACRate[PITCH] = rate[PITCH];
ACRate[ROLL] = rate[ROLL];
ACYawRate = yawRate;
}
 
DebugOut.Analog[3] = pitchRate;
// DebugOut.Analog[3 + 3] = ACPitchRate;
DebugOut.Analog[4] = rollRate;
// DebugOut.Analog[4 + 3] = ACRollRate;
DebugOut.Analog[5] = yawRate;
// DebugOut.Analog[5 + 3] = ACYawRate;
DebugOut.Analog[3] = ACRate[PITCH];
DebugOut.Analog[4] = ACRate[ROLL];
DebugOut.Analog[5] = ACYawRate;
 
/*
DebugOut.Analog[9] = int_cos(pitchAngle);
DebugOut.Analog[10] = int_sin(pitchAngle);
DebugOut.Analog[11] = int_tan(pitchAngle);
*/
 
/*
* Yaw
* Calculate yaw gyro integral (~ to rotation angle)
* Limit yawGyroHeading proportional to 0 deg to 360 deg
*/
yawGyroHeading += ACYawRate;
 
// Why is yawAngle not wrapped 'round?
320,22 → 260,14
/*
* Pitch axis integration and range boundary wrap.
*/
pitchAngle += ACPitchRate;
if(pitchAngle > PITCHROLLOVER180) {
pitchAngle -= PITCHROLLOVER360;
} else if (pitchAngle <= -PITCHROLLOVER180) {
pitchAngle += PITCHROLLOVER360;
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.
*/
rollAngle += ACRollRate;
if(rollAngle > PITCHROLLOVER180) {
rollAngle -= PITCHROLLOVER360;
} else if (rollAngle <= -PITCHROLLOVER180) {
rollAngle += PITCHROLLOVER360;
}
}
 
/************************************************************************
349,17 → 281,16
// 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.
if(!looping && //((ZAxisAcc >= -4) || (MKFlags & MKFLAG_MOTOR_RUN))) { // if not looping in any direction
ZAxisAcc >= -dynamicParams.UserParams[7] && ZAxisAcc <= dynamicParams.UserParams[7]) {
uint8_t axis;
if(!looping && //((ZAcc >= -4) || (MKFlags & MKFLAG_MOTOR_RUN))) { // if not looping in any direction
ZAcc >= -dynamicParams.UserParams[7] && ZAcc <= dynamicParams.UserParams[7]) {
DebugOut.Digital[0] = 1;
uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!!
uint8_t debugFullWeight = 1;
int32_t accDerived[2];
int32_t accDerivedPitch = getPitchAngleEstimateFromAcc();
int32_t accDerivedRoll = getRollAngleEstimateFromAcc();
if((maxControlPitch > 64) || (maxControlRoll > 64)) { // reduce effect during stick commands
if((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands
permilleAcc /= 2;
debugFullWeight = 0;
}
372,13 → 303,14
/*
* Add to each sum: The amount by which the angle is changed just below.
*/
pitchCorrectionSum += permilleAcc * (accDerivedPitch - pitchAngle);
rollCorrectionSum += permilleAcc * (accDerivedRoll - rollAngle);
for (axis=PITCH; axis<=ROLL; axis++) {
accDerived[axis] = getAngleEstimateFromAcc(axis);
correctionSum[axis] += permilleAcc * (accDerived[axis] - angle[axis]);
// There should not be a risk of overflow here, since the integrals do not exceed a few 100000.
pitchAngle = ((int32_t)(1000 - permilleAcc) * pitchAngle + (int32_t)permilleAcc * accDerivedPitch) / 1000L;
rollAngle = ((int32_t)(1000 - permilleAcc) * rollAngle + (int32_t)permilleAcc * accDerivedRoll) / 1000L;
// There should not be a risk of overflow here, since the integrals do not exceed a few 100000.
angle[axis] = ((int32_t)(1000 - permilleAcc) * angle[axis] + (int32_t)permilleAcc * accDerived[axis]) / 1000L;
}
DebugOut.Digital[1] = debugFullWeight;
} else {
DebugOut.Digital[0] = 0;
400,20 → 332,16
void driftCompensation(void) {
static int16_t timer = DRIFTCORRECTION_TIME;
int16_t deltaCompensation;
uint8_t axis;
if (! --timer) {
timer = DRIFTCORRECTION_TIME;
deltaCompensation = ((pitchCorrectionSum + 1000L * DRIFTCORRECTION_TIME / 2) / 1000 / DRIFTCORRECTION_TIME);
CHECK_MIN_MAX(deltaCompensation, -staticParams.DriftComp, staticParams.DriftComp);
dynamicOffsetPitch += deltaCompensation / staticParams.GyroAccTrim;
 
deltaCompensation = ((rollCorrectionSum + 1000L * DRIFTCORRECTION_TIME / 2) / 1000 / DRIFTCORRECTION_TIME);
CHECK_MIN_MAX(deltaCompensation, -staticParams.DriftComp, staticParams.DriftComp);
dynamicOffsetRoll += deltaCompensation / staticParams.GyroAccTrim;
 
pitchCorrectionSum = rollCorrectionSum = 0;
 
DebugOut.Analog[28] = dynamicOffsetPitch;
DebugOut.Analog[29] = dynamicOffsetRoll;
for (axis=PITCH; axis<=ROLL; axis++) {
deltaCompensation = ((correctionSum[axis] + 1000L * DRIFTCORRECTION_TIME / 2) / 1000 / DRIFTCORRECTION_TIME);
CHECK_MIN_MAX(deltaCompensation, -staticParams.DriftComp, staticParams.DriftComp);
dynamicOffset[axis] += deltaCompensation / staticParams.GyroAccTrim;
correctionSum[axis] = 0;
DebugOut.Analog[28 + axis] = dynamicOffset;
}
}
}
 
430,56 → 358,56
}
 
/*
void updateCompass(void) {
void updateCompass(void) {
int16_t w, v, r,correction, error;
if(compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) {
setCompassCalState();
setCompassCalState();
} else {
// get maximum attitude angle
w = abs(pitchAngle / 512);
v = abs(rollAngle / 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 error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) % 360) - 180;
if(abs(yawRate) > 128) { // spinning fast
error = 0;
}
if(!badCompassHeading && w < 25) {
if(updateCompassCourse) {
beep(200);
yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW;
compassCourse = (int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW);
updateCompassCourse = 0;
}
}
yawGyroHeading += (error * 8) / correction;
w = (w * dynamicParams.CompassYawEffect) / 32;
w = dynamicParams.CompassYawEffect - w;
if(w >= 0) {
if(!badCompassHeading) {
v = 64 + (maxControlPitch + maxControlRoll) / 8;
// calc course deviation
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;
yawAngle += v;
}
else
{ // wait a while
badCompassHeading--;
}
}
else { // ignore compass at extreme attitudes for a while
badCompassHeading = 500;
}
// get maximum attitude angle
w = abs(pitchAngle / 512);
v = abs(rollAngle / 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 error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) % 360) - 180;
if(abs(yawRate) > 128) { // spinning fast
error = 0;
}
}
if(!badCompassHeading && w < 25) {
if(updateCompassCourse) {
beep(200);
yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW;
compassCourse = (int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW);
updateCompassCourse = 0;
}
}
yawGyroHeading += (error * 8) / correction;
w = (w * dynamicParams.CompassYawEffect) / 32;
w = dynamicParams.CompassYawEffect - w;
if(w >= 0) {
if(!badCompassHeading) {
v = 64 + (maxControlPitch + maxControlRoll) / 8;
// calc course deviation
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;
yawAngle += v;
}
else
{ // wait a while
badCompassHeading--;
}
}
else { // ignore compass at extreme attitudes for a while
badCompassHeading = 500;
}
}
}
*/
 
/*
491,12 → 419,12
* speed unfortunately... must find a better way)
*/
/*
void attitude_startDynamicCalibration(void) {
void attitude_startDynamicCalibration(void) {
dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0;
savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000;
}
}
 
void attitude_continueDynamicCalibration(void) {
void attitude_continueDynamicCalibration(void) {
// measure dynamic offset now...
dynamicCalPitch += hiResPitchGyro;
dynamicCalRoll += hiResRollGyro;
505,18 → 433,18
// Param6: Manual mode. The offsets are taken from Param7 and Param8.
if (dynamicParams.UserParam6 || 1) { // currently always enabled.
// manual mode
dynamicOffsetPitch = dynamicParams.UserParam7 - 128;
dynamicOffsetRoll = dynamicParams.UserParam8 - 128;
// manual mode
dynamicOffsetPitch = dynamicParams.UserParam7 - 128;
dynamicOffsetRoll = dynamicParams.UserParam8 - 128;
} else {
// use the sampled value (does not seem to work so well....)
dynamicOffsetPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount;
dynamicOffsetRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount;
dynamicOffsetYaw = -dynamicCalYaw / dynamicCalCount;
// use the sampled value (does not seem to work so well....)
dynamicOffsetPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount;
dynamicOffsetRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount;
dynamicOffsetYaw = -dynamicCalYaw / dynamicCalCount;
}
// keep resetting these meanwhile, to avoid accumulating errors.
setStaticAttitudeIntegrals();
yawAngle = 0;
}
}
*/