74,10 → 74,6 |
// For Servo_On / Off |
// #include "timer2.h" |
|
#ifdef USE_MK3MAG |
#include "mk3mag.h" |
#include "gps.h" |
#endif |
#define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
|
/* |
113,13 → 109,22 |
|
int readingHeight = 0; |
|
// compass course |
int16_t compassHeading = -1; // negative angle indicates invalid data. |
int16_t compassCourse = -1; |
int16_t compassOffCourse = 0; |
uint16_t updateCompassCourse = 0; |
uint8_t compassCalState = 0; |
uint16_t badCompassHeading = 500; |
// Yaw angle and compass stuff. |
|
// This is updated/written from MM3. Negative angle indicates invalid data. |
int16_t compassHeading = -1; |
|
// This is NOT updated from MM3. Negative angle indicates invalid data. |
int16_t compassCourse = -1; |
|
// The difference between the above 2 (heading - course) on a -180..179 degree interval. |
// Not necessary. Never read anywhere. |
// int16_t compassOffCourse = 0; |
|
uint8_t updateCompassCourse = 0; |
uint8_t compassCalState = 0; |
uint16_t ignoreCompassTimer = 500; |
|
int32_t yawGyroHeading; // Yaw Gyro Integral supported by compass |
int16_t yawGyroDrift; |
|
127,15 → 132,15 |
#define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L) |
#define YAWOVER360 (GYRO_DEG_FACTOR_YAW * 360L) |
|
int16_t correctionSum[2] = {0,0}; |
int16_t correctionSum[2] = { 0, 0 }; |
|
// For NaviCTRL use. |
int16_t averageAcc[2] = {0,0}, averageAccCount = 0; |
int16_t averageAcc[2] = { 0, 0 }, averageAccCount = 0; |
|
/* |
* Experiment: Compensating for dynamic-induced gyro biasing. |
*/ |
int16_t driftComp[2] = {0,0}, driftCompYaw = 0; |
int16_t driftComp[2] = { 0, 0 }, driftCompYaw = 0; |
// int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0; |
// int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw; |
// int16_t dynamicCalCount; |
151,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 |
} |
|
167,29 → 172,29 |
* 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; |
|
// Calibrate hardware. |
analog_calibrate(); |
|
// reset gyro readings |
// rate_ATT[PITCH] = rate_ATT[ROLL] = yawRate = 0; |
driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0; |
correctionSum[PITCH] = correctionSum[ROLL] = 0; |
|
// reset gyro integrals to acc guessing |
setStaticAttitudeAngles(); |
yawAngleDiff = 0; |
// Calibrate hardware. |
analog_calibrate(); |
|
// update compass course to current heading |
compassCourse = compassHeading; |
// reset gyro readings |
// rate_ATT[PITCH] = rate_ATT[ROLL] = yawRate = 0; |
|
// Inititialize YawGyroIntegral value with current compass heading |
yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW; |
// reset gyro integrals to acc guessing |
setStaticAttitudeAngles(); |
yawAngleDiff = 0; |
|
// Servo_On(); //enable servo output |
// update compass course to current heading |
compassCourse = compassHeading; |
|
// Inititialize YawGyroIntegral value with current compass heading |
yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW; |
|
// Servo_On(); //enable servo output |
} |
|
/************************************************************************ |
199,22 → 204,24 |
* The rate variable end up in a range of about [-1024, 1023]. |
*************************************************************************/ |
void getAnalogData(void) { |
uint8_t axis; |
|
for (axis=PITCH; axis <=ROLL; axis++) { |
rate_PID[axis] = (gyro_PID[axis] + driftComp[axis]) / HIRES_GYRO_INTEGRATION_FACTOR; |
rate_ATT[axis] = (gyro_ATT[axis] + driftComp[axis]) / HIRES_GYRO_INTEGRATION_FACTOR; |
differential[axis] = gyroD[axis]; |
averageAcc[axis] += acc[axis]; |
} |
uint8_t axis; |
|
averageAccCount++; |
yawRate = yawGyro + driftCompYaw; |
for (axis = PITCH; axis <= ROLL; axis++) { |
rate_PID[axis] = (gyro_PID[axis] + driftComp[axis]) |
/ HIRES_GYRO_INTEGRATION_FACTOR; |
rate_ATT[axis] = (gyro_ATT[axis] + driftComp[axis]) |
/ HIRES_GYRO_INTEGRATION_FACTOR; |
differential[axis] = gyroD[axis]; |
averageAcc[axis] += acc[axis]; |
} |
|
// We are done reading variables from the analog module. |
// Interrupt-driven sensor reading may restart. |
analogDataReady = 0; |
analog_start(); |
averageAccCount++; |
yawRate = yawGyro + driftCompYaw; |
|
// We are done reading variables from the analog module. |
// Interrupt-driven sensor reading may restart. |
analogDataReady = 0; |
analog_start(); |
} |
|
/* |
224,54 → 231,59 |
* 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]); |
int16_t tanpitch = int_tan(angle[PITCH]); |
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 512 |
ACRate[PITCH] = ((int32_t) rate_ATT[PITCH] * cosroll - (int32_t)yawRate * sinroll) / (int32_t)MATH_UNIT_FACTOR; |
ACRate[ROLL] = rate_ATT[ROLL] + (((int32_t)rate_ATT[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_ATT[PITCH] * sinroll) / cospitch + ((int32_t)yawRate * cosroll) / cospitch; |
ACRate[PITCH] = ((int32_t) rate_ATT[PITCH] * cosroll - (int32_t) yawRate |
* sinroll) / (int32_t) MATH_UNIT_FACTOR; |
ACRate[ROLL] = rate_ATT[ROLL] + (((int32_t) rate_ATT[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_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; |
|
if(yawGyroHeading >= YAWOVER360) { |
yawGyroHeading -= YAWOVER360; // 360 deg. wrap |
} else if(yawGyroHeading < 0) { |
yawGyroHeading += YAWOVER360; |
} |
/* |
* Yaw |
* Calculate yaw gyro integral (~ to rotation angle) |
* Limit yawGyroHeading proportional to 0 deg to 360 deg |
*/ |
yawGyroHeading += ACYawRate; |
yawAngleDiff += yawRate; |
|
/* |
* 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; |
} |
} |
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; |
} |
} |
} |
|
/************************************************************************ |
282,57 → 294,60 |
* 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 correction; |
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; |
|
if((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active |
permilleAcc /= 2; |
debugFullWeight = 0; |
} |
// 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 correction; |
if (!looping && acc[Z] >= -dynamicParams.UserParams[7] && acc[Z] |
<= dynamicParams.UserParams[7]) { |
DebugOut.Digital[0] |= DEBUG_ACC0THORDER; |
|
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; |
uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!! |
uint8_t debugFullWeight = 1; |
int32_t accDerived; |
|
/* |
* 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; |
if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active |
permilleAcc /= 2; |
debugFullWeight = 0; |
} |
|
// 1000 * the correction amount that will be added to the gyro angle in next line. |
correction = angle[axis]; //(permilleAcc * (accDerived - angle[axis])) / 1000; |
angle[axis] = ((int32_t)(1000L - permilleAcc) * angle[axis] + (int32_t)permilleAcc * accDerived) / 1000L; |
correctionSum[axis] += angle[axis] - correction; |
DebugOut.Analog[16+axis] = angle[axis] - correction; |
} |
} else { |
DebugOut.Digital[0] &= ~DEBUG_ACC0THORDER; |
DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER; |
DebugOut.Analog[9] = 0; |
DebugOut.Analog[10] = 0; |
if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands |
permilleAcc /= 2; |
debugFullWeight = 0; |
} |
|
DebugOut.Analog[16] = 0; |
DebugOut.Analog[17] = 0; |
// experiment: Kill drift compensation updates when not flying smooth. |
correctionSum[PITCH] = correctionSum[ROLL] = 0; |
} |
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; |
|
// 1000 * the correction amount that will be added to the gyro angle in next line. |
correction = angle[axis]; //(permilleAcc * (accDerived - angle[axis])) / 1000; |
angle[axis] = ((int32_t) (1000L - permilleAcc) * angle[axis] |
+ (int32_t) permilleAcc * accDerived) / 1000L; |
correctionSum[axis] += angle[axis] - correction; |
DebugOut.Analog[16 + axis] = angle[axis] - correction; |
} |
} 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; |
} |
} |
|
/************************************************************************ |
348,108 → 363,127 |
// 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] * HIRES_GYRO_INTEGRATION_FACTOR + 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]; |
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] |
* HIRES_GYRO_INTEGRATION_FACTOR + 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[18+axis] = deltaCorrection / staticParams.GyroAccTrim; |
DebugOut.Analog[28+axis] = driftComp[axis]; |
DebugOut.Analog[18 + axis] = deltaCorrection |
/ staticParams.GyroAccTrim; |
DebugOut.Analog[28 + axis] = driftComp[axis]; |
|
correctionSum[axis] = 0; |
} |
} |
correctionSum[axis] = 0; |
} |
} |
} |
|
/************************************************************************ |
* Main procedure. |
************************************************************************/ |
void calculateFlightAttitude(void) { |
// part1: 550 usec. |
// part1a: 550 usec. |
// part1b: 60 usec. |
getAnalogData(); |
// end part1b |
integrate(); |
// end part1a |
void calculateFlightAttitude(void) { |
// part1: 550 usec. |
// part1a: 550 usec. |
// part1b: 60 usec. |
getAnalogData(); |
// end part1b |
integrate(); |
// end part1a |
|
|
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[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; |
|
#ifdef ATTITUDE_USE_ACC_SENSORS |
correctIntegralsByAcc0thOrder(); |
driftCorrection(); |
correctIntegralsByAcc0thOrder(); |
driftCorrection(); |
#endif |
// end part1 |
// end part1 |
} |
|
void updateCompass(void) { |
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 error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) % 360) - 180; |
if(abs(yawRate) > 128) { // spinning fast |
error = 0; |
} |
if(!badCompassHeading && w < 25) { |
yawGyroDrift += error; |
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; |
if(w >= 0) { |
if(!badCompassHeading) { |
v = 64 + (maxControl[PITCH] + maxControl[ROLL]) / 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; |
yawAngleDiff += v; |
} |
else |
{ // wait a while |
badCompassHeading--; |
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; |
|
/* |
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); |
|
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; |
} |
} |
} else { // ignore compass at extreme attitudes for a while |
badCompassHeading = 500; |
} |
} |
} |
|
/* |
461,32 → 495,32 |
* speed unfortunately... must find a better way) |
*/ |
/* |
void attitude_startDynamicCalibration(void) { |
dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0; |
savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000; |
} |
void attitude_startDynamicCalibration(void) { |
dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0; |
savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000; |
} |
|
void attitude_continueDynamicCalibration(void) { |
// measure dynamic offset now... |
dynamicCalPitch += hiResPitchGyro; |
dynamicCalRoll += hiResRollGyro; |
dynamicCalYaw += rawYawGyroSum; |
dynamicCalCount++; |
|
// Param6: Manual mode. The offsets are taken from Param7 and Param8. |
if (dynamicParams.UserParam6 || 1) { // currently always enabled. |
// manual mode |
driftCompPitch = dynamicParams.UserParam7 - 128; |
driftCompRoll = dynamicParams.UserParam8 - 128; |
} else { |
// use the sampled value (does not seem to work so well....) |
driftCompPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount; |
driftCompRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount; |
driftCompYaw = -dynamicCalYaw / dynamicCalCount; |
} |
|
// keep resetting these meanwhile, to avoid accumulating errors. |
setStaticAttitudeIntegrals(); |
yawAngle = 0; |
} |
*/ |
void attitude_continueDynamicCalibration(void) { |
// measure dynamic offset now... |
dynamicCalPitch += hiResPitchGyro; |
dynamicCalRoll += hiResRollGyro; |
dynamicCalYaw += rawYawGyroSum; |
dynamicCalCount++; |
|
// Param6: Manual mode. The offsets are taken from Param7 and Param8. |
if (dynamicParams.UserParam6 || 1) { // currently always enabled. |
// manual mode |
driftCompPitch = dynamicParams.UserParam7 - 128; |
driftCompRoll = dynamicParams.UserParam8 - 128; |
} else { |
// use the sampled value (does not seem to work so well....) |
driftCompPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount; |
driftCompRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount; |
driftCompYaw = -dynamicCalYaw / dynamicCalCount; |
} |
|
// keep resetting these meanwhile, to avoid accumulating errors. |
setStaticAttitudeIntegrals(); |
yawAngle = 0; |
} |
*/ |