comparison src/p2_deco.c @ 582:b455b31ce022

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