Subversion Repositories FlightCtrl

Rev

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

Rev 2020 Rev 2026
Line 171... Line 171...
171
uint16_t simpleAirPressure;
171
uint16_t simpleAirPressure;
Line 172... Line 172...
172
 
172
 
173
// Value of AIRPRESSURE_OVERSAMPLING samples, with range, filtered.
173
// Value of AIRPRESSURE_OVERSAMPLING samples, with range, filtered.
Line -... Line 174...
-
 
174
int32_t filteredAirPressure;
-
 
175
 
-
 
176
#define MAX_AIRPRESSURE_WINDOW_LENGTH 64
-
 
177
int16_t airPressureWindow[MAX_AIRPRESSURE_WINDOW_LENGTH];
-
 
178
int32_t windowedAirPressure;
174
int32_t filteredAirPressure;
179
uint8_t windowPtr;
175
 
180
 
Line 176... Line 181...
176
// Partial sum of AIRPRESSURE_SUMMATION_FACTOR samples.
181
// Partial sum of AIRPRESSURE_SUMMATION_FACTOR samples.
177
int32_t airPressureSum;
182
int32_t airPressureSum;
Line 262... Line 267...
262
        ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
267
        ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
263
        //Set ADC Control and Status Register B
268
        //Set ADC Control and Status Register B
264
        //Trigger Source to Free Running Mode
269
        //Trigger Source to Free Running Mode
265
        ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0));
270
        ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0));
Line -... Line 271...
-
 
271
 
-
 
272
        for (uint8_t i=0; i<MAX_AIRPRESSURE_WINDOW_LENGTH; i++) {
-
 
273
          airPressureWindow[i] = 0;
-
 
274
        }
-
 
275
 
-
 
276
        windowedAirPressure = 0;
266
 
277
 
Line 267... Line 278...
267
        startAnalogConversionCycle();
278
        startAnalogConversionCycle();
268
 
279
 
269
        // restore global interrupt flags
280
        // restore global interrupt flags
Line 294... Line 305...
294
/*
305
/*
295
 * Min.: 0
306
 * Min.: 0
296
 * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type.
307
 * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type.
297
 */
308
 */
298
uint16_t getSimplePressure(int advalue) {
309
uint16_t getSimplePressure(int advalue) {
299
        return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue;
310
        uint16_t result = (uint16_t) OCR0A * (uint16_t) rangewidth + advalue;
-
 
311
        result += (acc[Z] * (staticParams.airpressureAccZCorrection-128)) >> 10;
-
 
312
        return result;
300
}
313
}
Line 301... Line 314...
301
 
314
 
302
void startAnalogConversionCycle(void) {
315
void startAnalogConversionCycle(void) {
Line 432... Line 445...
432
  int16_t newrange;
445
  int16_t newrange;
433
  // air pressure
446
  // air pressure
434
  if (pressureAutorangingWait) {
447
  if (pressureAutorangingWait) {
435
    //A range switch was done recently. Wait for steadying.
448
    //A range switch was done recently. Wait for steadying.
436
    pressureAutorangingWait--;
449
    pressureAutorangingWait--;
437
    debugOut.analog[27] = (uint16_t) OCR0A;
-
 
438
    debugOut.analog[31] = simpleAirPressure;
-
 
439
  } else {
450
  } else {
440
    rawAirPressure = sensorInputs[AD_AIRPRESSURE];
451
    rawAirPressure = sensorInputs[AD_AIRPRESSURE];
441
    if (rawAirPressure < MIN_RAWPRESSURE) {
452
    if (rawAirPressure < MIN_RAWPRESSURE) {
442
      // value is too low, so decrease voltage on the op amp minus input, making the value higher.
453
      // value is too low, so decrease voltage on the op amp minus input, making the value higher.
443
      newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1;
454
      newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1;
Line 465... Line 476...
465
      }
476
      }
466
    }
477
    }
Line 467... Line 478...
467
   
478
   
468
    // Even if the sample is off-range, use it.
479
    // Even if the sample is off-range, use it.
469
    simpleAirPressure = getSimplePressure(rawAirPressure);
-
 
470
    debugOut.analog[27] = (uint16_t) OCR0A;
-
 
Line 471... Line 480...
471
    debugOut.analog[31] = simpleAirPressure;
480
    simpleAirPressure = getSimplePressure(rawAirPressure);
472
   
481
   
473
    if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) {
482
    if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) {
474
      // Danger: pressure near lower end of range. If the measurement saturates, the
483
      // Danger: pressure near lower end of range. If the measurement saturates, the
Line 484... Line 493...
484
      airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth
493
      airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth
485
        + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION
494
        + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION
486
           * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF;
495
           * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF;
487
    } else {
496
    } else {
488
      // normal case.
497
      // normal case.
489
      // If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample.
498
      // If AIRPRESSURE_OVERSAMPLING is an odd number we only want to add half the double sample.
490
      // The 2 cases above (end of range) are ignored for this.
499
      // The 2 cases above (end of range) are ignored for this.
491
      debugOut.digital[1] &= ~DEBUG_SENSORLIMIT;
500
      debugOut.digital[1] &= ~DEBUG_SENSORLIMIT;
492
      if (pressureMeasurementCount == AIRPRESSURE_OVERSAMPLING - 1)
501
      if (pressureMeasurementCount == AIRPRESSURE_OVERSAMPLING - 1)
493
        airPressureSum += simpleAirPressure / 2;
502
        airPressureSum += simpleAirPressure / 2;
494
      else
503
      else
Line 500... Line 509...
500
    if (pressureMeasurementCount >= AIRPRESSURE_OVERSAMPLING) {
509
    if (pressureMeasurementCount >= AIRPRESSURE_OVERSAMPLING) {
501
      filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1)
510
      filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1)
502
                             + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER;
511
                             + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER;
503
      pressureMeasurementCount = airPressureSum = 0;
512
      pressureMeasurementCount = airPressureSum = 0;
504
    }
513
    }
-
 
514
    //int16_t airPressureWindow[MAX_AIRPRESSURE_WINDOW_LENGTH];
-
 
515
    //int32_t windowedAirPressure = 0;
-
 
516
    //uint8_t windowPtr;
-
 
517
    windowedAirPressure += simpleAirPressure;
-
 
518
    windowedAirPressure -= airPressureWindow[windowPtr];
-
 
519
    airPressureWindow[windowPtr] = simpleAirPressure;
-
 
520
    windowPtr = (windowPtr+1) % MAX_AIRPRESSURE_WINDOW_LENGTH;
505
  }
521
  }
506
}
522
}
Line 507... Line 523...
507
 
523
 
508
void analog_updateBatteryVoltage(void) {
524
void analog_updateBatteryVoltage(void) {