Rev 1775 | Rev 1821 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1775 | Rev 1796 | ||
---|---|---|---|
Line 62... | Line 62... | ||
62 | #include "uart0.h" |
62 | #include "uart0.h" |
Line 63... | Line 63... | ||
63 | 63 | ||
64 | // For reading and writing acc. meter offsets. |
64 | // For reading and writing acc. meter offsets. |
Line -... | Line 65... | ||
- | 65 | #include "eeprom.h" |
|
- | 66 | ||
- | 67 | // For DebugOut.Digital |
|
65 | #include "eeprom.h" |
68 | #include "output.h" |
66 | 69 | ||
67 | /* |
70 | /* |
68 | * For each A/D conversion cycle, each analog channel is sampled a number of times |
71 | * For each A/D conversion cycle, each analog channel is sampled a number of times |
69 | * (see array channelsForStates), and the results for each channel are summed. |
72 | * (see array channelsForStates), and the results for each channel are summed. |
Line 239... | Line 242... | ||
239 | } else { |
242 | } else { |
240 | *noiseMeasurement = 0; |
243 | *noiseMeasurement = 0; |
241 | } |
244 | } |
242 | } |
245 | } |
Line -... | Line 246... | ||
- | 246 | ||
- | 247 | /* |
|
- | 248 | * Min.: 0 |
|
- | 249 | * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type. |
|
243 | 250 | */ |
|
244 | uint16_t getSimplePressure(int advalue) { |
251 | uint16_t getSimplePressure(int advalue) { |
245 | return (uint16_t)OCR0A * (uint16_t)rangewidth + advalue; |
252 | return (uint16_t)OCR0A * (uint16_t)rangewidth + advalue; |
Line 246... | Line 253... | ||
246 | } |
253 | } |
Line 306... | Line 313... | ||
306 | 313 | ||
307 | case 14: // air pressure |
314 | case 14: // air pressure |
308 | if (pressureAutorangingWait) { |
315 | if (pressureAutorangingWait) { |
309 | //A range switch was done recently. Wait for steadying. |
316 | //A range switch was done recently. Wait for steadying. |
- | 317 | pressureAutorangingWait--; |
|
- | 318 | DebugOut.Analog[27] = (uint16_t)OCR0A; |
|
310 | pressureAutorangingWait--; |
319 | DebugOut.Analog[31] = simpleAirPressure; |
311 | break; |
320 | break; |
- | 321 | } |
|
312 | } |
322 | |
313 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
323 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
314 | if (rawAirPressure < MIN_RAWPRESSURE) { |
324 | if (rawAirPressure < MIN_RAWPRESSURE) { |
315 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
325 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
316 | newrange = OCR0A - (MAX_RAWPRESSURE - rawAirPressure) / rangewidth - 1; |
326 | newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
317 | if (newrange > MIN_RANGES_EXTRAPOLATION) { |
327 | if (newrange > MIN_RANGES_EXTRAPOLATION) { |
318 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; |
328 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
319 | OCR0A = newrange; |
329 | OCR0A = newrange; |
320 | } else { |
330 | } else { |
321 | if (OCR0A) { |
331 | if (OCR0A) { |
322 | OCR0A--; |
332 | OCR0A--; |
323 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
333 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
324 | } |
334 | } |
325 | } |
335 | } |
326 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
336 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
327 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
337 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
328 | // If near the end, make a limited increase |
338 | // If near the end, make a limited increase |
329 | newrange = OCR0A + (rawAirPressure - MIN_RAWPRESSURE) / rangewidth - 1; |
339 | newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
330 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
340 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
331 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
341 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
332 | OCR0A = newrange; |
342 | OCR0A = newrange; |
333 | } else { |
343 | } else { |
Line 338... | Line 348... | ||
338 | } |
348 | } |
339 | } |
349 | } |
Line 340... | Line 350... | ||
340 | 350 | ||
341 | // Even if the sample is off-range, use it. |
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; |
|
342 | simpleAirPressure = getSimplePressure(rawAirPressure); |
355 | |
343 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
356 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
344 | // Danger: pressure near lower end of range. If the measurement saturates, the |
357 | // Danger: pressure near lower end of range. If the measurement saturates, the |
345 | // copter may climb uncontrolled... Simulate a drastic reduction in pressure. |
358 | // copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
346 | airPressureSum += (int16_t)MIN_RANGES_EXTRAPOLATION * rangewidth + (simpleAirPressure - (int32_t)MIN_RANGES_EXTRAPOLATION * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
359 | airPressureSum += (int16_t)MIN_RANGES_EXTRAPOLATION * rangewidth + (simpleAirPressure - (int16_t)MIN_RANGES_EXTRAPOLATION * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
347 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
360 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
348 | // Danger: pressure near upper end of range. If the measurement saturates, the |
361 | // Danger: pressure near upper end of range. If the measurement saturates, the |
349 | // copter may fall uncontrolled... Simulate a drastic increase in pressure. |
362 | // copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
350 | airPressureSum += (int16_t)MAX_RANGES_EXTRAPOLATION * rangewidth + (simpleAirPressure - (int32_t)MAX_RANGES_EXTRAPOLATION * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
363 | airPressureSum += (int16_t)MAX_RANGES_EXTRAPOLATION * rangewidth + (simpleAirPressure - (int16_t)MAX_RANGES_EXTRAPOLATION * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
351 | } else { |
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; |
|
352 | // normal case. |
370 | else |
353 | airPressureSum += simpleAirPressure; |
371 | airPressureSum += simpleAirPressure; |
354 | } |
372 | } |
355 | 373 | ||
356 | // 2 samples were added. |
374 | // 2 samples were added. |
357 | pressureMeasurementCount += 2; |
375 | pressureMeasurementCount += 2; |
358 | if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR) { |
376 | if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) { |
359 | filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER-1) + airPressureSum + AIRPRESSURE_FILTER/2) / AIRPRESSURE_FILTER; |
377 | filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER-1) + airPressureSum + AIRPRESSURE_FILTER/2) / AIRPRESSURE_FILTER; |
360 | pressureMeasurementCount = airPressureSum = 0; |
378 | pressureMeasurementCount = airPressureSum = 0; |
Line 361... | Line -... | ||
361 | } |
- | |
362 | - | ||
363 | // DebugOut.Analog[14] = OCR0A; |
- | |
364 | // DebugOut.Analog[15] = simpleAirPressure; |
- | |
365 | DebugOut.Analog[11] = UBat; |
379 | } |
Line 366... | Line 380... | ||
366 | DebugOut.Analog[27] = acc[Z]; |
380 | |
367 | break; |
381 | break; |
368 | 382 | ||
Line 423... | Line 437... | ||
423 | 437 | ||
424 | case 17: |
438 | case 17: |
425 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
439 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
426 | // This is divided by 3 --> 10.34 counts per volt. |
440 | // This is divided by 3 --> 10.34 counts per volt. |
- | 441 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
|
427 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
442 | DebugOut.Analog[11] = UBat; |
428 | analogDataReady = 1; // mark |
443 | analogDataReady = 1; // mark |
429 | ADCycleCount++; |
444 | ADCycleCount++; |
430 | // Stop the sampling. Cycle is over. |
445 | // Stop the sampling. Cycle is over. |
431 | state = 0; |
446 | state = 0; |
Line 481... | Line 496... | ||
481 | 496 | ||
482 | // Rough estimate. Hmm no nothing happens at calibration anyway. |
497 | // Rough estimate. Hmm no nothing happens at calibration anyway. |
483 | // airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2); |
498 | // airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2); |
Line 484... | Line -... | ||
484 | // pressureMeasurementCount = 0; |
- | |
485 | - | ||
486 | // Experiment! |
- | |
487 | // filteredAirPressureOffset = filteredAirPressure - 1000L; |
499 | // pressureMeasurementCount = 0; |
488 | 500 | ||
Line 489... | Line 501... | ||
489 | Delay_ms_Mess(100); |
501 | Delay_ms_Mess(100); |
490 | } |
502 | } |
Line 528... | Line 540... | ||
528 | // Therefore run measurement for 100ms to achive stable readings |
540 | // Therefore run measurement for 100ms to achive stable readings |
529 | Delay_ms_Mess(100); |
541 | Delay_ms_Mess(100); |
Line 530... | Line 542... | ||
530 | 542 | ||
531 | // Set the feedback so that air pressure ends up in the middle of the range. |
543 | // Set the feedback so that air pressure ends up in the middle of the range. |
532 | // (raw pressure high --> OCR0A also high...) |
- | |
533 | // OCR0A += (rawAirPressure - 512) / rangewidth; |
- | |
534 | // Delay_ms_Mess(500); |
- | |
535 | 544 | // (raw pressure high --> OCR0A also high...) |
|
536 | /* |
545 | /* |
537 | pressureDiff = 0; |
546 | OCR0A += ((rawAirPressure - 1024) / rangewidth) - 1; |
Line -... | Line 547... | ||
- | 547 | Delay_ms_Mess(1000); |
|
- | 548 | ||
- | 549 | pressureDiff = 0; |
|
538 | // DebugOut.Analog[16] = rawAirPressure; |
550 | // DebugOut.Analog[16] = rawAirPressure; |
539 | 551 | |
|
540 | #define PRESSURE_CAL_CYCLE_COUNT 2 |
552 | #define PRESSURE_CAL_CYCLE_COUNT 5 |
541 | for (i=0; i<PRESSURE_CAL_CYCLE_COUNT; i++) { |
553 | for (i=0; i<PRESSURE_CAL_CYCLE_COUNT; i++) { |
542 | savedRawAirPressure = rawAirPressure; |
554 | savedRawAirPressure = rawAirPressure; |
543 | OCR0A++; |
555 | OCR0A+=2; |
544 | Delay_ms_Mess(200); |
556 | Delay_ms_Mess(500); |
545 | // raw pressure will decrease. |
- | |
546 | pressureDiff += (savedRawAirPressure - rawAirPressure); |
557 | // raw pressure will decrease. |
547 | 558 | pressureDiff += (savedRawAirPressure - rawAirPressure); |
|
548 | savedRawAirPressure = rawAirPressure; |
559 | savedRawAirPressure = rawAirPressure; |
549 | OCR0A--; |
560 | OCR0A-=2; |
550 | Delay_ms_Mess(200); |
561 | Delay_ms_Mess(500); |
551 | // raw pressure will increase. |
562 | // raw pressure will increase. |
552 | pressureDiff += (rawAirPressure - savedRawAirPressure); |
563 | pressureDiff += (rawAirPressure - savedRawAirPressure); |
553 | } |
- | |
554 | 564 | } |
|
- | 565 | |
|
555 | // DebugOut.Analog[16] = |
566 | rangewidth = (pressureDiff + PRESSURE_CAL_CYCLE_COUNT * 2 * 2 - 1) / (PRESSURE_CAL_CYCLE_COUNT * 2 * 2); |
556 | rangewidth = (pressureDiff + PRESSURE_CAL_CYCLE_COUNT * 2 - 1) / (PRESSURE_CAL_CYCLE_COUNT * 2); |
567 | DebugOut.Analog[27] = rangewidth; |