Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1612 | dongfang | 1 | #include <stdlib.h> |
2 | #include <avr/io.h> |
||
3 | |||
4 | #include "attitude.h" |
||
5 | #include "dongfangMath.h" |
||
2048 | - | 6 | #include "commands.h" |
1612 | dongfang | 7 | |
1775 | - | 8 | // For scope debugging only! |
9 | #include "rc.h" |
||
10 | |||
1612 | dongfang | 11 | // where our main data flow comes from. |
12 | #include "analog.h" |
||
13 | |||
14 | #include "configuration.h" |
||
1775 | - | 15 | #include "output.h" |
1612 | dongfang | 16 | |
17 | // Some calculations are performed depending on some stick related things. |
||
18 | #include "controlMixer.h" |
||
19 | |||
20 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
||
21 | |||
22 | /* |
||
23 | * Gyro readings, as read from the analog module. It would have been nice to flow |
||
24 | * them around between the different calculations as a struct or array (doing |
||
25 | * things functionally without side effects) but this is shorter and probably |
||
26 | * faster too. |
||
27 | * The variables are overwritten at each attitude calculation invocation - the values |
||
28 | * are not preserved or reused. |
||
29 | */ |
||
1775 | - | 30 | int16_t rate_ATT[2], yawRate; |
1612 | dongfang | 31 | |
32 | // With different (less) filtering |
||
1645 | - | 33 | int16_t rate_PID[2]; |
34 | int16_t differential[2]; |
||
1612 | dongfang | 35 | |
36 | /* |
||
37 | * Gyro readings, after performing "axis coupling" - that is, the transfomation |
||
38 | * of rotation rates from the airframe-local coordinate system to a ground-fixed |
||
39 | * coordinate system. If axis copling is disabled, the gyro readings will be |
||
40 | * copied into these directly. |
||
41 | * These are global for the same pragmatic reason as with the gyro readings. |
||
42 | * The variables are overwritten at each attitude calculation invocation - the values |
||
43 | * are not preserved or reused. |
||
44 | */ |
||
1645 | - | 45 | int16_t ACRate[2], ACYawRate; |
1612 | dongfang | 46 | |
47 | /* |
||
48 | * Gyro integrals. These are the rotation angles of the airframe compared to the |
||
49 | * horizontal plane, yaw relative to yaw at start. |
||
50 | */ |
||
2048 | - | 51 | int32_t attitude[2]; |
1612 | dongfang | 52 | |
2048 | - | 53 | //int readingHeight = 0; |
1612 | dongfang | 54 | |
1805 | - | 55 | // Yaw angle and compass stuff. |
2051 | - | 56 | int32_t headingError; |
1805 | - | 57 | |
58 | // The difference between the above 2 (heading - course) on a -180..179 degree interval. |
||
59 | // Not necessary. Never read anywhere. |
||
60 | // int16_t compassOffCourse = 0; |
||
61 | |||
2051 | - | 62 | uint16_t ignoreCompassTimer = 0;// 500; |
1805 | - | 63 | |
2048 | - | 64 | int32_t heading; // Yaw Gyro Integral supported by compass |
1775 | - | 65 | int16_t yawGyroDrift; |
1612 | dongfang | 66 | |
1805 | - | 67 | int16_t correctionSum[2] = { 0, 0 }; |
1612 | dongfang | 68 | |
1775 | - | 69 | // For NaviCTRL use. |
1805 | - | 70 | int16_t averageAcc[2] = { 0, 0 }, averageAccCount = 0; |
1775 | - | 71 | |
1612 | dongfang | 72 | /* |
73 | * Experiment: Compensating for dynamic-induced gyro biasing. |
||
74 | */ |
||
1805 | - | 75 | int16_t driftComp[2] = { 0, 0 }, driftCompYaw = 0; |
1612 | dongfang | 76 | // int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0; |
77 | // int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw; |
||
78 | // int16_t dynamicCalCount; |
||
79 | |||
1980 | - | 80 | uint16_t accVector; |
81 | |||
1612 | dongfang | 82 | /************************************************************************ |
83 | * Set inclination angles from the acc. sensor data. |
||
84 | * If acc. sensors are not used, set to zero. |
||
85 | * TODO: One could use inverse sine to calculate the angles more |
||
1616 | dongfang | 86 | * accurately, but since: 1) the angles are rather small at times when |
87 | * it makes sense to set the integrals (standing on ground, or flying at |
||
1612 | dongfang | 88 | * constant speed, and 2) at small angles a, sin(a) ~= constant * a, |
89 | * it is hardly worth the trouble. |
||
90 | ************************************************************************/ |
||
91 | |||
1645 | - | 92 | int32_t getAngleEstimateFromAcc(uint8_t axis) { |
1991 | - | 93 | //int32_t correctionTerm = (dynamicParams.levelCorrection[axis] - 128) * 256L; |
2048 | - | 94 | return (int32_t) GYRO_ACC_FACTOR * (int32_t) filteredAcc[axis]; // + correctionTerm; |
2032 | - | 95 | // return 342L * filteredAcc[axis]; |
1612 | dongfang | 96 | } |
97 | |||
98 | void setStaticAttitudeAngles(void) { |
||
99 | #ifdef ATTITUDE_USE_ACC_SENSORS |
||
2048 | - | 100 | attitude[PITCH] = getAngleEstimateFromAcc(PITCH); |
101 | attitude[ROLL] = getAngleEstimateFromAcc(ROLL); |
||
1612 | dongfang | 102 | #else |
2048 | - | 103 | attitude[PITCH] = attitude[ROLL] = 0; |
1612 | dongfang | 104 | #endif |
105 | } |
||
106 | |||
107 | /************************************************************************ |
||
108 | * Neutral Readings |
||
109 | ************************************************************************/ |
||
110 | void attitude_setNeutral(void) { |
||
1869 | - | 111 | // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway. |
2032 | - | 112 | // dynamicParams.axisCoupling1 = dynamicParams.axisCoupling2 = 0; |
1612 | dongfang | 113 | |
1869 | - | 114 | driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0; |
115 | correctionSum[PITCH] = correctionSum[ROLL] = 0; |
||
1612 | dongfang | 116 | |
1869 | - | 117 | // Calibrate hardware. |
1961 | - | 118 | analog_setNeutral(); |
1612 | dongfang | 119 | |
1869 | - | 120 | // reset gyro integrals to acc guessing |
121 | setStaticAttitudeAngles(); |
||
2052 | - | 122 | #ifdef USE_MK3MAG |
2048 | - | 123 | attitude_resetHeadingToMagnetic(); |
2052 | - | 124 | #endif |
1869 | - | 125 | // Servo_On(); //enable servo output |
1612 | dongfang | 126 | } |
127 | |||
128 | /************************************************************************ |
||
129 | * Get sensor data from the analog module, and release the ADC |
||
130 | * TODO: Ultimately, the analog module could do this (instead of dumping |
||
1645 | - | 131 | * the values into variables). |
132 | * The rate variable end up in a range of about [-1024, 1023]. |
||
1612 | dongfang | 133 | *************************************************************************/ |
134 | void getAnalogData(void) { |
||
1869 | - | 135 | uint8_t axis; |
1612 | dongfang | 136 | |
1955 | - | 137 | analog_update(); |
138 | |||
1869 | - | 139 | for (axis = PITCH; axis <= ROLL; axis++) { |
1963 | - | 140 | rate_PID[axis] = gyro_PID[axis] + driftComp[axis]; |
141 | rate_ATT[axis] = gyro_ATT[axis] + driftComp[axis]; |
||
1869 | - | 142 | differential[axis] = gyroD[axis]; |
143 | averageAcc[axis] += acc[axis]; |
||
144 | } |
||
1775 | - | 145 | |
1869 | - | 146 | averageAccCount++; |
147 | yawRate = yawGyro + driftCompYaw; |
||
1612 | dongfang | 148 | } |
149 | |||
150 | /* |
||
151 | * This is the standard flight-style coordinate system transformation |
||
152 | * (from airframe-local axes to a ground-based system). For some reason |
||
153 | * the MK uses a left-hand coordinate system. The tranformation has been |
||
154 | * changed accordingly. |
||
155 | */ |
||
156 | void trigAxisCoupling(void) { |
||
2048 | - | 157 | int16_t rollAngleInDegrees = attitude[ROLL] / GYRO_DEG_FACTOR_PITCHROLL; |
158 | int16_t pitchAngleInDegrees = attitude[PITCH] / GYRO_DEG_FACTOR_PITCHROLL; |
||
1866 | - | 159 | |
2045 | - | 160 | int16_t cospitch = cos_360(pitchAngleInDegrees); |
161 | int16_t cosroll = cos_360(rollAngleInDegrees); |
||
162 | int16_t sinroll = sin_360(rollAngleInDegrees); |
||
163 | |||
2048 | - | 164 | ACRate[PITCH] = (((int32_t) rate_ATT[PITCH] * cosroll |
165 | - (int32_t) yawRate * sinroll) >> LOG_MATH_UNIT_FACTOR); |
||
1866 | - | 166 | |
2048 | - | 167 | ACRate[ROLL] = rate_ATT[ROLL] |
168 | + (((((int32_t) rate_ATT[PITCH] * sinroll + (int32_t) yawRate * cosroll) |
||
169 | >> LOG_MATH_UNIT_FACTOR) * tan_360(pitchAngleInDegrees)) |
||
170 | >> LOG_MATH_UNIT_FACTOR); |
||
1866 | - | 171 | |
2048 | - | 172 | ACYawRate = |
173 | ((int32_t) rate_ATT[PITCH] * sinroll + (int32_t) yawRate * cosroll) |
||
174 | / cospitch; |
||
1612 | dongfang | 175 | } |
176 | |||
1775 | - | 177 | // 480 usec with axis coupling - almost no time without. |
1612 | dongfang | 178 | void integrate(void) { |
1869 | - | 179 | // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate. |
180 | uint8_t axis; |
||
1872 | - | 181 | |
2058 | - | 182 | if (staticParams.bitConfig & CFG_AXIS_COUPLING_ENABLED) { |
1869 | - | 183 | trigAxisCoupling(); |
184 | } else { |
||
185 | ACRate[PITCH] = rate_ATT[PITCH]; |
||
186 | ACRate[ROLL] = rate_ATT[ROLL]; |
||
187 | ACYawRate = yawRate; |
||
188 | } |
||
1612 | dongfang | 189 | |
1869 | - | 190 | /* |
191 | * Yaw |
||
192 | * Calculate yaw gyro integral (~ to rotation angle) |
||
2048 | - | 193 | * Limit heading proportional to 0 deg to 360 deg |
1869 | - | 194 | */ |
2048 | - | 195 | heading += ACYawRate; |
196 | intervalWrap(&heading, YAWOVER360); |
||
2051 | - | 197 | headingError += ACYawRate; |
198 | |||
1869 | - | 199 | /* |
200 | * Pitch axis integration and range boundary wrap. |
||
201 | */ |
||
202 | for (axis = PITCH; axis <= ROLL; axis++) { |
||
2048 | - | 203 | attitude[axis] += ACRate[axis]; |
204 | if (attitude[axis] > PITCHROLLOVER180) { |
||
205 | attitude[axis] -= PITCHROLLOVER360; |
||
206 | } else if (attitude[axis] <= -PITCHROLLOVER180) { |
||
207 | attitude[axis] += PITCHROLLOVER360; |
||
1869 | - | 208 | } |
209 | } |
||
1612 | dongfang | 210 | } |
211 | |||
2062 | - | 212 | void correctIntegralsByAcc0thOrder_old(void) { |
213 | uint8_t axis; |
||
214 | int32_t temp; |
||
215 | |||
216 | uint8_t ca = controlActivity >> 8; |
||
217 | uint8_t highControlActivity = (ca > staticParams.maxControlActivity); |
||
218 | |||
219 | if (highControlActivity) { |
||
220 | debugOut.digital[1] |= DEBUG_ACC0THORDER; |
||
221 | } else { |
||
222 | debugOut.digital[1] &= ~DEBUG_ACC0THORDER; |
||
223 | } |
||
224 | |||
225 | if (accVector <= staticParams.maxAccVector) { |
||
226 | debugOut.digital[0] &= ~DEBUG_ACC0THORDER; |
||
227 | |||
2065 | - | 228 | uint8_t permilleAcc = staticParams.zerothOrderCorrection / 8; |
2062 | - | 229 | int32_t accDerived; |
230 | |||
231 | /* |
||
232 | if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active |
||
233 | permilleAcc /= 2; |
||
234 | debugFullWeight = 0; |
||
235 | } |
||
236 | |||
237 | if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands. Replace by controlActivity. |
||
238 | permilleAcc /= 2; |
||
239 | debugFullWeight = 0; |
||
240 | */ |
||
241 | |||
242 | if (highControlActivity) { // reduce effect during stick control activity |
||
243 | permilleAcc /= 4; |
||
244 | if (controlActivity > staticParams.maxControlActivity * 2) { // reduce effect during stick control activity |
||
245 | permilleAcc /= 4; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /* |
||
250 | * Add to each sum: The amount by which the angle is changed just below. |
||
251 | */ |
||
252 | for (axis = PITCH; axis <= ROLL; axis++) { |
||
253 | accDerived = getAngleEstimateFromAcc(axis); |
||
254 | //debugOut.analog[9 + axis] = accDerived / (GYRO_DEG_FACTOR_PITCHROLL / 10); |
||
255 | // 1000 * the correction amount that will be added to the gyro angle in next line. |
||
256 | temp = attitude[axis]; |
||
257 | attitude[axis] = ((int32_t) (1000L - permilleAcc) * temp |
||
258 | + (int32_t) permilleAcc * accDerived) / 1000L; |
||
259 | correctionSum[axis] += attitude[axis] - temp; |
||
260 | } |
||
261 | } else { |
||
262 | // experiment: Kill drift compensation updates when not flying smooth. |
||
263 | // correctionSum[PITCH] = correctionSum[ROLL] = 0; |
||
264 | debugOut.digital[0] |= DEBUG_ACC0THORDER; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | |||
1612 | dongfang | 269 | /************************************************************************ |
270 | * A kind of 0'th order integral correction, that corrects the integrals |
||
271 | * directly. This is the "gyroAccFactor" stuff in the original code. |
||
1646 | - | 272 | * There is (there) also a drift compensation |
1612 | dongfang | 273 | * - it corrects the differential of the integral = the gyro offsets. |
274 | * That should only be necessary with drifty gyros like ENC-03. |
||
275 | ************************************************************************/ |
||
2059 | - | 276 | #define LOG_DIVIDER 12 |
277 | #define DIVIDER (1L << LOG_DIVIDER) |
||
2062 | - | 278 | void correctIntegralsByAcc0thOrder_new(void) { |
1869 | - | 279 | // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities |
280 | // are less than ....., or reintroduce Kalman. |
||
281 | // Well actually the Z axis acc. check is not so silly. |
||
282 | uint8_t axis; |
||
283 | int32_t temp; |
||
1908 | - | 284 | |
2059 | - | 285 | uint16_t ca = controlActivity >> 6; |
286 | uint8_t controlActivityWeighted = ca / staticParams.zerothOrderCorrectionControlTolerance; |
||
287 | if (!controlActivityWeighted) controlActivityWeighted = 1; |
||
288 | uint8_t accVectorWeighted = accVector / staticParams.zerothOrderCorrectionAccTolerance; |
||
289 | if (!accVectorWeighted) accVectorWeighted = 1; |
||
1988 | - | 290 | |
2059 | - | 291 | uint8_t accPart = staticParams.zerothOrderCorrection; |
292 | int32_t accDerived; |
||
1988 | - | 293 | |
2059 | - | 294 | debugOut.analog[14] = controlActivity; |
295 | debugOut.analog[15] = accVector; |
||
2048 | - | 296 | |
2059 | - | 297 | /* |
298 | if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active |
||
299 | permilleAcc /= 2; |
||
300 | debugFullWeight = 0; |
||
301 | } |
||
1612 | dongfang | 302 | |
2059 | - | 303 | if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands. Replace by controlActivity. |
304 | permilleAcc /= 2; |
||
305 | debugFullWeight = 0; |
||
306 | */ |
||
1953 | - | 307 | |
2059 | - | 308 | debugOut.analog[20] = controlActivityWeighted; |
309 | debugOut.analog[21] = accVectorWeighted; |
||
310 | debugOut.analog[24] = accVector; |
||
1953 | - | 311 | |
2059 | - | 312 | accPart /= controlActivityWeighted; |
313 | accPart /= accVectorWeighted; |
||
314 | /* |
||
315 | * Add to each sum: The amount by which the angle is changed just below. |
||
316 | */ |
||
317 | for (axis = PITCH; axis <= ROLL; axis++) { |
||
318 | accDerived = getAngleEstimateFromAcc(axis); |
||
319 | //debugOut.analog[9 + axis] = accDerived / (GYRO_DEG_FACTOR_PITCHROLL / 10); |
||
320 | // 1000 * the correction amount that will be added to the gyro angle in next line. |
||
321 | temp = attitude[axis]; |
||
322 | attitude[axis] = ((int32_t) (DIVIDER - accPart) * temp + (int32_t)accPart * accDerived) >> LOG_DIVIDER; |
||
323 | correctionSum[axis] += attitude[axis] - temp; |
||
1869 | - | 324 | } |
1612 | dongfang | 325 | } |
326 | |||
327 | /************************************************************************ |
||
328 | * This is an attempt to correct not the error in the angle integrals |
||
329 | * (that happens in correctIntegralsByAcc0thOrder above) but rather the |
||
330 | * cause of it: Gyro drift, vibration and rounding errors. |
||
331 | * All the corrections made in correctIntegralsByAcc0thOrder over |
||
1646 | - | 332 | * DRIFTCORRECTION_TIME cycles are summed up. This number is |
333 | * then divided by DRIFTCORRECTION_TIME to get the approx. |
||
1612 | dongfang | 334 | * correction that should have been applied to each iteration to fix |
335 | * the error. This is then added to the dynamic offsets. |
||
336 | ************************************************************************/ |
||
1646 | - | 337 | // 2 times / sec. = 488/2 |
338 | #define DRIFTCORRECTION_TIME 256L |
||
339 | void driftCorrection(void) { |
||
1869 | - | 340 | static int16_t timer = DRIFTCORRECTION_TIME; |
341 | int16_t deltaCorrection; |
||
1872 | - | 342 | int16_t round; |
1869 | - | 343 | uint8_t axis; |
1872 | - | 344 | |
1869 | - | 345 | if (!--timer) { |
346 | timer = DRIFTCORRECTION_TIME; |
||
347 | for (axis = PITCH; axis <= ROLL; axis++) { |
||
348 | // Take the sum of corrections applied, add it to delta |
||
2048 | - | 349 | if (correctionSum[axis] >= 0) |
1872 | - | 350 | round = DRIFTCORRECTION_TIME / 2; |
351 | else |
||
352 | round = -DRIFTCORRECTION_TIME / 2; |
||
353 | deltaCorrection = (correctionSum[axis] + round) / DRIFTCORRECTION_TIME; |
||
1869 | - | 354 | // Add the delta to the compensation. So positive delta means, gyro should have higher value. |
1960 | - | 355 | driftComp[axis] += deltaCorrection / staticParams.driftCompDivider; |
356 | CHECK_MIN_MAX(driftComp[axis], -staticParams.driftCompLimit, staticParams.driftCompLimit); |
||
1869 | - | 357 | // DebugOut.Analog[11 + axis] = correctionSum[axis]; |
1955 | - | 358 | // DebugOut.Analog[16 + axis] = correctionSum[axis]; |
2035 | - | 359 | // debugOut.analog[28 + axis] = driftComp[axis]; |
1869 | - | 360 | correctionSum[axis] = 0; |
361 | } |
||
362 | } |
||
1612 | dongfang | 363 | } |
364 | |||
1980 | - | 365 | void calculateAccVector(void) { |
2048 | - | 366 | int16_t temp; |
367 | temp = filteredAcc[0] >> 3; |
||
368 | accVector = temp * temp; |
||
369 | temp = filteredAcc[1] >> 3; |
||
370 | accVector += temp * temp; |
||
371 | temp = filteredAcc[2] >> 3; |
||
372 | accVector += temp * temp; |
||
1980 | - | 373 | } |
374 | |||
2052 | - | 375 | #ifdef USE_MK3MAG |
2048 | - | 376 | void attitude_resetHeadingToMagnetic(void) { |
377 | if (commands_isCalibratingCompass()) |
||
378 | return; |
||
379 | |||
380 | // Compass is off, skip. |
||
2052 | - | 381 | if (!(staticParams.bitConfig & CFG_COMPASS_ENABLED)) |
2048 | - | 382 | return; |
383 | |||
384 | // Compass is invalid, skip. |
||
385 | if (magneticHeading < 0) |
||
386 | return; |
||
387 | |||
388 | heading = (int32_t) magneticHeading * GYRO_DEG_FACTOR_YAW; |
||
2051 | - | 389 | //targetHeading = heading; |
390 | headingError = 0; |
||
2048 | - | 391 | |
392 | debugOut.digital[0] ^= DEBUG_COMPASS; |
||
393 | } |
||
394 | |||
395 | void correctHeadingToMagnetic(void) { |
||
396 | int32_t error; |
||
397 | |||
2051 | - | 398 | if (commands_isCalibratingCompass()) { |
2059 | - | 399 | //debugOut.analog[30] = -1; |
2048 | - | 400 | return; |
2051 | - | 401 | } |
2048 | - | 402 | |
403 | // Compass is off, skip. |
||
404 | // Naaah this is assumed. |
||
405 | // if (!(staticParams.bitConfig & CFG_COMPASS_ACTIVE)) |
||
406 | // return; |
||
407 | |||
408 | // Compass is invalid, skip. |
||
2051 | - | 409 | if (magneticHeading < 0) { |
2059 | - | 410 | //debugOut.analog[30] = -2; |
2048 | - | 411 | return; |
2051 | - | 412 | } |
2048 | - | 413 | |
414 | // Spinning fast, skip |
||
2051 | - | 415 | if (abs(yawRate) > 128) { |
2059 | - | 416 | // debugOut.analog[30] = -3; |
2048 | - | 417 | return; |
2051 | - | 418 | } |
2048 | - | 419 | |
420 | // Otherwise invalidated, skip |
||
421 | if (ignoreCompassTimer) { |
||
422 | ignoreCompassTimer--; |
||
2059 | - | 423 | //debugOut.analog[30] = -4; |
2048 | - | 424 | return; |
425 | } |
||
426 | |||
2059 | - | 427 | //debugOut.analog[30] = magneticHeading; |
2058 | - | 428 | |
2048 | - | 429 | // TODO: Find computational cost of this. |
2051 | - | 430 | error = ((int32_t)magneticHeading*GYRO_DEG_FACTOR_YAW - heading); |
431 | if (error <= -YAWOVER180) error += YAWOVER360; |
||
432 | else if (error > YAWOVER180) error -= YAWOVER360; |
||
2048 | - | 433 | |
434 | // We only correct errors larger than the resolution of the compass, or else we would keep rounding the |
||
435 | // better resolution of the gyros to the worse resolution of the compass all the time. |
||
436 | // The correction should really only serve to compensate for gyro drift. |
||
437 | if(abs(error) < GYRO_DEG_FACTOR_YAW) return; |
||
438 | |||
2051 | - | 439 | int32_t correction = (error * staticParams.compassYawCorrection) >> 8; |
2055 | - | 440 | //debugOut.analog[30] = correction; |
2048 | - | 441 | |
442 | // The correction is added both to current heading (the direction in which the copter thinks it is pointing) |
||
2059 | - | 443 | // and to the heading error (the angle of yaw that the copter is off the set heading). |
2048 | - | 444 | heading += correction; |
2059 | - | 445 | headingError += correction; |
2048 | - | 446 | intervalWrap(&heading, YAWOVER360); |
447 | |||
2051 | - | 448 | // If we want a transparent flight wrt. compass correction (meaning the copter does not change attitude all |
449 | // when the compass corrects the heading - it only corrects numbers!) we want to add: |
||
450 | // This will however cause drift to remain uncorrected! |
||
451 | // headingError += correction; |
||
2055 | - | 452 | //debugOut.analog[29] = 0; |
2048 | - | 453 | } |
2052 | - | 454 | #endif |
2048 | - | 455 | |
1612 | dongfang | 456 | /************************************************************************ |
457 | * Main procedure. |
||
458 | ************************************************************************/ |
||
1805 | - | 459 | void calculateFlightAttitude(void) { |
1869 | - | 460 | getAnalogData(); |
1980 | - | 461 | calculateAccVector(); |
1869 | - | 462 | integrate(); |
1775 | - | 463 | |
1612 | dongfang | 464 | #ifdef ATTITUDE_USE_ACC_SENSORS |
2062 | - | 465 | if (staticParams.maxControlActivity) { |
466 | correctIntegralsByAcc0thOrder_old(); |
||
467 | } else { |
||
468 | correctIntegralsByAcc0thOrder_new(); |
||
469 | } |
||
1869 | - | 470 | driftCorrection(); |
1612 | dongfang | 471 | #endif |
2015 | - | 472 | |
473 | // We are done reading variables from the analog module. |
||
474 | // Interrupt-driven sensor reading may restart. |
||
475 | startAnalogConversionCycle(); |
||
1612 | dongfang | 476 | |
2052 | - | 477 | #ifdef USE_MK3MAG |
478 | if (staticParams.bitConfig & (CFG_COMPASS_ENABLED | CFG_GPS_ENABLED)) { |
||
2048 | - | 479 | correctHeadingToMagnetic(); |
1869 | - | 480 | } |
2052 | - | 481 | #endif |
1775 | - | 482 | } |
1612 | dongfang | 483 | |
484 | /* |
||
485 | * This is part of an experiment to measure average sensor offsets caused by motor vibration, |
||
486 | * and to compensate them away. It brings about some improvement, but no miracles. |
||
487 | * As long as the left stick is kept in the start-motors position, the dynamic compensation |
||
488 | * will measure the effect of vibration, to use for later compensation. So, one should keep |
||
489 | * the stick in the start-motors position for a few seconds, till all motors run (at the wrong |
||
490 | * speed unfortunately... must find a better way) |
||
491 | */ |
||
492 | /* |
||
1805 | - | 493 | void attitude_startDynamicCalibration(void) { |
494 | dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0; |
||
495 | savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000; |
||
496 | } |
||
1612 | dongfang | 497 | |
1805 | - | 498 | void attitude_continueDynamicCalibration(void) { |
499 | // measure dynamic offset now... |
||
500 | dynamicCalPitch += hiResPitchGyro; |
||
501 | dynamicCalRoll += hiResRollGyro; |
||
502 | dynamicCalYaw += rawYawGyroSum; |
||
503 | dynamicCalCount++; |
||
504 | |||
505 | // Param6: Manual mode. The offsets are taken from Param7 and Param8. |
||
506 | if (dynamicParams.UserParam6 || 1) { // currently always enabled. |
||
507 | // manual mode |
||
508 | driftCompPitch = dynamicParams.UserParam7 - 128; |
||
509 | driftCompRoll = dynamicParams.UserParam8 - 128; |
||
510 | } else { |
||
511 | // use the sampled value (does not seem to work so well....) |
||
512 | driftCompPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount; |
||
513 | driftCompRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount; |
||
514 | driftCompYaw = -dynamicCalYaw / dynamicCalCount; |
||
515 | } |
||
516 | |||
517 | // keep resetting these meanwhile, to avoid accumulating errors. |
||
518 | setStaticAttitudeIntegrals(); |
||
519 | yawAngle = 0; |
||
520 | } |
||
521 | */ |