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