Mercurial > public > ostc4
annotate Small_CPU/Src/pressure.c @ 353:9c8e2e962f55 fix-merge-mess
Added tag v1.5.0 release for changeset 143fe85f82a2
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author | Jan Mulder <jlmulder@xs4all.nl> |
---|---|
date | Tue, 08 Oct 2019 16:41:57 +0200 |
parents | 1f24022345d1 |
children | cb3870f79e9d |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @file pressure.c | |
4 * @author heinrichs weikamp gmbh | |
5 * @date 2014 | |
6 * @version V0.0.2 | |
7 * @since 20-Oct-2016 | |
8 * @brief | |
9 * | |
10 @verbatim | |
11 ============================================================================== | |
12 ##### How to use ##### | |
13 ============================================================================== | |
14 V0.0.2 18-Oct-2016 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015 | |
15 | |
16 @endverbatim | |
17 ****************************************************************************** | |
18 * @attention | |
19 * | |
20 * <h2><center>© COPYRIGHT(c) 2016 heinrichs weikamp</center></h2> | |
21 * | |
22 ****************************************************************************** | |
23 */ | |
24 | |
25 | |
26 | |
27 /* surface time | |
28 the last 30 minutes will be saved once per minute in a endless loop | |
29 at the beginning of a dive the oldest value will be used | |
30 */ | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
31 #include "math.h" |
241
2b9775f71e30
cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
32 #include "scheduler.h" |
38 | 33 #include "pressure.h" |
34 #include "i2c.h" | |
35 #include "rtc.h" | |
36 | |
37 #define CMD_RESET 0x1E // ADC reset command | |
38 #define CMD_ADC_READ 0x00 // ADC read command | |
39 #define CMD_ADC_CONV 0x40 // ADC conversion command | |
40 #define CMD_ADC_D1 0x00 // ADC D1 conversion | |
41 #define CMD_ADC_D2 0x10 // ADC D2 conversion | |
42 #define CMD_ADC_256 0x00 // ADC OSR=256 | |
43 #define CMD_ADC_512 0x02 // ADC OSR=512 | |
44 #define CMD_ADC_1024 0x04 // ADC OSR=1024 | |
45 #define CMD_ADC_2048 0x06 // ADC OSR=2056 | |
46 #define CMD_ADC_4096 0x08 // ADC OSR=4096 | |
47 #define CMD_PROM_RD 0xA0 // Prom read command | |
48 | |
352 | 49 /* remove comment to use a predefined profile for pressure changes instead of real world data */ |
50 /* #define SIMULATE_PRESSURE */ | |
51 | |
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 #define PRESSURE_JUMP_VALID_MBAR (500.0f) /* values are measure several times a second => jumps > 5m very unlikely */ | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
55 |
352 | 56 #define PRESSURE_SURFACE_QUE (30u) /* history buffer [minutes] for past pressure measurements */ |
57 #define PRESSURE_SURFACE_EVA_WINDOW (15u) /* Number of entries evaluated during instability test. Used to avoid detection while dive enters water */ | |
58 #define PRESSURE_SURFACE_STABLE_LIMIT (10u) /* Define pressure as stable if delta (mBar) is below this value */ | |
59 #define PRESSURE_SURFACE_DETECT_STABLE_CNT (5u) /* Event count to detect stable condition */ | |
60 #define PRESSURE_SURFACE_UNSTABLE_LIMIT (50u) /* Define pressure as not stable if delta (mBar) is larger than this value */ | |
61 #define PRESSURE_SURFACE_DETECT_UNSTABLE_CNT (3u) /* Event count to detect unstable condition */ | |
62 | |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
63 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
64 static uint16_t get_ci_by_coef_num(uint8_t coef_num); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
65 //void pressure_calculation_new(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
66 //void pressure_calculation_old(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
67 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
68 static uint8_t crc4(uint16_t n_prom[]); |
38 | 69 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
70 static HAL_StatusTypeDef pressure_sensor_get_data(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
71 static uint32_t get_adc(void); |
38 | 72 uint8_t pressureSensorInitSuccess = 0; |
73 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
74 static uint16_t C[8] = { 1 }; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
75 static uint32_t D1 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
76 static uint32_t D2 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
77 static uint8_t n_crc; |
38 | 78 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
79 static int64_t C5_x_2p8 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
80 static int64_t C2_x_2p16 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
81 static int64_t C1_x_2p15 = 1; |
38 | 82 |
83 /* | |
84 short C2plus10000 = -1; | |
85 short C3plus200 = -1; | |
86 short C4minus250 = -1; | |
87 short UT1 = -1; | |
88 short C6plus100 = -1; | |
89 */ | |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
90 static float pressure_offset = 0.0; /* Offset value which may be specified by the user via PC Software */ |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
91 static float temperature_offset = 0.0; /* Offset value which may be specified by the user via PC Software */ |
38 | 92 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
93 static float ambient_temperature = 0; |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
94 static float ambient_pressure_mbar = 1000.0; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
95 static float surface_pressure_mbar = 1000.0; |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
96 static float surface_ring_mbar[PRESSURE_SURFACE_QUE] = { 0 }; |
38 | 97 |
352 | 98 static uint8_t surface_pressure_writeIndex = 0; |
99 static float surface_pressure_stable_value = 0; | |
100 static uint8_t surface_pressure_stable = 0; | |
101 | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
102 static float pressure_history_mbar[PRESSURE_HISTORY_SIZE]; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
103 |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
104 static uint8_t secondCounterSurfaceRing = 0; |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
105 static uint8_t avgCount = 0; |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
106 static float runningAvg = 0; |
38 | 107 |
108 float get_temperature(void) | |
109 { | |
110 return ambient_temperature; | |
111 } | |
112 | |
113 float get_pressure_mbar(void) | |
114 { | |
115 return ambient_pressure_mbar; | |
116 } | |
117 | |
118 float get_surface_mbar(void) | |
119 { | |
120 return surface_pressure_mbar; | |
121 } | |
122 | |
123 | |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
124 void init_surface_ring(uint8_t force) |
38 | 125 { |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
126 if((surface_ring_mbar[0] == 0) || (force)) /* only initialize once. Keep value in place in case of an i2c recovery */ |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
127 { |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
128 secondCounterSurfaceRing = 0; /* restart calculation */ |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
129 avgCount = 0; |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
130 runningAvg = 0; |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
131 |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
132 for(int i=0; i<PRESSURE_SURFACE_QUE; i++) |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
133 surface_ring_mbar[i] = ambient_pressure_mbar; |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
134 surface_pressure_mbar = ambient_pressure_mbar; |
352 | 135 surface_pressure_writeIndex = 0; /* index of the oldest value in the ring buffer */ |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
136 } |
38 | 137 } |
138 | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
139 void init_pressure_history(void) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
140 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
141 for(int i=0; i<PRESSURE_HISTORY_SIZE; i++) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
142 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
143 pressure_history_mbar[i] = 1000.0; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
144 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
145 } |
38 | 146 |
352 | 147 uint8_t is_surface_pressure_stable(void) |
148 { | |
149 return surface_pressure_stable; | |
150 } | |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
151 |
352 | 152 float set_last_surface_pressure_stable(void) |
153 { | |
154 surface_pressure_mbar = surface_pressure_stable_value; | |
155 return surface_pressure_stable_value; | |
156 } | |
157 | |
158 /* iterate backward through the history memory and evaluate the changes pressure changes during the last 30 minutes */ | |
159 void evaluate_surface_pressure() | |
160 { | |
161 uint8_t index; | |
162 float lastvalue; | |
163 uint8_t stablecnt = 0; | |
164 uint8_t unstablecnt = 0; | |
165 uint8_t EvaluationWindow = PRESSURE_SURFACE_QUE - PRESSURE_SURFACE_EVA_WINDOW; /* do not use the latest 15 values to avoid unstable condition due to something like fin handling */ | |
166 uint8_t EvaluatedValues = 0; | |
167 | |
168 lastvalue = surface_ring_mbar[surface_pressure_writeIndex]; | |
169 surface_pressure_stable_value = surface_ring_mbar[surface_pressure_writeIndex]; /* default: if no stable value is found return the oldest value */ | |
170 index = surface_pressure_writeIndex; | |
171 surface_pressure_stable = 1; | |
172 | |
173 if(index == 0) | |
174 { | |
175 index = PRESSURE_SURFACE_QUE - 1; | |
176 } | |
177 else | |
178 { | |
179 index = index - 1; | |
180 } | |
181 do | |
182 { | |
183 if((EvaluatedValues < EvaluationWindow) && | |
184 (fabs(surface_pressure_stable_value - surface_ring_mbar[index]) > PRESSURE_SURFACE_UNSTABLE_LIMIT)) /* unusual change during last 30 minutes */ | |
185 { | |
186 unstablecnt++; | |
187 if(unstablecnt > PRESSURE_SURFACE_DETECT_UNSTABLE_CNT) | |
188 { | |
189 surface_pressure_stable = 0; | |
190 } | |
191 } | |
192 /* search for a value which does not change for several iterations */ | |
193 if (fabs(lastvalue - surface_ring_mbar[index]) < PRESSURE_SURFACE_STABLE_LIMIT) | |
194 { | |
195 stablecnt++; | |
196 } | |
197 else | |
198 { | |
199 stablecnt = 0; | |
200 } | |
201 if ((stablecnt >= PRESSURE_SURFACE_DETECT_STABLE_CNT) && (surface_pressure_stable == 0)&&(surface_pressure_stable_value == surface_ring_mbar[surface_pressure_writeIndex])) /* pressure is unstable => search for new stable value */ | |
202 { | |
203 surface_pressure_stable_value = surface_ring_mbar[index]; | |
204 unstablecnt = 0; | |
205 } | |
206 | |
207 lastvalue = surface_ring_mbar[index]; | |
208 | |
209 if(index == 0) | |
210 { | |
211 index = PRESSURE_SURFACE_QUE - 1; | |
212 } | |
213 else | |
214 { | |
215 index = index - 1; | |
216 } | |
217 EvaluatedValues++; | |
218 } while (index != surface_pressure_writeIndex); | |
219 } | |
38 | 220 void update_surface_pressure(uint8_t call_rhythm_seconds) |
221 { | |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
222 |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
223 |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
224 if(is_init_pressure_done()) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
225 { |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
226 runningAvg = (runningAvg * avgCount + ambient_pressure_mbar) / (avgCount +1); |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
227 avgCount++; |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
228 secondCounterSurfaceRing += call_rhythm_seconds; |
38 | 229 |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
230 if(secondCounterSurfaceRing >= 60) |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
231 { |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
232 if(runningAvg < PRESSURE_SURFACE_MAX_MBAR) |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
233 { |
352 | 234 surface_ring_mbar[surface_pressure_writeIndex] = runningAvg; |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
235 } |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
236 else |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
237 { |
352 | 238 surface_ring_mbar[surface_pressure_writeIndex] = PRESSURE_SURFACE_MAX_MBAR; |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
239 } |
352 | 240 surface_pressure_writeIndex++; /* the write index is now pointing to the oldest value in the buffer which will be overwritten next time */ |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
241 |
352 | 242 if(surface_pressure_writeIndex == PRESSURE_SURFACE_QUE) |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
243 { |
352 | 244 surface_pressure_writeIndex = 0; |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
245 } |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
246 |
352 | 247 surface_pressure_mbar = surface_ring_mbar[surface_pressure_writeIndex]; /* 30 minutes old measurement */ |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
248 |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
249 secondCounterSurfaceRing = 0; |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
250 avgCount = 1; /* use the current value as starting point but restart the weight decrement of the measurements */ |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
251 } |
352 | 252 evaluate_surface_pressure(); |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
253 } |
38 | 254 } |
255 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
256 #ifdef DEMOMODE |
38 | 257 float demo_modify_temperature_helper(float bottom_mbar_diff_to_surface) |
258 { | |
259 const float temperature_surface = 31.0; | |
260 const float temperature_bottom = 14.0; | |
261 | |
262 const float temperature_difference = temperature_bottom - temperature_surface; | |
263 | |
264 // range 0.0 - 1.0 | |
265 float position_now = (ambient_pressure_mbar - surface_pressure_mbar) / bottom_mbar_diff_to_surface; | |
266 | |
267 if(position_now <= 0) | |
268 return temperature_surface; | |
269 | |
270 if(position_now >= 1) | |
271 return temperature_bottom; | |
272 | |
273 return temperature_surface + (temperature_difference * position_now); | |
274 } | |
275 | |
276 | |
277 uint32_t demo_modify_temperature_and_pressure(int32_t divetime_in_seconds, uint8_t subseconds, float ceiling_mbar) | |
278 { | |
279 | |
280 const float descent_rate = 4000/60; | |
281 const float ascent_rate = 1000/60; | |
282 const uint32_t seconds_descend = (1 * 60) + 30; | |
283 const uint32_t turbo_seconds_at_bottom_start = (0 * 60) + 0; | |
284 const uint32_t seconds_descend_and_bottomtime = seconds_descend + turbo_seconds_at_bottom_start + (2 * 60) + 0; | |
285 uint32_t time_elapsed_in_seconds; | |
286 static float ambient_pressure_mbar_memory = 0; | |
287 static uint32_t time_last_call = 0; | |
288 | |
289 if(divetime_in_seconds <= seconds_descend) | |
290 { | |
291 ambient_pressure_mbar = (divetime_in_seconds * descent_rate) + ((float)(subseconds) * descent_rate) + surface_pressure_mbar; | |
292 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
293 | |
294 time_last_call = divetime_in_seconds; | |
295 return 0; | |
296 } | |
297 else | |
298 if(divetime_in_seconds <= seconds_descend + turbo_seconds_at_bottom_start) | |
299 { | |
300 ambient_pressure_mbar = (seconds_descend * descent_rate) + surface_pressure_mbar; | |
301 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
302 ambient_pressure_mbar_memory = ambient_pressure_mbar; | |
303 time_last_call = divetime_in_seconds; | |
304 return turbo_seconds_at_bottom_start; | |
305 } | |
306 else | |
307 if(divetime_in_seconds <= seconds_descend_and_bottomtime) | |
308 { | |
309 ambient_pressure_mbar = (seconds_descend * descent_rate) + surface_pressure_mbar; | |
310 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
311 ambient_pressure_mbar_memory = ambient_pressure_mbar; | |
312 time_last_call = divetime_in_seconds; | |
313 return 0; | |
314 } | |
315 else | |
316 { | |
317 time_elapsed_in_seconds = divetime_in_seconds - time_last_call; | |
318 ambient_pressure_mbar = ambient_pressure_mbar_memory - time_elapsed_in_seconds * ascent_rate; | |
319 | |
320 if(ambient_pressure_mbar < surface_pressure_mbar) | |
321 ambient_pressure_mbar = surface_pressure_mbar; | |
322 else if(ambient_pressure_mbar < ceiling_mbar) | |
323 ambient_pressure_mbar = ceiling_mbar; | |
324 | |
325 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
326 ambient_pressure_mbar_memory = ambient_pressure_mbar; | |
327 time_last_call = divetime_in_seconds; | |
328 return 0; | |
329 } | |
330 } | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
331 #endif |
38 | 332 |
333 uint8_t is_init_pressure_done(void) | |
334 { | |
335 return pressureSensorInitSuccess; | |
336 } | |
337 | |
338 uint8_t init_pressure(void) | |
339 { | |
340 uint8_t buffer[1]; | |
341 buffer[0] = 0x1e; | |
342 uint8_t retValue = 0xFF; | |
343 | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
344 pressureSensorInitSuccess = false; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
345 init_pressure_history(); |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
346 |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
347 /* Send reset request to pressure sensor */ |
38 | 348 retValue = I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1); |
349 if(retValue != HAL_OK) | |
350 { | |
351 return (HAL_StatusTypeDef)retValue; | |
352 } | |
353 HAL_Delay(3); | |
354 | |
355 for(uint8_t i=0;i<8;i++) | |
356 { | |
357 C[i] = get_ci_by_coef_num(i); | |
358 } | |
359 n_crc = crc4(C); // no evaluation at the moment hw 151026 | |
360 | |
361 C5_x_2p8 = C[5] * 256; | |
362 C2_x_2p16 = C[2] * 65536; | |
363 C1_x_2p15 = C[1] * 32768; | |
364 | |
241
2b9775f71e30
cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
365 if(global.I2C_SystemStatus == HAL_OK) |
38 | 366 { |
367 pressureSensorInitSuccess = 1; | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
368 retValue = pressure_update(); |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
369 |
38 | 370 } |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
371 return retValue; |
38 | 372 } |
373 | |
374 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
375 static uint32_t get_adc(void) |
38 | 376 { |
377 uint8_t buffer[1]; | |
378 uint8_t resivebuf[4]; | |
379 uint32_t answer = 0; | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
380 |
38 | 381 buffer[0] = 0x00; // Get ADC |
382 I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1); | |
383 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 4); | |
384 resivebuf[3] = 0; | |
385 answer = 256*256 *(uint32_t)resivebuf[0] + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2]; | |
386 | |
387 return answer; | |
388 } | |
389 | |
390 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
391 static uint16_t get_ci_by_coef_num(uint8_t coef_num) |
38 | 392 { |
393 uint8_t resivebuf[2]; | |
394 | |
395 uint8_t cmd = CMD_PROM_RD+coef_num*2; | |
396 I2C_Master_Transmit( DEVICE_PRESSURE, &cmd, 1); | |
397 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 2); | |
398 return (256*(uint16_t)resivebuf[0]) + (uint16_t)resivebuf[1]; | |
399 } | |
400 | |
401 | |
402 | |
403 uint8_t pressure_update(void) | |
404 { | |
405 HAL_StatusTypeDef statusReturn = HAL_TIMEOUT; | |
406 | |
407 statusReturn = pressure_sensor_get_data(); | |
408 pressure_calculation(); | |
409 return (uint8_t)statusReturn; | |
410 } | |
411 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
412 /* Switch between pressure and temperature measurement with every successful read operation */ |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
413 void pressure_update_alternating(void) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
414 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
415 static uint8_t getTemperature= 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
416 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
417 if(getTemperature) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
418 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
419 if(pressure_sensor_get_temperature_raw() == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
420 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
421 getTemperature = 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
422 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
423 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
424 else |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
425 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
426 if(pressure_sensor_get_pressure_raw() == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
427 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
428 getTemperature = 1; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
429 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
430 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
431 pressure_calculation(); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
432 return; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
433 } |
38 | 434 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
435 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn) |
38 | 436 { |
437 uint8_t command = CMD_ADC_CONV + cmd; | |
438 HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT; | |
439 | |
440 statusReturnTemp = I2C_Master_Transmit( DEVICE_PRESSURE, &command, 1); | |
441 | |
442 if(statusReturn) | |
443 { | |
444 *statusReturn = statusReturnTemp; | |
445 } | |
446 | |
447 switch (cmd & 0x0f) // wait necessary conversion time | |
448 { | |
449 case CMD_ADC_256 : HAL_Delay(1); break; | |
450 case CMD_ADC_512 : HAL_Delay(3); break; | |
451 case CMD_ADC_1024: HAL_Delay(4); break; | |
452 case CMD_ADC_2048: HAL_Delay(6); break; | |
453 case CMD_ADC_4096: HAL_Delay(10); break; | |
454 } | |
455 return get_adc(); | |
456 } | |
457 | |
458 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
459 static HAL_StatusTypeDef pressure_sensor_get_data(void) |
38 | 460 { |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
461 uint32_t requestedValue = 0; |
38 | 462 HAL_StatusTypeDef statusReturn1 = HAL_TIMEOUT; |
463 HAL_StatusTypeDef statusReturn2 = HAL_TIMEOUT; | |
464 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
465 |
38 | 466 |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
467 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_1024, &statusReturn2); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
468 if (statusReturn2 == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
469 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
470 D2 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
471 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
472 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
473 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_1024, &statusReturn1); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
474 if (statusReturn1 == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
475 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
476 D1 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
477 } |
38 | 478 if(statusReturn2 > statusReturn1) // if anything is not HAL_OK (0x00) or worse |
479 return statusReturn2; | |
480 else | |
481 return statusReturn1; | |
482 } | |
483 | |
484 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
485 HAL_StatusTypeDef pressure_sensor_get_pressure_raw(void) |
38 | 486 { |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
487 uint32_t requestedValue = 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
488 HAL_StatusTypeDef statusReturn = HAL_TIMEOUT; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
489 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
490 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_1024, &statusReturn); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
491 if (statusReturn == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
492 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
493 D1 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
494 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
495 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
496 return statusReturn; |
38 | 497 } |
498 | |
499 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
500 HAL_StatusTypeDef pressure_sensor_get_temperature_raw(void) |
38 | 501 { |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
502 uint32_t requestedValue = 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
503 HAL_StatusTypeDef statusReturn = HAL_TIMEOUT; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
504 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
505 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_1024, &statusReturn); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
506 if (statusReturn == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
507 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
508 D2 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
509 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
510 return statusReturn; |
38 | 511 } |
512 | |
513 | |
352 | 514 #ifdef SIMULATE_PRESSURE |
515 void pressure_simulation() | |
516 { | |
517 static uint32_t tickstart = 0; | |
518 static float pressure_sim_mbar = 0; | |
519 static uint32_t passedSecond = 0; | |
520 static uint32_t secondtick = 0; | |
521 | |
522 uint32_t lasttick = 0; | |
523 | |
524 | |
525 | |
526 if( tickstart == 0) | |
527 { | |
528 tickstart = HAL_GetTick(); /* init time stamp */ | |
529 secondtick = tickstart; | |
530 pressure_sim_mbar = 1000; | |
531 } | |
532 | |
533 lasttick = HAL_GetTick(); | |
534 if(time_elapsed_ms(secondtick,lasttick) > 1000) /* one second passed since last tick */ | |
535 { | |
536 secondtick = lasttick; | |
537 passedSecond++; | |
538 | |
539 #ifdef DIVE_AFTER_LANDING | |
540 if(passedSecond < 10) pressure_sim_mbar = 1000.0; /* stay stable for 10 seconds */ | |
541 else if(passedSecond < 300) pressure_sim_mbar -= 1.0; /* decrease pressure in 5 minutes target 770mbar => delta 330 */ | |
542 else if(passedSecond < 900) pressure_sim_mbar += 0.0; /*stay stable 10 minutes*/ | |
543 else if(passedSecond < 1500) pressure_sim_mbar += 0.5; /* return to 1 bar in 10 Minutes*/ | |
544 else if(passedSecond < 1800) pressure_sim_mbar += 0.0; /* 5 minutes break */ | |
545 else if(passedSecond < 2000) pressure_sim_mbar += 10.0; /* start dive */ | |
546 else if(passedSecond < 2300) pressure_sim_mbar += 0.0; /* stay on depth */ | |
547 else if(passedSecond < 2500) pressure_sim_mbar -= 10.0; /* return to surface */ | |
548 else pressure_sim_mbar = 1000.0; /* final state */ | |
549 #else /* short dive */ | |
550 if(passedSecond < 10) pressure_sim_mbar = 1000.0; /* stay stable for 10 seconds */ | |
551 else if(passedSecond < 180) pressure_sim_mbar += 10.0; /* Start dive */ | |
552 else if(passedSecond < 300) pressure_sim_mbar += 0.0; /*stay on depth*/ | |
553 else if(passedSecond < 460) pressure_sim_mbar -= 10.0; /* return to surface */ | |
554 else if(passedSecond < 600) pressure_sim_mbar += 0.0; /* stay */ | |
555 else if(passedSecond < 610) pressure_sim_mbar = 1000.0; /* get ready for second dive */ | |
556 else if(passedSecond < 780) pressure_sim_mbar += 10.0; /* Start dive */ | |
557 else if(passedSecond < 900) pressure_sim_mbar += 0.0; /*stay on depth*/ | |
558 else if(passedSecond < 1060) pressure_sim_mbar -= 10.0; /* return to surface */ | |
559 else if(passedSecond < 1200) pressure_sim_mbar += 0.0; /* stay */ | |
560 else pressure_sim_mbar = 1000.0; /* final state */ | |
561 #endif | |
562 } | |
563 | |
564 | |
565 ambient_pressure_mbar = pressure_sim_mbar; | |
566 ambient_temperature = 25.0; | |
567 return; | |
568 } | |
569 | |
570 #endif | |
571 | |
38 | 572 void pressure_calculation(void) |
573 { | |
241
2b9775f71e30
cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
574 if(global.I2C_SystemStatus != HAL_OK) |
38 | 575 return; |
352 | 576 |
577 #ifdef SIMULATE_PRESSURE | |
578 pressure_simulation(); | |
579 #else | |
38 | 580 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(); |
352 | 581 #endif |
38 | 582 } |
583 | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
584 static uint8_t pressure_plausible(float pressurevalue) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
585 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
586 static uint8_t pressurewriteindex = 0; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
587 uint8_t retval = 0; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
588 uint8_t index; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
589 float pressure_average = 0; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
590 |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
591 for(index = 0; index < PRESSURE_HISTORY_SIZE; index++) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
592 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
593 pressure_average += pressure_history_mbar[index]; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
594 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
595 pressure_average /= PRESSURE_HISTORY_SIZE; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
596 if(pressure_average == 1000.0) /* first pressure calculation */ |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
597 { |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
598 if(fabs(pressurevalue - pressure_average) < 11000.0) /* just in case a reset occur during dive assume value equal < 100m as valid */ |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
599 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
600 for(index = 0; index < PRESSURE_HISTORY_SIZE; index++) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
601 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
602 pressure_history_mbar[index] = pressurevalue; /* set history to current value */ |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
603 retval = 1; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
604 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
605 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
606 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
607 else |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
608 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
609 if(fabs(pressurevalue - pressure_average) < PRESSURE_JUMP_VALID_MBAR) |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
610 { |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
611 pressure_history_mbar[pressurewriteindex++] = pressurevalue; |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
612 pressurewriteindex &= 0x7; /* wrap around if necessary */ |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
613 retval = 1; |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
614 } |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
615 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
616 |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
617 return retval; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
618 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
619 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
620 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void) |
38 | 621 { |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
622 static float runningAvg = 0; |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
623 static uint8_t avgCnt = 0; |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
624 |
38 | 625 uint32_t local_D1; // ADC value of the pressure conversion |
626 uint32_t local_D2; // ADC value of the temperature conversion | |
627 int32_t local_Px10; // compensated pressure value | |
628 int32_t local_Tx100; // compensated temperature value | |
629 int64_t local_dT; // int32_t, difference between actual and measured temperature | |
630 int64_t local_OFF; // offset at actual temperature | |
631 int64_t local_SENS; // sensitivity at actual temperature | |
632 | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
633 float calc_pressure; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
634 |
38 | 635 int64_t T2; |
636 int64_t OFF2; | |
637 int64_t SENS2; | |
638 | |
639 local_D1 = D1; | |
640 local_D2 = D2; | |
641 | |
642 local_dT = ((int64_t)local_D2) - ((int64_t)C[5]) * 256; //pow(2,8); | |
643 local_OFF = ((int64_t)C[2]) * 65536 + local_dT * ((int64_t)C[4]) / 128; // pow(2,16), pow(2,7) | |
644 local_SENS = ((int64_t)C[1]) * 32768 + local_dT * ((int64_t)C[3]) / 256; // pow(2,15), pow(2,8) | |
645 | |
646 local_Tx100 = (int32_t)(2000 + (local_dT * ((int64_t)C[6])) / 8388608);// pow(2,23) | |
647 | |
648 | |
649 if(local_Tx100 < 2000) // low temperature | |
650 { | |
651 T2 = 3 * local_dT; | |
652 T2 *= local_dT; | |
653 T2 /= 8589934592; | |
654 | |
655 OFF2 = ((int64_t)local_Tx100) - 2000; | |
656 OFF2 *= OFF2; | |
657 OFF2 *= 3; | |
658 OFF2 /= 2; | |
659 | |
660 SENS2 = ((int64_t)local_Tx100) - 2000; | |
661 SENS2 *= SENS2; | |
662 SENS2 *= 5; | |
663 SENS2 /= 8; | |
664 | |
665 local_Tx100 -= (int32_t)T2; | |
666 local_OFF -= OFF2; | |
667 local_SENS -= SENS2; | |
668 } | |
669 else | |
670 { | |
671 T2 = 7 * local_dT; | |
672 T2 *= local_dT; | |
673 T2 /= 137438953472; | |
674 | |
675 OFF2 = ((int64_t)local_Tx100) - 2000; | |
676 OFF2 *= OFF2; | |
677 OFF2 /= 16; | |
678 | |
679 local_Tx100 -= (int32_t)T2; | |
680 local_OFF -= OFF2; | |
681 } | |
682 | |
683 local_Px10 = (int32_t)( | |
684 (((int64_t)((local_D1 * local_SENS) / 2097152)) - local_OFF) | |
685 / 8192 );// )) / 10; // pow(2,21), pow(2,13) | |
686 | |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
687 ambient_temperature = ((float)local_Tx100) / 100; |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
688 ambient_temperature += temperature_offset; |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
689 |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
690 calc_pressure = ((float)local_Px10) / 10; |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
691 calc_pressure += pressure_offset; |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
692 |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
693 if(pressure_plausible(calc_pressure)) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
694 { |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
695 runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 1); |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
696 if (avgCnt < 10) /* build an average considering the last measurements to have a weight "1 of 10" */ |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
697 { /* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */ |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
698 avgCnt++; /* by the measurement range of the sensor which is focused on under water pressure measurement */ |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
699 } |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
700 ambient_pressure_mbar = runningAvg; |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
701 } |
38 | 702 } |
703 | |
704 | |
705 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011 | |
706 * short and int are both 16bit according to AVR/GCC google results | |
707 */ | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
708 static uint8_t crc4(uint16_t n_prom[]) |
38 | 709 { |
710 uint16_t cnt; // simple counter | |
711 uint16_t n_rem; // crc reminder | |
712 uint16_t crc_read; // original value of the crc | |
713 uint8_t n_bit; | |
714 n_rem = 0x00; | |
715 crc_read=n_prom[7]; //save read CRC | |
716 n_prom[7]=(0xFF00 & (n_prom[7])); //CRC byte is replaced by 0 | |
717 for (cnt = 0; cnt < 16; cnt++) // operation is performed on bytes | |
718 { // choose LSB or MSB | |
719 if (cnt%2==1) n_rem ^= (uint16_t) ((n_prom[cnt>>1]) & 0x00FF); | |
720 else n_rem ^= (uint16_t) (n_prom[cnt>>1]>>8); | |
721 for (n_bit = 8; n_bit > 0; n_bit--) | |
722 { | |
723 if (n_rem & (0x8000)) | |
724 { | |
725 n_rem = (n_rem << 1) ^ 0x3000; | |
726 } | |
727 else | |
728 { | |
729 n_rem = (n_rem << 1); | |
730 } | |
731 } | |
732 } | |
733 n_rem= (0x000F & (n_rem >> 12)); // // final 4-bit reminder is CRC code | |
734 n_prom[7]=crc_read; // restore the crc_read to its original place | |
735 return (n_rem ^ 0x00); | |
736 } | |
737 /* | |
738 void test_calculation(void) | |
739 { | |
740 C1 = 29112; | |
741 C2 = 26814; | |
742 C3 = 19125; | |
743 C4 = 17865; | |
744 C5 = 32057; | |
745 C6 = 31305; | |
746 | |
747 C2_x_2p16 = C2 * 65536; | |
748 C1_x_2p15 = C1 * 32768; | |
749 | |
750 D1 = 4944364; | |
751 D2 = 8198974; | |
752 pressure_calculation() ; | |
753 }; | |
754 */ | |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
755 void pressure_set_offset (float pressureOffset, float temperatureOffset) |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
756 { |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
757 if(pressure_offset != pressureOffset) /* we received a new value => reinit surface que */ |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
758 { |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
759 ambient_pressure_mbar -= pressure_offset; /* revert old value */ |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
760 ambient_pressure_mbar += pressureOffset; /* apply new offset */ |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
761 init_surface_ring(1); |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
762 } |
38 | 763 |
339
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
764 pressure_offset = pressureOffset; |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
765 temperature_offset = temperatureOffset; |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
766 } |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
767 |
37f45300bc2e
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
ideenmodellierer
parents:
335
diff
changeset
|
768 |