comparison Small_CPU/Src/pressure.c @ 339:37f45300bc2e PressureMeasure_Improvment

Apply averaging to pressure measurement: In pre versions calculated pressure value jittered +/-10hPa. Since we measure the pressure several time a second but only use one value a second, calc average including not used values Activated pressure / temperature offsets: The functionality to store offsets was already present in the firmware but values have not been apllied in the RTE => added functionality to include offsets in calculation Set Max possible surface pressure to 1070hPa (Black sea level)
author ideenmodellierer
date Sat, 17 Aug 2019 19:03:47 +0200
parents c11ce8c885d3
children 591c03a1e68d 4093ac18b25c
comparison
equal deleted inserted replaced
338:b6a59e93cc91 339:37f45300bc2e
44 #define CMD_ADC_1024 0x04 // ADC OSR=1024 44 #define CMD_ADC_1024 0x04 // ADC OSR=1024
45 #define CMD_ADC_2048 0x06 // ADC OSR=2056 45 #define CMD_ADC_2048 0x06 // ADC OSR=2056
46 #define CMD_ADC_4096 0x08 // ADC OSR=4096 46 #define CMD_ADC_4096 0x08 // ADC OSR=4096
47 #define CMD_PROM_RD 0xA0 // Prom read command 47 #define CMD_PROM_RD 0xA0 // Prom read command
48 48
49 #define PRESSURE_SURFACE_MAX_MBAR (1070.0f) /* It is very unlikely that pressure at surface is greater than this value => clip to it */
49 #define PRESSURE_HISTORY_SIZE (8u) 50 #define PRESSURE_HISTORY_SIZE (8u)
50 #define PRESSURE_JUMP_VALID_MBAR (500.0f) /* values are measure several times a second => jumps > 5m very unlikely */ 51 #define PRESSURE_JUMP_VALID_MBAR (500.0f) /* values are measure several times a second => jumps > 5m very unlikely */
51 52
52 #define PRESSURE_SURFACE_QUE (30u) /* history buffer [minutes] for past pressure measurements */ 53 #define PRESSURE_SURFACE_QUE (30u) /* history buffer [minutes] for past pressure measurements */
53 54
75 short C3plus200 = -1; 76 short C3plus200 = -1;
76 short C4minus250 = -1; 77 short C4minus250 = -1;
77 short UT1 = -1; 78 short UT1 = -1;
78 short C6plus100 = -1; 79 short C6plus100 = -1;
79 */ 80 */
81 static float pressure_offset = 0.0; /* Offset value which may be specified by the user via PC Software */
82 static float temperature_offset = 0.0; /* Offset value which may be specified by the user via PC Software */
80 83
81 static float ambient_temperature = 0; 84 static float ambient_temperature = 0;
82 static float ambient_pressure_mbar = 1000.0; 85 static float ambient_pressure_mbar = 1000.0;
83 static float surface_pressure_mbar = 1000.0; 86 static float surface_pressure_mbar = 1000.0;
84 static float surface_ring_mbar[PRESSURE_SURFACE_QUE] = { 0 }; 87 static float surface_ring_mbar[PRESSURE_SURFACE_QUE] = { 0 };
85 88
86 static float pressure_history_mbar[PRESSURE_HISTORY_SIZE]; 89 static float pressure_history_mbar[PRESSURE_HISTORY_SIZE];
87 90
88 uint8_t secondCounterSurfaceRing = 0; 91 static uint8_t secondCounterSurfaceRing = 0;
92 static uint8_t avgCount = 0;
93 static float runningAvg = 0;
89 94
90 float get_temperature(void) 95 float get_temperature(void)
91 { 96 {
92 return ambient_temperature; 97 return ambient_temperature;
93 } 98 }
101 { 106 {
102 return surface_pressure_mbar; 107 return surface_pressure_mbar;
103 } 108 }
104 109
105 110
106 void init_surface_ring(void) 111 void init_surface_ring(uint8_t force)
107 { 112 {
108 if(surface_ring_mbar[0] == 0) /* only initialize once. Keep value in place in case of an i2c recovery */ 113 if((surface_ring_mbar[0] == 0) || (force)) /* only initialize once. Keep value in place in case of an i2c recovery */
109 { 114 {
115 secondCounterSurfaceRing = 0; /* restart calculation */
116 avgCount = 0;
117 runningAvg = 0;
118
110 for(int i=0; i<PRESSURE_SURFACE_QUE; i++) 119 for(int i=0; i<PRESSURE_SURFACE_QUE; i++)
111 surface_ring_mbar[i] = ambient_pressure_mbar; 120 surface_ring_mbar[i] = ambient_pressure_mbar;
112 surface_pressure_mbar = ambient_pressure_mbar; 121 surface_pressure_mbar = ambient_pressure_mbar;
113 } 122 }
114 } 123 }
123 132
124 133
125 void update_surface_pressure(uint8_t call_rhythm_seconds) 134 void update_surface_pressure(uint8_t call_rhythm_seconds)
126 { 135 {
127 static uint8_t writeIndex = 0; /* Reinitialization will reset all entries to the same value => no need to reinit write index */ 136 static uint8_t writeIndex = 0; /* Reinitialization will reset all entries to the same value => no need to reinit write index */
128 static uint8_t avgCount = 0; 137
129 static float runningAvg = 0;
130 138
131 if(is_init_pressure_done()) 139 if(is_init_pressure_done())
132 { 140 {
133 runningAvg = (runningAvg * avgCount + ambient_pressure_mbar) / (avgCount +1); 141 runningAvg = (runningAvg * avgCount + ambient_pressure_mbar) / (avgCount +1);
134 avgCount++; 142 avgCount++;
135 secondCounterSurfaceRing += call_rhythm_seconds; 143 secondCounterSurfaceRing += call_rhythm_seconds;
136 144
137 if(secondCounterSurfaceRing >= 60) 145 if(secondCounterSurfaceRing >= 60)
138 { 146 {
139 surface_ring_mbar[writeIndex] = runningAvg; 147 if(runningAvg < PRESSURE_SURFACE_MAX_MBAR)
148 {
149 surface_ring_mbar[writeIndex] = runningAvg;
150 }
151 else
152 {
153 surface_ring_mbar[writeIndex] = PRESSURE_SURFACE_MAX_MBAR;
154 }
140 writeIndex++; /* the write index is now pointing to the oldest value in the buffer which will be overwritten next time */ 155 writeIndex++; /* the write index is now pointing to the oldest value in the buffer which will be overwritten next time */
141 156
142 if(writeIndex == PRESSURE_SURFACE_QUE) 157 if(writeIndex == PRESSURE_SURFACE_QUE)
143 { 158 {
144 writeIndex = 0; 159 writeIndex = 0;
430 pressure_average += pressure_history_mbar[index]; 445 pressure_average += pressure_history_mbar[index];
431 } 446 }
432 pressure_average /= PRESSURE_HISTORY_SIZE; 447 pressure_average /= PRESSURE_HISTORY_SIZE;
433 if(pressure_average == 1000.0) /* first pressure calculation */ 448 if(pressure_average == 1000.0) /* first pressure calculation */
434 { 449 {
435 if(fabs(pressurevalue - pressure_average) < 11000.0) /* just in case a reset occure during dive assume value equal < 100m as valid */ 450 if(fabs(pressurevalue - pressure_average) < 11000.0) /* just in case a reset occur during dive assume value equal < 100m as valid */
436 { 451 {
437 for(index = 0; index < PRESSURE_HISTORY_SIZE; index++) 452 for(index = 0; index < PRESSURE_HISTORY_SIZE; index++)
438 { 453 {
439 pressure_history_mbar[index] = pressurevalue; /* set history to current value */ 454 pressure_history_mbar[index] = pressurevalue; /* set history to current value */
440 retval = 1; 455 retval = 1;
442 } 457 }
443 } 458 }
444 else 459 else
445 { 460 {
446 if(fabs(pressurevalue - pressure_average) < PRESSURE_JUMP_VALID_MBAR) 461 if(fabs(pressurevalue - pressure_average) < PRESSURE_JUMP_VALID_MBAR)
447 pressure_history_mbar[pressurewriteindex++] = pressurevalue; 462 {
448 pressurewriteindex &= 0x7; /* wrap around if necessary */ 463 pressure_history_mbar[pressurewriteindex++] = pressurevalue;
449 retval = 1; 464 pressurewriteindex &= 0x7; /* wrap around if necessary */
465 retval = 1;
466 }
450 } 467 }
451 468
452 return retval; 469 return retval;
453 } 470 }
454 471
517 534
518 local_Px10 = (int32_t)( 535 local_Px10 = (int32_t)(
519 (((int64_t)((local_D1 * local_SENS) / 2097152)) - local_OFF) 536 (((int64_t)((local_D1 * local_SENS) / 2097152)) - local_OFF)
520 / 8192 );// )) / 10; // pow(2,21), pow(2,13) 537 / 8192 );// )) / 10; // pow(2,21), pow(2,13)
521 538
522 ambient_temperature = ((float)local_Tx100) / 100; 539 ambient_temperature = ((float)local_Tx100) / 100;
540 ambient_temperature += temperature_offset;
523 541
524 calc_pressure = ((float)local_Px10) / 10; 542 calc_pressure = ((float)local_Px10) / 10;
543 calc_pressure += pressure_offset;
544
525 if(pressure_plausible(calc_pressure)) 545 if(pressure_plausible(calc_pressure))
526 { 546 {
527 runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 1); 547 runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 1);
528 if (avgCnt < 10) /* build an average considering the last measurements to a a weight "1 of 10" */ 548 if (avgCnt < 10) /* build an average considering the last measurements to have a weight "1 of 10" */
529 { /* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */ 549 { /* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */
530 avgCnt++; /* by the measurement range of the sensor which is focused on under water pressure measurement */ 550 avgCnt++; /* by the measurement range of the sensor which is focused on under water pressure measurement */
531 } 551 }
532 ambient_pressure_mbar = runningAvg; 552 ambient_pressure_mbar = runningAvg;
533 } 553 }
732 D1 = 4944364; 752 D1 = 4944364;
733 D2 = 8198974; 753 D2 = 8198974;
734 pressure_calculation() ; 754 pressure_calculation() ;
735 }; 755 };
736 */ 756 */
737 757 void pressure_set_offset (float pressureOffset, float temperatureOffset)
758 {
759 if(pressure_offset != pressureOffset) /* we received a new value => reinit surface que */
760 {
761 ambient_pressure_mbar -= pressure_offset; /* revert old value */
762 ambient_pressure_mbar += pressureOffset; /* apply new offset */
763 init_surface_ring(1);
764 }
765
766 pressure_offset = pressureOffset;
767 temperature_offset = temperatureOffset;
768 }
769
770