Rev 1796 | Rev 1854 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1796 | Rev 1821 | ||
---|---|---|---|
Line 75... | Line 75... | ||
75 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
75 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
76 | * the offsets with the DAC. |
76 | * the offsets with the DAC. |
77 | */ |
77 | */ |
78 | volatile int16_t rawGyroSum[3]; |
78 | volatile int16_t rawGyroSum[3]; |
79 | volatile int16_t acc[3]; |
79 | volatile int16_t acc[3]; |
80 | volatile int16_t filteredAcc[2]={0,0}; |
80 | volatile int16_t filteredAcc[2] = { 0, 0 }; |
Line 81... | Line 81... | ||
81 | 81 | ||
82 | /* |
82 | /* |
83 | * These 4 exported variables are zero-offset. The "PID" ones are used |
83 | * These 4 exported variables are zero-offset. The "PID" ones are used |
84 | * in the attitude control as rotation rates. The "ATT" ones are for |
84 | * in the attitude control as rotation rates. The "ATT" ones are for |
Line 92... | Line 92... | ||
92 | /* |
92 | /* |
93 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
93 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
94 | * standing still. They are used for adjusting the gyro and acc. meter values |
94 | * standing still. They are used for adjusting the gyro and acc. meter values |
95 | * to be centered on zero. |
95 | * to be centered on zero. |
96 | */ |
96 | */ |
97 | volatile int16_t gyroOffset[3] = { |
- | |
98 | 512 * GYRO_SUMMATION_FACTOR_PITCHROLL, |
97 | volatile int16_t gyroOffset[3] = { 512 * GYRO_SUMMATION_FACTOR_PITCHROLL, 512 |
99 | 512 * GYRO_SUMMATION_FACTOR_PITCHROLL, |
98 | * GYRO_SUMMATION_FACTOR_PITCHROLL, 512 * GYRO_SUMMATION_FACTOR_YAW }; |
100 | 512 * GYRO_SUMMATION_FACTOR_YAW |
- | |
101 | }; |
- | |
102 | 99 | ||
103 | volatile int16_t accOffset[3] = { |
- | |
104 | 512 * ACC_SUMMATION_FACTOR_PITCHROLL, |
100 | volatile int16_t accOffset[3] = { 512 * ACC_SUMMATION_FACTOR_PITCHROLL, 512 |
105 | 512 * ACC_SUMMATION_FACTOR_PITCHROLL, |
101 | * ACC_SUMMATION_FACTOR_PITCHROLL, 512 * ACC_SUMMATION_FACTOR_Z }; |
106 | 512 * ACC_SUMMATION_FACTOR_Z |
- | |
107 | }; |
- | |
Line 108... | Line 102... | ||
108 | 102 | ||
109 | /* |
103 | /* |
110 | * This allows some experimentation with the gyro filters. |
104 | * This allows some experimentation with the gyro filters. |
111 | * Should be replaced by #define's later on... |
105 | * Should be replaced by #define's later on... |
Line 174... | Line 168... | ||
174 | * re-enabling ADC, the real limit is (how much?) lower. |
168 | * re-enabling ADC, the real limit is (how much?) lower. |
175 | * The acc. sensor is sampled even if not used - or installed |
169 | * The acc. sensor is sampled even if not used - or installed |
176 | * at all. The cost is not significant. |
170 | * at all. The cost is not significant. |
177 | */ |
171 | */ |
Line 178... | Line 172... | ||
178 | 172 | ||
179 | const uint8_t channelsForStates[] PROGMEM = { |
- | |
180 | AD_GYRO_PITCH, |
- | |
181 | AD_GYRO_ROLL, |
173 | const uint8_t channelsForStates[] PROGMEM = { AD_GYRO_PITCH, AD_GYRO_ROLL, |
182 | AD_GYRO_YAW, |
174 | AD_GYRO_YAW, |
183 | - | ||
184 | AD_ACC_PITCH, |
- | |
185 | AD_ACC_ROLL, |
175 | |
186 | AD_AIRPRESSURE, |
176 | AD_ACC_PITCH, AD_ACC_ROLL, AD_AIRPRESSURE, |
187 | - | ||
188 | AD_GYRO_PITCH, |
- | |
189 | AD_GYRO_ROLL, |
177 | |
190 | AD_ACC_Z, // at 8, measure Z acc. |
178 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_ACC_Z, // at 8, measure Z acc. |
191 | - | ||
192 | AD_GYRO_PITCH, |
- | |
193 | AD_GYRO_ROLL, |
179 | |
194 | AD_GYRO_YAW, // at 11, finish yaw gyro |
180 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, // at 11, finish yaw gyro |
195 | 181 | ||
196 | AD_ACC_PITCH, // at 12, finish pitch axis acc. |
182 | AD_ACC_PITCH, // at 12, finish pitch axis acc. |
197 | AD_ACC_ROLL, // at 13, finish roll axis acc. |
183 | AD_ACC_ROLL, // at 13, finish roll axis acc. |
198 | AD_AIRPRESSURE, // at 14, finish air pressure. |
184 | AD_AIRPRESSURE, // at 14, finish air pressure. |
199 | 185 | ||
200 | AD_GYRO_PITCH, // at 15, finish pitch gyro |
186 | AD_GYRO_PITCH, // at 15, finish pitch gyro |
201 | AD_GYRO_ROLL, // at 16, finish roll gyro |
187 | AD_GYRO_ROLL, // at 16, finish roll gyro |
202 | AD_UBAT // at 17, measure battery. |
188 | AD_UBAT // at 17, measure battery. |
Line 203... | Line 189... | ||
203 | }; |
189 | }; |
204 | 190 | ||
Line 205... | Line 191... | ||
205 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
191 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
206 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
192 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
207 | 193 | ||
208 | void analog_init(void) { |
194 | void analog_init(void) { |
209 | uint8_t sreg = SREG; |
195 | uint8_t sreg = SREG; |
210 | // disable all interrupts before reconfiguration |
196 | // disable all interrupts before reconfiguration |
211 | cli(); |
197 | cli(); |
212 | 198 | ||
213 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
199 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
214 | DDRA = 0x00; |
200 | DDRA = 0x00; |
215 | PORTA = 0x00; |
201 | PORTA = 0x00; |
216 | // Digital Input Disable Register 0 |
202 | // Digital Input Disable Register 0 |
217 | // Disable digital input buffer for analog adc_channel pins |
203 | // Disable digital input buffer for analog adc_channel pins |
218 | DIDR0 = 0xFF; |
204 | DIDR0 = 0xFF; |
219 | // external reference, adjust data to the right |
205 | // external reference, adjust data to the right |
220 | ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR)); |
206 | ADMUX &= ~((1 << REFS1) | (1 << REFS0) | (1 << ADLAR)); |
221 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
207 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
222 | ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH; |
208 | ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH; |
- | 209 | //Set ADC Control and Status Register A |
|
223 | //Set ADC Control and Status Register A |
210 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
224 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
211 | ADCSRA = (0 << ADEN) | (0 << ADSC) | (0 << ADATE) | (1 << ADPS2) | (1 |
225 | ADCSRA = (0<<ADEN)|(0<<ADSC)|(0<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(0<<ADIE); |
212 | << ADPS1) | (1 << ADPS0) | (0 << ADIE); |
226 | //Set ADC Control and Status Register B |
213 | //Set ADC Control and Status Register B |
227 | //Trigger Source to Free Running Mode |
214 | //Trigger Source to Free Running Mode |
228 | ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0)); |
215 | ADCSRB &= ~((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0)); |
229 | // Start AD conversion |
216 | // Start AD conversion |
230 | analog_start(); |
217 | analog_start(); |
Line -... | Line 218... | ||
- | 218 | // restore global interrupt flags |
|
231 | // restore global interrupt flags |
219 | SREG = sreg; |
232 | SREG = sreg; |
220 | } |
233 | } |
221 | |
234 | 222 | void measureNoise(const int16_t sensor, |
|
235 | void measureNoise(const int16_t sensor, volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
223 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
236 | if (sensor > (int16_t)(*noiseMeasurement)) { |
224 | if (sensor > (int16_t) (*noiseMeasurement)) { |
237 | *noiseMeasurement = sensor; |
225 | *noiseMeasurement = sensor; |
238 | } else if (-sensor > (int16_t)(*noiseMeasurement)) { |
226 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
239 | *noiseMeasurement = -sensor; |
227 | *noiseMeasurement = -sensor; |
240 | } else if (*noiseMeasurement > damping) { |
228 | } else if (*noiseMeasurement > damping) { |
241 | *noiseMeasurement -= damping; |
229 | *noiseMeasurement -= damping; |
Line 242... | Line 230... | ||
242 | } else { |
230 | } else { |
243 | *noiseMeasurement = 0; |
231 | *noiseMeasurement = 0; |
244 | } |
232 | } |
245 | } |
233 | } |
246 | 234 | ||
247 | /* |
235 | /* |
248 | * Min.: 0 |
236 | * Min.: 0 |
Line 249... | Line 237... | ||
249 | * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type. |
237 | * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type. |
250 | */ |
238 | */ |
251 | uint16_t getSimplePressure(int advalue) { |
239 | uint16_t getSimplePressure(int advalue) { |
252 | return (uint16_t)OCR0A * (uint16_t)rangewidth + advalue; |
240 | return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue; |
253 | } |
241 | } |
254 | 242 | ||
328 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
447 | for (i = 0; i < 8; i++) { |
329 | OCR0A = newrange; |
- | |
330 | } else { |
- | |
331 | if (OCR0A) { |
- | |
332 | OCR0A--; |
- | |
333 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
- | |
334 | } |
- | |
335 | } |
- | |
336 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
- | |
337 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
- | |
338 | // If near the end, make a limited increase |
- | |
339 | newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
- | |
340 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
- | |
341 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
- | |
342 | OCR0A = newrange; |
- | |
343 | } else { |
- | |
Line 344... | Line -... | ||
344 | if (OCR0A<254) { |
- | |
345 | OCR0A++; |
- | |
346 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
- | |
347 | } |
- | |
348 | } |
- | |
349 | } |
- | |
350 | - | ||
351 | // Even if the sample is off-range, use it. |
- | |
352 | simpleAirPressure = getSimplePressure(rawAirPressure); |
- | |
353 | DebugOut.Analog[27] = (uint16_t)OCR0A; |
- | |
354 | DebugOut.Analog[31] = simpleAirPressure; |
- | |
355 | - | ||
356 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
- | |
357 | // Danger: pressure near lower end of range. If the measurement saturates, the |
- | |
358 | // copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
- | |
359 | airPressureSum += (int16_t)MIN_RANGES_EXTRAPOLATION * rangewidth + (simpleAirPressure - (int16_t)MIN_RANGES_EXTRAPOLATION * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
- | |
360 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
- | |
361 | // Danger: pressure near upper end of range. If the measurement saturates, the |
- | |
362 | // copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
- | |
363 | airPressureSum += (int16_t)MAX_RANGES_EXTRAPOLATION * rangewidth + (simpleAirPressure - (int16_t)MAX_RANGES_EXTRAPOLATION * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
- | |
364 | } else { |
- | |
365 | // normal case. |
- | |
366 | // If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample. |
- | |
367 | // The 2 cases above (end of range) are ignored for this. |
- | |
368 | if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR - 1) |
- | |
369 | airPressureSum += simpleAirPressure / 2; |
- | |
370 | else |
- | |
371 | airPressureSum += simpleAirPressure; |
- | |
372 | } |
- | |
373 | - | ||
374 | // 2 samples were added. |
- | |
375 | pressureMeasurementCount += 2; |
- | |
376 | if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) { |
- | |
377 | filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER-1) + airPressureSum + AIRPRESSURE_FILTER/2) / AIRPRESSURE_FILTER; |
- | |
378 | pressureMeasurementCount = airPressureSum = 0; |
- | |
379 | } |
- | |
380 | - | ||
381 | break; |
- | |
382 | - | ||
383 | case 15: |
- | |
384 | case 16: // pitch or roll gyro. |
- | |
385 | axis = state - 16; |
- | |
386 | tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH - axis]; |
- | |
387 | // DebugOut.Analog[6 + 3 * axis ] = tempGyro; |
- | |
388 | /* |
- | |
389 | * Process the gyro data for the PID controller. |
- | |
390 | */ |
- | |
391 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
- | |
392 | // gyro with a wider range, and helps counter saturation at full control. |
- | |
393 | - | ||
394 | if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER) { |
- | |
395 | if (tempGyro < SENSOR_MIN_PITCHROLL) { |
- | |
396 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
- | |
397 | } |
- | |
398 | else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
- | |
399 | tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE + SENSOR_MAX_PITCHROLL; |
- | |
400 | } |
- | |
401 | } |
- | |
402 | - | ||
403 | // 2) Apply sign and offset, scale before filtering. |
- | |
404 | if (GYRO_REVERSED[axis]) { |
- | |
405 | tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
- | |
406 | } else { |
- | |
407 | tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL; |
- | |
408 | } |
- | |
409 | - | ||
410 | // 3) Scale and filter. |
- | |
411 | tempOffsetGyro = (gyro_PID[axis] * (GYROS_PID_FILTER-1) + tempOffsetGyro) / GYROS_PID_FILTER; |
- | |
412 | - | ||
413 | // 4) Measure noise. |
- | |
414 | measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
- | |
415 | - | ||
416 | // 5) Differential measurement. |
- | |
417 | gyroD[axis] = (gyroD[axis] * (GYROS_D_FILTER-1) + (tempOffsetGyro - gyro_PID[axis])) / GYROS_D_FILTER; |
- | |
418 | - | ||
419 | // 6) Done. |
- | |
420 | gyro_PID[axis] = tempOffsetGyro; |
- | |
421 | - | ||
422 | /* |
- | |
423 | * Now process the data for attitude angles. |
- | |
424 | */ |
- | |
425 | tempGyro = rawGyroSum[axis]; |
- | |
426 | - | ||
427 | // 1) Apply sign and offset, scale before filtering. |
- | |
428 | if (GYRO_REVERSED[axis]) { |
- | |
429 | tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
- | |
430 | } else { |
- | |
431 | tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL; |
- | |
432 | } |
- | |
433 | - | ||
434 | // 2) Filter. |
- | |
435 | gyro_ATT[axis] = (gyro_ATT[axis] * (GYROS_ATT_FILTER-1) + tempOffsetGyro) / GYROS_ATT_FILTER; |
- | |
436 | break; |
- | |
437 | - | ||
438 | case 17: |
- | |
439 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
- | |
440 | // This is divided by 3 --> 10.34 counts per volt. |
- | |
441 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
- | |
442 | DebugOut.Analog[11] = UBat; |
- | |
443 | analogDataReady = 1; // mark |
- | |
444 | ADCycleCount++; |
- | |
445 | // Stop the sampling. Cycle is over. |
- | |
446 | state = 0; |
- | |
447 | for (i=0; i<8; i++) { |
448 | sensorInputs[i] = 0; |
448 | sensorInputs[i] = 0; |
449 | } |
449 | } |
450 | break; |
450 | break; |
451 | default: { |
451 | default: {} // do nothing. |
452 | } // do nothing. |
452 | } |
453 | } |
453 | 454 | ||
- | 455 | // set up for next state. |
|
454 | // set up for next state. |
456 | ad_channel = pgm_read_byte(&channelsForStates[state]); |
455 | ad_channel = pgm_read_byte(&channelsForStates[state]); |
457 | // ad_channel = channelsForStates[state]; |
Line 456... | Line 458... | ||
456 | // ad_channel = channelsForStates[state]; |
458 | |
457 | 459 | // set adc muxer to next ad_channel |
|
458 | // set adc muxer to next ad_channel |
460 | ADMUX = (ADMUX & 0xE0) | ad_channel; |
459 | ADMUX = (ADMUX & 0xE0) | ad_channel; |
461 | // after full cycle stop further interrupts |
Line 460... | Line 462... | ||
460 | // after full cycle stop further interrupts |
462 | if (state) |
461 | if(state) analog_start(); |
463 | analog_start(); |
462 | } |
464 | } |
463 | 465 | ||
464 | void analog_calibrate(void) { |
466 | void analog_calibrate(void) { |
465 | #define GYRO_OFFSET_CYCLES 32 |
467 | #define GYRO_OFFSET_CYCLES 32 |
466 | uint8_t i, axis; |
468 | uint8_t i, axis; |
467 | int32_t deltaOffsets[3] = {0,0,0}; |
469 | int32_t deltaOffsets[3] = { 0, 0, 0 }; |
468 | 470 | ||
469 | // Set the filters... to be removed again, once some good settings are found. |
471 | // Set the filters... to be removed again, once some good settings are found. |
470 | GYROS_PID_FILTER = (dynamicParams.UserParams[4] & 0b00000011) + 1; |
472 | GYROS_PID_FILTER = (dynamicParams.UserParams[4] & 0b00000011) + 1; |
471 | GYROS_ATT_FILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1; |
473 | GYROS_ATT_FILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1; |
472 | GYROS_D_FILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1; |
474 | GYROS_D_FILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1; |
473 | ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1; |
- | |
474 | 475 | ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1; |
|
475 | gyro_calibrate(); |
- | |
476 | - | ||
477 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
- | |
478 | for(i=0; i < GYRO_OFFSET_CYCLES; i++) { |
- | |
479 | Delay_ms_Mess(20); |
476 | |
480 | for (axis=PITCH; axis<=YAW; axis++) { |
- | |
481 | deltaOffsets[axis] += rawGyroSum[axis]; |
- | |
482 | } |
- | |
483 | } |
- | |
484 | - | ||
485 | for (axis=PITCH; axis<=YAW; axis++) { |
- | |
486 | gyroOffset[axis] = (deltaOffsets[axis] + GYRO_OFFSET_CYCLES/2) / GYRO_OFFSET_CYCLES; |
- | |
487 | DebugOut.Analog[20+axis] = gyroOffset[axis]; |
- | |
488 | } |
- | |
489 | - | ||
490 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
- | |
Line -... | Line 477... | ||
- | 477 | gyro_calibrate(); |
|
- | 478 | ||
- | 479 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
|
- | 480 | for (i = 0; i < GYRO_OFFSET_CYCLES; i++) { |
|
- | 481 | Delay_ms_Mess(20); |
|
- | 482 | for (axis = PITCH; axis <= YAW; axis++) { |
|
- | 483 | deltaOffsets[axis] += rawGyroSum[axis]; |
|
- | 484 | } |
|
- | 485 | } |
|
- | 486 | ||
- | 487 | for (axis = PITCH; axis <= YAW; axis++) { |
|
- | 488 | gyroOffset[axis] = (deltaOffsets[axis] + GYRO_OFFSET_CYCLES / 2) |
|
- | 489 | / GYRO_OFFSET_CYCLES; |
|
- | 490 | DebugOut.Analog[20 + axis] = gyroOffset[axis]; |
|
- | 491 | } |
|
- | 492 | ||
- | 493 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
|
491 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
494 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
492 | 495 | ||
Line 493... | Line 496... | ||
493 | accOffset[PITCH] = GetParamWord(PID_ACC_PITCH); |
496 | accOffset[PITCH] = GetParamWord(PID_ACC_PITCH); |
494 | accOffset[ROLL] = GetParamWord(PID_ACC_ROLL); |
497 | accOffset[ROLL] = GetParamWord(PID_ACC_ROLL); |
495 | accOffset[Z] = GetParamWord(PID_ACC_Z); |
498 | accOffset[Z] = GetParamWord(PID_ACC_Z); |
Line 508... | Line 511... | ||
508 | * anyway. There would be nothing wrong with updating the variables |
511 | * anyway. There would be nothing wrong with updating the variables |
509 | * directly from here, though. |
512 | * directly from here, though. |
510 | */ |
513 | */ |
511 | void analog_calibrateAcc(void) { |
514 | void analog_calibrateAcc(void) { |
512 | #define ACC_OFFSET_CYCLES 10 |
515 | #define ACC_OFFSET_CYCLES 10 |
513 | uint8_t i, axis; |
516 | uint8_t i, axis; |
514 | int32_t deltaOffset[3] = {0,0,0}; |
517 | int32_t deltaOffset[3] = { 0, 0, 0 }; |
515 | int16_t filteredDelta; |
518 | int16_t filteredDelta; |
516 | // int16_t pressureDiff, savedRawAirPressure; |
519 | // int16_t pressureDiff, savedRawAirPressure; |
517 | 520 | ||
518 | for(i=0; i < ACC_OFFSET_CYCLES; i++) { |
521 | for (i = 0; i < ACC_OFFSET_CYCLES; i++) { |
519 | Delay_ms_Mess(10); |
522 | Delay_ms_Mess(10); |
520 | for (axis=PITCH; axis<=YAW; axis++) { |
523 | for (axis = PITCH; axis <= YAW; axis++) { |
521 | deltaOffset[axis] += acc[axis]; |
524 | deltaOffset[axis] += acc[axis]; |
522 | } |
525 | } |
523 | } |
526 | } |
524 | 527 | ||
525 | for (axis=PITCH; axis<=YAW; axis++) { |
528 | for (axis = PITCH; axis <= YAW; axis++) { |
526 | filteredDelta = (deltaOffset[axis] + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES; |
529 | filteredDelta = (deltaOffset[axis] + ACC_OFFSET_CYCLES / 2) |
- | 530 | / ACC_OFFSET_CYCLES; |
|
527 | accOffset[axis] += ACC_REVERSED[axis] ? -filteredDelta : filteredDelta; |
531 | accOffset[axis] += ACC_REVERSED[axis] ? -filteredDelta : filteredDelta; |
528 | } |
532 | } |
529 | 533 | ||
530 | // Save ACC neutral settings to eeprom |
534 | // Save ACC neutral settings to eeprom |
531 | SetParamWord(PID_ACC_PITCH, accOffset[PITCH]); |
535 | SetParamWord(PID_ACC_PITCH, accOffset[PITCH]); |
532 | SetParamWord(PID_ACC_ROLL, accOffset[ROLL]); |
536 | SetParamWord(PID_ACC_ROLL, accOffset[ROLL]); |
533 | SetParamWord(PID_ACC_Z, accOffset[Z]); |
537 | SetParamWord(PID_ACC_Z, accOffset[Z]); |
534 | 538 | ||
535 | // Noise is relative to offset. So, reset noise measurements when |
539 | // Noise is relative to offset. So, reset noise measurements when |
536 | // changing offsets. |
540 | // changing offsets. |
537 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
541 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
538 | 542 | ||
539 | // Setting offset values has an influence in the analog.c ISR |
543 | // Setting offset values has an influence in the analog.c ISR |
540 | // Therefore run measurement for 100ms to achive stable readings |
544 | // Therefore run measurement for 100ms to achive stable readings |
541 | Delay_ms_Mess(100); |
545 | Delay_ms_Mess(100); |
542 | 546 | ||
543 | // Set the feedback so that air pressure ends up in the middle of the range. |
547 | // Set the feedback so that air pressure ends up in the middle of the range. |
544 | // (raw pressure high --> OCR0A also high...) |
548 | // (raw pressure high --> OCR0A also high...) |
545 | /* |
549 | /* |
546 | OCR0A += ((rawAirPressure - 1024) / rangewidth) - 1; |
550 | OCR0A += ((rawAirPressure - 1024) / rangewidth) - 1; |
547 | Delay_ms_Mess(1000); |
551 | Delay_ms_Mess(1000); |
548 | 552 | ||
549 | pressureDiff = 0; |
553 | pressureDiff = 0; |
550 | // DebugOut.Analog[16] = rawAirPressure; |
554 | // DebugOut.Analog[16] = rawAirPressure; |
551 | |
555 | |
552 | #define PRESSURE_CAL_CYCLE_COUNT 5 |
556 | #define PRESSURE_CAL_CYCLE_COUNT 5 |
553 | for (i=0; i<PRESSURE_CAL_CYCLE_COUNT; i++) { |
557 | for (i=0; i<PRESSURE_CAL_CYCLE_COUNT; i++) { |
554 | savedRawAirPressure = rawAirPressure; |
558 | savedRawAirPressure = rawAirPressure; |
555 | OCR0A+=2; |
559 | OCR0A+=2; |
556 | Delay_ms_Mess(500); |
560 | Delay_ms_Mess(500); |
557 | // raw pressure will decrease. |
561 | // raw pressure will decrease. |
558 | pressureDiff += (savedRawAirPressure - rawAirPressure); |
562 | pressureDiff += (savedRawAirPressure - rawAirPressure); |
559 | savedRawAirPressure = rawAirPressure; |
563 | savedRawAirPressure = rawAirPressure; |
560 | OCR0A-=2; |
564 | OCR0A-=2; |
561 | Delay_ms_Mess(500); |
565 | Delay_ms_Mess(500); |
562 | // raw pressure will increase. |
566 | // raw pressure will increase. |
563 | pressureDiff += (rawAirPressure - savedRawAirPressure); |
567 | pressureDiff += (rawAirPressure - savedRawAirPressure); |
564 | } |
568 | } |
565 | |
569 | |
566 | rangewidth = (pressureDiff + PRESSURE_CAL_CYCLE_COUNT * 2 * 2 - 1) / (PRESSURE_CAL_CYCLE_COUNT * 2 * 2); |
570 | rangewidth = (pressureDiff + PRESSURE_CAL_CYCLE_COUNT * 2 * 2 - 1) / (PRESSURE_CAL_CYCLE_COUNT * 2 * 2); |
567 | DebugOut.Analog[27] = rangewidth; |
571 | DebugOut.Analog[27] = rangewidth; |
568 | */ |
572 | */ |
569 | } |
573 | } |