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