comparison Small_CPU/Src/pressure.c @ 480:3fe9cc747c5c Improve_Button_Sleep

Code cleanup pressure plausibility check: The function was deactivated (always returning true) some time ago => removed useless code
author ideenmodellierer
date Wed, 13 May 2020 21:56:35 +0200
parents 4b9427ae0a65
children 9eeab3fead8f
comparison
equal deleted inserted replaced
479:4b9427ae0a65 480:3fe9cc747c5c
48 48
49 /* remove comment to use a predefined profile for pressure changes instead of real world data */ 49 /* remove comment to use a predefined profile for pressure changes instead of real world data */
50 /* #define SIMULATE_PRESSURE */ 50 /* #define SIMULATE_PRESSURE */
51 51
52 #define PRESSURE_SURFACE_MAX_MBAR (1030.0f) /* It is unlikely that pressure at surface is greater than this value => clip to it */ 52 #define PRESSURE_SURFACE_MAX_MBAR (1030.0f) /* It is unlikely that pressure at surface is greater than this value => clip to it */
53 #define PRESSURE_HISTORY_SIZE (8u)
54 53
55 #define PRESSURE_SURFACE_QUE (30u) /* history buffer [minutes] for past pressure measurements */ 54 #define PRESSURE_SURFACE_QUE (30u) /* history buffer [minutes] for past pressure measurements */
56 #define PRESSURE_SURFACE_EVA_WINDOW (15u) /* Number of entries evaluated during instability test. Used to avoid detection while dive enters water */ 55 #define PRESSURE_SURFACE_EVA_WINDOW (15u) /* Number of entries evaluated during instability test. Used to avoid detection while dive enters water */
57 #define PRESSURE_SURFACE_STABLE_LIMIT (10u) /* Define pressure as stable if delta (mBar) is below this value */ 56 #define PRESSURE_SURFACE_STABLE_LIMIT (10u) /* Define pressure as stable if delta (mBar) is below this value */
58 #define PRESSURE_SURFACE_DETECT_STABLE_CNT (5u) /* Event count to detect stable condition */ 57 #define PRESSURE_SURFACE_DETECT_STABLE_CNT (5u) /* Event count to detect stable condition */
98 97
99 static uint8_t surface_pressure_writeIndex = 0; 98 static uint8_t surface_pressure_writeIndex = 0;
100 static float surface_pressure_stable_value = 0; 99 static float surface_pressure_stable_value = 0;
101 static uint8_t surface_pressure_stable = 0; 100 static uint8_t surface_pressure_stable = 0;
102 101
103 static float pressure_history_mbar[PRESSURE_HISTORY_SIZE];
104
105 static uint8_t secondCounterSurfaceRing = 0; 102 static uint8_t secondCounterSurfaceRing = 0;
106 static uint8_t avgCount = 0; 103 static uint8_t avgCount = 0;
107 static float runningAvg = 0; 104 static float runningAvg = 0;
108 105
109 float get_temperature(void) 106 float get_temperature(void)
132 129
133 for(int i=0; i<PRESSURE_SURFACE_QUE; i++) 130 for(int i=0; i<PRESSURE_SURFACE_QUE; i++)
134 surface_ring_mbar[i] = ambient_pressure_mbar; 131 surface_ring_mbar[i] = ambient_pressure_mbar;
135 surface_pressure_mbar = ambient_pressure_mbar; 132 surface_pressure_mbar = ambient_pressure_mbar;
136 surface_pressure_writeIndex = 0; /* index of the oldest value in the ring buffer */ 133 surface_pressure_writeIndex = 0; /* index of the oldest value in the ring buffer */
137 }
138 }
139
140 void init_pressure_history(void)
141 {
142 for(int i=0; i<PRESSURE_HISTORY_SIZE; i++)
143 {
144 pressure_history_mbar[i] = 1000.0;
145 } 134 }
146 } 135 }
147 136
148 uint8_t is_surface_pressure_stable(void) 137 uint8_t is_surface_pressure_stable(void)
149 { 138 {
339 uint8_t buffer[1]; 328 uint8_t buffer[1];
340 buffer[0] = 0x1E; // Reset Command 329 buffer[0] = 0x1E; // Reset Command
341 uint8_t retValue = 0xFF; 330 uint8_t retValue = 0xFF;
342 331
343 pressureSensorInitSuccess = false; 332 pressureSensorInitSuccess = false;
344 init_pressure_history();
345 333
346 /* Probe new sensor first */ 334 /* Probe new sensor first */
347 retValue = I2C_Master_Transmit( DEVICE_PRESSURE_MS5837, buffer, 1); 335 retValue = I2C_Master_Transmit( DEVICE_PRESSURE_MS5837, buffer, 1);
348 if(retValue != HAL_OK) 336 if(retValue != HAL_OK)
349 { 337 {
615 #else 603 #else
616 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(); 604 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015();
617 #endif 605 #endif
618 } 606 }
619 607
620 static uint8_t pressure_plausible(float pressurevalue)
621 {
622 static uint8_t pressurewriteindex = 0;
623 uint8_t retval = 0;
624 uint8_t index;
625 float pressure_average = 0;
626
627 for(index = 0; index < PRESSURE_HISTORY_SIZE; index++)
628 {
629 pressure_average += pressure_history_mbar[index];
630 }
631 pressure_average /= PRESSURE_HISTORY_SIZE;
632 if(pressure_average == 1000.0) /* first pressure calculation */
633 {
634 if(fabs(pressurevalue - pressure_average) < 11000.0) /* just in case a reset occur during dive assume value equal < 100m as valid */
635 {
636 for(index = 0; index < PRESSURE_HISTORY_SIZE; index++)
637 {
638 pressure_history_mbar[index] = pressurevalue; /* set history to current value */
639 retval = 1;
640 }
641 }
642 }
643 else
644 {
645 pressure_history_mbar[pressurewriteindex++] = pressurevalue;
646 pressurewriteindex &= 0x7; /* wrap around if necessary */
647 retval = 1;
648 }
649
650 return retval;
651 }
652
653 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void) 608 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void)
654 { 609 {
655 static float runningAvg = 0; 610 static float runningAvg = 0;
656 static uint8_t avgCnt = 0; 611 static uint8_t avgCnt = 0;
657 612
721 ambient_temperature += temperature_offset; 676 ambient_temperature += temperature_offset;
722 677
723 calc_pressure = ((float)local_Px10) / 10; 678 calc_pressure = ((float)local_Px10) / 10;
724 calc_pressure += pressure_offset; 679 calc_pressure += pressure_offset;
725 680
726 if(pressure_plausible(calc_pressure)) 681 runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 1);
727 { 682 if (avgCnt < 10) /* build an average considering the last measurements to have a weight "1 of 10" */
728 runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 1); 683 { /* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */
729 if (avgCnt < 10) /* build an average considering the last measurements to have a weight "1 of 10" */ 684 avgCnt++; /* by the measurement range of the sensor which is focused on under water pressure measurement */
730 { /* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */ 685 }
731 avgCnt++; /* by the measurement range of the sensor which is focused on under water pressure measurement */ 686 ambient_pressure_mbar = runningAvg;
732 }
733 ambient_pressure_mbar = runningAvg;
734 }
735 } 687 }
736 688
737 689
738 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011 690 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011
739 * short and int are both 16bit according to AVR/GCC google results 691 * short and int are both 16bit according to AVR/GCC google results