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