Rev 1868 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1868 | Rev 1869 | ||
---|---|---|---|
Line 154... | Line 154... | ||
154 | * constant speed, and 2) at small angles a, sin(a) ~= constant * a, |
154 | * constant speed, and 2) at small angles a, sin(a) ~= constant * a, |
155 | * it is hardly worth the trouble. |
155 | * it is hardly worth the trouble. |
156 | ************************************************************************/ |
156 | ************************************************************************/ |
Line 157... | Line 157... | ||
157 | 157 | ||
158 | int32_t getAngleEstimateFromAcc(uint8_t axis) { |
158 | int32_t getAngleEstimateFromAcc(uint8_t axis) { |
159 | return GYRO_ACC_FACTOR * (int32_t)filteredAcc[axis]; |
159 | return GYRO_ACC_FACTOR * (int32_t) filteredAcc[axis]; |
Line 160... | Line 160... | ||
160 | } |
160 | } |
161 | 161 | ||
162 | void setStaticAttitudeAngles(void) { |
162 | void setStaticAttitudeAngles(void) { |
163 | #ifdef ATTITUDE_USE_ACC_SENSORS |
163 | #ifdef ATTITUDE_USE_ACC_SENSORS |
164 | angle[PITCH] = getAngleEstimateFromAcc(PITCH); |
164 | angle[PITCH] = getAngleEstimateFromAcc(PITCH); |
165 | angle[ROLL] = getAngleEstimateFromAcc(ROLL); |
165 | angle[ROLL] = getAngleEstimateFromAcc(ROLL); |
166 | #else |
166 | #else |
167 | angle[PITCH] = angle[ROLL] = 0; |
167 | angle[PITCH] = angle[ROLL] = 0; |
Line 168... | Line 168... | ||
168 | #endif |
168 | #endif |
169 | } |
169 | } |
170 | 170 | ||
171 | /************************************************************************ |
171 | /************************************************************************ |
172 | * Neutral Readings |
172 | * Neutral Readings |
173 | ************************************************************************/ |
173 | ************************************************************************/ |
Line 174... | Line 174... | ||
174 | void attitude_setNeutral(void) { |
174 | void attitude_setNeutral(void) { |
175 | // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway. |
175 | // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway. |
Line 176... | Line 176... | ||
176 | dynamicParams.AxisCoupling1 = dynamicParams.AxisCoupling2 = 0; |
176 | dynamicParams.AxisCoupling1 = dynamicParams.AxisCoupling2 = 0; |
177 | 177 | ||
Line 178... | Line 178... | ||
178 | driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0; |
178 | driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0; |
179 | correctionSum[PITCH] = correctionSum[ROLL] = 0; |
179 | correctionSum[PITCH] = correctionSum[ROLL] = 0; |
180 | 180 | ||
Line 181... | Line 181... | ||
181 | // Calibrate hardware. |
181 | // Calibrate hardware. |
182 | analog_calibrate(); |
182 | analog_calibrate(); |
Line 183... | Line 183... | ||
183 | 183 | ||
184 | // reset gyro integrals to acc guessing |
184 | // reset gyro integrals to acc guessing |
Line 185... | Line 185... | ||
185 | setStaticAttitudeAngles(); |
185 | setStaticAttitudeAngles(); |
186 | yawAngleDiff = 0; |
186 | yawAngleDiff = 0; |
Line 187... | Line 187... | ||
187 | 187 | ||
188 | // update compass course to current heading |
188 | // update compass course to current heading |
189 | compassCourse = compassHeading; |
189 | compassCourse = compassHeading; |
190 | 190 | ||
191 | // Inititialize YawGyroIntegral value with current compass heading |
191 | // Inititialize YawGyroIntegral value with current compass heading |
192 | yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW; |
192 | yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW; |
193 | 193 | ||
194 | // Servo_On(); //enable servo output |
194 | // Servo_On(); //enable servo output |
Line 195... | Line 195... | ||
195 | } |
195 | } |
196 | 196 | ||
- | 197 | /************************************************************************ |
|
197 | /************************************************************************ |
198 | * Get sensor data from the analog module, and release the ADC |
- | 199 | * TODO: Ultimately, the analog module could do this (instead of dumping |
|
198 | * Get sensor data from the analog module, and release the ADC |
200 | * the values into variables). |
199 | * TODO: Ultimately, the analog module could do this (instead of dumping |
201 | * The rate variable end up in a range of about [-1024, 1023]. |
200 | * the values into variables). |
202 | *************************************************************************/ |
201 | * The rate variable end up in a range of about [-1024, 1023]. |
203 | void getAnalogData(void) { |
202 | *************************************************************************/ |
204 | uint8_t axis; |
203 | void getAnalogData(void) { |
205 | |
204 | uint8_t axis; |
206 | for (axis = PITCH; axis <= ROLL; axis++) { |
205 | 207 | rate_PID[axis] = gyro_PID[axis] / HIRES_GYRO_INTEGRATION_FACTOR |
|
206 | for (axis = PITCH; axis <= ROLL; axis++) { |
208 | + driftComp[axis]; |
207 | rate_PID[axis] = gyro_PID[axis] / HIRES_GYRO_INTEGRATION_FACTOR + driftComp[axis]; |
209 | rate_ATT[axis] = gyro_ATT[axis] / HIRES_GYRO_INTEGRATION_FACTOR |
208 | rate_ATT[axis] = gyro_ATT[axis] / HIRES_GYRO_INTEGRATION_FACTOR + driftComp[axis]; |
210 | + driftComp[axis]; |
209 | differential[axis] = gyroD[axis]; |
211 | differential[axis] = gyroD[axis]; |
Line 210... | Line 212... | ||
210 | averageAcc[axis] += acc[axis]; |
212 | averageAcc[axis] += acc[axis]; |
211 | } |
213 | } |
212 | 214 | ||
213 | averageAccCount++; |
215 | averageAccCount++; |
214 | yawRate = yawGyro + driftCompYaw; |
216 | yawRate = yawGyro + driftCompYaw; |
215 | 217 | ||
216 | // We are done reading variables from the analog module. |
218 | // We are done reading variables from the analog module. |
- | 219 | // Interrupt-driven sensor reading may restart. |
|
217 | // Interrupt-driven sensor reading may restart. |
220 | analogDataReady = 0; |
218 | analogDataReady = 0; |
221 | analog_start(); |
219 | analog_start(); |
222 | } |
220 | } |
223 | |
221 | 224 | /* |
|
222 | /* |
225 | * This is the standard flight-style coordinate system transformation |
223 | * This is the standard flight-style coordinate system transformation |
226 | * (from airframe-local axes to a ground-based system). For some reason |
224 | * (from airframe-local axes to a ground-based system). For some reason |
- | |
225 | * the MK uses a left-hand coordinate system. The tranformation has been |
227 | * the MK uses a left-hand coordinate system. The tranformation has been |
226 | * changed accordingly. |
228 | * changed accordingly. |
227 | */ |
229 | */ |
Line 228... | Line 230... | ||
228 | void trigAxisCoupling(void) { |
230 | void trigAxisCoupling(void) { |
229 | int16_t cospitch = int_cos(angle[PITCH]); |
231 | J5HIGH; |
230 | int16_t cosroll = int_cos(angle[ROLL]); |
232 | int16_t cospitch = int_cos(angle[PITCH]); |
Line 231... | Line 233... | ||
231 | int16_t sinroll = int_sin(angle[ROLL]); |
233 | int16_t cosroll = int_cos(angle[ROLL]); |
232 | 234 | int16_t sinroll = int_sin(angle[ROLL]); |
|
233 | ACRate[PITCH] = (((int32_t) rate_ATT[PITCH] * cosroll - (int32_t) yawRate |
235 | |
234 | * sinroll) >> MATH_UNIT_FACTOR_LOG); |
236 | ACRate[PITCH] = (((int32_t) rate_ATT[PITCH] * cosroll - (int32_t) yawRate |
235 | 237 | * sinroll) >> MATH_UNIT_FACTOR_LOG); |
|
236 | ACRate[ROLL] = rate_ATT[ROLL] + |
238 | |
237 | (((((int32_t)rate_ATT[PITCH] * sinroll + (int32_t)yawRate * cosroll) |
239 | ACRate[ROLL] = rate_ATT[ROLL] + (((((int32_t) rate_ATT[PITCH] * sinroll |
238 | >> MATH_UNIT_FACTOR_LOG) |
240 | + (int32_t) yawRate * cosroll) >> MATH_UNIT_FACTOR_LOG) * int_tan( |
239 | * int_tan(angle[PITCH])) >> MATH_UNIT_FACTOR_LOG); |
241 | angle[PITCH])) >> MATH_UNIT_FACTOR_LOG); |
240 | 242 | ||
241 | ACYawRate = ((int32_t) rate_ATT[PITCH] * sinroll) / cospitch |
243 | ACYawRate = ((int32_t) rate_ATT[PITCH] * sinroll) / cospitch |
242 | + ((int32_t) yawRate * cosroll) / cospitch; |
244 | + ((int32_t) yawRate * cosroll) / cospitch; |
243 | } |
245 | } |
244 | 246 | ||
245 | // 480 usec with axis coupling - almost no time without. |
247 | // 480 usec with axis coupling - almost no time without. |
246 | void integrate(void) { |
248 | void integrate(void) { |
247 | // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate. |
249 | // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate. |
248 | uint8_t axis; |
250 | uint8_t axis; |
249 | if (!looping && (staticParams.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) { |
251 | if (!looping && (staticParams.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) { |
250 | // The rotary rate limiter bit is abused for selecting axis coupling algorithm instead. |
252 | // The rotary rate limiter bit is abused for selecting axis coupling algorithm instead. |
251 | trigAxisCoupling(); |
253 | trigAxisCoupling(); |
252 | } else { |
254 | } else { |
253 | ACRate[PITCH] = rate_ATT[PITCH]; |
255 | ACRate[PITCH] = rate_ATT[PITCH]; |
254 | ACRate[ROLL] = rate_ATT[ROLL]; |
256 | ACRate[ROLL] = rate_ATT[ROLL]; |
255 | ACYawRate = yawRate; |
257 | ACYawRate = yawRate; |
256 | } |
258 | } |
257 | 259 | ||
258 | /* |
260 | /* |
259 | * Yaw |
261 | * Yaw |
260 | * Calculate yaw gyro integral (~ to rotation angle) |
262 | * Calculate yaw gyro integral (~ to rotation angle) |
261 | * Limit yawGyroHeading proportional to 0 deg to 360 deg |
263 | * Limit yawGyroHeading proportional to 0 deg to 360 deg |
262 | */ |
264 | */ |
263 | yawGyroHeading += ACYawRate; |
265 | yawGyroHeading += ACYawRate; |
264 | yawAngleDiff += yawRate; |
266 | yawAngleDiff += yawRate; |
265 | 267 | ||
266 | if (yawGyroHeading >= YAWOVER360) { |
268 | if (yawGyroHeading >= YAWOVER360) { |
267 | yawGyroHeading -= YAWOVER360; // 360 deg. wrap |
269 | yawGyroHeading -= YAWOVER360; // 360 deg. wrap |
268 | } else if (yawGyroHeading < 0) { |
270 | } else if (yawGyroHeading < 0) { |
- | 271 | yawGyroHeading += YAWOVER360; |
|
269 | yawGyroHeading += YAWOVER360; |
272 | } |
Line 270... | Line 273... | ||
270 | } |
273 | |
271 | 274 | /* |
|
272 | /* |
275 | * Pitch axis integration and range boundary wrap. |
273 | * Pitch axis integration and range boundary wrap. |
276 | */ |
274 | */ |
277 | for (axis = PITCH; axis <= ROLL; axis++) { |
275 | for (axis = PITCH; axis <= ROLL; axis++) { |
278 | angle[axis] += ACRate[axis]; |
276 | angle[axis] += ACRate[axis]; |
279 | if (angle[axis] > PITCHROLLOVER180) { |
277 | if (angle[axis] > PITCHROLLOVER180) { |
280 | angle[axis] -= PITCHROLLOVER360; |
278 | angle[axis] -= PITCHROLLOVER360; |
281 | } else if (angle[axis] <= -PITCHROLLOVER180) { |
279 | } else if (angle[axis] <= -PITCHROLLOVER180) { |
282 | angle[axis] += PITCHROLLOVER360; |
280 | angle[axis] += PITCHROLLOVER360; |
283 | } |
281 | } |
284 | } |
282 | } |
285 | J5LOW; |
283 | } |
286 | } |
284 | 287 | ||
285 | /************************************************************************ |
288 | /************************************************************************ |
286 | * A kind of 0'th order integral correction, that corrects the integrals |
289 | * A kind of 0'th order integral correction, that corrects the integrals |
287 | * directly. This is the "gyroAccFactor" stuff in the original code. |
290 | * directly. This is the "gyroAccFactor" stuff in the original code. |
288 | * There is (there) also a drift compensation |
291 | * There is (there) also a drift compensation |
289 | * - it corrects the differential of the integral = the gyro offsets. |
292 | * - it corrects the differential of the integral = the gyro offsets. |
290 | * That should only be necessary with drifty gyros like ENC-03. |
293 | * That should only be necessary with drifty gyros like ENC-03. |
291 | ************************************************************************/ |
294 | ************************************************************************/ |
292 | void correctIntegralsByAcc0thOrder(void) { |
295 | void correctIntegralsByAcc0thOrder(void) { |
293 | // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities |
296 | // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities |
294 | // are less than ....., or reintroduce Kalman. |
297 | // are less than ....., or reintroduce Kalman. |
295 | // Well actually the Z axis acc. check is not so silly. |
298 | // Well actually the Z axis acc. check is not so silly. |
296 | uint8_t axis; |
299 | uint8_t axis; |
297 | int32_t temp; |
300 | int32_t temp; |
298 | if (!looping && acc[Z] >= -dynamicParams.UserParams[7] && acc[Z] |
301 | if (!looping && acc[Z] >= -dynamicParams.UserParams[7] && acc[Z] |
299 | <= dynamicParams.UserParams[7]) { |
302 | <= dynamicParams.UserParams[7]) { |
300 | DebugOut.Digital[0] |= DEBUG_ACC0THORDER; |
303 | DebugOut.Digital[0] |= DEBUG_ACC0THORDER; |
301 | 304 | ||
302 | uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!! |
305 | uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!! |
303 | uint8_t debugFullWeight = 1; |
306 | uint8_t debugFullWeight = 1; |
304 | int32_t accDerived; |
307 | int32_t accDerived; |
305 | 308 | ||
306 | if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active |
309 | if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active |
307 | permilleAcc /= 2; |
310 | permilleAcc /= 2; |
308 | debugFullWeight = 0; |
311 | debugFullWeight = 0; |
309 | } |
312 | } |
310 | 313 | ||
311 | if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands |
314 | if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands |
312 | permilleAcc /= 2; |
315 | permilleAcc /= 2; |
313 | debugFullWeight = 0; |
316 | debugFullWeight = 0; |
314 | } |
317 | } |
315 | 318 | ||
316 | if (debugFullWeight) |
319 | if (debugFullWeight) |
317 | DebugOut.Digital[1] |= DEBUG_ACC0THORDER; |
320 | DebugOut.Digital[1] |= DEBUG_ACC0THORDER; |
318 | else |
321 | else |
319 | DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER; |
322 | DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER; |
320 | 323 | ||
321 | /* |
324 | /* |
322 | * Add to each sum: The amount by which the angle is changed just below. |
325 | * Add to each sum: The amount by which the angle is changed just below. |
323 | */ |
326 | */ |
324 | for (axis = PITCH; axis <= ROLL; axis++) { |
327 | for (axis = PITCH; axis <= ROLL; axis++) { |
325 | accDerived = getAngleEstimateFromAcc(axis); |
328 | accDerived = getAngleEstimateFromAcc(axis); |
326 | DebugOut.Analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL; |
329 | DebugOut.Analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL; |
327 | 330 | ||
328 | // 1000 * the correction amount that will be added to the gyro angle in next line. |
331 | // 1000 * the correction amount that will be added to the gyro angle in next line. |
329 | temp = angle[axis]; //(permilleAcc * (accDerived - angle[axis])) / 1000; |
332 | temp = angle[axis]; //(permilleAcc * (accDerived - angle[axis])) / 1000; |
330 | angle[axis] = ((int32_t) (1000L - permilleAcc) * temp |
333 | angle[axis] = ((int32_t) (1000L - permilleAcc) * temp |
Line 331... | Line 334... | ||
331 | + (int32_t) permilleAcc * accDerived) / 1000L; |
334 | + (int32_t) permilleAcc * accDerived) / 1000L; |
332 | correctionSum[axis] += angle[axis] - temp; |
335 | correctionSum[axis] += angle[axis] - temp; |
333 | } |
336 | } |
Line 355... | Line 358... | ||
355 | * the error. This is then added to the dynamic offsets. |
358 | * the error. This is then added to the dynamic offsets. |
356 | ************************************************************************/ |
359 | ************************************************************************/ |
357 | // 2 times / sec. = 488/2 |
360 | // 2 times / sec. = 488/2 |
358 | #define DRIFTCORRECTION_TIME 256L |
361 | #define DRIFTCORRECTION_TIME 256L |
359 | void driftCorrection(void) { |
362 | void driftCorrection(void) { |
360 | static int16_t timer = DRIFTCORRECTION_TIME; |
363 | static int16_t timer = DRIFTCORRECTION_TIME; |
361 | int16_t deltaCorrection; |
364 | int16_t deltaCorrection; |
362 | uint8_t axis; |
365 | uint8_t axis; |
363 | if (!--timer) { |
366 | if (!--timer) { |
364 | timer = DRIFTCORRECTION_TIME; |
367 | timer = DRIFTCORRECTION_TIME; |
365 | for (axis = PITCH; axis <= ROLL; axis++) { |
368 | for (axis = PITCH; axis <= ROLL; axis++) { |
366 | // Take the sum of corrections applied, add it to delta |
369 | // Take the sum of corrections applied, add it to delta |
367 | deltaCorrection = (correctionSum[axis] + DRIFTCORRECTION_TIME / 2) / DRIFTCORRECTION_TIME; |
370 | deltaCorrection = (correctionSum[axis] + DRIFTCORRECTION_TIME / 2) |
- | 371 | / DRIFTCORRECTION_TIME; |
|
368 | // Add the delta to the compensation. So positive delta means, gyro should have higher value. |
372 | // Add the delta to the compensation. So positive delta means, gyro should have higher value. |
369 | driftComp[axis] += deltaCorrection / staticParams.GyroAccTrim; |
373 | driftComp[axis] += deltaCorrection / staticParams.GyroAccTrim; |
370 | CHECK_MIN_MAX(driftComp[axis], -staticParams.DriftComp, staticParams.DriftComp); |
374 | CHECK_MIN_MAX(driftComp[axis], -staticParams.DriftComp, staticParams.DriftComp); |
371 | // DebugOut.Analog[11 + axis] = correctionSum[axis]; |
375 | // DebugOut.Analog[11 + axis] = correctionSum[axis]; |
372 | DebugOut.Analog[16 + axis] = correctionSum[axis]; |
376 | DebugOut.Analog[16 + axis] = correctionSum[axis]; |
373 | DebugOut.Analog[18 + axis] = deltaCorrection / staticParams.GyroAccTrim; |
377 | DebugOut.Analog[18 + axis] = deltaCorrection / staticParams.GyroAccTrim; |
374 | DebugOut.Analog[28 + axis] = driftComp[axis]; |
378 | DebugOut.Analog[28 + axis] = driftComp[axis]; |
375 | 379 | ||
376 | correctionSum[axis] = 0; |
380 | correctionSum[axis] = 0; |
377 | } |
381 | } |
378 | } |
382 | } |
379 | } |
383 | } |
Line 380... | Line 384... | ||
380 | 384 | ||
381 | /************************************************************************ |
385 | /************************************************************************ |
382 | * Main procedure. |
386 | * Main procedure. |
383 | ************************************************************************/ |
387 | ************************************************************************/ |
384 | void calculateFlightAttitude(void) { |
388 | void calculateFlightAttitude(void) { |
385 | // part1: 550 usec. |
389 | // part1: 550 usec. |
386 | // part1a: 550 usec. |
390 | // part1a: 550 usec. |
387 | // part1b: 60 usec. |
391 | // part1b: 60 usec. |
388 | getAnalogData(); |
392 | getAnalogData(); |
389 | // end part1b |
393 | // end part1b |
390 | integrate(); |
394 | integrate(); |
391 | // end part1a |
- | |
392 | 395 | // end part1a |
|
393 | 396 | ||
394 | DebugOut.Analog[6] = ACRate[PITCH]; |
397 | DebugOut.Analog[6] = stronglyFilteredAcc[PITCH]; |
395 | DebugOut.Analog[7] = ACRate[ROLL]; |
398 | DebugOut.Analog[7] = stronglyFilteredAcc[ROLL]; |
396 | DebugOut.Analog[8] = ACYawRate; |
399 | DebugOut.Analog[8] = stronglyFilteredAcc[Z]; |
397 | 400 | ||
398 | DebugOut.Analog[3] = rate_PID[PITCH]; |
401 | DebugOut.Analog[3] = rate_PID[PITCH]; |
399 | DebugOut.Analog[4] = rate_PID[ROLL]; |
402 | DebugOut.Analog[4] = rate_PID[ROLL]; |
Line 400... | Line 403... | ||
400 | DebugOut.Analog[5] = yawRate; |
403 | DebugOut.Analog[5] = yawRate; |
401 | 404 | ||
402 | #ifdef ATTITUDE_USE_ACC_SENSORS |
405 | #ifdef ATTITUDE_USE_ACC_SENSORS |
403 | correctIntegralsByAcc0thOrder(); |
406 | correctIntegralsByAcc0thOrder(); |
404 | driftCorrection(); |
407 | driftCorrection(); |
405 | #endif |
408 | #endif |
Line 406... | Line 409... | ||
406 | // end part1 |
409 | // end part1 |
407 | } |
410 | } |
Line 408... | Line 411... | ||
408 | 411 | ||
409 | void updateCompass(void) { |
412 | void updateCompass(void) { |
410 | int16_t w, v, r, correction, error; |
413 | int16_t w, v, r, correction, error; |
411 | 414 | ||
412 | if (compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) { |
415 | if (compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) { |
413 | if (controlMixer_testCompassCalState()) { |
416 | if (controlMixer_testCompassCalState()) { |
414 | compassCalState++; |
417 | compassCalState++; |
415 | if (compassCalState < 5) |
418 | if (compassCalState < 5) |
416 | beepNumber(compassCalState); |
419 | beepNumber(compassCalState); |
417 | else |
420 | else |
418 | beep(1000); |
421 | beep(1000); |
419 | } |
422 | } |
420 | } else { |
423 | } else { |
421 | // get maximum attitude angle |
424 | // get maximum attitude angle |
422 | w = abs(angle[PITCH] / 512); |
425 | w = abs(angle[PITCH] / 512); |
423 | v = abs(angle[ROLL] / 512); |
426 | v = abs(angle[ROLL] / 512); |
424 | if (v > w) |
427 | if (v > w) |
425 | w = v; |
428 | w = v; |
426 | correction = w / 8 + 1; |
429 | correction = w / 8 + 1; |
427 | // calculate the deviation of the yaw gyro heading and the compass heading |
430 | // calculate the deviation of the yaw gyro heading and the compass heading |
428 | if (compassHeading < 0) |
431 | if (compassHeading < 0) |
429 | error = 0; // disable yaw drift compensation if compass heading is undefined |
432 | error = 0; // disable yaw drift compensation if compass heading is undefined |
430 | else if (abs(yawRate) > 128) { // spinning fast |
433 | else if (abs(yawRate) > 128) { // spinning fast |
431 | error = 0; |
434 | error = 0; |
432 | } else { |
435 | } else { |
433 | // compassHeading - yawGyroHeading, on a -180..179 deg interval. |
436 | // compassHeading - yawGyroHeading, on a -180..179 deg interval. |
434 | error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) |
437 | error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) |
435 | % 360) - 180; |
438 | % 360) - 180; |
436 | } |
439 | } |
437 | if (!ignoreCompassTimer && w < 25) { |
440 | if (!ignoreCompassTimer && w < 25) { |
438 | yawGyroDrift += error; |
441 | yawGyroDrift += error; |
439 | // Basically this gets set if we are in "fix" mode, and when starting. |
442 | // Basically this gets set if we are in "fix" mode, and when starting. |
440 | if (updateCompassCourse) { |
443 | if (updateCompassCourse) { |
441 | beep(200); |
444 | beep(200); |
442 | yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW; |
445 | yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW; |
443 | compassCourse = compassHeading; //(int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW); |
446 | compassCourse = compassHeading; //(int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW); |
444 | updateCompassCourse = 0; |
447 | updateCompassCourse = 0; |
445 | } |
448 | } |
446 | } |
449 | } |
447 | yawGyroHeading += (error * 8) / correction; |
450 | yawGyroHeading += (error * 8) / correction; |
448 | 451 | ||
449 | /* |
452 | /* |
450 | w = (w * dynamicParams.CompassYawEffect) / 32; |
453 | w = (w * dynamicParams.CompassYawEffect) / 32; |
451 | w = dynamicParams.CompassYawEffect - w; |
454 | w = dynamicParams.CompassYawEffect - w; |
452 | */ |
455 | */ |
453 | w = dynamicParams.CompassYawEffect - (w * dynamicParams.CompassYawEffect) |
456 | w = dynamicParams.CompassYawEffect - (w * dynamicParams.CompassYawEffect) |
454 | / 32; |
457 | / 32; |
455 | 458 | ||
456 | // As readable formula: |
459 | // As readable formula: |
457 | // w = dynamicParams.CompassYawEffect * (1-w/32); |
460 | // w = dynamicParams.CompassYawEffect * (1-w/32); |
458 | 461 | ||
459 | if (w >= 0) { // maxAttitudeAngle < 32 |
462 | if (w >= 0) { // maxAttitudeAngle < 32 |
460 | if (!ignoreCompassTimer) { |
463 | if (!ignoreCompassTimer) { |
461 | v = 64 + (maxControl[PITCH] + maxControl[ROLL]) / 8; |
464 | v = 64 + (maxControl[PITCH] + maxControl[ROLL]) / 8; |
462 | // yawGyroHeading - compassCourse on a -180..179 degree interval. |
465 | // yawGyroHeading - compassCourse on a -180..179 degree interval. |
463 | r |
466 | r |
464 | = ((540 + yawGyroHeading / GYRO_DEG_FACTOR_YAW - compassCourse) |
467 | = ((540 + yawGyroHeading / GYRO_DEG_FACTOR_YAW - compassCourse) |
465 | % 360) - 180; |
468 | % 360) - 180; |
466 | v = (r * w) / v; // align to compass course |
469 | v = (r * w) / v; // align to compass course |
467 | // limit yaw rate |
470 | // limit yaw rate |
468 | w = 3 * dynamicParams.CompassYawEffect; |
471 | w = 3 * dynamicParams.CompassYawEffect; |
469 | if (v > w) |
472 | if (v > w) |
470 | v = w; |
473 | v = w; |
471 | else if (v < -w) |
474 | else if (v < -w) |
472 | v = -w; |
475 | v = -w; |
473 | yawAngleDiff += v; |
476 | yawAngleDiff += v; |
474 | } else { // wait a while |
477 | } else { // wait a while |
475 | ignoreCompassTimer--; |
478 | ignoreCompassTimer--; |
476 | } |
479 | } |
477 | } else { // ignore compass at extreme attitudes for a while |
480 | } else { // ignore compass at extreme attitudes for a while |
Line 478... | Line 481... | ||
478 | ignoreCompassTimer = 500; |
481 | ignoreCompassTimer = 500; |
479 | } |
482 | } |
480 | } |
483 | } |