Subversion Repositories FlightCtrl

Rev

Rev 1887 | Rev 1955 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1887 Rev 1952
Line 66... Line 66...
66
#include "eeprom.h"
66
#include "eeprom.h"
Line 67... Line 67...
67
 
67
 
68
// For DebugOut.Digital
68
// For DebugOut.Digital
Line -... Line 69...
-
 
69
#include "output.h"
-
 
70
 
-
 
71
// set ADC enable & ADC Start Conversion & ADC Interrupt Enable bit
69
#include "output.h"
72
#define startADC() (ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIE))
70
 
73
 
71
/*
74
/*
72
 * For each A/D conversion cycle, each analog channel is sampled a number of times
75
 * For each A/D conversion cycle, each analog channel is sampled a number of times
73
 * (see array channelsForStates), and the results for each channel are summed.
76
 * (see array channelsForStates), and the results for each channel are summed.
74
 * Here are those for the gyros and the acc. meters. They are not zero-offset.
77
 * Here are those for the gyros and the acc. meters. They are not zero-offset.
75
 * They are exported in the analog.h file - but please do not use them! The only
78
 * They are exported in the analog.h file - but please do not use them! The only
76
 * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating
79
 * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating
-
 
80
 * the offsets with the DAC.
77
 * the offsets with the DAC.
81
 */
78
 */
82
volatile uint16_t sensorInputs[8];
79
volatile int16_t rawGyroSum[3];
83
volatile int16_t rawGyroSum[3];
80
volatile int16_t acc[3];
84
volatile int16_t acc[3];
Line 201... Line 205...
201
        PORTA = 0x00;
205
        PORTA = 0x00;
202
        // Digital Input Disable Register 0
206
        // Digital Input Disable Register 0
203
        // Disable digital input buffer for analog adc_channel pins
207
        // Disable digital input buffer for analog adc_channel pins
204
        DIDR0 = 0xFF;
208
        DIDR0 = 0xFF;
205
        // external reference, adjust data to the right
209
        // external reference, adjust data to the right
206
        ADMUX &= ~((1 << REFS1) | (1 << REFS0) | (1 << ADLAR));
210
        ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR));
207
        // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
211
        // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
208
        ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH;
212
        ADMUX = (ADMUX & 0xE0) | channelsForStates[0];
209
        //Set ADC Control and Status Register A
213
        //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
214
        //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
-
 
212
                        << ADPS1) | (1 << ADPS0) | (0 << ADIE);
215
        ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
213
        //Set ADC Control and Status Register B
216
        //Set ADC Control and Status Register B
214
        //Trigger Source to Free Running Mode
217
        //Trigger Source to Free Running Mode
215
        ADCSRB &= ~((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0));
218
        ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0));
-
 
219
 
216
        // Start AD conversion
220
        startAnalogConversionCycle();
217
        analog_start();
-
 
-
 
221
 
218
        // restore global interrupt flags
222
        // restore global interrupt flags
219
        SREG = sreg;
223
        SREG = sreg;
220
}
224
}
Line 221... Line 225...
221
 
225
 
Line 238... Line 242...
238
 */
242
 */
239
uint16_t getSimplePressure(int advalue) {
243
uint16_t getSimplePressure(int advalue) {
240
        return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue;
244
        return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue;
241
}
245
}
Line -... Line 246...
-
 
246
 
-
 
247
void startAnalogConversionCycle(void) {
-
 
248
  // Stop the sampling. Cycle is over.
-
 
249
  for (uint8_t i = 0; i < 8; i++) {
-
 
250
    sensorInputs[i] = 0;
-
 
251
  }
-
 
252
  ADMUX = (ADMUX & 0xE0) | channelsForStates[0];
-
 
253
  startADC();
-
 
254
}
242
 
255
 
243
/*****************************************************
256
/*****************************************************
244
 * Interrupt Service Routine for ADC
257
 * Interrupt Service Routine for ADC
245
 * Runs at 312.5 kHz or 3.2 µs. When all states are
-
 
246
 * processed the interrupt is disabled and further
258
 * Runs at 312.5 kHz or 3.2 µs. When all states are
247
 * AD conversions are stopped.
259
 * processed further conversions are stopped.
248
 *****************************************************/
260
 *****************************************************/
249
ISR(ADC_vect) {
261
ISR(ADC_vect) {
250
        static uint8_t ad_channel = AD_GYRO_PITCH, state = 0;
-
 
251
        static uint16_t sensorInputs[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
-
 
252
        static uint16_t pressureAutorangingWait = 25;
-
 
253
        uint16_t rawAirPressure;
-
 
254
        uint8_t i, axis;
-
 
255
        int16_t newrange;
-
 
256
 
-
 
257
        // for various filters...
-
 
258
        int16_t tempOffsetGyro, tempGyro;
-
 
259
 
262
  static uint8_t ad_channel = AD_GYRO_PITCH, state = 0;
260
        sensorInputs[ad_channel] += ADC;
-
 
261
 
-
 
262
        /*
-
 
263
         * Actually we don't need this "switch". We could do all the sampling into the
-
 
264
         * sensorInputs array first, and all the processing after the last sample.
-
 
265
         */
-
 
266
        switch (state++) {
-
 
267
 
-
 
268
        case 8: // Z acc
-
 
269
                if (ACC_REVERSED[Z])
-
 
270
                        acc[Z] = accOffset[Z] - sensorInputs[AD_ACC_Z];
-
 
271
                else
-
 
272
                        acc[Z] = sensorInputs[AD_ACC_Z] - accOffset[Z];
-
 
273
 
-
 
274
                /*
-
 
275
        stronglyFilteredAcc[Z] =
-
 
276
            (stronglyFilteredAcc[Z] * 99 + acc[Z] * 10) / 100;
-
 
277
        */
-
 
278
 
-
 
279
                break;
-
 
280
 
-
 
281
        case 11: // yaw gyro
-
 
282
                rawGyroSum[YAW] = sensorInputs[AD_GYRO_YAW];
-
 
283
                if (GYRO_REVERSED[YAW])
-
 
284
                        yawGyro = gyroOffset[YAW] - sensorInputs[AD_GYRO_YAW];
-
 
285
                else
-
 
286
                        yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset[YAW];
-
 
287
                break;
-
 
288
 
-
 
289
        case 12: // pitch axis acc.
-
 
290
                if (ACC_REVERSED[PITCH])
-
 
291
                        acc[PITCH] = accOffset[PITCH] - sensorInputs[AD_ACC_PITCH];
-
 
292
                else
-
 
293
                        acc[PITCH] = sensorInputs[AD_ACC_PITCH] - accOffset[PITCH];
-
 
294
 
-
 
295
                filteredAcc[PITCH] =
-
 
296
                    (filteredAcc[PITCH] * (ACC_FILTER - 1) + acc[PITCH]) / ACC_FILTER;
-
 
297
 
-
 
298
                /*
-
 
299
                stronglyFilteredAcc[PITCH] =
-
 
300
                    (stronglyFilteredAcc[PITCH] * 99 + acc[PITCH] * 10) / 100;
-
 
301
                */
-
 
302
 
-
 
303
                measureNoise(acc[PITCH], &accNoisePeak[PITCH], 1);
-
 
304
                break;
-
 
305
 
263
  sensorInputs[ad_channel] += ADC;
306
        case 13: // roll axis acc.
-
 
307
                if (ACC_REVERSED[ROLL])
-
 
308
                        acc[ROLL] = accOffset[ROLL] - sensorInputs[AD_ACC_ROLL];
-
 
309
                else
-
 
310
                        acc[ROLL] = sensorInputs[AD_ACC_ROLL] - accOffset[ROLL];
-
 
311
                filteredAcc[ROLL] =
-
 
312
                    (filteredAcc[ROLL] * (ACC_FILTER - 1) + acc[ROLL]) / ACC_FILTER;
-
 
313
 
-
 
314
                /*
-
 
315
        stronglyFilteredAcc[ROLL] =
-
 
316
            (stronglyFilteredAcc[ROLL] * 99 + acc[ROLL] * 10) / 100;
264
  // set up for next state.
317
        */
-
 
318
 
-
 
319
                measureNoise(acc[ROLL], &accNoisePeak[ROLL], 1);
-
 
320
                break;
-
 
321
 
-
 
322
        case 14: // air pressure
-
 
323
                if (pressureAutorangingWait) {
-
 
324
                        //A range switch was done recently. Wait for steadying.
-
 
325
                        pressureAutorangingWait--;
-
 
326
                        DebugOut.Analog[27] = (uint16_t) OCR0A;
-
 
327
                        DebugOut.Analog[31] = simpleAirPressure;
-
 
328
                        break;
-
 
329
                }
-
 
330
 
-
 
331
                rawAirPressure = sensorInputs[AD_AIRPRESSURE];
-
 
332
                if (rawAirPressure < MIN_RAWPRESSURE) {
-
 
333
                        // value is too low, so decrease voltage on the op amp minus input, making the value higher.
-
 
334
                        newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1;
-
 
335
                        if (newrange > MIN_RANGES_EXTRAPOLATION) {
-
 
336
                                pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 +
-
 
337
                                OCR0A = newrange;
-
 
338
                        } else {
-
 
339
                                if (OCR0A) {
-
 
340
                                        OCR0A--;
-
 
341
                                        pressureAutorangingWait = AUTORANGE_WAIT_FACTOR;
-
 
342
                                }
-
 
343
                        }
-
 
344
                } else if (rawAirPressure > MAX_RAWPRESSURE) {
-
 
345
                        // value is too high, so increase voltage on the op amp minus input, making the value lower.
-
 
346
                        // If near the end, make a limited increase
-
 
347
                        newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4;  // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1;
-
 
348
                        if (newrange < MAX_RANGES_EXTRAPOLATION) {
-
 
349
                                pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR;
-
 
350
                                OCR0A = newrange;
-
 
351
                        } else {
265
  state++;
352
                                if (OCR0A < 254) {
-
 
353
                                        OCR0A++;
-
 
354
                                        pressureAutorangingWait = AUTORANGE_WAIT_FACTOR;
-
 
355
                                }
-
 
356
                        }
-
 
357
                }
-
 
358
 
-
 
359
                // Even if the sample is off-range, use it.
266
  if (state < 18) {
360
                simpleAirPressure = getSimplePressure(rawAirPressure);
267
    ad_channel = pgm_read_byte(&channelsForStates[state]);
361
                DebugOut.Analog[27] = (uint16_t) OCR0A;
268
    // set adc muxer to next ad_channel
362
                DebugOut.Analog[31] = simpleAirPressure;
-
 
363
 
-
 
364
                if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) {
-
 
365
                        // Danger: pressure near lower end of range. If the measurement saturates, the
-
 
366
                        // copter may climb uncontrolledly... Simulate a drastic reduction in pressure.
-
 
367
                        DebugOut.Digital[1] |= DEBUG_SENSORLIMIT;
-
 
368
                        airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth
-
 
369
                                        + (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION
-
 
370
                                                        * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF;
-
 
371
                } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) {
-
 
372
                        // Danger: pressure near upper end of range. If the measurement saturates, the
-
 
373
                        // copter may descend uncontrolledly... Simulate a drastic increase in pressure.
-
 
374
                        DebugOut.Digital[1] |= DEBUG_SENSORLIMIT;
-
 
375
                        airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth
-
 
376
                                        + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION
-
 
377
                                                        * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF;
-
 
378
                } else {
-
 
379
                        // normal case.
-
 
380
                        // If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample.
269
    ADMUX = (ADMUX & 0xE0) | ad_channel;
381
                        // The 2 cases above (end of range) are ignored for this.
-
 
382
                        DebugOut.Digital[1] &= ~DEBUG_SENSORLIMIT;
-
 
383
                        if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR - 1)
-
 
384
                                airPressureSum += simpleAirPressure / 2;
-
 
385
                        else
-
 
386
                                airPressureSum += simpleAirPressure;
-
 
387
                }
-
 
388
 
-
 
389
                // 2 samples were added.
-
 
390
                pressureMeasurementCount += 2;
-
 
391
                if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) {
-
 
392
                        filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1)
-
 
393
                                        + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER;
-
 
394
                        pressureMeasurementCount = airPressureSum = 0;
-
 
395
                }
-
 
396
 
-
 
397
                break;
-
 
398
 
-
 
399
        case 15:
-
 
400
        case 16: // pitch or roll gyro.
270
    // after full cycle stop further interrupts
401
                axis = state - 16;
-
 
402
                tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH - axis];
-
 
403
                // DebugOut.Analog[6 + 3 * axis ] = tempGyro;
-
 
404
                /*
-
 
405
                 * Process the gyro data for the PID controller.
-
 
406
                 */
-
 
407
                // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a
-
 
408
                //    gyro with a wider range, and helps counter saturation at full control.
-
 
409
 
-
 
410
                if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER) {
-
 
411
                        if (tempGyro < SENSOR_MIN_PITCHROLL) {
-
 
412
                                DebugOut.Digital[0] |= DEBUG_SENSORLIMIT;
-
 
413
                                tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT;
-
 
414
                        } else if (tempGyro > SENSOR_MAX_PITCHROLL) {
-
 
415
                                DebugOut.Digital[0] |= DEBUG_SENSORLIMIT;
-
 
416
                                tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE
-
 
417
                                                + SENSOR_MAX_PITCHROLL;
-
 
418
                        } else {
-
 
419
                                DebugOut.Digital[0] &= ~DEBUG_SENSORLIMIT;
-
 
420
                        }
-
 
421
                }
-
 
422
 
-
 
423
                // 2) Apply sign and offset, scale before filtering.
-
 
424
                if (GYRO_REVERSED[axis]) {
-
 
425
                        tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
271
    startADC();
426
                } else {
-
 
427
                        tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
-
 
428
                }
-
 
429
 
-
 
430
                // 3) Scale and filter.
-
 
431
                tempOffsetGyro = (gyro_PID[axis] * (GYROS_PID_FILTER - 1) + tempOffsetGyro)
-
 
432
                                / GYROS_PID_FILTER;
-
 
433
 
-
 
434
                // 4) Measure noise.
-
 
435
                measureNoise(tempOffsetGyro, &gyroNoisePeak[axis],
-
 
436
                                GYRO_NOISE_MEASUREMENT_DAMPING);
-
 
437
 
-
 
438
                // 5) Differential measurement.
-
 
439
                gyroD[axis] = (gyroD[axis] * (GYROS_D_FILTER - 1) + (tempOffsetGyro
-
 
440
                                - gyro_PID[axis])) / GYROS_D_FILTER;
-
 
441
 
-
 
442
                // 6) Done.
-
 
443
                gyro_PID[axis] = tempOffsetGyro;
-
 
444
 
-
 
445
                /*
-
 
446
                 * Now process the data for attitude angles.
-
 
447
                 */
-
 
448
                tempGyro = rawGyroSum[axis];
-
 
449
 
-
 
450
                // 1) Apply sign and offset, scale before filtering.
-
 
451
                if (GYRO_REVERSED[axis]) {
-
 
452
                        tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
272
  } else {
453
                } else {
-
 
454
                        tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
-
 
455
                }
-
 
456
 
-
 
457
                // 2) Filter.
-
 
458
                gyro_ATT[axis] = (gyro_ATT[axis] * (GYROS_ATT_FILTER - 1) + tempOffsetGyro)
-
 
459
                                / GYROS_ATT_FILTER;
-
 
460
                break;
-
 
461
 
-
 
462
        case 17:
-
 
463
                // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v).
-
 
464
                // This is divided by 3 --> 10.34 counts per volt.
-
 
465
                UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4;
-
 
466
                DebugOut.Analog[11] = UBat;
-
 
467
                analogDataReady = 1; // mark
273
    state = 0;
468
                ADCycleCount++;
-
 
469
                // Stop the sampling. Cycle is over.
274
    ADCycleCount++;
470
                state = 0;
275
    analogDataReady = 1;
471
                for (i = 0; i < 8; i++) {
-
 
472
                        sensorInputs[i] = 0;
276
    // do not restart ADC converter. 
473
                }
-
 
474
                break;
-
 
475
        default: {
-
 
476
        } // do nothing.
277
  }
Line -... Line 278...
-
 
278
}
477
        }
279
 
-
 
280
void analog_updateGyros(void) {
-
 
281
  // for various filters...
-
 
282
  int16_t tempOffsetGyro, tempGyro;
478
 
283
 
479
        // set up for next state.
284
  for (uint8_t axis=0; axis<2; axis++) {
-
 
285
    tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH-axis];
-
 
286
    // DebugOut.Analog[6 + 3 * axis ] = tempGyro;
-
 
287
    /*
-
 
288
     * Process the gyro data for the PID controller.
-
 
289
     */
-
 
290
    // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a
-
 
291
    //    gyro with a wider range, and helps counter saturation at full control.
-
 
292
   
-
 
293
    if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER) {
-
 
294
      if (tempGyro < SENSOR_MIN_PITCHROLL) {
-
 
295
        DebugOut.Digital[0] |= DEBUG_SENSORLIMIT;
-
 
296
        tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT;
-
 
297
      } else if (tempGyro > SENSOR_MAX_PITCHROLL) {
-
 
298
        DebugOut.Digital[0] |= DEBUG_SENSORLIMIT;
-
 
299
        tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE
-
 
300
          + SENSOR_MAX_PITCHROLL;
-
 
301
      } else {
-
 
302
        DebugOut.Digital[0] &= ~DEBUG_SENSORLIMIT;
-
 
303
      }
-
 
304
    }
-
 
305
   
-
 
306
    // 2) Apply sign and offset, scale before filtering.
-
 
307
    if (GYRO_REVERSED[axis]) {
-
 
308
      tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
-
 
309
    } else {
-
 
310
      tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
-
 
311
    }
-
 
312
   
-
 
313
    // 3) Scale and filter.
-
 
314
    tempOffsetGyro = (gyro_PID[axis] * (GYROS_PID_FILTER - 1) + tempOffsetGyro) / GYROS_PID_FILTER;
-
 
315
   
480
        ad_channel = pgm_read_byte(&channelsForStates[state]);
316
    // 4) Measure noise.
481
        // ad_channel = channelsForStates[state];
317
    measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING);
-
 
318
   
-
 
319
    // 5) Differential measurement.
-
 
320
    gyroD[axis] = (gyroD[axis] * (GYROS_D_FILTER - 1) + (tempOffsetGyro - gyro_PID[axis])) / GYROS_D_FILTER;
482
 
321
   
-
 
322
    // 6) Done.
-
 
323
    gyro_PID[axis] = tempOffsetGyro;
483
        // set adc muxer to next ad_channel
324
   
-
 
325
    /*
-
 
326
     * Now process the data for attitude angles.
-
 
327
     */
-
 
328
    tempGyro = rawGyroSum[axis];
-
 
329
   
-
 
330
    // 1) Apply sign and offset, scale before filtering.
484
        ADMUX = (ADMUX & 0xE0) | ad_channel;
331
    if (GYRO_REVERSED[axis]) {
-
 
332
      tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL;
-
 
333
    } else {
-
 
334
      tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL;
485
        // after full cycle stop further interrupts
335
    }
-
 
336
   
-
 
337
    // 2) Filter.
-
 
338
    gyro_ATT[axis] = (gyro_ATT[axis] * (GYROS_ATT_FILTER - 1) + tempOffsetGyro) / GYROS_ATT_FILTER;
-
 
339
  }
-
 
340
 
-
 
341
  // Yaw gyro.
-
 
342
  rawGyroSum[YAW] = sensorInputs[AD_GYRO_YAW];
-
 
343
  if (GYRO_REVERSED[YAW])
-
 
344
    yawGyro = gyroOffset[YAW] - sensorInputs[AD_GYRO_YAW];
486
        if (state)
345
  else
Line 487... Line 346...
487
                analog_start();
346
    yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset[YAW];
-
 
347
}
-
 
348
 
488
}
349
void analog_updateAccelerometers(void) {
-
 
350
  // Pitch and roll axis accelerations.
-
 
351
  for (uint8_t axis=0; axis<2; axis++) {
-
 
352
    if (ACC_REVERSED[axis])
-
 
353
      acc[axis] = accOffset[axis] - sensorInputs[AD_ACC_PITCH-axis];
-
 
354
    else
-
 
355
      acc[axis] = sensorInputs[AD_ACC_PITCH-axis] - accOffset[axis];
-
 
356
   
-
 
357
    filteredAcc[axis] = (filteredAcc[axis] * (ACC_FILTER - 1) + acc[axis]) / ACC_FILTER;
-
 
358
   
-
 
359
    /*
-
 
360
      stronglyFilteredAcc[PITCH] =
-
 
361
      (stronglyFilteredAcc[PITCH] * 99 + acc[PITCH] * 10) / 100;
-
 
362
    */
-
 
363
   
489
 
364
    measureNoise(acc[axis], &accNoisePeak[axis], 1);
-
 
365
  }
490
void analog_calibrate(void) {
366
 
-
 
367
  // Z acc.
-
 
368
  if (ACC_REVERSED[Z])
-
 
369
    acc[Z] = accOffset[Z] - sensorInputs[AD_ACC_Z];
-
 
370
  else
-
 
371
    acc[Z] = sensorInputs[AD_ACC_Z] - accOffset[Z];
-
 
372
 
-
 
373
  /*
-
 
374
    stronglyFilteredAcc[Z] =
Line -... Line 375...
-
 
375
    (stronglyFilteredAcc[Z] * 99 + acc[Z] * 10) / 100;
491
#define GYRO_OFFSET_CYCLES 32
376
  */
-
 
377
}
-
 
378
 
-
 
379
void analog_updateAirPressure(void) {
492
        uint8_t i, axis;
380
  static uint16_t pressureAutorangingWait = 25;
493
        int32_t deltaOffsets[3] = { 0, 0, 0 };
381
  uint16_t rawAirPressure;
-
 
382
  int16_t newrange;
494
 
383
  // air pressure
495
        // Set the filters... to be removed again, once some good settings are found.
384
  if (pressureAutorangingWait) {
496
        GYROS_PID_FILTER = (dynamicParams.UserParams[4] & 0b00000011) + 1;
385
    //A range switch was done recently. Wait for steadying.
-
 
386
    pressureAutorangingWait--;
497
        GYROS_ATT_FILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1;
387
    DebugOut.Analog[27] = (uint16_t) OCR0A;
498
        GYROS_D_FILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1;
-
 
499
        ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1;
388
    DebugOut.Analog[31] = simpleAirPressure;
-
 
389
  } else {
500
 
390
    rawAirPressure = sensorInputs[AD_AIRPRESSURE];
-
 
391
    if (rawAirPressure < MIN_RAWPRESSURE) {
501
        gyro_calibrate();
392
      // value is too low, so decrease voltage on the op amp minus input, making the value higher.
502
 
393
      newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1;
503
        // determine gyro bias by averaging (requires that the copter does not rotate around any axis!)
394
      if (newrange > MIN_RANGES_EXTRAPOLATION) {
504
        for (i = 0; i < GYRO_OFFSET_CYCLES; i++) {
395
        pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 +
-
 
396
        OCR0A = newrange;
505
                delay_ms_Mess(20);
397
      } else {
506
                for (axis = PITCH; axis <= YAW; axis++) {
398
        if (OCR0A) {
507
                        deltaOffsets[axis] += rawGyroSum[axis];
399
          OCR0A--;
-
 
400
          pressureAutorangingWait = AUTORANGE_WAIT_FACTOR;
-
 
401
        }
-
 
402
      }
-
 
403
    } else if (rawAirPressure > MAX_RAWPRESSURE) {
508
                }
404
      // value is too high, so increase voltage on the op amp minus input, making the value lower.
-
 
405
      // If near the end, make a limited increase
-
 
406
      newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4;  // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1;
-
 
407
      if (newrange < MAX_RANGES_EXTRAPOLATION) {
-
 
408
        pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR;
509
        }
409
        OCR0A = newrange;
510
 
410
      } else {
-
 
411
        if (OCR0A < 254) {
-
 
412
          OCR0A++;
-
 
413
          pressureAutorangingWait = AUTORANGE_WAIT_FACTOR;
-
 
414
        }
-
 
415
      }
-
 
416
    }
-
 
417
   
-
 
418
    // Even if the sample is off-range, use it.
-
 
419
    simpleAirPressure = getSimplePressure(rawAirPressure);
-
 
420
    DebugOut.Analog[27] = (uint16_t) OCR0A;
-
 
421
    DebugOut.Analog[31] = simpleAirPressure;
-
 
422
   
-
 
423
    if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) {
-
 
424
      // Danger: pressure near lower end of range. If the measurement saturates, the
-
 
425
      // copter may climb uncontrolledly... Simulate a drastic reduction in pressure.
-
 
426
      DebugOut.Digital[1] |= DEBUG_SENSORLIMIT;
-
 
427
      airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth
-
 
428
        + (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION
-
 
429
           * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF;
-
 
430
    } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) {
-
 
431
      // Danger: pressure near upper end of range. If the measurement saturates, the
-
 
432
      // copter may descend uncontrolledly... Simulate a drastic increase in pressure.
-
 
433
      DebugOut.Digital[1] |= DEBUG_SENSORLIMIT;
-
 
434
      airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth
-
 
435
        + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION
-
 
436
           * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF;
-
 
437
    } else {
-
 
438
      // normal case.
-
 
439
      // If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample.
-
 
440
      // The 2 cases above (end of range) are ignored for this.
-
 
441
      DebugOut.Digital[1] &= ~DEBUG_SENSORLIMIT;
-
 
442
      if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR - 1)
-
 
443
        airPressureSum += simpleAirPressure / 2;
-
 
444
      else
-
 
445
        airPressureSum += simpleAirPressure;
-
 
446
    }
-
 
447
   
-
 
448
    // 2 samples were added.
-
 
449
    pressureMeasurementCount += 2;
-
 
450
    if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) {
-
 
451
      filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1)
-
 
452
                             + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER;
Line -... Line 453...
-
 
453
      pressureMeasurementCount = airPressureSum = 0;
-
 
454
    }
511
        for (axis = PITCH; axis <= YAW; axis++) {
455
  }
512
                gyroOffset[axis] = (deltaOffsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
456
}
-
 
457
 
-
 
458
void analog_updateBatteryVoltage(void) {
Line -... Line 459...
-
 
459
  // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v).
-
 
460
  // This is divided by 3 --> 10.34 counts per volt.
513
                // DebugOut.Analog[20 + axis] = gyroOffset[axis];
461
  UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4;
514
        }
462
  DebugOut.Analog[11] = UBat;
515
 
463
}
516
        // Noise is relativ to offset. So, reset noise measurements when changing offsets.
464
 
517
        gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0;
-
 
518
 
-
 
519
        accOffset[PITCH] = GetParamWord(PID_ACC_PITCH);
-
 
Line -... Line 465...
-
 
465
void analog_update(void) {
-
 
466
  analog_updateGyros();
-
 
467
  analog_updateAccelerometers();
-
 
468
  analog_updateAirPressure();
-
 
469
  analog_updateBatteryVoltage();
-
 
470
}
-
 
471
 
-
 
472
void analog_calibrate(void) {
-
 
473
#define GYRO_OFFSET_CYCLES 32
-
 
474
  uint8_t i, axis;
-
 
475
  int32_t deltaOffsets[3] = { 0, 0, 0 };
-
 
476
 
-
 
477
  // Set the filters... to be removed again, once some good settings are found.
-
 
478
  GYROS_PID_FILTER = (dynamicParams.UserParams[4] & 0b00000011) + 1;
-
 
479
  GYROS_ATT_FILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1;
-
 
480
  GYROS_D_FILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1;
-
 
481
  ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1;
-
 
482
 
-
 
483
  gyro_calibrate();
-
 
484
 
-
 
485
  // determine gyro bias by averaging (requires that the copter does not rotate around any axis!)
-
 
486
  for (i = 0; i < GYRO_OFFSET_CYCLES; i++) {
-
 
487
    delay_ms_Mess(20);
-
 
488
    for (axis = PITCH; axis <= YAW; axis++) {
-
 
489
      deltaOffsets[axis] += rawGyroSum[axis];
-
 
490
    }
-
 
491
  }
-
 
492
 
-
 
493
  for (axis = PITCH; axis <= YAW; axis++) {
-
 
494
    gyroOffset[axis] = (deltaOffsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES;
-
 
495
    // DebugOut.Analog[20 + axis] = gyroOffset[axis];
-
 
496
  }
-
 
497
 
-
 
498
  // Noise is relativ to offset. So, reset noise measurements when changing offsets.
-
 
499
  gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0;
-
 
500
 
-
 
501
  accOffset[PITCH] = GetParamWord(PID_ACC_PITCH);
520
        accOffset[ROLL] = GetParamWord(PID_ACC_ROLL);
502
  accOffset[ROLL] = GetParamWord(PID_ACC_ROLL);
521
        accOffset[Z] = GetParamWord(PID_ACC_Z);
503
  accOffset[Z] = GetParamWord(PID_ACC_Z);
Line 522... Line 504...
522
 
504
 
523
        // Rough estimate. Hmm no nothing happens at calibration anyway.
505
  // Rough estimate. Hmm no nothing happens at calibration anyway.
524
        // airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2);
506
  // airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2);