Rev 1634 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1634 | Rev 1645 | ||
---|---|---|---|
Line 82... | Line 82... | ||
82 | * things functionally without side effects) but this is shorter and probably |
82 | * things functionally without side effects) but this is shorter and probably |
83 | * faster too. |
83 | * faster too. |
84 | * The variables are overwritten at each attitude calculation invocation - the values |
84 | * The variables are overwritten at each attitude calculation invocation - the values |
85 | * are not preserved or reused. |
85 | * are not preserved or reused. |
86 | */ |
86 | */ |
87 | int16_t pitchRate, rollRate, yawRate; |
87 | int16_t rate[2], yawRate; |
Line 88... | Line 88... | ||
88 | 88 | ||
89 | // With different (less) filtering |
89 | // With different (less) filtering |
90 | int16_t pitchRate_PID, rollRate_PID; |
90 | int16_t rate_PID[2]; |
Line 91... | Line 91... | ||
91 | int16_t pitchDifferential, rollDifferential; |
91 | int16_t differential[2]; |
92 | 92 | ||
93 | /* |
93 | /* |
94 | * Gyro readings, after performing "axis coupling" - that is, the transfomation |
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 |
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 |
96 | * coordinate system. If axis copling is disabled, the gyro readings will be |
97 | * copied into these directly. |
97 | * copied into these directly. |
98 | * These are global for the same pragmatic reason as with the gyro readings. |
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 |
99 | * The variables are overwritten at each attitude calculation invocation - the values |
100 | * are not preserved or reused. |
100 | * are not preserved or reused. |
Line 101... | Line 101... | ||
101 | */ |
101 | */ |
102 | int16_t ACPitchRate, ACRollRate, ACYawRate; |
102 | int16_t ACRate[2], ACYawRate; |
103 | 103 | ||
104 | /* |
104 | /* |
105 | * Gyro integrals. These are the rotation angles of the airframe compared to the |
105 | * Gyro integrals. These are the rotation angles of the airframe compared to the |
Line 106... | Line 106... | ||
106 | * horizontal plane, yaw relative to yaw at start. |
106 | * horizontal plane, yaw relative to yaw at start. |
Line 107... | Line 107... | ||
107 | */ |
107 | */ |
108 | int32_t pitchAngle, rollAngle, yawAngle; |
108 | int32_t angle[2], yawAngle; |
Line 122... | Line 122... | ||
122 | 122 | ||
123 | #define PITCHROLLOVER180 (GYRO_DEG_FACTOR_PITCHROLL * 180L) |
123 | #define PITCHROLLOVER180 (GYRO_DEG_FACTOR_PITCHROLL * 180L) |
124 | #define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L) |
124 | #define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L) |
Line 125... | Line 125... | ||
125 | #define YAWOVER360 (GYRO_DEG_FACTOR_YAW * 360L) |
125 | #define YAWOVER360 (GYRO_DEG_FACTOR_YAW * 360L) |
Line 126... | Line 126... | ||
126 | 126 | ||
127 | int32_t pitchCorrectionSum = 0, rollCorrectionSum = 0; |
127 | int32_t correctionSum[2] = {0,0}; |
128 | 128 | ||
129 | /* |
129 | /* |
130 | * Experiment: Compensating for dynamic-induced gyro biasing. |
130 | * Experiment: Compensating for dynamic-induced gyro biasing. |
131 | */ |
131 | */ |
132 | int16_t dynamicOffsetPitch = 0, dynamicOffsetRoll = 0, dynamicOffsetYaw = 0; |
132 | int16_t dynamicOffset[2] = {0,0}, dynamicOffsetYaw = 0; |
Line 133... | Line 133... | ||
133 | // int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0; |
133 | // int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0; |
Line 142... | Line 142... | ||
142 | * it makes sense to set the integrals (standing on ground, or flying at |
142 | * it makes sense to set the integrals (standing on ground, or flying at |
143 | * constant speed, and 2) at small angles a, sin(a) ~= constant * a, |
143 | * constant speed, and 2) at small angles a, sin(a) ~= constant * a, |
144 | * it is hardly worth the trouble. |
144 | * it is hardly worth the trouble. |
145 | ************************************************************************/ |
145 | ************************************************************************/ |
Line 146... | Line -... | ||
146 | - | ||
147 | int32_t getPitchAngleEstimateFromAcc(void) { |
- | |
148 | return GYRO_ACC_FACTOR * (int32_t)filteredPitchAxisAcc; |
- | |
149 | } |
- | |
150 | 146 | ||
151 | int32_t getRollAngleEstimateFromAcc(void) { |
147 | int32_t getAngleEstimateFromAcc(uint8_t axis) { |
152 | return GYRO_ACC_FACTOR * (int32_t)filteredRollAxisAcc; |
148 | return GYRO_ACC_FACTOR * (int32_t)filteredAcc[axis]; |
Line 153... | Line 149... | ||
153 | } |
149 | } |
154 | 150 | ||
155 | void setStaticAttitudeAngles(void) { |
151 | void setStaticAttitudeAngles(void) { |
156 | #ifdef ATTITUDE_USE_ACC_SENSORS |
152 | #ifdef ATTITUDE_USE_ACC_SENSORS |
157 | pitchAngle = getPitchAngleEstimateFromAcc(); |
153 | angle[PITCH] = getAngleEstimateFromAcc(PITCH); |
158 | rollAngle = getRollAngleEstimateFromAcc(); |
- | |
159 | #else |
154 | angle[ROLL] = getAngleEstimateFromAcc(ROLL); |
160 | pitchAngle = 0; |
155 | #else |
161 | rollAngle = 0; |
156 | angle[PITCH] = angle[ROLL] = 0; |
Line 162... | Line 157... | ||
162 | #endif |
157 | #endif |
163 | } |
158 | } |
164 | 159 | ||
165 | /************************************************************************ |
160 | /************************************************************************ |
166 | * Neutral Readings |
161 | * Neutral Readings |
167 | ************************************************************************/ |
162 | ************************************************************************/ |
Line 168... | Line 163... | ||
168 | void attitude_setNeutral(void) { |
163 | void attitude_setNeutral(void) { |
Line 169... | Line 164... | ||
169 | // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway. |
164 | // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway. |
170 | dynamicParams.AxisCoupling1 = dynamicParams.AxisCoupling2 = 0; |
165 | dynamicParams.AxisCoupling1 = dynamicParams.AxisCoupling2 = 0; |
Line 171... | Line 166... | ||
171 | 166 | ||
172 | dynamicOffsetPitch = dynamicOffsetRoll = 0; |
167 | dynamicOffset[PITCH] = dynamicOffset[ROLL] = 0; |
Line 173... | Line 168... | ||
173 | 168 | ||
174 | // Calibrate hardware. |
169 | // Calibrate hardware. |
175 | analog_calibrate(); |
170 | analog_calibrate(); |
Line 190... | Line 185... | ||
190 | } |
185 | } |
Line 191... | Line 186... | ||
191 | 186 | ||
192 | /************************************************************************ |
187 | /************************************************************************ |
193 | * Get sensor data from the analog module, and release the ADC |
188 | * Get sensor data from the analog module, and release the ADC |
194 | * TODO: Ultimately, the analog module could do this (instead of dumping |
189 | * TODO: Ultimately, the analog module could do this (instead of dumping |
- | 190 | * the values into variables). |
|
- | 191 | * The rate variable end up in a range of about [-1024, 1023]. |
|
195 | * the values into variables). |
192 | * When scaled down by CONTROL_SCALING, the interval is about [-256, 256]. |
196 | *************************************************************************/ |
193 | *************************************************************************/ |
197 | void getAnalogData(void) { |
- | |
198 | // For the differential calculation. Diff. is not supported right now. |
194 | void getAnalogData(void) { |
199 | // int16_t d2Pitch, d2Roll; |
- | |
200 | pitchRate_PID = (hiResPitchGyro + dynamicOffsetPitch) / HIRES_GYRO_INTEGRATION_FACTOR; |
- | |
201 | pitchRate = (filteredHiResPitchGyro + dynamicOffsetPitch) / HIRES_GYRO_INTEGRATION_FACTOR; |
- | |
202 | pitchDifferential = pitchGyroD; |
195 | uint8_t axis; |
- | 196 | ||
203 | 197 | for (axis=PITCH; axis <=ROLL; axis++) { |
|
204 | rollRate_PID = (hiResRollGyro + dynamicOffsetRoll) / HIRES_GYRO_INTEGRATION_FACTOR; |
198 | rate_PID[axis] = (gyro_PID[axis] + dynamicOffset[axis]) / HIRES_GYRO_INTEGRATION_FACTOR; |
205 | rollRate = (filteredHiResRollGyro + dynamicOffsetRoll) / HIRES_GYRO_INTEGRATION_FACTOR; |
199 | rate[axis] = (gyro_ATT[axis] + dynamicOffset[axis]) / HIRES_GYRO_INTEGRATION_FACTOR; |
206 | rollDifferential = rollGyroD; |
200 | differential[axis] = gyroD[axis]; |
207 | 201 | } |
|
Line 208... | Line 202... | ||
208 | yawRate = yawGyro + dynamicOffsetYaw; |
202 | yawRate = yawGyro + dynamicOffsetYaw; |
- | 203 | ||
209 | 204 | // We are done reading variables from the analog module. |
|
210 | // We are done reading variables from the analog module. Interrupt-driven sensor reading may restart. |
205 | // Interrupt-driven sensor reading may restart. |
211 | analogDataReady = 0; |
206 | analogDataReady = 0; |
Line 212... | Line -... | ||
212 | analog_start(); |
- | |
213 | } |
- | |
214 | - | ||
215 | /************************************************************************ |
- | |
216 | * Axis coupling, H&I Style |
- | |
217 | * Currently not working (and there is a bug in it, |
- | |
218 | * which causes unstable flight in heading-hold mode). |
- | |
219 | ************************************************************************/ |
- | |
220 | void H_and_I_axisCoupling(void) { |
- | |
221 | int32_t tmpl = 0, tmpl2 = 0, tmp13 = 0, tmp14 = 0; |
- | |
222 | int16_t CouplingNickRoll = 0, CouplingRollNick = 0; |
- | |
223 | - | ||
224 | tmp13 = (rollRate * pitchAngle) / 2048L; |
- | |
225 | tmp13 *= dynamicParams.AxisCoupling2; // 65 |
- | |
226 | tmp13 /= 4096L; |
- | |
227 | CouplingNickRoll = tmp13; |
- | |
228 | - | ||
229 | tmp14 = (pitchRate * rollAngle) / 2048L; |
- | |
230 | tmp14 *= dynamicParams.AxisCoupling2; // 65 |
- | |
231 | tmp14 /= 4096L; |
- | |
232 | CouplingRollNick = tmp14; |
- | |
233 | - | ||
234 | tmp14 -= tmp13; |
- | |
235 | - | ||
236 | ACYawRate = yawRate + tmp14; |
- | |
237 | - | ||
238 | /* |
- | |
239 | if(!dynamicParams.AxisCouplingYawCorrection) ACYawRate = yawRate - tmp14 / 2; // force yaw |
- | |
240 | else ACYawRate |
- | |
241 | */ |
- | |
242 | - | ||
243 | tmpl = ((yawRate + tmp14) * pitchAngle) / 2048L; |
- | |
244 | tmpl *= dynamicParams.AxisCoupling1; |
- | |
245 | tmpl /= 4096L; |
- | |
246 | - | ||
247 | tmpl2 = ((yawRate + tmp14) * rollAngle) / 2048L; |
- | |
248 | tmpl2 *= dynamicParams.AxisCoupling1; |
- | |
249 | tmpl2 /= 4096L; |
- | |
250 | - | ||
251 | // if(abs(yawRate > 64)) { |
- | |
252 | // if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1; |
- | |
253 | // } |
- | |
254 | - | ||
255 | ACPitchRate = pitchRate - tmpl2 + tmpl / 100L; |
- | |
256 | ACRollRate = rollRate + tmpl - tmpl2 / 100L; |
207 | analog_start(); |
257 | } |
208 | } |
258 | 209 | ||
259 | /* |
210 | /* |
260 | * This is the standard flight-style coordinate system transformation |
211 | * This is the standard flight-style coordinate system transformation |
261 | * (from airframe-local axes to a ground-based system). For some reason |
212 | * (from airframe-local axes to a ground-based system). For some reason |
262 | * the MK uses a left-hand coordinate system. The tranformation has been |
213 | * the MK uses a left-hand coordinate system. The tranformation has been |
263 | * changed accordingly. |
214 | * changed accordingly. |
264 | */ |
215 | */ |
265 | void trigAxisCoupling(void) { |
216 | void trigAxisCoupling(void) { |
266 | int16_t cospitch = int_cos(pitchAngle); |
217 | int16_t cospitch = int_cos(angle[PITCH]); |
267 | int16_t cosroll = int_cos(rollAngle); |
218 | int16_t cosroll = int_cos(angle[ROLL]); |
268 | int16_t sinroll = int_sin(rollAngle); |
219 | int16_t sinroll = int_sin(angle[ROLL]); |
269 | int16_t tanpitch = int_tan(pitchAngle); |
220 | int16_t tanpitch = int_tan(angle[PITCH]); |
270 | #define ANTIOVF 1024 |
221 | #define ANTIOVF 1024 |
271 | ACPitchRate = ((int32_t)pitchRate * cosroll - (int32_t)yawRate * sinroll) / (int32_t)MATH_UNIT_FACTOR; |
222 | ACRate[PITCH] = ((int32_t) rate[PITCH] * cosroll - (int32_t)yawRate * sinroll) / (int32_t)MATH_UNIT_FACTOR; |
Line 272... | Line 223... | ||
272 | ACRollRate = rollRate + (((int32_t)pitchRate * sinroll / ANTIOVF * tanpitch + (int32_t)yawRate * int_cos(rollAngle) / ANTIOVF * tanpitch) / ((int32_t)MATH_UNIT_FACTOR / ANTIOVF * MATH_UNIT_FACTOR)); |
223 | 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)); |
273 | ACYawRate = ((int32_t)pitchRate * sinroll) / cospitch + ((int32_t)yawRate * cosroll) / cospitch; |
224 | ACYawRate = ((int32_t) rate[PITCH] * sinroll) / cospitch + ((int32_t)yawRate * cosroll) / cospitch; |
- | 225 | } |
|
- | 226 | ||
274 | } |
227 | void integrate(void) { |
275 | 228 | // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate. |
|
276 | void integrate(void) { |
- | |
277 | // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate. |
229 | uint8_t axis; |
278 | if(!looping && (staticParams.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) { |
- | |
279 | // The rotary rate limiter bit is abused for selecting axis coupling algorithm instead. |
- | |
280 | if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER) |
230 | |
281 | trigAxisCoupling(); |
231 | if(!looping && (staticParams.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) { |
282 | else |
232 | // The rotary rate limiter bit is abused for selecting axis coupling algorithm instead. |
283 | H_and_I_axisCoupling(); |
233 | trigAxisCoupling(); |
284 | } else { |
234 | } else { |
Line 285... | Line 235... | ||
285 | ACPitchRate = pitchRate; |
235 | ACRate[PITCH] = rate[PITCH]; |
286 | ACRollRate = rollRate; |
- | |
287 | ACYawRate = yawRate; |
236 | ACRate[ROLL] = rate[ROLL]; |
288 | } |
- | |
289 | 237 | ACYawRate = yawRate; |
|
290 | DebugOut.Analog[3] = pitchRate; |
- | |
291 | // DebugOut.Analog[3 + 3] = ACPitchRate; |
- | |
292 | DebugOut.Analog[4] = rollRate; |
- | |
293 | // DebugOut.Analog[4 + 3] = ACRollRate; |
- | |
294 | DebugOut.Analog[5] = yawRate; |
- | |
295 | // DebugOut.Analog[5 + 3] = ACYawRate; |
- | |
296 | - | ||
Line 297... | Line 238... | ||
297 | /* |
238 | } |
298 | DebugOut.Analog[9] = int_cos(pitchAngle); |
239 | |
299 | DebugOut.Analog[10] = int_sin(pitchAngle); |
240 | DebugOut.Analog[3] = ACRate[PITCH]; |
300 | DebugOut.Analog[11] = int_tan(pitchAngle); |
241 | DebugOut.Analog[4] = ACRate[ROLL]; |
301 | */ |
242 | DebugOut.Analog[5] = ACYawRate; |
302 | - | ||
303 | /* |
243 | |
Line 304... | Line 244... | ||
304 | * Yaw |
244 | /* |
305 | * Calculate yaw gyro integral (~ to rotation angle) |
245 | * Yaw |
Line 318... | Line 258... | ||
318 | } |
258 | } |
Line 319... | Line 259... | ||
319 | 259 | ||
320 | /* |
260 | /* |
321 | * Pitch axis integration and range boundary wrap. |
261 | * Pitch axis integration and range boundary wrap. |
- | 262 | */ |
|
322 | */ |
263 | for (axis=PITCH; axis<=ROLL; axis++) { |
323 | pitchAngle += ACPitchRate; |
264 | angle[axis] += ACRate[axis]; |
324 | if(pitchAngle > PITCHROLLOVER180) { |
265 | if(angle[axis] > PITCHROLLOVER180) { |
325 | pitchAngle -= PITCHROLLOVER360; |
266 | angle[axis] -= PITCHROLLOVER360; |
326 | } else if (pitchAngle <= -PITCHROLLOVER180) { |
267 | } else if (angle[axis] <= -PITCHROLLOVER180) { |
327 | pitchAngle += PITCHROLLOVER360; |
- | |
328 | } |
- | |
329 | - | ||
330 | /* |
- | |
331 | * Pitch axis integration and range boundary wrap. |
268 | angle[axis] += PITCHROLLOVER360; |
332 | */ |
- | |
333 | rollAngle += ACRollRate; |
- | |
334 | if(rollAngle > PITCHROLLOVER180) { |
- | |
335 | rollAngle -= PITCHROLLOVER360; |
- | |
336 | } else if (rollAngle <= -PITCHROLLOVER180) { |
- | |
337 | rollAngle += PITCHROLLOVER360; |
269 | } |
338 | } |
270 | } |
Line 339... | Line 271... | ||
339 | } |
271 | } |
340 | 272 | ||
Line 347... | Line 279... | ||
347 | ************************************************************************/ |
279 | ************************************************************************/ |
348 | void correctIntegralsByAcc0thOrder(void) { |
280 | void correctIntegralsByAcc0thOrder(void) { |
349 | // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities |
281 | // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities |
350 | // are less than ....., or reintroduce Kalman. |
282 | // are less than ....., or reintroduce Kalman. |
351 | // Well actually the Z axis acc. check is not so silly. |
283 | // Well actually the Z axis acc. check is not so silly. |
- | 284 | uint8_t axis; |
|
352 | if(!looping && //((ZAxisAcc >= -4) || (MKFlags & MKFLAG_MOTOR_RUN))) { // if not looping in any direction |
285 | if(!looping && //((ZAcc >= -4) || (MKFlags & MKFLAG_MOTOR_RUN))) { // if not looping in any direction |
353 | ZAxisAcc >= -dynamicParams.UserParams[7] && ZAxisAcc <= dynamicParams.UserParams[7]) { |
286 | ZAcc >= -dynamicParams.UserParams[7] && ZAcc <= dynamicParams.UserParams[7]) { |
354 | DebugOut.Digital[0] = 1; |
287 | DebugOut.Digital[0] = 1; |
Line 355... | Line 288... | ||
355 | 288 | ||
356 | uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!! |
289 | uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!! |
- | 290 | uint8_t debugFullWeight = 1; |
|
Line 357... | Line -... | ||
357 | uint8_t debugFullWeight = 1; |
- | |
358 | - | ||
359 | int32_t accDerivedPitch = getPitchAngleEstimateFromAcc(); |
- | |
360 | int32_t accDerivedRoll = getRollAngleEstimateFromAcc(); |
291 | int32_t accDerived[2]; |
361 | 292 | ||
362 | if((maxControlPitch > 64) || (maxControlRoll > 64)) { // reduce effect during stick commands |
293 | if((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands |
363 | permilleAcc /= 2; |
294 | permilleAcc /= 2; |
Line 364... | Line 295... | ||
364 | debugFullWeight = 0; |
295 | debugFullWeight = 0; |
Line 370... | Line 301... | ||
370 | } |
301 | } |
Line 371... | Line 302... | ||
371 | 302 | ||
372 | /* |
303 | /* |
373 | * Add to each sum: The amount by which the angle is changed just below. |
304 | * Add to each sum: The amount by which the angle is changed just below. |
374 | */ |
305 | */ |
375 | pitchCorrectionSum += permilleAcc * (accDerivedPitch - pitchAngle); |
306 | for (axis=PITCH; axis<=ROLL; axis++) { |
376 | rollCorrectionSum += permilleAcc * (accDerivedRoll - rollAngle); |
- | |
377 | 307 | accDerived[axis] = getAngleEstimateFromAcc(axis); |
|
378 | // There should not be a risk of overflow here, since the integrals do not exceed a few 100000. |
- | |
379 | pitchAngle = ((int32_t)(1000 - permilleAcc) * pitchAngle + (int32_t)permilleAcc * accDerivedPitch) / 1000L; |
- | |
Line -... | Line 308... | ||
- | 308 | correctionSum[axis] += permilleAcc * (accDerived[axis] - angle[axis]); |
|
- | 309 | ||
- | 310 | // There should not be a risk of overflow here, since the integrals do not exceed a few 100000. |
|
- | 311 | angle[axis] = ((int32_t)(1000 - permilleAcc) * angle[axis] + (int32_t)permilleAcc * accDerived[axis]) / 1000L; |
|
380 | rollAngle = ((int32_t)(1000 - permilleAcc) * rollAngle + (int32_t)permilleAcc * accDerivedRoll) / 1000L; |
312 | } |
381 | 313 | ||
382 | DebugOut.Digital[1] = debugFullWeight; |
314 | DebugOut.Digital[1] = debugFullWeight; |
383 | } else { |
315 | } else { |
384 | DebugOut.Digital[0] = 0; |
316 | DebugOut.Digital[0] = 0; |
Line 398... | Line 330... | ||
398 | // 2 times / sec. |
330 | // 2 times / sec. |
399 | #define DRIFTCORRECTION_TIME 488/2 |
331 | #define DRIFTCORRECTION_TIME 488/2 |
400 | void driftCompensation(void) { |
332 | void driftCompensation(void) { |
401 | static int16_t timer = DRIFTCORRECTION_TIME; |
333 | static int16_t timer = DRIFTCORRECTION_TIME; |
402 | int16_t deltaCompensation; |
334 | int16_t deltaCompensation; |
- | 335 | uint8_t axis; |
|
403 | if (! --timer) { |
336 | if (! --timer) { |
404 | timer = DRIFTCORRECTION_TIME; |
337 | timer = DRIFTCORRECTION_TIME; |
405 | deltaCompensation = ((pitchCorrectionSum + 1000L * DRIFTCORRECTION_TIME / 2) / 1000 / DRIFTCORRECTION_TIME); |
- | |
406 | CHECK_MIN_MAX(deltaCompensation, -staticParams.DriftComp, staticParams.DriftComp); |
- | |
407 | dynamicOffsetPitch += deltaCompensation / staticParams.GyroAccTrim; |
338 | for (axis=PITCH; axis<=ROLL; axis++) { |
408 | - | ||
409 | deltaCompensation = ((rollCorrectionSum + 1000L * DRIFTCORRECTION_TIME / 2) / 1000 / DRIFTCORRECTION_TIME); |
339 | deltaCompensation = ((correctionSum[axis] + 1000L * DRIFTCORRECTION_TIME / 2) / 1000 / DRIFTCORRECTION_TIME); |
410 | CHECK_MIN_MAX(deltaCompensation, -staticParams.DriftComp, staticParams.DriftComp); |
340 | CHECK_MIN_MAX(deltaCompensation, -staticParams.DriftComp, staticParams.DriftComp); |
411 | dynamicOffsetRoll += deltaCompensation / staticParams.GyroAccTrim; |
341 | dynamicOffset[axis] += deltaCompensation / staticParams.GyroAccTrim; |
412 | - | ||
413 | pitchCorrectionSum = rollCorrectionSum = 0; |
342 | correctionSum[axis] = 0; |
414 | - | ||
415 | DebugOut.Analog[28] = dynamicOffsetPitch; |
343 | DebugOut.Analog[28 + axis] = dynamicOffset; |
416 | DebugOut.Analog[29] = dynamicOffsetRoll; |
344 | } |
417 | } |
345 | } |
418 | } |
346 | } |
Line 419... | Line 347... | ||
419 | 347 | ||
420 | /************************************************************************ |
348 | /************************************************************************ |
Line 428... | Line 356... | ||
428 | driftCompensation(); |
356 | driftCompensation(); |
429 | #endif |
357 | #endif |
430 | } |
358 | } |
Line 431... | Line 359... | ||
431 | 359 | ||
432 | /* |
360 | /* |
433 | void updateCompass(void) { |
361 | void updateCompass(void) { |
Line 434... | Line 362... | ||
434 | int16_t w, v, r,correction, error; |
362 | int16_t w, v, r,correction, error; |
435 | |
363 | |
436 | if(compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) { |
364 | if(compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) { |
437 | setCompassCalState(); |
365 | setCompassCalState(); |
438 | } else { |
366 | } else { |
439 | // get maximum attitude angle |
367 | // get maximum attitude angle |
440 | w = abs(pitchAngle / 512); |
368 | w = abs(pitchAngle / 512); |
441 | v = abs(rollAngle / 512); |
369 | v = abs(rollAngle / 512); |
442 | if(v > w) w = v; |
370 | if(v > w) w = v; |
443 | correction = w / 8 + 1; |
371 | correction = w / 8 + 1; |
444 | // calculate the deviation of the yaw gyro heading and the compass heading |
372 | // calculate the deviation of the yaw gyro heading and the compass heading |
445 | if (compassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined |
373 | if (compassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined |
446 | else error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) % 360) - 180; |
374 | else error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) % 360) - 180; |
447 | if(abs(yawRate) > 128) { // spinning fast |
375 | if(abs(yawRate) > 128) { // spinning fast |
448 | error = 0; |
376 | error = 0; |
449 | } |
377 | } |
450 | if(!badCompassHeading && w < 25) { |
378 | if(!badCompassHeading && w < 25) { |
451 | if(updateCompassCourse) { |
379 | if(updateCompassCourse) { |
452 | beep(200); |
380 | beep(200); |
453 | yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW; |
381 | yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW; |
454 | compassCourse = (int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW); |
382 | compassCourse = (int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW); |
455 | updateCompassCourse = 0; |
383 | updateCompassCourse = 0; |
456 | } |
384 | } |
457 | } |
385 | } |
458 | yawGyroHeading += (error * 8) / correction; |
386 | yawGyroHeading += (error * 8) / correction; |
459 | w = (w * dynamicParams.CompassYawEffect) / 32; |
387 | w = (w * dynamicParams.CompassYawEffect) / 32; |
460 | w = dynamicParams.CompassYawEffect - w; |
388 | w = dynamicParams.CompassYawEffect - w; |
461 | if(w >= 0) { |
389 | if(w >= 0) { |
462 | if(!badCompassHeading) { |
390 | if(!badCompassHeading) { |
463 | v = 64 + (maxControlPitch + maxControlRoll) / 8; |
391 | v = 64 + (maxControlPitch + maxControlRoll) / 8; |
464 | // calc course deviation |
392 | // calc course deviation |
465 | r = ((540 + (yawGyroHeading / GYRO_DEG_FACTOR_YAW) - compassCourse) % 360) - 180; |
393 | r = ((540 + (yawGyroHeading / GYRO_DEG_FACTOR_YAW) - compassCourse) % 360) - 180; |
466 | v = (r * w) / v; // align to compass course |
394 | v = (r * w) / v; // align to compass course |
467 | // limit yaw rate |
395 | // limit yaw rate |
468 | w = 3 * dynamicParams.CompassYawEffect; |
396 | w = 3 * dynamicParams.CompassYawEffect; |
469 | if (v > w) v = w; |
397 | if (v > w) v = w; |
470 | else if (v < -w) v = -w; |
398 | else if (v < -w) v = -w; |
471 | yawAngle += v; |
399 | yawAngle += v; |
472 | } |
400 | } |
473 | else |
401 | else |
474 | { // wait a while |
402 | { // wait a while |
475 | badCompassHeading--; |
403 | badCompassHeading--; |
476 | } |
404 | } |
477 | } |
405 | } |
- | 406 | else { // ignore compass at extreme attitudes for a while |
|
478 | else { // ignore compass at extreme attitudes for a while |
407 | badCompassHeading = 500; |
479 | badCompassHeading = 500; |
408 | } |
480 | } |
- | |
481 | } |
409 | } |
Line 482... | Line 410... | ||
482 | } |
410 | } |
483 | */ |
411 | */ |
484 | 412 | ||
Line 489... | Line 417... | ||
489 | * will measure the effect of vibration, to use for later compensation. So, one should keep |
417 | * will measure the effect of vibration, to use for later compensation. So, one should keep |
490 | * the stick in the start-motors position for a few seconds, till all motors run (at the wrong |
418 | * the stick in the start-motors position for a few seconds, till all motors run (at the wrong |
491 | * speed unfortunately... must find a better way) |
419 | * speed unfortunately... must find a better way) |
492 | */ |
420 | */ |
493 | /* |
421 | /* |
494 | void attitude_startDynamicCalibration(void) { |
422 | void attitude_startDynamicCalibration(void) { |
495 | dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0; |
423 | dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0; |
496 | savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000; |
424 | savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000; |
497 | } |
425 | } |
Line 498... | Line 426... | ||
498 | 426 | ||
499 | void attitude_continueDynamicCalibration(void) { |
427 | void attitude_continueDynamicCalibration(void) { |
500 | // measure dynamic offset now... |
428 | // measure dynamic offset now... |
501 | dynamicCalPitch += hiResPitchGyro; |
429 | dynamicCalPitch += hiResPitchGyro; |
502 | dynamicCalRoll += hiResRollGyro; |
430 | dynamicCalRoll += hiResRollGyro; |
503 | dynamicCalYaw += rawYawGyroSum; |
431 | dynamicCalYaw += rawYawGyroSum; |
Line 504... | Line 432... | ||
504 | dynamicCalCount++; |
432 | dynamicCalCount++; |
505 | |
433 | |
506 | // Param6: Manual mode. The offsets are taken from Param7 and Param8. |
434 | // Param6: Manual mode. The offsets are taken from Param7 and Param8. |
507 | if (dynamicParams.UserParam6 || 1) { // currently always enabled. |
435 | if (dynamicParams.UserParam6 || 1) { // currently always enabled. |
508 | // manual mode |
436 | // manual mode |
509 | dynamicOffsetPitch = dynamicParams.UserParam7 - 128; |
437 | dynamicOffsetPitch = dynamicParams.UserParam7 - 128; |
510 | dynamicOffsetRoll = dynamicParams.UserParam8 - 128; |
438 | dynamicOffsetRoll = dynamicParams.UserParam8 - 128; |
511 | } else { |
439 | } else { |
512 | // use the sampled value (does not seem to work so well....) |
440 | // use the sampled value (does not seem to work so well....) |
513 | dynamicOffsetPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount; |
441 | dynamicOffsetPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount; |
514 | dynamicOffsetRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount; |
442 | dynamicOffsetRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount; |
Line 515... | Line 443... | ||
515 | dynamicOffsetYaw = -dynamicCalYaw / dynamicCalCount; |
443 | dynamicOffsetYaw = -dynamicCalYaw / dynamicCalCount; |
516 | } |
444 | } |
517 | |
445 | |
518 | // keep resetting these meanwhile, to avoid accumulating errors. |
446 | // keep resetting these meanwhile, to avoid accumulating errors. |
519 | setStaticAttitudeIntegrals(); |
447 | setStaticAttitudeIntegrals(); |