Mercurial > public > ostc4
annotate Small_CPU/Src/pressure.c @ 337:38367bb982b8 PressureMeasure_Improvment
Set Max value for pressure offset to 50mBar (20mBar before)
Some OSTC4 might have a higher delta and need a bigger offset
author | ideenmodellierer |
---|---|
date | Sat, 17 Aug 2019 18:47:08 +0200 |
parents | c11ce8c885d3 |
children | 37f45300bc2e |
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 | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
49 #define PRESSURE_HISTORY_SIZE (8u) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
50 #define PRESSURE_JUMP_VALID_MBAR (500.0f) /* values are measure several times a second => jumps > 5m very unlikely */ |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
51 |
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
|
52 #define PRESSURE_SURFACE_QUE (30u) /* history buffer [minutes] for past pressure 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
|
53 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
54 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
|
55 //void pressure_calculation_new(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
56 //void pressure_calculation_old(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
57 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
58 static uint8_t crc4(uint16_t n_prom[]); |
38 | 59 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
60 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
|
61 static uint32_t get_adc(void); |
38 | 62 uint8_t pressureSensorInitSuccess = 0; |
63 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
64 static uint16_t C[8] = { 1 }; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
65 static uint32_t D1 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
66 static uint32_t D2 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
67 static uint8_t n_crc; |
38 | 68 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
69 static int64_t C5_x_2p8 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
70 static int64_t C2_x_2p16 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
71 static int64_t C1_x_2p15 = 1; |
38 | 72 |
73 /* | |
74 short C2plus10000 = -1; | |
75 short C3plus200 = -1; | |
76 short C4minus250 = -1; | |
77 short UT1 = -1; | |
78 short C6plus100 = -1; | |
79 */ | |
80 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
81 static float ambient_temperature = 0; |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
82 static float ambient_pressure_mbar = 1000.0; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
83 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
|
84 static float surface_ring_mbar[PRESSURE_SURFACE_QUE] = { 0 }; |
38 | 85 |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
86 static float pressure_history_mbar[PRESSURE_HISTORY_SIZE]; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
87 |
38 | 88 uint8_t secondCounterSurfaceRing = 0; |
89 | |
90 float get_temperature(void) | |
91 { | |
92 return ambient_temperature; | |
93 } | |
94 | |
95 float get_pressure_mbar(void) | |
96 { | |
97 return ambient_pressure_mbar; | |
98 } | |
99 | |
100 float get_surface_mbar(void) | |
101 { | |
102 return surface_pressure_mbar; | |
103 } | |
104 | |
105 | |
106 void init_surface_ring(void) | |
107 { | |
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
|
108 if(surface_ring_mbar[0] == 0) /* only initialize once. Keep value in place in case of an i2c recovery */ |
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
|
109 { |
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
|
110 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
|
111 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
|
112 surface_pressure_mbar = 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
|
113 } |
38 | 114 } |
115 | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
116 void init_pressure_history(void) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
117 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
118 for(int i=0; i<PRESSURE_HISTORY_SIZE; i++) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
119 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
120 pressure_history_mbar[i] = 1000.0; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
121 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
122 } |
38 | 123 |
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
|
124 |
38 | 125 void update_surface_pressure(uint8_t call_rhythm_seconds) |
126 { | |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
127 static uint8_t writeIndex = 0; /* Reinitialization will reset all entries to the same value => no need to reinit write index */ |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
128 static uint8_t avgCount = 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
|
129 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
|
130 |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
131 if(is_init_pressure_done()) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
132 { |
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
|
133 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
|
134 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
|
135 secondCounterSurfaceRing += call_rhythm_seconds; |
38 | 136 |
335
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
137 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
|
138 { |
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
|
139 surface_ring_mbar[writeIndex] = runningAvg; |
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
|
140 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
|
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 if(writeIndex == PRESSURE_SURFACE_QUE) |
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
|
143 { |
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
|
144 writeIndex = 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
|
145 } |
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
|
146 |
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
|
147 surface_pressure_mbar = surface_ring_mbar[writeIndex]; /* 30 minutes old measurement */ |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
148 |
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
|
149 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
|
150 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
|
151 } |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
152 } |
38 | 153 } |
154 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
155 #ifdef DEMOMODE |
38 | 156 float demo_modify_temperature_helper(float bottom_mbar_diff_to_surface) |
157 { | |
158 const float temperature_surface = 31.0; | |
159 const float temperature_bottom = 14.0; | |
160 | |
161 const float temperature_difference = temperature_bottom - temperature_surface; | |
162 | |
163 // range 0.0 - 1.0 | |
164 float position_now = (ambient_pressure_mbar - surface_pressure_mbar) / bottom_mbar_diff_to_surface; | |
165 | |
166 if(position_now <= 0) | |
167 return temperature_surface; | |
168 | |
169 if(position_now >= 1) | |
170 return temperature_bottom; | |
171 | |
172 return temperature_surface + (temperature_difference * position_now); | |
173 } | |
174 | |
175 | |
176 uint32_t demo_modify_temperature_and_pressure(int32_t divetime_in_seconds, uint8_t subseconds, float ceiling_mbar) | |
177 { | |
178 | |
179 const float descent_rate = 4000/60; | |
180 const float ascent_rate = 1000/60; | |
181 const uint32_t seconds_descend = (1 * 60) + 30; | |
182 const uint32_t turbo_seconds_at_bottom_start = (0 * 60) + 0; | |
183 const uint32_t seconds_descend_and_bottomtime = seconds_descend + turbo_seconds_at_bottom_start + (2 * 60) + 0; | |
184 uint32_t time_elapsed_in_seconds; | |
185 static float ambient_pressure_mbar_memory = 0; | |
186 static uint32_t time_last_call = 0; | |
187 | |
188 if(divetime_in_seconds <= seconds_descend) | |
189 { | |
190 ambient_pressure_mbar = (divetime_in_seconds * descent_rate) + ((float)(subseconds) * descent_rate) + surface_pressure_mbar; | |
191 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
192 | |
193 time_last_call = divetime_in_seconds; | |
194 return 0; | |
195 } | |
196 else | |
197 if(divetime_in_seconds <= seconds_descend + turbo_seconds_at_bottom_start) | |
198 { | |
199 ambient_pressure_mbar = (seconds_descend * descent_rate) + surface_pressure_mbar; | |
200 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
201 ambient_pressure_mbar_memory = ambient_pressure_mbar; | |
202 time_last_call = divetime_in_seconds; | |
203 return turbo_seconds_at_bottom_start; | |
204 } | |
205 else | |
206 if(divetime_in_seconds <= seconds_descend_and_bottomtime) | |
207 { | |
208 ambient_pressure_mbar = (seconds_descend * descent_rate) + surface_pressure_mbar; | |
209 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
210 ambient_pressure_mbar_memory = ambient_pressure_mbar; | |
211 time_last_call = divetime_in_seconds; | |
212 return 0; | |
213 } | |
214 else | |
215 { | |
216 time_elapsed_in_seconds = divetime_in_seconds - time_last_call; | |
217 ambient_pressure_mbar = ambient_pressure_mbar_memory - time_elapsed_in_seconds * ascent_rate; | |
218 | |
219 if(ambient_pressure_mbar < surface_pressure_mbar) | |
220 ambient_pressure_mbar = surface_pressure_mbar; | |
221 else if(ambient_pressure_mbar < ceiling_mbar) | |
222 ambient_pressure_mbar = ceiling_mbar; | |
223 | |
224 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
225 ambient_pressure_mbar_memory = ambient_pressure_mbar; | |
226 time_last_call = divetime_in_seconds; | |
227 return 0; | |
228 } | |
229 } | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
230 #endif |
38 | 231 |
232 uint8_t is_init_pressure_done(void) | |
233 { | |
234 return pressureSensorInitSuccess; | |
235 } | |
236 | |
237 uint8_t init_pressure(void) | |
238 { | |
239 uint8_t buffer[1]; | |
240 buffer[0] = 0x1e; | |
241 uint8_t retValue = 0xFF; | |
242 | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
243 pressureSensorInitSuccess = false; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
244 init_pressure_history(); |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
245 |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
246 /* Send reset request to pressure sensor */ |
38 | 247 retValue = I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1); |
248 if(retValue != HAL_OK) | |
249 { | |
250 return (HAL_StatusTypeDef)retValue; | |
251 } | |
252 HAL_Delay(3); | |
253 | |
254 for(uint8_t i=0;i<8;i++) | |
255 { | |
256 C[i] = get_ci_by_coef_num(i); | |
257 } | |
258 n_crc = crc4(C); // no evaluation at the moment hw 151026 | |
259 | |
260 C5_x_2p8 = C[5] * 256; | |
261 C2_x_2p16 = C[2] * 65536; | |
262 C1_x_2p15 = C[1] * 32768; | |
263 | |
241
2b9775f71e30
cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
264 if(global.I2C_SystemStatus == HAL_OK) |
38 | 265 { |
266 pressureSensorInitSuccess = 1; | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
267 retValue = pressure_update(); |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
268 |
38 | 269 } |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
270 return retValue; |
38 | 271 } |
272 | |
273 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
274 static uint32_t get_adc(void) |
38 | 275 { |
276 uint8_t buffer[1]; | |
277 uint8_t resivebuf[4]; | |
278 uint32_t answer = 0; | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
279 |
38 | 280 buffer[0] = 0x00; // Get ADC |
281 I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1); | |
282 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 4); | |
283 resivebuf[3] = 0; | |
284 answer = 256*256 *(uint32_t)resivebuf[0] + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2]; | |
285 | |
286 return answer; | |
287 } | |
288 | |
289 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
290 static uint16_t get_ci_by_coef_num(uint8_t coef_num) |
38 | 291 { |
292 uint8_t resivebuf[2]; | |
293 | |
294 uint8_t cmd = CMD_PROM_RD+coef_num*2; | |
295 I2C_Master_Transmit( DEVICE_PRESSURE, &cmd, 1); | |
296 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 2); | |
297 return (256*(uint16_t)resivebuf[0]) + (uint16_t)resivebuf[1]; | |
298 } | |
299 | |
300 | |
301 | |
302 uint8_t pressure_update(void) | |
303 { | |
304 HAL_StatusTypeDef statusReturn = HAL_TIMEOUT; | |
305 | |
306 statusReturn = pressure_sensor_get_data(); | |
307 pressure_calculation(); | |
308 return (uint8_t)statusReturn; | |
309 } | |
310 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
311 /* Switch between pressure and temperature measurement with every successful read operation */ |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
312 void pressure_update_alternating(void) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
313 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
314 static uint8_t getTemperature= 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
315 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
316 if(getTemperature) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
317 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
318 if(pressure_sensor_get_temperature_raw() == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
319 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
320 getTemperature = 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
321 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
322 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
323 else |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
324 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
325 if(pressure_sensor_get_pressure_raw() == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
326 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
327 getTemperature = 1; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
328 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
329 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
330 pressure_calculation(); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
331 return; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
332 } |
38 | 333 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
334 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn) |
38 | 335 { |
336 uint8_t command = CMD_ADC_CONV + cmd; | |
337 HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT; | |
338 | |
339 statusReturnTemp = I2C_Master_Transmit( DEVICE_PRESSURE, &command, 1); | |
340 | |
341 if(statusReturn) | |
342 { | |
343 *statusReturn = statusReturnTemp; | |
344 } | |
345 | |
346 switch (cmd & 0x0f) // wait necessary conversion time | |
347 { | |
348 case CMD_ADC_256 : HAL_Delay(1); break; | |
349 case CMD_ADC_512 : HAL_Delay(3); break; | |
350 case CMD_ADC_1024: HAL_Delay(4); break; | |
351 case CMD_ADC_2048: HAL_Delay(6); break; | |
352 case CMD_ADC_4096: HAL_Delay(10); break; | |
353 } | |
354 return get_adc(); | |
355 } | |
356 | |
357 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
358 static HAL_StatusTypeDef pressure_sensor_get_data(void) |
38 | 359 { |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
360 uint32_t requestedValue = 0; |
38 | 361 HAL_StatusTypeDef statusReturn1 = HAL_TIMEOUT; |
362 HAL_StatusTypeDef statusReturn2 = HAL_TIMEOUT; | |
363 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
364 |
38 | 365 |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
366 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_1024, &statusReturn2); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
367 if (statusReturn2 == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
368 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
369 D2 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
370 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
371 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
372 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_1024, &statusReturn1); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
373 if (statusReturn1 == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
374 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
375 D1 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
376 } |
38 | 377 if(statusReturn2 > statusReturn1) // if anything is not HAL_OK (0x00) or worse |
378 return statusReturn2; | |
379 else | |
380 return statusReturn1; | |
381 } | |
382 | |
383 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
384 HAL_StatusTypeDef pressure_sensor_get_pressure_raw(void) |
38 | 385 { |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
386 uint32_t requestedValue = 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
387 HAL_StatusTypeDef statusReturn = HAL_TIMEOUT; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
388 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
389 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_1024, &statusReturn); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
390 if (statusReturn == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
391 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
392 D1 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
393 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
394 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
395 return statusReturn; |
38 | 396 } |
397 | |
398 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
399 HAL_StatusTypeDef pressure_sensor_get_temperature_raw(void) |
38 | 400 { |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
401 uint32_t requestedValue = 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
402 HAL_StatusTypeDef statusReturn = HAL_TIMEOUT; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
403 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
404 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_1024, &statusReturn); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
405 if (statusReturn == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
406 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
407 D2 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
408 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
409 return statusReturn; |
38 | 410 } |
411 | |
412 | |
413 void pressure_calculation(void) | |
414 { | |
241
2b9775f71e30
cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
415 if(global.I2C_SystemStatus != HAL_OK) |
38 | 416 return; |
417 | |
418 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(); | |
419 } | |
420 | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
421 static uint8_t pressure_plausible(float pressurevalue) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
422 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
423 static uint8_t pressurewriteindex = 0; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
424 uint8_t retval = 0; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
425 uint8_t index; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
426 float pressure_average = 0; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
427 |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
428 for(index = 0; index < PRESSURE_HISTORY_SIZE; index++) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
429 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
430 pressure_average += pressure_history_mbar[index]; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
431 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
432 pressure_average /= PRESSURE_HISTORY_SIZE; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
433 if(pressure_average == 1000.0) /* first pressure calculation */ |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
434 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
435 if(fabs(pressurevalue - pressure_average) < 11000.0) /* just in case a reset occure during dive assume value equal < 100m as valid */ |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
436 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
437 for(index = 0; index < PRESSURE_HISTORY_SIZE; index++) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
438 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
439 pressure_history_mbar[index] = pressurevalue; /* set history to current value */ |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
440 retval = 1; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
441 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
442 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
443 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
444 else |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
445 { |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
446 if(fabs(pressurevalue - pressure_average) < PRESSURE_JUMP_VALID_MBAR) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
447 pressure_history_mbar[pressurewriteindex++] = pressurevalue; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
448 pressurewriteindex &= 0x7; /* wrap around if necessary */ |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
449 retval = 1; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
450 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
451 |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
452 return retval; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
453 } |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
454 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
455 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void) |
38 | 456 { |
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
|
457 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
|
458 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
|
459 |
38 | 460 uint32_t local_D1; // ADC value of the pressure conversion |
461 uint32_t local_D2; // ADC value of the temperature conversion | |
462 int32_t local_Px10; // compensated pressure value | |
463 int32_t local_Tx100; // compensated temperature value | |
464 int64_t local_dT; // int32_t, difference between actual and measured temperature | |
465 int64_t local_OFF; // offset at actual temperature | |
466 int64_t local_SENS; // sensitivity at actual temperature | |
467 | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
468 float calc_pressure; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
469 |
38 | 470 int64_t T2; |
471 int64_t OFF2; | |
472 int64_t SENS2; | |
473 | |
474 local_D1 = D1; | |
475 local_D2 = D2; | |
476 | |
477 local_dT = ((int64_t)local_D2) - ((int64_t)C[5]) * 256; //pow(2,8); | |
478 local_OFF = ((int64_t)C[2]) * 65536 + local_dT * ((int64_t)C[4]) / 128; // pow(2,16), pow(2,7) | |
479 local_SENS = ((int64_t)C[1]) * 32768 + local_dT * ((int64_t)C[3]) / 256; // pow(2,15), pow(2,8) | |
480 | |
481 local_Tx100 = (int32_t)(2000 + (local_dT * ((int64_t)C[6])) / 8388608);// pow(2,23) | |
482 | |
483 | |
484 if(local_Tx100 < 2000) // low temperature | |
485 { | |
486 T2 = 3 * local_dT; | |
487 T2 *= local_dT; | |
488 T2 /= 8589934592; | |
489 | |
490 OFF2 = ((int64_t)local_Tx100) - 2000; | |
491 OFF2 *= OFF2; | |
492 OFF2 *= 3; | |
493 OFF2 /= 2; | |
494 | |
495 SENS2 = ((int64_t)local_Tx100) - 2000; | |
496 SENS2 *= SENS2; | |
497 SENS2 *= 5; | |
498 SENS2 /= 8; | |
499 | |
500 local_Tx100 -= (int32_t)T2; | |
501 local_OFF -= OFF2; | |
502 local_SENS -= SENS2; | |
503 } | |
504 else | |
505 { | |
506 T2 = 7 * local_dT; | |
507 T2 *= local_dT; | |
508 T2 /= 137438953472; | |
509 | |
510 OFF2 = ((int64_t)local_Tx100) - 2000; | |
511 OFF2 *= OFF2; | |
512 OFF2 /= 16; | |
513 | |
514 local_Tx100 -= (int32_t)T2; | |
515 local_OFF -= OFF2; | |
516 } | |
517 | |
518 local_Px10 = (int32_t)( | |
519 (((int64_t)((local_D1 * local_SENS) / 2097152)) - local_OFF) | |
520 / 8192 );// )) / 10; // pow(2,21), pow(2,13) | |
521 | |
522 ambient_temperature = ((float)local_Tx100) / 100; | |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
523 |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
524 calc_pressure = ((float)local_Px10) / 10; |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
525 if(pressure_plausible(calc_pressure)) |
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
526 { |
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
|
527 runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 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
|
528 if (avgCnt < 10) /* build an average considering the last measurements to a a weight "1 of 10" */ |
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
|
529 { /* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */ |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
530 avgCnt++; /* by the measurement range of the sensor which is focused on under water pressure measurement */ |
c11ce8c885d3
Use average calculation for pressure: precondition was that pressure values jittered +- 10 HPa from one capture (once a second) to the other. Basically pressure is measured several times a second => using these values in an additional history calculation reduces the jitter down to +-1 per second
ideenmodellierer
parents:
331
diff
changeset
|
531 } |
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
|
532 ambient_pressure_mbar = runningAvg; |
331
b4c578caaafb
Added plausibility check for pressure values
ideenmodellierer
parents:
276
diff
changeset
|
533 } |
38 | 534 } |
535 | |
536 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
537 /* |
38 | 538 void pressure_calculation_new(void) |
539 { | |
540 #define POW2_8 (256) | |
541 #define POW2_17 (131072) | |
542 #define POW2_6 (64) | |
543 #define POW2_16 (65536) | |
544 #define POW2_7 (128) | |
545 #define POW2_23 (8388608) | |
546 #define POW2_21 (2097152) | |
547 #define POW2_15 (32768) | |
548 #define POW2_13 (8192) | |
549 #define POW2_37 (137438953472) | |
550 #define POW2_4 (16) | |
551 #define POW2_33 (8589934592) | |
552 #define POW2_3 (8) | |
553 | |
554 int32_t P; // compensated pressure value | |
555 int32_t T; // compensated temperature value | |
556 int32_t dT; // difference between actual and measured temperature | |
557 int64_t OFF; // offset at actual temperature | |
558 int64_t SENS; | |
559 | |
560 int32_t T2; | |
561 int64_t OFF2; | |
562 int64_t SENS2; | |
563 | |
564 dT = ((int32_t)D2) - ((int32_t)C[5]) * POW2_8; | |
565 OFF = ((int64_t)C[2]) * POW2_16 + ((int64_t)dT) * ((int64_t)C[4]) / POW2_7; | |
566 SENS = ((int64_t)C[1]) * POW2_15 + ((int64_t)dT) * ((int64_t)C[3]) / POW2_8; | |
567 | |
568 T = 2000 + (dT * ((int32_t)C[6])) / POW2_23; | |
569 | |
570 | |
571 if(T < 2000) // low temperature | |
572 { | |
573 T2 = 3 * dT * dT; | |
574 T2 /= POW2_33; | |
575 OFF2 = ((int64_t)T) - 2000; | |
576 OFF2 *= OFF2; | |
577 OFF2 *= 3; | |
578 OFF2 /= 2; | |
579 SENS2 = ((int64_t)T) - 2000; | |
580 SENS2 *= SENS2; | |
581 SENS2 *= 5; | |
582 SENS2 /= POW2_3; | |
583 } | |
584 else // high temperature | |
585 { | |
586 T2 = 7 * dT * dT; | |
587 T2 /= POW2_37; | |
588 OFF2 = ((int64_t)T) - 2000; | |
589 OFF2 *= OFF2; | |
590 OFF2 /= POW2_4; | |
591 SENS2 = 0; | |
592 } | |
593 | |
594 T = T - T2; | |
595 OFF = OFF - OFF2; | |
596 SENS = SENS - SENS2; | |
597 | |
598 P = (int32_t)(((((int64_t)D1) * SENS) / POW2_21 - OFF) / POW2_13); | |
599 | |
600 ambient_temperature = ((float)T) / 100; | |
601 ambient_pressure_mbar = ((float)P) / 10; | |
602 } | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
603 */ |
38 | 604 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
605 /* |
38 | 606 void pressure_calculation_old(void) { |
607 // | |
608 double ambient_temperature_centigrad = 0; | |
609 double ambient_pressure_decimbar = 0; | |
610 | |
611 // static for debug | |
612 static int64_t dt = 0; | |
613 static int64_t temp = 0; | |
614 static int64_t ms_off = 0; | |
615 static int64_t sens = 0; | |
616 // | |
617 static int64_t ms_off2 = 0; | |
618 static int64_t sens2 = 0; | |
619 static int64_t t2 = 0; | |
620 | |
621 if((D2 == 0) || (D1 == 0)) | |
622 return; | |
623 // | |
624 | |
625 // dT = D2 - C[5] * POW2_8; | |
626 // T = 2000 + (dT * C[6]) / POW2_23; | |
627 dt = (int64_t)D2 - C5_x_2p8; | |
628 //temp ; // in 10 milliGrad Celcius | |
629 ambient_temperature_centigrad = 2000 + dt * C[6] / 8388608; | |
630 | |
631 | |
632 if(ambient_temperature_centigrad < 2000) // low temperature | |
633 { | |
634 t2 = 3 * dt; | |
635 t2 *= dt; | |
636 t2 /= 8589934592; | |
637 ms_off2 = ambient_temperature_centigrad - 2000; | |
638 ms_off2 *= ms_off2; | |
639 sens2 = ms_off2; | |
640 ms_off2 *= 3; | |
641 ms_off2 /= 2; | |
642 sens2 *= 5; | |
643 sens2 /= 8; | |
644 } | |
645 else // high temperature | |
646 { | |
647 t2 = 7 * dt; | |
648 t2 *= dt; | |
649 t2 /= 137438953472; | |
650 ms_off2 = ambient_temperature_centigrad - 2000; | |
651 ms_off2 *= ms_off2; | |
652 ms_off2 /= 16; | |
653 sens2 = 0; | |
654 } | |
655 | |
656 | |
657 // | |
658 | |
659 // pressure | |
660 // OFF = C[2] * POW2_16 + dT * C[4] / POW2_7; | |
661 // SENS = C[1] * POW2_15 + dT * C[3] / POW2_8; | |
662 ms_off = C[4] * dt; | |
663 ms_off /= 128; | |
664 ms_off += C2_x_2p16; | |
665 // | |
666 sens = C[3] * dt; | |
667 sens /= 256; | |
668 sens += C1_x_2p15; | |
669 | |
670 // 2nd order correction | |
671 ambient_temperature_centigrad -= t2; | |
672 ms_off -= ms_off2; | |
673 sens -= sens2; | |
674 | |
675 ambient_temperature = ambient_temperature_centigrad / 100; | |
676 // P = (D1 * SENS / POW2_21 - OFF) / POW2_13; | |
677 temp = D1 * sens; | |
678 temp /= 2097152; | |
679 temp -= ms_off; | |
680 temp /= 8192; | |
681 ambient_pressure_decimbar = temp; // to float/double | |
682 ambient_pressure_mbar = ambient_pressure_decimbar / 10; | |
683 } | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
684 */ |
38 | 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 */ | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
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 } | |
719 /* | |
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 */ | |
737 |