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