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