Mercurial > public > hwos_code
annotate src/p2_deco.c @ 628:cd58f7fc86db
3.05 stable work
author | heinrichsweikamp |
---|---|
date | Thu, 19 Sep 2019 12:01:29 +0200 |
parents | c40025d8e750 |
children | 237931377539 |
rev | line source |
---|---|
582 | 1 // *************************************************************************** |
628 | 2 // p2_deco.c combined next generation V3.04.3 |
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 | |
623 | 15 // the Free Software Foundation, either version 3 of the License, or |
0 | 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 | |
623 | 20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
0 | 21 // GNU General Public License for more details. |
22 // | |
23 // You should have received a copy of the GNU General Public License | |
623 | 24 // along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 | 25 // |
26 ////////////////////////////////////////////////////////////////////////////// | |
27 | |
623 | 28 // History: |
0 | 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 |
623 | 33 // 03/13/25 v101: CNS_fraction_real calculation |
0 | 34 // 03/13/26 v101: optimization of tissue calc routines |
623 | 35 // 07/xx/2008 v102a: debug of bottom time routine |
36 // 09/xx/2008 v102d: Gradient Factor Model implementation | |
37 // 10/10/2008 v104: renamed to build v103 for v118 stable | |
38 // 10/14/2008 v104: integration of char_I_depth_last_deco for Gradient Model | |
39 // 03/31/2009 v107: integration of FONT Incon24 | |
40 // 05/23/2010 v109: 5 gas changes & 1 min timer | |
41 // 07/13/2010 v110: cns vault added | |
42 // 12/25/2010 v110: split in three files (deco.c, main.c, definitions.h) | |
0 | 43 // 2011/01/20: [jDG] Create a common file included in ASM and C code. |
623 | 44 // 2011/01/24: [jDG] Make ascent time an short. No more overflow! |
0 | 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. | |
623 | 50 // 2011/04/15: [jDG] Store GF_low_depth in 32 bits (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 | |
623 | 61 // 2012/10/05: [jDG] Better calc_gas_needs_ascent accuracy (average depth, switch between stop). |
62 // 2013/03/05: [jDG] Should vault GF_low_depth too. | |
0 | 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 | |
623 | 90 |
604 | 91 #include <math.h> |
582 | 92 #include "p2_definitions.h" |
93 #define TEST_MAIN | |
94 #include "shared_definitions.h" | |
623 | 95 #include "configuration.inc" |
0 | 96 |
560 | 97 |
604 | 98 // ********************************************************************************************************************************* |
99 // | |
100 // C O N S T A N T S D E F I N I T I O N S | |
101 // | |
102 // ********************************************************************************************************************************* | |
103 | |
623 | 104 |
105 // deco engine scheduling | |
106 #define INVOKES_PER_SECOND 2 // number of invocations of the deco engine per second (use powers of 2 only: 1, 2, 4, ...) | |
628 | 107 |
108 #ifdef _hwos_sport | |
109 #define BUDGET_PER_SECOND 320 // [ms] total time budget per second for the deco engine, each invocation will preempt after BUDGET_PER_SECOND / INVOKES_PER_SECOND | |
110 #else | |
623 | 111 #define BUDGET_PER_SECOND 640 // [ms] total time budget per second for the deco engine, each invocation will preempt after BUDGET_PER_SECOND / INVOKES_PER_SECOND |
628 | 112 #endif |
604 | 113 |
560 | 114 // ambient pressure at different mountain heights |
115 #define P_ambient_1000m 0.880 // [bar] based on 990 hPa and 20°C at sea level, 15°C at altitude | |
116 #define P_ambient_2000m 0.782 // [bar] | |
117 #define P_ambient_3000m 0.695 // [bar] | |
118 | |
119 // ambient pressure in aircraft cabin during flying - worst case according to Buhlmann | |
120 #define P_ambient_fly 0.600 // [bar], 0.600 bar is the value used by Buhlmann for his flying-after-diving calculations | |
582 | 121 // 0.735 bar is a typical cabin pressure for nowadays commercial jet aircrafts |
560 | 122 // ----- |
123 // 0.135 bar safety margin | |
124 | |
125 // constants and factors | |
604 | 126 #define ppWater 0.06270 // water vapor partial pressure in the lungs |
582 | 127 #define METER_TO_BAR 0.09985 // conversion factor |
128 #define BAR_TO_METER 10.0150 // conversion factor (1.0/METER_TO_BAR) | |
604 | 129 #define SURFACE_DESAT_FACTOR 0.70420 // surface desaturation safety factor |
560 | 130 #define HYST 1.0E-06 // threshold for tissue graphics on-gassing / off-gassing visualization |
131 | |
132 // thresholds | |
628 | 133 #define CNS_LIMIT_WARNING 100 // threshold for CNS warning |
134 #define CNS_LIMIT_ATTENTION 70 // threshold for CNS attention | |
604 | 135 #define PRESSURE_LIMIT_WARNING 200 // threshold for pressure reading warning : 20.0 bar |
136 #define PRESSURE_LIMIT_ATTENTION 500 // threshold for pressure reading attention: 50.0 bar | |
628 | 137 #define GAS_NEEDS_LIMIT_ATTENTION 0.70 // threshold for gas needs attention [1.00 = 100%] |
604 | 138 #define O2_CONSUMPTION_LIMIT_ATTENTION 20 // threshold for O2 "SAC" attention: 2.0 l/min |
628 | 139 #define ppO2_GAP_TO_SETPOINT 10 // gap between setpoint and max. ppO2 of the pure diluent [cbar] |
623 | 140 #define ppO2_MARGIN_ON_MAX 3 // [cbar] margin on ppO2 max to compensate for surface pressures > 1.000 mbar |
628 | 141 #define STOP_CHAINING_LIMIT 5 // max. number of chained stop table entries before deco calculation is aborted |
142 | |
623 | 143 |
144 // deco engine states and modes - (char_O_)main_status: controls current tissue and deco status calculation (as-is situation) | |
145 #define CALC_VOLUME 0x01 // =1: calculate gas needs | |
146 #define CALCULATE_BOTTOM 0x02 // =1: calculate gas needs in deco calculator mode, =0: in dive mode | |
147 #define CAVE_MODE 0x04 // =1: calculate ascent and gas needs using backtracking data | |
148 #define USE_Z_FACTOR 0x08 // =1: calculate with Z factor when converting gas volumes <-> pressures | |
149 | |
150 #define TR_FUNCTIONS 0x10 // =1: calculate TR functions (pressure reading) processing | |
151 #define EXTENDED_STOPS 0x20 // =1: allow placement of gas switches below the depth of the 1st stop | |
152 | |
153 #define MODE_MASK 0xC0 // mask for real tissues mode selection | |
154 #define MODE_LOOP 0x40 // =1: CCR (MODE_PSCR needs to be cleared) or pSCR mode | |
155 #define MODE_CCR 0x40 // to be used with == operator in combination with MODE_MASK only! | |
156 #define MODE_PSCR 0x80 // =1: pSCR mode (MODE_LOOP needs to be set, too) | |
157 | |
158 // deco engine states and modes - (char_O_)deco_status: controls deco plan calculation (to-be scenario) | |
159 #define PLAN_MASK 0x03 // bit mask covering normal & alternative plan flag | |
160 #define COMMAND_MASK 0x07 // bit mask covering all command flags | |
161 #define CALCULATING 0x00 // calculations are ongoing | |
162 #define START_NORM 0x01 // input: start calculation of a normal deco plan | |
163 #define CALC_NORM 0x01 // internal: calculating a normal deco plan | |
164 #define COMPLETED_NORM 0x01 // output: calculation of a normal deco plan has completed | |
165 #define START_ALT 0x02 // input: start calculation of an alternative deco plan | |
166 #define CALC_ALT 0x02 // internal: calculating an alternative deco plan | |
167 #define COMPLETED_ALT 0x02 // output: calculation of an alternative deco plan has completed | |
168 #define INITIALIZE 0x04 // input: initialize deco engine | |
169 #define INITIALIZE_START_NORM 0x05 // input: initialize deco engine and start calculation of a normal deco plan | |
170 #define INITIALIZE_START_ALT 0x06 // input: initialize deco engine and start calculation of an alternative deco plan | |
628 | 171 #define DECO_CALCULATOR_MODE 0x08 // input: deco engine is run from deco calculator |
623 | 172 |
173 #define BAILOUT_MODE 0x10 // =1: allow gas switches before first deco stop | |
174 #define DELAYED_ASCENT 0x20 // =1: figure in a delayed ascent (fTTS) | |
175 | |
176 // MODE_MASK 0xC0 // mask for simulated tissues mode selection | |
177 // MODE_LOOP 0x40 // =1: CCR (MODE_PSCR needs to be cleared) or pSCR mode | |
178 // MODE_CCR 0x40 // to be used with == operator in combination with MODE_MASK only! | |
179 // MODE_PSCR 0x80 // =1: pSCR mode (MODE_LOOP needs to be set, too) | |
180 | |
181 | |
182 // deco engine warnings - (char_O_)deco_warnings | |
604 | 183 #define DECO_WARNING_IBCD 0x01 // IBCD occurring now |
184 #define DECO_WARNING_IBCD_lock 0x02 // IBCD has occurred during the dive | |
185 #define DECO_WARNING_MBUBBLES 0x04 // micro bubbles likely to develop now | |
186 #define DECO_WARNING_MBUBBLES_lock 0x08 // ditto, but sometime during the dive | |
187 #define DECO_WARNING_OUTSIDE 0x10 // tissue pressures outside the Buhlmann model now | |
188 #define DECO_WARNING_OUTSIDE_lock 0x20 // tissue pressures outside the model sometime during the dive | |
189 #define DECO_ATTENTION_OUTSIDE 0x40 // tissue pressures are very close to the Buhlmann limit | |
628 | 190 #define DECO_WARNING_INCOMPLETE 0x80 // internal error: deco calculation incomplete |
604 | 191 |
623 | 192 // deco engine status (char_O_)deco_info |
628 | 193 #define DECO_MODE 0x01 // =1: deco ppO2 levels are permitted |
604 | 194 #define IND_DOUBLE_SWITCH_FLAG 0x02 // =1: switch to other tank advice active |
623 | 195 // 0x04 // --- unused |
196 #define DECO_ZONE 0x08 // =1: fTTS < TTS (not updated when in bailout mode) | |
604 | 197 #define DECO_CEILING 0x10 // =1: ceiling depth > 0 |
623 | 198 #define DECO_STOPS 0x20 // =1: deco stops found |
199 #define GAS_NEEDS_CAVE 0x40 // =1: indicated gas needs are calculated in cave mode | |
200 // 0x80 // --- unused | |
201 | |
604 | 202 |
203 // deco engine control - tissue_increment | |
623 | 204 #define TIME_MASK 0x7F // =0: time increment is 2 seconds, 1..127: time increments is 1..127 minutes |
205 #define TISSUE_SELECTOR 0x80 // =1: calculate on real tissues, =0: calculate on simulated tissues | |
206 | |
207 | |
208 // deco engine control - next_planning_phase | |
209 #define PHASE_00_DONE 0x00 // calculation cycle finished | |
210 #define PHASE_10_DIVE_INIT 0x10 // once-per-dive initialization of the deco engine | |
628 | 211 #define PHASE_20_CYCLIC_INIT 0x20 // once-every-cycle initialization of the deco engine |
212 #define PHASE_30_EXTENDED_BOTTOM_TIME 0x30 // calculate extended bottom time | |
213 #define PHASE_40_BOTTOM_GAS_NEED 0x40 // calculate gas needs for bottom segment | |
214 #define PHASE_50_NDL_TIME 0x50 // calculate NDL time | |
215 #define PHASE_60_CAVE_RETURN 0x60 // calculate cave mode return | |
216 #define PHASE_70_OPEN_WATER_ASCENT 0x70 // calculate open water ascent | |
217 #define PHASE_80_RESULTS 0x80 // results - initialization | |
218 #define PHASE_81_RESULTS_STOPS_TABLE 0x81 // results - publish stops table | |
219 #define PHASE_82_RESULTS_NDL 0x82 // results - publish data / within NDL | |
220 #define PHASE_83_RESULTS_DECO 0x83 // results - publish data / in deco | |
221 #define PHASE_84_GAS_NEEDS_PRESSURES 0x84 // results - convert gas needs from volumes to pressures | |
623 | 222 #define PHASE_90_FINISH 0x90 // finish calculation cycle |
223 | |
224 | |
560 | 225 // flags used with integer numbers |
604 | 226 #define INT_FLAG_INVALID 0x0400 // =1: value not valid |
623 | 227 #define INT_FLAG_NOT_COMPUTED_YET 0x0800 // =1: value not computed yet |
604 | 228 #define INT_FLAG_ZERO 0x0800 // =1: value is zero |
229 #define INT_FLAG_LOW 0x1000 // =1: value is below a lower warning threshold | |
230 #define INT_FLAG_NOT_AVAIL 0x1000 // =1: value is not available (not computed) | |
231 #define INT_FLAG_HIGH 0x2000 // =1: value is above an upper warning threshold | |
232 #define INT_FLAG_OUTDATED 0x2000 // =1: value has not been updated for too long | |
233 #define INT_FLAG_ATTENTION 0x4000 // =1: value exceeds the attention threshold | |
234 #define INT_FLAG_WARNING 0x8000 // =1: value exceeds the warning threshold | |
235 #define INT_FLAG_OUT_OF_RANGE 0x8000 // =1: value exceeds presentable range | |
236 | |
237 | |
238 | |
239 // ********************************************************************************************************************************* | |
240 // | |
241 // ** P R O T O T Y P E S ** | |
242 // | |
243 // The Functions are listed in sequence of intended usage / application. | |
244 // | |
245 // ********************************************************************************************************************************* | |
246 | |
623 | 247 // Functions used in Surface Mode |
604 | 248 static void calc_interval(PARAMETER unsigned char time_increment); |
249 // Calculates the tissue off-gassing under surface conditions. | |
250 static void calc_desaturation_time(void); // Calculates the desaturation and no-fly times. | |
251 static void clear_tissue(void); // Resets all tissues to surface pressure equilibrium state. | |
623 | 252 static void init_output_vars(void); // Initializes all deco engine output variables to defaults |
253 | |
254 // Main entry point in Dive Mode | |
604 | 255 static void calc_hauptroutine(void); // Sequences all calculations for the real tissues and the deco calculation. |
256 | |
623 | 257 // Functions dedicated to the real Tissues |
604 | 258 static void calc_hauptroutine_data_input(void);// Initializes environment data and sets gas ratios for the real tissues. |
259 | |
623 | 260 // Functions combined for real Tissues & Deco Calculations |
604 | 261 static void calc_alveolar_pressures(void); // Computes the partial pressures from the gas ratios and many more parameters, |
262 // needs either calc_hauptroutine_data_input() be called beforehand or | |
628 | 263 // gas_take_current() or gas_find_best()/gas_take_best() and gas_set_ratios(). |
604 | 264 static void calc_tissues(void); // Updates the tissues dependent on the partial pressures of N2 and He. |
265 static void calc_CNS(void); // Updates the CNS value dependent on the partial pressure of the O2. | |
266 static void calc_limit(PARAMETER float GF_current); | |
267 // Calculates ceiling, current GF (supersaturation) and some more data. | |
268 | |
623 | 269 // Functions for TR |
270 #ifdef _rx_functions | |
271 static void calc_TR_functions(void); // Calculates SAC etc. | |
272 #endif | |
273 | |
274 // Functions dedicated to Deco Calculations | |
604 | 275 static void clear_deco_table(void); // Clears the deco stops table, invoked at the start of each calculation cycle. |
628 | 276 static void gas_take_current(void); // Sets the first gas used for deco calculation, invoked at start of cycle, too. |
277 static unsigned char gas_find_best(void); // Searches for the best gas available. | |
278 static void gas_take_best(void); // Switches to the best gas that has been found found before by gas_find_best(). | |
604 | 279 static void gas_set_ratios(void); // Sets the gas ratios for use in deco calculation (simulated tissues), |
628 | 280 // needs to be called after each gas change (gas_take_current/_better). |
623 | 281 static void calc_NDL_time_tissue(void); // Calculates the remaining NDL time for a given tissue. |
282 static unsigned char find_next_stop(void); // Finds the next stop when in a deco ascent. | |
560 | 283 static unsigned char update_deco_table(PARAMETER unsigned char time_increment); |
604 | 284 // Enters a new stop or extends an existing stop in the deco stops table. |
623 | 285 static void calc_due_by_depth_time_sac(void); // Calculates gas volume required for a given depth, time and usage (SAC rate). |
286 static void convert_gas_needs_to_press(void); // Converts gas volumes into pressures and sets respective flags. | |
287 | |
288 // Functions for Results Reporting | |
604 | 289 static void publish_deco_table(void); // Copies the internal deco stops table to the export interface. |
623 | 290 static void convert_cur_CNS_for_display(void); // Converts the current CNS value from float to integer. |
604 | 291 static void convert_sim_CNS_for_display(void); // Converts the end-of-dive CNS value from float to integer. |
623 | 292 static void convert_sat_for_display(void); // Converts leading tissue saturation value from float to integer, 1.0 = 100%. |
604 | 293 static void convert_ceiling_for_display(void); // Converts ceiling from float to integer in mbar relative pressure. |
294 | |
295 | |
623 | 296 // internal helper Functions |
297 static void load_tmr5(void); // Loads a hardware timer which is used for preemptive scheduling. | |
298 static void read_tmr5(void); // Reads a hardware timer which is used for preemptive scheduling. | |
299 static void read_CNS_ab_coefficient(void); // Reads the CNS a and b coefficients from a ROM table. | |
300 static void read_CNS_c_coefficient(void); // Reads the CNS c coefficient from a ROM table. | |
301 static void read_Buhlmann_coefficients(void); // Reads the Buhlmann a and b coefficients from a ROM table. | |
604 | 302 static void read_Buhlmann_times(PARAMETER char period); |
303 // Reads pre-computed tissue increment factors from a ROM table. | |
304 static void read_Buhlmann_ht(void); // Reads the half-times from a ROM table. | |
305 static void adopt_Buhlmann_coefficients(void); // Computes average a and b coefficient by the N2/He tissue ratio. | |
306 static void push_tissues_to_vault(void); // Stores the state of the real tissues during simulator runs. | |
307 static void pull_tissues_from_vault(void); // Restores the state of the real tissues after a simulator run. | |
628 | 308 static void calc_sim_pres_respiration(void); // Calculate sim_pres_respiration from char_depth_sim. |
309 static void calc_N2_equilibrium(void); // Calculate partial pressure of N2 in respired air at surface pressure. | |
310 static void get_saturation_factors(void); // Get, safeguard and convert the saturation and desaturation factors. | |
311 static void apply_saturation_factors(void); // Applies saturation and desaturation factors. | |
604 | 312 |
313 | |
314 // ********************************************************************************************************************************* | |
315 // | |
316 // V A R I A B L E S D E F I N I T I O N S | |
317 // | |
318 // ********************************************************************************************************************************* | |
0 | 319 |
320 //---- Bank 5 parameters ----------------------------------------------------- | |
321 #ifndef UNIX | |
322 # pragma udata bank5=0x500 | |
323 #endif | |
324 | |
623 | 325 // Environmental and Gas Data (52 byte) |
560 | 326 |
582 | 327 static float pres_surface; // absolute pressure at the surface |
560 | 328 |
628 | 329 static float float_depth_real; // current real depth in meters, float |
330 static unsigned char char_depth_real; // current real depth in meters, integer | |
331 static unsigned char char_depth_sim_start; // start value of simulated depth in meters, integer | |
332 static unsigned char char_depth_sim; // current value of simulated depth in meters, integer | |
333 static unsigned char char_depth_last; // last value of simulated depth in meters, integer | |
604 | 334 |
335 static float real_pres_respiration; // current real depth in absolute pressure | |
336 static float real_O2_ratio; // real breathed gas oxygen ratio | |
337 static float real_N2_ratio; // real breathed gas nitrogen ratio | |
338 static float real_He_ratio; // real breathed gas helium ratio | |
339 static float real_pSCR_drop; // real ppO2 drop in pSCR loop | |
582 | 340 |
341 static float sim_pres_respiration; // simulated current depth in abs.pressure, used for deco calculations | |
342 static float sim_O2_ratio; // simulated breathed gas oxygen ratio | |
343 static float sim_N2_ratio; // simulated breathed gas nitrogen ratio | |
344 static float sim_He_ratio; // simulated breathed gas helium ratio | |
345 static float sim_pSCR_drop; // simulated ppO2 drop in pSCR loop | |
560 | 346 |
604 | 347 |
623 | 348 // general Deco Parameters (57 byte) |
349 | |
350 static float GF_low; // gradient factor to determine 1st stop | |
351 static float GF_high; // gradient factor to determine surfacing | |
352 | |
353 static unsigned char GF_low_last; // last GF low, used to detect changes | |
354 static unsigned char GF_high_last; // last GF high, used to detect changes | |
355 | |
356 static unsigned char GF_low_depth; // GF low reference depth in current calculation cycle | |
357 static unsigned char GF_low_depth_norm; // GF low reference depth in normal plan | |
358 static unsigned char GF_low_depth_alt; // GF low reference depth in alternative plan | |
359 | |
360 static float GF_slope; // (GF_high - GF_low) / GF_low_depth in current calculation cycle | |
361 static float GF_slope_norm; // (GF_high - GF_low) / GF_low_depth_norm in normal plan | |
362 static float GF_slope_alt; // (GF_high - GF_low) / GF_low_depth_alt in alternative plan | |
363 | |
364 static float float_ascent_speed; // ascent speed from options_table (5.0 .. 10.0 m/min) | |
628 | 365 static float float_deco_distance; // additional depth below stop depth - not used any more |
623 | 366 static float float_saturation_multiplier; // safety factor for on-gassing rates |
367 static float float_desaturation_multiplier; // safety factor for off-gassing rates | |
368 | |
369 static unsigned char split_N2_He[NUM_COMP]; // used for calculating the desaturation time | |
370 | |
371 | |
372 // real Context: what we are doing now (16 byte) | |
373 | |
374 static float CNS_fraction_real; // current real CNS (1.00 = 100%) | |
375 static unsigned short IBCD_tissue_vector; // 16 bit vector to memorize all tissues that experience IBCD | |
376 | |
377 static float pres_respiration_sac; // used in SAC calculation: current depth in absolute pressure | |
378 static float float_sac; // used in SAC calculation: SAC value in float | |
379 static unsigned short max_sac_rate; // used in SAC calculation: threshold for SAC rate attention | |
380 | |
381 | |
382 // simulated Context: used to calculate Ascent (11 byte) | |
383 | |
384 static float CNS_fraction_sim; // CNS after predicted ascent, 0.01 = 1%, as float | |
385 static unsigned short int_sim_CNS_fraction; // CNS after predicted ascent, 1 = 1%, as integer | |
386 static unsigned char NDL_tissue_start_norm; // tissue to start with when calculating the normal NDL time | |
387 static unsigned char NDL_tissue_start_alt; // tissue to start with when calculating the alternative NDL time | |
388 static unsigned char NDL_tissue_start; // tissue to start with in current cycle | |
389 static unsigned char NDL_tissue_lead; // tissue with the shortest NDL time found in current cycle | |
390 static unsigned char NDL_tissue; // tissue for which the NDL is calculated right now | |
391 | |
628 | 392 |
623 | 393 // Result Values from Calculation Functions (9 byte) |
394 | |
395 static float ceiling; // minimum tolerated relative pressure (i.e. without surface pressure) | |
396 static float lead_supersat; // supersaturation of the leading tissue, 1.0 = 100% | |
397 static unsigned char lead_tissue; // number of the leading tissue | |
398 | |
399 | |
400 // Transfer Variables between calc_desaturation_time() and calc_desaturation_time_helper() (18 byte) | |
401 | |
402 static float desat_factor; // used to cache a pre-computed factor | |
403 static float var_ht; // buffer for a half-time factor | |
404 static float pres_target; // target pressure for a compartment | |
405 static float pres_actual; // current pressure of the compartment | |
406 static unsigned short int_time; // time it takes for the compartment to reach the target pressure | |
407 | |
408 | |
628 | 409 // Gas in Use and Gas Needs (26 byte) |
410 | |
411 static unsigned char sim_gas_last_num; // number of the last used gas | |
623 | 412 static unsigned char sim_gas_current_num; // number of the currently used gas |
413 static unsigned char sim_gas_current_depth; // change depth of the currently used gas | |
414 | |
628 | 415 static unsigned char sim_gas_best_num; // number of the better gas |
416 static unsigned char sim_gas_best_depth; // change depth of the better gas | |
417 | |
623 | 418 static unsigned char gas_needs_gas_index; // index to the gas and tank data arrays |
628 | 419 static float gas_volume_need[NUM_GAS]; // gas volumes required for return/ascent in liters |
420 static float gas_volume_avail[NUM_GAS]; // gas volumes available for return/ascent in liters | |
421 | |
422 | |
423 // Transfer Variables for calc_due_by_depth_time_sac() (7 byte) | |
424 | |
425 static unsigned char gas_needs_depth; // depth of the stop or half-way point | |
426 static unsigned char gas_needs_time; // duration of the stop or ascent phase | |
427 static unsigned char gas_needs_usage_rate; // gas usage in l/min | |
623 | 428 static float gas_needs_volume_due; // computed due of gas volume required |
429 | |
430 | |
431 // Auxiliary Variables for Data Buffering (28 byte) | |
432 | |
433 static float N2_equilibrium; // used for N2 tissue graphics scaling | |
434 static float temp_tissue; // auxiliary variable to buffer tissue pressures | |
435 static float float_pSCR_factor; // pre-computed factor for pSCR ppO2 drop calculation | |
436 static float calc_pres_tissue_N2; // auxiliary variable to buffer tissue N2 pressure | |
437 static float calc_pres_tissue_He; // auxiliary variable to buffer tissue He pressure | |
438 static float pres_tissue; // auxiliary variable to buffer total tissue pressure | |
439 static float old_pres_respiration; // auxiliary variable to buffer sim_pres_respiration | |
440 | |
441 | |
628 | 442 // 244 byte used, 12 byte left in this bank (4 bytes per float, 2 bytes per short, 1 byte per char) |
623 | 443 |
444 | |
445 //---- Bank 6 parameters ----------------------------------------------------- | |
446 #ifndef UNIX | |
447 # pragma udata bank6=0x600 | |
448 #endif | |
449 | |
450 // Timer5 Interface (3 byte) - Attention: keep order and keep at beginning of bank 6, i.e. at address 0x600 ! | |
451 | |
452 static volatile unsigned short tmr5_value; // timer 5 value buffer MUST be at address 0x600 | |
453 static volatile unsigned char tmr5_overflow; // timer 5 overflow flag MUST be at address 0x602 | |
454 | |
455 | |
628 | 456 // Modes, Sequencing and Indexing (12 byte) |
623 | 457 |
458 static unsigned char main_status; // shadow register for char_O_main_status | |
459 static unsigned char deco_status; // shadow register for char_O_deco_status | |
460 static unsigned char deco_info; // shadow register for char_O_deco_info | |
461 static unsigned char deco_warnings; // shadow register for char_O_deco_warnings | |
462 static unsigned char next_planning_phase; // next calculation phase to be executed | |
463 static unsigned char tissue_increment; // selector for real/simulated tissues and time increment | |
464 static unsigned char sequence_timer; // timer to sequence deco engine tasks | |
465 static unsigned char ci; // index to the Buhlmann tables (compartment index) | |
466 static unsigned char cns_i; // index to the CNS tables (ppO2 range index) | |
467 static unsigned char i; // general purpose loop counter and index | |
468 static unsigned char fast; // selects 1 minute or 2 second ascent steps | |
628 | 469 static unsigned char stop_index; // current stop table position |
470 static unsigned char chained_stops; // counter for chained stop entries | |
623 | 471 |
472 | |
473 // Result Values from Calculation Functions (28 byte) | |
604 | 474 |
560 | 475 static float O2_ppO2; // ppO2 - calculated for pure oxygen at current depth |
604 | 476 static float OC_ppO2; // ppO2 - calculated for breathing in OC mode |
477 static float pSCR_ppO2; // ppO2 - calculated for breathing in pSCR mode | |
582 | 478 |
479 static float ppO2; // partial pressure of breathed oxygen | |
480 static float ppN2; // partial pressure of breathed nitrogen | |
481 static float ppHe; // partial pressure of breathed helium | |
482 | |
483 static unsigned char char_ppO2; // partial pressure of breathed oxygen, as integer 100 = 1.00 bar | |
484 static unsigned char NDL_time; // time in minutes until reaching NDL | |
623 | 485 static unsigned short ascent_time; // time in minutes needed for the ascent |
486 | |
487 | |
488 // Buhlmann Model Parameters (40 byte) | |
560 | 489 |
582 | 490 static float var_N2_a; // Buhlmann a, for current N2 tissue |
491 static float var_N2_b; // Buhlmann b, for current N2 tissue | |
492 static float var_He_a; // Buhlmann a, for current He tissue | |
493 static float var_He_b; // Buhlmann b, for current He tissue | |
623 | 494 static float var_a; // Buhlmann a, adopted to current N2/He ratio |
495 static float var_b; // Buhlmann b, adopted to current N2/He ratio | |
582 | 496 static float var_N2_e; // exposition, for current N2 tissue |
497 static float var_He_e; // exposition, for current He tissue | |
604 | 498 static float var_N2_ht; // half-time, for current N2 tissue |
499 static float var_He_ht; // half-time, for current He tissue | |
500 | |
501 | |
628 | 502 // CNS Coefficients (10 byte) |
503 | |
504 static float var_cns_a; // two coefficients approximation, gain | |
505 static float var_cns_b; // two coefficients approximation, offset | |
506 static unsigned short var_cns_c; // one coefficient approximation, value | |
507 | |
508 | |
623 | 509 // Vault to back-up & restore Tissue related Data (134 byte) |
510 | |
511 static float vault_pres_tissue_N2[NUM_COMP]; // stores the nitrogen tissue pressures | |
512 static float vault_pres_tissue_He[NUM_COMP]; // stores the helium tissue pressures | |
513 static float vault_CNS_fraction_real; // stores current CNS (float representation) | |
514 static unsigned char vault_deco_warnings; // stores warnings status | |
515 static unsigned char vault_deco_info; // stores info status | |
516 | |
517 | |
628 | 518 // Transfer values for convert_float_int and convert_float_to_char() (7 byte) |
519 | |
520 static float float_value; // input value, float | |
521 static unsigned short int_value; // output value, 16 bit | |
522 static unsigned char char_value; // output value, 8 bit | |
523 | |
524 | |
525 // Performance Profiling (4 byte) | |
526 | |
527 static unsigned short profiling_runtime; // performance measurement: runtime of current invocation | |
528 static unsigned char profiling_runs; // performance measurement: invocations per deco calculation cycle | |
529 static unsigned char profiling_phase; // performance measurement: current calculation phase | |
530 | |
531 | |
623 | 532 // 7 byte occupied by compiler-placed vars |
533 | |
534 | |
628 | 535 // 245 byte used, 11 byte left in this bank (4 bytes per float, 2 bytes per short, 1 byte per char) |
623 | 536 |
537 | |
538 | |
539 //---- Bank 12 parameters ----------------------------------------------------- | |
540 #ifndef UNIX | |
541 # pragma udata bank12=0xc00 | |
542 #endif | |
543 | |
544 // stops table (96 byte) | |
545 | |
546 static unsigned char internal_deco_depth[NUM_STOPS]; // depths of the stops in meters | |
547 static unsigned char internal_deco_time[NUM_STOPS]; // durations of the stops in minutes | |
548 static unsigned char internal_deco_gas[NUM_STOPS]; // gases used on the stops (0 / 1-5) | |
549 | |
550 | |
551 // 96 byte used, 160 byte left in this bank (4 bytes per float, 2 bytes per short, 1 byte per char) | |
560 | 552 |
0 | 553 |
554 //---- Bank 7 parameters ----------------------------------------------------- | |
555 #ifndef UNIX | |
556 # pragma udata bank7=0x700 | |
557 #endif | |
560 | 558 |
623 | 559 // Keep order and position of the variables in bank 7 as they are backed-up to & restored from EEPROM |
560 | |
561 static float real_pres_tissue_N2[NUM_COMP]; // 16 floats = 64 bytes | |
562 static float real_pres_tissue_He[NUM_COMP]; // 16 floats = 64 bytes | |
563 | |
564 static float sim_pres_tissue_N2[NUM_COMP]; // 16 floats = 64 bytes | |
565 static float sim_pres_tissue_He[NUM_COMP]; // 16 floats = 64 bytes | |
566 | |
567 // 256 byte used, bank is full | |
582 | 568 |
0 | 569 |
570 //---- Bank 8 parameters ----------------------------------------------------- | |
571 #ifndef UNIX | |
572 # pragma udata overlay bank8=0x800 | |
560 | 573 |
604 | 574 static char md_pi_subst[256]; // overlay C-code data stack here, too |
560 | 575 |
576 # define C_STACK md_pi_subst | |
0 | 577 #endif |
578 | |
582 | 579 |
0 | 580 // Back to bank6 for further tmp data |
582 | 581 // Do not delete this assignment, it is needed by the compiler/linker. |
0 | 582 #ifndef UNIX |
583 # pragma udata bank6 | |
584 #endif | |
585 | |
582 | 586 |
604 | 587 // ********************************************************************************************************************************* |
588 // | |
589 // L O O K - U P T A B L E S | |
590 // | |
591 // ********************************************************************************************************************************* | |
582 | 592 |
0 | 593 #ifndef UNIX |
623 | 594 # pragma romdata CNS_tables = 0x1DC80 // needs to be in the UPPER bank |
595 #endif | |
596 | |
597 rom const float CNS_ab[2*11] = { | |
628 | 598 // CNS increment per 2 sec = 1 / (a*ppO2 + b) with ppO2 in [cbar] |
623 | 599 // a b for ppO2 cbar range |
600 -533.07, 54000, // 51 - 60 (index 0) | |
601 -444.22, 48600, // 61 - 70 (index 1) | |
602 -355.38, 42300, // 71 - 80 (index 2) | |
603 -266.53, 35100, // 81 - 90 (index 3) | |
604 -177.69, 27000, // 91 - 100 (index 4) | |
605 -177.69, 27000, // 101 - 110 (index 5) | |
606 -88.84, 17100, // 111 - 120 (index 6) | |
607 -88.84, 17100, // 121 - 130 (index 7) | |
608 -88.84, 17100, // 131 - 140 (index 8) | |
609 -88.84, 17100, // 141 - 150 (index 9) | |
610 -222.11, 37350 // 151 - 160 (index 10) | |
611 }; | |
612 | |
628 | 613 rom const unsigned short CNS_c[1*18] = { |
614 // CNS increment per 2 sec = c / 100000.0 | |
623 | 615 // c in [1/100000] for ppO2 cbar range |
616 75, // 161 - 165 (index 0) | |
617 102, // 166 - 170 (index 1) | |
618 136, // 171 - 175 (index 2) | |
619 180, // 176 - 180 (index 3) | |
620 237, // 181 - 185 (index 4) | |
621 310, // 186 - 190 (index 5) | |
622 401, // 191 - 195 (index 6) | |
623 517, // 196 - 200 (index 7) | |
624 760, // 201 - 205 (index 8) | |
625 1100, // 206 - 210 (index 9) | |
626 1500, // 211 - 215 (index 10) | |
627 2090, // 216 - 220 (index 11) | |
628 2900, // 221 - 225 (index 12) | |
629 3900, // 226 - 230 (index 13) | |
630 4820, // 231 - 235 (index 14) | |
631 4820, // 236 - 240 (index 15) | |
632 4820, // 241 - 245 (index 16) | |
628 | 633 4820 // 246 - 250 (index 17) |
623 | 634 }; |
635 | |
636 | |
637 #ifndef UNIX | |
604 | 638 # pragma romdata Buhlmann_tables = 0x1DD00 // needs to be in the UPPER bank |
0 | 639 #endif |
640 | |
560 | 641 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
|
642 // 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
|
643 // 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
|
644 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
|
645 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
|
646 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
|
647 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
|
648 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
|
649 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
|
650 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
|
651 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
|
652 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
|
653 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
|
654 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
|
655 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
|
656 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
|
657 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
|
658 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
|
659 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
|
660 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
661 |
560 | 662 rom const float Buhlmann_ht[2*16] = { |
628 | 663 // Compartment half-life, in minutes |
560 | 664 //--- N2 ---- He ---------------------- |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
665 4.0, 1.51, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
666 8.0, 3.02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
667 12.5, 4.72, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
668 18.5, 6.99, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
669 27.0, 10.21, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
670 38.3, 14.48, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
671 54.3, 20.53, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
672 77.0, 29.11, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
673 109.0, 41.20, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
674 146.0, 55.19, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
675 187.0, 70.69, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
676 239.0, 90.34, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
677 305.0, 115.29, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
678 390.0, 147.42, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
679 498.0, 188.24, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
680 635.0, 240.03 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
681 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
682 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
683 rom const float e2secs[2*16] = { |
628 | 684 // Integration constant for 2 seconds, |
685 // result of 1 - 2^(-1/(2sec/60sec * HT)) | |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
686 //---- N2 ------------- He ------------ |
623 | 687 5.75958E-03, 1.51848E-02, |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
688 2.88395E-03, 7.62144E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
689 1.84669E-03, 4.88315E-03, |
582 | 690 1.24813E-03, 3.29997E-03, |
691 8.55371E-04, 2.26041E-03, | |
692 6.03079E-04, 1.59437E-03, | |
693 4.25414E-04, 1.12479E-03, | |
694 3.00019E-04, 7.93395E-04, | |
695 2.11949E-04, 5.60641E-04, | |
696 1.58240E-04, 4.18555E-04, | |
697 1.23548E-04, 3.26795E-04, | |
698 9.66686E-05, 2.55722E-04, | |
699 7.57509E-05, 2.00387E-04, | |
700 5.92416E-05, 1.56716E-04, | |
701 4.63943E-05, 1.22734E-04, | |
702 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
|
703 //------------------------------------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
704 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
705 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
706 rom const float e1min[2*16] = { |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
707 // Integration constant for 1 minute, |
628 | 708 // result of 1 - 2^(-1/HT) |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
709 //----- N2 --------- e 1min He -------- |
582 | 710 1.59104E-01, 3.68109E-01, |
711 8.29960E-02, 2.05084E-01, | |
712 5.39424E-02, 1.36579E-01, | |
713 3.67742E-02, 9.44046E-02, | |
714 2.53454E-02, 6.56359E-02, | |
715 1.79351E-02, 4.67416E-02, | |
716 1.26840E-02, 3.31991E-02, | |
717 8.96152E-03, 2.35301E-02, | |
718 6.33897E-03, 1.66832E-02, | |
719 4.73633E-03, 1.24808E-02, | |
720 3.69981E-03, 9.75753E-03, | |
721 2.89600E-03, 7.64329E-03, | |
722 2.27003E-03, 5.99417E-03, | |
723 1.77572E-03, 4.69082E-03, | |
724 1.39089E-03, 3.67548E-03, | |
725 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
|
726 //------------------------------------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
727 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
728 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
729 rom const float e10min[2*16] = { |
628 | 730 // Integration constant for 10 minutes, |
731 // result of 1 - 2^(-10/ht) | |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
732 //---- N2 -------------- He ----------- |
582 | 733 8.23223E-01, 9.89851E-01, |
734 5.79552E-01, 8.99258E-01, | |
735 4.25651E-01, 7.69737E-01, | |
736 3.12487E-01, 6.29027E-01, | |
737 2.26416E-01, 4.92821E-01, | |
738 1.65547E-01, 3.80407E-01, | |
739 1.19840E-01, 2.86538E-01, | |
740 8.60863E-02, 2.11886E-01, | |
741 6.16117E-02, 1.54849E-01, | |
742 4.63665E-02, 1.18026E-01, | |
743 3.63881E-02, 9.34005E-02, | |
744 2.85855E-02, 7.38569E-02, | |
745 2.24698E-02, 5.83504E-02, | |
746 1.76160E-02, 4.59303E-02, | |
747 1.38222E-02, 3.61528E-02, | |
748 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
|
749 //------------------------------------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
750 }; |
0 | 751 |
604 | 752 |
753 // ********************************************************************************************************************************* | |
0 | 754 // |
604 | 755 // H E L P E R F U N C T I O N S |
756 // | |
757 // ********************************************************************************************************************************* | |
758 | |
759 | |
628 | 760 // p2deco code moved from 0x0D000 to 0x0C000 in v.108 |
0 | 761 #ifndef UNIX |
584 | 762 # pragma code p2_deco = 0x0C000 |
0 | 763 #endif |
764 | |
765 | |
766 ////////////////////////////////////////////////////////////////////////////// | |
767 // Bump to blue-screen when an assert is wrong | |
623 | 768 #ifdef _DEBUG |
0 | 769 void assert_failed(PARAMETER short int line) |
770 { | |
771 } | |
772 #endif | |
773 | |
604 | 774 |
0 | 775 ////////////////////////////////////////////////////////////////////////////// |
628 | 776 // When calling C code from ASM context, the C data stack pointer need to be |
777 // reset. The C stack is located in bank 8. | |
0 | 778 |
779 #ifdef CROSS_COMPILE | |
584 | 780 # define RESET_C_STACK |
0 | 781 #else |
623 | 782 # ifdef _DEBUG |
584 | 783 # define RESET_C_STACK fillDataStack(); |
784 void fillDataStack(void) | |
785 { | |
786 _asm | |
604 | 787 LFSR 1,C_STACK |
788 MOVLW 0xCC | |
789 loop: MOVWF POSTINC1,0 | |
790 TSTFSZ FSR1L,0 | |
791 BRA loop | |
792 | |
793 LFSR 1,C_STACK | |
794 LFSR 2,C_STACK | |
584 | 795 _endasm |
796 } | |
797 # else | |
798 # define RESET_C_STACK \ | |
799 _asm \ | |
604 | 800 LFSR 1,C_STACK \ |
801 LFSR 2,C_STACK \ | |
584 | 802 _endasm |
803 # endif | |
0 | 804 #endif |
805 | |
604 | 806 |
0 | 807 ////////////////////////////////////////////////////////////////////////////// |
623 | 808 // Reset timer 5 |
809 // | |
810 // Note: TMR5 is configured in 16 bit mode: a value written to TMR5H is buffered | |
811 // and will be written to TMR5 together with a successive write to TMR5L. | |
812 // As we don't know in which bank the code will be executed, we use either | |
813 // the bank-save "movff" command, or address mapping via access bank (",0"). | |
814 // | |
815 static void load_tmr5(void) | |
0 | 816 { |
817 #ifndef CROSS_COMPILE | |
584 | 818 _asm |
623 | 819 movff 0x601,0xF7D // bank-safe load TMR5H from C variable tmr5_value first |
820 movff 0x600,0xF7C // bank-safe load TMR5L from c variable tmr5_value thereafter | |
821 bcf 0xFBA,1,0 // clear timer 5 overrun flag (0xFBA = PIR5, bit 1 = TMR5IF) | |
822 _endasm | |
0 | 823 #else |
623 | 824 return; |
825 #endif | |
826 } | |
827 | |
828 | |
829 ////////////////////////////////////////////////////////////////////////////// | |
830 // Read timer 5 | |
831 // | |
832 // Note: TMR5 reads in multiples of 1/32 ms, 30.51757813 us/bit to be precise. | |
833 // TMR5 is configured in 16 bit mode: on reading of TMR5L the contents of | |
834 // TMR5H is latched and can be read afterwards without potential rollover. | |
835 // As we don't know in which bank the code will be executed, we use either | |
836 // the bank-save "movff" command, or address mapping via access bank (",0"). | |
837 // | |
838 static void read_tmr5(void) | |
839 { | |
840 #ifndef CROSS_COMPILE | |
841 _asm | |
842 movff 0xF7C,0x600 // copy TMR5L to C variable tmr5_value, low byte first | |
843 movff 0xF7D,0x601 // copy TMR5H to C variable tmr5_value, high byte thereafter | |
844 clrf WREG,0 // clear WREG to 0x00 = no overrun by default | |
845 btfsc 0xFBA,1,0 // did timer 5 overrun? (0xFBA = PIR5, bit 1 = TMR5IF) | |
846 setf WREG,0 // YES - set WREG to 0xff = overrun detected | |
847 movff WREG,0x602 // copy WREG to C variable tmr5_overflow | |
848 _endasm | |
849 #else | |
850 return; | |
0 | 851 #endif |
852 } | |
853 | |
604 | 854 |
0 | 855 ////////////////////////////////////////////////////////////////////////////// |
623 | 856 // Read CNS coefficients a and b |
857 // | |
858 static void read_CNS_ab_coefficient(void) | |
859 { | |
860 #ifndef CROSS_COMPILE | |
861 // Note: We don't use far ROM pointer, because handling | |
862 // 24 bit is to complex, hence we have to set the | |
863 // UPPER page ourself... | |
864 // -> set to zero if tables are moved to lower pages! | |
865 _asm | |
866 movlw 1 | |
867 movwf TBLPTRU,0 | |
868 _endasm | |
869 #endif | |
870 | |
871 { | |
872 overlay rom const float* ptr = &CNS_ab[2*cns_i]; | |
873 var_cns_a = *ptr++; | |
874 var_cns_b = *ptr++; | |
875 } | |
876 } | |
877 | |
878 | |
879 ////////////////////////////////////////////////////////////////////////////// | |
880 // Read CNS coefficient c | |
881 // | |
882 static void read_CNS_c_coefficient(void) | |
883 { | |
884 #ifndef CROSS_COMPILE | |
885 // Note: We don't use far ROM pointer, because handling | |
886 // 24 bit is to complex, hence we have to set the | |
887 // UPPER page ourself... | |
888 // -> set to zero if tables are moved to lower pages! | |
889 _asm | |
890 movlw 1 | |
891 movwf TBLPTRU,0 | |
892 _endasm | |
893 #endif | |
894 | |
895 { | |
896 overlay rom const unsigned short* ptr = &CNS_c[cns_i]; | |
897 var_cns_c = *ptr++; | |
898 } | |
899 } | |
900 | |
901 ////////////////////////////////////////////////////////////////////////////// | |
902 // Read Buhlmann coefficients a and b for compartment ci | |
0 | 903 // |
560 | 904 static void read_Buhlmann_coefficients(void) |
0 | 905 { |
906 #ifndef CROSS_COMPILE | |
604 | 907 // Note: We don't use far ROM pointer, because handling |
908 // 24 bit is too complex, hence we have to set the | |
909 // UPPER page ourself... | |
628 | 910 // -> set to zero if tables are moved to lower pages! |
584 | 911 _asm |
604 | 912 movlw 1 |
913 movwf TBLPTRU,0 | |
584 | 914 _endasm |
0 | 915 #endif |
916 | |
584 | 917 assert( ci < NUM_COMP ); |
918 | |
604 | 919 // use an interleaved array (AoS) to access coefficients with a single addressing |
584 | 920 { |
921 overlay rom const float* ptr = &Buhlmann_ab[4*ci]; | |
922 var_N2_a = *ptr++; | |
923 var_N2_b = *ptr++; | |
924 var_He_a = *ptr++; | |
925 var_He_b = *ptr++; | |
926 } | |
0 | 927 } |
928 | |
604 | 929 |
0 | 930 ////////////////////////////////////////////////////////////////////////////// |
623 | 931 // Read Buhlmann increments for compartment ci |
604 | 932 // If period == 0 : 2 sec interval |
933 // 1 : 1 min interval | |
934 // 2 : 10 min interval | |
560 | 935 static void read_Buhlmann_times(PARAMETER char period) |
0 | 936 { |
937 #ifndef CROSS_COMPILE | |
604 | 938 // Note: We don't use far ROM pointer, because handling |
939 // 24 bit is to complex, hence we have to set the | |
940 // UPPER page ourself... | |
941 // -> set to zero if tables are moved to lower pages! | |
584 | 942 _asm |
604 | 943 movlw 1 |
944 movwf TBLPTRU,0 | |
584 | 945 _endasm |
0 | 946 #endif |
947 | |
584 | 948 assert( ci < NUM_COMP ); |
949 | |
623 | 950 // Integration Intervals |
584 | 951 switch(period) |
952 { | |
953 case 0: //---- 2 sec ----------------------------------------------------- | |
954 { | |
955 overlay rom const float* ptr = &e2secs[2*ci]; | |
956 var_N2_e = *ptr++; | |
957 var_He_e = *ptr++; | |
958 } | |
959 break; | |
960 | |
961 case 1: //---- 1 min ----------------------------------------------------- | |
962 { | |
963 overlay rom const float* ptr = &e1min[2*ci]; | |
964 var_N2_e = *ptr++; | |
965 var_He_e = *ptr++; | |
966 } | |
967 break; | |
968 | |
969 case 2: //---- 10 min ---------------------------------------------------- | |
970 { | |
971 overlay rom const float* ptr = &e10min[2*ci]; | |
972 var_N2_e = *ptr++; | |
973 var_He_e = *ptr++; | |
974 } | |
623 | 975 break; |
584 | 976 |
977 default: | |
623 | 978 assert(0); // code execution shall never pass along here! |
584 | 979 } |
0 | 980 } |
981 | |
604 | 982 |
0 | 983 ////////////////////////////////////////////////////////////////////////////// |
623 | 984 // Read Buhlmann half-times for compartment ci |
0 | 985 // |
560 | 986 static void read_Buhlmann_ht(void) |
0 | 987 { |
988 | |
989 #ifndef CROSS_COMPILE | |
604 | 990 // Note: We don't use far ROM pointer, because handling |
991 // 24 bit is to complex, hence we have to set the | |
992 // UPPER page ourself... | |
993 // -> Set to zero if tables are moved to lower pages! | |
584 | 994 _asm |
604 | 995 movlw 1 |
996 movwf TBLPTRU,0 | |
584 | 997 _endasm |
0 | 998 #endif |
999 | |
584 | 1000 assert( ci < NUM_COMP ); |
1001 { | |
1002 overlay rom const float* ptr = &Buhlmann_ht[2*ci]; | |
1003 var_N2_ht = *ptr++; | |
1004 var_He_ht = *ptr++; | |
1005 } | |
1006 | |
604 | 1007 assert( 4.0 <= var_N2_ht && var_N2_ht <= 635.0 ); |
584 | 1008 assert( 1.5099 <= var_He_ht && var_He_ht <= 240.03 ); |
0 | 1009 } |
1010 | |
604 | 1011 |
0 | 1012 ////////////////////////////////////////////////////////////////////////////// |
623 | 1013 // Calculate adopted Buhlmann coefficients |
1014 // | |
1015 // Input: var_N2_a, var_N2_b coefficients for N2 | |
1016 // var_He_a, var_He_b coefficients for He | |
1017 // calc_pres_tissue_N2 partial pressure of N2 in tissue | |
1018 // calc_pres_tissue_He partial pressure of He in tissue | |
1019 // pres_tissue total pressure in tissue | |
1020 // | |
1021 // Output: var_a, var_b coefficients adopted by N2/He ratio | |
604 | 1022 // |
1023 static void adopt_Buhlmann_coefficients(void) | |
1024 { | |
1025 // adopt a and b coefficients to current N2/He ratio inside the tissue | |
623 | 1026 |
1027 #ifdef _helium | |
1028 | |
1029 var_a = (var_N2_a * calc_pres_tissue_N2 + var_He_a * calc_pres_tissue_He) / pres_tissue; | |
1030 var_b = (var_N2_b * calc_pres_tissue_N2 + var_He_b * calc_pres_tissue_He) / pres_tissue; | |
1031 | |
1032 #else | |
1033 | |
1034 var_a = var_N2_a; | |
1035 var_b = var_N2_b; | |
1036 | |
1037 #endif | |
1038 | |
1039 } | |
1040 | |
1041 | |
1042 ////////////////////////////////////////////////////////////////////////////// | |
628 | 1043 // Calculate sim_pres_respiration from char_depth_sim |
1044 // | |
1045 // Input: char_depth_sim simulated depth in meters | |
1046 // pres_surface surface pressure | |
1047 // | |
1048 // Output: sim_pres_respiration simulated depth in absolute pressure | |
1049 // | |
1050 static void calc_sim_pres_respiration(void) | |
1051 { | |
1052 sim_pres_respiration = (float)char_depth_sim * METER_TO_BAR + pres_surface; | |
1053 } | |
1054 | |
1055 | |
1056 ////////////////////////////////////////////////////////////////////////////// | |
623 | 1057 // Calculate partial pressure of N2 in respired air at surface pressure |
1058 // | |
1059 // Input: pres_surface surface pressure | |
1060 // | |
1061 // Output: N2_equilibrium partial pressure of N2 in surface air | |
1062 // | |
1063 static void calc_N2_equilibrium(void) | |
1064 { | |
1065 N2_equilibrium = 0.7902 * (pres_surface - ppWater); | |
1066 } | |
1067 | |
1068 | |
1069 ////////////////////////////////////////////////////////////////////////////// | |
1070 // Get, safeguard and convert the saturation and desaturation factors | |
1071 // | |
1072 // Input: char_I_saturation_multiplier saturation factor (integer) | |
1073 // char_I_desaturation_multiplier desaturation factor (integer) | |
1074 // | |
1075 // Output: float_saturation_multiplier saturation factor (float) | |
1076 // float_desaturation_multiplier desaturation factor (float) | |
1077 // | |
1078 static void get_saturation_factors(void) | |
1079 { | |
1080 // safeguard input parameters that are constant during the course of the dive | |
1081 if( char_I_saturation_multiplier < 100 ) char_I_saturation_multiplier = 100; | |
1082 if( char_I_saturation_multiplier > 140 ) char_I_saturation_multiplier = 140; | |
1083 | |
1084 if( char_I_desaturation_multiplier < 60 ) char_I_desaturation_multiplier = 60; | |
1085 if( char_I_desaturation_multiplier > 100 ) char_I_desaturation_multiplier = 100; | |
1086 | |
1087 // convert input parameters to float numbers | |
1088 float_saturation_multiplier = 0.01 * char_I_saturation_multiplier; | |
1089 float_desaturation_multiplier = 0.01 * char_I_desaturation_multiplier; | |
1090 } | |
1091 | |
1092 | |
1093 ////////////////////////////////////////////////////////////////////////////// | |
1094 // apply_saturation_factors | |
1095 // | |
1096 // Apply safety factors for both ZH-L16 models. | |
1097 // | |
1098 // Modified: temp_tissue safeguarded tissue increment/decrement | |
1099 // | |
1100 static void apply_saturation_factors(void) | |
1101 { | |
1102 assert( 0.0 < float_desaturation_multiplier && float_desaturation_multiplier <= 1.0 ); | |
1103 assert( 1.0 <= float_saturation_multiplier && float_saturation_multiplier <= 2.0 ); | |
1104 | |
1105 if ( temp_tissue < 0.0 ) temp_tissue *= float_desaturation_multiplier; | |
1106 else temp_tissue *= float_saturation_multiplier; | |
1107 } | |
1108 | |
1109 | |
1110 ////////////////////////////////////////////////////////////////////////////// | |
1111 // convert_float_to_int | |
1112 // | |
1113 // Converts a float within range 0.0 - 9.99 into 16 bit integer scaled in 1/100. | |
1114 // | |
1115 static void convert_float_to_int(void) | |
1116 { | |
1117 if ( float_value < 0.005 ) int_value = 0; | |
1118 else if ( float_value >= 9.985 ) int_value = 999; | |
1119 else int_value = (unsigned short)(100 * float_value + 0.5); | |
1120 } | |
1121 | |
1122 | |
1123 ////////////////////////////////////////////////////////////////////////////// | |
1124 // convert_float_to_char | |
1125 // | |
1126 // Converts a float within range 0.0 - 255 into 8 bit integer | |
1127 // | |
1128 static void convert_float_to_char(void) | |
1129 { | |
1130 if (float_value < 0.0) char_value = 0; | |
1131 else if (float_value >= 254.5) char_value = 255; | |
1132 else char_value = (unsigned char)(float_value + 0.5); | |
604 | 1133 } |
1134 | |
1135 | |
1136 // ********************************************************************************************************************************* | |
1137 // | |
1138 // J U M P I N F U N C T I O N S | |
1139 // | |
1140 // ********************************************************************************************************************************* | |
1141 | |
582 | 1142 |
1143 ////////////////////////////////////////////////////////////////////////////// | |
1144 // deco_calc_hauptroutine | |
1145 // | |
1146 // called from: divemode.asm | |
1147 // | |
628 | 1148 // Called two times per second during diving, updates the |
1149 // tissues every second (i.e. on every second invocation). | |
582 | 1150 // |
628 | 1151 // On each computation cycle: |
582 | 1152 // - Updates deco table (char_O_deco_time/depth) with new values, |
1153 // - updates ascent time, and | |
628 | 1154 // - sets status to zero (so we can check a cycle has finished). |
582 | 1155 // |
1156 void deco_calc_hauptroutine(void) | |
1157 { | |
1158 RESET_C_STACK | |
1159 calc_hauptroutine(); | |
1160 } | |
1161 | |
623 | 1162 |
1163 ////////////////////////////////////////////////////////////////////////////// | |
1164 // deco_init_output_vars | |
1165 // | |
1166 // called from divemode.asm | |
1167 // | |
1168 // Initializes all output variables to their default values. | |
1169 // | |
1170 void deco_init_output_vars(void) | |
1171 { | |
1172 RESET_C_STACK | |
1173 init_output_vars(); | |
1174 } | |
1175 | |
628 | 1176 |
582 | 1177 ////////////////////////////////////////////////////////////////////////////// |
1178 // deco_clear_tissue | |
1179 // | |
1180 // called from: start.asm | |
1181 // menu_tree.asm | |
1182 // simulator.asm | |
1183 // | |
628 | 1184 // Sets all tissues to equilibrium with air at ambient pressure, |
582 | 1185 // resets all CNS values, any warnings and resets all model output. |
1186 // | |
1187 void deco_clear_tissue(void) | |
1188 { | |
1189 RESET_C_STACK | |
1190 clear_tissue(); | |
1191 } | |
1192 | |
623 | 1193 |
582 | 1194 ////////////////////////////////////////////////////////////////////////////// |
1195 // deco_calc_dive_interval | |
1196 // | |
1197 // called from: simulator.asm | |
1198 // | |
628 | 1199 // Updates tissues and CNS value for char_I_dive_interval minutes on air at |
1200 // ambient pressure and calculates resulting saturation and ceiling for a | |
1201 // GF-high of 100% (ceiling and saturation not used by simulator.asm) | |
582 | 1202 // |
1203 void deco_calc_dive_interval(void) | |
1204 { | |
1205 RESET_C_STACK | |
1206 calc_interval(char_I_dive_interval); | |
1207 } | |
1208 | |
623 | 1209 |
582 | 1210 ////////////////////////////////////////////////////////////////////////////// |
1211 // deco_calc_dive_interval_1min | |
1212 // | |
1213 // called from: start.asm | |
1214 // sleepmode.asm | |
1215 // surfmode.asm | |
1216 // menu_tree.asm | |
1217 // ghostwriter.asm | |
1218 // | |
604 | 1219 // Updates tissues and CNS value for 1 minute on air at ambient pressure and |
628 | 1220 // calculates resulting saturation and ceiling for a GF-high of 100%. |
582 | 1221 // |
1222 void deco_calc_dive_interval_1min(void) | |
1223 { | |
1224 RESET_C_STACK | |
1225 calc_interval(1); | |
1226 } | |
1227 | |
1228 | |
1229 ////////////////////////////////////////////////////////////////////////////// | |
604 | 1230 // deco_calc_dive_interval_10min |
582 | 1231 // |
1232 // called from: sleepmode.asm | |
1233 // | |
604 | 1234 // Updates tissues and CNS value for 10 minutes on air at ambient pressure and |
628 | 1235 // calculates resulting saturation and ceiling for a GF-high of 100%. |
582 | 1236 // |
1237 void deco_calc_dive_interval_10min(void) | |
1238 { | |
1239 RESET_C_STACK | |
1240 calc_interval(10); | |
1241 } | |
1242 | |
1243 | |
1244 ////////////////////////////////////////////////////////////////////////////// | |
1245 // deco_calc_desaturation_time | |
1246 // | |
1247 // called from: start.asm | |
1248 // surfmode.asm | |
1249 // menu_tree.asm | |
1250 // ghostwriter.asm | |
1251 // | |
1252 // Computes desaturation and no-fly times. | |
1253 // | |
1254 void deco_calc_desaturation_time(void) | |
1255 { | |
1256 RESET_C_STACK | |
1257 calc_desaturation_time(); | |
1258 } | |
1259 | |
623 | 1260 |
582 | 1261 ////////////////////////////////////////////////////////////////////////////// |
1262 // deco_push_tissues_to_vault | |
1263 // | |
1264 // called from: simulator.asm | |
1265 // | |
1266 // Makes a backup of the state of the real tissues and the deco engine. | |
1267 // | |
1268 void deco_push_tissues_to_vault(void) | |
1269 { | |
1270 RESET_C_STACK | |
1271 push_tissues_to_vault(); | |
1272 } | |
1273 | |
623 | 1274 |
582 | 1275 ////////////////////////////////////////////////////////////////////////////// |
1276 // deco_pull_tissues_from_vault | |
1277 // | |
1278 // called from: simulator.asm | |
1279 // ghostwriter.asm | |
1280 // | |
1281 // Restores the state of the real tissues and the deco engine from the backup. | |
1282 // | |
1283 void deco_pull_tissues_from_vault(void) | |
1284 { | |
1285 RESET_C_STACK | |
1286 pull_tissues_from_vault(); | |
1287 } | |
1288 | |
604 | 1289 |
1290 // ********************************************************************************************************************************* | |
1291 // | |
1292 // M A I N F U N C T I O N S | |
1293 // | |
1294 // ********************************************************************************************************************************* | |
582 | 1295 |
1296 | |
1297 ////////////////////////////////////////////////////////////////////////////// | |
628 | 1298 // Calculate the next deco stop |
0 | 1299 // |
1300 // INPUT, fixed during dive: | |
623 | 1301 // pres_surface : surface pressure (as absolute pressure) |
1302 // char_I_depth_last_deco : depth of the last deco stop | |
1303 // | |
628 | 1304 // INPUT, may change during dive: |
623 | 1305 // GF_high : GF high factor |
1306 // GF_low : GF low factor | |
1307 // | |
1308 // INPUT & OUTPUT | |
1309 // char_depth_sim : simulated depth in meters | |
1310 // GF_low_depth : GF low depth in current calculation cycle | |
1311 // GF_slope : GF slope in current calculation cycle | |
1312 // GF_low_depth_norm/_alt : frozen GF low depth reference | |
1313 // GF_slope_norm/_alt : frozen GF slope | |
0 | 1314 // |
1315 // OUTPUT | |
623 | 1316 // char_depth_last : depth we came from |
1317 // sim_pres_respiration : simulated depth in absolute pressure | |
1318 // | |
1319 // RETURN | |
1320 // TRUE: a stop is needed, FALSE: no stop needed | |
1321 // | |
1322 static unsigned char find_next_stop(void) | |
0 | 1323 { |
623 | 1324 overlay unsigned char depth_1min; |
1325 overlay unsigned char depth_limit; | |
628 | 1326 overlay unsigned char stop_depth; |
1327 overlay unsigned char next_stop; | |
582 | 1328 overlay unsigned char need_stop; |
1329 | |
623 | 1330 |
628 | 1331 // memorize the depth we came from |
623 | 1332 char_depth_last = char_depth_sim; |
1333 | |
628 | 1334 // calculate the ceiling (minimum relative pressure) for the current depth |
1335 if( char_I_deco_model == 0 ) calc_limit(1.0); // deco or not - straight Buhlmann | |
1336 else if( NDL_time ) calc_limit(GF_high); // not in deco | |
1337 else if( char_depth_sim >= GF_low_depth ) calc_limit(GF_low); // in deco with GF, below or at low depth reference | |
1338 else calc_limit(GF_high - GF_slope * (float)char_depth_sim); // in deco with GF, above low depth reference | |
1339 | |
1340 // convert the ceiling from relative pressure to meters, | |
1341 // rounded up (i.e. made deeper) to next full meter, | |
1342 // limiting at surface. | |
1343 depth_limit = (ceiling > 0.0) ? (unsigned char)(ceiling * BAR_TO_METER + 0.99) : 0; | |
1344 | |
1345 // calculate the stop depth, i.e. round up (make deeper) to | |
1346 // the next multiple of 3 meters using integer arithmetics | |
1347 stop_depth = 3 * ( (depth_limit + 2) / 3 ); | |
623 | 1348 |
1349 // apply correction for the shallowest stop | |
628 | 1350 if( stop_depth == 3 ) stop_depth = char_I_depth_last_deco; |
623 | 1351 |
1352 // compute depth in meters that will be reached in 1 minute of ascent | |
1353 // at a speed of char_I_ascent_speed (5..10 m/min) | |
1354 if( char_depth_sim > char_I_ascent_speed ) | |
1355 { | |
1356 depth_1min = char_depth_sim - char_I_ascent_speed; | |
1357 } | |
1358 else | |
1359 { | |
1360 depth_1min = 0; | |
1361 } | |
1362 | |
628 | 1363 // is the stop depth shallower than the depth that can be reached within 1 minute, |
1364 // or are we allowed to surface AND can we reach the surface within 1 minute? | |
1365 if( ( ( stop_depth < depth_1min ) ) | |
1366 || ( ( stop_depth == 0 ) && ( depth_1min == 0 ) ) | |
1367 ) | |
623 | 1368 { |
1369 // YES - report the depth that will be reached within 1 minute of ascent | |
1370 char_depth_sim = depth_1min; | |
1371 | |
628 | 1372 // - no stop needed |
1373 need_stop = 0; | |
1374 | |
623 | 1375 // - done |
1376 goto done; | |
1377 } | |
1378 | |
1379 // ----------------------------------------------------------------------- | |
628 | 1380 // we need to make a stop now, or need to stay at the current stop depth |
623 | 1381 // ----------------------------------------------------------------------- |
1382 | |
1383 // set stop data | |
1384 need_stop = 1; | |
628 | 1385 char_depth_sim = stop_depth; |
1386 | |
1387 // Apply correction in case the stop is to be placed deeper than a | |
1388 // previously recorded stop for a gas change. This may happen because | |
1389 // the deco stops are placed at the next deeper multiple of 3 meters | |
1390 // instead of the real stop's depth. Correction is to relocate the | |
1391 // deco stop to the depth of the last gas change. The resulting combined | |
1392 // stop's duration will be the sum of the configured gas change time plus | |
1393 // the duration of the deco stop itself. | |
1394 if( 0 < internal_deco_depth[stop_index] ) | |
1395 if( char_depth_sim > internal_deco_depth[stop_index] ) | |
1396 char_depth_sim = internal_deco_depth[stop_index]; | |
623 | 1397 |
1398 // done so far if using straight Buhlmann | |
1399 if( char_I_deco_model == 0 ) goto done; | |
1400 | |
1401 // ----------------------------------------------------------------------- | |
628 | 1402 // we need to make or hold a stop and we are using the GF extension |
623 | 1403 // ----------------------------------------------------------------------- |
1404 | |
628 | 1405 // is the stop depth deeper than the GF low depth reference used up to now? |
1406 if( stop_depth > GF_low_depth ) | |
623 | 1407 { |
1408 // YES - update the reference | |
628 | 1409 GF_low_depth = stop_depth; |
623 | 1410 GF_slope = (GF_high - GF_low) / (float)GF_low_depth; |
1411 | |
1412 // store for use in next cycles | |
1413 if( deco_status & CALC_NORM ) | |
560 | 1414 { |
623 | 1415 GF_low_depth_norm = GF_low_depth; |
1416 GF_slope_norm = GF_slope; | |
582 | 1417 } |
560 | 1418 else |
1419 { | |
623 | 1420 GF_low_depth_alt = GF_low_depth; |
1421 GF_slope_alt = GF_slope; | |
560 | 1422 } |
623 | 1423 } |
1424 | |
1425 // We have a (first) stop. But with a steep GF slope, the stop(s) after this | |
1426 // first stop may be allowed to ascent to, too. This is because the gradient | |
1427 // factor that will be used at the next depth(s) will allow more tissue super- | |
1428 // saturation, maybe so much more that the next stop(s) will be allowed to | |
1429 // ascent to. So we have to probe the next stops that are within the reach of | |
1430 // 1 minute of ascent as well. | |
1431 | |
628 | 1432 // probe all stop depths that are in reach of 1 minute of ascent |
1433 next_stop = stop_depth; | |
1434 | |
1435 while(next_stop > 0) | |
623 | 1436 { |
628 | 1437 // compute the depth of the next stop ... |
1438 if ( next_stop <= char_I_depth_last_deco ) next_stop = 0; | |
1439 else if ( next_stop == 6 ) next_stop = char_I_depth_last_deco; | |
1440 else next_stop -= 3; | |
1441 | |
1442 // would the next stop be beyond the 1 minute limit? | |
1443 if( next_stop < depth_1min ) | |
582 | 1444 { |
628 | 1445 // YES - signal that probing is done |
1446 next_stop = 0; | |
582 | 1447 } |
1448 else | |
560 | 1449 { |
628 | 1450 // NO - compute the ceiling at the next stop depth |
1451 calc_limit(GF_high - GF_slope * (float)next_stop); | |
1452 | |
1453 // - limit ceiling to positive values, i.e. the surface | |
1454 if( ceiling < 0.0 ) ceiling = 0.0; | |
1455 | |
1456 // - check if this stop depth would be allowed | |
1457 if( next_stop >= (unsigned char)(ceiling * BAR_TO_METER + 0.99) ) | |
1458 { | |
1459 // YES - this stop depth is allowed | |
1460 char_depth_sim = next_stop; | |
1461 | |
1462 // - no stop needed if stop depth actually is the surface | |
1463 if( next_stop == 0 ) need_stop = 0; | |
1464 } | |
560 | 1465 } |
582 | 1466 } |
1467 | |
623 | 1468 |
1469 // ----------------------------------------------------------------------- | |
1470 // common end for straight Buhlmann and Buhlmann with GF extension | |
1471 // ----------------------------------------------------------------------- | |
1472 | |
1473 done: | |
1474 | |
1475 // calculate absolute pressure at the depth found | |
628 | 1476 calc_sim_pres_respiration(); |
0 | 1477 |
582 | 1478 return need_stop; |
0 | 1479 } |
1480 | |
604 | 1481 |
0 | 1482 ////////////////////////////////////////////////////////////////////////////// |
628 | 1483 // Find current gas in the list and get its change depth |
560 | 1484 // |
623 | 1485 // Input: char_I_current_gas_num number of current gas (1..5 or 6) |
1486 // | |
1487 // Output: sim_gas_current_num 1..6 or 0 for the manually configured gas/dil | |
1488 // sim_gas_current_depth change depth (MOD) of the gas/dil in meters | |
560 | 1489 // |
628 | 1490 static void gas_take_current(void) |
560 | 1491 { |
623 | 1492 assert( 1 <= char_I_current_gas_num && char_I_current_gas_num <= 6 ); |
1493 | |
1494 if( char_I_current_gas_num <= NUM_GAS ) // gas/diluent 1-5 | |
582 | 1495 { |
623 | 1496 sim_gas_current_num = char_I_current_gas_num; |
1497 sim_gas_current_depth = char_I_deco_gas_change[sim_gas_current_num-1]; | |
628 | 1498 |
1499 // capture case of non-configured change depth | |
1500 if( sim_gas_current_depth == 0 ) sim_gas_current_depth = 255; | |
582 | 1501 } |
1502 else | |
560 | 1503 { |
623 | 1504 sim_gas_current_num = 0; |
604 | 1505 sim_gas_current_depth = char_I_gas6_depth; |
560 | 1506 } |
1507 } | |
1508 | |
1509 | |
1510 ////////////////////////////////////////////////////////////////////////////// | |
628 | 1511 // Find the gas with the shallowest change depth below or at the current depth |
560 | 1512 // |
623 | 1513 // Input: char_depth_sim simulated depth in meters |
1514 // sim_gas_current_num number of the currently used gas/dil | |
1515 // char_I_deco_gas_type[] types of the gases/dils | |
1516 // char_I_deco_gas_change[] change depths of the gases/dils | |
1517 // | |
628 | 1518 // Modified: sim_gas_best_num index of the gas (1..5) - only if return value is true |
1519 // sim_gas_best_depth switch depth - only if return value is true | |
623 | 1520 // |
1521 // Return value is TRUE if a better gas is available | |
560 | 1522 // |
628 | 1523 static unsigned char gas_find_best(void) |
560 | 1524 { |
1525 overlay unsigned char switch_depth = 255; | |
604 | 1526 overlay unsigned char switch_gas = 0; |
560 | 1527 overlay unsigned char j; |
582 | 1528 |
628 | 1529 // loop over all gases to find the shallowest one below or at current depth |
604 | 1530 for( j = 0; j < NUM_GAS; ++j ) |
560 | 1531 { |
623 | 1532 // Is this gas available? |
1533 if( char_I_deco_gas_type[j] > 0 ) | |
604 | 1534 |
1535 // Is the change depth of the this gas deeper than or | |
1536 // at least equal to the current depth? | |
623 | 1537 if( char_I_deco_gas_change[j] >= char_depth_sim ) |
604 | 1538 |
628 | 1539 // Is the change depth of this gas shallower than or |
1540 // at least equal to the change depth of the best gas | |
1541 // found so far, or is it the first better gas found? | |
1542 if( char_I_deco_gas_change[j] <= switch_depth ) | |
1543 | |
1544 #ifdef _gas_contingency | |
1545 // Is there still enough of this gas or doesn't it matter? | |
1546 if( ( gas_volume_need[j] < gas_volume_avail[j] ) || ( !char_I_gas_contingency ) ) | |
1547 #endif | |
604 | 1548 |
1549 // If there is a yes to all these questions, we have a better gas! | |
560 | 1550 { |
628 | 1551 // memorize this gas (1..5) and its change depth |
1552 switch_gas = j+1; | |
1553 switch_depth = char_I_deco_gas_change[j]; | |
560 | 1554 } |
1555 } // continue looping through all gases to eventually find an even better gas | |
1556 | |
628 | 1557 // has a best gas been found? |
560 | 1558 if( switch_gas ) |
1559 { | |
628 | 1560 // YES - export the best gas and its change depth |
1561 sim_gas_best_num = switch_gas; | |
1562 sim_gas_best_depth = switch_depth; | |
1563 | |
1564 // is the best gas different from the current gas? | |
1565 if( sim_gas_best_num != sim_gas_current_num ) | |
1566 { | |
1567 // YES - signal advice for a gas change | |
1568 return 1; | |
1569 } | |
560 | 1570 } |
628 | 1571 |
1572 // no best gas found or current gas is the best gas | |
1573 return 0; | |
1574 } | |
1575 | |
1576 | |
1577 ////////////////////////////////////////////////////////////////////////////// | |
1578 // Switch to the best gas | |
1579 // | |
1580 // Input: sim_gas_best_num index of the best gas (1..5) | |
1581 // sim_gas_best_depth switch depth of the best gas | |
1582 // | |
1583 // Modified: sim_gas_current_num index of the new gas (1..5) | |
1584 // sim_gas_current_depth switch depth of the new gas | |
1585 // | |
1586 static void gas_take_best(void) | |
1587 { | |
1588 // set new gas | |
1589 sim_gas_current_num = sim_gas_best_num; | |
1590 sim_gas_current_depth = sim_gas_best_depth; | |
0 | 1591 } |
1592 | |
604 | 1593 |
0 | 1594 ////////////////////////////////////////////////////////////////////////////// |
623 | 1595 // Set calc_N2/He/O2_ratios by sim_gas_current_num |
1596 // | |
1597 // Input: sim_gas_current_num index of gas to use | |
1598 // real_O2_ratio, real_He_ratio if gas = 0 (the manually set gas) | |
1599 // char_I_deco_O2/He_ratio[] if gas = 1..5 (the configured gases) | |
1600 // | |
1601 // Output: sim_N2_ratio, sim_He_ratio ratios of the inert gases | |
1602 // sim_pSCR_drop ppO2 drop in pSCR loop | |
0 | 1603 // |
582 | 1604 static void gas_set_ratios(void) |
0 | 1605 { |
582 | 1606 overlay float sim_IG_ratio; |
1607 | |
623 | 1608 assert( 0 <= sim_gas_current_num <= NUM_GAS ); |
1609 | |
1610 | |
1611 #ifdef _helium | |
582 | 1612 // get gas ratios |
623 | 1613 if( sim_gas_current_num == 0 ) |
560 | 1614 { |
604 | 1615 sim_O2_ratio = real_O2_ratio; |
1616 sim_He_ratio = real_He_ratio; | |
560 | 1617 } |
1618 else | |
1619 { | |
623 | 1620 sim_O2_ratio = 0.01 * char_I_deco_O2_ratio[sim_gas_current_num-1]; |
1621 sim_He_ratio = 0.01 * char_I_deco_He_ratio[sim_gas_current_num-1]; | |
560 | 1622 } |
1623 | |
582 | 1624 // inert gas ratio (local helper variable) |
604 | 1625 sim_IG_ratio = 1.00 - sim_O2_ratio; |
582 | 1626 |
1627 // N2 ratio | |
604 | 1628 sim_N2_ratio = sim_IG_ratio - sim_He_ratio; |
623 | 1629 #else |
1630 // get O2 ratio | |
1631 sim_O2_ratio = ( sim_gas_current_num == 0 ) ? real_O2_ratio : 0.01 * char_I_deco_O2_ratio[sim_gas_current_num-1]; | |
1632 | |
1633 // set H2 ratio to zero | |
1634 sim_He_ratio = 0.0; | |
1635 | |
1636 // inert gas ratio (local helper variable) | |
1637 sim_IG_ratio = 1.00 - sim_O2_ratio; | |
1638 | |
1639 // N2 ratio | |
1640 sim_N2_ratio = sim_IG_ratio; | |
1641 #endif | |
1642 | |
1643 #ifdef _ccr_pscr | |
582 | 1644 // ppO2 drop in pSCR loop |
604 | 1645 sim_pSCR_drop = sim_IG_ratio * float_pSCR_factor; |
623 | 1646 #endif |
582 | 1647 |
1648 | |
1649 assert( 0.0 <= sim_N2_ratio && sim_N2_ratio <= 0.95 ); | |
1650 assert( 0.0 <= sim_He_ratio && sim_He_ratio <= 0.95 ); | |
1651 assert( (sim_N2_ratio + sim_He_ratio) <= 0.95 ); | |
0 | 1652 } |
1653 | |
604 | 1654 |
0 | 1655 ////////////////////////////////////////////////////////////////////////////// |
604 | 1656 // Compute respired ppO2, ppN2 and ppHe |
0 | 1657 // |
604 | 1658 // Input: tissue_increment : selector for targeting simulated or real tissues |
623 | 1659 // main_status : breathing mode for real tissues |
1660 // deco_status : breathing mode for simulated tissues | |
604 | 1661 // sim_/real_O2_ratio : (simulated) O2 ratio breathed |
1662 // sim_/real_N2_ratio : (simulated) N2 ratio breathed | |
1663 // sim_/real_He_ratio : (simulated) He ratio breathed | |
1664 // sim_/real_pres_respiration : (simulated) respiration pressure [bar] | |
1665 // sim_/real_pSCR_drop : (simulated) pSCR O2 drop | |
1666 // pres_surface : surface pressure [bar] | |
1667 // char_I_const_ppO2 : ppO2 reported from sensors or setpoint [cbar] | |
628 | 1668 // float_deco_distance : safety factor, additional depth below stop depth [bar] - not used any more |
604 | 1669 // ppWater : water-vapor pressure inside respiratory tract [bar] |
0 | 1670 // |
604 | 1671 // Output: ppN2 : respired N2 partial pressure |
1672 // ppHe : respired He partial pressure | |
623 | 1673 // char_ppO2 : breathed ppO2 in %, used for CNS calculation |
0 | 1674 // |
582 | 1675 void calc_alveolar_pressures(void) |
0 | 1676 { |
604 | 1677 overlay float calc_pres_respiration; |
582 | 1678 overlay float calc_O2_ratio; |
1679 overlay float calc_N2_ratio; | |
623 | 1680 |
1681 #ifdef _helium | |
582 | 1682 overlay float calc_He_ratio; |
623 | 1683 #endif |
1684 | |
1685 #ifdef _ccr_pscr | |
582 | 1686 overlay float calc_pSCR_drop; |
623 | 1687 #endif |
582 | 1688 |
1689 overlay unsigned char status; | |
1690 | |
1691 | |
623 | 1692 assert( 0.00 <= real_N2_ratio && real_N2_ratio <= 1.00 ); |
1693 assert( 0.00 <= real_He_ratio && real_He_ratio <= 1.00 ); | |
1694 assert( (real_N2_ratio + real_He_ratio) <= 1.00 ); | |
604 | 1695 assert( 0.800 < real_pres_respiration && real_pres_respiration < 14.0 ); |
1696 | |
623 | 1697 assert( 0.00 <= sim_N2_ratio && real_N2_ratio <= 1.00 ); |
1698 assert( 0.00 <= sim_He_ratio && real_He_ratio <= 1.00 ); | |
1699 assert( (sim_N2_ratio + sim_He_ratio) <= 1.00 ); | |
582 | 1700 assert( 0.800 < sim_pres_respiration && sim_pres_respiration < 14.0 ); |
1701 | |
1702 | |
1703 // get input data according to context | |
623 | 1704 if( tissue_increment & TISSUE_SELECTOR ) |
582 | 1705 { |
1706 //---- real tissues ----------------------------------------------------------- | |
604 | 1707 calc_pres_respiration = real_pres_respiration; |
623 | 1708 |
1709 status = main_status; | |
604 | 1710 calc_O2_ratio = real_O2_ratio; |
1711 calc_N2_ratio = real_N2_ratio; | |
623 | 1712 |
1713 #ifdef _helium | |
604 | 1714 calc_He_ratio = real_He_ratio; |
623 | 1715 #endif |
1716 | |
1717 #ifdef _ccr_pscr | |
1718 calc_pSCR_drop = real_pSCR_drop; | |
1719 #endif | |
582 | 1720 } |
1721 else | |
1722 { | |
1723 //---- simulated tissues ------------------------------------------------------ | |
623 | 1724 |
1725 // correct sim_pres_respiration if shallower than calculated stop depth | |
1726 calc_pres_respiration = ( real_pres_respiration < sim_pres_respiration ) ? real_pres_respiration : sim_pres_respiration; | |
1727 | |
628 | 1728 // +++ pressure surcharge if outside deco stops area yet ??? |
1729 | |
623 | 1730 status = deco_status; |
604 | 1731 calc_O2_ratio = sim_O2_ratio; |
1732 calc_N2_ratio = sim_N2_ratio; | |
623 | 1733 |
1734 #ifdef _helium | |
604 | 1735 calc_He_ratio = sim_He_ratio; |
623 | 1736 #endif |
1737 | |
1738 #ifdef _ccr_pscr | |
1739 calc_pSCR_drop = sim_pSCR_drop; | |
1740 #endif | |
582 | 1741 } |
1742 | |
1743 //---- OC, CCR and Bailout Mode Gas Calculations ----------------------------------- | |
1744 | |
1745 // calculate ppO2 of pure oxygen | |
604 | 1746 O2_ppO2 = calc_pres_respiration - ppWater; |
1747 | |
1748 // capture failure condition in case real_pres_respiration is < ppWater (should never happen...) | |
582 | 1749 if( O2_ppO2 < 0.0 ) O2_ppO2 = 0.0; |
1750 | |
602 | 1751 // calculate ppO2 of the pure gas (OC, diluent) |
582 | 1752 OC_ppO2 = O2_ppO2 * calc_O2_ratio; |
1753 | |
623 | 1754 #ifdef _ccr_pscr |
1755 | |
582 | 1756 // calculate pSCR ppO2 |
1757 pSCR_ppO2 = OC_ppO2 - calc_pSCR_drop; | |
1758 | |
1759 // capture failure condition in case pSCR_ppO2 becomes negative | |
1760 if( pSCR_ppO2 < 0.0 ) pSCR_ppO2 = 0.0; | |
1761 | |
1762 | |
1763 //---- Loop modes : adjust ppN2 and ppHe for change in ppO2 due to setpoint (CCR) or drop (pSCR) --- | |
623 | 1764 if( status & MODE_LOOP ) |
582 | 1765 { |
1766 overlay float const_ppO2; | |
604 | 1767 overlay float max_ppO2; |
582 | 1768 |
1769 // get the current sensor reading (CCR / pSCR if fitted) or the fixed setpoint (CCR) / a zero (pSCR) | |
1770 const_ppO2 = 0.01 * char_I_const_ppO2; | |
1771 | |
1772 // Limit the setpoint to the maximum physically possible ppO2. This prevents for | |
1773 // example calculating with a setpoint of 1.3 bar in only 2 meters of depth. | |
604 | 1774 // Additionally, the ppO2 can be further reduced to account for exhaled inert gases |
623 | 1775 // accumulating in the loop by the user-adjustable setting char_I_CC_max_frac_O2. |
604 | 1776 // (ppWater is neglected here) |
623 | 1777 max_ppO2 = 0.01 * char_I_CC_max_frac_O2 * calc_pres_respiration; |
604 | 1778 |
1779 if( const_ppO2 > max_ppO2 ) const_ppO2 = max_ppO2; | |
582 | 1780 |
1781 // check which kind of loop we are on | |
623 | 1782 if( status & MODE_PSCR ) |
560 | 1783 { |
582 | 1784 //---- pSCR Mode -------------------------------------------------------------------------- |
1785 | |
1786 // Use the sensor value if available, but only in real tissue context! | |
1787 // In all other cases use calculated ppO2. | |
623 | 1788 if ( char_I_const_ppO2 && (tissue_increment & TISSUE_SELECTOR)) ppO2 = const_ppO2; |
1789 else ppO2 = pSCR_ppO2; | |
582 | 1790 } |
560 | 1791 else |
1792 { | |
582 | 1793 //---- CCR Mode --------------------------------------------------------------------------- |
1794 | |
604 | 1795 // derive breathed ppO2 from (char_I_)const_ppO2, which holds sensor reading or selected setpoint |
582 | 1796 ppO2 = const_ppO2; |
560 | 1797 } |
1798 | |
604 | 1799 // adjust overall gas pressure for change in ppO2 due to setpoint (CCR) or drop (pSCR), |
1800 // capture potential failure conditions first: | |
1801 if( ( calc_pres_respiration < ppO2 ) // sensor reading or selected setpoint is higher than ambient pressure | |
623 | 1802 || ( calc_O2_ratio > 0.995 ) ) // diluent is pure O2, i.e. calc_N2_ratio + calc_He_ratio = 0 would give a div/0 |
560 | 1803 { |
604 | 1804 // failure condition present, set predetermined result |
1805 calc_pres_respiration = 0.0; | |
1806 } | |
1807 else | |
1808 { | |
1809 // no failure conditions present, equation can be executed | |
1810 calc_pres_respiration -= ppO2; | |
623 | 1811 #ifdef _helium |
604 | 1812 calc_pres_respiration /= calc_N2_ratio + calc_He_ratio; |
623 | 1813 #else |
1814 calc_pres_respiration /= calc_N2_ratio; | |
1815 #endif | |
560 | 1816 } |
582 | 1817 } |
1818 else | |
623 | 1819 #endif // _ccr_pscr |
604 | 1820 { |
1821 //---- OC mode --------------------------------------------------------------------------------- | |
582 | 1822 |
1823 // breathed ppO2 is ppO2 of pure gas | |
1824 ppO2 = OC_ppO2; | |
560 | 1825 } |
1826 | |
582 | 1827 |
604 | 1828 //---- derive char_ppO2 in [cbar], used for calculating CNS% --------------------------------------- |
1829 | |
582 | 1830 if ( ppO2 < 0.01 ) char_ppO2 = 0; |
1831 else if ( ppO2 >= 2.545 ) char_ppO2 = 255; | |
1832 else char_ppO2 = (unsigned char)(100 * ppO2 + 0.5); | |
1833 | |
1834 | |
1835 //---- calculate ppN2 and ppHe --------------------------------------------------------------------- | |
1836 | |
628 | 1837 // add deco safety distance when working on simulated tissues - not used any more |
1838 // if( !(tissue_increment & TISSUE_SELECTOR) ) calc_pres_respiration += float_deco_distance; | |
604 | 1839 |
1840 // compute ppN2 and ppHe, capture potential failure conditions first: | |
1841 if( calc_pres_respiration > ppWater ) | |
582 | 1842 { |
604 | 1843 // subtract water vapor pressure |
1844 calc_pres_respiration -= ppWater; | |
1845 | |
1846 // calculate partial pressures | |
1847 ppN2 = calc_N2_ratio * calc_pres_respiration; | |
623 | 1848 |
1849 #ifdef _helium | |
604 | 1850 ppHe = calc_He_ratio * calc_pres_respiration; |
623 | 1851 #else |
1852 ppHe = 0.0; | |
1853 #endif | |
1854 | |
582 | 1855 } |
1856 else | |
1857 { | |
604 | 1858 // calculated respired pressure is < water vapor pressure, thus set ppN2 and ppHe to 0 |
582 | 1859 ppN2 = 0.0; |
1860 ppHe = 0.0; | |
1861 } | |
628 | 1862 |
1863 #ifdef _helium | |
1864 // calculating real tissues? | |
1865 if( tissue_increment & TISSUE_SELECTOR ) | |
1866 { | |
1867 overlay unsigned char temp; | |
1868 | |
1869 // compute gas density of current mix in multiples of 0.01 grams per liter | |
1870 int_O_gas_density = (unsigned int)( 17.9 * ppHe + 125.1 * ppN2 + 142.8 * ppO2 ); | |
1871 | |
1872 // convert gas density into a 8 bit integer | |
1873 temp = (unsigned char)( (int_O_gas_density + 9) / 10 ); | |
1874 | |
1875 // limit to display max and set warning or attention flag | |
1876 if( temp > 99 ) int_O_gas_density = 999 | INT_FLAG_WARNING; | |
1877 else if( temp > char_I_gas_density_warn ) int_O_gas_density |= INT_FLAG_WARNING; | |
1878 else if( temp > char_I_gas_density_att ) int_O_gas_density |= INT_FLAG_ATTENTION; | |
1879 } | |
1880 #endif | |
0 | 1881 } |
1882 | |
604 | 1883 |
0 | 1884 ////////////////////////////////////////////////////////////////////////////// |
623 | 1885 // Initializes all output variables to their default values |
1886 // | |
1887 static void init_output_vars(void) | |
1888 { | |
1889 // clear the internal stops table from remains lasting from the previous dive or deco calculator run | |
1890 clear_deco_table(); | |
1891 | |
1892 // publish the cleared stops table to the display functions | |
1893 publish_deco_table(); | |
1894 | |
1895 // clear the published gas needs in volume and pressure | |
1896 for( i = 0; i < NUM_GAS; ++i ) | |
1897 { | |
1898 int_O_gas_need_vol[i] = 0; | |
1899 int_O_gas_need_pres[i] = 0 + INT_FLAG_ZERO + INT_FLAG_INVALID; | |
1900 } | |
1901 | |
1902 // values initially to be set to zero | |
1903 int_O_ceiling = 0; // ceiling depth in mbar | |
1904 char_O_deco_info = 0; // clear all deco information flags | |
1905 char_O_deco_warnings = 0; // clear all deco warning flags | |
1906 | |
1907 // default desaturation time to 24 hours (it will not be computed during a dive) | |
1908 int_O_desaturation_time = 1440; | |
1909 | |
1910 // initialize CNS values | |
1911 int_O_CNS_norm = 0 + INT_FLAG_INVALID; | |
1912 int_O_CNS_alt = 0 + INT_FLAG_INVALID; | |
1913 | |
1914 // initialize NDL times | |
1915 char_O_NDL_norm = 240; | |
1916 char_O_NDL_alt = 240; | |
1917 | |
1918 // initialize ascent times | |
1919 int_O_TTS_norm = 0; | |
1920 int_O_TTS_alt = 0 + INT_FLAG_INVALID + INT_FLAG_NOT_COMPUTED_YET; | |
1921 | |
1922 #ifdef _rx_functions | |
1923 // clear TR values | |
1924 int_O_SAC_measured = 0 + INT_FLAG_NOT_AVAIL; // SAC rate | |
1925 int_O_pressure_need[0] = 0 + INT_FLAG_NOT_AVAIL; // pressure need to reading 1 | |
1926 int_O_pressure_need[1] = 0 + INT_FLAG_NOT_AVAIL; // pressure need to reading 2 | |
628 | 1927 int_O_tank_pressure = 0; // tank pressure for logging |
623 | 1928 #endif |
1929 | |
628 | 1930 #ifdef _helium |
1931 int_O_gas_density = 0; | |
1932 #endif | |
1933 | |
1934 // also clear any time++ request | |
1935 char_I_sim_advance_time = 0; | |
623 | 1936 } |
1937 | |
1938 | |
1939 ////////////////////////////////////////////////////////////////////////////// | |
1940 // Reset all tissues to surface pressure equilibrium state | |
1941 // | |
1942 // Input: int_I_pres_surface current surface pressure in hPa (mbar) | |
1943 // | |
1944 // Output: real_pres_tissue_N2[] partial pressure of N2 in real tissues | |
1945 // real_pres_tissue_He[] partial pressure of He in real tissues | |
1946 // char_O_tissue_pres_N2[] partial pressure of N2 for tissue graphics | |
1947 // char_O_tissue_pres_He[] partial pressure of He for tissue graphics | |
1948 // char_O_tissue_pressure[] total pressure for tissue graphics | |
1949 // CNS_fraction_real internal CNS value | |
1950 // int_O_CNS_current current CNS value | |
1951 // int_O_CNS_norm CNS value at end of normal dive plan | |
1952 // int_O_CNS_alt CNS value at end of alternative dive plan | |
1953 // char_O_deco_warnings deco warnings vector | |
1954 // char_O_NDL_norm remaining NDL time in normal dive plan | |
1955 // char_O_NDL_alt remaining NDL time in alternative dive plan | |
1956 // int_O_TTS_norm ascent time (TTS) in normal dive plan | |
1957 // int_O_TTS_alt ascent time (TTS) in alternative dive plan | |
1958 // int_O_lead_supersat supersaturation of the leading tissue | |
0 | 1959 // |
1960 static void clear_tissue(void) | |
1961 { | |
623 | 1962 // safeguard and convert the surface pressure (mbar -> bar) (*) |
1963 if( int_I_pres_surface < 500 ) pres_surface = 0.500; | |
1964 else pres_surface = 0.001 * int_I_pres_surface; | |
1965 | |
1966 // calculate partial pressure of N2 in respired air at surface pressure | |
1967 calc_N2_equilibrium(); | |
1968 | |
1969 // cycle through the 16 Buhlmann tissues | |
604 | 1970 for( ci = 0; ci < NUM_COMP; ci++ ) |
582 | 1971 { |
623 | 1972 // reset tissue pressures |
1973 real_pres_tissue_He[ci] = 0.0; // He | |
1974 real_pres_tissue_N2[ci] = N2_equilibrium; // N2 | |
1975 | |
1976 // reset tissue pressures for scaled for tissue graphics | |
1977 char_O_tissue_pres_He[ci] = 0; // He | |
1978 char_O_tissue_pres_N2[ci] = 10; // N2 | |
1979 char_O_tissue_pressure[ci] = 10; // combined | |
582 | 1980 } |
1981 | |
1982 // reset CNS values | |
623 | 1983 CNS_fraction_real = 0.0; |
1984 int_O_CNS_current = int_O_CNS_norm = int_O_CNS_alt = 0; | |
582 | 1985 |
1986 // reset some more vars to their defaults | |
623 | 1987 char_O_NDL_norm = 240; |
1988 char_O_NDL_alt = 240; | |
1989 int_O_TTS_norm = 0; | |
1990 int_O_TTS_alt = 0; | |
1991 int_O_lead_supersat = 0; | |
1992 | |
1993 // reset all warning and info flags | |
1994 char_O_deco_warnings = 0; | |
1995 char_O_deco_info = 0; | |
0 | 1996 } |
1997 | |
582 | 1998 |
0 | 1999 ////////////////////////////////////////////////////////////////////////////// |
628 | 2000 // Deco engine main code |
0 | 2001 // |
623 | 2002 // This is the major code in dive mode, it calculates the tissue pressures, |
2003 // the bottom time, and it calculates the ascend with all deco stops, etc. | |
2004 // | |
2005 // Input: char_O_main_status deco engine control and real tissues mode | |
2006 // char_O_deco_status deco engine control and simulated tissues mode | |
2007 // char_I_sim_advance_time mailbox for bottom time incrementing | |
2008 // | |
2009 // char_I_SAC_work gas usage rate during working phase in l/min | |
2010 // char_I_SAC_deco gas usage rate during deco stops phase in l/min | |
2011 // | |
2012 // char_I_deco_model selector for GF extension | |
2013 // char_I_ascent_speed ascent speed | |
2014 // char_I_saturation_multiplier safety factor for tissue saturation | |
2015 // char_I_desaturation_multiplier safety factor for tissue desaturation | |
2016 // | |
2017 // char_I_pressure_gas[] amount of gas available for ascent in bar | |
2018 // int_I_pressure_drop[] pressure drop used to calculate SAC rate | |
2019 // char_I_gas_avail_size[] size of the tanks in liters | |
2020 // | |
2021 // Output: int_O_O2_ppO2 partial pressure of pure O2 at current depth | |
2022 // int_O_pure_ppO2 partial pressure of O2 in gas at current depth | |
2023 // int_O_pSCR_ppO2 partial pressure of O2 in gas at current depth, corrected for pSCR mode | |
2024 // int_O_breathed_ppO2 partial pressure of O2 currently breathed | |
2025 // | |
2026 // char_O_deco_status deco engine computations status | |
2027 // char_O_deco_info deco engine information vector | |
2028 // char_O_deco_warnings deco engine warnings vector | |
2029 // | |
2030 // char_O_NDL_norm remaining NDL time in normal dive plan | |
2031 // char_O_NDL_alt remaining NDL time in alternative dive plan | |
2032 // int_O_TTS_norm ascent time (TTS) in normal dive plan | |
2033 // int_O_TTS_alt ascent time (TTS) in alternative dive plan | |
2034 // int_O_CNS_norm CNS value at end of normal dive plan | |
2035 // int_O_CNS_alt CNS value at end of alternative dive plan | |
2036 // | |
2037 // int_O_gas_need_vol calculated gas volumes needed for ascent | |
2038 // int_O_gas_need_pres calculated gas pressures needed for ascent | |
2039 // | |
2040 // int_O_SAC_measured measured surface air consumption (SAC) rate in l/min | |
2041 // | |
2042 // Modified: int_IO_pressure_value[] warning flags added to pressure reading 1 & 2 | |
2043 // int_IO_pressure_need[] pressure needs to pressure reading 1 & 2 | |
0 | 2044 // |
2045 static void calc_hauptroutine(void) | |
2046 { | |
623 | 2047 overlay unsigned short int_ppO2_min; |
628 | 2048 overlay unsigned short int_ppO2_max_warn; |
2049 overlay unsigned short int_ppO2_max_att; | |
623 | 2050 overlay unsigned short int_ppO2_max_dil; |
2051 overlay float EAD; | |
2052 overlay float END; | |
2053 | |
2054 //============================================================================================= | |
2055 | |
2056 // | |
2057 //--- Setup Part --------------------------------------------------------------------------------- | |
2058 // | |
2059 | |
2060 // set time limit for preempting deco calculations, timer is 16 bit and increments every 1/32 ms | |
2061 tmr5_value = 65535 - (32 * BUDGET_PER_SECOND / INVOKES_PER_SECOND); | |
2062 | |
2063 // load timer | |
2064 load_tmr5(); | |
2065 | |
2066 // read command flags and set up what to do | |
2067 switch( char_O_deco_status & COMMAND_MASK ) | |
560 | 2068 { |
623 | 2069 |
2070 case INITIALIZE: | |
2071 case INITIALIZE_START_NORM: | |
2072 case INITIALIZE_START_ALT: | |
2073 | |
2074 // copy master modes to shadow registers | |
2075 main_status = char_O_main_status; | |
2076 deco_status = char_O_deco_status; | |
2077 | |
2078 // clear all command flags on the master mode to signal that the command is read | |
2079 char_O_deco_status &= ~COMMAND_MASK; | |
2080 | |
2081 // clear the initialization flag on the shadow copy | |
2082 deco_status &= ~INITIALIZE; | |
2083 | |
2084 // initialize the sequence timer | |
2085 sequence_timer = 0; | |
2086 | |
2087 // set the calculation phase to start with to doing the once-per-dive initialization | |
2088 next_planning_phase = PHASE_10_DIVE_INIT; | |
2089 | |
2090 break; | |
2091 | |
2092 | |
2093 case START_NORM: | |
2094 case START_ALT: | |
2095 | |
2096 // copy master modes to shadow registers | |
2097 main_status = char_O_main_status; | |
2098 deco_status = char_O_deco_status; | |
2099 | |
2100 // clear all command flags on the master mode to signal that the command is read | |
2101 char_O_deco_status &= ~COMMAND_MASK; | |
2102 | |
2103 // set the calculation phase to start with to doing the cyclic initialization | |
628 | 2104 next_planning_phase = PHASE_20_CYCLIC_INIT; |
623 | 2105 |
2106 // continue in CALCULATING | |
2107 | |
2108 | |
2109 case CALCULATING: | |
2110 | |
2111 // keep current calculation phase | |
2112 | |
2113 // step the sequence timer | |
2114 sequence_timer = (sequence_timer < INVOKES_PER_SECOND * 2 - 1) ? sequence_timer + 1 : 0; | |
2115 | |
2116 break; | |
560 | 2117 } |
2118 | |
582 | 2119 |
623 | 2120 // |
2121 //--- End of Setup Part ----------------------------------------------------------------------- | |
2122 // | |
2123 | |
2124 //============================================================================================= | |
2125 | |
2126 // | |
2127 //---- Calculations Part (real Tissues) ------------------------------------------------------- | |
2128 // | |
2129 | |
582 | 2130 |
604 | 2131 // target the real tissues with 2 second increments by default |
623 | 2132 tissue_increment = TISSUE_SELECTOR | 0; |
2133 | |
2134 | |
2135 // Tasks every second, if more than 1 invocation per second: on the first section of the second. | |
2136 // Requests for tissue "fast forward" are executed immediately. | |
2137 #if (INVOKES_PER_SECOND > 1) | |
2138 if( ( sequence_timer == 0 ) | |
2139 || ( sequence_timer == INVOKES_PER_SECOND ) | |
2140 || ( char_I_sim_advance_time > 0 ) | |
2141 ) | |
2142 #endif | |
584 | 2143 { |
623 | 2144 // acquire current environmental data |
2145 calc_hauptroutine_data_input(); | |
2146 | |
628 | 2147 // calculate ppO2, ppN2 and ppHe for real tissues |
623 | 2148 calc_alveolar_pressures(); |
2149 | |
2150 // add decent calculation here and include trigger in above if-statement | |
2151 // TODO | |
2152 | |
2153 } // tasks every second, on the first section of the second | |
2154 | |
2155 | |
2156 // Tasks every 2 seconds, on the first section of the respective second. | |
2157 // Requests for tissue "fast forward" are executed immediately. | |
2158 if( ( sequence_timer == 0 ) | |
2159 || ( char_I_sim_advance_time > 0 ) | |
2160 ) | |
2161 { | |
2162 // Tissue and CNS updates are based on 2 seconds periods! | |
2163 | |
604 | 2164 // Set up normal tissue updating or "fast forward" updating for simulator |
2165 // sim+5' function and deco calculator bottom time calculation. | |
2166 if( char_I_sim_advance_time > 0 ) | |
2167 { | |
2168 // configure "fast forward" tissue updating | |
623 | 2169 tissue_increment = TISSUE_SELECTOR | char_I_sim_advance_time; |
2170 | |
2171 // clear the request | |
604 | 2172 char_I_sim_advance_time = 0; |
2173 } | |
2174 | |
584 | 2175 // calculate the real tissues |
2176 calc_tissues(); | |
2177 | |
604 | 2178 // update the CNS value for the real tissues |
2179 calc_CNS(); | |
2180 | |
2181 // calculate ceiling (at GF_high or 100%) and leading tissue supersaturation | |
623 | 2182 if ( char_I_deco_model ) calc_limit(GF_high); // GF factors enabled |
2183 else calc_limit( 1.0 ); // classic Buhlmann | |
2184 | |
2185 // convert the ceiling value to integer | |
604 | 2186 convert_ceiling_for_display(); |
2187 | |
623 | 2188 // convert the saturation value of the leading tissue to integer |
2189 convert_sat_for_display(); | |
2190 | |
2191 // convert the CNS value to integer | |
2192 convert_cur_CNS_for_display(); | |
2193 | |
2194 } // tasks every 2 seconds | |
2195 | |
2196 | |
2197 // Tasks every second, if more than 1 invocation per second: on the first section of the second. | |
2198 #if (INVOKES_PER_SECOND > 1) | |
2199 if( ( sequence_timer == 0 ) | |
2200 || ( sequence_timer == INVOKES_PER_SECOND ) | |
2201 ) | |
2202 #endif | |
582 | 2203 { |
623 | 2204 //---- Calculate and Export EAD and END ------------------------------------------------------ |
2205 | |
2206 // calculate EAD (Equivalent Air Depth): equivalent depth for the same N2 level with plain air | |
2207 EAD = (ppN2 / 0.7902 + ppWater - pres_surface) * BAR_TO_METER; | |
2208 | |
2209 // calculate END (Equivalent Narcotic Depth): here O2 is treated as narcotic, too | |
2210 // Source cited: The Physiology and Medicine of Diving by Peter Bennett and David Elliott, | |
2211 // 4th edition, 1993, W.B.Saunders Company Ltd, London. | |
2212 END = (real_pres_respiration - ppHe - pres_surface) * BAR_TO_METER; | |
2213 | |
2214 // export EAD | |
2215 float_value = EAD; convert_float_to_char(); char_O_EAD = char_value; | |
2216 | |
2217 // export END | |
2218 float_value = END; convert_float_to_char(); char_O_END = char_value; | |
2219 | |
2220 | |
2221 //---- Compute ppO2 Values in [cbar] --------------------------------------------------------- | |
2222 | |
2223 float_value = ppO2; convert_float_to_int(); int_O_breathed_ppO2 = int_value; // breathed gas | |
2224 #ifdef _ccr_pscr | |
2225 float_value = O2_ppO2; convert_float_to_int(); int_O_O2_ppO2 = int_value; // pure oxygen | |
2226 float_value = OC_ppO2; convert_float_to_int(); int_O_pure_ppO2 = int_value; // pure gas | |
2227 float_value = pSCR_ppO2; convert_float_to_int(); int_O_pSCR_ppO2 = int_value; // pSCR calculated | |
2228 #endif | |
2229 | |
2230 | |
628 | 2231 //---- Set/Clear Deco Mode ------------------------------------------------------------------ |
2232 | |
2233 // Clear the deco mode flag if: | |
2234 // deco mode is set | |
2235 // AND we are deeper than 7 meters below the deepest deco stop | |
2236 // (7 meters chosen as to be 2 stop depth intervals plus 1 additional meter below) | |
2237 if ( ( deco_info & DECO_MODE ) > 0 ) | |
2238 if ( ( char_depth_real ) > char_O_deco_depth[0] + 7 ) | |
2239 deco_info &= ~DECO_MODE; | |
623 | 2240 |
2241 // Set the deco mode flag if: | |
2242 // deco mode is not set | |
2243 // AND breathing an OC deco gas (gas type 3) | |
2244 // OR breathing a gas or diluent that officially is disabled (type 0) | |
628 | 2245 // OR there is a deco stop |
2246 if ( ( deco_info & DECO_MODE ) == 0 ) | |
2247 if ( ( char_I_current_gas_type == 3 ) | |
2248 || ( char_I_current_gas_type == 0 ) | |
2249 || ( char_O_deco_depth[0] > 0 ) | |
623 | 2250 ) |
628 | 2251 deco_info |= DECO_MODE; |
623 | 2252 |
2253 | |
2254 //---- Compute ppO2 Warnings ------------------------------------------------------------------ | |
2255 | |
628 | 2256 // compute conditional min value |
623 | 2257 #ifdef _ccr_pscr |
2258 int_ppO2_min = ( main_status & MODE_LOOP ) ? (unsigned short)char_I_ppO2_min_loop : (unsigned short)char_I_ppO2_min; | |
2259 #else | |
2260 int_ppO2_min = (unsigned short)char_I_ppO2_min; | |
2261 #endif | |
2262 | |
628 | 2263 // determine the absolute max value (should be the deco one, but who knows...) |
2264 int_ppO2_max_warn = ( char_I_ppO2_max_work > char_I_ppO2_max_deco) ? char_I_ppO2_max_work : char_I_ppO2_max_deco; | |
2265 | |
2266 // add some margin to compensate for surface pressures > 1.000 mbar | |
2267 int_ppO2_max_warn += ppO2_MARGIN_ON_MAX; | |
2268 | |
2269 // determine the normal max value | |
2270 int_ppO2_max_att = ( deco_info & DECO_MODE ) ? (unsigned short)char_I_ppO2_max_deco : (unsigned short)char_I_ppO2_max_work; | |
2271 | |
2272 // add some margin to compensate for surface pressures > 1.000 mbar | |
2273 int_ppO2_max_att += ppO2_MARGIN_ON_MAX; | |
623 | 2274 |
2275 #ifdef _ccr_pscr | |
628 | 2276 // default value for the upper diluent ppO2 warning threshold is the upper warning threshold |
2277 int_ppO2_max_dil = int_ppO2_max_warn; | |
2278 | |
2279 // when enabled and in CCR mode, the upper diluent warning threshold gets adjust according to the current setpoint | |
2280 if( char_I_dil_ppO2_check ) | |
623 | 2281 if( (main_status & MODE_MASK) == MODE_CCR ) |
2282 { | |
2283 overlay unsigned short max_dil; | |
2284 | |
2285 // The upper diluent ppO2 threshold is ppO2_GAP_TO_SETPOINT below the setpoint... | |
2286 // (the condition protects from negative numbers which would cause a wrap-around in unsigned integers) | |
2287 max_dil = (char_I_const_ppO2 > ppO2_GAP_TO_SETPOINT) ? (unsigned short)(char_I_const_ppO2 - ppO2_GAP_TO_SETPOINT) : 0; | |
2288 | |
628 | 2289 // ...but never above int_ppO2_max_warn |
2290 if( max_dil < int_ppO2_max_warn ) int_ppO2_max_dil = max_dil; | |
623 | 2291 |
2292 // We do not need to guard int_ppO2_max_dil against becoming lower than char_I_ppO2_min because the check | |
2293 // against char_I_ppO2_min is done first and will then raise a low warning and inhibit further checks. | |
2294 } | |
2295 #endif | |
2296 | |
2297 // check for safe range of breathed gas | |
628 | 2298 if ( int_O_breathed_ppO2 <= int_ppO2_min ) int_O_breathed_ppO2 |= INT_FLAG_WARNING + INT_FLAG_LOW; |
2299 else if ( int_O_breathed_ppO2 >= int_ppO2_max_warn ) int_O_breathed_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; | |
2300 else if ( deco_info & DECO_MODE ) ; // no attention generated in deco mode | |
2301 else if ( main_status & MODE_LOOP ) ; // no attention generated in loop modes | |
2302 else if ( int_O_breathed_ppO2 >= int_ppO2_max_att ) int_O_breathed_ppO2 |= INT_FLAG_ATTENTION; | |
623 | 2303 |
2304 #ifdef _ccr_pscr | |
2305 // check for safe range of pure oxygen | |
628 | 2306 if ( int_O_O2_ppO2 >= int_ppO2_max_warn ) int_O_O2_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; |
623 | 2307 |
2308 // check for safe range of pure diluent | |
628 | 2309 if ( int_O_pure_ppO2 <= (unsigned short)char_I_ppO2_min ) int_O_pure_ppO2 |= INT_FLAG_WARNING + INT_FLAG_LOW; |
2310 else if ( int_O_pure_ppO2 >= int_ppO2_max_warn ) int_O_pure_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; | |
2311 else if ( int_O_pure_ppO2 >= int_ppO2_max_dil ) int_O_pure_ppO2 |= INT_FLAG_ATTENTION; | |
623 | 2312 |
2313 // check for safe range of calculated pSCR loop gas | |
628 | 2314 if ( int_O_pSCR_ppO2 <= int_ppO2_min ) int_O_pSCR_ppO2 |= INT_FLAG_WARNING + INT_FLAG_LOW; |
2315 else if ( int_O_pSCR_ppO2 >= int_ppO2_max_warn ) int_O_pSCR_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; | |
623 | 2316 #endif |
2317 | |
2318 } // tasks every second / on the first section of the second | |
604 | 2319 |
2320 | |
2321 #ifdef _rx_functions | |
2322 | |
623 | 2323 // only when TR functions are enabled |
2324 if( main_status & TR_FUNCTIONS ) | |
2325 | |
2326 // Tasks every second, if more than 1 invocation per second: on the second section of the second. | |
2327 #if (INVOKES_PER_SECOND > 1) | |
2328 if( ( sequence_timer == 1 ) | |
2329 || ( sequence_timer == INVOKES_PER_SECOND + 1 ) | |
2330 ) | |
2331 #endif | |
604 | 2332 { |
623 | 2333 calc_TR_functions(); |
2334 } | |
2335 | |
2336 #endif // _rx_functions | |
2337 | |
2338 | |
2339 // | |
604 | 2340 //---- End of Computations for the real Tissues ----------------------------------------------- |
2341 // | |
623 | 2342 |
604 | 2343 //============================================================================================= |
623 | 2344 |
604 | 2345 // |
623 | 2346 //---- Begin of Computations for Ascent and Decompression (simulated Tissues) ----------------- |
2347 // | |
2348 | |
2349 // Dispatcher: select what to do based on the current calculation phase | |
2350 do | |
582 | 2351 { |
623 | 2352 |
2353 #ifdef _profiling | |
2354 profiling_phase = next_planning_phase; | |
2355 #endif | |
2356 | |
2357 switch( next_planning_phase ) | |
560 | 2358 { |
623 | 2359 |
2360 // | |
2361 //---- once-per-dive Initialization of the Deco Engine ------------------------------------ | |
2362 // | |
2363 case PHASE_10_DIVE_INIT: | |
2364 | |
2365 // initialize all output variables to defaults | |
2366 init_output_vars(); | |
2367 | |
2368 // safeguard input parameters that are constant during the course of the dive | |
2369 if( char_I_ascent_speed < 5 ) char_I_ascent_speed = 5; | |
2370 if( char_I_ascent_speed > 10 ) char_I_ascent_speed = 10; | |
2371 | |
2372 // convert input parameters to float numbers | |
2373 float_ascent_speed = 1.00 * char_I_ascent_speed; | |
582 | 2374 |
628 | 2375 |
582 | 2376 // initialize values that will be recalculated later on periodically |
623 | 2377 deco_warnings = 0; // reset all deco warnings |
2378 deco_info = 0; // reset all deco infos | |
2379 IBCD_tissue_vector = 0; // reset tissue IBCD vector | |
2380 NDL_tissue_start_norm = 0; // initialize the tissue to start with when calculating normal NDL time | |
2381 NDL_tissue_start_alt = 0; // initialize the tissue to start with when calculating alternative NDL time | |
2382 | |
2383 // enforce initialization of GF data on first cyclic initialization | |
628 | 2384 GF_high_last = 255; |
2385 GF_low_last = 255; | |
2386 | |
2387 #ifdef _gas_contingency | |
2388 // shall check for gas pan out? | |
2389 if( char_I_gas_contingency ) | |
2390 { | |
2391 overlay float reduction = 0.5 * (float)char_I_SAC_deco * (1.0 + (float)char_I_depth_last_deco / 10.0); | |
2392 | |
2393 // YES - calculate volumes available for each gas | |
2394 for( i = 0; i < NUM_GAS; i++ ) | |
2395 { | |
2396 // total available volume = tank size * fill press, char_I_gas_avail_pres is in multiples of 10 bar | |
2397 gas_volume_avail[i] = (float)char_I_gas_avail_size[i] * (float)char_I_gas_avail_pres[i] * 10.0; | |
2398 | |
2399 // reduce total available volumes by due for 1/2 minute on last stop | |
2400 gas_volume_avail[i] -= reduction; | |
2401 } | |
2402 } | |
2403 #endif | |
623 | 2404 |
2405 #ifdef _cave_mode | |
2406 char_I_backtrack_time = 0; //clear backtracking time (index to char_I_backtrack_depth) | |
2407 char_I_backtrack_depth = 0; //prime first entry with a depth of 0 meter | |
2408 #endif | |
2409 | |
2410 #ifdef _profiling | |
2411 int_O_profiling_overrun_max = 0; | |
2412 char_O_profiling_runs_norm = 0; | |
2413 char_O_profiling_runs_alt = 0; | |
2414 #endif | |
2415 | |
2416 | |
2417 // the next calculation phase will do the cyclic initialization of the deco engine if a | |
2418 // normal or alternative plan shall be calculated, else the calculation cycle is done. | |
628 | 2419 if( deco_status & PLAN_MASK ) next_planning_phase = PHASE_20_CYCLIC_INIT; |
623 | 2420 else next_planning_phase = PHASE_00_DONE; |
2421 | |
2422 break; | |
2423 | |
582 | 2424 |
2425 // | |
628 | 2426 //---- once-per-cycle Initialization of the Deco Engine ----------------------------------- |
582 | 2427 // |
628 | 2428 case PHASE_20_CYCLIC_INIT: |
623 | 2429 |
2430 // target the simulated tissues (flag bit 7 = 0) | |
2431 tissue_increment = 0; | |
2432 | |
2433 // clear the internal stops table | |
582 | 2434 clear_deco_table(); |
604 | 2435 |
560 | 2436 // initialize the simulated tissues with the current state of the real tissues |
604 | 2437 for( i = 0; i < NUM_COMP; i++ ) |
560 | 2438 { |
623 | 2439 sim_pres_tissue_N2[i] = real_pres_tissue_N2[i]; |
2440 sim_pres_tissue_He[i] = real_pres_tissue_He[i]; | |
582 | 2441 } |
2442 | |
623 | 2443 // initialize GF parameters if using GF model |
2444 if( char_I_deco_model != 0 ) | |
604 | 2445 { |
623 | 2446 // update GF parameters (GFs may have been switched between GF and aGF) |
628 | 2447 if( (char_I_GF_Low_percentage != GF_low_last) || (char_I_GF_High_percentage != GF_high_last) ) |
604 | 2448 { |
623 | 2449 // store new values in integer format |
628 | 2450 GF_low_last = char_I_GF_Low_percentage; |
623 | 2451 GF_high_last = char_I_GF_High_percentage; |
628 | 2452 |
2453 // safeguard and store new values in float format | |
2454 GF_low = ( GF_low_last > 10 ) ? 0.01 * GF_low_last : 0.10 ; | |
2455 GF_high = ( GF_high_last > GF_low_last ) ? 0.01 * GF_high_last : GF_low; | |
2456 | |
623 | 2457 |
2458 // reset low depth references and slopes | |
2459 GF_low_depth_norm = 0; | |
2460 GF_low_depth_alt = 0; | |
2461 GF_slope_norm = 0.0; | |
2462 GF_slope_alt = 0.0; | |
604 | 2463 } |
623 | 2464 |
2465 // retrieve GF parameters for current calculation cycle | |
2466 if( deco_status & CALC_NORM ) | |
560 | 2467 { |
623 | 2468 GF_low_depth = GF_low_depth_norm; |
2469 GF_slope = GF_slope_norm; | |
560 | 2470 } |
2471 else | |
2472 { | |
623 | 2473 GF_low_depth = GF_low_depth_alt; |
2474 GF_slope = GF_slope_alt; | |
560 | 2475 } |
604 | 2476 } |
2477 | |
623 | 2478 // initialize the simulated CNS value with the current CNS value of the real tissues |
2479 CNS_fraction_sim = CNS_fraction_real; | |
2480 | |
628 | 2481 // initialize the simulated depth with the current depth, |
2482 // memorize current depth as start depth of the simulation | |
623 | 2483 sim_pres_respiration = real_pres_respiration; |
628 | 2484 char_depth_sim = char_depth_real; |
2485 char_depth_sim_start = char_depth_real; | |
623 | 2486 |
2487 // Lookup the gas that is currently breathed with the real tissues and set it as | |
2488 // the gas to be used with the simulated tissues, too. This gas will be used until | |
628 | 2489 // gas_find_best()/gas_take_best() is invoked and switches to a better gas. |
2490 gas_take_current(); | |
2491 | |
2492 // Setup the calculation ratio's for N2, He and O2 (sim_N2/He/_O2_ratio). | |
2493 // These ratios can be kept until a gas switch is done. Thus, if a call to | |
2494 // gas_find_best() has found a better gas and this gas is taken by a call | |
2495 // to gas_take_best(), gas_set_ratios() needs to be called again. | |
623 | 2496 gas_set_ratios(); |
2497 | |
2498 // compute ppO2, ppN2 and ppHe for current depth from sim_pres_respiration | |
2499 calc_alveolar_pressures(); | |
2500 | |
2501 // initialize the no decompression limit (NDL) time to 240 minutes | |
2502 NDL_time = 240; | |
2503 | |
628 | 2504 // initialize the total ascent time to 0 minutes |
2505 ascent_time = 0; | |
2506 | |
623 | 2507 // retrieve the tissue that had the shortest NDL time during last calculation |
2508 NDL_tissue_start = ( deco_status & CALC_NORM ) ? NDL_tissue_start_norm : NDL_tissue_start_alt; | |
2509 | |
2510 // start calculating NDL time with the tissue that had the shortest NDL last time | |
2511 NDL_tissue = NDL_tissue_start; | |
2512 NDL_tissue_lead = NDL_tissue_start; | |
2513 | |
2514 // initialization for calculating the initial ascent | |
2515 // start with 1 minute ascent steps when calculating the initial ascent | |
2516 fast = 1; | |
2517 | |
2518 // initialization for convert_gas_needs_to_press() | |
2519 gas_needs_gas_index = 0; | |
2520 | |
628 | 2521 // shall calculate gas needs? |
2522 if( main_status & CALC_VOLUME ) | |
2523 { | |
2524 // set the usage rate (SAC rate), starting with working part of the dive | |
2525 gas_needs_usage_rate = char_I_SAC_work; | |
2526 | |
2527 // clear the gas volume needs for gases 1-5 | |
2528 for( i = 0; i < NUM_GAS; ++i ) gas_volume_need[i] = 0.0; | |
2529 | |
2530 #ifdef _rx_functions | |
2531 // only for OSTC TR model with TR functions enabled | |
2532 if( main_status & TR_FUNCTIONS ) | |
2533 { | |
2534 // invalidate pressure needs to pressure readings | |
2535 int_O_pressure_need[0] = 0 + INT_FLAG_NOT_AVAIL; | |
2536 int_O_pressure_need[1] = 0 + INT_FLAG_NOT_AVAIL; | |
2537 } | |
2538 #endif | |
2539 } | |
623 | 2540 |
2541 #ifdef _profiling | |
2542 profiling_runs = 0; | |
2543 #endif | |
2544 | |
2545 // The next calculation phase will | |
628 | 2546 // - calculate the extended bottom segment if extended bottom time is configured (fTTS), |
2547 // - calculate the bottom segment gas need if gas needs calculation is configured, | |
623 | 2548 // - proceed with calculating the NDL time else. |
628 | 2549 if ( deco_status & DELAYED_ASCENT ) next_planning_phase = PHASE_30_EXTENDED_BOTTOM_TIME; |
2550 else if ( main_status & CALC_VOLUME ) next_planning_phase = PHASE_40_BOTTOM_GAS_NEED; | |
2551 else next_planning_phase = PHASE_50_NDL_TIME; | |
623 | 2552 |
2553 break; | |
2554 | |
2555 | |
2556 // | |
2557 //---- extended Bottom Time --------------------------------------------------------------- | |
2558 // | |
628 | 2559 case PHASE_30_EXTENDED_BOTTOM_TIME: |
623 | 2560 |
2561 // program interval on simulated tissues (flag bit 7 = 0) | |
2562 tissue_increment = char_I_extra_time; | |
2563 | |
628 | 2564 // update the simulated tissues for tissue_increment (char_I_extra_time) minutes at depth, |
2565 // calc_alveolar_pressures() has already been called in cyclic initialization | |
623 | 2566 calc_tissues(); |
2567 | |
628 | 2568 // update the CNS value for tissue_increment (char_I_extra_time) minutes at depth |
623 | 2569 calc_CNS(); |
2570 | |
628 | 2571 // the next calculation phase will |
2572 // - calculate the extended bottom segment gas needs if gas needs calculation is configured, | |
2573 // - proceed with calculating the NDL time else. | |
2574 if ( main_status & CALC_VOLUME ) next_planning_phase = PHASE_40_BOTTOM_GAS_NEED; | |
2575 else next_planning_phase = PHASE_50_NDL_TIME; | |
2576 | |
2577 break; | |
2578 | |
2579 | |
2580 // | |
2581 //---- Bottom Segment Gas Need ------------------------------------------------------------ | |
2582 // | |
2583 case PHASE_40_BOTTOM_GAS_NEED: | |
2584 | |
2585 // on gas 1-5 ? | |
2586 if( sim_gas_current_num ) | |
2587 { | |
2588 // YES - set the bottom depth | |
2589 gas_needs_depth = char_depth_sim_start; | |
2590 | |
2591 // take either the whole bottom time or just the fTTS/bailout extra time | |
2592 gas_needs_time = ( main_status & CALCULATE_BOTTOM ) ? char_I_bottom_time : char_I_extra_time; | |
2593 | |
2594 // calculate gas demand | |
2595 calc_due_by_depth_time_sac(); | |
2596 | |
2597 // take the result | |
2598 gas_volume_need[sim_gas_current_num-1] = gas_needs_volume_due; | |
2599 } | |
2600 | |
2601 | |
623 | 2602 // the next calculation phase will calculate the NDL time |
628 | 2603 next_planning_phase = PHASE_50_NDL_TIME; |
604 | 2604 |
582 | 2605 break; |
2606 | |
623 | 2607 |
2608 // | |
2609 //---- NDL Time --------------------------------------------------------------------------- | |
2610 // | |
628 | 2611 case PHASE_50_NDL_TIME: |
623 | 2612 |
2613 // Calculate the remaining no decompression limit (NDL) time for the tissue NDL_tissue. | |
2614 // NDL_time will be updated if the NDL time found is shorter than the current NDL_time. | |
2615 // | |
2616 // In the initialization phase of the calculation cycle: | |
2617 // - NDL_time had been initialized to 240 (minutes), | |
2618 // - NDL_tissue had been initialized to the tissue with | |
2619 // the shortest NDL time in the last cycle. | |
2620 // | |
2621 calc_NDL_time_tissue(); | |
2622 | |
2623 // advance to next tissue, wrapping around after last tissue | |
2624 NDL_tissue = (NDL_tissue + 1) & (NUM_COMP - 1); | |
2625 | |
2626 // did we run out of NDL time or did we have probed all tissues? | |
2627 if( (NDL_time == 0) || (NDL_tissue == NDL_tissue_start) ) | |
2628 { | |
2629 // YES | |
2630 | |
2631 // set the tissue with the shortest NDL time found as | |
2632 // the one to start with in the next calculation cycle | |
2633 if( deco_status & CALC_NORM ) NDL_tissue_start_norm = NDL_tissue_lead; | |
2634 else NDL_tissue_start_alt = NDL_tissue_lead; | |
2635 | |
2636 // done with calculating NDL time, set next calculation phase: | |
2637 // - calculate return and ascent in cave mode if configured, else | |
628 | 2638 // - proceed with no-stop ascent if within NDL time, or |
2639 // - proceed with deco ascent if beyond NDL time. | |
623 | 2640 #ifdef _cave_mode |
628 | 2641 if ( main_status & CAVE_MODE ) next_planning_phase = PHASE_60_CAVE_RETURN; |
623 | 2642 else |
2643 #endif | |
628 | 2644 next_planning_phase = PHASE_70_OPEN_WATER_ASCENT; |
623 | 2645 } |
2646 | |
2647 break; | |
2648 | |
2649 | |
2650 #ifdef _cave_mode | |
2651 // | |
628 | 2652 //---- Cave Mode Return ------------------------------------------------------------------- |
560 | 2653 // |
628 | 2654 case PHASE_60_CAVE_RETURN: |
623 | 2655 |
2656 // TODO | |
2657 | |
2658 // the next calculation phase will gather all results | |
628 | 2659 next_planning_phase = PHASE_80_RESULTS; |
623 | 2660 |
2661 break; | |
2662 #endif | |
2663 | |
2664 | |
560 | 2665 // |
628 | 2666 //---- Open Water Ascent ------------------------------------------------------------------ |
560 | 2667 // |
628 | 2668 case PHASE_70_OPEN_WATER_ASCENT: |
623 | 2669 |
2670 // program 1 minute interval on simulated tissues | |
2671 tissue_increment = 1; | |
2672 | |
628 | 2673 // memorize current gas in case there will be a gas change |
2674 sim_gas_last_num = sim_gas_current_num; | |
2675 | |
2676 // find_next_stop(): | |
2677 // stays at the current stop depth, ascents to the next stop depth, or | |
2678 // ascents to the depth that is reachable within one minute of ascent | |
2679 // without needing to stop. | |
2680 // | |
2681 // return value : 1/true if a stop is required, else 0/false | |
2682 // char_depth_sim : current depth (depth achieved) | |
2683 // char_depth_last : last depth (depth we came from) | |
2684 // | |
623 | 2685 if( find_next_stop() ) |
560 | 2686 { |
628 | 2687 overlay unsigned char silent_stop = 0; |
2688 overlay unsigned char gas_change = 0; | |
2689 | |
623 | 2690 //---- stop required -------------------- |
2691 | |
2692 // check if there is a better gas to switch to | |
628 | 2693 if( gas_find_best() ) |
623 | 2694 { |
628 | 2695 // YES - memorize it |
2696 gas_change = 1; | |
2697 | |
2698 // take the gas | |
2699 gas_take_best(); | |
2700 | |
623 | 2701 // set the new calculation ratios for N2, He and O2 |
2702 gas_set_ratios(); | |
2703 | |
628 | 2704 // add the gas change time to the stop time |
2705 tissue_increment += char_I_gas_change_time; | |
2706 | |
2707 // extended stops option enabled and | |
2708 // gas change depth deeper than the current depth ? | |
2709 if( main_status & EXTENDED_STOPS ) | |
2710 if( sim_gas_current_depth > char_depth_sim ) | |
623 | 2711 { |
628 | 2712 // YES - make a "silent" stop at the gas change depth |
2713 // to figure in the gas change | |
2714 silent_stop = 1; | |
2715 | |
2716 // locate the stop at the shallower one of the | |
2717 // gas change depth or the depth we came from | |
2718 char_depth_sim = (sim_gas_current_depth < char_depth_last) ? | |
2719 sim_gas_current_depth : char_depth_last; | |
2720 | |
2721 // calculate sim_pres_respiration for | |
2722 // the adjusted value of char_depth_sim | |
2723 calc_sim_pres_respiration(); | |
2724 | |
2725 // as we didn't travel the full distance, | |
2726 // account for the gas change time only | |
2727 tissue_increment = char_I_gas_change_time; | |
2728 | |
2729 // if run from the deco calculator, | |
2730 // put the gas change into the stops table or | |
2731 // abort deco calculation if the table is full | |
2732 if( deco_status & DECO_CALCULATOR_MODE ) | |
2733 if( !update_deco_table(tissue_increment) ) | |
2734 next_planning_phase = PHASE_80_RESULTS; | |
623 | 2735 } |
628 | 2736 } // better gas |
2737 | |
2738 | |
2739 // if the stop is not a silent one, | |
2740 // add the stop to an existing stop or add a new stop, | |
2741 // or abort deco calculation if the deco table is full | |
2742 if( !silent_stop ) | |
2743 if( !update_deco_table(tissue_increment) ) | |
2744 next_planning_phase = PHASE_80_RESULTS; | |
2745 | |
2746 | |
2747 // shall calculate gas needs? | |
2748 if( main_status & CALC_VOLUME ) | |
2749 { | |
2750 // encountered a stop, so switch to deco usage rate (SAC deco) | |
2751 gas_needs_usage_rate = char_I_SAC_deco; | |
2752 | |
2753 // set the depth for gas need calculation to the shallower one | |
2754 // of the start depth (current real depth) and the stop depth | |
2755 // (assumed depth to be) as we may be shallower than we should be | |
2756 gas_needs_depth = ( char_depth_sim_start < char_depth_sim ) ? | |
2757 char_depth_sim_start : char_depth_sim; | |
2758 | |
2759 // did a gas change occur and last gas is 1-5 and a gas change time set? | |
2760 if( gas_change && sim_gas_last_num && char_I_gas_change_time ) | |
2761 { | |
2762 // YES - set time it takes for switching the gas | |
2763 gas_needs_time = char_I_gas_change_time; | |
2764 | |
2765 // calculate gas demand | |
2766 calc_due_by_depth_time_sac(); | |
2767 | |
2768 // add the demand to the overall demand on the last gas | |
2769 gas_volume_need[sim_gas_last_num-1] += gas_needs_volume_due; | |
2770 } | |
2771 | |
2772 // current gas is 1-5 ? | |
2773 if( sim_gas_current_num ) | |
2774 { | |
2775 // YES - time is 1 minute plus the gas change time (if set) | |
2776 gas_needs_time = tissue_increment; | |
2777 | |
2778 // calculate gas demand | |
2779 calc_due_by_depth_time_sac(); | |
2780 | |
2781 // add the demand to the overall demand on the current gas | |
2782 gas_volume_need[sim_gas_current_num-1] += gas_needs_volume_due; | |
2783 } | |
2784 } // gas need | |
560 | 2785 } |
2786 else | |
2787 { | |
623 | 2788 //---- no stop required ----------------- |
2789 | |
628 | 2790 // switch to a better gas, but only: |
623 | 2791 // |
628 | 2792 // if extended stops are activated OR if in bailout OR if within NDL |
2793 // AND if the actual depth is below (deeper) or at the change depth of the | |
2794 // better gas (switch depth has not been passed yet) | |
2795 // AND if the depth of the last stop is above (shallower) or at the change | |
2796 // depth of the better gas (do not switch on final ascent) | |
623 | 2797 // |
628 | 2798 // Attention: do not use a && formula over all 'if' terms, the |
2799 // conditions need to be evaluated in the given order! | |
623 | 2800 // |
628 | 2801 if( (main_status & EXTENDED_STOPS) || (deco_status & BAILOUT_MODE) || NDL_time ) |
2802 if( gas_find_best() ) | |
2803 if( char_depth_real >= sim_gas_best_depth ) | |
2804 if( char_I_depth_last_deco <= sim_gas_best_depth ) | |
560 | 2805 { |
628 | 2806 // YES - take the gas |
2807 gas_take_best(); | |
2808 | |
623 | 2809 // set the new calculation values for N2, He and O2 |
2810 gas_set_ratios(); | |
2811 | |
628 | 2812 // set char_depth_sim to the gas change depth |
2813 char_depth_sim = sim_gas_current_depth; | |
2814 | |
2815 // calculate sim_pres_respiration for | |
2816 // the adjusted value of char_depth_sim | |
2817 calc_sim_pres_respiration(); | |
2818 | |
2819 // as we didn't travel the full distance, | |
2820 // account for the gas change time only | |
2821 tissue_increment = char_I_gas_change_time; | |
2822 | |
2823 // if in deco and | |
2824 // if run from the deco calculator: | |
2825 // create a stop for the gas change in the stops table, | |
2826 // abort deco calculation if the deco table is full | |
2827 if( !NDL_time ) | |
2828 if( deco_status & DECO_CALCULATOR_MODE ) | |
2829 if( !update_deco_table(tissue_increment) ) | |
2830 next_planning_phase = PHASE_80_RESULTS; | |
2831 | |
2832 // shall calculate gas needs and gas change time is set? | |
2833 if( main_status & CALC_VOLUME ) | |
2834 if( char_I_gas_change_time ) | |
2835 { | |
2836 // YES - set depth to current depth | |
2837 gas_needs_depth = char_depth_sim; | |
2838 | |
2839 // set time it takes for switching the gas | |
2840 gas_needs_time = tissue_increment; | |
2841 | |
2842 // calculate gas demand | |
2843 calc_due_by_depth_time_sac(); | |
2844 | |
2845 // add gas demand to the overall demand on the new gas | |
2846 gas_volume_need[sim_gas_current_num-1] += gas_needs_volume_due; | |
2847 | |
2848 // was the last gas one of the gases 1-5 ? | |
2849 if( sim_gas_last_num ) | |
2850 { | |
2851 // YES - add the same demand to the overall demand on the last gas | |
2852 gas_volume_need[sim_gas_last_num-1] += gas_needs_volume_due; | |
2853 } | |
2854 } // gas switching needs | |
2855 } // gas switch | |
2856 | |
2857 // shall calculate gas needs and | |
2858 // last (or still current) gas is 1-5 ? | |
2859 if( main_status & CALC_VOLUME ) | |
2860 if( sim_gas_last_num ) | |
2861 { | |
2862 // YES - compute distance traveled | |
2863 gas_needs_depth = char_depth_last - char_depth_sim; | |
2864 | |
2865 // at least some positive distance traveled? | |
2866 if( gas_needs_depth > 1 ) | |
2867 { | |
2868 // YES - set depth to average depth along the distance | |
2869 gas_needs_depth += 1; | |
2870 gas_needs_depth /= 2; | |
2871 gas_needs_depth += char_depth_sim; | |
2872 | |
2873 // ascent time is 1 minute | |
2874 gas_needs_time = 1; | |
2875 | |
2876 // calculate gas demand | |
2877 calc_due_by_depth_time_sac(); | |
2878 | |
2879 // add to overall demand | |
2880 gas_volume_need[sim_gas_last_num-1] += gas_needs_volume_due; | |
2881 } | |
2882 } // gas travel needs | |
2883 | |
2884 } // stop / no stop | |
2885 | |
2886 // --- one or more minutes have passed by now --- | |
2887 | |
2888 // update the ascent time | |
2889 ascent_time += tissue_increment; | |
582 | 2890 |
604 | 2891 // compute current ppO2, ppN2 and ppHe |
582 | 2892 calc_alveolar_pressures(); |
2893 | |
560 | 2894 // update the tissues |
582 | 2895 calc_tissues(); |
604 | 2896 |
2897 // update the CNS value | |
2898 calc_CNS(); | |
623 | 2899 |
2900 // finish stops calculation if the surface is reached | |
628 | 2901 if( char_depth_sim == 0 ) next_planning_phase = PHASE_80_RESULTS; |
623 | 2902 |
2903 break; | |
2904 | |
2905 | |
2906 /// | |
2907 //--- Results - Initialization ------------------------------------------------------------ | |
2908 // | |
628 | 2909 case PHASE_80_RESULTS: |
2910 | |
2911 // convert the CNS value to integer | |
2912 convert_sim_CNS_for_display(); | |
2913 | |
2914 if( deco_status & CALC_NORM ) | |
2915 { | |
2916 // export the integer CNS value | |
2917 int_O_CNS_norm = int_sim_CNS_fraction; | |
2918 } | |
2919 else | |
2920 { | |
2921 // export the integer CNS value | |
2922 int_O_CNS_alt = int_sim_CNS_fraction; | |
2923 } | |
623 | 2924 |
2925 // The next calculation phase will | |
2926 // - publish the stops table if in normal plan mode, | |
2927 // - proceed with remaining results dependent on if within NDL, or | |
2928 // - in deco | |
628 | 2929 if ( deco_status & CALC_NORM ) next_planning_phase = PHASE_81_RESULTS_STOPS_TABLE; |
2930 else if ( NDL_time ) next_planning_phase = PHASE_82_RESULTS_NDL; | |
2931 else next_planning_phase = PHASE_83_RESULTS_DECO; | |
623 | 2932 |
2933 break; | |
2934 | |
2935 | |
2936 /// | |
2937 //--- Publish Stops Table ----------------------------------------------------------------- | |
2938 // | |
628 | 2939 case PHASE_81_RESULTS_STOPS_TABLE: |
623 | 2940 |
2941 // publish the stops table to the display functions | |
2942 publish_deco_table(); | |
2943 | |
2944 // When entering deco and the ceiling depth becomes > 0 but the | |
2945 // deco calculation reveals no distinct deco stop yet because | |
2946 // the deco obligation will vanish during the ascent, create an | |
2947 // artificial stop to signal that expedite surfacing ("popping | |
2948 // up") is not allowed anymore. | |
2949 if( char_O_deco_depth[0] == 0 ) // simulated ascent reveals no required stops | |
2950 if( int_O_ceiling > 0 ) // real tissues have a ceiling | |
2951 { | |
2952 // set a pro forma stop at the configured last stop depth | |
2953 char_O_deco_depth[0] = char_I_depth_last_deco; | |
2954 | |
2955 // set a stop time of 0 minutes, this will be displayed as "..'" | |
2956 char_O_deco_time[0] = 0; | |
2957 } | |
2958 | |
2959 // update deco info vector | |
628 | 2960 if( char_O_deco_depth[0] ) deco_info |= DECO_STOPS; // set flag for deco stops found |
623 | 2961 else deco_info &= ~DECO_STOPS; // clear flag for deco stops found |
2962 | |
2963 // The next calculation phase will publish the main results dependent on being | |
2964 // - within NDL, | |
2965 // - in deco. | |
628 | 2966 if ( NDL_time ) next_planning_phase = PHASE_82_RESULTS_NDL; |
2967 else next_planning_phase = PHASE_83_RESULTS_DECO; | |
623 | 2968 |
2969 break; | |
2970 | |
2971 | |
2972 /// | |
2973 //--- Results - within NDL ---------------------------------------------------------------- | |
2974 // | |
628 | 2975 case PHASE_82_RESULTS_NDL: |
623 | 2976 |
2977 // results to publish depend on normal or alternative plan | |
2978 if( deco_status & CALC_NORM ) | |
2979 { | |
2980 // output the NDL time | |
2981 char_O_NDL_norm = NDL_time; | |
2982 | |
2983 // clear the normal ascent time | |
2984 int_O_TTS_norm = 0; | |
2985 } | |
2986 else | |
2987 { | |
2988 // output the NDL time | |
2989 char_O_NDL_alt = NDL_time; | |
2990 | |
2991 // clear the alternative ascent time | |
2992 int_O_TTS_alt = 0; | |
2993 } | |
2994 | |
2995 // The next calculation phase will | |
2996 // - finish the calculation cycle if no gas needs calculation configured, else | |
628 | 2997 // - calculate the gas needs pressures |
623 | 2998 if ( !(main_status & CALC_VOLUME ) ) next_planning_phase = PHASE_90_FINISH; |
628 | 2999 else next_planning_phase = PHASE_84_GAS_NEEDS_PRESSURES; |
623 | 3000 |
3001 break; | |
3002 | |
3003 | |
3004 /// | |
3005 //--- Results - in Deco ------------------------------------------------------------------- | |
3006 // | |
628 | 3007 case PHASE_83_RESULTS_DECO: |
3008 | |
3009 // limit ascent time to display max. | |
3010 if( ascent_time > 999) ascent_time = 999; | |
3011 | |
3012 // tag ascent time as invalid if there is an overflow in the stops table | |
3013 if( deco_warnings & DECO_WARNING_INCOMPLETE ) ascent_time |= INT_FLAG_INVALID; | |
623 | 3014 |
3015 // results to publish depend on normal or alternative plan | |
3016 if( deco_status & CALC_NORM ) | |
3017 { | |
3018 // clear the normal NDL time | |
3019 char_O_NDL_norm = 0; | |
3020 | |
3021 // export the ascent time | |
3022 int_O_TTS_norm = ascent_time; | |
3023 } | |
3024 else | |
3025 { | |
3026 // clear the alternative NDL time | |
3027 char_O_NDL_alt = 0; | |
3028 | |
3029 // export the ascent time | |
3030 int_O_TTS_alt = ascent_time; | |
3031 } | |
3032 | |
3033 // The next calculation phase will | |
3034 // - finish the calculation cycle if no gas needs calculation configured, else | |
3035 // - calculate the gas needs along the ascent | |
3036 if ( !(main_status & CALC_VOLUME ) ) next_planning_phase = PHASE_90_FINISH; | |
628 | 3037 else next_planning_phase = PHASE_84_GAS_NEEDS_PRESSURES; |
623 | 3038 |
3039 break; | |
3040 | |
3041 | |
3042 // | |
628 | 3043 //--- Results - convert Gas Needs Volumes to Pressures ------------------------------------ |
623 | 3044 // |
628 | 3045 case PHASE_84_GAS_NEEDS_PRESSURES: |
623 | 3046 |
3047 // convert required volume of the gas pointed to by gas_needs_gas_index | |
3048 // into the respective pressure and set the flags | |
3049 convert_gas_needs_to_press(); | |
3050 | |
3051 // increment index to address next gas | |
3052 gas_needs_gas_index++; | |
3053 | |
3054 // if all gases have been converted, advance to next calculation phase | |
3055 if( gas_needs_gas_index == NUM_GAS ) next_planning_phase = PHASE_90_FINISH; | |
3056 | |
3057 break; | |
3058 | |
3059 | |
3060 // | |
3061 //--- finish Calculation Cycle ------------------------------------------------------------ | |
3062 // | |
3063 case PHASE_90_FINISH: | |
3064 | |
3065 // Check if deco obligation is steady state or decreasing. | |
3066 // This works only when an alternative plan is enabled and if it is not a bailout plan, | |
3067 // thus BAILOUT_MODE must not be set while doing the alternative plan. | |
3068 if( (deco_status & CALC_ALT) && !(deco_status & BAILOUT_MODE) ) | |
3069 { | |
628 | 3070 if( int_O_TTS_alt < int_O_TTS_norm ) deco_info |= DECO_ZONE; |
3071 if( int_O_TTS_alt > int_O_TTS_norm ) deco_info &= ~DECO_ZONE; | |
623 | 3072 } |
3073 | |
3074 // export updated deco infos and warnings | |
3075 char_O_deco_info = deco_info; | |
3076 char_O_deco_warnings = deco_warnings; | |
3077 | |
3078 // restore command flag to indicate that deco calculation cycle has finished | |
3079 char_O_deco_status = deco_status; | |
3080 | |
3081 // signal end of deco calculation | |
3082 next_planning_phase = PHASE_00_DONE; | |
3083 | |
3084 break; | |
3085 | |
3086 } // switch | |
3087 | |
3088 // read timer 5, result will be stored in tmr5_value (in 1/32 ms) and tmr5_overflow | |
3089 read_tmr5(); | |
3090 | |
3091 } // sequence calculation phases while not timed out and calculation cycle is not finished | |
3092 while( (tmr5_overflow == 0) && ( next_planning_phase != PHASE_00_DONE ) ); | |
3093 | |
3094 // report where we are in terms of depth reached, used in deco calculator to show deco calculation progress | |
3095 char_O_depth_sim = char_depth_sim; | |
3096 | |
3097 | |
3098 #ifdef _profiling | |
3099 | |
3100 //---- Performance Measurement ------------------------------------------- | |
3101 | |
3102 // convert timer 5 readout into ms | |
3103 profiling_runtime = tmr5_value / 32; | |
3104 | |
3105 // actual runtime longer than target runtime? | |
3106 if( tmr5_overflow ) | |
3107 { | |
3108 // YES - report excess | |
3109 int_O_profiling_overrun = profiling_runtime; | |
3110 | |
3111 // - excess > max we had so far? | |
3112 if( int_O_profiling_overrun > int_O_profiling_overrun_max ) | |
3113 { | |
3114 // YES - update max | |
3115 int_O_profiling_overrun_max = int_O_profiling_overrun; | |
3116 | |
3117 // - store the causing phase | |
3118 char_O_profiling_overrun_phase = profiling_phase; | |
3119 } | |
560 | 3120 } |
623 | 3121 else |
3122 { | |
3123 // NO - calculate unused budget and flag it to be under-run time | |
3124 int_O_profiling_overrun = (2048 - profiling_runtime) | 0x8000; | |
3125 } | |
3126 | |
3127 // increment number of runs in current cycle | |
3128 profiling_runs += 1; | |
3129 | |
3130 // planning cycle completed? | |
3131 if( next_planning_phase == PHASE_00_DONE ) | |
3132 { | |
3133 // YES - export number of runs it took | |
3134 if( deco_status & COMPLETED_NORM ) char_O_profiling_runs_norm = profiling_runs; | |
3135 else char_O_profiling_runs_alt = profiling_runs; | |
3136 } | |
3137 | |
3138 #endif | |
3139 | |
3140 } | |
3141 | |
3142 | |
3143 ////////////////////////////////////////////////////////////////////////////// | |
3144 // calc_hauptroutine_data_input | |
3145 // | |
3146 // Set all C-code dive parameters from their ASM-code values. | |
3147 // | |
3148 void calc_hauptroutine_data_input(void) | |
3149 { | |
3150 overlay float IG_ratio; | |
3151 | |
3152 // safeguard and convert the surface pressure (mbar -> bar) (*) | |
3153 if( int_I_pres_surface < 500 ) pres_surface = 0.500; | |
3154 else pres_surface = 0.001 * int_I_pres_surface; | |
3155 | |
3156 // safeguard and convert the current real pressure | |
3157 if( int_I_pres_respiration < 500 ) real_pres_respiration = 0.500; | |
3158 else real_pres_respiration = 0.001 * int_I_pres_respiration; | |
3159 | |
3160 // safeguard further parameters to protect the tissue-flag and the stop table | |
3161 if( char_I_sim_advance_time > 127 ) char_I_sim_advance_time = 127; | |
3162 if( char_I_extra_time > 127 ) char_I_extra_time = 127; | |
3163 if( char_I_gas_change_time > 99 ) char_I_gas_change_time = 99; | |
3164 | |
628 | 3165 |
3166 // compute the depth in meters where we are now | |
3167 float_depth_real = (real_pres_respiration - pres_surface) * BAR_TO_METER; | |
3168 | |
3169 // convert to integer and round up to next full meter | |
3170 char_depth_real = (unsigned char)(float_depth_real + 0.99); | |
3171 | |
3172 | |
623 | 3173 // calculate partial pressure of N2 in respired air at surface pressure |
3174 calc_N2_equilibrium(); | |
3175 | |
3176 // get, safeguard and convert the saturation and desaturation factors | |
3177 get_saturation_factors(); | |
3178 | |
3179 #ifdef _ccr_pscr | |
3180 // compute a factor that will be used later on for pSCR ppO2 drop calculation (*) | |
3181 float_pSCR_factor = 0.01 * char_I_PSCR_drop * char_I_PSCR_lungratio; | |
3182 #endif | |
3183 | |
3184 #ifdef _helium | |
3185 // get the currently breathed gas ratios | |
3186 real_O2_ratio = 0.01 * char_I_O2_ratio; | |
3187 real_He_ratio = 0.01 * char_I_He_ratio; | |
3188 | |
3189 // calculate the inert gas ratio (local helper variable) | |
3190 IG_ratio = 1.00 - real_O2_ratio; | |
3191 | |
3192 // calculate the N2 ratio | |
3193 real_N2_ratio = IG_ratio - real_He_ratio; | |
3194 #else | |
3195 // get the currently breathed O2 ratio | |
3196 real_O2_ratio = 0.01 * char_I_O2_ratio; | |
3197 | |
3198 // set the He ratio to zero | |
3199 real_He_ratio = 0.0; | |
3200 | |
3201 // calculate the N2 / inert gas ratio | |
3202 real_N2_ratio = IG_ratio = 1.00 - real_O2_ratio; | |
3203 #endif // _helium | |
3204 | |
3205 #ifdef _ccr_pscr | |
3206 // calculate ppO2 drop in pSCR loop for real tissues | |
3207 real_pSCR_drop = IG_ratio * float_pSCR_factor; | |
3208 #endif | |
3209 | |
0 | 3210 } |
3211 | |
3212 | |
3213 ////////////////////////////////////////////////////////////////////////////// | |
582 | 3214 // calc_tissues |
0 | 3215 // |
623 | 3216 // INPUT: ppN2 partial pressure of inspired N2 |
3217 // ppHe partial pressure of inspired He | |
3218 // tissue_increment integration time and tissue selector (real or simulated) | |
3219 // | |
3220 // MODIFIED: real_pres_tissue_N2[] tissue N2 pressures (in real tissues context) | |
3221 // real_pres_tissue_He[] tissue He pressures (in real tissues context) | |
3222 // sim_pres_tissue_N2[] tissue N2 pressures (in simulated tissues context) | |
3223 // sim_pres_tissue_He[] tissue He pressures (in simulated tissues context) | |
3224 // | |
3225 // OUTPUT: char_O_tissue_pres_N2[] tissue N2 pressures scaled for display purpose (in real tissues context) | |
3226 // char_O_tissue_pres_He[] tissue He pressures scaled for display purpose (in real tissues context) | |
3227 // char_O_tissue_pressure[] combined tissue pressures scaled for display purpose (in real tissue context) | |
582 | 3228 // |
3229 static void calc_tissues() | |
0 | 3230 { |
623 | 3231 overlay unsigned char period; |
560 | 3232 overlay float temp_tissue_N2; |
623 | 3233 |
3234 #ifdef _helium | |
560 | 3235 overlay float temp_tissue_He; |
623 | 3236 #endif |
582 | 3237 |
3238 | |
3239 assert( 0.00 <= ppN2 && ppN2 < 11.2 ); // 80% N2 at 130m | |
3240 assert( 0.00 <= ppHe && ppHe < 12.6 ); // 90% He at 130m | |
3241 | |
3242 | |
604 | 3243 for( ci=0; ci < NUM_COMP; ci++ ) // iterate through all compartments |
582 | 3244 { |
3245 i = tissue_increment & TIME_MASK; // extract number of minutes to do (if i > 0) | |
3246 // or if one 2 second period is to do (if i = 0) | |
3247 | |
3248 if( i == 0 ) // check if we shall do one 2-seconds period | |
560 | 3249 { |
623 | 3250 read_Buhlmann_times(0); // YES - program coefficients for a 2 seconds period |
3251 period = 1; // - set period length (in cycles) | |
3252 i = 1; // - and one cycle to do | |
560 | 3253 } |
582 | 3254 else if( i > 9 ) // check if we can start with 10 minutes periods |
560 | 3255 { |
623 | 3256 read_Buhlmann_times(2); // YES - program coefficients for 10 minutes periods |
3257 period = 10; // set period length (in cycles) to ten | |
560 | 3258 } |
623 | 3259 else // last but not lease, do 1 to 9 minutes |
560 | 3260 { |
623 | 3261 read_Buhlmann_times(1); // NO - program coefficients for 1 minute periods |
3262 period = 1; // - set period length (in cycles) to one | |
560 | 3263 } |
3264 | |
3265 do | |
3266 { | |
623 | 3267 //---- N2 -------------------------------------------------------- |
3268 | |
3269 temp_tissue = (tissue_increment & TISSUE_SELECTOR) ? real_pres_tissue_N2[ci] : sim_pres_tissue_N2[ci]; | |
560 | 3270 |
3271 temp_tissue = (ppN2 - temp_tissue) * var_N2_e; | |
3272 | |
623 | 3273 apply_saturation_factors(); |
3274 | |
3275 if( tissue_increment & TISSUE_SELECTOR ) | |
560 | 3276 { |
623 | 3277 temp_tissue_N2 = temp_tissue; |
3278 real_pres_tissue_N2[ci] += temp_tissue; | |
560 | 3279 } |
3280 else | |
3281 { | |
623 | 3282 sim_pres_tissue_N2[ci] += temp_tissue; |
560 | 3283 } |
3284 | |
623 | 3285 #ifdef _helium |
3286 //---- He -------------------------------------------------------- | |
3287 | |
3288 temp_tissue = (tissue_increment & TISSUE_SELECTOR) ? real_pres_tissue_He[ci] : sim_pres_tissue_He[ci]; | |
560 | 3289 |
3290 temp_tissue = (ppHe - temp_tissue) * var_He_e; | |
3291 | |
623 | 3292 apply_saturation_factors(); |
3293 | |
3294 if( tissue_increment & TISSUE_SELECTOR ) | |
560 | 3295 { |
623 | 3296 temp_tissue_He = temp_tissue; |
3297 real_pres_tissue_He[ci] += temp_tissue; | |
560 | 3298 } |
3299 else | |
3300 { | |
623 | 3301 sim_pres_tissue_He[ci] += temp_tissue; |
560 | 3302 } |
623 | 3303 #endif |
3304 | |
3305 //---- decrement loop counter and adjust step size --------------- | |
560 | 3306 |
3307 // decrement loop counter | |
3308 i -= period; | |
582 | 3309 |
560 | 3310 // check if we need to switch from 10 minute periods to 1 minute periods |
3311 if( (i > 0) && (period = 10) && (i < 10) ) | |
3312 { | |
582 | 3313 read_Buhlmann_times(1); // program coefficients for 1 minute periods |
560 | 3314 period = 1; // set period length (in cycles) to one |
3315 } | |
3316 } | |
3317 while( i ); | |
582 | 3318 |
3319 | |
560 | 3320 // have the computations been done for the "real" tissues? |
623 | 3321 if( tissue_increment & TISSUE_SELECTOR ) |
560 | 3322 { |
623 | 3323 |
3324 #ifdef _helium | |
3325 | |
560 | 3326 // net tissue balance |
3327 temp_tissue = temp_tissue_N2 + temp_tissue_He; | |
582 | 3328 |
623 | 3329 |
560 | 3330 // check tissue on-/off-gassing and IBCD with applying a threshold of +/-HYST |
3331 // | |
584 | 3332 if ( temp_tissue < -HYST ) // check if the tissue is off-gassing |
560 | 3333 { |
623 | 3334 // tag tissue as not experiencing mentionable IBCD |
3335 IBCD_tissue_vector &= ~(1 << ci); | |
560 | 3336 } |
3337 else if ( temp_tissue > +HYST ) // check if the tissue in on-gassing | |
3338 { | |
623 | 3339 // check for counter diffusion |
3340 if( ((temp_tissue_N2 > 0.0) && (temp_tissue_He < 0.0)) | |
604 | 3341 || ((temp_tissue_N2 < 0.0) && (temp_tissue_He > 0.0)) ) |
560 | 3342 { |
623 | 3343 // tag tissue as experiencing mentionable IBCD |
3344 IBCD_tissue_vector |= (1 << ci); | |
560 | 3345 } |
3346 } | |
3347 | |
623 | 3348 #endif |
3349 | |
3350 // For N2 tissue pressure display purpose: | |
3351 | |
3352 // basically keep the on-gassing / off-gassing flag from last invocation, but flip | |
3353 // it in case the rate exceeds a set hysteresis (actual value: see #define of HYST) | |
3354 char_O_tissue_pres_N2[ci] &= 128; | |
3355 if ( temp_tissue_N2 > +HYST ) char_O_tissue_pres_N2[ci] = 128; // set flag for tissue pressure is increasing | |
3356 else if ( temp_tissue_N2 < -HYST ) char_O_tissue_pres_N2[ci] = 0; // clear flag (-> tissue pressure is decreasing) | |
3357 | |
3358 // scale N2 tissue pressure such that the surface steady-state tissue loading | |
3359 // of [0.7902 * (1013 hPa - ppWater)] bar will give a 8, which aligns with | |
3360 // the 2nd scale line. | |
3361 temp_tissue_N2 = (8 / (0.7902 * (1.013 - ppWater))) * real_pres_tissue_N2[ci]; | |
3362 | |
3363 // limit to 127 to protect the uppermost bit which holds the sat/desat flag | |
3364 if (temp_tissue_N2 > 127) temp_tissue_N2 = 127; | |
3365 | |
3366 // convert to integer and combine with sat/desat flag | |
3367 char_O_tissue_pres_N2[ci] += (unsigned char)temp_tissue_N2; | |
3368 | |
3369 #ifdef _helium | |
3370 | |
3371 // For He tissue pressure display purpose: | |
3372 | |
3373 // basically keep the on-gassing / off-gassing flag from last invocation, but flip | |
3374 // it in case the rate exceeds a set hysteresis (actual value: see #define of HYST) | |
3375 char_O_tissue_pres_He[ci] &= 128; | |
3376 if ( temp_tissue_He > +HYST ) char_O_tissue_pres_He[ci] = 128; // set flag for tissue pressure is increasing | |
3377 else if ( temp_tissue_He < -HYST ) char_O_tissue_pres_He[ci] = 0; // clear flag (-> tissue pressure is decreasing) | |
3378 | |
3379 // scale He tissue pressure alike it is done for N2. | |
3380 // With no He in a tissue, the result will be 0. | |
3381 temp_tissue_He = (8 / (0.7902 * (1.013 - ppWater))) * real_pres_tissue_He[ci]; | |
3382 | |
3383 // limit to 127 to protect the uppermost bit which holds the sat/desat flag | |
3384 if (temp_tissue_He > 127) temp_tissue_He = 127; | |
3385 | |
3386 // convert to integer and combine with sat/desat flag | |
3387 char_O_tissue_pres_He[ci] += (unsigned char)temp_tissue_He; | |
3388 | |
3389 | |
3390 // For combined tissue pressure display purpose: | |
3391 | |
3392 // basically keep the on-gassing / off-gassing flag from last invocation, but flip | |
3393 // it in case the rate exceeds a set hysteresis (actual value: see #define of HYST) | |
3394 char_O_tissue_pressure[ci] &= 128; | |
3395 if ( temp_tissue > +HYST ) char_O_tissue_pressure[ci] = 128; // set flag for tissue pressure is increasing | |
3396 else if ( temp_tissue < -HYST ) char_O_tissue_pressure[ci] = 0; // clear flag (-> tissue pressure is decreasing) | |
3397 | |
3398 // add the two scaled pressures. | |
3399 temp_tissue = temp_tissue_N2 + temp_tissue_He; | |
3400 | |
3401 // limit to 127 to protect the uppermost bit which holds the sat/desat flag | |
560 | 3402 if (temp_tissue > 127) temp_tissue = 127; |
3403 | |
623 | 3404 // convert to integer and combine with sat/desat flag |
3405 char_O_tissue_pressure[ci] += (unsigned char)temp_tissue; | |
3406 | |
3407 #else | |
3408 | |
3409 // He tissue pressure is zero | |
3410 char_O_tissue_pres_He[ci] = 0; | |
3411 | |
3412 // combined tissue pressure equals N2 tissue pressure | |
3413 char_O_tissue_pressure[ci] = char_O_tissue_pres_N2[ci]; | |
3414 | |
3415 #endif | |
3416 | |
582 | 3417 } //if |
3418 | |
3419 } // for | |
0 | 3420 } |
3421 | |
623 | 3422 |
0 | 3423 ////////////////////////////////////////////////////////////////////////////// |
3424 // calc_limit | |
3425 // | |
623 | 3426 // Input: GF_parameter gradient factor to be used, negative values activate surface mode |
3427 // tissue_increment selector for context: real or simulated tissues | |
3428 // sim_pres_tissue_N2/_He tissue pressures (used in simulated tissues context) | |
3429 // real_pres_tissue_N2/_He tissue pressures (used in real tissues context) | |
3430 // | |
3431 // Output: lead_supersat highest supersaturation found among all tissues, 1.0 = 100% | |
3432 // lead_tissue number of the leading tissue (0-15) | |
3433 // ceiling ceiling in bar relative pressure | |
3434 // | |
3435 // Modified: deco_warnings for IBCD, micro bubbles and outside warning (only in real tissues context) | |
582 | 3436 // |
3437 static void calc_limit(PARAMETER float GF_parameter) | |
0 | 3438 { |
623 | 3439 overlay float pres_respiration_min_total = 0.0; |
3440 overlay unsigned char surface_mode = 0; // 0: off, 1: on | |
3441 | |
3442 | |
3443 // check mode | |
3444 if( GF_parameter < 0 ) | |
3445 { | |
3446 // activate surface mode | |
3447 surface_mode = 1; | |
3448 | |
3449 // normalize parameter | |
3450 GF_parameter = -GF_parameter; | |
3451 } | |
3452 | |
3453 // set leading tissue number to tissue 1 (it has the index 0) | |
3454 lead_tissue = 0; | |
604 | 3455 |
3456 // initialize leading tissue supersaturation value to null | |
623 | 3457 lead_supersat = 0.0; |
3458 | |
3459 // next code section is relevant only when invoked on the real tissues | |
3460 if( tissue_increment & TISSUE_SELECTOR ) | |
582 | 3461 { |
623 | 3462 // clear IBCD, micro-bubbles and outside warning flags (locked warnings will be preserved) |
3463 deco_warnings &= ~( DECO_WARNING_IBCD + DECO_WARNING_MBUBBLES + DECO_WARNING_OUTSIDE + DECO_ATTENTION_OUTSIDE ); | |
582 | 3464 } |
3465 | |
3466 // loop over all tissues | |
604 | 3467 for( ci = 0; ci < NUM_COMP; ci++ ) |
582 | 3468 { |
623 | 3469 overlay float pres_respiration_min_tissue; |
3470 | |
3471 | |
3472 // get the coefficients for tissue ci | |
3473 read_Buhlmann_coefficients(); | |
3474 | |
3475 #ifdef _helium | |
582 | 3476 |
3477 // get the tissue pressures | |
623 | 3478 // adopt_Buhlmann_coefficients needs calc_pres_tissue_N2/He when compiled for helium |
3479 if( tissue_increment & TISSUE_SELECTOR ) | |
560 | 3480 { |
582 | 3481 // context is real tissues |
623 | 3482 calc_pres_tissue_N2 = real_pres_tissue_N2[ci]; |
3483 calc_pres_tissue_He = real_pres_tissue_He[ci]; | |
560 | 3484 } |
3485 else | |
3486 { | |
582 | 3487 // context is simulated tissues |
3488 calc_pres_tissue_N2 = sim_pres_tissue_N2[ci]; | |
3489 calc_pres_tissue_He = sim_pres_tissue_He[ci]; | |
3490 } | |
3491 | |
3492 // overall tissue pressure | |
3493 pres_tissue = calc_pres_tissue_N2 + calc_pres_tissue_He; | |
3494 | |
623 | 3495 #else |
3496 | |
3497 // get the tissue pressure | |
3498 pres_tissue = ( tissue_increment & TISSUE_SELECTOR ) ? real_pres_tissue_N2[ci] : sim_pres_tissue_N2[ci]; | |
3499 | |
3500 #endif | |
582 | 3501 |
604 | 3502 // adopt a and b coefficients to current N2/He ratio inside the tissue |
3503 adopt_Buhlmann_coefficients(); | |
582 | 3504 |
3505 // next calculations are only relevant when invoked on the real tissues | |
623 | 3506 if( tissue_increment & TISSUE_SELECTOR ) |
582 | 3507 { |
618 | 3508 overlay float pres_tissue_max; |
582 | 3509 overlay float supersat; |
608 | 3510 overlay float baseline_threshold; |
604 | 3511 |
623 | 3512 |
582 | 3513 // check if tissue is in supersaturation |
618 | 3514 if( pres_tissue > real_pres_respiration ) |
582 | 3515 { |
618 | 3516 // calculate maximum allowed tissue pressure at current ambient pressure |
623 | 3517 pres_tissue_max = real_pres_respiration / var_b + var_a; |
618 | 3518 |
3519 // calculate current supersaturation value (1.0 = 100%) of this tissue according to straight Buhlmann | |
3520 supersat = ( pres_tissue - real_pres_respiration ) | |
3521 / ( pres_tissue_max - real_pres_respiration ); | |
3522 | |
623 | 3523 // calculate supersaturation value for display purpose: 1.35 = 135% = 86 pixel |
3524 if( supersat <= 1.35 ) char_O_tissue_saturation[ci] = (unsigned char)(supersat * 64); | |
3525 else char_O_tissue_saturation[ci] = 86; | |
3526 | |
582 | 3527 // memorize highest supersaturation found |
3528 if( supersat > lead_supersat ) lead_supersat = supersat; | |
3529 | |
608 | 3530 // tissue-dependent baseline threshold for micro bubbles and outside warnings |
3531 baseline_threshold = 0.02 * ci + 1.0; | |
3532 | |
3533 // micro bubbles warning: supersaturation > baseline threshold | |
3534 if( supersat > baseline_threshold ) | |
623 | 3535 deco_warnings |= (DECO_WARNING_MBUBBLES + DECO_WARNING_MBUBBLES_lock); |
604 | 3536 |
608 | 3537 // outside warning: supersaturation > baseline threshold + additional 5% margin |
623 | 3538 if( supersat > (baseline_threshold + 0.05) ) |
3539 deco_warnings |= (DECO_WARNING_OUTSIDE + DECO_WARNING_OUTSIDE_lock ); | |
3540 } | |
3541 else | |
3542 { | |
3543 // supersaturation is defined as zero while tissue pressure <= ambient pressure | |
3544 supersat = 0.0; | |
3545 char_O_tissue_saturation[ci] = 0; | |
560 | 3546 } |
623 | 3547 |
3548 // next only when in surface mode | |
3549 if( surface_mode ) | |
3550 { | |
3551 // tag tissue whether it is beyond the M-line limit or not | |
3552 if( supersat > 1.0 ) | |
3553 { | |
3554 char_O_tissue_pres_N2[ci] |= 128; | |
3555 #ifdef _helium | |
3556 char_O_tissue_pres_He[ci] |= 128; | |
3557 #endif | |
3558 char_O_tissue_pressure[ci] |= 128; | |
3559 } | |
3560 else | |
3561 { | |
3562 char_O_tissue_pres_N2[ci] &= ~128; | |
3563 #ifdef _helium | |
3564 char_O_tissue_pres_He[ci] &= ~128; | |
3565 #endif | |
3566 char_O_tissue_pressure[ci] &= ~128; | |
3567 } | |
3568 } | |
3569 } // real tissues | |
582 | 3570 |
618 | 3571 // calculate the minimum ambient pressure that the tissue can withstand |
3572 if( char_I_deco_model == 0 ) | |
3573 { | |
3574 // straight Buhlmann | |
623 | 3575 pres_respiration_min_tissue = (pres_tissue - var_a) * var_b; |
618 | 3576 } |
3577 else | |
3578 { | |
3579 // Buhlmann with Eric Baker's varying gradient factor correction | |
3580 // note: this equation [1] is the inverse of equation [2] | |
623 | 3581 pres_respiration_min_tissue = ( pres_tissue - (var_a * GF_parameter) ) |
3582 / ( 1.0 - GF_parameter + (GF_parameter / var_b ) ); | |
618 | 3583 } |
560 | 3584 |
3585 // check if this tissue requires a higher ambient pressure than was found to be needed up to now | |
623 | 3586 if( pres_respiration_min_tissue > pres_respiration_min_total ) |
582 | 3587 { |
623 | 3588 pres_respiration_min_total = pres_respiration_min_tissue; |
3589 lead_tissue = ci; | |
582 | 3590 } |
3591 } // for | |
3592 | |
604 | 3593 // compute ceiling for the real tissues in bar relative pressure |
623 | 3594 ceiling = pres_respiration_min_total - pres_surface; |
3595 | |
3596 #ifdef _helium | |
3597 // IBCD is checked for real tissues only | |
3598 if( tissue_increment & TISSUE_SELECTOR ) | |
560 | 3599 { |
604 | 3600 // check if the leading tissue is in IBCD condition |
623 | 3601 if( (IBCD_tissue_vector & (1 << lead_tissue)) |
3602 && ((real_pres_tissue_N2[lead_tissue] + real_pres_tissue_He[lead_tissue]) > real_pres_respiration) ) | |
582 | 3603 { |
623 | 3604 // leading tissue is in IBCD condition and in super-saturation, so issue a warning. |
3605 deco_warnings |= (DECO_WARNING_IBCD + DECO_WARNING_IBCD_lock); | |
582 | 3606 } |
560 | 3607 } |
623 | 3608 #endif |
3609 | |
0 | 3610 } |
623 | 3611 |
3612 | |
0 | 3613 ////////////////////////////////////////////////////////////////////////////// |
623 | 3614 // calc_NDL_time_tissue |
3615 // | |
3616 // calculation of the remaining no decompression limit (NDL) time for a tissue | |
0 | 3617 // |
604 | 3618 // NOTE: Erik Baker's closed formula works for Nitrox. Trimix adds a second |
0 | 3619 // exponential term to the M-value equation, making it impossible to |
623 | 3620 // invert. So we have to solve the problem with a search approach. |
3621 // | |
3622 // Input: NDL_tissue tissue for which to calculate remaining NDL time | |
3623 // GF_high gradient factor used when GF factors are enabled | |
3624 // ppN2, ppHe partial pressures of N2 and He breathed | |
3625 // | |
3626 // Modified: NDL_time shortest NDL time found so far | |
3627 // NDL_tissue_lead leading tissue, i.e. tissue with the shortest NDL | |
3628 // | |
3629 static void calc_NDL_time_tissue(void) | |
0 | 3630 { |
623 | 3631 overlay unsigned char NDL_time_tissue = 0; // NDL time of this tissue, starting with 0 minutes |
3632 overlay unsigned char step_size = 10; // step size in searching, starting with 10 minutes | |
3633 overlay float pres_limit; // max. tissue pressure allowed | |
3634 | |
3635 #ifdef _helium | |
3636 overlay float last_pres_tissue_N2; // last tissue pressure for N2 | |
3637 overlay float last_pres_tissue_He; // last tissue pressure for He | |
3638 #else | |
3639 overlay float last_pres_tissue; // last tissue pressure | |
3640 #endif | |
3641 | |
3642 | |
3643 // set the compartment index ci for reading the Buhlmann increments and coefficients | |
3644 ci = NDL_tissue; | |
3645 | |
3646 // read the tissue increments for a step size of 10 minutes | |
3647 read_Buhlmann_times(2); | |
3648 | |
3649 // read Buhlmann a and b coefficients for tissue ci | |
3650 read_Buhlmann_coefficients(); | |
3651 | |
3652 #ifdef _helium | |
3653 | |
3654 // get the current simulated tissue pressures | |
3655 calc_pres_tissue_N2 = last_pres_tissue_N2 = sim_pres_tissue_N2[ci]; | |
3656 calc_pres_tissue_He = last_pres_tissue_He = sim_pres_tissue_He[ci]; | |
3657 | |
3658 #else | |
3659 | |
3660 // get the current simulated tissue pressure | |
3661 pres_tissue = last_pres_tissue = sim_pres_tissue_N2[ci]; | |
3662 | |
3663 // set the a and b coefficients | |
3664 adopt_Buhlmann_coefficients(); | |
3665 | |
3666 #endif | |
3667 | |
3668 // simulate an increasing bottom time and check when the NDL is hit | |
3669 for(;;) | |
582 | 3670 { |
623 | 3671 |
3672 #ifdef _helium | |
3673 | |
3674 // calculate the total tissue pressure | |
582 | 3675 pres_tissue = calc_pres_tissue_N2 + calc_pres_tissue_He; |
3676 | |
623 | 3677 // adopt a and b coefficients to current N2/He ratio inside the tissue |
3678 adopt_Buhlmann_coefficients(); | |
3679 | |
3680 #endif | |
3681 | |
3682 // compute the maximum tissue pressure allowed to be exposed to an | |
3683 // ambient pressure equaling the surface pressure | |
3684 if( char_I_deco_model != 0 ) | |
3685 { | |
3686 // GF model enabled, this equation [2] is the inverse of equation [1] | |
3687 pres_limit = (1.0 - GF_high + GF_high / var_b) * pres_surface + GF_high * var_a; | |
3688 } | |
3689 else | |
582 | 3690 { |
623 | 3691 // straight Buhlmann |
3692 pres_limit = pres_surface / var_b + var_a; | |
3693 } | |
3694 | |
3695 // is the tissue pressure higher than the maximum tissue pressure allowed? | |
3696 if( pres_tissue > pres_limit) | |
3697 { | |
3698 // YES - tissue is outside NDL | |
3699 | |
3700 // was the tissue outside NDL right from the start? | |
3701 if( NDL_time_tissue == 0 ) | |
582 | 3702 { |
623 | 3703 // YES - search can be aborted |
3704 | |
3705 // at least one tissue is outside NDL, so overall NDL time is zero | |
3706 NDL_time = 0; | |
3707 | |
3708 // store the number of this tissue as being the leading one | |
3709 NDL_tissue_lead = NDL_tissue; | |
3710 | |
3711 // done | |
582 | 3712 break; |
3713 } | |
3714 | |
623 | 3715 // when code execution passes here, the tissue has become |
3716 // being outside NDL after doing one or more search steps | |
3717 | |
3718 // still searching with a step size of 10 minutes? | |
3719 if( step_size == 10 ) | |
582 | 3720 { |
623 | 3721 // YES - retry with smaller step size |
3722 | |
3723 // go back to last NDL time | |
3724 NDL_time_tissue -= 10; | |
3725 | |
3726 #ifdef _helium | |
3727 | |
3728 // go back to last pressures | |
3729 calc_pres_tissue_N2 = last_pres_tissue_N2; | |
3730 calc_pres_tissue_He = last_pres_tissue_He; | |
3731 | |
3732 #else | |
3733 | |
3734 // go back to last pressure | |
3735 pres_tissue = last_pres_tissue; | |
3736 | |
3737 #endif | |
3738 | |
3739 // reduce step size to 1 minute | |
3740 step_size = 1; | |
3741 | |
3742 // read the tissue increments for a step size of 1 minute | |
3743 read_Buhlmann_times(1); | |
3744 | |
3745 // redo search from last pressure & time within NDL with smaller step size | |
582 | 3746 continue; |
3747 } | |
623 | 3748 else |
582 | 3749 { |
623 | 3750 // NO - already tried with a step size of 1 minute |
3751 | |
3752 // go back to last NDL time that was within NDL | |
3753 NDL_time_tissue -= 1; | |
3754 | |
3755 // is the NDL time of this tissue shorter than the overall NDL time found so far? | |
3756 if( NDL_time_tissue < NDL_time ) | |
3757 { | |
3758 // YES - set this tissue's NDL time as the new overall NDL time | |
3759 NDL_time = NDL_time_tissue; | |
3760 | |
3761 // - store the number of this tissue as being the leading one | |
3762 NDL_tissue_lead = NDL_tissue; | |
3763 } | |
3764 | |
3765 // done | |
3766 break; | |
582 | 3767 } |
623 | 3768 } |
3769 else | |
582 | 3770 { |
623 | 3771 // NO - tissue is still within NDL |
3772 | |
3773 // The search can be terminated when the NDL time of this tissue | |
3774 // exceeds the overall NDL time, thus when a shorter NDL time has | |
3775 // already been found with another tissue. | |
3776 if( NDL_time_tissue >= NDL_time ) break; | |
3777 | |
3778 #ifdef _helium | |
3779 | |
3780 // back-up current tissue pressures | |
3781 last_pres_tissue_N2 = calc_pres_tissue_N2; | |
3782 last_pres_tissue_He = calc_pres_tissue_He; | |
3783 | |
3784 #else | |
3785 | |
3786 // back-up current tissue pressure | |
3787 last_pres_tissue = pres_tissue; | |
3788 | |
3789 #endif | |
3790 | |
3791 // step forward NDL time of current tissue | |
3792 NDL_time_tissue += step_size; | |
3793 | |
3794 #ifdef _helium | |
3795 | |
3796 // step forward tissue pressure - N2 | |
3797 temp_tissue = (ppN2 - calc_pres_tissue_N2) * var_N2_e; // pressure delta breathed - tissue | |
3798 apply_saturation_factors(); // apply safety factor | |
3799 calc_pres_tissue_N2 += temp_tissue; // add pressure delta to tissue | |
3800 | |
3801 // step forward tissue pressure - He | |
3802 temp_tissue = (ppHe - calc_pres_tissue_He) * var_He_e; // pressure delta breathed - tissue | |
3803 apply_saturation_factors(); // apply safety factor | |
3804 calc_pres_tissue_He += temp_tissue; // add pressure delta to tissue | |
3805 | |
3806 #else | |
3807 | |
3808 // step forward tissue pressure | |
3809 temp_tissue = (ppN2 - pres_tissue ) * var_N2_e; // pressure delta breathed - tissue | |
3810 apply_saturation_factors(); // apply safety factor | |
3811 pres_tissue += temp_tissue; // add pressure delta to tissue | |
3812 | |
3813 #endif | |
3814 | |
582 | 3815 } |
623 | 3816 } |
0 | 3817 } |
3818 | |
604 | 3819 |
0 | 3820 ////////////////////////////////////////////////////////////////////////////// |
3821 // clear_deco_table | |
3822 // | |
623 | 3823 // Modified: internal_deco_time[] stop durations |
3824 // internal_deco_depth[] stop depths | |
3825 // internal_deco_gas[] gases used at stops | |
0 | 3826 // |
3827 static void clear_deco_table(void) | |
3828 { | |
623 | 3829 for( i = 0; i < NUM_STOPS; ++i ) |
582 | 3830 { |
623 | 3831 internal_deco_time [i] = 0; |
3832 internal_deco_depth[i] = 0; | |
3833 internal_deco_gas[i] = 0; | |
582 | 3834 } |
3835 | |
628 | 3836 // reset stop table index and chained stops counter |
3837 stop_index = 0; | |
3838 chained_stops = 0; | |
3839 | |
560 | 3840 // clear stop table overflow warning |
628 | 3841 deco_warnings &= ~DECO_WARNING_INCOMPLETE; |
0 | 3842 } |
3843 | |
623 | 3844 |
0 | 3845 ////////////////////////////////////////////////////////////////////////////// |
3846 // update_deco_table | |
3847 // | |
623 | 3848 // Add time to a stop at char_depth_sim |
560 | 3849 // |
3850 // It is possible to create stops with a duration of 0 minutes, e.g. to | |
3851 // note a gas change "on the fly" while ascending. Therefore the criteria | |
604 | 3852 // to have reached the end of the list is depth == 0. |
0 | 3853 // |
623 | 3854 // Input: char_depth_sim stop's depth, in meters |
3855 // sim_gas_current_num gas used at stop, as index 1..5 or 0 for gas 6 | |
3856 // time_increment number of minutes to add to the stop | |
3857 // | |
3858 // Updated: internal_deco_depth[] depth (in meters) of each stop | |
3859 // internal_deco_time [] time (in minutes) of each stop | |
3860 // internal_deco_gas [] gas used (index 1-5) at each stop | |
560 | 3861 // |
3862 static unsigned char update_deco_table(PARAMETER unsigned char time_increment) | |
0 | 3863 { |
623 | 3864 assert( char_depth_sim > 0 ); // no stop at surface |
560 | 3865 |
628 | 3866 |
3867 // is there already a stop entry matching with the current depth and gas? | |
3868 if( internal_deco_depth[stop_index] == char_depth_sim ) | |
3869 if( internal_deco_gas [stop_index] == sim_gas_current_num ) | |
560 | 3870 { |
628 | 3871 // YES - increment stop time if possible, stop time entries are |
3872 // limited to 99 minutes because of display constraints | |
3873 if( internal_deco_time[stop_index] < (100 - time_increment) ) | |
560 | 3874 { |
628 | 3875 // YES - time increment fits into current stop entry, |
3876 // increment stop time and return with status 'success' | |
3877 internal_deco_time[stop_index] += time_increment; | |
3878 return 1; | |
3879 } | |
3880 else | |
3881 { | |
3882 // NO - A chained stop entry will be created further down in the | |
3883 // code to continue the stop, but we will limit the number | |
3884 // of chained stop table entries in order to abort an ever- | |
3885 // running deco calculation. Too many chained entries? | |
3886 if( ++chained_stops >= STOP_CHAINING_LIMIT ) | |
560 | 3887 { |
628 | 3888 // YES - set overflow warning and return with status 'failed' |
3889 deco_warnings |= DECO_WARNING_INCOMPLETE; | |
3890 return 0; | |
560 | 3891 } |
3892 } | |
628 | 3893 } |
3894 | |
3895 // the current stop entry does not match the current depth and gas, | |
3896 // or hasn't enough room left for the time increment | |
3897 | |
3898 // is the current stop entry in use? | |
3899 if( internal_deco_depth[stop_index] > 0 ) | |
3900 { | |
3901 // YES - current entry is in use, need to move on | |
3902 // to next entry position if possible | |
3903 | |
3904 // have all entry positions been used up? | |
3905 if( stop_index < (NUM_STOPS - 1) ) | |
560 | 3906 { |
628 | 3907 // NO - move on to next entry position |
3908 stop_index += 1; | |
3909 } | |
3910 else | |
3911 { | |
3912 // YES - set overflow warning and return with status 'failed' | |
3913 deco_warnings |= DECO_WARNING_INCOMPLETE; | |
3914 return 0; | |
560 | 3915 } |
3916 } | |
3917 | |
628 | 3918 // initial use of a new (or the very first) stop entry, |
3919 // store all stop data and return with status 'success' | |
3920 internal_deco_time [stop_index] = time_increment; | |
3921 internal_deco_depth[stop_index] = char_depth_sim; | |
3922 internal_deco_gas [stop_index] = sim_gas_current_num; | |
3923 return 1; | |
3924 } | |
3925 | |
3926 | |
3927 ////////////////////////////////////////////////////////////////////////////// | |
3928 // publish_deco_table | |
3929 // | |
3930 // Input: internal_deco_depth[] depth in internal stops table | |
3931 // internal_deco_time[] times ... | |
3932 // internal_deco_gas[] gases ... | |
3933 // | |
3934 // Output: char_O_deco_depth[] depth in the external stops table | |
3935 // char_O_deco_time[] times ... | |
3936 // char_O_deco_gas[] gases ... | |
3937 // char_O_deco_time_for_log times in reverse order | |
3938 // | |
3939 static void publish_deco_table(void) | |
3940 { | |
3941 overlay unsigned char x = stop_index; | |
3942 overlay unsigned char y; | |
3943 | |
3944 | |
3945 // copy depth, time and gas from internal to external stops table | |
3946 for( y = 0; y < NUM_STOPS; y++ ) | |
3947 { | |
3948 char_O_deco_depth[y] = internal_deco_depth[y]; | |
3949 char_O_deco_time [y] = internal_deco_time [y]; | |
3950 char_O_deco_gas [y] = internal_deco_gas [y]; | |
3951 } | |
3952 | |
3953 // copy times of shallowest stops to logging table | |
3954 for(y = 0; y < NUM_STOPS_LOG; x-- ) | |
3955 { | |
3956 // copy all stops that have a non-null stop time | |
3957 if( internal_deco_time[x] ) | |
3958 char_O_deco_time_for_log[y++] = internal_deco_time[x]; | |
3959 | |
3960 // abort if all stops are copied | |
3961 if( x == 0) break; | |
3962 } | |
3963 | |
3964 // fill the remainder of the logging table with null | |
3965 // if it is not completely filled already | |
3966 while( y < NUM_STOPS_LOG ) | |
3967 { | |
3968 char_O_deco_time_for_log[y++] = 0; | |
3969 } | |
0 | 3970 } |
3971 | |
3972 | |
3973 ////////////////////////////////////////////////////////////////////////////// | |
623 | 3974 // calc_desaturation_time_helper |
0 | 3975 // |
560 | 3976 // Helper function |
3977 // | |
623 | 3978 // Input: pres_actual current tissue pressure |
3979 // pres_target target tissue pressure | |
3980 // var_ht half-time of the tissue | |
3981 // desat_factor desaturation factor | |
3982 // | |
3983 // Output: int_time time needed by tissue to reach target pressure | |
3984 // | |
604 | 3985 static void calc_desaturation_time_helper(void) |
0 | 3986 { |
623 | 3987 // check if actual pressure is higher then target pressure |
3988 if( pres_actual > pres_target ) | |
3989 { | |
3990 // YES - compute remaining time | |
3991 | |
604 | 3992 overlay float pres_ratio; |
582 | 3993 |
623 | 3994 // compute pressure ratio to archive |
560 | 3995 pres_ratio = pres_actual / pres_target; |
3996 | |
3997 // Compute desaturation time with result rounded up to multiples of 10 minutes. | |
582 | 3998 // Main purpose is to avoid confusion, because the times do not clock down in |
3999 // one minute steps any more but get constantly re-computed according to current | |
4000 // ambient pressure and may therefor make steps of several minutes forwards and | |
604 | 4001 // backwards as ambient pressure rises/falls and N2/He ratio is being adjusted. |
623 | 4002 int_time = (unsigned short)( (var_ht * log(pres_ratio) / desat_factor) + 0.9 ); |
560 | 4003 } |
4004 else | |
623 | 4005 { |
4006 // NO - desaturation state reached, no remaining time | |
582 | 4007 int_time = 0; |
560 | 4008 } |
4009 } | |
4010 | |
623 | 4011 |
560 | 4012 ///////////////////////////////////////////////////////////////////////////// |
604 | 4013 // calc_desaturation_time |
4014 // | |
623 | 4015 // Calculates the time needed for the tissues to equilibrate with |
4016 // surface pressure and the no-fly / no-altitude time. | |
4017 // | |
4018 // Input: int_I_pres_surface | |
4019 // char_I_desaturation_multiplier | |
4020 // | |
4021 // Output: int_O_desaturation_time | |
4022 // int_O_nofly_time | |
560 | 4023 // |
4024 void calc_desaturation_time(void) | |
4025 { | |
623 | 4026 overlay float P_ambient_altitude; |
4027 | |
604 | 4028 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); |
4029 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
4030 | |
4031 | |
623 | 4032 // safeguard and convert surface pressure |
4033 if( int_I_pres_surface < 500) pres_surface = 0.5; | |
4034 else pres_surface = 0.001 * int_I_pres_surface; | |
4035 | |
4036 // calculate partial pressure of N2 in respired air at surface pressure | |
4037 calc_N2_equilibrium(); | |
4038 | |
4039 // get, safeguard and convert the saturation and desaturation factors | |
4040 get_saturation_factors(); | |
4041 | |
4042 // pre-computed term for later use: 10 [Min] * 0.6931 [=log(2)] * 1 [Desat Factor] * ... | |
4043 desat_factor = (6.931 * SURFACE_DESAT_FACTOR) * float_desaturation_multiplier; | |
582 | 4044 |
4045 // initialize vars | |
560 | 4046 int_O_desaturation_time = 0; |
582 | 4047 int_O_nofly_time = 0; |
4048 | |
623 | 4049 // get selected target altitude |
4050 switch( char_I_altitude_wait ) | |
4051 { | |
4052 case 1: P_ambient_altitude = P_ambient_1000m; break; | |
4053 case 2: P_ambient_altitude = P_ambient_2000m; break; | |
4054 case 3: P_ambient_altitude = P_ambient_3000m; break; | |
4055 default: P_ambient_altitude = P_ambient_fly; break; | |
4056 } | |
4057 | |
4058 // loop over all compartments in order slowest to fastest | |
604 | 4059 for( ci = NUM_COMP; ci > 0; ) |
582 | 4060 { |
623 | 4061 overlay float pres_tissue_max; |
4062 overlay unsigned short nofly_last = ~0; | |
4063 overlay unsigned short nofly_N2 = 0; | |
4064 | |
4065 #ifdef _helium | |
4066 overlay signed char search_direction; | |
4067 overlay unsigned short nofly_He = 0; | |
4068 #endif | |
4069 | |
4070 | |
4071 // decrement compartment index | |
560 | 4072 ci -= 1; |
582 | 4073 |
623 | 4074 // get the Buhlmann halftimes and coefficients |
582 | 4075 read_Buhlmann_ht(); |
560 | 4076 read_Buhlmann_coefficients(); |
582 | 4077 |
623 | 4078 |
4079 // | |
4080 // Desaturation time | |
4081 // | |
4082 | |
4083 // calculate desaturation time for N2 in tissue, | |
4084 // desaturated state is defined as residual tissue pressure <= 1.05 x ppN2 respired | |
4085 | |
4086 // current tissue pressure above equilibrium pressure | |
4087 pres_actual = real_pres_tissue_N2[ci] - N2_equilibrium; | |
4088 | |
4089 // target pressure above equilibrium pressure | |
4090 pres_target = 0.05 * N2_equilibrium; | |
4091 | |
4092 // half-time of the current tissue | |
4093 var_ht = var_N2_ht; | |
4094 | |
4095 // calculate desaturation time | |
4096 calc_desaturation_time_helper(); | |
4097 | |
4098 // store desaturation time if it is longer than longest found so far | |
4099 if( int_time > int_O_desaturation_time) int_O_desaturation_time = int_time; | |
4100 | |
4101 | |
4102 #ifdef _helium | |
4103 | |
4104 // calculate desaturation time for He in the tissue, | |
4105 // desaturated state is defined as residual tissue pressure <= 0.05 x ppN2 respired | |
4106 | |
4107 // actual tissue pressure above equilibrium: equilibrium for He is 0 bar | |
4108 pres_actual = real_pres_tissue_He[ci]; | |
4109 | |
4110 // target pressure above equilibrium pressure: use same target pressure as for N2 | |
4111 pres_target = 0.05 * N2_equilibrium; | |
4112 | |
4113 // half-time of the current tissue | |
4114 var_ht = var_He_ht; | |
4115 | |
4116 // calculate desaturation time | |
4117 calc_desaturation_time_helper(); | |
4118 | |
4119 // store desaturation time if it is longer than longest found so far | |
4120 if( int_time > int_O_desaturation_time) int_O_desaturation_time = int_time; | |
4121 | |
4122 #endif | |
4123 | |
4124 // | |
4125 // no-fly time | |
4126 // | |
582 | 4127 |
560 | 4128 // Target pressure for the tissue is the Buhlmann limit. We use the Buhlmann |
4129 // coefficients for N2 also for He because it is easier to calculate and the | |
4130 // N2 coefficients are more conservative than those for He, so we are on the | |
4131 // safe side, too. | |
582 | 4132 pres_tissue_max = (P_ambient_altitude/var_N2_b + var_N2_a); |
4133 | |
623 | 4134 // adjust target pressure by GF-high in case the GF model is in use, but not |
4135 // for the no-fly time as it's target pressure is hard to reach anyhow | |
4136 if( char_I_deco_model && char_I_altitude_wait ) | |
4137 pres_tissue_max = P_ambient_altitude + | |
4138 0.01 * char_I_GF_High_percentage * (pres_tissue_max - P_ambient_altitude); | |
4139 | |
4140 | |
4141 #ifdef _helium | |
4142 | |
4143 //---- Variant with Helium ------------------------------------------- | |
4144 | |
4145 // initialize split_N2_He in case there was a hard reboot / memory clear | |
604 | 4146 if( split_N2_He[ci] == 0 ) split_N2_He[ci] = 90; |
4147 | |
560 | 4148 // initialize search direction |
4149 search_direction = 0; | |
582 | 4150 |
560 | 4151 for(;;) |
4152 { | |
4153 // Calculate no-fly time for N2 in the tissue. | |
4154 // Flying is permitted when the N2 pressure fits into the assigned fraction above equilibrium. | |
4155 | |
623 | 4156 // current tissue pressure above equilibrium |
4157 pres_actual = real_pres_tissue_N2[ci] - N2_equilibrium; | |
4158 | |
4159 // target pressure above equilibrium pressure, weighted by N2/He split | |
560 | 4160 pres_target = (split_N2_He[ci] * 0.01) * (pres_tissue_max - N2_equilibrium); |
4161 | |
623 | 4162 // half-time of the current tissue |
4163 var_ht = var_N2_ht; | |
4164 | |
4165 // check if desaturation to target pressure is possible at all | |
4166 if( pres_target < 0.0 ) | |
560 | 4167 { |
623 | 4168 // NO - set no-fly time to 288 * 10 min = 48 h |
4169 int_O_nofly_time = 288; | |
4170 break; | |
560 | 4171 } |
604 | 4172 else |
560 | 4173 { |
623 | 4174 // YES - calculate desaturation time |
560 | 4175 calc_desaturation_time_helper(); |
623 | 4176 |
4177 // store time found | |
582 | 4178 nofly_N2 = int_time; |
560 | 4179 } |
582 | 4180 |
623 | 4181 // calculate no-fly time for He in the tissue, |
4182 // flying is permitted when the He pressure fits into the assigned fraction | |
4183 | |
4184 // current tissue pressure above equilibrium: equilibrium for He is 0 bar | |
4185 pres_actual = real_pres_tissue_He[ci]; | |
4186 | |
4187 // target pressure above equilibrium pressure, weighted by N2/He split | |
4188 pres_target = ((100 - split_N2_He[ci]) * 0.01) * (pres_tissue_max - N2_equilibrium); | |
4189 | |
4190 // half-time of the current tissue | |
582 | 4191 var_ht = var_He_ht; |
4192 | |
623 | 4193 // calculate desaturation time |
560 | 4194 calc_desaturation_time_helper(); |
623 | 4195 |
4196 // store time found | |
582 | 4197 nofly_He = int_time; |
560 | 4198 |
4199 | |
4200 // Because the sum of N2 and He tissue pressures needs to fit into the Buhlmann limit for | |
4201 // no-fly time calculation, each gas gets assigned a fraction of the available total pressure | |
623 | 4202 // limit. The optimum split between the two gases can not be computed by a single formula, |
560 | 4203 // because this would require the inversion of a function with two exponential terms, which is |
4204 // not possible. We do not want to do a computational complex simulation here like it is done | |
4205 // in the deco calculation code (although we tackle the same base problem here), so we just let | |
4206 // the computer try out which split will balance the no-fly times induced by the N2 and the He | |
4207 // at best. | |
582 | 4208 |
560 | 4209 // first of all, skip any optimization in case the current compartment is not the leading one |
4210 if( (nofly_N2 <= int_O_nofly_time) && (nofly_He <= int_O_nofly_time) ) break; | |
4211 | |
4212 // check if the N2 requires more waiting time than the He | |
582 | 4213 if( nofly_N2 >= nofly_He ) |
560 | 4214 { |
4215 // check if the search direction has changed, which means we are beyond the | |
4216 // optimum now, or if we are at the upper stop limit of split_N2_He | |
4217 if( (search_direction < 0) || (split_N2_He[ci] == 99) ) | |
604 | 4218 { |
623 | 4219 // either the just completed iteration was more close to the optimum or the one before |
4220 // was, so we take the best (i.e. shortest) time of both as the final no-fly time | |
560 | 4221 int_O_nofly_time = (nofly_N2 < nofly_last) ? nofly_N2 : nofly_last; |
623 | 4222 |
4223 // done | |
560 | 4224 break; |
4225 } | |
4226 | |
4227 // store the no-fly time found in this iteration | |
582 | 4228 nofly_last = nofly_N2; |
4229 | |
623 | 4230 // increase the N2 fraction of the split |
560 | 4231 split_N2_He[ci] += 1; |
623 | 4232 |
4233 // set search direction towards more N2 | |
560 | 4234 search_direction = +1; |
4235 } | |
4236 else | |
4237 { | |
4238 // check if the search direction has changed, which means we are beyond the | |
4239 // optimum now, or if we are at the lower stop limit of split_N2_He | |
4240 if( (search_direction > 0) || (split_N2_He[ci] == 1) ) | |
582 | 4241 { |
623 | 4242 // either the just completed iteration was more close to the optimum or the one before |
4243 // was, so we take the best (i.e. shortest) time of both as the final no-fly time | |
560 | 4244 int_O_nofly_time = (nofly_He < nofly_last) ? nofly_He : nofly_last; |
623 | 4245 |
4246 // done | |
560 | 4247 break; |
4248 } | |
4249 | |
4250 // store the no-fly time found in this iteration | |
582 | 4251 nofly_last = nofly_He; |
4252 | |
623 | 4253 // decrease the N2 fraction of the split |
560 | 4254 split_N2_He[ci] -= 1; |
623 | 4255 |
4256 // set search direction towards less N2 | |
604 | 4257 search_direction = -1; |
560 | 4258 } |
582 | 4259 |
560 | 4260 } // for(;;) |
4261 | |
623 | 4262 #else |
4263 | |
4264 //---- Variant without Helium ---------------------------------------- | |
4265 | |
4266 // current tissue pressure above equilibrium | |
4267 pres_actual = real_pres_tissue_N2[ci] - N2_equilibrium; | |
4268 | |
4269 // target pressure above equilibrium pressure | |
4270 pres_target = pres_tissue_max - N2_equilibrium; | |
4271 | |
4272 // half-time of the current tissue | |
4273 var_ht = var_N2_ht; | |
4274 | |
4275 // check if desaturation to target pressure is possible at all | |
4276 if( pres_target < 0.0 ) | |
4277 { | |
4278 // NO - set no-fly time to 288 * 10 min = 48 h | |
4279 int_O_nofly_time = 288; | |
4280 } | |
4281 else | |
4282 { | |
4283 // YES - calculate desaturation time | |
4284 calc_desaturation_time_helper(); | |
4285 | |
4286 // - extend desaturation time if this tissue needs | |
4287 // more time than already found to be needed | |
4288 if( int_time > int_O_nofly_time ) int_O_nofly_time = int_time; | |
4289 } | |
4290 | |
4291 #endif | |
4292 | |
560 | 4293 } // for(compartments) |
4294 | |
582 | 4295 |
623 | 4296 // rescale int_O_desaturation_time and int_O_nofly_time to full minutes for display purpose |
560 | 4297 int_O_desaturation_time *= 10; |
4298 int_O_nofly_time *= 10; | |
582 | 4299 |
623 | 4300 // limit int_O_desaturation_time and int_O_nofly_time to 5999 = 99 hours + 59 minutes |
4301 // because of display space constraints and rounding done above | |
560 | 4302 if( int_O_desaturation_time > 5999 ) int_O_desaturation_time = 5999; |
4303 if( int_O_nofly_time > 5999 ) int_O_nofly_time = 5999; | |
4304 | |
4305 | |
623 | 4306 // Clear the micro bubbles warning when the current gradient factor is < 100%. |
604 | 4307 // The current gradient factor is calculated by calc_interval() while not in diving mode. |
4308 // As the locked warning will stay set, this will cause the warning be be displayed in | |
4309 // attention color instead of warning color. | |
623 | 4310 if( int_O_lead_supersat < 100 ) |
4311 deco_warnings &= ~DECO_WARNING_MBUBBLES; | |
582 | 4312 |
560 | 4313 // clear some warnings when the desaturation time has become zero |
582 | 4314 if( int_O_desaturation_time == 0 ) |
623 | 4315 deco_warnings &= ~( DECO_WARNING_IBCD + DECO_WARNING_IBCD_lock |
4316 + DECO_WARNING_MBUBBLES + DECO_WARNING_MBUBBLES_lock | |
4317 + DECO_WARNING_OUTSIDE + DECO_WARNING_OUTSIDE_lock | |
4318 + DECO_ATTENTION_OUTSIDE ); | |
0 | 4319 } |
4320 | |
623 | 4321 |
0 | 4322 ////////////////////////////////////////////////////////////////////////////// |
582 | 4323 // Calculate desaturation of the real tissues for a given time interval |
0 | 4324 // |
582 | 4325 // Caution: Works on the real tissues! |
4326 // If in doubt, use this function only inside a context surrounded with | |
4327 // push_tissues_to_vault() / pull_tissues_from_vault() ! | |
4328 // | |
623 | 4329 // Input: int_I_pres_surface surface pressure in mbar |
4330 // time_interval time interval in minutes, must be limited to 254 at max | |
4331 // | |
4332 // Modified: tissue pressures N2 and He pressures of the tissues | |
4333 // CNS_fraction_real current real CNS value | |
4334 // ceiling minimum allowed depth in mbar relative pressure | |
4335 // lead_supersat supersaturation of the leading tissue (float) | |
4336 // int_O_lead_supersat supersaturation of the leading tissue (integer) | |
560 | 4337 // |
582 | 4338 static void calc_interval(PARAMETER unsigned char time_interval) |
0 | 4339 { |
582 | 4340 overlay unsigned char time; |
4341 | |
4342 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); | |
604 | 4343 assert( 100 <= char_I_saturation_multiplier && char_I_saturation_multiplier < 200 ); |
4344 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
4345 | |
4346 | |
623 | 4347 // safeguard and convert surface pressure |
4348 if( int_I_pres_surface < 500) pres_surface = 0.500; | |
4349 else pres_surface = 0.001 * int_I_pres_surface; | |
4350 | |
4351 // set breathed pressure to surface pressure | |
4352 real_pres_respiration = pres_surface; | |
4353 | |
4354 // calculate partial pressure of N2 in respired air at surface pressure | |
4355 calc_N2_equilibrium(); | |
4356 | |
4357 // calculate partial pressures (0.7902 is fraction of N2 in atmosphere as of Buhlmann) | |
4358 ppN2 = N2_equilibrium; | |
4359 ppHe = 0.0; | |
4360 | |
4361 // get, safeguard and convert the saturation and desaturation factors | |
4362 get_saturation_factors(); | |
4363 | |
4364 // adjust desaturation factor to surface mode | |
4365 float_desaturation_multiplier *= SURFACE_DESAT_FACTOR; | |
582 | 4366 |
4367 // Calculate the tissues: | |
4368 // Because calc_tissues() can calculate for 127 minutes at max, | |
4369 // the tissue updating may need to be done in two chunks. | |
4370 | |
4371 time = time_interval; | |
4372 | |
4373 // first chunk for the part exceeding 127 minutes | |
4374 if( time > 127) | |
560 | 4375 { |
582 | 4376 // do a full 127 minutes on the real tissues |
623 | 4377 tissue_increment = TISSUE_SELECTOR | 127; |
582 | 4378 calc_tissues(); |
4379 | |
623 | 4380 // determine the remaining time |
582 | 4381 time -= 127; |
560 | 4382 } |
4383 | |
623 | 4384 // program the remaining time (or full time if not exceeding 127 minutes) on the real tissues |
4385 tissue_increment = TISSUE_SELECTOR | time; | |
604 | 4386 |
4387 // update the N2 and He pressures in the tissues | |
582 | 4388 calc_tissues(); |
4389 | |
560 | 4390 |
4391 // Calculate CNS: | |
4392 // To speed up things and because on most invocations of this code char_I_dive_interval | |
4393 // is a multiple of 10 minutes, we loop the loop-counter down using two speeds. | |
4394 | |
582 | 4395 time = time_interval; |
4396 | |
604 | 4397 while( time ) |
560 | 4398 { |
582 | 4399 if( time > 9 ) |
560 | 4400 { |
623 | 4401 CNS_fraction_real *= 0.925874712; // half-time = 90 min -> 10 min: (1/2)^(1/9) |
4402 time -= 10; // fast speed looping | |
560 | 4403 } |
4404 else | |
4405 { | |
623 | 4406 CNS_fraction_real *= 0.992327946; // half-time = 90 min -> 1 min: (1/2)^(1/90) |
4407 time -= 1; // slow speed looping | |
560 | 4408 } |
582 | 4409 } |
560 | 4410 |
623 | 4411 // convert the CNS value to integer |
4412 convert_cur_CNS_for_display(); | |
4413 | |
4414 // calculate the supersaturation of the leading tissue, the | |
4415 // negative argument puts calc_limit() into surface mode | |
4416 // Attention: do not pass char_I_GF_High_percentage as an argument | |
4417 // here because it is not configured outside dive mode | |
4418 calc_limit(-1.0); | |
4419 | |
4420 // convert the saturation value of the leading tissue to integer | |
4421 convert_sat_for_display(); | |
0 | 4422 } |
4423 | |
560 | 4424 |
4425 ////////////////////////////////////////////////////////////////////////////// | |
604 | 4426 // calc_CNS |
560 | 4427 // |
623 | 4428 // Input: char_ppO2 current ppO2 [in 0.1 bars] |
4429 // tissue_increment time increment and tissue selector | |
4430 // | |
4431 // Modified: CNS_fraction_real accumulated CNS (real tissue context) | |
4432 // CNS_fraction_sim accumulated CNS (simulated tissue context) | |
560 | 4433 // |
604 | 4434 static void calc_CNS(void) |
560 | 4435 { |
604 | 4436 overlay float CNS_fraction_inc; // increment of CNS load, 0.01 = 1% |
623 | 4437 |
4438 | |
4439 // calculate CNS increment for 2 seconds interval | |
4440 if( char_ppO2 > 160 ) | |
4441 { | |
4442 // step-wise CNS increment | |
4443 | |
4444 // calculate index for increment look-up | |
4445 cns_i = (char_ppO2 - 161) / 5; // integer division | |
4446 | |
628 | 4447 // indexes > 17 use increment of index 17 |
4448 if( cns_i > 17 ) cns_i = 17; | |
4449 | |
623 | 4450 // read coefficient (increment) |
4451 read_CNS_c_coefficient(); | |
4452 | |
4453 // re-scale coefficient from storage format in [1/100000] to productive value | |
4454 CNS_fraction_inc = (float)var_cns_c / 100000.0; | |
4455 } | |
4456 else if( char_ppO2 > 50 ) | |
4457 { | |
4458 // range wise CNS increment approximation | |
4459 | |
4460 // calculate index for approximation coefficients look-up | |
4461 cns_i = (char_ppO2 - 51) / 10; // integer division | |
4462 | |
4463 // read coefficients | |
4464 read_CNS_ab_coefficient(); | |
4465 | |
4466 // calculate the CNS increment | |
4467 CNS_fraction_inc = 1.0 / (var_cns_a * char_ppO2 + var_cns_b ); | |
4468 } | |
4469 else | |
4470 { // no increment up to 0.5 bar ppO2 | |
4471 CNS_fraction_inc = 0.0; | |
4472 } | |
4473 | |
4474 // apply a time factor in case of minute-based interval (factor = N * 30.0) | |
4475 if( tissue_increment & TIME_MASK ) | |
4476 { | |
4477 CNS_fraction_inc *= (float)(tissue_increment & TIME_MASK) * 30.0; | |
4478 } | |
604 | 4479 |
4480 // update the CNS accumulator | |
623 | 4481 if ( tissue_increment & TISSUE_SELECTOR ) CNS_fraction_real += CNS_fraction_inc; // real tissues |
4482 else CNS_fraction_sim += CNS_fraction_inc; // simulated tissues | |
4483 } | |
4484 | |
4485 | |
4486 ////////////////////////////////////////////////////////////////////////////// | |
4487 // calc_due_by_depth_time_sac (Helper Function saving Code Space) | |
4488 // | |
4489 // Calculates the gas volume required for a given depth, time and usage (SAC) | |
4490 // rate. It uses a fixed surface pressure of 1.0 bar to deliver stable results | |
4491 // when used through the deco calculator. | |
4492 // | |
628 | 4493 // Input: gas_needs_depth depth in meters |
4494 // gas_needs_time time in minutes | |
4495 // gas_needs_usage_rate gas usage in liters per minute at surface pressure | |
623 | 4496 // |
628 | 4497 // Output: gas_needs_volume_due required gas volume in liters |
623 | 4498 // |
4499 static void calc_due_by_depth_time_sac(void) | |
4500 { | |
628 | 4501 gas_needs_volume_due = ((float)gas_needs_depth * METER_TO_BAR + pres_surface) * gas_needs_time * gas_needs_usage_rate; |
0 | 4502 } |
4503 | |
560 | 4504 |
0 | 4505 ////////////////////////////////////////////////////////////////////////////// |
623 | 4506 // calc_TR_functions |
4507 // | |
4508 // Process Pressure Readings (OSTC TR only) | |
4509 // | |
4510 // Input: todo | |
4511 // | |
4512 // Output: todo | |
4513 // | |
4514 #ifdef _rx_functions | |
4515 static void calc_TR_functions(void) | |
560 | 4516 { |
623 | 4517 // pressure warnings for reading 1, but only if enabled and pressure value available |
4518 if( (char_I_pressure_gas[0] > 0) && !(int_IO_pressure_value[0] & INT_FLAG_NOT_AVAIL) ) | |
604 | 4519 { |
623 | 4520 overlay unsigned short pressure_value = int_IO_pressure_value[0] & ~INT_FLAG_OUTDATED; |
4521 | |
4522 if( (char_I_pressure_gas[0] < 6 ) && !(int_O_pressure_need[0] & INT_FLAG_NOT_AVAIL) ) | |
4523 { | |
4524 // not a diluent and need available: warning & attention by need | |
4525 if ( pressure_value <= int_O_pressure_need[0]) | |
4526 int_IO_pressure_value[0] |= INT_FLAG_WARNING; | |
4527 else if( pressure_value <= int_O_pressure_need[0] + int_O_pressure_need[0] / 2 ) | |
4528 int_IO_pressure_value[0] |= INT_FLAG_ATTENTION; | |
4529 } | |
4530 else | |
4531 { | |
4532 // a diluent or need not available: warning & attention by fixed thresholds | |
4533 if ( pressure_value <= PRESSURE_LIMIT_WARNING ) int_IO_pressure_value[0] |= INT_FLAG_WARNING; | |
4534 else if ( pressure_value <= PRESSURE_LIMIT_ATTENTION ) int_IO_pressure_value[0] |= INT_FLAG_ATTENTION; | |
4535 } | |
604 | 4536 } |
4537 | |
623 | 4538 // pressure warnings for reading 2, but only if enabled and pressure value available |
4539 if( (char_I_pressure_gas[1] > 0) && !(int_IO_pressure_value[1] & INT_FLAG_NOT_AVAIL) ) | |
560 | 4540 { |
623 | 4541 overlay unsigned short pressure_value = int_IO_pressure_value[1] & ~INT_FLAG_OUTDATED; |
4542 | |
4543 if( (char_I_pressure_gas[1] < 6 ) && !(int_O_pressure_need[1] & INT_FLAG_NOT_AVAIL) ) | |
560 | 4544 { |
623 | 4545 // not a diluent and need available: warning & attention by need |
4546 if ( pressure_value <= int_O_pressure_need[1]) | |
4547 int_IO_pressure_value[1] |= INT_FLAG_WARNING; | |
4548 else if ( pressure_value <= int_O_pressure_need[1] + int_O_pressure_need[1] / 2 ) | |
4549 int_IO_pressure_value[1] |= INT_FLAG_ATTENTION; | |
560 | 4550 } |
4551 else | |
4552 { | |
623 | 4553 // a diluent or need not available: warning & attention by fixed thresholds |
4554 if ( pressure_value <= PRESSURE_LIMIT_WARNING ) int_IO_pressure_value[1] |= INT_FLAG_WARNING; | |
4555 else if ( pressure_value <= PRESSURE_LIMIT_ATTENTION ) int_IO_pressure_value[1] |= INT_FLAG_ATTENTION; | |
560 | 4556 } |
4557 } | |
582 | 4558 |
623 | 4559 //--- SAC Calculation --------------------------------------------------------------------- |
4560 // | |
4561 // char_I_SAC_mode =0: disabled | |
4562 // =1: SAC from 1st reading | |
4563 // =2: SAC from 2nd reading | |
4564 // =3: SAC from higher one of both pressure drops (independent double mode) | |
4565 // =4: SAC (O2 usage) from 2nd reading without real_pres_respiration term | |
4566 | |
4567 // set SAC rate to not available by default | |
4568 int_O_SAC_measured = 0 + INT_FLAG_NOT_AVAIL; | |
4569 | |
4570 // get a copy of the current absolute pressure | |
4571 pres_respiration_sac = real_pres_respiration; | |
4572 | |
4573 // set threshold for SAC rate attention | |
628 | 4574 max_sac_rate = (deco_info & DECO_MODE) ? char_I_SAC_deco : char_I_SAC_work; |
623 | 4575 |
4576 // char_I_SAC_deco / char_I_SAC_work are in l/min, max_sac_rate is in 0.1 l/min | |
4577 max_sac_rate *= 10; | |
4578 | |
4579 | |
4580 // pre-process SAC mode 3 (independent double) | |
4581 if( char_I_SAC_mode == 3 ) | |
560 | 4582 { |
623 | 4583 overlay unsigned char reading1_gas; |
4584 overlay unsigned char reading2_gas; | |
4585 overlay unsigned char reading1_tanksize; | |
4586 overlay unsigned char reading2_tanksize; | |
4587 overlay unsigned short reading1_press; | |
4588 overlay unsigned short reading2_press; | |
4589 overlay unsigned short reading1_drop; | |
4590 overlay unsigned short reading2_drop; | |
4591 | |
4592 // get gas numbers (1-10) of both readings | |
4593 reading1_gas = char_I_pressure_gas[0]; | |
4594 reading2_gas = char_I_pressure_gas[1]; | |
4595 | |
4596 // default to no SAC calculation | |
4597 char_I_SAC_mode = 0; | |
4598 | |
4599 // clear switch advice by default | |
4600 deco_info &= ~IND_DOUBLE_SWITCH_FLAG; | |
4601 | |
4602 // check if both readings are configured and available | |
4603 if( reading1_gas ) | |
4604 if( reading2_gas ) | |
4605 if( !(int_IO_pressure_value[0] & INT_FLAG_NOT_AVAIL) ) | |
4606 if( !(int_IO_pressure_value[1] & INT_FLAG_NOT_AVAIL) ) | |
4607 if( !(int_I_pressure_drop[0] & INT_FLAG_NOT_AVAIL) ) | |
4608 if( !(int_I_pressure_drop[1] & INT_FLAG_NOT_AVAIL) ) | |
560 | 4609 { |
623 | 4610 // get tank pressures, stripping flags |
4611 reading1_press = int_IO_pressure_value[0] & 0x0FFF; // in 0.1 bar | |
4612 reading2_press = int_IO_pressure_value[1] & 0x0FFF; // in 0.1 bar | |
4613 | |
4614 // get pressure drops as integers, stripping flags and shifting right | |
4615 // to avoid an overflow when multiplying with the tank size later on | |
4616 reading1_drop = (int_I_pressure_drop[0] & 0x0FFF) >> 2; | |
4617 reading2_drop = (int_I_pressure_drop[1] & 0x0FFF) >> 2; | |
4618 | |
4619 // get tank sizes | |
4620 reading1_tanksize = char_I_gas_avail_size[reading1_gas-1]; | |
4621 reading2_tanksize = char_I_gas_avail_size[reading2_gas-1]; | |
4622 | |
4623 // set mode to calculate SAC on the reading with the higher absolute drop | |
4624 char_I_SAC_mode = (reading1_drop * reading1_tanksize > reading2_drop * reading2_tanksize) ? 1 : 2; | |
4625 | |
4626 // compute switch advice if pressure (in 0.1 bar) of tank breathed from is | |
4627 // more than char_I_max_pres_diff (in bar) below pressure of the other tank. | |
4628 if( char_I_SAC_mode == 1 ) | |
4629 { | |
4630 // breathing from reading 1, switch advice if pressure on reading 1 lower than on 2 | |
4631 if( (reading1_press + 10*char_I_max_pres_diff) <= reading2_press ) | |
4632 deco_info |= IND_DOUBLE_SWITCH_FLAG; | |
4633 } | |
4634 else | |
4635 { | |
4636 // breathing from reading 2, switch advice if pressure on reading 2 lower than on 1 | |
4637 if( (reading2_press + 10*char_I_max_pres_diff) <= reading1_press ) | |
4638 deco_info |= IND_DOUBLE_SWITCH_FLAG; | |
4639 } | |
560 | 4640 } |
4641 } | |
4642 | |
623 | 4643 |
4644 // pre-process SAC mode 4 (O2 usage by reading 2) | |
4645 if( char_I_SAC_mode == 4 ) | |
4646 { | |
4647 // O2 usage on CCR is independent from absolute pressure | |
4648 pres_respiration_sac = 1.0; | |
4649 | |
4650 // O2 pressure drop is measured via reading 2 | |
4651 char_I_SAC_mode = 2; | |
4652 | |
4653 // reconfigure max SAC rate to O2 consumption attention threshold | |
4654 max_sac_rate = O2_CONSUMPTION_LIMIT_ATTENTION; | |
4655 } | |
4656 | |
4657 | |
628 | 4658 // select which pressure reading to log |
4659 if( char_I_SAC_mode == 1 ) int_O_tank_pressure = int_IO_pressure_value[0]; | |
4660 else if( char_I_SAC_mode == 2 ) int_O_tank_pressure = int_IO_pressure_value[1]; | |
4661 else int_O_tank_pressure = 0; | |
4662 | |
4663 // strip flags | |
4664 int_O_tank_pressure &= 0x0FFF; | |
4665 | |
4666 // TODO: decide if log shall be in 0.1 bar of full bar only | |
4667 // scale to full bar only | |
4668 int_O_tank_pressure /= 10; | |
4669 | |
4670 | |
623 | 4671 // calculate SAC - modes 1 & 2 |
4672 if( (char_I_SAC_mode == 1) || (char_I_SAC_mode == 2) ) | |
560 | 4673 { |
623 | 4674 overlay unsigned char reading_index; |
4675 overlay unsigned char reading_gas; | |
4676 overlay unsigned char reading_tanksize; | |
4677 overlay float reading_drop; | |
4678 | |
4679 // set index: char_I_SAC_mode = 1 -> reading one, index 0 | |
4680 // = 2 -> two, 1 | |
4681 reading_index = char_I_SAC_mode - 1; | |
4682 | |
4683 // get gas number (1-10) | |
4684 reading_gas = char_I_pressure_gas[reading_index]; | |
4685 | |
4686 // check if reading is configured and available | |
4687 if( reading_gas ) | |
4688 if( !(int_I_pressure_drop[reading_index] & INT_FLAG_NOT_AVAIL) ) | |
4689 { | |
4690 // get tank size (in liter) | |
4691 reading_tanksize = char_I_gas_avail_size[reading_gas-1]; | |
4692 | |
4693 // get pressure drop as float, stripping flags (in 1/5120 bar/sec) | |
4694 reading_drop = (float)(int_I_pressure_drop[reading_index] & 0x0FFF); | |
4695 | |
4696 // check if pressure drop is within range | |
4697 if( !(int_I_pressure_drop[reading_index] & INT_FLAG_OUT_OF_RANGE) ) | |
4698 { | |
4699 // calculate SAC, 10 is factor to have result in 0.1 liter/min | |
4700 // 60 is factor for 60 seconds per 1 minute, | |
4701 // 5120 accounts for reading_drop being in 1/5120 bar/sec | |
4702 // 10*60/5120 = 60/512 = 15/128 | |
4703 float_sac = reading_drop * 15/128 * reading_tanksize / pres_respiration_sac; | |
4704 | |
4705 // limit result to 999 (99.9 liter/min) | |
4706 if ( float_sac >= 998.5 ) | |
4707 { | |
4708 int_O_SAC_measured = 999 + INT_FLAG_ATTENTION; | |
4709 } | |
4710 else | |
4711 { | |
4712 // convert float to integer | |
4713 int_O_SAC_measured = (unsigned short)(float_sac + 0.5); | |
4714 | |
4715 // set attention flag if exceeding SAC threshold, but only if pressure drop is not outdated | |
4716 if( !(int_I_pressure_drop[reading_index] & INT_FLAG_OUTDATED) ) | |
4717 if( int_O_SAC_measured >= max_sac_rate ) | |
4718 { | |
4719 int_O_SAC_measured |= INT_FLAG_ATTENTION; | |
4720 } | |
4721 } | |
4722 } | |
4723 else | |
4724 { | |
4725 // pressure drop is out of range, so SAC will be set out of range, too | |
4726 int_O_SAC_measured = 999 + INT_FLAG_ATTENTION; | |
4727 } | |
4728 | |
4729 // copy outdated flag from int_I_pressure_drop to int_O_SAC_measured | |
4730 if( int_I_pressure_drop[reading_index] & INT_FLAG_OUTDATED ) | |
4731 { | |
4732 int_O_SAC_measured |= INT_FLAG_OUTDATED; | |
4733 } | |
4734 } | |
4735 } | |
4736 } | |
4737 #endif | |
4738 | |
4739 | |
4740 ////////////////////////////////////////////////////////////////////////////// | |
4741 // convert_gas_needs_to_press | |
4742 // | |
4743 // Converts gas volumes into pressures and sets respective flags | |
4744 // | |
4745 // Input: gas_needs_gas_index index of the gas to convert (0-4) | |
4746 // gas_volume_need[] needed gas volume in liters | |
4747 // char_I_gas_avail_pres[] available gas volume in bar | |
4748 // char_I_gas_avail_size[] size of the tanks in liters | |
4749 // char_I_pressure_gas[] gas configured on reading 1/2 (TR only) | |
4750 // | |
4751 // Output: int_O_gas_need_vol[] required gas amount in liters, including flags | |
4752 // int_O_gas_need_pres[] required gas amount in bar, including flags | |
4753 // int_O_pressure_need[] required gas amount for reading 1/2 (TR only) | |
4754 // | |
4755 static void convert_gas_needs_to_press(void) | |
4756 { | |
4757 | |
4758 // just to make the code more readable... | |
4759 i = gas_needs_gas_index; | |
4760 | |
4761 if( gas_volume_need[i] >= 65534.5 ) | |
4762 { | |
4763 int_O_gas_need_vol[i] = 65535; // clip at 65535 liters | |
4764 int_O_gas_need_pres[i] = 999 | INT_FLAG_WARNING | INT_FLAG_HIGH; // 999 bar + warning flag + >999 flag | |
560 | 4765 } |
4766 else | |
4767 { | |
623 | 4768 overlay unsigned short int_pres_warn; |
4769 overlay unsigned short int_pres_attn; | |
4770 | |
4771 // set warning and attention thresholds | |
4772 int_pres_warn = 10.0 * (unsigned short)char_I_gas_avail_pres[i]; | |
628 | 4773 int_pres_attn = GAS_NEEDS_LIMIT_ATTENTION * int_pres_warn; |
623 | 4774 |
4775 // convert ascent gas volume need from float to integer [in liter] | |
4776 int_O_gas_need_vol[i] = (unsigned short)gas_volume_need[i]; | |
4777 | |
4778 // compute how much pressure in the tank will be needed [in bar] | |
4779 int_O_gas_need_pres[i] = (unsigned short)( gas_volume_need[i] / char_I_gas_avail_size[i] + 0.999 ); | |
4780 | |
4781 // limit result to 999 bar because of display constraints | |
4782 if( int_O_gas_need_pres[i] > 999 ) int_O_gas_need_pres[i] = 999 | INT_FLAG_HIGH; | |
4783 | |
4784 // set flags for fast evaluation by dive mode | |
4785 if ( int_O_gas_need_pres[i] == 0 ) int_O_gas_need_pres[i] |= INT_FLAG_ZERO; | |
4786 else if ( int_O_gas_need_pres[i] >= int_pres_warn ) int_O_gas_need_pres[i] |= INT_FLAG_WARNING; | |
4787 else if ( int_O_gas_need_pres[i] >= int_pres_attn ) int_O_gas_need_pres[i] |= INT_FLAG_ATTENTION; | |
560 | 4788 } |
4789 | |
623 | 4790 // set invalid flag if there is an overflow in the stops table |
628 | 4791 if( deco_warnings & DECO_WARNING_INCOMPLETE ) int_O_gas_need_pres[i] |= INT_FLAG_INVALID; |
560 | 4792 |
604 | 4793 #ifdef _rx_functions |
4794 // only for OSTC TR model with TR functions enabled | |
623 | 4795 if( main_status & TR_FUNCTIONS ) |
560 | 4796 { |
623 | 4797 // char_I_pressure_gas[] uses gas indexes from 1-10, loop variable i runs from 0 to 4 |
4798 overlay unsigned char j = i+1; | |
4799 | |
4800 // check if the current gas is configured on pressure reading 1 or 2 | |
4801 if( (char_I_pressure_gas[0] == j) || (char_I_pressure_gas[1] == j) ) | |
560 | 4802 { |
623 | 4803 // get a copy of the required pressure in full bar |
4804 overlay unsigned short int_pres_need = int_O_gas_need_pres[i]; | |
4805 | |
4806 // strip all flags | |
4807 int_pres_need &= 1023; | |
4808 | |
4809 // limit to 400 bar and multiply by 10 to get required pressure in 0.1 bar | |
4810 int_pres_need = (int_pres_need > 400) ? 4000 | INT_FLAG_OUT_OF_RANGE : 10 * int_pres_need; | |
4811 | |
4812 // tag as not available if there is an overflow in the stops table | |
628 | 4813 if( deco_warnings & DECO_WARNING_INCOMPLETE ) int_pres_need |= INT_FLAG_NOT_AVAIL; |
623 | 4814 |
4815 // copy to reading data (in both readings the same gas could be configured) | |
4816 if( char_I_pressure_gas[0] == j ) int_O_pressure_need[0] = int_pres_need; | |
4817 if( char_I_pressure_gas[1] == j ) int_O_pressure_need[1] = int_pres_need; | |
4818 } | |
4819 } // TR functions | |
604 | 4820 #endif |
0 | 4821 } |
4822 | |
623 | 4823 |
4824 ////////////////////////////////////////////////////////////////////////////// | |
4825 // convert the real CNS value to integer | |
4826 // | |
4827 // Input CNS_fraction_real current CNS value as float | |
4828 // | |
4829 // Output: int_O_CNS_current current CNS value as integer including flags | |
4830 // | |
4831 static void convert_cur_CNS_for_display(void) | |
4832 { | |
4833 // convert to integer | |
4834 float_value = CNS_fraction_real; convert_float_to_int(); int_O_CNS_current = int_value; | |
4835 | |
4836 // set warning & attention flags | |
628 | 4837 if ( int_O_CNS_current >= CNS_LIMIT_WARNING ) int_O_CNS_current |= INT_FLAG_WARNING; |
4838 else if ( int_O_CNS_current >= CNS_LIMIT_ATTENTION ) int_O_CNS_current |= INT_FLAG_ATTENTION; | |
623 | 4839 } |
4840 | |
4841 | |
0 | 4842 ////////////////////////////////////////////////////////////////////////////// |
623 | 4843 // convert the simulated CNS value to integer |
4844 // | |
4845 // Input: CNS_fraction_sim CNS value after predicted ascent in float | |
4846 // | |
4847 // Output: int_sim_CNS_fraction CNS value after predicted ascent in integer | |
4848 // including flags, will be routed to | |
4849 // int_O_{normal,alternative}_CNS_fraction | |
4850 // | |
604 | 4851 static void convert_sim_CNS_for_display(void) |
582 | 4852 { |
623 | 4853 // convert to integer |
4854 float_value = CNS_fraction_sim; convert_float_to_int(); int_sim_CNS_fraction = int_value; | |
4855 | |
4856 // set warning & attention flags | |
628 | 4857 if ( int_sim_CNS_fraction >= CNS_LIMIT_WARNING ) int_sim_CNS_fraction |= INT_FLAG_WARNING; |
4858 else if ( int_sim_CNS_fraction >= CNS_LIMIT_ATTENTION ) int_sim_CNS_fraction |= INT_FLAG_ATTENTION; | |
604 | 4859 |
4860 // set invalid flag if there is an overflow in the stops table | |
628 | 4861 if ( deco_warnings & DECO_WARNING_INCOMPLETE ) int_sim_CNS_fraction |= INT_FLAG_INVALID; |
604 | 4862 } |
4863 | |
623 | 4864 |
604 | 4865 ////////////////////////////////////////////////////////////////////////////// |
623 | 4866 // convert the saturation value of the leading tissue to integer |
4867 // | |
4868 // Input lead_supersat saturation of the leading tissue | |
4869 // lead_tissue number of the leading tissue | |
4870 // char_I_GF_High_percentage GF high factor | |
4871 // | |
4872 // Output: int_O_lead_supersat saturation of the leading tissue | |
4873 // char_O_lead_tissue number of the leading tissue | |
4874 // | |
4875 // Modified: deco_warnings deco engine warnings vector | |
4876 // | |
4877 static void convert_sat_for_display(void) | |
604 | 4878 { |
623 | 4879 // convert supersaturation of the leading tissue to int_O_lead_supersat in % (1.0 = 100%) |
604 | 4880 // limit to 255 because of constraints in ghostwriter code |
623 | 4881 if ( lead_supersat <= 0.000 ) int_O_lead_supersat = 0; |
4882 else if ( lead_supersat > 2.545 ) int_O_lead_supersat = 255; | |
4883 else int_O_lead_supersat = (unsigned short)(100 * lead_supersat + 0.5); | |
4884 | |
4885 // set warning & attention flags | |
4886 if( int_O_lead_supersat > 100 ) | |
604 | 4887 { |
623 | 4888 int_O_lead_supersat |= INT_FLAG_WARNING; // make GF factor shown in red |
4889 deco_warnings |= DECO_WARNING_OUTSIDE; // make depth shown in red | |
4890 } | |
4891 else if( (char_I_deco_model != 0) && (int_O_lead_supersat > char_I_GF_High_percentage) | |
4892 || (char_I_deco_model == 0) && (int_O_lead_supersat > 99 ) ) | |
4893 { | |
4894 int_O_lead_supersat |= INT_FLAG_ATTENTION; // make GF factor shown in yellow | |
4895 deco_warnings |= DECO_ATTENTION_OUTSIDE; // make depth shown in yellow | |
604 | 4896 } |
4897 | |
4898 // export also the number of the leading tissue | |
623 | 4899 char_O_lead_tissue = lead_tissue; |
604 | 4900 } |
4901 | |
623 | 4902 |
604 | 4903 ////////////////////////////////////////////////////////////////////////////// |
623 | 4904 // convert the ceiling value to integer |
4905 // | |
4906 // Input: ceiling minimum depth permitted in float | |
4907 // | |
628 | 4908 // Output: int_O_ceiling minimum depth permitted in mbar (cm) |
623 | 4909 // |
4910 // Modified: deco_info deco engine information vector | |
4911 // | |
604 | 4912 static void convert_ceiling_for_display(void) |
4913 { | |
4914 // Convert ceiling to int_O_ceiling in mbar relative pressure. | |
4915 // Round up to next 10 cm so that the ceiling disappears only | |
4916 // when the ceiling limit is really zero. This will coincident | |
4917 // with TTS switching back to NDL time. | |
628 | 4918 // The +1.5 term figures in the conversion factor of 10.015 m/bar |
4919 // which is used inside the deco engine but not outside of it. | |
623 | 4920 if ( ceiling <= 0.0 ) int_O_ceiling = 0; |
4921 else if ( ceiling > 16.0 ) int_O_ceiling = 16000; | |
628 | 4922 else int_O_ceiling = (unsigned short)(ceiling * (1000+1.5) + 9); |
604 | 4923 |
4924 // set/reset ceiling flag | |
623 | 4925 if ( int_O_ceiling ) deco_info |= DECO_CEILING; |
4926 else deco_info &= ~DECO_CEILING; | |
0 | 4927 } |
4928 | |
623 | 4929 |
0 | 4930 ////////////////////////////////////////////////////////////////////////////// |
582 | 4931 // push_tissues_to_vault & pull_tissues_from_vault |
4932 // | |
4933 // ATTENTION: Do not use from inside the deco engine! | |
4934 // The vault is exclusively reserved to back-up and restore the real | |
4935 // tissues and related data when entering / leaving simulation mode! | |
4936 // | |
623 | 4937 // Input/Output: CNS_fraction_real current real CNS value |
4938 // char_O_deco_warnings deco engine warnings vector | |
4939 // real_pres_tissue_N2[] partial pressure of N2 in real tissues | |
4940 // real_pres_tissue_He[] partial pressure of He in real tissues | |
4941 // | |
4942 // Output: int_O_CNS_current current CNS value as integer including flags | |
4943 // | |
604 | 4944 static void push_tissues_to_vault(void) |
0 | 4945 { |
623 | 4946 // store the current CNS value and deco warnings |
4947 vault_CNS_fraction_real = CNS_fraction_real; | |
4948 vault_deco_warnings = char_O_deco_warnings; | |
4949 vault_deco_info = char_O_deco_info; | |
4950 | |
4951 // store the tissue pressures | |
4952 for( i = 0; i < NUM_COMP; i++ ) | |
582 | 4953 { |
623 | 4954 vault_pres_tissue_N2[i] = real_pres_tissue_N2[i]; |
4955 #ifdef _helium | |
4956 vault_pres_tissue_He[i] = real_pres_tissue_He[i]; | |
4957 #else | |
4958 vault_pres_tissue_He[i] = 0; | |
4959 #endif | |
582 | 4960 } |
0 | 4961 } |
4962 | |
604 | 4963 static void pull_tissues_from_vault(void) |
0 | 4964 { |
623 | 4965 // restore the CNS value and deco warnings |
4966 CNS_fraction_real = vault_CNS_fraction_real; | |
4967 char_O_deco_warnings = vault_deco_warnings; | |
4968 char_O_deco_info = vault_deco_info; | |
4969 | |
4970 // convert the CNS value to integer | |
4971 convert_cur_CNS_for_display(); | |
4972 | |
4973 // restore the tissue pressures | |
4974 for( i = 0; i < NUM_COMP; i++ ) | |
582 | 4975 { |
623 | 4976 real_pres_tissue_N2[i] = vault_pres_tissue_N2[i]; |
4977 #ifdef _helium | |
4978 real_pres_tissue_He[i] = vault_pres_tissue_He[i]; | |
4979 #else | |
4980 real_pres_tissue_He[i] = 0; | |
4981 #endif | |
582 | 4982 } |
0 | 4983 } |
4984 | |
623 | 4985 |
0 | 4986 ////////////////////////////////////////////////////////////////////////////// |
4987 // | |
4988 #ifndef CROSS_COMPILE | |
4989 void main() {} | |
4990 #endif |