Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1612 | dongfang | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
||
3 | // + Nur für den privaten Gebrauch |
||
4 | // + www.MikroKopter.com |
||
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
||
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
11 | // + Verkauf von Luftbildaufnahmen, usw. |
||
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
||
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
||
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
17 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
18 | // + eindeutig als Ursprung verlinkt werden |
||
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
21 | // + Benutzung auf eigene Gefahr |
||
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
25 | // + mit unserer Zustimmung zulässig |
||
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
30 | // + this list of conditions and the following disclaimer. |
||
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
32 | // + from this software without specific prior written permission. |
||
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
||
34 | // + for non-commercial use (directly or indirectly) |
||
35 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
36 | // + with our written permission |
||
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
38 | // + clearly linked as origin |
||
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
||
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
49 | // + POSSIBILITY OF SUCH DAMAGE. |
||
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
51 | |||
52 | /************************************************************************/ |
||
53 | /* Flight Attitude */ |
||
54 | /************************************************************************/ |
||
55 | |||
56 | #include <stdlib.h> |
||
57 | #include <avr/io.h> |
||
58 | |||
59 | #include "attitude.h" |
||
60 | #include "dongfangMath.h" |
||
61 | |||
62 | // where our main data flow comes from. |
||
63 | #include "analog.h" |
||
64 | |||
65 | #include "configuration.h" |
||
66 | |||
67 | // Some calculations are performed depending on some stick related things. |
||
68 | #include "controlMixer.h" |
||
69 | |||
70 | // For Servo_On / Off |
||
71 | // #include "timer2.h" |
||
72 | |||
73 | #ifdef USE_MK3MAG |
||
74 | #include "mk3mag.h" |
||
75 | #include "gps.h" |
||
76 | #endif |
||
77 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
||
78 | |||
79 | /* |
||
80 | * Gyro readings, as read from the analog module. It would have been nice to flow |
||
81 | * them around between the different calculations as a struct or array (doing |
||
82 | * things functionally without side effects) but this is shorter and probably |
||
83 | * faster too. |
||
84 | * The variables are overwritten at each attitude calculation invocation - the values |
||
85 | * are not preserved or reused. |
||
86 | */ |
||
1645 | - | 87 | int16_t rate[2], yawRate; |
1612 | dongfang | 88 | |
89 | // With different (less) filtering |
||
1645 | - | 90 | int16_t rate_PID[2]; |
91 | int16_t differential[2]; |
||
1612 | dongfang | 92 | |
93 | /* |
||
94 | * Gyro readings, after performing "axis coupling" - that is, the transfomation |
||
95 | * of rotation rates from the airframe-local coordinate system to a ground-fixed |
||
96 | * coordinate system. If axis copling is disabled, the gyro readings will be |
||
97 | * copied into these directly. |
||
98 | * These are global for the same pragmatic reason as with the gyro readings. |
||
99 | * The variables are overwritten at each attitude calculation invocation - the values |
||
100 | * are not preserved or reused. |
||
101 | */ |
||
1645 | - | 102 | int16_t ACRate[2], ACYawRate; |
1612 | dongfang | 103 | |
104 | /* |
||
105 | * Gyro integrals. These are the rotation angles of the airframe compared to the |
||
106 | * horizontal plane, yaw relative to yaw at start. |
||
107 | */ |
||
1645 | - | 108 | int32_t angle[2], yawAngle; |
1612 | dongfang | 109 | |
110 | int readingHeight = 0; |
||
111 | |||
112 | // compass course |
||
113 | int16_t compassHeading = -1; // negative angle indicates invalid data. |
||
114 | int16_t compassCourse = -1; |
||
115 | int16_t compassOffCourse = 0; |
||
116 | uint16_t updateCompassCourse = 0; |
||
117 | uint8_t compassCalState = 0; |
||
118 | |||
119 | // uint8_t FunnelCourse = 0; |
||
120 | uint16_t badCompassHeading = 500; |
||
121 | int32_t yawGyroHeading; // Yaw Gyro Integral supported by compass |
||
122 | |||
1616 | dongfang | 123 | #define PITCHROLLOVER180 (GYRO_DEG_FACTOR_PITCHROLL * 180L) |
124 | #define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L) |
||
125 | #define YAWOVER360 (GYRO_DEG_FACTOR_YAW * 360L) |
||
1612 | dongfang | 126 | |
1646 | - | 127 | int16_t correctionSum[2] = {0,0}; |
1612 | dongfang | 128 | |
129 | /* |
||
130 | * Experiment: Compensating for dynamic-induced gyro biasing. |
||
131 | */ |
||
1645 | - | 132 | int16_t dynamicOffset[2] = {0,0}, dynamicOffsetYaw = 0; |
1612 | dongfang | 133 | // int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0; |
134 | // int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw; |
||
135 | // int16_t dynamicCalCount; |
||
136 | |||
137 | /************************************************************************ |
||
138 | * Set inclination angles from the acc. sensor data. |
||
139 | * If acc. sensors are not used, set to zero. |
||
140 | * TODO: One could use inverse sine to calculate the angles more |
||
1616 | dongfang | 141 | * accurately, but since: 1) the angles are rather small at times when |
142 | * it makes sense to set the integrals (standing on ground, or flying at |
||
1612 | dongfang | 143 | * constant speed, and 2) at small angles a, sin(a) ~= constant * a, |
144 | * it is hardly worth the trouble. |
||
145 | ************************************************************************/ |
||
146 | |||
1645 | - | 147 | int32_t getAngleEstimateFromAcc(uint8_t axis) { |
148 | return GYRO_ACC_FACTOR * (int32_t)filteredAcc[axis]; |
||
1612 | dongfang | 149 | } |
150 | |||
151 | void setStaticAttitudeAngles(void) { |
||
152 | #ifdef ATTITUDE_USE_ACC_SENSORS |
||
1645 | - | 153 | angle[PITCH] = getAngleEstimateFromAcc(PITCH); |
154 | angle[ROLL] = getAngleEstimateFromAcc(ROLL); |
||
1612 | dongfang | 155 | #else |
1645 | - | 156 | angle[PITCH] = angle[ROLL] = 0; |
1612 | dongfang | 157 | #endif |
158 | } |
||
159 | |||
160 | /************************************************************************ |
||
161 | * Neutral Readings |
||
162 | ************************************************************************/ |
||
163 | void attitude_setNeutral(void) { |
||
164 | // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway. |
||
165 | dynamicParams.AxisCoupling1 = dynamicParams.AxisCoupling2 = 0; |
||
166 | |||
1645 | - | 167 | dynamicOffset[PITCH] = dynamicOffset[ROLL] = 0; |
1646 | - | 168 | correctionSum[PITCH] = correctionSum[ROLL] = 0; |
1612 | dongfang | 169 | |
170 | // Calibrate hardware. |
||
171 | analog_calibrate(); |
||
172 | |||
173 | // reset gyro readings |
||
1645 | - | 174 | rate[PITCH] = rate[ROLL] = yawRate = 0; |
1612 | dongfang | 175 | |
176 | // reset gyro integrals to acc guessing |
||
177 | setStaticAttitudeAngles(); |
||
178 | yawAngle = 0; |
||
179 | |||
180 | // update compass course to current heading |
||
181 | compassCourse = compassHeading; |
||
1646 | - | 182 | |
1612 | dongfang | 183 | // Inititialize YawGyroIntegral value with current compass heading |
184 | yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW; |
||
185 | |||
186 | // Servo_On(); //enable servo output |
||
187 | } |
||
188 | |||
189 | /************************************************************************ |
||
190 | * Get sensor data from the analog module, and release the ADC |
||
191 | * TODO: Ultimately, the analog module could do this (instead of dumping |
||
1645 | - | 192 | * the values into variables). |
193 | * The rate variable end up in a range of about [-1024, 1023]. |
||
194 | * When scaled down by CONTROL_SCALING, the interval is about [-256, 256]. |
||
1612 | dongfang | 195 | *************************************************************************/ |
196 | void getAnalogData(void) { |
||
1645 | - | 197 | uint8_t axis; |
198 | |||
199 | for (axis=PITCH; axis <=ROLL; axis++) { |
||
200 | rate_PID[axis] = (gyro_PID[axis] + dynamicOffset[axis]) / HIRES_GYRO_INTEGRATION_FACTOR; |
||
201 | rate[axis] = (gyro_ATT[axis] + dynamicOffset[axis]) / HIRES_GYRO_INTEGRATION_FACTOR; |
||
202 | differential[axis] = gyroD[axis]; |
||
203 | } |
||
1646 | - | 204 | |
1612 | dongfang | 205 | yawRate = yawGyro + dynamicOffsetYaw; |
206 | |||
1645 | - | 207 | // We are done reading variables from the analog module. |
208 | // Interrupt-driven sensor reading may restart. |
||
1612 | dongfang | 209 | analogDataReady = 0; |
210 | analog_start(); |
||
211 | } |
||
212 | |||
213 | /* |
||
214 | * This is the standard flight-style coordinate system transformation |
||
215 | * (from airframe-local axes to a ground-based system). For some reason |
||
216 | * the MK uses a left-hand coordinate system. The tranformation has been |
||
217 | * changed accordingly. |
||
218 | */ |
||
219 | void trigAxisCoupling(void) { |
||
1645 | - | 220 | int16_t cospitch = int_cos(angle[PITCH]); |
221 | int16_t cosroll = int_cos(angle[ROLL]); |
||
222 | int16_t sinroll = int_sin(angle[ROLL]); |
||
223 | int16_t tanpitch = int_tan(angle[PITCH]); |
||
1612 | dongfang | 224 | #define ANTIOVF 1024 |
1645 | - | 225 | ACRate[PITCH] = ((int32_t) rate[PITCH] * cosroll - (int32_t)yawRate * sinroll) / (int32_t)MATH_UNIT_FACTOR; |
226 | 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)); |
||
227 | ACYawRate = ((int32_t) rate[PITCH] * sinroll) / cospitch + ((int32_t)yawRate * cosroll) / cospitch; |
||
1612 | dongfang | 228 | } |
229 | |||
230 | void integrate(void) { |
||
231 | // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate. |
||
1645 | - | 232 | uint8_t axis; |
233 | |||
1612 | dongfang | 234 | if(!looping && (staticParams.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) { |
235 | // The rotary rate limiter bit is abused for selecting axis coupling algorithm instead. |
||
1645 | - | 236 | trigAxisCoupling(); |
1612 | dongfang | 237 | } else { |
1645 | - | 238 | ACRate[PITCH] = rate[PITCH]; |
239 | ACRate[ROLL] = rate[ROLL]; |
||
1612 | dongfang | 240 | ACYawRate = yawRate; |
241 | } |
||
242 | |||
243 | /* |
||
244 | * Yaw |
||
245 | * Calculate yaw gyro integral (~ to rotation angle) |
||
246 | * Limit yawGyroHeading proportional to 0 deg to 360 deg |
||
247 | */ |
||
248 | yawGyroHeading += ACYawRate; |
||
1617 | dongfang | 249 | |
250 | // Why is yawAngle not wrapped 'round? |
||
1612 | dongfang | 251 | yawAngle += ACYawRate; |
1617 | dongfang | 252 | |
253 | if(yawGyroHeading >= YAWOVER360) { |
||
254 | yawGyroHeading -= YAWOVER360; // 360 deg. wrap |
||
255 | } else if(yawGyroHeading < 0) { |
||
256 | yawGyroHeading += YAWOVER360; |
||
257 | } |
||
1612 | dongfang | 258 | |
259 | /* |
||
260 | * Pitch axis integration and range boundary wrap. |
||
261 | */ |
||
1645 | - | 262 | for (axis=PITCH; axis<=ROLL; axis++) { |
263 | angle[axis] += ACRate[axis]; |
||
264 | if(angle[axis] > PITCHROLLOVER180) { |
||
265 | angle[axis] -= PITCHROLLOVER360; |
||
266 | } else if (angle[axis] <= -PITCHROLLOVER180) { |
||
267 | angle[axis] += PITCHROLLOVER360; |
||
268 | } |
||
1612 | dongfang | 269 | } |
270 | } |
||
271 | |||
272 | /************************************************************************ |
||
273 | * A kind of 0'th order integral correction, that corrects the integrals |
||
274 | * directly. This is the "gyroAccFactor" stuff in the original code. |
||
1646 | - | 275 | * There is (there) also a drift compensation |
1612 | dongfang | 276 | * - it corrects the differential of the integral = the gyro offsets. |
277 | * That should only be necessary with drifty gyros like ENC-03. |
||
278 | ************************************************************************/ |
||
279 | void correctIntegralsByAcc0thOrder(void) { |
||
280 | // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities |
||
281 | // are less than ....., or reintroduce Kalman. |
||
282 | // Well actually the Z axis acc. check is not so silly. |
||
1645 | - | 283 | uint8_t axis; |
1646 | - | 284 | int32_t correction; |
285 | if(!looping && acc[Z] >= -dynamicParams.UserParams[7] && acc[Z] <= dynamicParams.UserParams[7]) { |
||
1612 | dongfang | 286 | DebugOut.Digital[0] = 1; |
287 | |||
288 | uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!! |
||
289 | uint8_t debugFullWeight = 1; |
||
1646 | - | 290 | int32_t accDerived; |
1612 | dongfang | 291 | |
1645 | - | 292 | if((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands |
1612 | dongfang | 293 | permilleAcc /= 2; |
294 | debugFullWeight = 0; |
||
295 | } |
||
296 | |||
297 | if(abs(controlYaw) > 25) { // reduce further if yaw stick is active |
||
298 | permilleAcc /= 2; |
||
299 | debugFullWeight = 0; |
||
300 | } |
||
301 | |||
302 | /* |
||
303 | * Add to each sum: The amount by which the angle is changed just below. |
||
304 | */ |
||
1645 | - | 305 | for (axis=PITCH; axis<=ROLL; axis++) { |
1646 | - | 306 | accDerived = getAngleEstimateFromAcc(axis); |
307 | DebugOut.Analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL; |
||
308 | |||
309 | // 1000 * the correction amount that will be added to the gyro angle in next line. |
||
310 | correction = angle[axis]; //(permilleAcc * (accDerived - angle[axis])) / 1000; |
||
311 | angle[axis] = ((int32_t)(1000 - permilleAcc) * angle[axis] + (int32_t)permilleAcc * accDerived) / 1000L; |
||
312 | |||
313 | correctionSum[axis] += angle[axis] - correction; |
||
1612 | dongfang | 314 | |
1645 | - | 315 | // There should not be a risk of overflow here, since the integrals do not exceed a few 100000. |
1646 | - | 316 | // change = ((1000 - permilleAcc) * angle[axis] + permilleAcc * accDerived) / 1000 - angle[axis] |
317 | // = (1000 * angle[axis] - permilleAcc * angle[axis] + permilleAcc * accDerived) / 1000 - angle[axis] |
||
318 | // = (- permilleAcc * angle[axis] + permilleAcc * accDerived) / 1000 |
||
319 | // = permilleAcc * (accDerived - angle[axis]) / 1000 |
||
320 | |||
321 | // Experiment: Do not acutally apply the correction. See if drift compensation does that. |
||
322 | // angle[axis] += correction / 1000; |
||
1645 | - | 323 | } |
324 | |||
1612 | dongfang | 325 | DebugOut.Digital[1] = debugFullWeight; |
326 | } else { |
||
327 | DebugOut.Digital[0] = 0; |
||
328 | } |
||
329 | } |
||
330 | |||
331 | /************************************************************************ |
||
332 | * This is an attempt to correct not the error in the angle integrals |
||
333 | * (that happens in correctIntegralsByAcc0thOrder above) but rather the |
||
334 | * cause of it: Gyro drift, vibration and rounding errors. |
||
335 | * All the corrections made in correctIntegralsByAcc0thOrder over |
||
1646 | - | 336 | * DRIFTCORRECTION_TIME cycles are summed up. This number is |
337 | * then divided by DRIFTCORRECTION_TIME to get the approx. |
||
1612 | dongfang | 338 | * correction that should have been applied to each iteration to fix |
339 | * the error. This is then added to the dynamic offsets. |
||
340 | ************************************************************************/ |
||
1646 | - | 341 | // 2 times / sec. = 488/2 |
342 | #define DRIFTCORRECTION_TIME 256L |
||
343 | void driftCorrection(void) { |
||
1612 | dongfang | 344 | static int16_t timer = DRIFTCORRECTION_TIME; |
1646 | - | 345 | int16_t deltaCorrection; |
1645 | - | 346 | uint8_t axis; |
1612 | dongfang | 347 | if (! --timer) { |
348 | timer = DRIFTCORRECTION_TIME; |
||
1645 | - | 349 | for (axis=PITCH; axis<=ROLL; axis++) { |
1646 | - | 350 | // Take the sum of corrections applied, add it to delta |
351 | deltaCorrection = ((correctionSum[axis] + DRIFTCORRECTION_TIME / 2) * HIRES_GYRO_INTEGRATION_FACTOR) / DRIFTCORRECTION_TIME; |
||
352 | // Add the delta to the compensation. So positive delta means, gyro should have higher value. |
||
353 | dynamicOffset[axis] += deltaCorrection / staticParams.GyroAccTrim; |
||
354 | CHECK_MIN_MAX(dynamicOffset[axis], -staticParams.DriftComp, staticParams.DriftComp); |
||
355 | DebugOut.Analog[11 + axis] = correctionSum[axis]; |
||
356 | DebugOut.Analog[28 + axis] = dynamicOffset[axis]; |
||
1645 | - | 357 | correctionSum[axis] = 0; |
358 | } |
||
1612 | dongfang | 359 | } |
360 | } |
||
361 | |||
362 | /************************************************************************ |
||
363 | * Main procedure. |
||
364 | ************************************************************************/ |
||
365 | void calculateFlightAttitude(void) { |
||
366 | getAnalogData(); |
||
367 | integrate(); |
||
1646 | - | 368 | |
369 | DebugOut.Analog[6] = ACRate[PITCH]; |
||
370 | DebugOut.Analog[7] = ACRate[ROLL]; |
||
371 | DebugOut.Analog[8] = ACYawRate; |
||
372 | |||
373 | DebugOut.Analog[3] = rate_PID[PITCH]; |
||
374 | DebugOut.Analog[4] = rate_PID[ROLL]; |
||
375 | DebugOut.Analog[5] = yawRate; |
||
376 | |||
1612 | dongfang | 377 | #ifdef ATTITUDE_USE_ACC_SENSORS |
378 | correctIntegralsByAcc0thOrder(); |
||
1646 | - | 379 | driftCorrection(); |
1612 | dongfang | 380 | #endif |
381 | } |
||
382 | |||
383 | /* |
||
1645 | - | 384 | void updateCompass(void) { |
1612 | dongfang | 385 | int16_t w, v, r,correction, error; |
386 | |||
387 | if(compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) { |
||
1645 | - | 388 | setCompassCalState(); |
1612 | dongfang | 389 | } else { |
1645 | - | 390 | // get maximum attitude angle |
391 | w = abs(pitchAngle / 512); |
||
392 | v = abs(rollAngle / 512); |
||
393 | if(v > w) w = v; |
||
394 | correction = w / 8 + 1; |
||
395 | // calculate the deviation of the yaw gyro heading and the compass heading |
||
396 | if (compassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined |
||
397 | else error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) % 360) - 180; |
||
398 | if(abs(yawRate) > 128) { // spinning fast |
||
399 | error = 0; |
||
1612 | dongfang | 400 | } |
1645 | - | 401 | if(!badCompassHeading && w < 25) { |
402 | if(updateCompassCourse) { |
||
403 | beep(200); |
||
404 | yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW; |
||
405 | compassCourse = (int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW); |
||
406 | updateCompassCourse = 0; |
||
407 | } |
||
408 | } |
||
409 | yawGyroHeading += (error * 8) / correction; |
||
410 | w = (w * dynamicParams.CompassYawEffect) / 32; |
||
411 | w = dynamicParams.CompassYawEffect - w; |
||
412 | if(w >= 0) { |
||
413 | if(!badCompassHeading) { |
||
414 | v = 64 + (maxControlPitch + maxControlRoll) / 8; |
||
415 | // calc course deviation |
||
416 | r = ((540 + (yawGyroHeading / GYRO_DEG_FACTOR_YAW) - compassCourse) % 360) - 180; |
||
417 | v = (r * w) / v; // align to compass course |
||
418 | // limit yaw rate |
||
419 | w = 3 * dynamicParams.CompassYawEffect; |
||
420 | if (v > w) v = w; |
||
421 | else if (v < -w) v = -w; |
||
422 | yawAngle += v; |
||
423 | } |
||
424 | else |
||
425 | { // wait a while |
||
426 | badCompassHeading--; |
||
427 | } |
||
428 | } |
||
429 | else { // ignore compass at extreme attitudes for a while |
||
430 | badCompassHeading = 500; |
||
431 | } |
||
432 | } |
||
433 | } |
||
1612 | dongfang | 434 | */ |
435 | |||
436 | /* |
||
437 | * This is part of an experiment to measure average sensor offsets caused by motor vibration, |
||
438 | * and to compensate them away. It brings about some improvement, but no miracles. |
||
439 | * As long as the left stick is kept in the start-motors position, the dynamic compensation |
||
440 | * will measure the effect of vibration, to use for later compensation. So, one should keep |
||
441 | * the stick in the start-motors position for a few seconds, till all motors run (at the wrong |
||
442 | * speed unfortunately... must find a better way) |
||
443 | */ |
||
444 | /* |
||
1645 | - | 445 | void attitude_startDynamicCalibration(void) { |
1612 | dongfang | 446 | dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0; |
447 | savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000; |
||
1645 | - | 448 | } |
1612 | dongfang | 449 | |
1645 | - | 450 | void attitude_continueDynamicCalibration(void) { |
1612 | dongfang | 451 | // measure dynamic offset now... |
452 | dynamicCalPitch += hiResPitchGyro; |
||
453 | dynamicCalRoll += hiResRollGyro; |
||
454 | dynamicCalYaw += rawYawGyroSum; |
||
455 | dynamicCalCount++; |
||
456 | |||
457 | // Param6: Manual mode. The offsets are taken from Param7 and Param8. |
||
458 | if (dynamicParams.UserParam6 || 1) { // currently always enabled. |
||
1645 | - | 459 | // manual mode |
460 | dynamicOffsetPitch = dynamicParams.UserParam7 - 128; |
||
461 | dynamicOffsetRoll = dynamicParams.UserParam8 - 128; |
||
1612 | dongfang | 462 | } else { |
1645 | - | 463 | // use the sampled value (does not seem to work so well....) |
464 | dynamicOffsetPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount; |
||
465 | dynamicOffsetRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount; |
||
466 | dynamicOffsetYaw = -dynamicCalYaw / dynamicCalCount; |
||
1612 | dongfang | 467 | } |
468 | |||
469 | // keep resetting these meanwhile, to avoid accumulating errors. |
||
470 | setStaticAttitudeIntegrals(); |
||
471 | yawAngle = 0; |
||
1645 | - | 472 | } |
1612 | dongfang | 473 | */ |