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