comparison Small_CPU/Src/pressure.c @ 984:41136649b90d GasConsumption

Improvment temperature measurent: In the previous verion no plausibility check for over temperature values was done. In addition the temperature display switches fast between e.g. 24 and 25 in case the value is 24.9 because of measurement tolerances. To avoid this a hysterese was added to the display function.
author Ideenmodellierer
date Sat, 29 Mar 2025 14:56:08 +0100 (3 weeks ago)
parents 06aaccaf2e02
children
comparison
equal deleted inserted replaced
983:7891160acde3 984:41136649b90d
50 /* #define SIMULATE_PRESSURE */ 50 /* #define SIMULATE_PRESSURE */
51 51
52 #define PRESSURE_SURFACE_MAX_MBAR (1060.0f) /* It is unlikely that pressure at surface is greater than this value => clip to it */ 52 #define PRESSURE_SURFACE_MAX_MBAR (1060.0f) /* It is unlikely that pressure at surface is greater than this value => clip to it */
53 53
54 #define PRESSURE_MINIMUM (0.0f) 54 #define PRESSURE_MINIMUM (0.0f)
55 #define TEMPERATURE_MINIMUM (-100.0f) 55 #define TEMPERATURE_MINIMUM (-40.0f)
56 #define TEMPERATURE_MAXIMUM (80.0f)
56 57
57 #define PRESSURE_SURFACE_QUE (30u) /* history buffer [minutes] for past pressure measurements */ 58 #define PRESSURE_SURFACE_QUE (30u) /* history buffer [minutes] for past pressure measurements */
58 #define PRESSURE_SURFACE_EVA_WINDOW (15u) /* Number of entries evaluated during instability test. Used to avoid detection while dive enters water */ 59 #define PRESSURE_SURFACE_EVA_WINDOW (15u) /* Number of entries evaluated during instability test. Used to avoid detection while dive enters water */
59 #define PRESSURE_SURFACE_STABLE_LIMIT (10u) /* Define pressure as stable if delta (mBar) is below this value */ 60 #define PRESSURE_SURFACE_STABLE_LIMIT (10u) /* Define pressure as stable if delta (mBar) is below this value */
60 #define PRESSURE_SURFACE_DETECT_STABLE_CNT (5u) /* Event count to detect stable condition */ 61 #define PRESSURE_SURFACE_DETECT_STABLE_CNT (5u) /* Event count to detect stable condition */
87 short C6plus100 = -1; 88 short C6plus100 = -1;
88 */ 89 */
89 static float pressure_offset = 0.0; /* Offset value which may be specified by the user via PC Software */ 90 static float pressure_offset = 0.0; /* Offset value which may be specified by the user via PC Software */
90 static float temperature_offset = 0.0; /* Offset value which may be specified by the user via PC Software */ 91 static float temperature_offset = 0.0; /* Offset value which may be specified by the user via PC Software */
91 92
92 static float ambient_temperature = 0; 93 static float ambient_temperature = 20.0;
93 static float ambient_pressure_mbar = 1000.0; 94 static float ambient_pressure_mbar = 1000.0;
94 static float surface_pressure_mbar = 1000.0; 95 static float surface_pressure_mbar = 1000.0;
95 static float surface_ring_mbar[PRESSURE_SURFACE_QUE] = { 0 }; 96 static float surface_ring_mbar[PRESSURE_SURFACE_QUE] = { 0 };
96 97
97 static uint8_t surface_pressure_writeIndex = 0; 98 static uint8_t surface_pressure_writeIndex = 0;
636 #endif 637 #endif
637 } 638 }
638 639
639 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void) 640 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void)
640 { 641 {
641 static float runningAvg = 0; 642 static float runningAvgPressure = 0;
642 static uint8_t avgCnt = 0; 643 static uint8_t avgCntPressure = 0;
644 static float runningAvgTemp = 0;
645 static uint8_t avgCntTemp = 0;
646
647 float newTemperature = 0.0;
643 648
644 uint32_t local_D1; // ADC value of the pressure conversion 649 uint32_t local_D1; // ADC value of the pressure conversion
645 uint32_t local_D2; // ADC value of the temperature conversion 650 uint32_t local_D2; // ADC value of the temperature conversion
646 int32_t local_Px10; // compensated pressure value 651 int32_t local_Px10; // compensated pressure value
647 int32_t local_Tx100; // compensated temperature value 652 int32_t local_Tx100; // compensated temperature value
701 706
702 local_Px10 = (int32_t)( 707 local_Px10 = (int32_t)(
703 (((int64_t)((local_D1 * local_SENS) / 2097152)) - local_OFF) 708 (((int64_t)((local_D1 * local_SENS) / 2097152)) - local_OFF)
704 / 8192 );// )) / 10; // pow(2,21), pow(2,13) 709 / 8192 );// )) / 10; // pow(2,21), pow(2,13)
705 710
706 ambient_temperature = ((float)local_Tx100) / 100; 711 newTemperature = ((float)local_Tx100) / 100;
707 ambient_temperature += temperature_offset; 712 newTemperature += temperature_offset;
708 713
709 if(ambient_temperature < TEMPERATURE_MINIMUM) 714 if((newTemperature < TEMPERATURE_MINIMUM) || (newTemperature > TEMPERATURE_MAXIMUM))
710 { 715 {
711 ambient_temperature = 20.0; 716 ambient_temperature = 20.0;
712 } 717 }
718
719 runningAvgTemp = (avgCntTemp * runningAvgTemp + newTemperature) / (avgCntTemp + 1);
720 if (avgCntTemp < 10)
721 {
722 avgCntTemp++;
723 }
724 ambient_temperature = runningAvgTemp;
725
713 726
714 calc_pressure = ((float)local_Px10) / 10; 727 calc_pressure = ((float)local_Px10) / 10;
715 calc_pressure += pressure_offset; 728 calc_pressure += pressure_offset;
716 729
717 runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 1); 730 runningAvgPressure = (avgCntPressure * runningAvgPressure + calc_pressure) / (avgCntPressure + 1);
718 if (avgCnt < 10) /* build an average considering the last measurements to have a weight "1 of 10" */ 731 if (avgCntPressure < 10) /* build an average considering the last measurements to have a weight "1 of 10" */
719 { /* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */ 732 { /* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */
720 avgCnt++; /* by the measurement range of the sensor which is focused on under water pressure measurement */ 733 avgCntPressure++; /* by the measurement range of the sensor which is focused on under water pressure measurement */
721 } 734 }
722 ambient_pressure_mbar = runningAvg; 735 ambient_pressure_mbar = runningAvgPressure;
723 736
724 if(ambient_pressure_mbar < PRESSURE_MINIMUM) 737 if(ambient_pressure_mbar < PRESSURE_MINIMUM)
725 { 738 {
726 ambient_pressure_mbar = 1000.0 + pressure_offset; 739 ambient_pressure_mbar = 1000.0 + pressure_offset;
727 } 740 }