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