Mercurial > public > hwos_code
annotate src/p2_deco.c @ 565:0ba88db66492
CHANGE: Limit button sensitivity to 80%
author | heinrichsweikamp |
---|---|
date | Tue, 06 Feb 2018 10:07:38 +0100 |
parents | b7eb98dbd800 |
children | 3febf1cd1bf4 |
rev | line source |
---|---|
0 | 1 // ************************************************************** |
560 | 2 // p2_deco.c REFACTORED VERSION V2.95a2 |
0 | 3 // |
4 // Created on: 12.05.2009 | |
560 | 5 // Author: heinrichs weikamp, contributions by Ralph Lembcke and others |
0 | 6 // |
7 // ************************************************************** | |
8 | |
9 ////////////////////////////////////////////////////////////////////////////// | |
10 // OSTC - diving computer code | |
11 // Copyright (C) 2011 HeinrichsWeikamp GbR | |
12 // | |
13 // This program is free software: you can redistribute it and/or modify | |
14 // it under the terms of the GNU General Public License as published by | |
15 // the Free Software Foundation, either version 3 of the License, or | |
16 // (at your option) any later version. | |
17 // | |
18 // This program is distributed in the hope that it will be useful, | |
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 // GNU General Public License for more details. | |
22 // | |
23 // You should have received a copy of the GNU General Public License | |
24 // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 // | |
26 ////////////////////////////////////////////////////////////////////////////// | |
27 | |
28 // history: | |
29 // 01/03/08 v100: first release candidate | |
30 // 03/13/08 v101: start of programming ppO2 code | |
560 | 31 // 03/13/25 v101a: backup of interim version with ppO2 calculation |
0 | 32 // 03/13/25 v101: open circuit gas change during deco |
33 // 03/13/25 v101: CNS_fraction calculation | |
34 // 03/13/26 v101: optimization of tissue calc routines | |
35 // 07/xx/08 v102a: debug of bottom time routine | |
560 | 36 // 09/xx/08 v102d: Gradient Factor Model implementation |
0 | 37 // 10/10/08 v104: renamed to build v103 for v118 stable |
38 // 10/14/08 v104: integration of char_I_depth_last_deco for Gradient Model | |
39 // 03/31/09 v107: integration of FONT Incon24 | |
40 // 05/23/10 v109: 5 gas changes & 1 min timer | |
41 // 07/13/10 v110: cns vault added | |
42 // 12/25/10 v110: split in three files (deco.c, main.c, definitions.h) | |
43 // 2011/01/20: [jDG] Create a common file included in ASM and C code. | |
44 // 2011/01/24: [jDG] Make ascenttime an short. No more overflow! | |
45 // 2011/01/25: [jDG] Fusion deco array for both models. | |
46 // 2011/01/25: [jDG] Use CF(54) to reverse deco order. | |
47 // 2011/02/11: [jDG] Reworked gradient-factor implementation. | |
48 // 2011/02/15: [jDG] Fixed inconsistencies introduced by gas switch delays. | |
49 // 2011/03/21: [jDG] Added gas consumption (CF56 & CF57) evaluation for OCR mode. | |
50 // 2011/04/15: [jDG] Store low_depth in 32bits (w/o rounding), for a better stability. | |
560 | 51 // 2011/04/25: [jDG] Added 1mn mode for CNS calculation, to allow it for deco planning. |
0 | 52 // 2011/04/27: [jDG] Fixed char_O_gradient_factor calculation when model uses gradient-factor. |
53 // 2011/05/02: [jDG] Added "Future TTS" function (CF58). | |
54 // 2011/05/17: [jDG] Various cleanups. | |
55 // 2011/08/08: [jDG] Computes CNS during deco planning ascent. | |
56 // 2011/11/24: [jDG] Slightly faster and better NDL computation. | |
57 // 2011/12/17: [mH] Remove of the useless debug stuff | |
58 // 2012/02/24: [jDG] Remove missed stop bug. | |
59 // 2012/02/25: [jDG] Looking for a more stable LOW grad factor reference. | |
60 // 2012/09/10: [mH] Fill char_O_deco_time_for_log for logbook write | |
560 | 61 // 2012/10/05: [jDG] Better gas_volumes accuracy (average depth, switch between stop). |
0 | 62 // 2013/03/05: [jDG] Should vault low_depth too. |
63 // 2013/03/05: [jDG] Wrobell remark: ascent_to_first_stop works better with finer steps (2sec). | |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
64 // 2013/05/08: [jDG] A. Salm remark: NOAA tables for CNS are in ATA, not bar. |
560 | 65 // 2013/12/21: [jDG] Fix CNS calculation in deco plan w/o marked gas switch |
66 // 2014/06/16: [jDG] Fix Helium diluent. Fix volumes with many travel mix. | |
324
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
67 // 2014/06/29: [mH] Compute int_O_ceiling |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
68 // 2015/06/12: [jDG] Fix NDL prediction while desaturating with the Buhlmann model. |
560 | 69 // 2017/08/04: [mH] Switch to absolute GF everywhere and apply safety margin parameters to both models (GF and non-GF), fixes from Ralph Lembcke |
70 // 2017/10/31: [rl] enhancements for pSCR mode and introduction of 2nd deco plan computation | |
71 // 2017/12/31: [rl] completion of 2nd deco plan computation and various up-fixes | |
0 | 72 // |
73 // | |
74 // Literature: | |
313 | 75 // Buhlmann, Albert: Tauchmedizin; 4. Auflage [2002]; |
0 | 76 // Schr"oder, Kai & Reith, Steffen; 2000; S"attigungsvorg"ange beim Tauchen, das Modell ZH-L16, Funktionsweise von Tauchcomputern; http://www.achim-und-kai.de/kai/tausim/saett_faq |
77 // Morrison, Stuart; 2000; DIY DECOMPRESSION; http://www.lizardland.co.uk/DIYDeco.html | |
78 // Balthasar, Steffen; Dekompressionstheorie I: Neo Haldane Modelle; http://www.txfreak.de/dekompressionstheorie_1.pdf | |
79 // Baker, Erik C.; Clearing Up The Confusion About "Deep Stops" | |
80 // Baker, Erik C.; Understanding M-values; http://www.txfreak.de/understanding_m-values.pdf | |
81 // | |
82 // | |
83 | |
84 // ********************* | |
85 // ** I N C L U D E S ** | |
86 // ********************* | |
87 #include <math.h> | |
88 | |
89 // *********************************************** | |
90 // ** V A R I A B L E S D E F I N I T I O N S ** | |
91 // *********************************************** | |
92 | |
93 #include "p2_definitions.h" | |
560 | 94 #define TEST_MAIN |
0 | 95 #include "shared_definitions.h" |
96 | |
560 | 97 |
98 // ambient pressure at different mountain heights | |
99 #define P_ambient_1000m 0.880 // [bar] based on 990 hPa and 20°C at sea level, 15°C at altitude | |
100 #define P_ambient_2000m 0.782 // [bar] | |
101 #define P_ambient_3000m 0.695 // [bar] | |
102 | |
103 // ambient pressure in aircraft cabin during flying - worst case according to Buhlmann | |
104 #define P_ambient_fly 0.600 // [bar], 0.600 bar is the value used by Buhlmann for his flying-after-diving calculations | |
105 // 0.735 bar is a typical cabin pressure for nowadays commercial jet aircrafts | |
106 // ----- | |
107 // 0.135 bar safety margin | |
108 | |
109 // constants and factors | |
110 #define ppWater 0.0627 // water vapor partial pressure in the lungs | |
111 #define METER_TO_BAR 0.09985 // conversion factor | |
112 #define BAR_TO_METER 10.0150 // conversion factor (1.0/METER_TO_BAR) | |
113 #define SURFACE_DESAT_FACTOR 0.7042 // surface desaturation safety factor | |
114 #define HYST 1.0E-06 // threshold for tissue graphics on-gassing / off-gassing visualization | |
115 | |
116 // thresholds | |
117 #define GF_warning_threshold 100 // threshold for GF warning (attention threshold is current GF_high) | |
118 #define CNS_warning_threshold 100 // threshold for CNS warning | |
119 #define CNS_prewarning_threshold 70 // threshold for CNS attention | |
120 #define ppO2_prewarn_threshold 120 // threshold for ppO2 attention (master warnings come through options_table.asm) | |
121 #define GAS_NEEDS_ATTENTION_THRESHOLD 0.70 // threshold for gas needs attention | |
122 | |
123 // deco engine states and modes - char_O_deco_status | |
124 #define DECO_STATUS_MASK 0x03 | |
125 #define DECO_STATUS_START 0x00 | |
126 #define DECO_STATUS_FINISHED 0x00 | |
127 #define DECO_STATUS_STOPS 0x01 | |
128 #define DECO_STATUS_ASCENT 0x02 | |
129 #define DECO_STATUS_INIT 0x03 | |
130 | |
131 #define DECO_MODE_MASK 0x0C | |
132 #define DECO_MODE_LOOP 0x04 | |
133 #define DECO_MODE_CCR 0x04 // to be used with == operator in combination with DECO_MODE_MASK only! | |
134 #define DECO_MODE_PSCR 0x08 | |
135 | |
136 #define DECO_PLAN_ALTERNATE 0x10 | |
137 #define DECO_CNS_CALCULATE 0x20 | |
138 #define DECO_VOLUME_CALCULATE 0x40 | |
139 #define DECO_ASCENT_DELAYED 0x80 | |
140 | |
141 // deco engine states and modes - char_O_main_status | |
142 //#define DECO_MODE_MASK 0x0C | |
143 //#define DECO_MODE_LOOP 0x04 | |
144 //#define DECO_MODE_CCR 0x04 // to be used with == operator in combination with DECO_MODE_MASK only! | |
145 //#define DECO_MODE_PSCR 0x08 | |
146 #define DECO_GASCHANGE_OVRD 0x10 | |
147 #define DECO_BOTTOM_CALCULATE 0x40 | |
148 | |
149 | |
150 // deco engine warnings | |
151 #define DECO_WARNING_IBCD 0x01 | |
152 #define DECO_WARNING_IBCD_lock 0x02 | |
153 #define DECO_WARNING_MBUBBLES 0x04 | |
154 #define DECO_WARNING_MBUBBLES_lock 0x08 | |
155 #define DECO_WARNING_OUTSIDE 0x10 | |
156 #define DECO_WARNING_OUTSIDE_lock 0x20 | |
157 #define DECO_WARNING_STOPTABLE_OVERFLOW 0x40 | |
158 #define DECO_FLAG 0x80 | |
159 | |
160 // flags used with integer numbers | |
161 #define INT_FLAG_INVALID 0x0400 | |
162 #define INT_FLAG_ZERO 0x0800 | |
163 #define INT_FLAG_LOW 0x1000 | |
164 #define INT_FLAG_HIGH 0x2000 | |
165 #define INT_FLAG_PREWARNING 0x4000 | |
166 #define INT_FLAG_WARNING 0x8000 | |
167 | |
168 | |
0 | 169 |
170 // ************************* | |
171 // ** P R O T O T Y P E S ** | |
172 // ************************* | |
173 | |
174 static void calc_hauptroutine(void); | |
175 static void calc_hauptroutine_data_input(void); | |
176 static void calc_hauptroutine_update_tissues(void); | |
177 static void calc_hauptroutine_calc_deco(void); | |
560 | 178 static void calc_tissue(void); |
179 static void calc_limit(void); | |
180 static void calc_nullzeit(void); | |
181 static void calc_ascenttime(void); | |
182 static void calc_dive_interval(void); | |
183 static void calc_gradient_factor(void); | |
184 static void calc_wo_deco_step_1_min(void); | |
185 static void calc_desaturation_time(void); | |
186 | |
187 static void sim_extra_time(void); | |
0 | 188 static void sim_ascent_to_first_stop(void); |
560 | 189 static void sim_limit(PARAMETER float GF_current); |
190 | |
191 static void update_startvalues(void); | |
0 | 192 static void gas_switch_set(void); |
560 | 193 static void compute_CNS_for_display(void); |
194 | |
195 static void clear_deco_table(void); | |
196 static void clear_tissue(void); | |
197 | |
198 static unsigned char gas_find_better(void); | |
0 | 199 static unsigned char calc_nextdecodepth(void); |
560 | 200 static unsigned char update_deco_table(PARAMETER unsigned char time_increment); |
201 | |
0 | 202 |
203 //---- Bank 5 parameters ----------------------------------------------------- | |
204 #ifndef UNIX | |
205 # pragma udata bank5=0x500 | |
206 #endif | |
207 | |
560 | 208 // general deco parameters |
209 | |
210 static float GF_low; // initialized from deco parameters, constant during all computations | |
211 static float GF_high; // initialized from deco parameters, constant during all computations | |
212 static float GF_delta; // initialized from deco parameters, constant during all computations | |
213 static float locked_GF_step_norm; // GF_delta / low_depth_norm in normal plan | |
214 static float locked_GF_step_alt; // GF_delta / low_depth_alt in alternative plan | |
215 | |
216 static float low_depth_norm; // Depth of deepest stop in normal plan | |
217 static float low_depth_alt; // Depth of deepest stop in alternative plan | |
218 | |
219 static float float_ascent_speed; // ascent speed from options_table (1.0 .. 10.0 m/min) | |
220 static float float_saturation_multiplier; // safety factor for on-gassing rates | |
221 static float float_desaturation_multiplier; // safety factor for off-gassing rates | |
222 static float float_deco_distance; // additional depth below stop depth for tissue, CNS and gas volume calculation | |
223 | |
224 | |
225 // real context: what we are doing now. | |
226 | |
227 static float calc_lead_tissue_limit; // minimum tolerated ambient pressure by Buhlmann model | |
228 static float CNS_fraction; // current CNS (1.00 = 100%) | |
229 | |
230 static unsigned short deco_tissue_vector; // 32 bit vector to memories all tissues that are in decompression | |
231 static unsigned short IBCD_tissue_vector; // 32 bit vector to memories all tissues that experience IBCD | |
232 | |
233 // simulation context: used to predict ascent. | |
234 | |
235 static float sim_lead_tissue_limit; // minimum tolerated ambient pressure by Buhlmann model | |
236 static float CNS_sim_norm_fraction; // CNS at end of dive in normal plan | |
237 static float CNS_sim_alt_fraction; // CNS at end of dive in alternative plan | |
238 | |
239 static unsigned char temp_depth_limit; // depth of next stop in meters, used in deco calculations | |
240 static unsigned char sim_lead_tissue_no; // Leading compartment number | |
241 static unsigned char split_N2_He[NUM_COMP]; // used for calculating the desaturation time | |
242 | |
243 | |
244 // stops table | |
245 | |
246 static unsigned char internal_deco_depth[NUM_STOPS]; // depth of the stop | |
247 static unsigned char internal_deco_time[NUM_STOPS]; // duration of the stop | |
248 static unsigned char internal_deco_gas[NUM_STOPS]; // gas used at the stop | |
249 | |
250 | |
251 // transfer variables between calc_desaturation_time() and calc_desaturation_time_helper() | |
252 | |
253 static float desat_factor; // used to cache a pre-computed factor | |
254 static float var_ht; // buffer for a half-time factor | |
255 static float pres_target; // target pressure for a compartment | |
256 static float pres_actual; // current pressure of the compartment | |
257 static unsigned short short_time; // time it takes for the compartment to reach the target pressure | |
258 | |
259 // transfer variables between gas_volumes() and gas_volumes_helper() | |
260 static float float_depth; // depth of the stop or half-way point | |
261 static float float_time; // duration of the stop or ascent phase | |
262 static float volume; // computed volume of gas | |
263 static unsigned char usage; // gas usage in l/min | |
264 | |
265 | |
266 // 44 byte free space left in this bank | |
267 | |
0 | 268 |
269 //---- Bank 6 parameters ----------------------------------------------------- | |
270 #ifndef UNIX | |
271 # pragma udata bank6=0x600 | |
272 #endif | |
273 | |
560 | 274 // indexing and sequencing |
275 | |
276 static unsigned char ci; // used as index to the Buhlmann tables | |
277 static unsigned char twosectimer = 0; // used for timing the tissue updating | |
278 static unsigned char tissue_increment; // Selector for real/simulated tissues and time increment | |
279 | |
280 | |
281 // environmental and gas data | |
282 | |
283 static float pres_respiration; // current depth in absolute pressure | |
284 static float pres_surface; // absolute pressure at the surface | |
285 static float temp_deco; // simulated current depth in abs.pressure, used for deco calculations | |
286 | |
287 static unsigned char bottom_depth; // bottom depth in meters, used by CNS and gas needs calculation | |
288 | |
289 static float O2_ratio; // real breathed gas oxygen ratio | |
290 static float N2_ratio; // real breathed gas nitrogen ratio | |
291 static float He_ratio; // real breathed gas helium ratio | |
292 | |
293 static float calc_O2_ratio; // simulated breathed gas oxygen ratio | |
294 static float calc_N2_ratio; // simulated breathed gas nitrogen ratio | |
295 static float calc_He_ratio; // simulated breathed gas helium ratio | |
296 | |
297 static float O2_ppO2; // ppO2 - calculated for pure oxygen at current depth | |
298 static float pSCR_ppO2; // ppO2 - calculated for breathed from pSCR loop | |
299 static float pure_ppO2; // ppO2 - calculated for breathed in OC mode | |
300 | |
301 static unsigned char char_actual_ppO2; // ppO2 - assumed to be breathed, as integer 100 = 1.00 bar | |
302 | |
303 static float breathed_ppO2; // partial pressure of breathed oxygen | |
304 static float ppN2; // partial pressure of breathed nitrogen | |
305 static float ppHe; // partial pressure of breathed helium | |
306 | |
307 | |
308 // Buhlmann model parameters | |
309 | |
310 static float var_N2_a; // Buhlmann a, for current N2 tissue | |
311 static float var_N2_b; // Buhlmann b, for current N2 tissue | |
312 static float var_He_a; // Buhlmann a, for current He tissue | |
313 static float var_He_b; // Buhlmann b, for current He tissue | |
314 static float var_N2_e; // exposition, for current N2 tissue | |
315 static float var_He_e; // exposition, for current He tissue | |
316 static float var_N2_ht; // half-time for current N2 tissue | |
317 static float var_He_ht; // half-time for current N2 tissue | |
318 | |
319 | |
320 // gas switch history | |
321 | |
322 static unsigned char sim_gas_first_used; // Number of first used gas, for bottom segment | |
323 static unsigned char sim_gas_last_used; // number of last used gas | |
324 static unsigned char sim_gas_last_depth; // change depth of last used gas | |
325 | |
326 | |
327 // vault to back-up & restore tissue data | |
328 | |
329 static float pres_tissue_N2_vault[NUM_COMP]; // stores the nitrogen tissue pressures | |
330 static float pres_tissue_He_vault[NUM_COMP]; // stores the helium tissue pressures | |
331 static float low_depth_norm_vault; // stores a parameter of the GF model for normal plan | |
332 static float low_depth_alt_vault; // stores a parameter of the GF model for alternative plan | |
333 static float cns_vault_float; // stores current CNS (float representation) | |
334 | |
335 static unsigned int cns_vault_int; // stores current CNS (integer representation) | |
336 static unsigned char deco_warnings_vault; // stores warnings status | |
337 | |
338 | |
339 // auxiliary variables for local data buffering | |
340 | |
341 static float N2_equilibrium; // used for N2 tissue graphics scaling | |
342 static float temp_tissue; // auxiliary variable to buffer tissue pressures | |
343 | |
344 | |
345 // 6 byte free space left in this bank | |
346 | |
0 | 347 |
348 //---- Bank 7 parameters ----------------------------------------------------- | |
349 #ifndef UNIX | |
350 # pragma udata bank7=0x700 | |
351 #endif | |
560 | 352 |
353 // Keep order and position of the variables in bank 7 as they are backed-up to & restored from EEPROM | |
354 | |
355 float pres_tissue_N2[NUM_COMP]; // 16 floats = 64 bytes | |
356 float pres_tissue_He[NUM_COMP]; // 16 floats = 64 bytes | |
357 float sim_pres_tissue_N2[NUM_COMP]; // 16 floats = 64 bytes | |
358 float sim_pres_tissue_He[NUM_COMP]; // 16 floats = 64 bytes | |
359 | |
0 | 360 |
361 //---- Bank 8 parameters ----------------------------------------------------- | |
362 #ifndef UNIX | |
363 # pragma udata overlay bank8=0x800 | |
560 | 364 |
365 static char md_pi_subst[256]; // Overlay C-code data stack here, too. | |
366 | |
367 # define C_STACK md_pi_subst | |
0 | 368 #endif |
369 | |
370 // Back to bank6 for further tmp data | |
371 #ifndef UNIX | |
372 # pragma udata bank6 | |
373 #endif | |
374 | |
375 ////////////////////////////////////////////////////////////////////////////// | |
376 ////////////////////////////////////////////////////////////////////////////// | |
377 ///////////////////////////// THE LOOKUP TABLES ////////////////////////////// | |
378 ////////////////////////////////////////////////////////////////////////////// | |
379 ////////////////////////////////////////////////////////////////////////////// | |
380 // | |
381 // End of PROM code is 17F00, So push tables on PROM top... | |
382 // | |
383 #ifndef UNIX | |
560 | 384 # pragma romdata Buhlmann_tables = 0x1DD00 // Needs to be in UPPER bank. |
0 | 385 #endif |
386 | |
560 | 387 rom const float Buhlmann_ab[4*16] = { |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
388 // Data ZH-L16C, from Bühlmann Tauchmedizin 2002, option 1a (4mn) |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
389 // a for N2 b for N2 a of He b for He |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
390 1.2599, 0.5050, 1.7424, 0.4245, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
391 1.0000, 0.6514, 1.3830, 0.5747, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
392 0.8618, 0.7222, 1.1919, 0.6527, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
393 0.7562, 0.7825, 1.0458, 0.7223, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
394 0.6200, 0.8126, 0.9220, 0.7582, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
395 0.5043, 0.8434, 0.8205, 0.7957, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
396 0.4410, 0.8693, 0.7305, 0.8279, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
397 0.4000, 0.8910, 0.6502, 0.8553, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
398 0.3750, 0.9092, 0.5950, 0.8757, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
399 0.3500, 0.9222, 0.5545, 0.8903, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
400 0.3295, 0.9319, 0.5333, 0.8997, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
401 0.3065, 0.9403, 0.5189, 0.9073, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
402 0.2835, 0.9477, 0.5181, 0.9122, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
403 0.2610, 0.9544, 0.5176, 0.9171, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
404 0.2480, 0.9602, 0.5172, 0.9217, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
405 0.2327, 0.9653, 0.5119, 0.9267 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
406 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
407 |
560 | 408 rom const float Buhlmann_ht[2*16] = { |
409 // Compartment half-life, in minute | |
410 //--- N2 ---- He ---------------------- | |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
411 4.0, 1.51, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
412 8.0, 3.02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
413 12.5, 4.72, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
414 18.5, 6.99, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
415 27.0, 10.21, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
416 38.3, 14.48, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
417 54.3, 20.53, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
418 77.0, 29.11, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
419 109.0, 41.20, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
420 146.0, 55.19, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
421 187.0, 70.69, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
422 239.0, 90.34, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
423 305.0, 115.29, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
424 390.0, 147.42, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
425 498.0, 188.24, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
426 635.0, 240.03 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
427 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
428 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
429 rom const float e2secs[2*16] = { |
560 | 430 // result of 1 - 2^(-1/(2sec*HT)) |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
431 //---- N2 ------------- He ------------ |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
432 5.75958E-03, 1.51848E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
433 2.88395E-03, 7.62144E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
434 1.84669E-03, 4.88315E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
435 1.24813E-03, 3.29997E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
436 8.55371E-04, 2.26041E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
437 6.03079E-04, 1.59437E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
438 4.25414E-04, 1.12479E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
439 3.00019E-04, 7.93395E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
440 2.11949E-04, 5.60641E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
441 1.58240E-04, 4.18555E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
442 1.23548E-04, 3.26795E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
443 9.66686E-05, 2.55722E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
444 7.57509E-05, 2.00387E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
445 5.92416E-05, 1.56716E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
446 4.63943E-05, 1.22734E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
447 3.63850E-05, 9.62538E-05 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
448 //------------------------------------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
449 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
450 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
451 rom const float e1min[2*16] = { |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
452 // Integration constant for 1 minute, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
453 // Ie. 1- 2^(-1/HT) |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
454 //----- N2 --------- e 1min He -------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
455 1.59104E-01, 3.68109E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
456 8.29960E-02, 2.05084E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
457 5.39424E-02, 1.36579E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
458 3.67742E-02, 9.44046E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
459 2.53454E-02, 6.56359E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
460 1.79351E-02, 4.67416E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
461 1.26840E-02, 3.31991E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
462 8.96152E-03, 2.35301E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
463 6.33897E-03, 1.66832E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
464 4.73633E-03, 1.24808E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
465 3.69981E-03, 9.75753E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
466 2.89600E-03, 7.64329E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
467 2.27003E-03, 5.99417E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
468 1.77572E-03, 4.69082E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
469 1.39089E-03, 3.67548E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
470 1.09097E-03, 2.88359E-03 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
471 //------------------------------------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
472 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
473 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
474 rom const float e10min[2*16] = { |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
475 // The 10 min Value in float notation: |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
476 // result of 1 - 2^(-10/ht) |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
477 //---- N2 -------------- He ----------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
478 8.23223E-01, 9.89851E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
479 5.79552E-01, 8.99258E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
480 4.25651E-01, 7.69737E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
481 3.12487E-01, 6.29027E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
482 2.26416E-01, 4.92821E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
483 1.65547E-01, 3.80407E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
484 1.19840E-01, 2.86538E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
485 8.60863E-02, 2.11886E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
486 6.16117E-02, 1.54849E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
487 4.63665E-02, 1.18026E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
488 3.63881E-02, 9.34005E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
489 2.85855E-02, 7.38569E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
490 2.24698E-02, 5.83504E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
491 1.76160E-02, 4.59303E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
492 1.38222E-02, 3.61528E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
493 1.08563E-02, 2.84646E-02 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
494 //------------------------------------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
495 }; |
0 | 496 |
497 ////////////////////////////////////////////////////////////////////////////// | |
498 ////////////////////////////////////////////////////////////////////////////// | |
499 ////////////////////////////// THE SUBROUTINES /////////////////////////////// | |
500 ////////////////////////////////////////////////////////////////////////////// | |
501 ////////////////////////////////////////////////////////////////////////////// | |
502 // | |
503 // all new in v.102 | |
504 // moved from 0x0D000 to 0x0C000 in v.108 | |
505 #ifndef UNIX | |
506 # pragma code p2_deco = 0x0C000 | |
507 #endif | |
508 | |
509 ////////////////////////////////////////////////////////////////////////////// | |
510 ////////////////////////////////////////////////////////////////////////////// | |
511 /////////////////////// U T I L I T I E S ///////////////////////////////// | |
512 ////////////////////////////////////////////////////////////////////////////// | |
513 ////////////////////////////////////////////////////////////////////////////// | |
514 | |
515 ////////////////////////////////////////////////////////////////////////////// | |
516 // Bump to blue-screen when an assert is wrong | |
517 #ifdef __DEBUG | |
518 void assert_failed(PARAMETER short int line) | |
519 { | |
520 } | |
521 #endif | |
522 | |
523 ////////////////////////////////////////////////////////////////////////////// | |
524 // When calling C code from ASM context, the data stack pointer and | |
525 // frames should be reset. Bank8 is used by stack | |
526 | |
527 #ifdef CROSS_COMPILE | |
528 # define RESET_C_STACK | |
529 #else | |
530 # ifdef __DEBUG | |
531 # define RESET_C_STACK fillDataStack(); | |
532 void fillDataStack(void) | |
533 { | |
534 _asm | |
535 LFSR 1,C_STACK | |
536 MOVLW 0xCC | |
537 loop: MOVWF POSTINC1,0 | |
538 TSTFSZ FSR1L,0 | |
539 BRA loop | |
540 | |
541 LFSR 1,C_STACK | |
542 LFSR 2,C_STACK | |
543 _endasm | |
544 } | |
545 # else | |
546 # define RESET_C_STACK \ | |
547 _asm \ | |
548 LFSR 1, C_STACK \ | |
549 LFSR 2, C_STACK \ | |
550 _endasm | |
551 # endif | |
552 #endif | |
553 | |
554 ////////////////////////////////////////////////////////////////////////////// | |
555 // Fast subroutine to read timer 5. | |
560 | 556 // Note: result is in 1/32 of milliseconds (30,51757813 us/bit to be precise) |
0 | 557 static unsigned short tmr5(void) |
558 { | |
559 #ifndef CROSS_COMPILE | |
560 _asm | |
561 movff 0xf7c,PRODL // TMR5L | |
562 movff 0xf7d,PRODH // TMR5H | |
563 _endasm // result in PRODH:PRODL. | |
564 #else | |
565 return 0; | |
566 #endif | |
567 } | |
568 | |
569 ////////////////////////////////////////////////////////////////////////////// | |
560 | 570 // read Buhlmann tables A and B for compartment ci |
0 | 571 // |
560 | 572 static void read_Buhlmann_coefficients(void) |
0 | 573 { |
574 #ifndef CROSS_COMPILE | |
575 // Note: we don't use far rom pointer, because the | |
576 // 24 bits is too complex, hence we have to set | |
577 // the UPPER page ourself... | |
578 // --> Set zero if tables are moved to lower pages ! | |
579 _asm | |
580 movlw 1 | |
581 movwf TBLPTRU,0 | |
582 _endasm | |
583 #endif | |
584 | |
585 assert( ci < NUM_COMP ); | |
586 | |
587 // Use an interleaved array (AoS) to access coefficients with a | |
588 // single addressing. | |
589 { | |
560 | 590 overlay rom const float* ptr = &Buhlmann_ab[4*ci]; |
0 | 591 var_N2_a = *ptr++; |
592 var_N2_b = *ptr++; | |
593 var_He_a = *ptr++; | |
594 var_He_b = *ptr++; | |
595 } | |
596 } | |
597 | |
598 ////////////////////////////////////////////////////////////////////////////// | |
560 | 599 // read Buhlmann tables for compartment ci |
0 | 600 // If period == 0 : 2sec interval |
601 // 1 : 1 min interval | |
602 // 2 : 10 min interval. | |
560 | 603 static void read_Buhlmann_times(PARAMETER char period) |
0 | 604 { |
605 #ifndef CROSS_COMPILE | |
606 // Note: we don't use far rom pointer, because the | |
607 // 24 bits is to complex, hence we have to set | |
608 // the UPPER page ourself... | |
609 // --> Set zero if tables are moved to lower pages ! | |
610 _asm | |
611 movlw 1 | |
612 movwf TBLPTRU,0 | |
613 _endasm | |
614 #endif | |
615 | |
616 assert( ci < NUM_COMP ); | |
617 | |
618 // Integration intervals. | |
619 switch(period) | |
620 { | |
621 case 0: //---- 2 sec ----------------------------------------------------- | |
622 { | |
623 overlay rom const float* ptr = &e2secs[2*ci]; | |
624 var_N2_e = *ptr++; | |
625 var_He_e = *ptr++; | |
626 } | |
627 break; | |
628 | |
629 case 1: //---- 1 min ----------------------------------------------------- | |
630 { | |
631 overlay rom const float* ptr = &e1min[2*ci]; | |
632 var_N2_e = *ptr++; | |
633 var_He_e = *ptr++; | |
634 } | |
635 break; | |
636 | |
637 case 2: //---- 10 min ---------------------------------------------------- | |
638 { | |
639 overlay rom const float* ptr = &e10min[2*ci]; | |
640 var_N2_e = *ptr++; | |
641 var_He_e = *ptr++; | |
642 } | |
643 break; | |
644 | |
645 default: | |
646 assert(0); // Never go there... | |
647 } | |
648 } | |
649 | |
650 ////////////////////////////////////////////////////////////////////////////// | |
560 | 651 // read Buhlmann tables for compartment ci |
0 | 652 // |
560 | 653 static void read_Buhlmann_ht(void) |
0 | 654 { |
655 | |
656 #ifndef CROSS_COMPILE | |
657 // Note: we don't use far rom pointer, because the | |
658 // 24 bits is to complex, hence we have to set | |
659 // the UPPER page ourself... | |
660 // --> Set zero if tables are moved to lower pages ! | |
661 _asm | |
662 movlw 1 | |
663 movwf TBLPTRU,0 | |
664 _endasm | |
665 #endif | |
666 | |
667 assert( ci < NUM_COMP ); | |
668 { | |
560 | 669 overlay rom const float* ptr = &Buhlmann_ht[2*ci]; |
0 | 670 var_N2_ht = *ptr++; |
671 var_He_ht = *ptr++; | |
672 } | |
673 | |
674 assert( 4.0 <= var_N2_ht && var_N2_ht <= 635.0 ); | |
675 assert( 1.5099 <= var_He_ht && var_He_ht <= 240.03 ); | |
676 } | |
677 | |
678 ////////////////////////////////////////////////////////////////////////////// | |
679 // calc_nextdecodepth | |
680 // | |
681 // new in v.102 | |
682 // | |
683 // INPUT, changing during dive: | |
560 | 684 // temp_deco : current depth in absolute pressure |
0 | 685 // |
686 // INPUT, fixed during dive: | |
687 // pres_surface | |
688 // GF_delta | |
689 // GF_high | |
690 // GF_low | |
691 // char_I_depth_last_deco | |
692 // | |
560 | 693 // MODIFIED |
694 // locked_GF_step_norm/_alt : used for GF model | |
695 // low_depth_norm/_alt : used for GF model | |
0 | 696 // |
697 // OUTPUT | |
560 | 698 // temp_depth_limit : depth of next stop in meters (if RETURN == true ) |
699 // depth we can ascent to without stop (if RETURN == false) | |
700 // | |
701 // RETURN TRUE if a stop is needed. | |
0 | 702 // |
703 static unsigned char calc_nextdecodepth(void) | |
704 { | |
560 | 705 overlay unsigned char need_stop; |
706 | |
707 // compute current depth in meters | |
0 | 708 overlay float depth = (temp_deco - pres_surface) * BAR_TO_METER; |
709 | |
560 | 710 // compute depth in meters after 1 minute of ascent at float_ascent_speed (5..10 m/min) |
711 overlay float min_depth = (depth > float_ascent_speed) ? (depth - float_ascent_speed) : 0.0; | |
712 | |
713 | |
714 // allow for 200mbar of weather dependent surface pressure change | |
715 assert( depth >= -0.2 ); | |
716 | |
717 | |
718 //---- check if a stop is needed for deco reasons ---------------------------- | |
719 | |
720 // switch on deco model | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
721 if( char_I_deco_model != 0 ) |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
722 { |
560 | 723 //---- ZH-L16 + GRADIENT FACTOR Model ------------------------------------ |
724 | |
725 overlay float locked_GF_step; | |
726 overlay float low_depth; | |
727 overlay float pres_gradient; | |
728 | |
0 | 729 overlay unsigned char first_stop = 0; |
560 | 730 |
731 | |
732 // calculate minimum depth we can ascent to in absolute pressure | |
0 | 733 sim_limit( GF_low ); |
560 | 734 |
735 // ...and convert the depth into relative pressure | |
736 pres_gradient = sim_lead_tissue_limit - pres_surface; | |
737 | |
738 // check if we can surface directly | |
739 if( pres_gradient <= 0.0 ) | |
740 { | |
741 min_depth = 0.0; // set minimum depth to 0 meters = surface | |
742 goto no_deco_stop; // done. | |
743 } | |
744 | |
745 // convert minimum depth we can ascent to from relative pressure to depth in meters | |
746 pres_gradient *= BAR_TO_METER; | |
747 | |
748 // recall low_depth dependent on current plan | |
749 low_depth = (char_O_deco_status & DECO_PLAN_ALTERNATE) ? low_depth_alt : low_depth_norm; | |
750 | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
751 // Store the deepest point needing a deco stop as the LOW reference for GF. |
560 | 752 // NOTE: following stops will be validated using this LOW-HIGH GF scale, |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
753 // so if we want to keep coherency, we should not validate this stop |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
754 // yet, but apply the search to it, as for all the following stops afterward. |
560 | 755 if( pres_gradient > low_depth ) |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
756 { |
560 | 757 // update GF parameters |
758 low_depth = pres_gradient; | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
759 locked_GF_step = GF_delta / low_depth; |
560 | 760 |
761 // store updated GF parameters dependent on current plan | |
762 if( char_O_deco_status & DECO_PLAN_ALTERNATE ) | |
763 { | |
764 low_depth_alt = low_depth; | |
765 locked_GF_step_alt = locked_GF_step; | |
766 } | |
767 else | |
768 { | |
769 low_depth_norm = low_depth; | |
770 locked_GF_step_norm = locked_GF_step; | |
771 } | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
772 } |
560 | 773 else |
774 { | |
775 // recall locked_GF_step dependent on current plan | |
776 locked_GF_step = (char_O_deco_status & DECO_PLAN_ALTERNATE) ? locked_GF_step_alt : locked_GF_step_norm; | |
777 } | |
778 | |
779 // invalidate this stop if we can ascent for 1 minute without going above minimum required deco depth | |
780 if( pres_gradient < min_depth ) goto no_deco_stop; | |
781 | |
782 | |
783 // if program execution passes here, we need a deco stop | |
784 | |
785 // Round to multiple of 3 meters | |
786 first_stop = 3 * (unsigned char)(0.9995 + pres_gradient * 0.333333); | |
787 | |
788 // check a constraint | |
0 | 789 assert( first_stop < 128 ); |
790 | |
560 | 791 // apply correction for the shallowest stop, use char_I_depth_last_deco (3..6 m) instead |
792 if( first_stop == 3 ) first_stop = char_I_depth_last_deco; | |
0 | 793 |
794 // We have a stop candidate. | |
795 // But maybe ascending to the next stop will diminish the constraint, | |
560 | 796 // because the GF might decrease more than the pressure gradient... |
0 | 797 while(first_stop > 0) |
798 { | |
560 | 799 // Next depth |
800 overlay unsigned char next_stop; | |
801 | |
802 // invalidate this stop if we can ascent one more minute without going above minimum required deco depth | |
803 if( first_stop <= (unsigned char)min_depth ) goto no_deco_stop; | |
804 | |
805 // compute depth of next stop | |
806 if ( first_stop <= char_I_depth_last_deco ) next_stop = 0; | |
807 else if ( first_stop == 6 ) next_stop = char_I_depth_last_deco; | |
808 else next_stop = first_stop - 3; | |
809 | |
810 // compute total pressure at the new stop candidate | |
811 pres_gradient = next_stop * METER_TO_BAR + pres_surface; | |
812 | |
813 // compute limit for the new stop candidate | |
814 if( (low_depth == 0.0) || (next_stop > low_depth) ) sim_limit( GF_low ); | |
815 else sim_limit( GF_high - next_stop * locked_GF_step ); | |
816 | |
817 // check if ascent to the next stop candidate is possible | |
818 if( sim_lead_tissue_limit >= pres_gradient ) goto deco_stop_found; // no - ascent to next_stop forbidden | |
819 | |
820 // else, validate that stop and loop... | |
0 | 821 first_stop = next_stop; |
822 } | |
823 | |
824 no_deco_stop: | |
560 | 825 need_stop = 0; // set flag for stop needed to 'no' |
826 temp_depth_limit = (unsigned char)min_depth; // report depth we can ascent to without stop | |
827 goto done; | |
0 | 828 |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
829 deco_stop_found: |
560 | 830 need_stop = 1; // set flag for stop needed to 'yes' |
831 temp_depth_limit = (unsigned char)first_stop; // stop depth, in meters | |
0 | 832 |
833 done: | |
834 ; | |
835 } | |
560 | 836 else |
0 | 837 { |
560 | 838 //---- ZH-L16 model ------------------------------------------------- |
839 | |
0 | 840 overlay float pres_gradient; |
841 | |
560 | 842 |
843 // calculate minimum depth we can ascent to in absolute pressure | |
0 | 844 sim_limit(1.0); |
845 | |
560 | 846 // ...and convert the depth into relative pressure |
0 | 847 pres_gradient = sim_lead_tissue_limit - pres_surface; |
560 | 848 |
849 // check if we can surface directly | |
0 | 850 if (pres_gradient >= 0) |
851 { | |
560 | 852 // no - set flag for stop needed to 'yes' |
853 need_stop = 1; | |
854 | |
855 // convert stop depth in relative pressure to stop index | |
856 pres_gradient *= BAR_TO_METER / 3; | |
857 | |
858 // convert stop index to depth in meters, rounded to multiple of 3 meters | |
859 temp_depth_limit = 3 * (short) (pres_gradient + 0.99); | |
860 | |
861 // correct last stop to 4m/5m/6m | |
862 if( temp_depth_limit == 3 ) temp_depth_limit = char_I_depth_last_deco; | |
0 | 863 } |
864 else | |
560 | 865 { |
866 // yes - set flag for stop needed to 'no' | |
867 need_stop = 0; | |
868 | |
869 // set depth we can ascent to as 0 = surface | |
0 | 870 temp_depth_limit = 0; |
560 | 871 } |
0 | 872 } |
873 | |
560 | 874 |
875 // After the first deco stop, gas changes are only done at deco stops now! | |
876 | |
877 // check if a stop is found and there is a better gas to switch to | |
878 if( need_stop && gas_find_better() ) | |
879 { | |
880 // set the new calculation ratios for N2, He and O2 | |
881 gas_switch_set(); | |
882 | |
883 // prime the deco stop with the gas change time | |
884 update_deco_table(char_I_gas_change_time); | |
885 } | |
0 | 886 |
887 return need_stop; | |
888 } | |
889 | |
890 ////////////////////////////////////////////////////////////////////////////// | |
891 // copy_deco_table | |
892 // | |
893 // Buffer the stops, once computed, so we can continue to display them | |
894 // while computing the next set. | |
895 // | |
896 static void copy_deco_table(void) | |
897 { | |
898 // Copy depth of the first (deepest) stop, because when reversing | |
899 // order, it will be hard to find... | |
111 | 900 char_O_first_deco_depth = internal_deco_depth[0]; |
0 | 901 char_O_first_deco_time = internal_deco_time [0]; |
902 | |
903 { | |
904 overlay unsigned char x, y; | |
905 | |
906 for(x=0; x<NUM_STOPS; x++) | |
907 { | |
908 char_O_deco_depth[x] = internal_deco_depth[x]; | |
909 char_O_deco_time [x] = internal_deco_time [x]; | |
111 | 910 char_O_deco_gas [x] = internal_deco_gas [x]; |
0 | 911 } |
912 | |
913 //Now fill the char_O_deco_time_for_log array | |
914 //---- First: search the first non-null depth | |
915 for(x=(NUM_STOPS-1); x != 0; --x) | |
916 if( internal_deco_depth[x] != 0 ) break; | |
917 | |
918 //---- Second: copy to output table (in reverse order) | |
919 for(y=0; y<NUM_STOPS; y++, --x) | |
920 { | |
921 char_O_deco_time_for_log[y] = internal_deco_time [x]; | |
922 | |
923 // Stop only once the last transfer is done. | |
924 if( x == 0 ) break; | |
925 } | |
926 | |
927 //---- Third: fill table end with null | |
928 for(y++; y<NUM_STOPS; y++) | |
929 { | |
560 | 930 char_O_deco_time_for_log[y] = 0; |
0 | 931 } |
932 } | |
933 } | |
934 | |
935 ////////////////////////////////////////////////////////////////////////////// | |
560 | 936 // temp_tissue_safety |
0 | 937 // |
938 // outsourced in v.102 | |
939 // | |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
940 // Apply safety factors for both ZH-L16 models. |
0 | 941 // |
942 static void temp_tissue_safety(void) | |
943 { | |
944 assert( 0.0 < float_desaturation_multiplier && float_desaturation_multiplier <= 1.0 ); | |
945 assert( 1.0 <= float_saturation_multiplier && float_saturation_multiplier <= 2.0 ); | |
946 | |
560 | 947 if( temp_tissue < 0.0 ) temp_tissue *= float_desaturation_multiplier; |
948 else temp_tissue *= float_saturation_multiplier; | |
0 | 949 } |
950 | |
951 ////////////////////////////////////////////////////////////////////////////// | |
952 ////////////////////////////////////////////////////////////////////////////// | |
953 // ** THE JUMP-IN CODE ** | |
954 // ** for the asm code ** | |
955 ////////////////////////////////////////////////////////////////////////////// | |
956 ////////////////////////////////////////////////////////////////////////////// | |
957 | |
958 ////////////////////////////////////////////////////////////////////////////// | |
560 | 959 // Called every second during diving. |
960 // updates tissues every second invocation. | |
0 | 961 // |
560 | 962 // Every few seconds (or slower when TTS > 16): |
963 // - updates deco table (char_O_deco_time/depth) with new values. | |
964 // - updates ascent time, | |
965 // - sets status to zero (so we can check there is new results). | |
0 | 966 // |
967 void deco_calc_hauptroutine(void) | |
968 { | |
969 RESET_C_STACK | |
970 calc_hauptroutine(); | |
971 } | |
972 | |
973 ////////////////////////////////////////////////////////////////////////////// | |
974 // Reset decompression model: | |
975 // + Set all tissues to equilibrium with Air at ambient pressure. | |
976 // + Reset last stop to 0m | |
977 // + Reset all model output. | |
978 void deco_clear_tissue(void) | |
979 { | |
980 RESET_C_STACK | |
981 clear_tissue(); | |
982 } | |
983 | |
984 ////////////////////////////////////////////////////////////////////////////// | |
985 | |
986 void deco_calc_wo_deco_step_1_min(void) | |
987 { | |
988 RESET_C_STACK | |
989 calc_wo_deco_step_1_min(); | |
560 | 990 } |
991 | |
992 ////////////////////////////////////////////////////////////////////////////// | |
993 | |
994 void deco_calc_desaturation_time(void) | |
995 { | |
996 RESET_C_STACK | |
997 calc_desaturation_time(); | |
0 | 998 } |
999 | |
1000 ////////////////////////////////////////////////////////////////////////////// | |
1001 | |
1002 void deco_calc_dive_interval(void) | |
1003 { | |
1004 RESET_C_STACK | |
1005 calc_dive_interval(); | |
1006 } | |
1007 | |
1008 ////////////////////////////////////////////////////////////////////////////// | |
560 | 1009 // deco_calc_CNS_decrease_15min |
0 | 1010 // |
560 | 1011 // new in v.101 |
0 | 1012 // |
560 | 1013 // calculates the half time of 90 minutes in 6 steps of 15 min |
1014 // (Used in sleep mode, for low battery mode). | |
0 | 1015 // |
560 | 1016 // Output: int_O_CNS_fraction |
1017 // Uses and Updates: CNS_fraction | |
1018 // | |
1019 void deco_calc_CNS_decrease_15min(void) | |
0 | 1020 { |
560 | 1021 RESET_C_STACK |
1022 | |
1023 // clock down CNS | |
1024 CNS_fraction = 0.890899 * CNS_fraction; | |
1025 | |
1026 // compute integer copy of CNS value | |
1027 compute_CNS_for_display(); | |
1028 } | |
1029 | |
1030 | |
1031 ////////////////////////////////////////////////////////////////////////////// | |
1032 // Find current gas in the list (if any) and get its change depth | |
1033 // | |
1034 // Input: char_I_current_gas : 1..5 or 6 | |
1035 // | |
1036 // Output: sim_gas_last_used : 1..6 or 0 if it is the gas set as FIRST | |
1037 // sim_gas_last_depth : change depth in meters or 0 if it is the gas set as FIRST | |
1038 // | |
1039 static void gas_find_current(void) | |
1040 { | |
1041 assert( 1 <= char_I_current_gas && char_I_current_gas <= 6 ); | |
1042 | |
1043 if( char_I_current_gas <= NUM_GAS ) // Gas 1-5 | |
0 | 1044 { |
560 | 1045 sim_gas_last_used = sim_gas_first_used = char_I_current_gas; |
1046 | |
1047 // If current gas is a deco gas get it's change depth. | |
1048 // Set change depth to 0 if the current gas is the first gas or | |
1049 // a travel/normal gas, i.e. if it can be breathed at "any" depth. | |
1050 if( char_I_deco_gas_change[sim_gas_last_used-1] ) sim_gas_last_depth = char_I_deco_gas_change[sim_gas_last_used-1]; | |
1051 else sim_gas_last_depth = 0; | |
0 | 1052 } |
1053 else | |
560 | 1054 { |
1055 sim_gas_last_used = sim_gas_first_used = 0; // Gas 6 (the manually set one) has number 0 here | |
1056 sim_gas_last_depth = 0; // handle it as a travel/normal gas | |
1057 } | |
1058 } | |
1059 | |
1060 | |
1061 ////////////////////////////////////////////////////////////////////////////// | |
1062 // Find the deco gas with the shallowest change depth beyond current depth | |
1063 // | |
1064 // INPUT temp_depth_limit : current depth in meters | |
1065 // char_I_deco_gas_change[] : change depths of the deco gases | |
1066 // sim_gas_last_depth : change depth of the currently used gas, 0 if on the gas set as FIRST | |
1067 // | |
1068 // OUTPUT sim_gas_last_depth : switch depth - only if return value is true | |
1069 // sim_gas_last_used : index of the gas (1..5) - only if return value is true | |
1070 // | |
1071 // RETURNS TRUE if a better gas is available | |
1072 // | |
1073 static unsigned char gas_find_better(void) | |
1074 { | |
1075 overlay unsigned char switch_depth = 255; | |
1076 overlay unsigned char switch_gas = 0; | |
1077 overlay unsigned char j; | |
1078 | |
1079 | |
1080 // no automatic gas changes in CCR mode and - as of now - in pSCR mode | |
1081 if( char_O_deco_status & DECO_MODE_LOOP ) return 0; | |
1082 | |
1083 // Loop over all deco gases to find the shallowest one below or at current depth. | |
1084 for(j=0; j<NUM_GAS; ++j) | |
1085 { | |
1086 // Is this the gas we are already breathing? | |
1087 // If yes, skip this gas. | |
1088 if( j+1 == sim_gas_last_used ) continue; | |
1089 | |
1090 // Is the change depth of the gas shallower than the current depth? | |
1091 // If yes, skip this gas as it is not to be used yet. | |
1092 // Remark: this check will also skip all disabled gases and the gas set | |
1093 // as 'first' because these have their change depth set to 0. | |
1094 if( temp_depth_limit > char_I_deco_gas_change[j] ) continue; | |
1095 | |
1096 // Is the change depth of the gas deeper than the change depth of the | |
1097 // gas we are currently one? | |
1098 // If yes, skip this gas as it is not better than the current one. | |
1099 // Remark: if there is more than one gas with the same change depth, | |
1100 // the last one from the list will be taken. | |
1101 if( sim_gas_last_depth && (char_I_deco_gas_change[j] > sim_gas_last_depth) ) continue; | |
1102 | |
1103 // Is the change depth of the gas shallower or equal to the change depth | |
1104 // of the best gas found so far, or is it the first better gas found? | |
1105 // If yes, we have a better gas | |
1106 if( char_I_deco_gas_change[j] <= switch_depth ) | |
1107 { | |
1108 switch_gas = j+1; // remember this gas (1..5) | |
1109 switch_depth = char_I_deco_gas_change[j]; // remember its change depth | |
1110 } | |
1111 } // continue looping through all gases to eventually find an even better gas | |
1112 | |
1113 // has a better gas been found? | |
1114 if( switch_gas ) | |
1115 { | |
1116 // yes | |
1117 sim_gas_last_used = switch_gas; // report the index of the better | |
1118 sim_gas_last_depth = switch_depth; // report its change depth | |
1119 | |
1120 assert( sim_gas_last_depth < switch_depth ); | |
1121 | |
1122 return 1; // signal a better gas was found | |
1123 } | |
1124 else | |
1125 { | |
1126 return 0; // signal no better gas was found | |
1127 } | |
0 | 1128 } |
1129 | |
1130 ////////////////////////////////////////////////////////////////////////////// | |
560 | 1131 // Set calc_N2/He/O2_ratios by sim_gas_last_used |
0 | 1132 // |
560 | 1133 // Input: sim_gas_last_used : index of gas to use |
1134 // N2_ratio, He_ratio : if gas 0 = the manually set gas is in use | |
0 | 1135 // |
560 | 1136 // Output: calc_N2_ratio, calc_He_ratio, calc_O2ratio |
0 | 1137 // |
560 | 1138 static void gas_switch_set(void) |
0 | 1139 { |
560 | 1140 assert( 0 <= sim_gas_last_used <= NUM_GAS ); |
1141 | |
1142 if( sim_gas_last_used == 0 ) // Gas6 = manually set gas. | |
1143 { | |
1144 calc_O2_ratio = O2_ratio; | |
1145 calc_He_ratio = He_ratio; | |
1146 } | |
1147 else | |
1148 { | |
1149 calc_O2_ratio = char_I_deco_O2_ratio[sim_gas_last_used-1] * 0.01; | |
1150 calc_He_ratio = char_I_deco_He_ratio[sim_gas_last_used-1] * 0.01; | |
1151 } | |
1152 | |
1153 calc_N2_ratio = 1.0 - calc_O2_ratio - calc_He_ratio; | |
1154 | |
1155 assert( 0.0 <= calc_N2_ratio && calc_N2_ratio <= 0.95 ); | |
1156 assert( 0.0 <= calc_He_ratio && calc_He_ratio <= 1.00 ); | |
1157 assert( (calc_N2_ratio + calc_He_ratio) <= 1.00 ); | |
0 | 1158 } |
1159 | |
1160 ////////////////////////////////////////////////////////////////////////////// | |
560 | 1161 // Compute ppN2 and ppHe |
0 | 1162 // |
1163 // Input: calc_N2_ratio, calc_He_ratio : simulated gas mix. | |
560 | 1164 // temp_deco : simulated respiration pressure |
1165 // float_deco_distance : safety factor | |
1166 // ppWater : water-vapor pressure inside respiratory tract | |
0 | 1167 // |
1168 // Output: ppN2, ppHe. | |
1169 // | |
1170 static void sim_alveolar_presures(void) | |
1171 { | |
560 | 1172 overlay float deco_diluent = temp_deco; |
1173 | |
1174 // read ppO2 reported from sensors or by setpoint // TODO: can be deleted | |
1175 // char_actual_ppO2 = char_I_const_ppO2; | |
1176 | |
0 | 1177 |
1178 // Take deco offset into account, but not at surface. | |
560 | 1179 // Note: this should be done on ambient pressure, hence before |
1180 // computing the diluent partial pressure... | |
1181 if( deco_diluent > pres_surface ) deco_diluent += float_deco_distance; | |
1182 | |
1183 if( char_O_deco_status & DECO_MODE_LOOP ) | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1184 { |
560 | 1185 //---- Loop mode : adjust ppN2 and ppHe for change in ppO2 due to setpoint (CCR) or drop (pSCR)------- |
1186 | |
1187 // get current setpoint (CCR) or sensor value (CCR, for pSCR see text below) as default | |
1188 overlay float const_ppO2 = char_I_const_ppO2 * 0.01; | |
1189 | |
1190 if( char_O_deco_status & DECO_MODE_PSCR ) | |
1191 { | |
1192 //---- PSCR mode : compute loop gas ---------------------------------------- | |
1193 // | |
1194 // As the ppO2 in the loop changes with water depth, we can not use the current | |
1195 // sensor value as with CCR mode, but need to compute the ppO2 for the given depth. | |
1196 // Then we continue with the CCR mode code which calculates the increases of ppN2 | |
1197 // and ppH2 due to the reduction of the ppO2 in the loop. Essentially, diving a | |
1198 // PSCR is like diving a CCR with a setpoint lower than the ambient pressure x the | |
1199 // O2 fraction of the diluent would yield... | |
1200 // | |
1201 | |
1202 // deco_diluent is 0.0 ... in bar | |
1203 // calc_O2_ratio is 0.0 ... 1 as factor | |
1204 // char_I_PSCR_drop is 0 ... 15 as % | |
1205 // char_I_PSCR_lungratio is 5 ... 20 as % | |
1206 // const_ppO2 is 0.0 ... in bar | |
1207 | |
1208 const_ppO2 = (deco_diluent * calc_O2_ratio) - (1 - calc_O2_ratio) * 0.01 * char_I_PSCR_drop * char_I_PSCR_lungratio; | |
1209 | |
1210 // capture failure condition | |
1211 if( const_ppO2 < 0.0 ) const_ppO2 = 0.0; | |
1212 } | |
1213 else | |
1214 { | |
1215 | |
1216 //---- CCR mode ------------------------------------------------------------ | |
1217 | |
1218 // Limit the setpoint to the maximum physically possible ppO2. This prevents for | |
1219 // example calculating with a setpoint of 1.3 bar in only 2 meters of depth. | |
1220 // Additionally, if limiting occurs, the ppO2 can be further reduced to account | |
1221 // for residual inert gases by the user-adjustable setting char_I_cc_max_frac_o2. | |
1222 | |
1223 if( const_ppO2 > deco_diluent ) // no ppWater subtracted here to give some margin for | |
1224 { // sensors delivering data a little bit over target | |
1225 | |
1226 const_ppO2 = 0.01 * char_I_cc_max_frac_o2 * (deco_diluent - ppWater); | |
1227 } | |
1228 } | |
1229 | |
1230 if ( const_ppO2 == 0.0 ) char_actual_ppO2 = 0; | |
1231 else if ( const_ppO2 > 2.545 ) char_actual_ppO2 = 255; | |
1232 else char_actual_ppO2 = (unsigned char)(const_ppO2*100 + 0.5); | |
1233 | |
1234 // Note: ppO2 and ratios are known outside the lungs, so there is no ppWater in the equations below: | |
0 | 1235 deco_diluent -= const_ppO2; |
560 | 1236 deco_diluent /= calc_N2_ratio + calc_He_ratio; |
527
015b7fdd90a7
BUGFIX: O2 as diluent in CCR caused wrong calculations (Deco, EAD, END) in less then 6m depth
heinrichsweikamp
parents:
522
diff
changeset
|
1237 |
560 | 1238 // capture all failure conditions, including div/0 in case diluent is pure O2 |
1239 if( (deco_diluent < 0.0) || (calc_O2_ratio > 99.5) ) | |
1240 { | |
1241 deco_diluent = 0.0; | |
1242 | |
1243 char_actual_ppO2 = (unsigned char)(temp_deco*100 + 0.5); // without float_deco_distance here as this situation | |
1244 // is likely to occur only at 6 meters or shallower | |
1245 } | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1246 } |
560 | 1247 else |
1248 { | |
1249 //---- OC mode: char_actual_ppO2 will be needed for CNS calculation later -------------------------------- | |
1250 | |
1251 overlay float ppO2 = pres_respiration * calc_O2_ratio; | |
1252 | |
1253 if ( ppO2 > 2.545 ) char_actual_ppO2 = 255; | |
1254 else char_actual_ppO2 = (unsigned char)(ppO2*100 + 0.5); | |
1255 } | |
1256 | |
1257 | |
0 | 1258 if( deco_diluent > ppWater ) |
1259 { | |
1260 ppN2 = calc_N2_ratio * (deco_diluent - ppWater); | |
1261 ppHe = calc_He_ratio * (deco_diluent - ppWater); | |
1262 } | |
1263 else | |
1264 { | |
1265 ppN2 = 0.0; | |
1266 ppHe = 0.0; | |
1267 } | |
560 | 1268 |
0 | 1269 assert( 0.0 <= ppN2 && ppN2 < 14.0 ); |
1270 assert( 0.0 <= ppHe && ppHe < 14.0 ); | |
1271 } | |
1272 | |
1273 ////////////////////////////////////////////////////////////////////////////// | |
1274 // clear_tissue | |
1275 // | |
1276 // optimized in v.101 (var_N2_a) | |
1277 // | |
1278 // preload tissues with standard pressure for the given ambient pressure. | |
1279 // Note: fixed N2_ratio for standard air. | |
1280 // | |
1281 static void clear_tissue(void) | |
1282 { | |
560 | 1283 pres_respiration = 0.001 * int_I_pres_respiration; |
1284 N2_equilibrium = 0.7902 * (pres_respiration - ppWater); | |
1285 | |
0 | 1286 for(ci=0; ci<NUM_COMP; ci++) |
1287 { | |
313 | 1288 // cycle through the 16 Buhlmann N2 tissues |
560 | 1289 pres_tissue_N2[ci] = N2_equilibrium; // initialize data for "real" tissue |
1290 char_O_tissue_N2_saturation[ci] = 11; // initialize data for tissue graphics | |
1291 | |
1292 | |
1293 // cycle through the 16 Buhlmann He tissues | |
1294 pres_tissue_He[ci] = 0.0; // initialize data for "real" tissue | |
1295 char_O_tissue_He_saturation[ci] = 0; // initialize data for tissue graphics | |
0 | 1296 } |
1297 | |
560 | 1298 clear_CNS_fraction(); |
1299 | |
0 | 1300 clear_deco_table(); |
560 | 1301 |
1302 char_O_main_status = 0; | |
1303 char_O_deco_status = 0; | |
1304 char_O_nullzeit = 0; | |
1305 char_O_gtissue_no = 0; | |
1306 char_O_deco_warnings = 0; | |
1307 | |
1308 int_O_ascenttime = 0; | |
1309 int_O_gradient_factor = 0; | |
1310 | |
1311 calc_lead_tissue_limit = 0.0; | |
0 | 1312 } |
1313 | |
1314 ////////////////////////////////////////////////////////////////////////////// | |
1315 // calc_hauptroutine | |
1316 // | |
1317 // this is the major code in dive mode calculates: | |
1318 // the tissues, | |
1319 // the bottom time, | |
1320 // and simulates the ascend with all deco stops. | |
1321 // | |
1322 // | |
1323 static void calc_hauptroutine(void) | |
1324 { | |
560 | 1325 unsigned int int_ppO2_min; |
1326 unsigned int int_ppO2_max; | |
1327 | |
1328 | |
1329 //--- set-up part -------------------------------------------------------------------------------- | |
1330 | |
1331 // twosectimer: | |
1332 // calc_hauptroutine is now invoked every second to speed up the deco planning. | |
1333 // Because the tissue and CNS calculations are based on a 2 seconds period, the | |
1334 // the following toggle-timer will be used by the respective routines to skip | |
1335 // every 2nd invocation. | |
1336 twosectimer = (twosectimer) ? 0 : 1; // toggle the toggle-timer | |
1337 | |
1338 | |
1339 // set up normal tissue updating or "fast forward" updating for simulator sim+5' function | |
1340 // and deco calculator bottom time calculation | |
1341 if( char_I_sim_advance_time > 0 ) | |
1342 { | |
1343 // configure char_I_sim_advance_time minutes of tissue updating | |
1344 tissue_increment = char_I_sim_advance_time // given number of minutes, limited to 127 | |
1345 | 128; // set flag for updating the "real" tissues & CNS | |
1346 | |
1347 char_I_sim_advance_time = 0; // clear "mailbox" | |
1348 } | |
1349 else | |
1350 { | |
1351 // configure 2 seconds of tissue updating | |
1352 tissue_increment = 0 // encoding for 2 seconds update | |
1353 | 128; // set flag for updating the "real" tissues & CNS | |
1354 } | |
1355 | |
1356 //---- calculate the real tissue's data ----------------------------------------------------------------- | |
1357 | |
1358 calc_hauptroutine_data_input(); // acquire current environment data | |
1359 | |
1360 calc_hauptroutine_update_tissues(); // update tissue pressures, also sets char_actual_ppO2 | |
1361 | |
1362 calc_CNS_fraction(); // calculate CNS% for the real tissues | |
1363 | |
1364 compute_CNS_for_display(); // compute integer copy of CNS value for display purpose | |
1365 | |
1366 calc_gradient_factor(); // compute current GF | |
1367 | |
1368 | |
1369 //---- compute ppO2 warnings ------------------------------------------------------------------------------ | |
1370 | |
1371 // compute conditional min/max values | |
1372 int_ppO2_min = (char_O_main_status & DECO_MODE_LOOP) ? (unsigned int)char_I_ppO2_min_loop : (unsigned int)char_I_ppO2_min; | |
1373 int_ppO2_max = (char_O_deco_warnings & DECO_FLAG ) ? (unsigned int)char_I_ppO2_max_deco : (unsigned int)char_I_ppO2_max; | |
1374 | |
1375 // check for safe range of pure oxygen | |
1376 if ( int_O_O2_ppO2 >= int_ppO2_max ) int_O_O2_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; | |
1377 | |
1378 // check for safe range of breathed gas | |
1379 if ( int_O_breathed_ppO2 <= int_ppO2_min ) int_O_breathed_ppO2 |= INT_FLAG_WARNING + INT_FLAG_LOW; | |
1380 else if ( int_O_breathed_ppO2 >= int_ppO2_max ) int_O_breathed_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; | |
1381 else if ( int_O_breathed_ppO2 >= ppO2_prewarn_threshold ) int_O_breathed_ppO2 |= INT_FLAG_PREWARNING; | |
1382 | |
1383 // check for safe range of pure diluent | |
1384 if ( int_O_pure_ppO2 <= (unsigned int)char_I_ppO2_min ) int_O_pure_ppO2 |= INT_FLAG_WARNING + INT_FLAG_LOW; | |
1385 else if ( int_O_pure_ppO2 >= int_ppO2_max ) int_O_pure_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; | |
1386 | |
1387 // check for safe range of calculated pSCR loop gas | |
1388 if ( int_O_pSCR_ppO2 <= int_ppO2_min ) int_O_pSCR_ppO2 |= INT_FLAG_WARNING + INT_FLAG_LOW; | |
1389 else if ( int_O_pSCR_ppO2 >= int_ppO2_max ) int_O_pSCR_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; | |
1390 | |
1391 | |
1392 //---- toggle between calculation for NDL (bottom time), deco stops and more deco stops (continue) ------ | |
1393 | |
1394 switch( char_O_deco_status & DECO_STATUS_MASK ) | |
0 | 1395 { |
560 | 1396 overlay unsigned char i; |
1397 | |
1398 case DECO_STATUS_INIT: //---- At surface: start a new dive --------------------- | |
1399 | |
1400 clear_deco_table(); | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1401 copy_deco_table(); |
560 | 1402 |
1403 | |
1404 char_I_gas_change_time = 1; // TODO: validate proper operation before enabling this options-table parameter | |
1405 | |
1406 char_I_ascent_speed = 10; // TODO: validate proper operation before enabling this options-table parameter, | |
1407 // caution: values < 10 may have an impact on the deco calculation run-times! | |
1408 | |
1409 | |
1410 float_ascent_speed = 1.00 * char_I_ascent_speed; | |
1411 float_desaturation_multiplier = 0.01 * char_I_desaturation_multiplier; | |
1412 float_saturation_multiplier = 0.01 * char_I_saturation_multiplier; | |
1413 float_deco_distance = 0.01 * char_I_deco_distance; | |
1414 | |
1415 int_O_ascenttime = 0; // reset ascent time in normal plan | |
1416 int_O_alternate_ascenttime = 0; // reset ascent time in alternative plan | |
1417 char_O_nullzeit = 0; // reset no decompression limit (NDL) in normal plan | |
1418 char_O_alternate_nullzeit = 0; // reset no decompression limit (NDL) in alternative plan | |
1419 char_O_deco_warnings = 0; // reset all deco warning flags | |
1420 deco_tissue_vector = 0; // reset tissue deco vector | |
1421 IBCD_tissue_vector = 0; // reset tissue IBCD vector | |
1422 | |
1423 int_O_desaturation_time = 65535; // tag desaturation time as invalid (it will not be computed during a dive) | |
1424 | |
1425 | |
1426 for(i=0; i<NUM_GAS; ++i) | |
1427 { | |
1428 int_O_gas_volumes[i] = 0; | |
1429 int_O_tank_pres_need[i] = 0 + INT_FLAG_ZERO; // 0 bar + flag for 0 bar | |
1430 } | |
1431 | |
1432 for(i=0; i<NUM_COMP; ++i) | |
1433 { | |
1434 split_N2_He[i] = 90; // used for calculation of no-fly time | |
1435 } | |
1436 | |
1437 | |
1438 // init CNS counters | |
1439 CNS_sim_norm_fraction = CNS_sim_alt_fraction = CNS_fraction; // the floats | |
1440 int_O_normal_CNS_fraction = int_O_alternate_CNS_fraction = int_O_CNS_fraction; // the integers | |
1441 | |
1442 | |
0 | 1443 // Values that should be reset just once for the full real dive. |
1444 // This is used to record the lowest stop for the whole dive, | |
560 | 1445 // including ACCROSS all simulated ascents. |
1446 low_depth_norm = low_depth_alt = 0.0; | |
1447 locked_GF_step_norm = locked_GF_step_alt = 0.0; | |
1448 | |
1449 | |
1450 // continue in state DECO_STATUS_START to calculate the bottom-part of the dive and the NDL | |
1451 char_O_deco_status &= ~DECO_STATUS_MASK; | |
1452 | |
1453 // code execution continues in state DECO_STATUS_START | |
1454 | |
1455 | |
1456 case DECO_STATUS_START: //---- bottom time ------------------------------------- | |
0 | 1457 default: |
560 | 1458 |
1459 // reread the GF settings in case there was a switch between GF/aGF | |
1460 GF_low = char_I_GF_Low_percentage * 0.01; | |
1461 GF_high = char_I_GF_High_percentage * 0.01; | |
1462 GF_delta = GF_high - GF_low; | |
1463 | |
1464 // Lookup current gas and store it also as the first gas used. This gas will be used for the bottom | |
1465 // segment of the dive and for the period of delayed ascent when calculating fTTS or bailout. | |
1466 gas_find_current(); | |
1467 | |
1468 // setup the calculation ratio's calc_N2_ratio, calc_He_ratio and calc_O2_ratio | |
1469 gas_switch_set(); | |
1470 | |
1471 // calculate ppN2 and ppHe from calc_N2_ratio & calc_He_ratio | |
1472 sim_alveolar_presures(); | |
1473 | |
1474 // clear the internal(!) stops table | |
1475 clear_deco_table(); | |
1476 | |
1477 // initialize the simulated tissues with the current state of the real tissues | |
1478 update_startvalues(); | |
1479 | |
1480 // calculate the effect of extended bottom time due to delayed ascent / fTTS on current gas | |
1481 if( char_O_deco_status & DECO_ASCENT_DELAYED ) sim_extra_time(); | |
1482 | |
1483 // calculate if we are within no decompression limit (NDL) | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1484 calc_nullzeit(); |
560 | 1485 |
1486 // check which plan we are on | |
1487 if( char_O_deco_status & DECO_PLAN_ALTERNATE ) | |
1488 { | |
1489 //---- alternate dive plan -------------------------------------------------------------------- | |
1490 | |
1491 // Some NDL time left in alternate plan? | |
1492 if( char_O_alternate_nullzeit > 0 ) | |
1493 { | |
1494 // clear tank pressure needs | |
1495 if( char_O_deco_status & DECO_VOLUME_CALCULATE ) | |
1496 for(i=0; i<NUM_GAS; ++i) int_O_tank_pres_need[i] = 0 + INT_FLAG_ZERO; // 0 bar + flag for 0 bar | |
1497 | |
1498 // calculate the CNS% at the end of the dive if requested: | |
1499 // as we are in no stop, CNS at end of dive is more or less the same CNS we have now | |
1500 if( char_O_deco_status & DECO_CNS_CALCULATE ) int_O_alternate_CNS_fraction = int_O_CNS_fraction; | |
1501 | |
1502 // clear fTTS ascent time | |
1503 int_O_alternate_ascenttime = 0; | |
1504 | |
1505 // YES - computation of alternate plan completed | |
1506 char_O_deco_status &= ~DECO_STATUS_MASK; | |
1507 } | |
1508 else | |
1509 { | |
1510 // NO - clear status bits and set status bits for | |
1511 // calculation of ascent on next invocation | |
1512 char_O_deco_status &= ~DECO_STATUS_MASK; | |
1513 char_O_deco_status |= DECO_STATUS_ASCENT; | |
1514 } | |
1515 } | |
1516 else | |
1517 { | |
1518 //---- normal dive plan ------------------------------------------------------------------------- | |
1519 | |
1520 // Some NDL time left in normal plan? | |
1521 if( char_O_nullzeit > 0 ) | |
1522 { | |
1523 // published (erased) stops table | |
1524 copy_deco_table(); | |
1525 | |
1526 // ** commented out - char_O_deco_last_stop is not used for anything | |
1527 // | |
1528 // // set last stop to 0 (for OSTC menu animation) | |
1529 // char_O_deco_last_stop = 0; | |
1530 | |
1531 // clear tank pressure needs | |
1532 if( char_O_deco_status & DECO_VOLUME_CALCULATE ) | |
1533 for(i=0; i<NUM_GAS; ++i) int_O_tank_pres_need[i] = 0 + INT_FLAG_ZERO; // 0 bar + flag for 0 bar | |
1534 | |
1535 // calculate the CNS% at the end of the dive if requested: | |
1536 // as we are in no stop, CNS at end of dive is more or less the same CNS we have now | |
1537 if( char_O_deco_status & DECO_CNS_CALCULATE ) int_O_normal_CNS_fraction = int_O_CNS_fraction; | |
1538 | |
1539 // YES - computation of normal plan completed | |
1540 char_O_deco_status &= ~DECO_STATUS_MASK; | |
1541 } | |
1542 else | |
1543 { | |
1544 // NO - clear status bits and set status bits for | |
1545 // calculation of ascent on next invocation | |
1546 char_O_deco_status &= ~DECO_STATUS_MASK; | |
1547 char_O_deco_status |= DECO_STATUS_ASCENT; | |
1548 } | |
1549 } | |
1550 | |
1551 break; | |
1552 | |
1553 | |
1554 case DECO_STATUS_ASCENT: //---- Simulate ascent to first stop ------------------- | |
1555 | |
1556 // initialize depth (in abs.pressure) for ascent and deco simulation, start from current real depth | |
1557 temp_deco = pres_respiration; | |
1558 | |
1559 // calculate ascent to first stop | |
1560 sim_ascent_to_first_stop(); | |
1561 | |
1562 // calculate all further stops next time | |
1563 char_O_deco_status &= ~DECO_STATUS_MASK; // clear status bits and set status bits | |
1564 char_O_deco_status |= DECO_STATUS_STOPS; // for calculation of stops on next invocation | |
1565 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1566 break; |
0 | 1567 |
560 | 1568 |
1569 case DECO_STATUS_STOPS: //---- Simulate stops ---------------------------------- | |
1570 | |
1571 calc_hauptroutine_calc_deco(); | |
1572 | |
1573 // If simulation is finished, do some more computations if requested | |
1574 // and restore the GF low reference so that the next ascent simulation | |
1575 // is done from the current depth: | |
1576 if( !(char_O_deco_status & DECO_STATUS_MASK) ) | |
1577 { | |
1578 // Calculate ascent time, result in int_O_ascenttime or int_O_alternate_ascenttime | |
1579 calc_ascenttime(); | |
1580 | |
1581 // the current depth is needed by calc_CNS_planning() and gas_volumes() | |
1582 bottom_depth = (unsigned char)((pres_respiration - pres_surface)*BAR_TO_METER); | |
1583 | |
1584 // if requested, calculate the CNS% at the end of the dive (including the deco stops) | |
1585 if( char_O_deco_status & DECO_CNS_CALCULATE ) calc_CNS_planning(); | |
1586 | |
1587 // if requested, calculate the required gas volumes and tank pressures at the end of the dive. | |
1588 if( char_O_deco_status & DECO_VOLUME_CALCULATE ) gas_volumes(); | |
1589 | |
1590 // some more aftermath dependent on the current plan | |
1591 if( char_O_deco_status & DECO_PLAN_ALTERNATE ) | |
1592 { | |
1593 //---- alternative plan ---------------------------------------------------- | |
1594 | |
1595 // was CNS at end of dive calculated? | |
1596 if( char_O_deco_status & DECO_CNS_CALCULATE ) | |
1597 { | |
1598 // yes - compute CNS value to display | |
1599 if ( CNS_sim_alt_fraction < 0.01 ) int_O_alternate_CNS_fraction = 0; | |
1600 else if ( CNS_sim_alt_fraction > 9.985 ) int_O_alternate_CNS_fraction = 999 + INT_FLAG_WARNING; | |
1601 else | |
1602 { | |
1603 // convert float to integer | |
1604 int_O_alternate_CNS_fraction = (unsigned short)(100 * CNS_sim_alt_fraction + 0.5); | |
1605 | |
1606 // set warning flag if CNS is >= 100% | |
1607 if( int_O_alternate_CNS_fraction >= 100 ) | |
1608 int_O_alternate_CNS_fraction |= INT_FLAG_WARNING; | |
1609 | |
1610 // set invalid flag if there is an overflow in the stops table | |
1611 if( char_O_deco_warnings & DECO_WARNING_STOPTABLE_OVERFLOW ) | |
1612 int_O_alternate_CNS_fraction |= INT_FLAG_INVALID; | |
1613 } | |
1614 } | |
1615 else | |
1616 { | |
1617 // no - invalidate value (value = 0, invalid flag set) | |
1618 int_O_alternate_CNS_fraction = INT_FLAG_INVALID; | |
1619 } | |
1620 } | |
1621 else | |
1622 { | |
1623 //---- normal plan --------------------------------------------------------- | |
1624 | |
1625 // publish the stops table | |
1626 copy_deco_table(); | |
1627 | |
1628 // was CNS at end of dive calculated? | |
1629 if( char_O_deco_status & DECO_CNS_CALCULATE ) | |
1630 { | |
1631 // yes - compute CNS value to display | |
1632 if ( CNS_sim_norm_fraction < 0.01 ) int_O_normal_CNS_fraction = 0; | |
1633 else if ( CNS_sim_norm_fraction >= 9.985 ) int_O_normal_CNS_fraction = 999 + INT_FLAG_WARNING; | |
1634 else | |
1635 { | |
1636 // convert float to integer | |
1637 int_O_normal_CNS_fraction = (unsigned short)(100 * CNS_sim_norm_fraction + 0.5); | |
1638 | |
1639 // set warning flag if CNS is >= 100% | |
1640 if( int_O_normal_CNS_fraction >= 100 ) | |
1641 int_O_normal_CNS_fraction |= INT_FLAG_WARNING; | |
1642 | |
1643 // set invalid flag if there is an overflow in the stops table | |
1644 if( char_O_deco_warnings & DECO_WARNING_STOPTABLE_OVERFLOW ) | |
1645 int_O_normal_CNS_fraction |= INT_FLAG_INVALID; | |
1646 } | |
1647 } | |
1648 else | |
1649 { | |
1650 // no - invalidate value (value = 0, invalid flag set) | |
1651 int_O_normal_CNS_fraction = INT_FLAG_INVALID; | |
1652 } | |
1653 | |
1654 } // aftermath | |
1655 } // if | |
1656 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1657 break; |
560 | 1658 |
1659 } // switch | |
0 | 1660 } |
1661 | |
1662 ////////////////////////////////////////////////////////////////////////////// | |
1663 // calc_hauptroutine_data_input | |
1664 // | |
1665 // Reset all C-code dive parameters from their ASM-code values. | |
1666 // Detect gas change condition. | |
1667 // | |
1668 void calc_hauptroutine_data_input(void) | |
1669 { | |
560 | 1670 // get the current pressures |
1671 pres_respiration = 0.001 * int_I_pres_respiration; | |
1672 pres_surface = 0.001 * int_I_pres_surface; | |
1673 | |
1674 // get the currently breathed gas mixture | |
1675 O2_ratio = 0.01 * char_I_O2_ratio; | |
1676 He_ratio = 0.01 * char_I_He_ratio; | |
1677 | |
1678 // N2 ratios are computed within p2_deco.c from the O2 and He ratios | |
1679 N2_ratio = 1.0 - O2_ratio - He_ratio; | |
1680 | |
1681 // N2 tissue pressure at surface equilibrium, used for tissue graphics scaling | |
1682 N2_equilibrium = 0.7902 * (pres_surface - ppWater); | |
0 | 1683 } |
1684 | |
1685 ////////////////////////////////////////////////////////////////////////////// | |
1686 // | |
1687 // | |
1688 void calc_hauptroutine_update_tissues(void) | |
1689 { | |
560 | 1690 overlay float pres_diluent = pres_respiration; |
1691 | |
1692 | |
0 | 1693 assert( 0.00 <= N2_ratio && N2_ratio <= 1.00 ); |
1694 assert( 0.00 <= He_ratio && He_ratio <= 1.00 ); | |
114
109df032cc54
minor fix: assert should pass for pure helium diluant
heinrichsweikamp
parents:
111
diff
changeset
|
1695 assert( (N2_ratio + He_ratio) <= 1.00 ); |
0 | 1696 assert( 0.800 < pres_respiration && pres_respiration < 14.0 ); |
1697 | |
560 | 1698 |
1699 //---- OC, CCR and Bailout Mode Gas Calculations ------------------------------------------------------------ | |
1700 | |
1701 // calculate ppO2 of pure oxygen | |
1702 O2_ppO2 = (pres_respiration - ppWater); | |
1703 | |
1704 // capture failure condition in case pres_respiration is < ppWater (should never happen...) | |
1705 if( O2_ppO2 < 0.0 ) O2_ppO2 = 0.0; | |
1706 | |
1707 // calculate ppO2 of the pure gas (diluent) | |
1708 pure_ppO2 = O2_ppO2 * O2_ratio; | |
1709 | |
1710 | |
1711 //---- PSCR Mode Gas Calculation----------------------------------------------------------- | |
1712 | |
1713 // With flags set for PSCR we compute the ppO2 in the loop from the diluent's O2 | |
1714 // ratio and the PSCR parameters. This figure will be used in the pSCR custom view. | |
1715 // If sensors are used (char_I_const_ppO2 > 0), we will override the calculated ppO2 | |
1716 // with the sensor data. Then we continue with the CCR mode code which calculates | |
1717 // the increase of ppN2 and ppH2 due to the reduction of the ppO2 in the loop. | |
1718 // Essentially, diving a pSCR is like diving a CCR with a setpoint set lower than | |
1719 // the ambient pressure multiplied with the O2 fraction of the diluent... | |
1720 | |
1721 // calculate pSCR ppO2 | |
1722 // | |
1723 // pres_respiration is 0.0 ... in bar | |
1724 // O2_ratio is 0.0 ... 1.0 as factor | |
1725 // char_I_PSCR_drop is 0 ... 15 as % | |
1726 // char_I_PSCR_lungratio is 5 ... 20 as % | |
1727 // pSCRppO2 is 0.0 ... in bar | |
1728 | |
1729 pSCR_ppO2 = (pres_respiration * O2_ratio) - (1 - O2_ratio) * 0.01 * char_I_PSCR_drop * char_I_PSCR_lungratio; | |
1730 | |
1731 // capture failure condition if case pSCR_ppO2 becomes negative | |
1732 if( pSCR_ppO2 < 0.0 ) pSCR_ppO2 = 0.0; | |
1733 | |
1734 | |
1735 //---- Loop modes : adjust ppN2 and ppHe for change in ppO2 due to setpoint (CCR) or drop (pSCR) ------------ | |
1736 if ( char_O_main_status & DECO_MODE_LOOP ) | |
0 | 1737 { |
560 | 1738 overlay float const_ppO2; |
1739 | |
1740 // get the current sensor reading (CCR / pSCR if fitted) or the fixed setpoint (CCR) / a zero (pSCR) | |
1741 const_ppO2 = 0.01 * char_I_const_ppO2; | |
1742 | |
1743 // Limit the setpoint to the maximum physically possible ppO2. This prevents for | |
1744 // example calculating with a setpoint of 1.3 bar in only 2 meters of depth. | |
1745 // Additionally, if limiting occurs, the ppO2 can be further reduced to account | |
1746 // for residual inert gases by the user-adjustable setting char_I_cc_max_frac_o2. | |
1747 | |
1748 if( const_ppO2 > pres_respiration ) // no ppWater subtracted here to give some margin for | |
1749 { // sensors delivering data a little bit over target | |
1750 | |
1751 const_ppO2 = 0.01 * char_I_cc_max_frac_o2 * (pres_respiration - ppWater); | |
1752 } | |
1753 | |
1754 // check which kind of loop we are on | |
1755 if( char_O_main_status & DECO_MODE_PSCR ) | |
1756 { | |
1757 //---- pSCR Mode -------------------------------------------------------------------------- | |
1758 | |
1759 // check if a sensor is fitted | |
1760 if( char_I_const_ppO2 ) breathed_ppO2 = const_ppO2; // yes - derive ppO2s from (char_I_)const_ppO2 | |
1761 else breathed_ppO2 = pSCR_ppO2; // no - derive ppO2s from calculated ppO2 | |
1762 } | |
1763 else | |
1764 { | |
1765 //---- CCR Mode --------------------------------------------------------------------------- | |
1766 | |
1767 // derive breathed ppO2 from (char_I_)const_ppO2, which holds sensor reading or fixed setpoint | |
1768 breathed_ppO2 = const_ppO2; | |
1769 } | |
1770 | |
1771 // adjust diluent pressure (ppN2 + ppHe) for change in ppO2 due to setpoint (CCR) or drop (pSCR) | |
0 | 1772 pres_diluent -= const_ppO2; |
560 | 1773 pres_diluent /= N2_ratio + He_ratio; |
1774 | |
1775 // capture all failure conditions, including div/0 in case diluent is pure O2 | |
1776 if( (pres_diluent < 0.0) || (char_I_O2_ratio == 100) ) | |
1777 { | |
1778 pres_diluent = 0.0; | |
1779 breathed_ppO2 = pure_ppO2; | |
1780 } | |
1781 | |
1782 } | |
1783 else | |
1784 { //---- OC mode ----------------------------------------------------------------------------------------- | |
1785 | |
1786 // breathed ppO2 is ppO2 of pure gas | |
1787 breathed_ppO2 = pure_ppO2; | |
1788 } | |
1789 | |
1790 | |
1791 // derive char_actual_ppO2 in [cbar], used for calculating CNS% | |
1792 if ( breathed_ppO2 < 0.01 ) char_actual_ppO2 = 0; | |
1793 else if ( breathed_ppO2 >= 2.545 ) char_actual_ppO2 = 255; | |
1794 else char_actual_ppO2 = (unsigned char)(100 * breathed_ppO2 + 0.5); | |
1795 | |
1796 | |
1797 //---- export ppO2 values in [cbar] for warning generation and display purpose ------------------------------ | |
1798 | |
1799 // pure oxygen ppO2 | |
1800 if ( O2_ppO2 < 0.01 ) int_O_O2_ppO2 = 0; | |
1801 else if ( O2_ppO2 >= 9.995 ) int_O_O2_ppO2 = 999; | |
1802 else int_O_O2_ppO2 = (unsigned int)(100 * O2_ppO2 + 0.5); | |
1803 | |
1804 // pure gas ppO2 | |
1805 if ( pure_ppO2 < 0.01 ) int_O_pure_ppO2 = 0; | |
1806 else if ( pure_ppO2 >= 9.995 ) int_O_pure_ppO2 = 999; | |
1807 else int_O_pure_ppO2 = (unsigned int)(100 * pure_ppO2 + 0.5); | |
1808 | |
1809 // calculated pSCR ppO2 | |
1810 if ( pSCR_ppO2 < 0.01 ) int_O_pSCR_ppO2 = 0; | |
1811 else if ( pSCR_ppO2 >= 9.995 ) int_O_pSCR_ppO2 = 999; | |
1812 else int_O_pSCR_ppO2 = (unsigned int)(100 * pSCR_ppO2 + 0.5); | |
1813 | |
1814 // breathed ppO2 | |
1815 if ( breathed_ppO2 < 0.01 ) int_O_breathed_ppO2 = 0; | |
1816 else if ( breathed_ppO2 >= 9.995 ) int_O_breathed_ppO2 = 999; | |
1817 else int_O_breathed_ppO2 = (unsigned int)(100 * breathed_ppO2 + 0.5); | |
1818 | |
1819 | |
1820 //---- calculate ppN2, ppHe and EAD, END ------------------------------------------------------------------- | |
1821 | |
0 | 1822 if( pres_diluent > ppWater ) |
1823 { | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1824 overlay float EAD, END; |
0 | 1825 |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1826 ppN2 = N2_ratio * (pres_diluent - ppWater); |
0 | 1827 ppHe = He_ratio * (pres_diluent - ppWater); |
1828 | |
560 | 1829 // EAD : Equivalent Air Depth. Equivalent depth for the same N2 level with plain air. |
0 | 1830 // ppN2 = 79% * (P_EAD - ppWater) |
1831 // EAD = (P_EAD - Psurface) * 10 | |
1832 // ie: EAD = (ppN2 / 0.7902 + ppWater -Psurface) * 10 | |
560 | 1833 |
0 | 1834 EAD = (ppN2 / 0.7902 + ppWater - pres_surface) * BAR_TO_METER; |
560 | 1835 |
1836 if( (EAD < 0.0) || (EAD > 245.5) ) EAD = 0.0; | |
1837 | |
0 | 1838 char_O_EAD = (unsigned char)(EAD + 0.5); |
1839 | |
560 | 1840 |
1841 // END : Equivalent Narcotic Depth. | |
1842 // Here we count O2 as narcotic too. Hence everything but helium (has a narcosis | |
1843 // factor of 0.23 btw). Hence the formula becomes: | |
0 | 1844 // END * BarPerMeter * (1.0 - 0.0) - ppWater + Psurface == Pambient - ppHe - ppWater |
1845 // ie: END = (Pambient - ppHe - Psurface) * BAR_TO_METER | |
1846 // | |
1847 // Source cited: | |
1848 // The Physiology and Medicine of Diving by Peter Bennett and David Elliott, | |
1849 // 4th edition, 1993, W.B.Saunders Company Ltd, London. | |
560 | 1850 |
0 | 1851 END = (pres_respiration - ppHe - pres_surface) * BAR_TO_METER; |
560 | 1852 |
1853 if( (END < 0.0) || (END > 245.5) ) END = 0.0; | |
1854 | |
1855 char_O_END = (unsigned char)(END + 0.5); | |
0 | 1856 } |
560 | 1857 else |
0 | 1858 { |
560 | 1859 ppN2 = ppHe = 0.0; |
1860 | |
1861 char_O_EAD = char_O_END = 0; | |
0 | 1862 } |
1863 | |
560 | 1864 |
1865 //---- calculate decompression status ---------------------------------------------------------------------- | |
1866 | |
1867 // Calculate tissues | |
1868 calc_tissue(); | |
1869 | |
1870 // Calculate limit for surface, ie. GF_high. | |
0 | 1871 calc_limit(); |
560 | 1872 |
1873 | |
1874 // Fill int_O_ceiling (in mbar) if ceiling is below the surface | |
1875 if( (calc_lead_tissue_limit - pres_surface) > 0 ) | |
1876 { | |
1877 | |
1878 // compatibility version | |
1879 int_O_ceiling = (short)((calc_lead_tissue_limit - pres_surface) * 1000); | |
1880 | |
1881 // new version | |
1882 // // Round up to next 10 cm so that the ceiling disappears on the display only when the ceiling | |
1883 // // limit is really zero. This will coincident then with TTS switching back to NDL time. | |
1884 // int_O_ceiling = (short)((calc_lead_tissue_limit - pres_surface) * 1000 + 9); | |
1885 | |
1886 | |
1887 // limit int_O_ceiling to 16000 mbar (150 m) | |
1888 if( int_O_ceiling > 16000) int_O_ceiling = 16000; | |
1889 } | |
126 | 1890 else |
560 | 1891 { |
126 | 1892 int_O_ceiling = 0; |
560 | 1893 } |
126 | 1894 |
0 | 1895 int_O_gtissue_press = (short)((pres_tissue_N2[char_O_gtissue_no] + pres_tissue_He[char_O_gtissue_no]) * 1000); |
1896 } | |
1897 | |
1898 | |
1899 ////////////////////////////////////////////////////////////////////////////// | |
1900 // Compute stops. | |
1901 // | |
1902 // Note: because this can be very long, break on 16 iterations, and set state | |
560 | 1903 // to DECO_STATUS_FINISHED when finished, or to DECO_STATUS_STOPS when |
1904 // needing to continue. | |
0 | 1905 // Note: because each iteration might be very long too (~ 66 ms in 1.84beta), |
560 | 1906 // break the loop when elapsed time exceeds 512 milliseconds. |
0 | 1907 // |
1908 void calc_hauptroutine_calc_deco(void) | |
1909 { | |
560 | 1910 overlay unsigned char loop; |
1911 | |
1912 for(loop = 0; loop < 16; ++loop) | |
1913 { | |
1914 // limit loops to 512ms, using timer 5 | |
1915 if( tmr5() & (512*32) ) break; | |
1916 | |
1917 // calc_nextdecodepth() | |
1918 // | |
1919 // INPUT temp_deco : current depth in absolute pressure | |
1920 // OUTPUT temp_depth_limit : depth of next stop in meters | |
1921 // RETURN true if a stop is needed | |
1922 // | |
1923 // The function manages gas changes by itself, including priming | |
1924 // the deco stop with the configured gas change time. | |
1925 // | |
1926 if( calc_nextdecodepth() ) | |
1927 { | |
1928 if( temp_depth_limit == 0 ) goto Surface; // this check should not bee needed as in | |
1929 // this case the RETURN value will be false | |
1930 | |
1931 //---- stop required at temp_depth_limit ------------------------------------- | |
1932 | |
1933 // convert stop depth in meters to absolute pressure | |
1934 temp_deco = temp_depth_limit * METER_TO_BAR + pres_surface; | |
1935 | |
1936 // add one minute to the current stop, or add a new stop, | |
1937 // or abort deco calculation if the deco table is full. | |
1938 if( !update_deco_table(1) ) goto Surface; | |
1939 } | |
1940 else | |
1941 { | |
1942 //---- no stop required -------------------------------------- | |
1943 | |
1944 // ascend by float_ascent_speed for 1 minute | |
1945 temp_deco -= float_ascent_speed * METER_TO_BAR; | |
1946 | |
1947 // finish deco calculation if surface is reached | |
1948 if( temp_deco <= pres_surface ) | |
1949 { | |
0 | 1950 Surface: |
560 | 1951 // set deco engine status to done (DECO_STATUS_FINISHED) |
1952 char_O_deco_status &= ~DECO_STATUS_MASK; | |
1953 | |
1954 // ** commented out - char_O_deco_last_stop is not used for anything | |
1955 // | |
1956 // // surface reached (to animate menu) | |
1957 // if( !(char_O_deco_status & DECO_PLAN_ALTERNATE)) char_O_deco_last_stop = 0; | |
1958 | |
1959 return; | |
1960 } | |
1961 } | |
1962 | |
1963 | |
1964 //---- as one minute as passed now, update the tissues ---------------------- | |
1965 | |
1966 // program 1 minute interval on simulated tissues (Flagbit 7 = 0) | |
1967 tissue_increment = 1; | |
1968 | |
1969 // compute current ppN2 and ppHe | |
1970 sim_alveolar_presures(); | |
1971 | |
1972 // update the tissues | |
1973 calc_tissue(); | |
1974 } | |
1975 | |
1976 // ** commented out - char_O_deco_last_stop is not used for anything | |
1977 // | |
1978 // // surface not reached, need more stops... store reached depth for menu animation. | |
1979 // if( !(char_O_deco_status & DECO_PLAN_ALTERNATE) ) char_O_deco_last_stop = temp_depth_limit; | |
0 | 1980 } |
1981 | |
1982 | |
1983 ////////////////////////////////////////////////////////////////////////////// | |
560 | 1984 // Simulate ascent to first deco stop. |
0 | 1985 // |
1986 // | |
560 | 1987 // Modified: temp_deco : current depth in ascent and deco simulation, in bar absolute pressure |
0 | 1988 // |
1989 void sim_ascent_to_first_stop(void) | |
1990 { | |
560 | 1991 overlay unsigned char fast = 1; // 1 = 1 minute steps, 0 = 2 seconds steps |
1992 overlay unsigned char gaschange = 0; // 1 = do a gas change, 0 = no better gas available | |
1993 | |
1994 | |
1995 //---- Loop until first deco stop or surface is reached ---------- | |
1996 for(;;) | |
1997 { | |
1998 // depth in absolute pressure we came from | |
1999 overlay float old_deco = temp_deco; | |
2000 | |
2001 // try ascending 1 full minute (fast) or 2 seconds (!fast) | |
2002 if ( fast ) temp_deco -= float_ascent_speed * METER_TO_BAR; // 1 min at float_ascent_speed ( 5 .. 10 m/min) | |
2003 else temp_deco -= (float_ascent_speed/30.0) * METER_TO_BAR; // 2 sec at float_ascent_speed (17 .. 33 cm/min) | |
2004 | |
2005 // but don't go over surface | |
2006 if( temp_deco < pres_surface ) temp_deco = pres_surface; | |
2007 | |
2008 // compute sim_lead_tissue_limit | |
2009 if ( char_I_deco_model != 0 ) sim_limit(GF_low); | |
2010 else sim_limit(1.0); | |
2011 | |
2012 // did we overshoot the first deco stop? | |
2013 if( temp_deco < sim_lead_tissue_limit ) | |
2014 { | |
2015 // YES - back to last depth below first stop | |
2016 temp_deco = old_deco; | |
2017 | |
2018 // switch to 2 seconds ascent if not yet in, else done | |
2019 if( fast ) | |
2020 { | |
2021 fast = 0; // retry with 2 seconds ascent steps | |
2022 continue; | |
2023 } | |
2024 else | |
2025 { | |
2026 break; // done... | |
2027 } | |
2028 } | |
2029 | |
2030 // If code execution passes along here, we did not overshoot the first stop. | |
2031 | |
2032 // did we reach the surface? if yes, done! | |
2033 if( temp_deco == pres_surface ) break; | |
2034 | |
2035 // depth in meters where we are now (no round-up) | |
2036 temp_depth_limit = (unsigned char)((temp_deco - pres_surface) * BAR_TO_METER); | |
2037 | |
2038 // Check if there is a better gas to switch to, but only in alternative plan mode | |
2039 // or if override is set. If yes, introduce a stop for the gas change. | |
2040 if( ((char_O_deco_status & DECO_PLAN_ALTERNATE) || (char_O_main_status & DECO_GASCHANGE_OVRD)) | |
2041 && gas_find_better() ) | |
2042 { | |
2043 // depth in meters we came from | |
2044 overlay unsigned char old_depth_limit = (unsigned char)((old_deco - pres_surface) * BAR_TO_METER); | |
2045 | |
2046 // adjust temp_depth_limit to the gas change depth, but not deeper than the depth we came from | |
2047 temp_depth_limit = (sim_gas_last_depth < old_depth_limit) ? sim_gas_last_depth : old_depth_limit; | |
2048 | |
2049 // create a stop for the gas change | |
2050 update_deco_table(char_I_gas_change_time); | |
2051 | |
2052 // set the new calculation values for N2, He and O2 | |
2053 gas_switch_set(); | |
2054 | |
2055 // signal to create a stop for the gas change and update the tissues | |
2056 gaschange = char_I_gas_change_time; | |
2057 | |
2058 // Adjust the depth for the tissue update to the stop depth. In case of fast mode, this | |
2059 // imposes that the ascent from the 'old_deco' depth to this stop took 1 minute although | |
2060 // we might have only ascended one or two meters... | |
2061 temp_deco = temp_depth_limit * METER_TO_BAR + pres_surface; | |
2062 } | |
2063 | |
2064 // Did one minute pass by and/or do we have a gas change? | |
2065 // Remark: The 2 seconds ascent iterations towards the first deco stop in !fast speed may take | |
2066 // up to 28 seconds in total - for this rough half of a minute no tissue updates will be computed. | |
2067 // Well, it could be done by setting tissue_increment = 0 in !fast condition and making calls to | |
2068 // sim_alveolar_presures() and calc_tissue() - see code commented out below. | |
2069 if( fast || gaschange ) | |
2070 { | |
2071 // program interval on simulated tissues (flag bit 7 = 0) | |
2072 tissue_increment = fast + gaschange; | |
2073 | |
2074 // clear gas change signal | |
2075 gaschange = 0; | |
2076 // } | |
2077 // else | |
2078 // { | |
2079 // // program 2 seconds interval on simulated tissues (flag bit 7 = 0) | |
2080 // tissue_increment = 0; | |
2081 // } | |
2082 // { | |
2083 // compute ppN2/ppHe for current depth from temp_deco | |
2084 sim_alveolar_presures(); | |
2085 | |
2086 // update the tissues | |
2087 calc_tissue(); | |
2088 } | |
2089 } | |
0 | 2090 } |
2091 | |
2092 ////////////////////////////////////////////////////////////////////////////// | |
560 | 2093 // Simulate extra time at the current depth. |
0 | 2094 // |
560 | 2095 // This routine is used for the futureTTS / delayed ascent feature. |
2096 // | |
0 | 2097 void sim_extra_time(void) |
2098 { | |
560 | 2099 overlay unsigned char backup = tissue_increment; // back-up tissue_increment |
2100 | |
2101 tissue_increment = char_I_extra_time; // program interval on simulated tissues (Flagbit 7 = 0) | |
2102 | |
2103 calc_tissue(); // update the tissues | |
2104 | |
2105 tissue_increment = backup; // restore tissue_increment | |
0 | 2106 } |
2107 | |
2108 ////////////////////////////////////////////////////////////////////////////// | |
2109 // calc_tissue | |
2110 // | |
2111 // optimized in v.101 | |
2112 // | |
560 | 2113 // INPUT: ppN2, ppHe, tissue_increment |
2114 // MODIFIED: pres_tissue_N2[], pres_tissue_He[] | |
2115 // OUTPUT: char_O_tissue_N2_saturation[], char_O_tissue_He_saturation[] | |
2116 // | |
2117 static void calc_tissue() | |
0 | 2118 { |
560 | 2119 overlay float temp_tissue_N2; |
2120 overlay float temp_tissue_He; | |
2121 overlay unsigned char period; | |
2122 overlay unsigned char i; | |
2123 | |
2124 | |
0 | 2125 assert( 0.00 <= ppN2 && ppN2 < 11.2 ); // 80% N2 at 130m |
2126 assert( 0.00 <= ppHe && ppHe < 12.6 ); // 90% He at 130m | |
560 | 2127 |
2128 | |
2129 for (ci=0;ci<NUM_COMP;ci++) // iterate through all compartments | |
0 | 2130 { |
560 | 2131 i = tissue_increment & 127; // extract number of minutes to do (if i > 0) |
2132 // or if one 2 second period is to do (if i = 0) | |
2133 | |
2134 if( i == 0 ) // check if we shall do one 2-seconds period | |
2135 { | |
2136 read_Buhlmann_times(0); // YES, program coefficients for a 2 seconds period | |
2137 period = 1; // set period length (in cycles) | |
2138 i = 1; // and one cycle to do | |
2139 } | |
2140 else if( i > 9 ) // check if we can start with 10 minutes periods | |
2141 { | |
2142 read_Buhlmann_times(2); // YES, program coefficients for 10 minutes periods | |
2143 period = 10; // set period length (in cycles) to ten | |
2144 } | |
2145 else // we shall do 1 to 9 minutes | |
2146 { | |
2147 read_Buhlmann_times(1); // program coefficients for 1 minute periods | |
2148 period = 1; // set period length (in cycles) to one | |
2149 } | |
2150 | |
2151 do | |
2152 { | |
2153 //---- N2 ------------------------------------------------------------------------------- | |
2154 | |
2155 temp_tissue = (tissue_increment & 128) ? pres_tissue_N2[ci] : sim_pres_tissue_N2[ci]; | |
2156 | |
2157 temp_tissue = (ppN2 - temp_tissue) * var_N2_e; | |
2158 | |
2159 temp_tissue_safety(); | |
2160 | |
2161 if( tissue_increment & 128 ) | |
2162 { | |
2163 // The temp variable takes on purpose just the tissue increment from the last loop's iteration. | |
2164 temp_tissue_N2 = temp_tissue; | |
2165 | |
2166 // Update the real tissues if either we are on the 2 seconds interval, | |
2167 // or if we shall advance the tissues on a one or several minutes basis. | |
2168 if( twosectimer || (tissue_increment & 127) ) pres_tissue_N2[ci] += temp_tissue; | |
2169 } | |
2170 else | |
2171 { | |
2172 // Updates of the sim-tissues always comes on a 1 minutes basis, | |
2173 // so we do not need to check of the 2 seconds interval. | |
2174 sim_pres_tissue_N2[ci] += temp_tissue; | |
2175 } | |
2176 | |
2177 | |
2178 //---- He ------------------------------------------------------------------------------- | |
2179 | |
2180 temp_tissue = (tissue_increment & 128) ? pres_tissue_He[ci] : sim_pres_tissue_He[ci]; | |
2181 | |
2182 temp_tissue = (ppHe - temp_tissue) * var_He_e; | |
2183 | |
2184 temp_tissue_safety(); | |
2185 | |
2186 if( tissue_increment & 128 ) | |
2187 { | |
2188 // The temp variable takes on purpose just the tissue increment from the last loop's iteration. | |
2189 temp_tissue_He = temp_tissue; | |
2190 | |
2191 // Update the real tissues if either we are on the 2 seconds interval, | |
2192 // or if we shall advance the tissues on a one or several minutes basis. | |
2193 if( twosectimer || (tissue_increment & 127) ) pres_tissue_He[ci] += temp_tissue; | |
2194 | |
2195 } | |
2196 else | |
2197 { | |
2198 // Updates of the sim-tissues always comes on a 1 minutes basis, | |
2199 // so we do not need to check of the 2 seconds interval. | |
2200 sim_pres_tissue_He[ci] += temp_tissue; | |
2201 } | |
2202 | |
2203 | |
2204 // decrement loop counter | |
2205 i -= period; | |
2206 | |
2207 // check if we need to switch from 10 minute periods to 1 minute periods | |
2208 if( (i > 0) && (period = 10) && (i < 10) ) | |
2209 { | |
2210 read_Buhlmann_times(1); // program coefficients for 1 minute periods | |
2211 period = 1; // set period length (in cycles) to one | |
2212 } | |
2213 } | |
2214 while( i ); | |
2215 | |
2216 | |
2217 // have the computations been done for the "real" tissues? | |
2218 if( (tissue_increment & 128) && (twosectimer || (tissue_increment & 127)) ) | |
2219 { | |
2220 // net tissue balance | |
2221 temp_tissue = temp_tissue_N2 + temp_tissue_He; | |
2222 | |
2223 // check tissue on-/off-gassing and IBCD with applying a threshold of +/-HYST | |
2224 // | |
2225 if ( temp_tissue < -HYST ) // Check if the tissue is off-gassing | |
2226 { | |
2227 deco_tissue_vector |= (1 << ci); // tag tissue as being in decompression | |
2228 IBCD_tissue_vector &= ~(1 << ci); // tag tissue as not experiencing mentionable IBCD | |
2229 } | |
2230 else if ( temp_tissue > +HYST ) // check if the tissue in on-gassing | |
2231 { | |
2232 deco_tissue_vector &= ~(1 << ci); // tag tissue as not being in decompression | |
2233 | |
2234 if( ((temp_tissue_N2 > 0.0) && (temp_tissue_He < 0.0)) // check for counter diffusion | |
2235 || ((temp_tissue_N2 < 0.0) && (temp_tissue_He > 0.0)) ) | |
2236 { | |
2237 IBCD_tissue_vector |= (1 << ci); // tag tissue as experiencing mentionable IBCD | |
2238 } | |
2239 } | |
2240 | |
2241 | |
2242 // keep the saturating / desaturating flags from last invocation | |
2243 char_O_tissue_N2_saturation[ci] &= 128; | |
2244 char_O_tissue_He_saturation[ci] &= 128; | |
2245 | |
2246 // flip the flags applying a hysteresis of HYST (actual value: see #define of HYST) | |
2247 if( temp_tissue_N2 > +HYST ) char_O_tissue_N2_saturation[ci] = 128; // set flag for tissue pressure is increasing | |
2248 else if( temp_tissue_N2 < -HYST ) char_O_tissue_N2_saturation[ci] = 0; // clear flag (-> tissue pressure is decreasing) | |
2249 | |
2250 if( temp_tissue_He > +HYST ) char_O_tissue_He_saturation[ci] = 128; // set flag for tissue pressure is increasing | |
2251 else if( temp_tissue_He < -HYST ) char_O_tissue_He_saturation[ci] = 0; // clear flag (-> tissue pressure is decreasing) | |
2252 | |
2253 | |
2254 // For N2 tissue display purpose: | |
2255 // Scale tissue press so that saturation in 70m on AIR gives a value of approx. 80. | |
2256 // The surface steady-state tissue loading of [0.7902 * (pres_respiration - ppWater)] bar | |
2257 // gives then a 10. If N2 is completely washed out of the tissue, result will be 0. | |
2258 // This scaling is adapted to the capabilities of the tissue graphics in the custom views. | |
2259 temp_tissue = (pres_tissue_N2[ci] / N2_equilibrium) * 10; | |
2260 | |
2261 // limit to 127 to leave space for sat/desat flag | |
2262 if (temp_tissue > 127) temp_tissue = 127; | |
2263 | |
2264 // export as integer | |
2265 char_O_tissue_N2_saturation[ci] += (unsigned char)temp_tissue; | |
2266 | |
2267 | |
2268 // For H2 tissue display purpose: | |
2269 // Scale tissue press so that saturation in 120m on TMX 10/70 gives a value of approx. 70. | |
2270 // With no He in a tissue, result will be 0. | |
2271 // This scaling is adapted to the capabilities of the tissue graphics in the custom views. | |
2272 temp_tissue = pres_tissue_He[ci] * 7.7; | |
2273 | |
2274 // limit to 127 to leave space for sat/desat flag | |
2275 if (temp_tissue > 127) temp_tissue = 127; | |
2276 | |
2277 // export as integer | |
2278 char_O_tissue_He_saturation[ci] += (unsigned char)temp_tissue; | |
2279 } | |
2280 | |
2281 }// for | |
0 | 2282 } |
2283 | |
2284 ////////////////////////////////////////////////////////////////////////////// | |
2285 // calc_limit | |
2286 // | |
2287 // New in v.111 : separated from calc_tissue(), and depends on GF value. | |
2288 // | |
2289 static void calc_limit(void) | |
2290 { | |
560 | 2291 char_O_gtissue_no = 0; |
2292 calc_lead_tissue_limit = 0.0; | |
2293 | |
2294 // clear IBCD, microbubbles and outside warning flags (locked warnings will be preserved) | |
2295 char_O_deco_warnings &= ~(DECO_WARNING_IBCD + DECO_WARNING_MBUBBLES + DECO_WARNING_OUTSIDE); | |
2296 | |
2297 | |
2298 for(ci=0; ci<NUM_COMP; ci++) | |
0 | 2299 { |
2300 overlay float N2 = pres_tissue_N2[ci]; | |
2301 overlay float He = pres_tissue_He[ci]; | |
560 | 2302 overlay float pres_tissue = N2 + He; |
2303 overlay float pres_min; | |
2304 overlay float gf; | |
2305 overlay float threshold; | |
2306 | |
2307 read_Buhlmann_coefficients(); | |
2308 var_N2_a = (var_N2_a * N2 + var_He_a * He) / pres_tissue; | |
2309 var_N2_b = (var_N2_b * N2 + var_He_b * He) / pres_tissue; | |
2310 | |
2311 // calculate minimum ambient pressure that the tissue can withstand according to straight Buhlmann | |
2312 pres_min = (pres_tissue - var_N2_a) * var_N2_b; | |
2313 | |
2314 // calculate current gf value (1.0 = 100%) of this tissue | |
2315 gf = (pres_tissue - pres_respiration) / (pres_tissue - pres_min); | |
2316 if( gf < 0.0 ) gf = 0.0; | |
2317 | |
2318 // calculate a threshold value for use below | |
2319 // ToDo: finalize the definition of the threshold | |
2320 threshold = 0.02 * ci + 0.9; | |
2321 | |
2322 // check if this tissue is likely to develop microbubbles | |
2323 // and/or if this tissue is outside the Buhlmann model | |
2324 if( ci <= 5 ) | |
2325 { | |
2326 if( gf >= threshold ) | |
2327 { | |
2328 char_O_deco_warnings |= (DECO_WARNING_MBUBBLES + DECO_WARNING_MBUBBLES_lock); | |
2329 | |
2330 if( gf >= 1.0 ) | |
2331 { | |
2332 char_O_deco_warnings |= (DECO_WARNING_OUTSIDE + DECO_WARNING_OUTSIDE_lock); | |
2333 } | |
2334 } | |
2335 } | |
2336 else | |
2337 { | |
2338 if( gf >= 1.0 ) | |
2339 { | |
2340 char_O_deco_warnings |= (DECO_WARNING_MBUBBLES + DECO_WARNING_MBUBBLES_lock); | |
2341 | |
2342 if( gf >= threshold ) | |
2343 { | |
2344 char_O_deco_warnings |= (DECO_WARNING_OUTSIDE + DECO_WARNING_OUTSIDE_lock); | |
2345 } | |
2346 } | |
2347 } | |
2348 | |
2349 | |
2350 // Apply the Eric Baker's varying gradient factor correction if the GF-Model is selected. | |
0 | 2351 // Note: the correction factor depends both on GF and b, |
2352 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
2353 // so that can change who is the leading gas... | |
560 | 2354 // Note: Also depends of the GF. So the calculus is different for GF_low, current GF, or GF_high... |
2355 // *BUT* calc_tissue() is used to compute bottom time, hence what would happen at surface, | |
0 | 2356 // hence at GF_high. |
560 | 2357 if( char_I_deco_model != 0 ) pres_min = ( pres_tissue - var_N2_a * ( GF_high) ) * var_N2_b |
2358 / ( GF_high + var_N2_b * (1.0 - GF_high) ); | |
2359 | |
2360 // check if this tissue requires a higher ambient pressure than was found to be needed up to now | |
2361 if( pres_min > calc_lead_tissue_limit ) | |
0 | 2362 { |
560 | 2363 char_O_gtissue_no = ci; |
2364 calc_lead_tissue_limit = pres_min; | |
0 | 2365 } |
2366 } | |
560 | 2367 |
2368 // check IBCD condition | |
2369 if( !IBCD_tissue_vector ) | |
2370 { | |
2371 char_O_deco_warnings &= ~DECO_WARNING_IBCD; // no IBCD in any tissue, clear flag | |
2372 } | |
2373 else if( (IBCD_tissue_vector & (1 << char_O_gtissue_no)) | |
2374 && ((pres_tissue_N2[char_O_gtissue_no] + pres_tissue_He[char_O_gtissue_no]) > pres_respiration) ) | |
2375 { | |
2376 // leading tissue is in IBCD condition and in super-saturation, set flags. | |
2377 char_O_deco_warnings |= (DECO_WARNING_IBCD + DECO_WARNING_IBCD_lock); | |
2378 } | |
2379 | |
2380 // check if is any tissue off-gassing | |
2381 if (deco_tissue_vector) char_O_deco_warnings |= DECO_FLAG; // yes, set deco flag | |
2382 else char_O_deco_warnings &= ~DECO_FLAG; // no, clear deco flag | |
2383 | |
0 | 2384 |
2385 assert( char_O_gtissue_no < NUM_COMP ); | |
2386 assert( 0.0 <= calc_lead_tissue_limit && calc_lead_tissue_limit <= 14.0); | |
2387 } | |
2388 | |
2389 ////////////////////////////////////////////////////////////////////////////// | |
2390 // calc_nullzeit | |
2391 // | |
2392 // calculates the remaining bottom time | |
2393 // | |
2394 // NOTE: Erik Baker's closed formula works for Nitroxes. Trimix adds a second | |
2395 // exponential term to the M-value equation, making it impossible to | |
2396 // invert... So we have to make a fast-simu until we find a better way. | |
2397 // | |
2398 // Input: pres_respiration | |
560 | 2399 // Output: char_O_nullzeit / char_O_alternate_nullzeit |
0 | 2400 // |
2401 static void calc_nullzeit(void) | |
2402 { | |
560 | 2403 overlay unsigned char nullzeit = 240; |
2404 | |
2405 | |
0 | 2406 //---- Compute ppN2 and ppHe --------------------------------------------- |
2407 temp_deco = pres_respiration; | |
2408 sim_alveolar_presures(); | |
560 | 2409 |
0 | 2410 for(ci=0; ci<NUM_COMP; ci++) |
2411 { | |
2412 //---- Read A/B values and loading factor for N2 and He -------------- | |
560 | 2413 |
2414 overlay float tN2 = sim_pres_tissue_N2[ci]; | |
2415 overlay float tHe = sim_pres_tissue_He[ci]; | |
2416 | |
0 | 2417 overlay float t = tN2 + tHe; |
2418 overlay unsigned char ndl; | |
2419 overlay unsigned char period = 10; | |
2420 | |
560 | 2421 read_Buhlmann_coefficients(); |
2422 read_Buhlmann_times(2); // Starts with a 10min period. | |
0 | 2423 |
2424 //---- Simulate for that tissue -------------------------------------- | |
560 | 2425 // NOTE: No need to simulate for longer than the already found NDL. |
2426 for(ndl=0; ndl<nullzeit;) | |
0 | 2427 { |
560 | 2428 //---- Compute updated mix M-value at surface |
2429 overlay float a = (var_N2_a * tN2 + var_He_a * tHe) / t; | |
2430 overlay float b = (var_N2_b * tN2 + var_He_b * tHe) / t; | |
2431 overlay float M0 = (a + pres_surface/b); | |
2432 | |
2433 //---- Add 10min/1min to N2/He tissues | |
2434 overlay float dTN2 = (ppN2 - tN2) * var_N2_e; | |
2435 overlay float dTHe = (ppHe - tHe) * var_He_e; | |
2436 | |
2437 //---- Apply safety margin for both models | |
2438 // NDL can be computed while ascending... SO we have | |
2439 // to check if we are saturating or desaturating. | |
2440 if( dTN2 > 0.0 ) dTN2 *= float_saturation_multiplier; | |
2441 else dTN2 *= float_desaturation_multiplier; | |
2442 | |
2443 if( dTHe > 0.0 ) dTHe *= float_saturation_multiplier; | |
2444 else dTHe *= float_saturation_multiplier; | |
2445 | |
2446 // adopt M0 value when using the GF extension | |
2447 if (char_I_deco_model != 0 ) M0 = GF_high * (M0 - pres_surface) + pres_surface; | |
2448 | |
2449 //---- Simulate off-gassing while going to surface | |
0 | 2450 // TODO ! |
2451 // dTN2 -= exp( ... ascent time ... ppN2...) | |
2452 // dTHe -= exp( ... ascent time ... ppHe...) | |
2453 | |
2454 //---- Ok now, and still ok to surface after 1 or 10 minutes ? | |
2455 if( (t <= M0) && (t + dTN2 + dTHe <= M0) ) | |
2456 { | |
560 | 2457 tN2 += dTN2; // YES: apply gas loadings, |
0 | 2458 tHe += dTHe; |
560 | 2459 t = tN2 + tHe; |
2460 | |
2461 ndl += period; // increment NDL, | |
2462 | |
2463 continue; // and loop. | |
0 | 2464 } |
2465 | |
2466 //---- Should we retry with smaller steps ? | |
2467 if( period == 10 ) | |
2468 { | |
560 | 2469 read_Buhlmann_times(1); // 1min coefs. |
0 | 2470 period = 1; |
560 | 2471 |
0 | 2472 continue; |
2473 } | |
2474 | |
2475 //---- ELSE make a linear approx for the last minute | |
560 | 2476 // Useful to have a meaningful rounding of NDL. |
2477 // But ONLY if positive (negative casted to unsigned is bad). | |
2478 if( M0 > t ) ndl += (unsigned char)(0.5f + (M0-t)/(dTN2+dTHe)); | |
2479 | |
0 | 2480 break; |
2481 } | |
2482 | |
2483 // Keep the shortest NDL found | |
560 | 2484 if ( ndl < nullzeit ) nullzeit = ndl; |
0 | 2485 } |
560 | 2486 |
2487 if( char_O_deco_status & DECO_PLAN_ALTERNATE) char_O_alternate_nullzeit = nullzeit; | |
2488 else char_O_nullzeit = nullzeit; | |
0 | 2489 } |
2490 | |
2491 ////////////////////////////////////////////////////////////////////////////// | |
2492 // calc_ascenttime | |
2493 // | |
560 | 2494 // Sum up ascent from bottom to surface at float_ascent_speed, |
2495 // but 1 minute per meter for the final ascent, and all stops. | |
0 | 2496 // |
560 | 2497 // Result in int_O_ascenttime, |
2498 // or int_O_alternate_ascenttime if doing the alternative plan. | |
2499 // | |
0 | 2500 static void calc_ascenttime(void) |
2501 { | |
560 | 2502 overlay unsigned char x; |
0 | 2503 overlay unsigned short sum; |
560 | 2504 |
2505 // preset final ascent | |
2506 overlay float final = (float)char_I_depth_last_deco; | |
2507 | |
2508 // calculate depth | |
2509 overlay float ascent = (pres_respiration - pres_surface) * BAR_TO_METER; | |
2510 | |
2511 // check if we are already in final ascent | |
2512 if (ascent <= final) | |
2513 { | |
2514 // yes - all ascent is final ascent | |
2515 final = ascent; | |
2516 ascent = 0.0; | |
2517 } | |
2518 else | |
2519 { | |
2520 // no - subtract final ascent part from overall ascent | |
2521 ascent -= final; | |
2522 | |
2523 // compute time for ascent part without final ascent | |
2524 ascent /= float_ascent_speed; | |
2525 } | |
2526 | |
2527 // add 1 minute for each meter of final ascent | |
2528 ascent += final; | |
2529 | |
2530 // convert to integer | |
2531 sum = (unsigned short)(ascent + 0.5); | |
2532 | |
2533 // add all stop times | |
0 | 2534 for(x=0; x<NUM_STOPS && internal_deco_depth[x]; x++) |
2535 sum += (unsigned short)internal_deco_time[x]; | |
2536 | |
560 | 2537 // limit result to display max. |
2538 if( sum > 999) sum = 999; | |
2539 | |
2540 // tag result as invalid if there is an overflow in the stops table | |
2541 if( char_O_deco_warnings & DECO_WARNING_STOPTABLE_OVERFLOW ) sum |= INT_FLAG_INVALID; | |
2542 | |
2543 // route result to output variable | |
2544 if( char_O_deco_status & DECO_PLAN_ALTERNATE ) int_O_alternate_ascenttime = sum; | |
2545 else int_O_ascenttime = sum; | |
0 | 2546 } |
2547 | |
2548 ////////////////////////////////////////////////////////////////////////////// | |
2549 // update_startvalues | |
2550 // | |
2551 // updated in v.102 | |
2552 // | |
2553 void update_startvalues(void) | |
2554 { | |
2555 overlay unsigned char x; | |
2556 | |
2557 // Start ascent simulation with current tissue partial pressures. | |
2558 for(x=0; x<NUM_COMP; x++) | |
2559 { | |
2560 sim_pres_tissue_N2[x] = pres_tissue_N2[x]; | |
2561 sim_pres_tissue_He[x] = pres_tissue_He[x]; | |
2562 } | |
2563 | |
2564 // No leading tissue (yet) for this ascent simulation. | |
2565 sim_lead_tissue_limit = 0.0; | |
560 | 2566 sim_lead_tissue_no = 1; |
0 | 2567 } |
2568 | |
2569 ////////////////////////////////////////////////////////////////////////////// | |
2570 // sim_limit() | |
2571 // | |
2572 // New in v.111 | |
2573 // | |
560 | 2574 // Function separated from calc_tissue() to allow recomputing limit on |
0 | 2575 // different depth, because it depends on current gradient factor. |
2576 // | |
2577 static void sim_limit(PARAMETER float GF_current) | |
2578 { | |
560 | 2579 assert( 0.0 < GF_current && GF_current <= 1.0 ); |
0 | 2580 |
2581 sim_lead_tissue_limit = 0.0; | |
560 | 2582 sim_lead_tissue_no = 0; // If no one is critic, keep first tissue. |
0 | 2583 |
2584 for(ci=0; ci<NUM_COMP; ci++) | |
2585 { | |
2586 overlay float N2 = sim_pres_tissue_N2[ci]; | |
2587 overlay float He = sim_pres_tissue_He[ci]; | |
2588 overlay float p = N2 + He; | |
2589 | |
560 | 2590 read_Buhlmann_coefficients(); |
0 | 2591 var_N2_a = (var_N2_a * N2 + var_He_a * He) / p; |
2592 var_N2_b = (var_N2_b * N2 + var_He_b * He) / p; | |
2593 | |
2594 // Apply the Eric Baker's varying gradient factor correction. | |
2595 // Note: the correction factor depends both on GF and b, | |
2596 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
2597 // so that can change who is the leading gas... | |
2598 // Note: Also depends of the GF_current... | |
560 | 2599 if( char_I_deco_model != 0 ) p = ( p - (var_N2_a * GF_current) ) |
2600 / ( 1.0 - GF_current + (GF_current / var_N2_b ) ); | |
2601 | |
2602 else p = (p - var_N2_a) * var_N2_b; | |
2603 | |
0 | 2604 |
2605 if( p > sim_lead_tissue_limit ) | |
2606 { | |
560 | 2607 sim_lead_tissue_no = ci; |
0 | 2608 sim_lead_tissue_limit = p; |
2609 } | |
2610 } // for ci | |
2611 | |
2612 assert( sim_lead_tissue_no < NUM_COMP ); | |
2613 assert( 0.0 <= sim_lead_tissue_limit && sim_lead_tissue_limit <= 14.0 ); | |
2614 } | |
2615 | |
2616 ////////////////////////////////////////////////////////////////////////////// | |
2617 // clear_deco_table | |
2618 // | |
2619 // | |
2620 static void clear_deco_table(void) | |
2621 { | |
2622 overlay unsigned char x; | |
2623 | |
2624 for(x=0; x<NUM_STOPS; ++x) | |
2625 { | |
2626 internal_deco_time [x] = 0; | |
2627 internal_deco_depth[x] = 0; | |
2628 } | |
560 | 2629 |
2630 // clear stop table overflow warning | |
2631 char_O_deco_warnings &= ~DECO_WARNING_STOPTABLE_OVERFLOW; | |
0 | 2632 } |
2633 | |
2634 ////////////////////////////////////////////////////////////////////////////// | |
2635 // update_deco_table | |
2636 // | |
560 | 2637 // Add time to a stop at temp_depth_limit |
2638 // | |
2639 // It is possible to create stops with a duration of 0 minutes, e.g. to | |
2640 // note a gas change "on the fly" while ascending. Therefore the criteria | |
2641 // to have reached the end of the list needs always to be depth == 0. | |
0 | 2642 // |
560 | 2643 // Input: temp_depth_limit : stop's depth, in meters. |
2644 // sim_gas_last_used : gas used at stop, as index 1..5 or 0 for gas 6 | |
2645 // PARAMETER time_increment : number of minutes to add to the stop | |
0 | 2646 // |
560 | 2647 // Updated: internal_deco_depth[] : depth (in meters) of each stop |
2648 // internal_deco_time [] : time (in minutes) of each stop | |
2649 // internal_deco_gas [] : gas used (index 1-5) at each stop | |
2650 // | |
2651 static unsigned char update_deco_table(PARAMETER unsigned char time_increment) | |
0 | 2652 { |
560 | 2653 overlay unsigned char x; |
2654 | |
2655 assert( temp_depth_limit > 0 ); // No stop at surface... | |
2656 | |
2657 // loop through internal deco table | |
2658 for(x=0; x<NUM_STOPS; ++x) | |
2659 { | |
2660 // Make sure deco-stops are recorded in order: | |
2661 assert( !internal_deco_depth[x] || temp_depth_limit <= internal_deco_depth[x] ); | |
2662 | |
2663 // Is there already a stop entry for our current depth? | |
2664 if( internal_deco_depth[x] == temp_depth_limit ) | |
2665 { | |
2666 // Yes - increment stop time if possible | |
2667 // Stop time entries are limited to 99 minutes because of display constraints. | |
2668 // Else a limit of 254 would account because of constrains in calc_CNS_planning(). | |
2669 if( internal_deco_time[x] < (100 - time_increment) ) | |
2670 { | |
2671 internal_deco_time[x] += time_increment; // increment stop time | |
2672 return 1; // return with status 'success' | |
2673 } | |
2674 } | |
2675 | |
2676 // If program flow passes here, there is either no stop entry for the current depth yet, or | |
2677 // the existing entry is saturated with 99 minutes. So we are looking for the next unused | |
2678 // table entry. | |
2679 if( internal_deco_depth[x] == 0 ) | |
2680 { | |
2681 internal_deco_time[x] = time_increment; // initialize entry with first stop's time, | |
2682 internal_deco_depth[x] = temp_depth_limit; // ... depth, and | |
2683 internal_deco_gas[x] = sim_gas_last_used; // ... gas | |
2684 return 1; // return with status 'success' | |
2685 } | |
2686 } | |
2687 | |
2688 // If program flow passes here, all deco table entries are used up. | |
2689 | |
2690 // set overflow warning | |
2691 char_O_deco_warnings |= DECO_WARNING_STOPTABLE_OVERFLOW; | |
2692 | |
2693 | |
2694 // return with status 'failed'. | |
2695 return 0; | |
0 | 2696 } |
2697 | |
2698 ////////////////////////////////////////////////////////////////////////////// | |
2699 // calc_gradient_factor | |
2700 // | |
2701 // optimized in v.101 (var_N2_a) | |
2702 // new code in v.102 | |
2703 // | |
2704 static void calc_gradient_factor(void) | |
2705 { | |
2706 overlay float gf; | |
2707 overlay float N2 = pres_tissue_N2[char_O_gtissue_no]; | |
2708 overlay float He = pres_tissue_He[char_O_gtissue_no]; | |
2709 | |
2710 assert( char_O_gtissue_no < NUM_COMP ); | |
2711 assert( 0.800 <= pres_respiration && pres_respiration < 14.0 ); | |
2712 | |
560 | 2713 // tissue > respiration (currently off-gassing) |
2714 // GF = 0.00 when respiration == tissue, ie. dissolved gases are at equilibrium. | |
2715 // GF = 1.00 when respiration == limit. | |
0 | 2716 temp_tissue = N2 + He; |
2717 if( temp_tissue <= pres_respiration ) | |
560 | 2718 { |
0 | 2719 gf = 0.0; |
560 | 2720 int_O_gradient_factor = 0; |
2721 } | |
0 | 2722 else |
2723 { | |
2724 overlay float limit = calc_lead_tissue_limit; | |
2725 // NOTE: in GF model, calc_lead_tissue_limit include already the | |
2726 // correction due to gradient factor. To compute the actual | |
560 | 2727 // current GF, we need to (re-)compute the raw ambient-pressure |
313 | 2728 // limit from the Buhlmann model. |
0 | 2729 if( char_I_deco_model != 0 ) |
2730 { | |
2731 ci = char_O_gtissue_no; | |
560 | 2732 |
2733 read_Buhlmann_coefficients(); | |
2734 | |
0 | 2735 var_N2_a = (var_N2_a * N2 + var_He_a * He) / temp_tissue; |
2736 var_N2_b = (var_N2_b * N2 + var_He_b * He) / temp_tissue; | |
560 | 2737 |
2738 limit = (temp_tissue - var_N2_a) * var_N2_b; | |
0 | 2739 } |
2740 | |
560 | 2741 gf = (temp_tissue - pres_respiration) / (temp_tissue - limit); |
2742 | |
2743 // limit to 255 because of constraints in ghostwriter code | |
2744 if ( gf <= 0.0 ) int_O_gradient_factor = 0; | |
2745 else if( gf > 2.545 ) int_O_gradient_factor = 255 + INT_FLAG_WARNING; | |
2746 else | |
2747 { | |
2748 int_O_gradient_factor = (unsigned int)(100 * gf + 0.5); | |
2749 | |
2750 if ( int_O_gradient_factor >= GF_warning_threshold ) | |
2751 int_O_gradient_factor |= INT_FLAG_WARNING; | |
2752 | |
2753 else if ( int_O_gradient_factor >= char_I_GF_High_percentage ) | |
2754 int_O_gradient_factor |= INT_FLAG_PREWARNING; | |
2755 } | |
2756 } | |
0 | 2757 } |
2758 | |
2759 ////////////////////////////////////////////////////////////////////////////// | |
560 | 2760 // calc_desaturation_time |
0 | 2761 // |
2762 // Inputs: int_I_pres_surface, ppWater, char_I_desaturation_multiplier | |
560 | 2763 // Outputs: int_O_desaturation_time, int_O_nofly_time |
0 | 2764 // |
560 | 2765 // Helper function |
2766 // | |
2767 void calc_desaturation_time_helper(void) | |
0 | 2768 { |
560 | 2769 if( pres_actual > pres_target ) // check if actual pressure is higher then target pressure |
2770 { // YES - compute remaining time | |
2771 overlay float pres_ratio; | |
2772 | |
2773 pres_ratio = pres_actual / pres_target; | |
2774 | |
2775 // Compute desaturation time with result rounded up to multiples of 10 minutes. | |
2776 // Main purpose is to avoid confusion, because the times do not clock down in one minute steps any more | |
2777 // but get constantly re-computed according to current ambient pressure and may therefor make steps of | |
2778 // several minutes forwards and backwards as ambient pressure rises and falls. | |
2779 short_time = (unsigned short)( (var_ht * log(pres_ratio) / desat_factor) + 0.9 ); | |
2780 } | |
2781 else | |
2782 { // NO - desaturation state reached, no remaining time | |
2783 short_time = 0; | |
2784 } | |
2785 } | |
2786 | |
2787 ///////////////////////////////////////////////////////////////////////////// | |
2788 // Main function | |
2789 // | |
2790 void calc_desaturation_time(void) | |
2791 { | |
2792 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); | |
2793 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
2794 | |
2795 | |
2796 N2_ratio = 0.7902; // fraction of N2 in respired air | |
2797 pres_surface = 0.001 * int_I_pres_surface; // surface pressure in bar | |
2798 N2_equilibrium = N2_ratio * (pres_surface - ppWater); // partial pressure of N2 in respired air | |
2799 desat_factor = 0.06931 * char_I_desaturation_multiplier * SURFACE_DESAT_FACTOR; // pre-computed term for later use: | |
2800 // 10 [Min] * 0.01 [%] * 0.6931 [ln(2)] * ... | |
2801 int_O_desaturation_time = 0; | |
2802 int_O_nofly_time = 0; | |
2803 | |
2804 | |
2805 for(ci=NUM_COMP; ci>0;) | |
0 | 2806 { |
560 | 2807 overlay float pres_tissue_max; |
2808 overlay float P_ambient_altitude; | |
2809 overlay signed char search_direction; | |
2810 overlay unsigned short nofly_N2 = 0; | |
2811 overlay unsigned short nofly_He = 0; | |
2812 overlay unsigned short nofly_last = ~0; | |
2813 | |
2814 | |
2815 ci -= 1; | |
2816 | |
2817 read_Buhlmann_ht(); | |
2818 read_Buhlmann_coefficients(); | |
2819 | |
2820 // get selected target altitude | |
2821 switch( char_I_altitude_wait ) | |
2822 { | |
2823 case 1: P_ambient_altitude = P_ambient_1000m; break; | |
2824 case 2: P_ambient_altitude = P_ambient_2000m; break; | |
2825 case 3: P_ambient_altitude = P_ambient_3000m; break; | |
2826 default: P_ambient_altitude = P_ambient_fly; break; | |
2827 } | |
2828 | |
2829 // Target pressure for the tissue is the Buhlmann limit. We use the Buhlmann | |
2830 // coefficients for N2 also for He because it is easier to calculate and the | |
2831 // N2 coefficients are more conservative than those for He, so we are on the | |
2832 // safe side, too. | |
2833 pres_tissue_max = (P_ambient_altitude/var_N2_b + var_N2_a); | |
2834 | |
2835 // Adjust target pressure in case the GF model is in use by GF-high | |
2836 if( char_I_deco_model != 0 ) | |
2837 { | |
2838 pres_tissue_max = ((pres_tissue_max - P_ambient_altitude) * char_I_GF_High_percentage * 0.01) + P_ambient_altitude; | |
2839 } | |
2840 | |
2841 | |
2842 // | |
2843 // Desaturation time | |
2844 // | |
2845 | |
2846 // N2: actual amount of tissue pressure above equilibrium. | |
2847 pres_actual = pres_tissue_N2[ci] - N2_equilibrium; | |
2848 | |
2849 // N2: half-time of the current tissue | |
2850 var_ht = var_N2_ht; | |
2851 | |
2852 // Calculate desaturation time for N2 in tissue. | |
2853 // Desaturated state is defined as residual tissue pressure <= 1.05 x ppN2 respired | |
2854 | |
2855 pres_target = 0.05 * N2_equilibrium; | |
2856 | |
2857 calc_desaturation_time_helper(); | |
2858 | |
2859 if( short_time > int_O_desaturation_time) int_O_desaturation_time = short_time; | |
2860 | |
2861 | |
2862 // He: actual amount of tissue pressure above equilibrium. | |
2863 pres_actual = pres_tissue_He[ci]; // equilibrium for He is 0 bar | |
2864 | |
2865 // He: half-time of the current tissue | |
2866 var_ht = var_He_ht; | |
2867 | |
2868 // Calculate desaturation time for He in the tissue. | |
2869 // Desaturated state is defined as residual tissue pressure <= 0.05 x ppN2 respired | |
2870 | |
2871 pres_target = 0.05 * N2_equilibrium; | |
2872 | |
2873 calc_desaturation_time_helper(); | |
2874 | |
2875 if( short_time > int_O_desaturation_time) int_O_desaturation_time = short_time; | |
2876 | |
2877 | |
2878 // | |
2879 // no-fly time | |
2880 // | |
2881 | |
2882 // initialize search direction | |
2883 search_direction = 0; | |
2884 | |
2885 for(;;) | |
2886 { | |
2887 // N2: actual amount of tissue pressure above equilibrium. | |
2888 pres_actual = pres_tissue_N2[ci] - N2_equilibrium; | |
2889 | |
2890 // N2: half-time of the current tissue | |
2891 var_ht = var_N2_ht; | |
2892 | |
2893 // Calculate no-fly time for N2 in the tissue. | |
2894 // Flying is permitted when the N2 pressure fits into the assigned fraction above equilibrium. | |
2895 | |
2896 pres_target = (split_N2_He[ci] * 0.01) * (pres_tissue_max - N2_equilibrium); | |
2897 | |
2898 if( pres_target < 0.0 ) // check if desaturation to fly target is possible | |
2899 { | |
2900 int_O_nofly_time = 288; // NO - set no-fly time to 288 * 10 min = 48 h | |
2901 break; // done for this compartment | |
2902 } | |
2903 else | |
2904 { | |
2905 calc_desaturation_time_helper(); | |
2906 nofly_N2 = short_time; | |
2907 } | |
2908 | |
2909 // He: actual amount of tissue pressure above equilibrium - equilibrium for He is 0 bar. | |
2910 pres_actual = pres_tissue_He[ci]; | |
2911 | |
2912 // He: half-time of the current tissue | |
2913 var_ht = var_He_ht; | |
2914 | |
2915 // Calculate no-fly time for He in the tissue. | |
2916 // Flying is permitted when the He pressure fits into the assigned fraction. | |
2917 | |
2918 pres_target = ((100 - split_N2_He[ci]) * 0.01) * (pres_tissue_max - N2_equilibrium); | |
2919 | |
2920 calc_desaturation_time_helper(); | |
2921 nofly_He = short_time; | |
2922 | |
2923 | |
2924 // Because the sum of N2 and He tissue pressures needs to fit into the Buhlmann limit for | |
2925 // no-fly time calculation, each gas gets assigned a fraction of the available total pressure | |
2926 // limit. The optimum split between the two gases can not be computed by a single formular, | |
2927 // because this would require the inversion of a function with two exponential terms, which is | |
2928 // not possible. We do not want to do a computational complex simulation here like it is done | |
2929 // in the deco calculation code (although we tackle the same base problem here), so we just let | |
2930 // the computer try out which split will balance the no-fly times induced by the N2 and the He | |
2931 // at best. | |
2932 | |
2933 // first of all, skip any optimization in case the current compartment is not the leading one | |
2934 if( (nofly_N2 <= int_O_nofly_time) && (nofly_He <= int_O_nofly_time) ) break; | |
2935 | |
2936 // check if the N2 requires more waiting time than the He | |
2937 if( nofly_N2 >= nofly_He ) | |
2938 { | |
2939 // check if the search direction has changed, which means we are beyond the | |
2940 // optimum now, or if we are at the upper stop limit of split_N2_He | |
2941 if( (search_direction < 0) || (split_N2_He[ci] == 99) ) | |
2942 { | |
2943 // Either the just completed iteration was more close to the optimum or the one before | |
2944 // was, so we take the best (i.e. shortest) time of both as the final no-fly time. | |
2945 int_O_nofly_time = (nofly_N2 < nofly_last) ? nofly_N2 : nofly_last; | |
2946 break; | |
2947 } | |
2948 | |
2949 // store the no-fly time found in this iteration | |
2950 nofly_last = nofly_N2; | |
2951 | |
2952 // increase the N2 fraction of the split and set search direction towards more N2 | |
2953 split_N2_He[ci] += 1; | |
2954 search_direction = +1; | |
2955 } | |
2956 else | |
2957 { | |
2958 // check if the search direction has changed, which means we are beyond the | |
2959 // optimum now, or if we are at the lower stop limit of split_N2_He | |
2960 if( (search_direction > 0) || (split_N2_He[ci] == 1) ) | |
2961 { | |
2962 // Either the just completed iteration was more close to the optimum or the one before | |
2963 // was, so we take the best (i.e. shortest) time of both as the final no-fly time. | |
2964 int_O_nofly_time = (nofly_He < nofly_last) ? nofly_He : nofly_last; | |
2965 break; | |
2966 } | |
2967 | |
2968 // store the no-fly time found in this iteration | |
2969 nofly_last = nofly_He; | |
2970 | |
2971 // decrease the N2 fraction of the split and set search direction towards less N2 | |
2972 split_N2_He[ci] -= 1; | |
2973 search_direction = -1; | |
2974 } | |
2975 | |
2976 } // for(;;) | |
2977 | |
2978 } // for(compartments) | |
2979 | |
2980 | |
2981 // Rescale int_O_desaturation_time and int_O_nofly_time to full minutes for display purpose | |
2982 int_O_desaturation_time *= 10; | |
2983 int_O_nofly_time *= 10; | |
2984 | |
2985 // Limit int_O_desaturation_time and int_O_nofly_time to 5999 = 99 hours + 59 minutes | |
2986 // because of display space constraints and rounding done above. | |
2987 if( int_O_desaturation_time > 5999 ) int_O_desaturation_time = 5999; | |
2988 if( int_O_nofly_time > 5999 ) int_O_nofly_time = 5999; | |
2989 | |
2990 | |
2991 // Clear the microbubbles warning when the current gradient factor is < GF_warning_threshold. | |
2992 // As the locked warning will stay set, this will cause the warning be be displayed in attention | |
2993 // color instead of warning color. | |
2994 if( int_O_gradient_factor < GF_warning_threshold ) char_O_deco_warnings &= ~DECO_WARNING_MBUBBLES; | |
2995 | |
2996 // clear some warnings when the desaturation time has become zero | |
2997 if( int_O_desaturation_time == 0 ) char_O_deco_warnings &= ~( DECO_WARNING_IBCD + DECO_WARNING_IBCD_lock | |
2998 + DECO_WARNING_MBUBBLES + DECO_WARNING_MBUBBLES_lock | |
2999 + DECO_WARNING_OUTSIDE + DECO_WARNING_OUTSIDE_lock ); | |
3000 | |
0 | 3001 } |
3002 | |
3003 ////////////////////////////////////////////////////////////////////////////// | |
3004 // calc_wo_deco_step_1_min | |
3005 // | |
3006 // optimized in v.101 (...saturation_multiplier) | |
3007 // desaturation slowed down to 70,42% | |
3008 // | |
560 | 3009 // Input: int_I_pres_surface [mbar] |
3010 // | |
0 | 3011 static void calc_wo_deco_step_1_min(void) |
3012 { | |
560 | 3013 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); |
3014 assert( 100 <= char_I_saturation_multiplier && char_I_saturation_multiplier < 200 ); | |
3015 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
3016 | |
3017 // setup input data for deco routines | |
0 | 3018 pres_respiration = pres_surface = int_I_pres_surface * 0.001; |
560 | 3019 |
3020 N2_ratio = 0.7902; // according to Buhlmann | |
3021 N2_equilibrium = N2_ratio * (pres_surface - ppWater); // used for N2 tissue graphics scaling | |
3022 ppN2 = N2_ratio * (pres_respiration - ppWater); | |
3023 ppHe = 0.0; | |
3024 | |
3025 float_desaturation_multiplier = char_I_desaturation_multiplier * 0.01 * SURFACE_DESAT_FACTOR; | |
0 | 3026 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; |
3027 | |
560 | 3028 |
3029 // program what to do: 128 = Flag for "real" tissues, 1 = 1 minute | |
3030 tissue_increment = 128 + 1; | |
3031 | |
3032 // update the pressure in the tissues N2/He in accordance with the new ambient pressure | |
3033 calc_tissue(); | |
3034 | |
3035 // clock down CNS by a 1 minute step | |
3036 //CNS_fraction *= 0.992327946; // is done in deco_calc_CNS_decrease_15min | |
3037 | |
3038 // compute integer copy of CNS value | |
3039 //compute_CNS_for_display(); // is done in deco_calc_CNS_decrease_15min | |
3040 | |
3041 // reset deco engine start condition (probably not needed to be done here...) | |
3042 char_O_deco_status &= ~DECO_STATUS_MASK; // clear bits | |
3043 char_O_deco_status |= DECO_STATUS_INIT; // set bits | |
3044 | |
3045 // reset some more data that are not applicable in surface mode | |
3046 char_O_nullzeit = 0; | |
3047 int_O_ascenttime = 0; | |
3048 int_O_alternate_ascenttime = 0; | |
3049 clear_deco_table(); | |
3050 | |
3051 // calculate gradient factor | |
0 | 3052 calc_gradient_factor(); |
3053 } | |
3054 | |
3055 ////////////////////////////////////////////////////////////////////////////// | |
3056 // calc_dive_interval | |
3057 // | |
3058 // Prepare tissue for delay before the next dive simulation. | |
3059 // | |
560 | 3060 // Inputs: char_I_dive_interval == delay before dive (in 1 Minute steps). |
3061 // Modified: CNS_fraction, int_O_CNS_fraction | |
3062 // pres_tissue_N2/He[] | |
0 | 3063 // |
3064 // Should be protected by deco_push_tissues_to_vault(), | |
3065 // deco_pull_tissues_from_vault() | |
3066 // | |
3067 // desaturation slowed down to 70,42%. | |
3068 // | |
3069 static void calc_dive_interval(void) | |
3070 { | |
3071 overlay unsigned char t; | |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
3072 |
0 | 3073 //---- Initialize simulation parameters ---------------------------------- |
3074 pres_respiration = pres_surface = int_I_pres_surface * 0.001; | |
560 | 3075 |
3076 N2_ratio = 0.7902; // according to buehlmann | |
3077 N2_equilibrium = N2_ratio * (pres_surface - ppWater); // used for N2 tissue graphics scaling | |
3078 ppN2 = N2_ratio * (pres_respiration - ppWater); | |
3079 ppHe = 0.0; | |
3080 | |
3081 float_desaturation_multiplier = char_I_desaturation_multiplier * 0.01 * SURFACE_DESAT_FACTOR; | |
0 | 3082 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; |
560 | 3083 |
0 | 3084 //---- Perform simulation ------------------------------------------------ |
560 | 3085 |
3086 // Calculate tissues: | |
3087 // Because tissue_increment is limited to 127 minutes, we have to do two passes | |
3088 // in case char_I_dive_interval is bigger than 127. | |
3089 // Ops: char_I_dive_interval must be limited to 254! | |
3090 | |
3091 t = char_I_dive_interval; | |
3092 | |
3093 if ( t == 255 ) t = 254; | |
3094 | |
3095 if ( t > 127 ) // extra pass needed? | |
3096 { | |
3097 tissue_increment = 127 // dive interval length in minutes | |
3098 | 128; // Flag to update the "real" tissues | |
3099 | |
3100 calc_tissue(); // update tissues | |
3101 | |
3102 t -= 127; // calculate remaining dive interval length | |
3103 } | |
3104 | |
3105 tissue_increment = t // dive interval length in minutes to do | |
3106 | 128; // Flag to update the "real" tissues | |
3107 calc_tissue(); // update tissues | |
3108 | |
3109 | |
3110 // Calculate CNS: | |
3111 // To speed up things and because on most invocations of this code char_I_dive_interval | |
3112 // is a multiple of 10 minutes, we loop the loop-counter down using two speeds. | |
3113 | |
3114 t = char_I_dive_interval; | |
3115 | |
3116 while ( t ) | |
3117 { | |
3118 if( t > 9 ) | |
3119 { | |
3120 CNS_fraction *= 0.925874712; // Half-time = 90min -> 10 min: (1/2)^(1/9) | |
3121 t -= 10; // fast speed looping | |
3122 } | |
3123 else | |
3124 { | |
3125 CNS_fraction *= 0.992327946; // Half-time = 90min -> 1 min: (1/2)^(1/90) | |
3126 t -= 1; // slow speed looping | |
3127 } | |
3128 } | |
3129 | |
3130 // compute integer copy of CNS value | |
3131 compute_CNS_for_display(); | |
0 | 3132 } |
3133 | |
3134 ////////////////////////////////////////////////////////////////////////////// | |
560 | 3135 // clear_CNS_fraction |
0 | 3136 // |
560 | 3137 // new in v.101 |
0 | 3138 // |
560 | 3139 void clear_CNS_fraction(void) |
0 | 3140 { |
560 | 3141 CNS_fraction = CNS_sim_norm_fraction = CNS_sim_alt_fraction = 0; |
3142 int_O_CNS_fraction = int_O_normal_CNS_fraction = int_O_alternate_CNS_fraction = 0; | |
3143 } | |
3144 | |
3145 ////////////////////////////////////////////////////////////////////////////// | |
3146 // calc_CNS_fraction | |
3147 // | |
3148 // Input: char_actual_ppO2 : current ppO2 [decibars] | |
3149 // tissue_increment : time increment and tissue selector | |
3150 // CNS_fraction : current CNS% as float before period | |
3151 // | |
3152 // Output: CNS_fraction, int_O_CNS_fraction - for the real tissues | |
3153 // CNS_sim_norm_fraction, int_O_normal_CNS_fraction - in simulation mode, normal plan | |
3154 // CNS_sim_alt_fraction, int_O_alternate_CNS_fraction - in simulation mode, alternative plan | |
3155 // | |
3156 void calc_CNS_fraction(void) | |
3157 { | |
3158 overlay float time_factor = 1.0; // default is 2sec | |
3159 overlay float CNS_fraction_temp = 0.0; | |
3160 | |
3161 assert( char_actual_ppO2 > 15 ); | |
3162 | |
3163 // All deco code is now invoked every second. But as the CNS update is based on | |
3164 // 2 seconds periods, we skip every 2nd seconds-based invocation of this function. | |
3165 // 128 = 128 (flag for "real" CNS) + 0 (2 seconds period) | |
3166 // To distribute computational load, the CNS% is calculated in "the other second" | |
3167 // than the tissues. | |
3168 if( (tissue_increment == 128) && (twosectimer) ) return; | |
3169 | |
3170 // adjust time factor if minute-based stepping is commanded, mask out flag bit | |
3171 if( tissue_increment & 127 ) time_factor = 30.0 * (float)(tissue_increment & 127); | |
3172 | |
3173 | |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
3174 //------------------------------------------------------------------------ |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
3175 // Don't increase CNS below 0.5 bar, but keep it steady. |
560 | 3176 if (char_actual_ppO2 < 50) |
0 | 3177 ; // no changes |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
3178 //------------------------------------------------------------------------ |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
3179 // Below (and including) 1.60 bar |
560 | 3180 else if (char_actual_ppO2 < 61) |
3181 CNS_fraction_temp = time_factor/(-533.07 * char_actual_ppO2 + 54000.0); | |
3182 else if (char_actual_ppO2 < 71) | |
3183 CNS_fraction_temp = time_factor/(-444.22 * char_actual_ppO2 + 48600.0); | |
3184 else if (char_actual_ppO2 < 81) | |
3185 CNS_fraction_temp = time_factor/(-355.38 * char_actual_ppO2 + 42300.0); | |
3186 else if (char_actual_ppO2 < 91) | |
3187 CNS_fraction_temp = time_factor/(-266.53 * char_actual_ppO2 + 35100.0); | |
3188 else if (char_actual_ppO2 < 111) | |
3189 CNS_fraction_temp = time_factor/(-177.69 * char_actual_ppO2 + 27000.0); | |
3190 else if (char_actual_ppO2 < 152) | |
3191 CNS_fraction_temp = time_factor/( -88.84 * char_actual_ppO2 + 17100.0); | |
3192 else if (char_actual_ppO2 < 167) | |
3193 CNS_fraction_temp = time_factor/(-222.11 * char_actual_ppO2 + 37350.0); | |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
3194 //------------------------------------------------------------------------ |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
3195 // Arieli et all.(2002): Modeling pulmonary and CNS O2 toxicity: |
322 | 3196 // J Appl Physiol 92: 248--256, 2002, doi:10.1152/japplphysiol.00434.2001 |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
3197 // Formula (A1) based on value for 1.55 and c=20 |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
3198 // example calculation: Sqrt((1.7/1.55)^20)*0.000404 |
560 | 3199 else if (char_actual_ppO2 < 172) |
3200 CNS_fraction_temp = time_factor*0.00102; | |
3201 else if (char_actual_ppO2 < 177) | |
3202 CNS_fraction_temp = time_factor*0.00136; | |
3203 else if (char_actual_ppO2 < 182) | |
3204 CNS_fraction_temp = time_factor*0.00180; | |
3205 else if (char_actual_ppO2 < 187) | |
3206 CNS_fraction_temp = time_factor*0.00237; | |
3207 else if (char_actual_ppO2 < 192) | |
3208 CNS_fraction_temp = time_factor*0.00310; | |
3209 else if (char_actual_ppO2 < 198) | |
3210 CNS_fraction_temp = time_factor*0.00401; | |
3211 else if (char_actual_ppO2 < 203) | |
3212 CNS_fraction_temp = time_factor*0.00517; | |
3213 else if (char_actual_ppO2 < 233) | |
3214 CNS_fraction_temp = time_factor*0.0209; | |
0 | 3215 else |
560 | 3216 CNS_fraction_temp = time_factor*0.0482; // value for 2.5 bar, used for 2.33 bar and above |
3217 | |
3218 | |
3219 // Check from where we were called: | |
3220 // flag (bit 7) is set -> we were called from calc_hauptroutine() | |
3221 // flag (bit 7) not set -> we were called from the deco planning routines | |
3222 if ( tissue_increment & 128 ) CNS_fraction += CNS_fraction_temp; // real tissues | |
3223 else if ( char_O_deco_status & DECO_PLAN_ALTERNATE ) CNS_sim_alt_fraction += CNS_fraction_temp; // alternative plan | |
3224 else CNS_sim_norm_fraction += CNS_fraction_temp; // normal plan | |
3225 | |
0 | 3226 } |
3227 | |
3228 ////////////////////////////////////////////////////////////////////////////// | |
560 | 3229 // calc_CNS_planning |
0 | 3230 // |
3231 // Compute CNS during predicted ascent. | |
3232 // | |
3233 // Note: Needs a call to deco_push_tissues_to_vault(), | |
3234 // deco_pull_tissues_from_vault() to avoid trashing everything... | |
3235 // | |
560 | 3236 // Input: CNS_fraction, internal_deco_time[], internal_deco_depth[], internal_deco_gas[] |
3237 // Output: CNS_fraction, int_O_normal_CNS_fraction / int_O_alternate_CNS_fraction | |
0 | 3238 // |
560 | 3239 void calc_CNS_planning(void) |
0 | 3240 { |
560 | 3241 // start with CNS% we already have |
3242 if( char_O_deco_status & DECO_PLAN_ALTERNATE ) CNS_sim_alt_fraction = CNS_fraction; | |
3243 else CNS_sim_norm_fraction = CNS_fraction; | |
3244 | |
3245 | |
3246 //---- CCR mode : do the full TTS at once --------------------------------- | |
3247 | |
3248 if( ((char_O_deco_status & DECO_MODE_MASK) == DECO_MODE_CCR) ) | |
3249 { | |
3250 overlay unsigned short t; // needs 16 bits here ! | |
3251 | |
3252 // get current ppO2 from sensors or setpoint | |
3253 char_actual_ppO2 = char_I_const_ppO2; | |
3254 | |
3255 // calculate CNS% for the period of additional staying at bottom depth (fTTS / delayed ascent) | |
3256 if( char_O_deco_status & DECO_ASCENT_DELAYED) | |
3257 { | |
3258 tissue_increment = char_I_extra_time; // must be limited to 127, is limited by range of char_I_extra_time | |
3259 calc_CNS_fraction(); | |
3260 } | |
3261 | |
3262 // get the ascent time dependent on the current plan | |
3263 t = (char_O_deco_status & DECO_PLAN_ALTERNATE) ? int_O_alternate_ascenttime : int_O_ascenttime; | |
3264 | |
3265 // start simulating CNS% in chunks of 127 minutes | |
3266 tissue_increment = 127; | |
3267 | |
3268 while( t > 127 ) | |
3269 { | |
3270 t -= 127; // tissue_increment is limited to 127 minutes because of flag in bit 7 | |
3271 calc_CNS_fraction(); // calculate CNS in chunks of full 127 minutes | |
3272 } | |
3273 | |
3274 tissue_increment = (char)t; // get the remaining minutes <= 127 | |
3275 calc_CNS_fraction(); // calculate CNS for the remaining minutes | |
3276 } | |
3277 else //---- OC mode and pSCR without sensors: have to follow all gas switches... ----- | |
0 | 3278 { |
560 | 3279 overlay float float_actual_ppO2; |
3280 overlay float abs_pres; | |
3281 | |
3282 overlay unsigned char stop_depth; | |
3283 overlay unsigned char last_gas; | |
3284 overlay unsigned char i; // stop table index | |
3285 | |
3286 | |
3287 // retrieve bottom gas: 1-5 for the configured gases or 0 for the manually set gas | |
3288 last_gas = sim_gas_last_used = sim_gas_first_used; | |
3289 | |
3290 // get the calc_N2/He/O2_ratios of the bottom gas | |
3291 gas_switch_set(); | |
3292 | |
3293 // calculate absolute pressure | |
3294 abs_pres = pres_surface + bottom_depth * METER_TO_BAR; | |
3295 | |
3296 // switch on deco mode pSCR / OC | |
3297 if( char_O_deco_status & DECO_MODE_PSCR ) | |
3298 { | |
3299 //---- pSCR calculated -------------------------------------------- | |
3300 | |
3301 // abs_pres is 0.0 ... in bar | |
3302 // calc_O2_ratio is 0.0 ... 1.0 as factor | |
3303 // char_I_PSCR_drop is 0 ... 15 as % | |
3304 // char_I_PSCR_lungratio is 5 ... 20 as % | |
3305 // float_actual_ppO2 is 0.0 ... in cbar (!) | |
3306 | |
3307 float_actual_ppO2 = (100 * abs_pres * calc_O2_ratio) | |
3308 - (1.0 - calc_O2_ratio) * char_I_PSCR_drop * char_I_PSCR_lungratio; | |
3309 } | |
3310 else | |
3311 { | |
3312 //---- OC --------------------------------------------------------- | |
3313 | |
3314 float_actual_ppO2 = abs_pres * calc_O2_ratio * 100; // in cbar (!) | |
3315 } | |
3316 | |
3317 // caution: float_actual_ppO2 is in cbar here! | |
3318 if ( float_actual_ppO2 < 0.0 ) char_actual_ppO2 = 0; | |
3319 else if ( float_actual_ppO2 > 254.5 ) char_actual_ppO2 = 255; | |
3320 else char_actual_ppO2 = (unsigned char)(float_actual_ppO2 + 0.5); | |
3321 | |
3322 | |
3323 // simulate extended bottom time (fTTS) / delay before ascent (bailout) if configured | |
3324 if( char_O_deco_status & DECO_ASCENT_DELAYED ) | |
3325 { | |
3326 tissue_increment = char_I_extra_time; // must be limited to 127, is limited by range of char_I_extra_time | |
3327 calc_CNS_fraction(); | |
3328 } | |
3329 | |
3330 | |
3331 // For simplicity reason (non-linearity of the relation between ppO2 and CNS increments), the | |
3332 // whole ascent is calculated with bottom ppO2. This errs, but it does so to the safe side. | |
3333 | |
3334 // calculate ascent time (integer division and generous round-up) | |
3335 tissue_increment = bottom_depth / char_I_ascent_speed + 1; | |
3336 | |
3337 // ** commented out - not needed when char_I_ascent_speed is limited to a | |
3338 // ** minimum of 2.something, it is indeed limited to 5. | |
3339 // | |
3340 // // limit tissue_increment to 127 minutes | |
3341 // if( tissue_increment > 127 ) tissue_increment = 127; | |
3342 | |
3343 // simulate the CNS increase | |
3344 calc_CNS_fraction(); | |
3345 | |
3346 | |
3347 //---- Stops --------------------------------------------------------- | |
3348 | |
0 | 3349 for(i=0; i<NUM_STOPS; ++i) |
3350 { | |
560 | 3351 // get the depth of the stop |
3352 stop_depth = internal_deco_depth[i]; | |
3353 | |
3354 // did we reach the last entry (depth = 0)? if yes, done | |
3355 if (stop_depth == 0) break; | |
3356 | |
3357 // get the duration of the stop and the gas breathed | |
3358 tissue_increment = internal_deco_time[i]; | |
3359 sim_gas_last_used = internal_deco_gas[i]; | |
3360 | |
3361 // do we have a gas switch? | |
3362 if( sim_gas_last_used != last_gas ) | |
0 | 3363 { |
560 | 3364 // yes - get new calc ratios |
3365 gas_switch_set(); | |
3366 | |
3367 // remember new gas as last gas | |
3368 last_gas = sim_gas_last_used; | |
3369 } | |
3370 | |
3371 // calculate absolute pressure at stop depth | |
3372 abs_pres = pres_surface + stop_depth * METER_TO_BAR; | |
3373 | |
3374 // pSCR mode | |
3375 if( char_O_deco_status & DECO_MODE_PSCR ) | |
3376 { | |
3377 // abs_pres is 0.0 ... in bar | |
3378 // calc_O2_ratio is 0.0 ... 1.0 as factor | |
3379 // char_I_PSCR_drop is 0 ... 15 as % | |
3380 // char_I_PSCR_lungratio is 5 ... 20 as % | |
3381 // float_actual_ppO2 is 0.0 ... in cbar (!) | |
3382 | |
3383 float_actual_ppO2 = (100 * abs_pres * calc_O2_ratio) | |
3384 - (1.0 - calc_O2_ratio) * char_I_PSCR_drop * char_I_PSCR_lungratio; | |
3385 } | |
3386 else // OC mode | |
3387 { | |
3388 float_actual_ppO2 = abs_pres * calc_O2_ratio * 100; // in cbar (!) | |
3389 } | |
3390 | |
3391 // caution: float_actual_ppO2 is in cbar here! | |
3392 if ( float_actual_ppO2 < 0.0 ) char_actual_ppO2 = 0; | |
3393 else if ( float_actual_ppO2 > 254.5 ) char_actual_ppO2 = 255; | |
3394 else char_actual_ppO2 = (unsigned char)(float_actual_ppO2 + 0.5); | |
3395 | |
3396 | |
3397 // ** Currently, stop times per stop entry are limited to 99 minutes in update_deco_table(), | |
3398 // ** so the following code block is not needed at times. | |
3399 // | |
3400 // // tissue_increment is limited to 127 when fed to deco_calc_CNS_fraction(), | |
3401 // // so if the stop is longer than 127 minutes (but not longer than 254 minutes!) | |
3402 // // we need to calculate the CNS in two chunks. | |
3403 // if( tissue_increment > 127) | |
3404 // { | |
3405 // tissue_increment -= 127; // subtract full 127 minutes and do the "remaining" minutes first | |
3406 // calc_CNS_fraction(); | |
3407 // tissue_increment = 127; // catch up with the previously subtracted full 127 minutes | |
3408 // } | |
3409 | |
3410 // calculate CNS% for the stop | |
3411 calc_CNS_fraction(); | |
0 | 3412 } |
3413 } | |
3414 } | |
3415 | |
560 | 3416 |
0 | 3417 ////////////////////////////////////////////////////////////////////////////// |
560 | 3418 // gas_volumes |
3419 // | |
3420 // calculates volumes and required tank fill pressures for each gas. | |
0 | 3421 // |
560 | 3422 // Input: bottom_depth depth of the bottom segment |
3423 // char_I_bottom_time duration of the bottom segment | |
3424 // char_I_extra_time extra bottom time for fTTS / delayed ascent | |
3425 // float_ascent_speed ascent speed, in meters/minute | |
3426 // sim_gas_first_used the bottom gas (1-5 for configured gases, 0 for the manual gas) | |
3427 // internal_deco_depth[] depth of the stops | |
3428 // internal_deco_time[] duration of the stops | |
3429 // internal_deco_gas[] gas breathed at the stops | |
3430 // char_I_bottom_usage gas consumption during bottom part and initial ascent, in liters/minute | |
3431 // char_I_deco_usage gas consumption during stops and following ascents, in liters/minute | |
3432 // char_I_tank_size[] size of the tanks for gas 1-5, in liters | |
3433 // char_I_tank_pres_fill[] fill pressure of the tanks | |
0 | 3434 // |
560 | 3435 // Output: int_O_gas_volumes[] amount of gas needed, in liters |
3436 // int_O_tank_pres_need[] in bar, + flags for fast evaluation by dive mode warnings: | |
3437 // 2^15: pres_need >= pres_fill | |
3438 // 2^14: pres_need >= press_fill * GAS_NEEDS_ATTENTION_THRESHOLD | |
3439 // 2^11: pres_need == 0 | |
3440 // 2^10: pres_need invalid | |
0 | 3441 // |
560 | 3442 void gas_volumes_helper(void) |
3443 { | |
3444 // Calculate the gas volume needed at a given depth, time and usage (SAC rate). | |
3445 // We use 1.0 for the surface pressure to have stable results when used through | |
3446 // the deco calculator (simulation mode). | |
3447 volume = (float_depth * METER_TO_BAR + 1.0) * float_time * usage; | |
3448 | |
3449 return; | |
3450 } | |
3451 | |
3452 void gas_volumes(void) | |
0 | 3453 { |
560 | 3454 overlay float volumes[NUM_GAS]; |
3455 | |
3456 overlay unsigned char stop_gas; | |
3457 overlay unsigned char stop_gas_last; | |
3458 overlay unsigned char stop_time; | |
3459 overlay unsigned char stop_depth; | |
3460 overlay unsigned char stop_depth_last; | |
3461 overlay unsigned char i; | |
3462 | |
3463 | |
3464 //---- initialization ---------------------------------------------------- | |
3465 | |
3466 // null the volume accumulators | |
3467 for(i=0; i<NUM_GAS; ++i) volumes[i] = 0.0; | |
3468 | |
3469 // quit for CCR and pSCR mode | |
3470 if( char_O_deco_status & DECO_MODE_LOOP ) goto done; | |
3471 | |
3472 | |
3473 //---- bottom demand ----------------------------------------------------- | |
3474 | |
3475 // sim_gas_first_used : gas used during bottom segment (0, 1-5) | |
3476 // bottom_depth: depth of the bottom segment | |
3477 | |
3478 assert(0 <= sim_gas_first_used && sim_gas_first_used <= NUM_GAS); | |
3479 | |
3480 // get the gas used during bottom segment | |
3481 stop_gas_last = stop_gas = sim_gas_first_used; | |
3482 | |
3483 // set the usage (SAC rate) to bottom usage rate for bottom part and initial ascent | |
3484 usage = char_I_bottom_usage; | |
3485 | |
3486 // volumes are only calculated for gases 1-5, but not the manually configured one | |
3487 if( stop_gas ) | |
3488 { | |
3489 // set the bottom depth | |
3490 float_depth = (float)bottom_depth; | |
3491 | |
3492 // calculate either bottom segment or just the fTTS/bailout delayed part | |
3493 if( char_O_main_status & DECO_BOTTOM_CALCULATE ) | |
3494 { | |
3495 // duration of bottom segment | |
3496 float_time = (float)char_I_bottom_time; | |
3497 } | |
3498 else | |
3499 { | |
3500 // duration of delayed ascent | |
3501 float_time = (float)char_I_extra_time; | |
3502 } | |
3503 | |
3504 // calculate gas demand | |
3505 gas_volumes_helper(); | |
3506 | |
3507 // take result | |
3508 volumes[stop_gas-1] = volume; | |
3509 } | |
3510 | |
3511 | |
3512 // initialize stop index with first stop | |
3513 i = 0; | |
3514 | |
3515 | |
3516 //---- initial ascent demand --------------------------------------------- | |
3517 | |
3518 // stop_gas : gas from bottom segment | |
3519 // bottom_depth : depth of the bottom segment in meters | |
3520 // internal_deco_depth[i=0]: depth of the first stop, may be 0 if no stop exists | |
3521 | |
3522 // get the data of the first stop | |
3523 stop_depth = internal_deco_depth[i]; | |
3524 stop_time = internal_deco_time[i]; | |
3525 | |
3526 // volumes are only calculated for gases 1-5, but not the manually configured one | |
3527 if( stop_gas ) | |
3528 { | |
3529 // compute distance between bottom and first stop | |
3530 float_depth = (float)bottom_depth - (float)stop_depth; | |
3531 | |
3532 // initial ascent exists only if ascent distance is > 0 | |
3533 if( float_depth > 0.0 ) | |
3534 { | |
3535 // compute ascent time | |
3536 float_time = float_depth / float_ascent_speed; | |
3537 | |
3538 // compute average depth between bottom and first stop | |
3539 float_depth = (float)bottom_depth - float_depth * 0.5; | |
3540 | |
3541 // calculate gas demand | |
3542 gas_volumes_helper(); | |
3543 | |
3544 // add result | |
3545 volumes[stop_gas-1] += volume; | |
3546 } | |
3547 } | |
3548 | |
3549 // switch the usage (SAC rate) to deco usage rate | |
3550 // for stops, intermediate and final ascent | |
3551 usage = char_I_deco_usage; | |
3552 | |
3553 // is there a (first) stop? if yes, goto stops processing | |
3554 if( stop_depth ) goto stops; | |
3555 | |
3556 // add demand of a 3 minutes safety stop at 5 meters, at least for contingency... | |
3557 float_time = 3.0; | |
3558 float_depth = 5.0; | |
3559 | |
3560 // calculate gas demand | |
3561 gas_volumes_helper(); | |
3562 | |
3563 // add result | |
3564 volumes[stop_gas-1] += volume; | |
3565 | |
3566 // proceed to volume conversion and pressure calculations | |
3567 goto done; | |
3568 | |
3569 | |
3570 //---- intermediate ascent demand --------------------------------------- | |
3571 inter_ascents: | |
3572 | |
3573 // store last stop depth and gas | |
3574 stop_depth_last = stop_depth; | |
3575 stop_gas_last = stop_gas; | |
3576 | |
3577 // check if we are at the end of the stops table | |
3578 if( i < NUM_STOPS-1 ) | |
3579 { | |
3580 // there are more entries - get the next stop data | |
3581 i++; | |
3582 | |
3583 // get the next stop depth | |
3584 stop_depth = internal_deco_depth[i]; | |
3585 | |
3586 // check if there is indeed another stop, | |
3587 // if not (depth = 0) treat as end of table | |
3588 if( stop_depth == 0 ) goto end_of_table; | |
3589 | |
3590 // get the next stop duration | |
3591 stop_time = internal_deco_time[i]; | |
3592 } | |
3593 else | |
3594 { | |
3595 end_of_table: | |
3596 | |
3597 // End of the stops table reached or no more stops: Split the remaining | |
3598 // ascent into an intermediate ascent and a final ascent by creating a | |
3599 // dummy stop at the usual last deco stop depth. Stop gas doesn't change. | |
3600 stop_time = 0; | |
3601 stop_depth = char_I_depth_last_deco; | |
3602 } | |
3603 | |
3604 // volumes are only calculated for gases 1-5, but not the manually configured one | |
3605 if( stop_gas_last ) | |
3606 { | |
3607 // compute distance between the two stops: | |
3608 // last stop will always be deeper than current stop | |
3609 float_depth = (float)(stop_depth_last - stop_depth); | |
3610 | |
3611 // compute ascent time | |
3612 float_time = float_depth / float_ascent_speed; | |
3613 | |
3614 // compute average depth between the two stops | |
3615 float_depth = (float)stop_depth_last - float_depth * 0.5; | |
3616 | |
3617 // calculate gas demand | |
3618 gas_volumes_helper(); | |
3619 | |
3620 // add result | |
3621 volumes[stop_gas_last-1] += volume; | |
3622 } | |
3623 | |
3624 | |
3625 //---- next stop demand ------------------------------------------------- | |
3626 stops: | |
3627 | |
3628 // convert depth of the stop | |
3629 float_depth = (float)stop_depth; | |
3630 | |
3631 // get the next gas | |
3632 stop_gas = internal_deco_gas[i]; | |
3633 | |
3634 // do we we have a gas change? | |
3635 if( stop_gas_last && (stop_gas != stop_gas_last) ) | |
3636 { | |
3637 // yes - spend an additional char_I_gas_change_time on the old gas | |
3638 float_time = (float)char_I_gas_change_time; | |
3639 | |
3640 // calculate gas demand | |
3641 gas_volumes_helper(); | |
3642 | |
3643 // add result | |
3644 volumes[stop_gas_last-1] += volume; | |
3645 } | |
3646 | |
3647 // calculate and add demand on new gas for the full stop duration | |
3648 if( stop_gas ) | |
3649 { | |
3650 // get the duration of the stop | |
3651 float_time = (float)stop_time; | |
3652 | |
3653 // calculate gas demand | |
3654 gas_volumes_helper(); | |
3655 | |
3656 // add result to last gas | |
3657 volumes[stop_gas-1] += volume; | |
3658 } | |
3659 | |
3660 // continue with the next intermediate ascent if this was not the last stop | |
3661 if( stop_depth > char_I_depth_last_deco ) goto inter_ascents; | |
3662 | |
3663 | |
3664 //---- final ascent demand ----------------------------------------------- | |
3665 final_ascent: | |
3666 | |
3667 // float_depth: depth of last stop | |
3668 // stop_gas : gas from last stop (0 or 1-5) | |
3669 | |
3670 // volumes are only calculated for gases 1-5, but not the manually configured one | |
3671 if( stop_gas ) | |
3672 { | |
3673 // set ascent time according to an ascent speed of 1 meter per minute | |
3674 float_time = float_depth; | |
3675 | |
3676 // set half-way depth | |
3677 float_depth *= 0.5; | |
3678 | |
3679 // calculate gas demand | |
3680 gas_volumes_helper(); | |
3681 | |
3682 // add result | |
3683 volumes[stop_gas-1] += volume; | |
3684 } | |
3685 | |
3686 | |
3687 //---- convert results for the assembler interface ----------------------------- | |
3688 done: | |
3689 | |
3690 for(i=0; i<NUM_GAS; ++i) | |
3691 { | |
3692 if( volumes[i] >= 65534.5 ) | |
3693 { | |
3694 int_O_gas_volumes[i] = 65535; | |
3695 int_O_tank_pres_need[i] = 999 + INT_FLAG_WARNING; // 999 bar + warning flag for > pres_fill | |
3696 } | |
3697 else | |
3698 { | |
3699 overlay unsigned short tank_pres_fill = 10.0 * (unsigned short)char_I_tank_pres_fill[i]; | |
3700 | |
3701 // No distinct rounding done here because volumes are not accurate to the single liter anyhow | |
3702 | |
3703 // convert gas volumes to integers | |
3704 int_O_gas_volumes[i] = (unsigned short)volumes[i]; | |
3705 | |
3706 // compute how much pressure in the tank will be needed [in bar] (integer-division) | |
3707 int_O_tank_pres_need[i] = (unsigned short)(int_O_gas_volumes[i] / char_I_tank_size[i]); | |
3708 | |
3709 // limit to 999 bar because of display constraints | |
3710 if( int_O_tank_pres_need[i] > 999 ) int_O_tank_pres_need[i] = 999; | |
3711 | |
3712 // set flags for fast evaluation by divemode check for warnings | |
3713 if ( int_O_tank_pres_need[i] == 0 ) | |
3714 { | |
3715 // set flag for 0 bar | |
3716 int_O_tank_pres_need[i] |= INT_FLAG_ZERO; | |
3717 } | |
3718 else if( int_O_tank_pres_need[i] >= tank_pres_fill ) | |
3719 { | |
3720 // set warning flag | |
3721 int_O_tank_pres_need[i] |= INT_FLAG_WARNING; | |
3722 | |
3723 } | |
3724 else if( int_O_tank_pres_need[i] >= tank_pres_fill * GAS_NEEDS_ATTENTION_THRESHOLD ) | |
3725 { | |
3726 // set pre-warning flag | |
3727 int_O_tank_pres_need[i] |= INT_FLAG_PREWARNING; | |
3728 } | |
3729 | |
3730 // set invalid flag if there is an overflow in the stops table | |
3731 if( char_O_deco_warnings & DECO_WARNING_STOPTABLE_OVERFLOW ) | |
3732 int_O_tank_pres_need[i] |= INT_FLAG_INVALID; | |
3733 | |
3734 } // if( volumes[i] ) | |
3735 } // for | |
0 | 3736 } |
3737 | |
3738 ////////////////////////////////////////////////////////////////////////////// | |
560 | 3739 |
3740 void compute_CNS_for_display(void) | |
0 | 3741 { |
560 | 3742 if ( CNS_fraction < 0.01 ) int_O_CNS_fraction = 0; |
3743 else if ( CNS_fraction >= 9.985 ) int_O_CNS_fraction = 999 + INT_FLAG_WARNING; | |
3744 else | |
3745 { | |
3746 // convert float to integer | |
3747 int_O_CNS_fraction = (unsigned short)(100 * CNS_fraction + 0.5); | |
3748 | |
3749 // compute warnings | |
3750 if ( int_O_CNS_fraction >= CNS_warning_threshold ) | |
3751 { | |
3752 // reset pre-warning and set main warning flag | |
3753 int_O_CNS_fraction &= ~INT_FLAG_PREWARNING; | |
3754 int_O_CNS_fraction |= INT_FLAG_WARNING; | |
3755 } | |
3756 else if ( int_O_CNS_fraction >= CNS_prewarning_threshold ) | |
3757 { | |
3758 // reset main warning but set pre-warning flag | |
3759 int_O_CNS_fraction &= ~INT_FLAG_WARNING; | |
3760 int_O_CNS_fraction |= INT_FLAG_PREWARNING; | |
3761 } | |
3762 else | |
3763 { | |
3764 // clear both warnings | |
3765 int_O_CNS_fraction &= ~(INT_FLAG_WARNING + INT_FLAG_PREWARNING); | |
3766 } | |
3767 } | |
0 | 3768 } |
3769 | |
3770 ////////////////////////////////////////////////////////////////////////////// | |
3771 | |
3772 void deco_push_tissues_to_vault(void) | |
3773 { | |
3774 overlay unsigned char x; | |
560 | 3775 |
0 | 3776 RESET_C_STACK |
3777 | |
560 | 3778 low_depth_norm_vault = low_depth_norm; |
3779 low_depth_alt_vault = low_depth_alt; | |
3780 cns_vault_float = CNS_fraction; | |
3781 cns_vault_int = int_O_CNS_fraction; | |
3782 deco_warnings_vault = char_O_deco_warnings; | |
0 | 3783 |
3784 for (x=0;x<NUM_COMP;x++) | |
3785 { | |
3786 pres_tissue_N2_vault[x] = pres_tissue_N2[x]; | |
3787 pres_tissue_He_vault[x] = pres_tissue_He[x]; | |
3788 } | |
3789 } | |
3790 | |
3791 void deco_pull_tissues_from_vault(void) | |
3792 { | |
3793 overlay unsigned char x; | |
560 | 3794 |
0 | 3795 RESET_C_STACK |
3796 | |
560 | 3797 low_depth_norm = low_depth_norm_vault; |
3798 low_depth_alt = low_depth_alt_vault; | |
3799 CNS_fraction = cns_vault_float; | |
3800 int_O_CNS_fraction = cns_vault_int; | |
3801 char_O_deco_warnings = deco_warnings_vault; | |
3802 | |
3803 locked_GF_step_norm = GF_delta / low_depth_norm; | |
3804 locked_GF_step_alt = GF_delta / low_depth_alt; | |
3805 | |
0 | 3806 for (x=0; x<NUM_COMP; x++) |
3807 { | |
3808 pres_tissue_N2[x] = pres_tissue_N2_vault[x]; | |
3809 pres_tissue_He[x] = pres_tissue_He_vault[x]; | |
3810 } | |
3811 } | |
3812 | |
3813 ////////////////////////////////////////////////////////////////////////////// | |
3814 // | |
3815 #ifndef CROSS_COMPILE | |
3816 void main() {} | |
3817 #endif |