Mercurial > public > mk2
annotate code_part1/OSTC_code_c_part2/p2_deco.c @ 201:e5c484d59a91
Gas switch debugging...
+ do stop at the exact gas switch depth.
+ keep selecting best gas in ascent simulation.
+ do not add gas-switch-delay for the already breathed gas.
*BUG* Keeps adding delay if the diver do not obey gas changes !
author | JeanDo |
---|---|
date | Tue, 15 Feb 2011 20:04:37 +0100 |
parents | 0a3ca358c684 |
children | 2d9af08ed0ac |
rev | line source |
---|---|
116 | 1 // ************************************************************** |
2 // p2_deco.c | |
3 // | |
4 // Created on: 12.05.2009 | |
5 // Author: chsw | |
6 // | |
7 // ************************************************************** | |
8 | |
9 ////////////////////////////////////////////////////////////////////////////// | |
10 // OSTC - diving computer code | |
11 // Copyright (C) 2008 HeinrichsWeikamp GbR | |
12 // | |
13 // This program is free software: you can redistribute it and/or modify | |
14 // it under the terms of the GNU General Public License as published by | |
15 // the Free Software Foundation, either version 3 of the License, or | |
16 // (at your option) any later version. | |
17 // | |
18 // This program is distributed in the hope that it will be useful, | |
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 // GNU General Public License for more details. | |
22 // | |
23 // You should have received a copy of the GNU General Public License | |
24 // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 // | |
26 ////////////////////////////////////////////////////////////////////////////// | |
27 | |
28 // ***************************** | |
29 // ** I N T R O D U C T I O N ** | |
30 // ***************************** | |
31 // | |
32 // OSTC | |
33 // | |
34 // code: | |
35 // p2_deco_main_c_v101.c | |
36 // part2 of the OSTC code | |
37 // code with constant O2 partial pressure routines | |
38 // under construction !! | |
39 // | |
40 // summary: | |
41 // decompression routines | |
42 // for the OSTC experimental project | |
43 // written by Christian Weikamp | |
44 // last revision __________ | |
45 // comments added _________ | |
46 // | |
47 // additional files: | |
48 // p2_tables_v100.romdata (other files) | |
49 // 18f4685_ostc_v100.lkr (linker script) | |
50 // | |
51 // history: | |
52 // 01/03/08 v100: first release candidate | |
53 // 03/13/08 v101: start of programming ppO2 code | |
54 // 03/13/25 v101a: backup of interrim version with ppO2 calculation | |
55 // 03/13/25 v101: open circuit gas change during deco | |
56 // 03/13/25 v101: CNS_fraction calculation | |
57 // 03/13/26 v101: optimization of tissue calc routines | |
58 // 07/xx/08 v102a: debug of bottom time routine | |
59 // 09/xx/08 v102d: Gradient Factor Model implemenation | |
60 // 10/10/08 v104: renamed to build v103 for v118 stable | |
171 | 61 // 10/14/08 v104: integration of char_I_depth_last_deco for Gradient Model |
116 | 62 // 03/31/09 v107: integration of FONT Incon24 |
63 // 05/23/10 v109: 5 gas changes & 1 min timer | |
64 // 07/13/10 v110: cns vault added | |
65 // 12/25/10 v110: split in three files (deco.c, main.c, definitions.h) | |
163 | 66 // 2011/01/20: [jDG] Create a common file included in ASM and C code. |
167 | 67 // 2011/01/23: [jDG] Added read_custom_function(). |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
68 // 2011/01/24: [jDG] Make ascenttime an int. No more overflow! |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
69 // 2011/01/25: [jDG] Fusion deco array for both models. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
70 // 2011/01/25: [jDG] Use CF(54) to reverse deco order. |
193 | 71 // 2011/02/11: [jDG] Reworked gradient-factor implementation. |
197 | 72 // 2011/02/13: [jDG] CF55 for additional gas switch delay in decoplan. |
116 | 73 // |
167 | 74 // TODO: |
75 // + Allow to abort MD2 calculation (have to restart next time). | |
76 // | |
77 // Literature: | |
200 | 78 // Bühlmann, Albert: Tauchmedizin; 4. Auflage [2002]; |
116 | 79 // Schr"oder, Kai & Reith, Steffen; 2000; S"attigungsvorg"ange beim Tauchen, das Modell ZH-L16, Funktionsweise von Tauchcomputern; http://www.achim-und-kai.de/kai/tausim/saett_faq |
80 // Morrison, Stuart; 2000; DIY DECOMPRESSION; http://www.lizardland.co.uk/DIYDeco.html | |
81 // Balthasar, Steffen; Dekompressionstheorie I: Neo Haldane Modelle; http://www.txfreak.de/dekompressionstheorie_1.pdf | |
82 // Baker, Erik C.; Clearing Up The Confusion About "Deep Stops" | |
83 // Baker, Erik C.; Understanding M-values; http://www.txfreak.de/understanding_m-values.pdf | |
167 | 84 // |
85 // | |
116 | 86 |
87 // ********************* | |
88 // ** I N C L U D E S ** | |
89 // ********************* | |
90 #include <math.h> | |
167 | 91 |
192 | 92 // *********************************************** |
93 // ** V A R I A B L E S D E F I N I T I O N S ** | |
94 // *********************************************** | |
95 | |
96 #include "p2_definitions.h" | |
97 #include "shared_definitions.h" | |
98 | |
200 | 99 // Water vapour partial pressure in the lumb. |
100 static const float ppWVapour = 0.0627; | |
101 | |
167 | 102 // ************************* |
103 // ** P R O T O T Y P E S ** | |
104 // ************************* | |
105 | |
106 static void calc_hauptroutine(void); | |
107 static void calc_nullzeit(void); | |
108 | |
192 | 109 static void calc_tissue(PARAMETER unsigned char period); |
110 static void calc_limit(PARAMETER float GF_current); | |
111 | |
167 | 112 static void clear_tissue(void); |
113 static void calc_ascenttime(void); | |
114 static void update_startvalues(void); | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
115 static void clear_deco_table(void); |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
116 static void update_deco_table(void); |
192 | 117 |
118 static void backup_sim_pres_tissue(void); | |
119 static void restore_sim_pres_tissue(void); | |
120 static void sim_tissue(PARAMETER unsigned char period); | |
121 static void sim_limit(PARAMETER float GF_current); | |
167 | 122 static void calc_gradient_factor(void); |
123 static void calc_wo_deco_step_1_min(void); | |
124 | |
125 static void calc_hauptroutine_data_input(void); | |
126 static void calc_hauptroutine_update_tissues(void); | |
127 static void calc_hauptroutine_calc_deco(void); | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
128 static void sim_ascent_to_first_stop(void); |
167 | 129 |
171 | 130 static void calc_nextdecodepth(void); |
116 | 131 |
200 | 132 //---- Bank 4 parameters ----------------------------------------------------- |
163 | 133 #pragma udata bank4=0x400 |
116 | 134 |
192 | 135 static unsigned char low_depth; |
116 | 136 static float temp_limit; |
137 static float GF_low; | |
138 static float GF_high; | |
139 static float GF_delta; | |
192 | 140 static unsigned char low_depth; // Depth of deepest stop |
141 static float locked_GF_step; // GF_delta / low_depth | |
142 | |
143 static unsigned char temp_depth_limit; | |
171 | 144 |
192 | 145 // Simulation context: used to predict ascent. |
146 static unsigned char sim_lead_tissue_no; // Leading compatiment number. | |
147 static float sim_lead_tissue_limit; // Buhlmann tolerated pressure. | |
116 | 148 |
192 | 149 // Real context: what we are doing now. |
150 static float calc_lead_tissue_limit; // | |
116 | 151 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
152 static unsigned char internal_deco_time[32]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
153 static unsigned char internal_deco_depth[32]; |
116 | 154 |
155 static float cns_vault; | |
156 static float pres_tissue_vault[32]; | |
157 | |
163 | 158 //---- Bank 5 parameters ----------------------------------------------------- |
159 #pragma udata bank5=0x500 | |
160 | |
167 | 161 static unsigned char ci; |
116 | 162 static float pres_respiration; |
163 static float pres_surface; | |
164 static float temp_deco; | |
200 | 165 static float ppO2; |
166 static float ppHe; | |
116 | 167 static float temp_tissue; |
200 | 168 static float N2_ratio; // Breathed gas nitrogen ratio. |
169 static float He_ratio; // Breathed gas helium ratio. | |
170 static float var_N2_a; // Bühlmann a, for current N2 tissue. | |
171 static float var_N2_b; // Bühlmann b, for current N2 tissue. | |
172 static float var_He_a; // Bühlmann a, for current He tissue. | |
173 static float var_He_b; // Bühlmann b, for current He tissue. | |
174 static float var_N2_e; // Exposition, for current N2 tissue. | |
175 static float var_He_e; // Exposition, for current He tissue. | |
116 | 176 |
164 | 177 static float pres_diluent; // new in v.101 |
178 static float deco_diluent; // new in v.101 | |
179 static float const_ppO2; // new in v.101 | |
180 static float deco_ppO2_change; // new in v.101 | |
181 static float deco_ppO2; // new in v.101 | |
116 | 182 |
200 | 183 static unsigned char sim_gas_last_used; // Last used gas, to detected a gas switch. |
184 static unsigned char sim_gas_delay; // Delay added for gas switch (count down) [min]. | |
185 static float calc_N2_ratio; // Simulated (switched) nitrogen ratio. | |
186 static float calc_He_ratio; // Simulated (switched) helium ratio. | |
187 static float CNS_fraction; // new in v.101 | |
188 static float float_saturation_multiplier; // new in v.101 | |
189 static float float_desaturation_multiplier; // new in v.101 | |
167 | 190 static float float_deco_distance; // new in v.101 |
191 static char flag_in_divemode; // new in v.108 | |
192 | |
193 static float deco_gas_change1; // new in v.101 | |
194 static float deco_gas_change2; // new in v.109 | |
195 static float deco_gas_change3; // new in v.109 | |
196 static float deco_gas_change4; // new in v.109 | |
197 static float deco_gas_change5; // new in v.109 | |
198 | |
199 static float deco_N2_ratio1; // new in v.101 | |
200 static float deco_He_ratio1; // new in v.101 | |
201 | |
202 | |
163 | 203 //---- Bank 6 parameters ----------------------------------------------------- |
204 #pragma udata bank6=0x600 | |
116 | 205 |
197 | 206 float pres_tissue[32]; |
207 float pres_tissue_limit[16]; | |
208 float sim_pres_tissue_limit[16]; | |
116 | 209 |
163 | 210 //---- Bank 7 parameters ----------------------------------------------------- |
211 #pragma udata bank7=0x700 | |
212 | |
197 | 213 float sim_pres_tissue[32]; // 32 floats = 128 bytes. |
116 | 214 static float sim_pres_tissue_backup[32]; |
215 | |
163 | 216 //---- Bank 8 parameters ----------------------------------------------------- |
116 | 217 #pragma udata bank8=0x800 |
163 | 218 |
116 | 219 static char md_pi_subst[256]; |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
220 #define C_STACK md_pi_subst // Overlay C-code data stack here, too. |
116 | 221 |
163 | 222 //---- Bank 9 parameters ----------------------------------------------------- |
167 | 223 #pragma udata bank9=0x900 |
116 | 224 |
163 | 225 static char md_state[48]; // DONT MOVE !! // has to be at the beginning of bank 9 for the asm code!!! |
116 | 226 |
164 | 227 // internal, dbg: |
167 | 228 static unsigned char DBG_char_I_deco_model; // new in v.108. |
164 | 229 static unsigned char DBG_char_I_depth_last_deco; // new in v.108 |
230 static float DBG_pres_surface; // new in v.108 | |
231 static float DBG_GF_low; // new in v.108 | |
232 static float DBG_GF_high; // new in v.108 | |
233 static float DBG_const_ppO2; // new in v.108 | |
234 static float DBG_deco_ppO2_change; // new in v.108 | |
235 static float DBG_deco_ppO2; // new in v.108 | |
236 static float DBG_deco_N2_ratio; // new in v.108 | |
237 static float DBG_deco_He_ratio; // new in v.108 | |
238 static float DBG_deco_gas_change; // new in v.108 | |
239 static float DBG_float_saturation_multiplier; // new in v.108 | |
240 static float DBG_float_desaturation_multiplier; // new in v.108 | |
241 static float DBG_float_deco_distance; // new in v.108 | |
242 static float DBG_deco_N2_ratio; // new in v.108 | |
243 static float DBG_deco_He_ratio; // new in v.108 | |
244 static float DBG_N2_ratio; // new in v.108 | |
245 static float DBG_He_ratio; // new in v.108 | |
246 | |
167 | 247 ////////////////////////////////////////////////////////////////////////////// |
248 ////////////////////////////////////////////////////////////////////////////// | |
249 ///////////////////////////// THE LOOKUP TABLES ////////////////////////////// | |
250 ////////////////////////////////////////////////////////////////////////////// | |
251 ////////////////////////////////////////////////////////////////////////////// | |
252 // | |
253 // End of PROM code is 17F00, So push tables on PROM top... | |
254 // | |
255 #pragma romdata buhlmann_tables = 0x017B00 // Needs to be in UPPER bank. | |
256 #include "p2_tables.romdata" // new table for deco_main_v.101 (var_N2_a modified) | |
116 | 257 |
167 | 258 // Magic table to compute the MD2 HASH |
259 // | |
260 #pragma romdata hash_tables = 0x017E00 // Address fixed by ASM access... | |
116 | 261 rom const rom unsigned int md_pi[] = |
262 { | |
263 0x292E, 0x43C9, 0xA2D8, 0x7C01, 0x3D36, 0x54A1, 0xECF0, 0x0613 | |
264 , 0x62A7, 0x05F3, 0xC0C7, 0x738C, 0x9893, 0x2BD9, 0xBC4C, 0x82CA | |
265 , 0x1E9B, 0x573C, 0xFDD4, 0xE016, 0x6742, 0x6F18, 0x8A17, 0xE512 | |
266 , 0xBE4E, 0xC4D6, 0xDA9E, 0xDE49, 0xA0FB, 0xF58E, 0xBB2F, 0xEE7A | |
267 , 0xA968, 0x7991, 0x15B2, 0x073F, 0x94C2, 0x1089, 0x0B22, 0x5F21 | |
268 , 0x807F, 0x5D9A, 0x5A90, 0x3227, 0x353E, 0xCCE7, 0xBFF7, 0x9703 | |
269 , 0xFF19, 0x30B3, 0x48A5, 0xB5D1, 0xD75E, 0x922A, 0xAC56, 0xAAC6 | |
270 , 0x4FB8, 0x38D2, 0x96A4, 0x7DB6, 0x76FC, 0x6BE2, 0x9C74, 0x04F1 | |
271 , 0x459D, 0x7059, 0x6471, 0x8720, 0x865B, 0xCF65, 0xE62D, 0xA802 | |
272 , 0x1B60, 0x25AD, 0xAEB0, 0xB9F6, 0x1C46, 0x6169, 0x3440, 0x7E0F | |
273 , 0x5547, 0xA323, 0xDD51, 0xAF3A, 0xC35C, 0xF9CE, 0xBAC5, 0xEA26 | |
274 , 0x2C53, 0x0D6E, 0x8528, 0x8409, 0xD3DF, 0xCDF4, 0x4181, 0x4D52 | |
275 , 0x6ADC, 0x37C8, 0x6CC1, 0xABFA, 0x24E1, 0x7B08, 0x0CBD, 0xB14A | |
276 , 0x7888, 0x958B, 0xE363, 0xE86D, 0xE9CB, 0xD5FE, 0x3B00, 0x1D39 | |
277 , 0xF2EF, 0xB70E, 0x6658, 0xD0E4, 0xA677, 0x72F8, 0xEB75, 0x4B0A | |
278 , 0x3144, 0x50B4, 0x8FED, 0x1F1A, 0xDB99, 0x8D33, 0x9F11, 0x8314 | |
279 }; | |
280 | |
167 | 281 ////////////////////////////////////////////////////////////////////////////// |
282 ////////////////////////////////////////////////////////////////////////////// | |
283 ////////////////////////////// THE SUBROUTINES /////////////////////////////// | |
284 ////////////////////////////////////////////////////////////////////////////// | |
285 ////////////////////////////////////////////////////////////////////////////// | |
286 // | |
116 | 287 // all new in v.102 |
288 // moved from 0x0D000 to 0x0C000 in v.108 | |
289 | |
290 #pragma code p2_deco = 0x0C000 | |
291 | |
167 | 292 ////////////////////////////////////////////////////////////////////////////// |
293 // DBS - debug on start of dive | |
294 // | |
295 static void create_dbs_set_dbg_and_ndl20mtr(void) | |
116 | 296 { |
167 | 297 overlay char i; // Local loop index. |
298 | |
299 //---- Reset DEBUG bit fields -------------------------------------------- | |
116 | 300 int_O_DBS_bitfield = 0; |
301 int_O_DBS2_bitfield = 0; | |
302 if(int_O_DBG_pre_bitfield & DBG_RUN) | |
303 int_O_DBG_pre_bitfield = DBG_RESTART; | |
304 else | |
305 int_O_DBG_pre_bitfield = DBG_RUN; | |
306 int_O_DBG_post_bitfield = 0; | |
167 | 307 |
308 //---- Set 20meters ND limit --------------------------------------------- | |
116 | 309 char_O_NDL_at_20mtr = 255; |
310 | |
167 | 311 //---- Copy all dive parameters ------------------------------------------ |
116 | 312 DBG_N2_ratio = N2_ratio; |
313 DBG_He_ratio = He_ratio; | |
314 DBG_char_I_deco_model = char_I_deco_model; | |
315 DBG_char_I_depth_last_deco = char_I_depth_last_deco; | |
316 DBG_pres_surface = pres_surface; | |
317 DBG_GF_low = GF_low; | |
318 DBG_GF_high = GF_high; | |
319 DBG_const_ppO2 = const_ppO2; | |
320 DBG_deco_ppO2_change = deco_ppO2_change; | |
321 DBG_deco_ppO2 = deco_ppO2; | |
167 | 322 DBG_deco_N2_ratio = deco_N2_ratio1; |
323 DBG_deco_He_ratio = deco_He_ratio1; | |
324 DBG_deco_gas_change = deco_gas_change1; | |
116 | 325 DBG_float_saturation_multiplier = float_saturation_multiplier; |
326 DBG_float_desaturation_multiplier = float_desaturation_multiplier; | |
327 DBG_float_deco_distance = float_deco_distance; | |
328 | |
167 | 329 //---- Setup some error (?) conditions ----------------------------------- |
116 | 330 if(char_I_deco_model) |
331 int_O_DBS_bitfield |= DBS_mode; | |
332 if(const_ppO2) | |
333 int_O_DBS_bitfield |= DBS_ppO2; | |
167 | 334 for(i = 16; i < 32; i++) |
335 if(pres_tissue[i]) | |
116 | 336 int_O_DBS_bitfield |= DBS_HE_sat; |
337 if(deco_ppO2_change) | |
338 int_O_DBS_bitfield |= DBS_ppO2chg; | |
339 if(float_saturation_multiplier < 0.99) | |
340 int_O_DBS_bitfield |= DBS_SAT2l; | |
341 if(float_saturation_multiplier > 1.3) | |
342 int_O_DBS_bitfield |= DBS_SAT2h; | |
343 if(GF_low < 0.19) | |
344 int_O_DBS_bitfield |= DBS_GFLOW2l; | |
345 if(GF_low > 1.01) | |
346 int_O_DBS_bitfield |= DBS_GFLOW2h; | |
347 if(GF_high < 0.6) | |
348 int_O_DBS_bitfield |= DBS_GFHGH2l; | |
349 if(GF_high > 1.01) | |
350 int_O_DBS_bitfield |= DBS_GFHGH2h; | |
351 if((N2_ratio + He_ratio) > 0.95) | |
352 int_O_DBS_bitfield |= DBS_GASO22l; | |
353 if((N2_ratio + He_ratio) < 0.05) | |
354 int_O_DBS_bitfield |= DBS_GASO22h; | |
355 if(float_deco_distance > 0.25) | |
356 int_O_DBS_bitfield |= DBS_DIST2h; | |
357 if(char_I_depth_last_deco > 8) | |
358 int_O_DBS_bitfield |= DBS_LAST2h; | |
167 | 359 if(DBG_deco_gas_change && ((deco_N2_ratio1 + deco_He_ratio1) > 0.95)) |
116 | 360 int_O_DBS_bitfield |= DBS_DECOO2l; |
167 | 361 if(DBG_deco_gas_change && ((deco_N2_ratio1 + deco_He_ratio1) < 0.05)) |
116 | 362 int_O_DBS_bitfield |= DBS_DECOO2h; |
363 if(pres_respiration > 3.0) | |
364 int_O_DBS2_bitfield |= DBS2_PRES2h; | |
365 if(pres_surface - pres_respiration > 0.2) | |
366 int_O_DBS2_bitfield |= DBS2_PRES2l; | |
367 if(pres_surface < 0.75) | |
368 int_O_DBS2_bitfield |= DBS2_SURF2l; | |
369 if(pres_surface > 1.11) | |
370 int_O_DBS2_bitfield |= DBS2_SURF2h; | |
371 if(float_desaturation_multiplier < 0.70) | |
372 int_O_DBS2_bitfield |= DBS2_DESAT2l; | |
373 if(float_desaturation_multiplier > 1.01) | |
374 int_O_DBS2_bitfield |= DBS2_DESAT2h; | |
375 if(GF_low > GF_high) | |
376 int_O_DBS2_bitfield |= DBS2_GFDneg; | |
377 } | |
378 | |
167 | 379 ////////////////////////////////////////////////////////////////////////////// |
380 // DBG - set DBG to end_of_dive | |
381 // | |
382 static void set_dbg_end_of_dive(void) | |
116 | 383 { |
384 int_O_DBG_pre_bitfield &= (~DBG_RUN); | |
385 int_O_DBG_post_bitfield &= (~DBG_RUN); | |
386 } | |
387 | |
167 | 388 ////////////////////////////////////////////////////////////////////////////// |
389 // DBG - NDL at first 20 m. hit | |
390 // | |
391 static void check_ndl(void) | |
116 | 392 { |
167 | 393 if( char_O_NDL_at_20mtr == 255 // Still in NDL mode ? |
394 && int_I_pres_respiration > 3000 // And we hit the 20m limit ? | |
395 ) | |
116 | 396 { |
167 | 397 char_O_NDL_at_20mtr = char_O_nullzeit; // change to max bottom time. |
398 if( char_O_NDL_at_20mtr == 255) // and avoid confusion. | |
184 | 399 char_O_NDL_at_20mtr = 254; |
116 | 400 } |
401 } | |
402 | |
167 | 403 ////////////////////////////////////////////////////////////////////////////// |
404 // DBG - multi main during dive | |
405 // | |
184 | 406 static void check_dbg(PARAMETER char is_post_check) |
116 | 407 { |
167 | 408 overlay unsigned int temp_DBS = 0; |
200 | 409 overlay unsigned char i; // Local loop index. |
167 | 410 |
116 | 411 if( (DBG_N2_ratio != N2_ratio) || (DBG_He_ratio != He_ratio) ) |
412 temp_DBS |= DBG_c_gas; | |
413 if(DBG_const_ppO2 != const_ppO2) | |
414 temp_DBS |= DBG_c_ppO2; | |
167 | 415 if( DBG_float_saturation_multiplier != float_saturation_multiplier |
416 || DBG_float_desaturation_multiplier != float_desaturation_multiplier | |
417 ) | |
116 | 418 temp_DBS |= DBG_CdeSAT; |
419 if(DBG_char_I_deco_model != char_I_deco_model) | |
420 temp_DBS |= DBG_C_MODE; | |
421 if(DBG_pres_surface != pres_surface) | |
422 temp_DBS |= DBG_C_SURF; | |
167 | 423 |
424 if( !DBS_HE_sat && !He_ratio) | |
425 for(i = 16; i < 32; i++) | |
426 if(pres_tissue[i]) | |
116 | 427 temp_DBS |= DBG_HEwoHE; |
167 | 428 |
116 | 429 if(DBG_deco_ppO2 != deco_ppO2) |
430 temp_DBS |= DBG_C_DPPO2; | |
167 | 431 |
432 if( DBG_deco_gas_change != deco_gas_change1 | |
433 || DBG_deco_N2_ratio != deco_N2_ratio1 | |
434 || DBG_deco_He_ratio != deco_He_ratio1 ) | |
116 | 435 temp_DBS |= DBG_C_DGAS; |
167 | 436 |
116 | 437 if(DBG_float_deco_distance != float_deco_distance) |
438 temp_DBS |= DBG_C_DIST; | |
439 if(DBG_char_I_depth_last_deco != char_I_depth_last_deco) | |
440 temp_DBS |= DBG_C_LAST; | |
167 | 441 if( DBG_GF_low != GF_low |
442 || DBG_GF_high != GF_high ) | |
116 | 443 temp_DBS |= DBG_C_GF; |
444 if(pres_respiration > 13.0) | |
445 temp_DBS |= DBG_PHIGH; | |
446 if(pres_surface - pres_respiration > 0.2) | |
447 temp_DBS |= DBG_PLOW; | |
448 if(is_post_check) | |
449 int_O_DBG_post_bitfield |= temp_DBS; | |
450 else | |
451 int_O_DBG_pre_bitfield |= temp_DBS; | |
452 } | |
453 | |
167 | 454 ////////////////////////////////////////////////////////////////////////////// |
455 // DBG - prior to calc. of dive | |
456 // | |
457 static void check_pre_dbg(void) | |
116 | 458 { |
459 check_dbg(0); | |
460 } | |
461 | |
167 | 462 ////////////////////////////////////////////////////////////////////////////// |
463 // DBG - after decocalc of dive | |
464 // | |
465 static void check_post_dbg(void) | |
116 | 466 { |
467 check_dbg(1); | |
468 } | |
469 | |
165 | 470 ////////////////////////////////////////////////////////////////////////////// |
167 | 471 ////////////////////////////////////////////////////////////////////////////// |
472 /////////////////////// U T I L I T I E S ///////////////////////////////// | |
473 ////////////////////////////////////////////////////////////////////////////// | |
474 ////////////////////////////////////////////////////////////////////////////// | |
475 | |
184 | 476 static int read_custom_function(PARAMETER unsigned char cf) |
167 | 477 { |
184 | 478 #ifdef CROSS_COMPILE |
479 extern unsigned short custom_functions[]; | |
480 return custom_functions[cf]; | |
481 #else | |
167 | 482 extern unsigned char hi, lo; |
483 extern void getcustom15(); | |
484 _asm | |
485 movff cf,WREG | |
486 call getcustom15,0 | |
487 movff lo,PRODL | |
488 movff hi,PRODH | |
489 _endasm | |
184 | 490 #endif |
167 | 491 } |
492 | |
493 ////////////////////////////////////////////////////////////////////////////// | |
165 | 494 // read buhlmann tables for compatriment ci |
167 | 495 // If period == 0 : 2sec interval |
496 // 1 : 1 min interval | |
497 // 2 : 10 min interval. | |
184 | 498 static void read_buhlmann_coefficients(PARAMETER char period) |
165 | 499 { |
184 | 500 #ifndef CROSS_COMPILE |
167 | 501 // Note: we don't use far rom pointer, because the |
502 // 24 bits is to complex, hence we have to set | |
503 // the UPPER page ourself... | |
504 // --> Set zero if tables are moved to lower pages ! | |
505 _asm | |
506 movlw 1 | |
507 movwf TBLPTRU,0 | |
508 _endasm | |
184 | 509 #endif |
197 | 510 assert( 0 <= ci && ci < 16 ); |
167 | 511 |
165 | 512 var_N2_a = buhlmann_a[ci]; |
513 var_N2_b = buhlmann_b[ci]; | |
167 | 514 var_He_a = (buhlmann_a+16)[ci]; |
515 var_He_b = (buhlmann_b+16)[ci]; | |
516 | |
165 | 517 // Check reading consistency: |
518 if( (var_N2_a < 0.231) | |
519 || (var_N2_a > 1.27) | |
520 || (var_N2_b < 0.504) | |
521 || (var_N2_b > 0.966) | |
522 || (var_He_a < 0.510) | |
523 || (var_He_a > 1.75) | |
524 || (var_He_b < 0.423) | |
525 || (var_He_b > 0.927) | |
526 ) | |
527 int_O_DBG_pre_bitfield |= DBG_ZH16ERR; | |
167 | 528 |
165 | 529 // Integration intervals. |
167 | 530 switch(period) |
165 | 531 { |
167 | 532 case -1://---- no interval ----------------------------------------------- |
533 var_N2_e = var_He_e = 0.0; | |
534 break; | |
535 | |
165 | 536 case 0: //---- 2 sec ----------------------------------------------------- |
537 var_N2_e = e2secs[ci]; | |
167 | 538 var_He_e = (e2secs+16)[ci]; |
165 | 539 |
540 // Check reading consistency: | |
541 if( (var_N2_e < 0.0000363) | |
542 || (var_N2_e > 0.00577) | |
543 || (var_He_e < 0.0000961) | |
544 || (var_He_e > 0.150) | |
545 ) | |
546 int_O_DBG_pre_bitfield |= DBG_ZH16ERR; | |
547 | |
548 break; | |
549 | |
550 case 1: //---- 1 min ----------------------------------------------------- | |
551 var_N2_e = e1min[ci]; | |
167 | 552 var_He_e = (e1min+16)[ci]; |
165 | 553 |
554 // Check reading consistency: | |
555 if( (var_N2_e < 1.09E-3) | |
556 || (var_N2_e > 0.1592) | |
557 || (var_He_e < 0.00288) | |
558 || (var_He_e > 0.3682) | |
559 ) | |
560 int_O_DBG_pre_bitfield |= DBG_ZH16ERR; | |
561 | |
562 break; | |
563 | |
564 case 2: //---- 10 min ---------------------------------------------------- | |
565 var_N2_e = e10min[ci]; | |
167 | 566 var_He_e = (e10min+16)[ci]; |
165 | 567 |
568 // Check reading consistency: | |
569 if( (var_N2_e < 0.01085) | |
570 || (var_N2_e > 0.82323) | |
571 || (var_He_e < 0.02846) | |
572 || (var_He_e > 0.98986) | |
573 ) | |
574 int_O_DBG_pre_bitfield |= DBG_ZH16ERR; | |
575 | |
576 break; | |
197 | 577 |
578 default: | |
579 assert(0); // Never go there... | |
165 | 580 } |
581 } | |
582 | |
583 ////////////////////////////////////////////////////////////////////////////// | |
167 | 584 // calc_next_decodepth_GF |
165 | 585 // |
116 | 586 // new in v.102 |
165 | 587 // |
116 | 588 // INPUT, changing during dive: |
192 | 589 // low_depth |
167 | 590 // |
116 | 591 // INPUT, fixed during dive: |
167 | 592 // pres_surface |
593 // GF_delta | |
594 // GF_high | |
595 // GF_low | |
171 | 596 // char_I_depth_last_deco |
167 | 597 // float_deco_distance |
598 // | |
116 | 599 // OUTPUT |
171 | 600 // locked_GF_step |
167 | 601 // temp_deco |
602 // temp_depth_limt | |
192 | 603 // low_depth |
167 | 604 // |
171 | 605 static void calc_nextdecodepth(void) |
167 | 606 { |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
607 //---- ZH-L16 + GRADIENT FACTOR model ------------------------------------ |
116 | 608 if (char_I_deco_model == 1) |
609 { | |
192 | 610 // Recompute leading gas limit, at current depth: |
611 overlay float depth = (temp_deco - pres_surface) / 0.09995; | |
612 assert( depth >= 0.0 ); | |
167 | 613 |
192 | 614 if( depth > low_depth ) |
615 sim_limit( GF_low ); | |
616 else | |
617 sim_limit( GF_high - depth * locked_GF_step ); | |
618 | |
619 // Stops are needed ? | |
620 if( sim_lead_tissue_limit > pres_surface ) | |
621 { | |
622 // Deepest stop, in meter. | |
623 overlay unsigned char first_stop = 3 * (int)(0.99 + (sim_lead_tissue_limit - pres_surface) / 0.29985); | |
624 assert( first_stop < 128 ); | |
171 | 625 |
192 | 626 // Apply correction for the first stop. |
627 if( first_stop == 3 ) // new in v104 | |
628 first_stop = char_I_depth_last_deco; // Use last 3m..6m instead. | |
171 | 629 |
192 | 630 // Is it a new deepest first stop ? If yes this is the reference for |
631 // the varying gradient factor. So reset that: | |
632 if( first_stop > low_depth ) | |
633 { | |
634 // Store the deepest stop depth, as reference for GF_low. | |
635 low_depth = first_stop; | |
636 locked_GF_step = GF_delta / low_depth; | |
637 } | |
171 | 638 |
192 | 639 // Check all stops until one is higher than tolerated presure |
640 while(first_stop > 0) | |
641 { | |
642 overlay unsigned char next_stop; // Next index (0..30) | |
643 overlay float pres_stop; // Next depth (0m..90m) | |
171 | 644 |
192 | 645 if( first_stop <= char_I_depth_last_deco ) // new in v104 |
646 next_stop = 0; | |
647 else | |
648 next_stop = first_stop - 3; // Index of next (upper) stop. | |
649 if( first_stop == 3 ) | |
650 first_stop = char_I_depth_last_deco; | |
167 | 651 |
192 | 652 pres_stop = next_stop * 0.09995 // Meters to bar |
653 + pres_surface; | |
654 | |
655 // current GF is GF_high - alpha (GF_high - GF_low) | |
656 // With alpha = currentDepth / maxDepth, hence in [0..1] | |
657 sim_limit( GF_high - next_stop * locked_GF_step ); | |
171 | 658 |
192 | 659 // upper limit (lowest pressure tolerated): |
660 if( sim_lead_tissue_limit >= pres_stop ) // check if ascent to next deco stop is ok | |
661 break; | |
662 | |
663 // Else, validate that stop and loop... | |
664 first_stop = next_stop; | |
665 } | |
171 | 666 |
192 | 667 // first_stop is the last validated depth found: |
668 temp_depth_limit = first_stop; // Stop depth, in meter. | |
669 temp_deco = first_stop * 0.09995 // Convert to bars, too. | |
670 + pres_surface; | |
671 if (first_stop > 0) | |
672 temp_deco += float_deco_distance; // Add security distance (bars too) | |
673 } | |
171 | 674 else |
192 | 675 { |
676 temp_deco = pres_surface; // surface ambient presure | |
677 temp_depth_limit = 0; // stop depth, in meter. | |
678 } | |
116 | 679 } |
167 | 680 else //---- ZH-L16 model ------------------------------------------------- |
116 | 681 { |
192 | 682 overlay float pres_gradient; |
683 | |
684 // Original model | |
116 | 685 // optimized in v.101 |
171 | 686 // char_I_depth_last_deco included in v.101 |
116 | 687 |
192 | 688 // Compute sim_lead_tissue_limit too, but just once. |
689 sim_limit(1.0); | |
690 | |
691 pres_gradient = sim_lead_tissue_limit - pres_surface; | |
167 | 692 if (pres_gradient >= 0) |
116 | 693 { |
192 | 694 pres_gradient /= 0.29985; // Bar --> stop number; |
695 temp_depth_limit = 3 * (int) (pres_gradient + 0.99); // --> meter : depth for deco | |
171 | 696 if (temp_depth_limit == 0) // At surface ? |
116 | 697 temp_deco = pres_surface; |
698 else | |
699 { | |
171 | 700 if (temp_depth_limit < char_I_depth_last_deco) // Implement last stop at 4m/5m/6m... |
701 temp_depth_limit = char_I_depth_last_deco; | |
702 temp_deco = (temp_depth_limit * 0.09995) // depth for deco [bar] = depth | |
703 + float_deco_distance // + security margin | |
704 + pres_surface; // + surface ambient presure | |
705 } | |
706 } | |
116 | 707 else |
708 { | |
171 | 709 temp_deco = pres_surface; // surface ambient presure |
192 | 710 temp_depth_limit = 0; // stop depth, in meter. |
171 | 711 } |
712 } | |
167 | 713 } |
116 | 714 |
167 | 715 ////////////////////////////////////////////////////////////////////////////// |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
716 // copy_deco_table |
167 | 717 // |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
718 // Buffer the stops, once computed, so we can continue to display them |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
719 // while computing the next set. |
167 | 720 // |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
721 static void copy_deco_table(void) |
116 | 722 { |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
723 // Copy depth of the first (deepest) stop, because when reversing |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
724 // order, it will be hard to find... |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
725 char_O_first_deco_depth = internal_deco_depth[0]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
726 char_O_first_deco_time = internal_deco_time [0]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
727 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
728 if( read_custom_function(54) & 1 ) //---- Should we reverse table ? ------ |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
729 { |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
730 overlay unsigned char x, y; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
731 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
732 //---- First: search the first non-null depth |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
733 for(x=31; x != 0; --x) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
734 if( internal_deco_depth[x] != 0 ) break; |
116 | 735 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
736 //---- Second: copy to output table (in reverse order) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
737 for(y=0; y<32; y++, --x) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
738 { |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
739 char_O_deco_depth[y] = internal_deco_depth[x]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
740 char_O_deco_time [y] = internal_deco_time [x]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
741 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
742 // Stop only once the last transfer is done. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
743 if( x == 0 ) break; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
744 } |
116 | 745 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
746 //---- Third: fill table end with null |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
747 for(y++; y<32; y++) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
748 { |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
749 char_O_deco_time [y] = 0; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
750 char_O_deco_depth[y] = 0; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
751 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
752 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
753 else //---- Straight copy ------------------------------------------------ |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
754 { |
167 | 755 overlay unsigned char x; |
756 | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
757 for(x=0; x<32; x++) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
758 { |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
759 char_O_deco_depth[x] = internal_deco_depth[x]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
760 char_O_deco_time [x] = internal_deco_time [x]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
761 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
762 } |
167 | 763 } |
116 | 764 |
167 | 765 ////////////////////////////////////////////////////////////////////////////// |
116 | 766 // temp_tissue_safety // |
167 | 767 // |
116 | 768 // outsourced in v.102 |
167 | 769 // |
770 // Apply safety factors for brand ZH-L16 model. | |
771 // | |
772 static void temp_tissue_safety(void) | |
116 | 773 { |
197 | 774 assert( 0.0 < float_desaturation_multiplier && float_desaturation_multiplier <= 1.0 ); |
775 assert( 1.0 <= float_saturation_multiplier && float_saturation_multiplier <= 2.0 ); | |
776 | |
167 | 777 if( char_I_deco_model == 0 ) |
116 | 778 { |
779 if (temp_tissue < 0.0) | |
126 | 780 temp_tissue *= float_desaturation_multiplier; |
116 | 781 else |
126 | 782 temp_tissue *= float_saturation_multiplier; |
116 | 783 } |
167 | 784 } |
116 | 785 |
167 | 786 ////////////////////////////////////////////////////////////////////////////// |
787 ////////////////////////////////////////////////////////////////////////////// | |
116 | 788 // ** THE JUMP-IN CODE ** |
789 // ** for the asm code ** | |
167 | 790 ////////////////////////////////////////////////////////////////////////////// |
791 ////////////////////////////////////////////////////////////////////////////// | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
792 |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
793 void fillDataStack(void) |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
794 { |
184 | 795 #ifndef CROSS_COMPILE |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
796 _asm |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
797 LFSR 1,C_STACK |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
798 MOVLW 0xCC |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
799 loop: MOVWF POSTINC1,0 |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
800 TSTFSZ FSR1L,0 |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
801 BRA loop |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
802 |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
803 LFSR 1,C_STACK |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
804 LFSR 2,C_STACK |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
805 _endasm |
184 | 806 #endif |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
807 } |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
808 |
167 | 809 ////////////////////////////////////////////////////////////////////////////// |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
810 // When calling C code from ASM context, the data stack pointer and |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
811 // frames should be reset. Bank3 is dedicated to the stack (see the |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
812 // .lkr script). |
184 | 813 #ifdef CROSS_COMPILE |
814 # define RESET_C_STACK | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
815 #else |
184 | 816 # ifdef __DEBUG |
817 # define RESET_C_STACK fillDataStack(); | |
818 # else | |
819 # define RESET_C_STACK \ | |
820 _asm \ | |
821 LFSR 1, C_STACK \ | |
822 LFSR 2, C_STACK \ | |
823 _endasm | |
824 # endif | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
825 #endif |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
826 |
167 | 827 ////////////////////////////////////////////////////////////////////////////// |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
828 // Called every 2 seconds during diving. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
829 // update tissues every time. |
184 | 830 // |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
831 // Every 6 seconds (or slower when TTS > 16): |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
832 // - update deco table (char_O_deco_time/depth) with new values. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
833 // - update ascent time, |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
834 // - set status to zero (so we can check there is new results). |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
835 // |
116 | 836 void deco_calc_hauptroutine(void) |
837 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
838 RESET_C_STACK |
116 | 839 calc_hauptroutine(); |
840 int_O_desaturation_time = 65535; | |
841 } | |
842 | |
167 | 843 ////////////////////////////////////////////////////////////////////////////// |
184 | 844 // Reset decompression model: |
845 // + Set all tissues to equilibrium with Air at ambient pressure. | |
846 // + Reset last stop to 0m | |
847 // + Reset all model output. | |
116 | 848 void deco_clear_tissue(void) |
849 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
850 RESET_C_STACK |
116 | 851 clear_tissue(); |
852 } | |
853 | |
167 | 854 ////////////////////////////////////////////////////////////////////////////// |
855 | |
116 | 856 void deco_calc_wo_deco_step_1_min(void) |
857 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
858 RESET_C_STACK |
116 | 859 calc_wo_deco_step_1_min(); |
860 deco_calc_desaturation_time(); | |
861 } | |
862 | |
167 | 863 ////////////////////////////////////////////////////////////////////////////// |
864 | |
116 | 865 void deco_debug(void) |
866 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
867 RESET_C_STACK |
116 | 868 } |
869 | |
167 | 870 ////////////////////////////////////////////////////////////////////////////// |
201 | 871 // Calculate gas switches |
872 // | |
873 // | |
874 void check_gas_switch(void) | |
875 { | |
876 overlay unsigned char temp_gas_switch = sim_gas_last_used; | |
877 overlay float switch_deco; | |
878 | |
879 calc_N2_ratio = N2_ratio; | |
880 calc_He_ratio = He_ratio; | |
881 | |
882 if (char_I_const_ppO2 == 0) | |
883 { | |
884 deco_diluent = temp_deco; | |
885 | |
886 // Keep selecting the best gas during the ascent simulation. | |
887 if( deco_gas_change1 && (temp_deco < deco_gas_change1)) | |
888 { | |
889 calc_N2_ratio = deco_N2_ratio1; | |
890 calc_He_ratio = deco_He_ratio1; | |
891 temp_gas_switch = 1; | |
892 switch_deco = deco_gas_change1; | |
893 } | |
894 if(deco_gas_change2 && (temp_deco < deco_gas_change2)) | |
895 { | |
896 calc_N2_ratio = char_I_deco_N2_ratio2 * 0.01; | |
897 calc_He_ratio = char_I_deco_He_ratio2 * 0.01; | |
898 temp_gas_switch = 2; | |
899 switch_deco = deco_gas_change2; | |
900 } | |
901 if(deco_gas_change3 && (temp_deco < deco_gas_change3)) | |
902 { | |
903 calc_N2_ratio = char_I_deco_N2_ratio3 * 0.01; | |
904 calc_He_ratio = char_I_deco_He_ratio3 * 0.01; | |
905 temp_gas_switch = 3; | |
906 switch_deco = deco_gas_change3; | |
907 } | |
908 if(deco_gas_change4 && (temp_deco < deco_gas_change4)) | |
909 { | |
910 calc_N2_ratio = char_I_deco_N2_ratio4 * 0.01; | |
911 calc_He_ratio = char_I_deco_He_ratio4 * 0.01; | |
912 temp_gas_switch = 4; | |
913 switch_deco = deco_gas_change4; | |
914 } | |
915 if(deco_gas_change5 && (temp_deco < deco_gas_change5)) | |
916 { | |
917 calc_N2_ratio = char_I_deco_N2_ratio5 * 0.01; | |
918 calc_He_ratio = char_I_deco_He_ratio5 * 0.01; | |
919 temp_gas_switch = 5; | |
920 switch_deco = deco_gas_change5; | |
921 } | |
922 } | |
923 | |
924 // Should detect gas switch only when gas do changes... | |
925 // sim_gas_last_used: used to detect just once in each ascent simu. | |
926 // N2_ratio : used to detect when already breathing that gas. | |
927 if( sim_gas_last_used < temp_gas_switch | |
928 && (calc_N2_ratio != N2_ratio || calc_He_ratio != He_ratio)) | |
929 { | |
930 sim_gas_last_used = temp_gas_switch; | |
931 sim_gas_delay = read_custom_function(55); | |
932 | |
933 // Apply depth correction ONLY if there delay is not null: | |
934 if( sim_gas_delay > 0 ) | |
935 { | |
936 temp_deco = switch_deco - float_deco_distance; | |
937 temp_depth_limit = (temp_deco - pres_surface) / 0.09985; | |
938 } | |
939 } | |
940 | |
941 assert( 0.0 <= calc_N2_ratio && calc_N2_ratio <= 0.95 ); | |
942 assert( 0.0 <= calc_He_ratio && calc_He_ratio <= 0.95 ); | |
943 assert( (calc_N2_ratio + calc_He_ratio) <= 1.00 ); | |
944 } | |
945 | |
946 ////////////////////////////////////////////////////////////////////////////// | |
947 // | |
948 static void alveolar_presures(void) | |
949 { | |
950 if (char_I_const_ppO2 != 0) | |
951 { | |
952 if (temp_deco > deco_ppO2_change) | |
953 { | |
954 deco_diluent = ((temp_deco - const_ppO2)/(N2_ratio + He_ratio)); | |
955 } | |
956 else | |
957 { | |
958 deco_diluent = ((temp_deco - deco_ppO2)/(N2_ratio + He_ratio)); | |
959 } | |
960 } | |
961 | |
962 if (deco_diluent > temp_deco) | |
963 deco_diluent = temp_deco; | |
964 if (deco_diluent > ppWVapour) | |
965 { | |
966 ppO2 = calc_N2_ratio * (deco_diluent - ppWVapour); | |
967 ppHe = calc_He_ratio * (deco_diluent - ppWVapour); | |
968 } | |
969 else | |
970 { | |
971 ppO2 = 0.0; | |
972 ppHe = 0.0; | |
973 } | |
974 assert( 0.0 <= ppO2 && ppO2 < 14.0 ); | |
975 assert( 0.0 <= ppHe && ppHe < 14.0 ); | |
976 } | |
977 | |
978 ////////////////////////////////////////////////////////////////////////////// | |
167 | 979 // clear_tissue |
980 // | |
165 | 981 // optimized in v.101 (var_N2_a) |
167 | 982 // |
983 // preload tissues with standard pressure for the given ambient pressure. | |
984 // Note: fixed N2_ratio for standard air. | |
201 | 985 // |
167 | 986 static void clear_tissue(void) |
116 | 987 { |
988 flag_in_divemode = 0; | |
989 int_O_DBS_bitfield = 0; | |
990 int_O_DBS2_bitfield = 0; | |
991 int_O_DBG_pre_bitfield = 0; | |
992 int_O_DBG_post_bitfield = 0; | |
993 char_O_NDL_at_20mtr = 255; | |
994 | |
167 | 995 // Kludge: the 0.0002 of 0.7902 are missing with standard air. |
996 N2_ratio = 0.7902; | |
126 | 997 pres_respiration = (float)int_I_pres_respiration / 1000.0; |
164 | 998 |
167 | 999 for(ci=0; ci<16; ci++) |
126 | 1000 { |
197 | 1001 // cycle through the 16 Bühlmann tissues |
200 | 1002 overlay float p = N2_ratio * (pres_respiration - ppWVapour); |
164 | 1003 pres_tissue[ci] = p; |
197 | 1004 |
184 | 1005 read_buhlmann_coefficients(-1); |
165 | 1006 |
1007 p = (p - var_N2_a) * var_N2_b ; | |
164 | 1008 if( p < 0.0 ) |
1009 p = 0.0; | |
1010 pres_tissue_limit[ci] = p; | |
1011 | |
197 | 1012 // cycle through the 16 Bühlmann tissues for Helium |
167 | 1013 (pres_tissue+16)[ci] = 0.0; |
126 | 1014 } // for 0 to 16 |
116 | 1015 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1016 clear_deco_table(); |
126 | 1017 char_O_deco_status = 0; |
1018 char_O_nullzeit = 0; | |
168 | 1019 int_O_ascenttime = 0; |
126 | 1020 char_O_gradient_factor = 0; |
1021 char_O_relative_gradient_GF = 0; | |
197 | 1022 char_I_depth_last_deco = 0; // for compatibility with v.101pre_no_last_deco |
167 | 1023 } |
116 | 1024 |
167 | 1025 ////////////////////////////////////////////////////////////////////////////// |
1026 // calc_hauptroutine | |
1027 // | |
1028 // this is the major code in dive mode calculates: | |
116 | 1029 // the tissues, |
167 | 1030 // the bottom time, |
1031 // and simulates the ascend with all deco stops. | |
171 | 1032 // |
1033 // The deco_state sequence is : | |
1034 // 3 (at surface) | |
1035 // +---> 0 : calc nullzeit | |
1036 // | 2 : simulate ascent to first stop (at 10m/min, less that 16x 1min simu) | |
1037 // | +-> 1 : simulate up to 16min of stops. | |
1038 // | +------< not finished | |
1039 // +--------< finish | |
1040 // | |
167 | 1041 static void calc_hauptroutine(void) |
116 | 1042 { |
186 | 1043 static float backup_GF_step; |
192 | 1044 static unsigned char backup_low_depth; |
201 | 1045 static unsigned char backup_gas_used; |
186 | 1046 |
116 | 1047 calc_hauptroutine_data_input(); |
1048 | |
1049 if(!flag_in_divemode) | |
1050 { | |
1051 flag_in_divemode = 1; | |
1052 create_dbs_set_dbg_and_ndl20mtr(); | |
1053 } | |
1054 else | |
1055 check_pre_dbg(); | |
1056 | |
1057 calc_hauptroutine_update_tissues(); | |
1058 calc_gradient_factor(); | |
1059 | |
167 | 1060 // toggle between calculation for nullzeit (bottom time), |
1061 // deco stops | |
1062 // and more deco stops (continue) | |
1063 switch( char_O_deco_status ) | |
116 | 1064 { |
186 | 1065 case 3: //---- At surface: start a new dive ------------------------------ |
1066 clear_deco_table(); | |
1067 copy_deco_table(); | |
200 | 1068 int_O_ascenttime = 0; // Reset DTR. |
1069 char_O_nullzeit = 0; // Reset bottom time. | |
1070 char_O_deco_status = 0; // Calc bottom-time/nullzeit next iteration. | |
201 | 1071 |
1072 // Values that should be reset just once for the full real dive | |
1073 // (not every time we simulate an ascent): | |
1074 low_depth = 0; // Reset GF history. | |
192 | 1075 backup_low_depth = 255; // backup is empty... |
201 | 1076 |
1077 // No gas change (yet) for this ascent simulation. | |
1078 sim_gas_last_used = 0; | |
186 | 1079 break; |
1080 | |
167 | 1081 case 0: //---- bottom time ----------------------------------------------- |
1082 calc_nullzeit(); | |
1083 check_ndl(); | |
186 | 1084 char_O_deco_status = 2; // calc ascent next time. |
1085 break; | |
1086 | |
1087 case 2: //---- Simulate ascent to first stop ----------------------------- | |
201 | 1088 // Backup ascention state, so the simulation won't polute the real |
1089 // dive data. | |
186 | 1090 backup_GF_step = locked_GF_step; |
192 | 1091 backup_low_depth = low_depth; |
201 | 1092 backup_gas_used = sim_gas_last_used; |
186 | 1093 |
1094 sim_ascent_to_first_stop(); | |
201 | 1095 |
1096 char_O_deco_status = 1; // Calc stops next time (deco or gas switch). | |
167 | 1097 break; |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1098 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1099 case 1: //---- Simulate stops -------------------------------------------- |
167 | 1100 calc_hauptroutine_calc_deco(); |
186 | 1101 |
1102 // If simulation is finished, restore the GF low reference, so that | |
1103 // next ascent simulation is done from the current depth: | |
192 | 1104 if( char_O_deco_status == 0 && backup_low_depth != 255) |
186 | 1105 { |
1106 locked_GF_step = backup_GF_step; | |
192 | 1107 low_depth = backup_low_depth; |
201 | 1108 sim_gas_last_used = backup_gas_used; |
192 | 1109 backup_low_depth = 255; |
186 | 1110 } |
167 | 1111 break; |
116 | 1112 } |
167 | 1113 |
116 | 1114 check_post_dbg(); |
1115 } | |
1116 | |
167 | 1117 ////////////////////////////////////////////////////////////////////////////// |
1118 // calc_hauptroutine_data_input | |
1119 // | |
1120 // Reset all C-code dive parameters from their ASM-code values. | |
1121 // Detect gas change condition. | |
1122 // | |
116 | 1123 void calc_hauptroutine_data_input(void) |
1124 { | |
167 | 1125 overlay int int_temp; |
126 | 1126 |
197 | 1127 pres_respiration = int_I_pres_respiration * 0.001; |
1128 pres_surface = int_I_pres_surface * 0.001; | |
1129 N2_ratio = char_I_N2_ratio * 0.01; | |
1130 He_ratio = char_I_He_ratio * 0.01; | |
1131 deco_N2_ratio1 = char_I_deco_N2_ratio1 * 0.01; | |
1132 deco_He_ratio1 = char_I_deco_He_ratio1 * 0.01; | |
1133 float_deco_distance = char_I_deco_distance * 0.01; // Get offset is in mbar. | |
116 | 1134 |
126 | 1135 // ____________________________________________________ |
1136 // | |
1137 // _____________ G A S _ C H A N G E S ________________ | |
1138 // ____________________________________________________ | |
1139 | |
1140 int_temp = (int_I_pres_respiration - int_I_pres_surface) + MBAR_REACH_GASCHANGE_AUTO_CHANGE_OFF; | |
1141 | |
200 | 1142 deco_gas_change1 = 0.0; |
1143 deco_gas_change2 = 0.0; | |
1144 deco_gas_change3 = 0.0; | |
1145 deco_gas_change4 = 0.0; | |
1146 deco_gas_change5 = 0.0; | |
116 | 1147 |
167 | 1148 if(char_I_deco_gas_change1) |
126 | 1149 { |
167 | 1150 overlay int int_temp2 = ((int)char_I_deco_gas_change1) * 100; |
126 | 1151 if(int_temp > int_temp2) |
1152 { | |
167 | 1153 deco_gas_change1 = (float)char_I_deco_gas_change1 / 9.995 + pres_surface; |
1154 deco_gas_change1 += float_deco_distance; | |
126 | 1155 } |
1156 } | |
1157 if(char_I_deco_gas_change2) | |
1158 { | |
167 | 1159 overlay int int_temp2 = ((int)char_I_deco_gas_change2) * 100; |
126 | 1160 if(int_temp > int_temp2) |
1161 { | |
1162 deco_gas_change2 = (float)char_I_deco_gas_change2 / 9.995 + pres_surface; | |
1163 deco_gas_change2 += float_deco_distance; | |
1164 } | |
1165 } | |
1166 if(char_I_deco_gas_change3) | |
1167 { | |
167 | 1168 overlay int int_temp2 = ((int)char_I_deco_gas_change3) * 100; |
126 | 1169 if(int_temp > int_temp2) |
1170 { | |
1171 deco_gas_change3 = (float)char_I_deco_gas_change3 / 9.995 + pres_surface; | |
1172 deco_gas_change3 += float_deco_distance; | |
1173 } | |
1174 } | |
1175 if(char_I_deco_gas_change4) | |
1176 { | |
167 | 1177 overlay int int_temp2 = ((int)char_I_deco_gas_change4) * 100; |
126 | 1178 if(int_temp > int_temp2) |
1179 { | |
1180 deco_gas_change4 = (float)char_I_deco_gas_change4 / 9.995 + pres_surface; | |
1181 deco_gas_change4 += float_deco_distance; | |
1182 } | |
1183 } | |
1184 if(char_I_deco_gas_change5) | |
1185 { | |
167 | 1186 overlay int int_temp2 = ((int)char_I_deco_gas_change5) * 100; |
126 | 1187 if(int_temp > int_temp2) |
1188 { | |
1189 deco_gas_change5 = (float)char_I_deco_gas_change5 / 9.995 + pres_surface; | |
1190 deco_gas_change5 += float_deco_distance; | |
1191 } | |
1192 } | |
116 | 1193 |
197 | 1194 const_ppO2 = char_I_const_ppO2 * 0.01; |
1195 deco_ppO2_change = char_I_deco_ppO2_change / 99.95 | |
1196 + pres_surface | |
1197 + float_deco_distance; | |
1198 deco_ppO2 = char_I_deco_ppO2 * 0.01; | |
1199 float_desaturation_multiplier = char_I_desaturation_multiplier * 0.01; | |
1200 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
1201 GF_low = char_I_GF_Low_percentage * 0.01; | |
1202 GF_high = char_I_GF_High_percentage * 0.01; | |
126 | 1203 GF_delta = GF_high - GF_low; |
116 | 1204 } |
1205 | |
167 | 1206 ////////////////////////////////////////////////////////////////////////////// |
171 | 1207 // |
1208 // | |
116 | 1209 void calc_hauptroutine_update_tissues(void) |
1210 { | |
197 | 1211 assert( 0.00 <= N2_ratio && N2_ratio <= 1.00 ); |
1212 assert( 0.00 <= He_ratio && He_ratio <= 1.00 ); | |
1213 assert( (N2_ratio + He_ratio) <= 0.95 ); | |
1214 | |
1215 if (char_I_const_ppO2 == 0) // new in v.101 | |
167 | 1216 pres_diluent = pres_respiration; // new in v.101 |
186 | 1217 else |
1218 { | |
167 | 1219 pres_diluent = ((pres_respiration - const_ppO2)/(N2_ratio + He_ratio)); // new in v.101 |
186 | 1220 if (pres_diluent > pres_respiration) // new in v.101 |
1221 pres_diluent = pres_respiration; // new in v.101 | |
1222 } | |
200 | 1223 if (pres_diluent > ppWVapour) // new in v.101 |
116 | 1224 { |
200 | 1225 ppO2 = N2_ratio * (pres_diluent - ppWVapour); // changed in v.101 |
1226 ppHe = He_ratio * (pres_diluent - ppWVapour); // changed in v.101 | |
116 | 1227 char_O_diluent = (char)(pres_diluent/pres_respiration*100.0); |
1228 } | |
167 | 1229 else // new in v.101 |
116 | 1230 { |
200 | 1231 ppO2 = 0.0; // new in v.101 |
1232 ppHe = 0.0; // new in v.101 | |
116 | 1233 char_O_diluent = 0; |
1234 } | |
1235 | |
1236 if(!char_I_step_is_1min) | |
192 | 1237 calc_tissue(0); |
116 | 1238 else |
192 | 1239 calc_tissue(1); |
186 | 1240 |
192 | 1241 // Calc limit for surface, ie. GF_high. |
1242 calc_limit(GF_high); | |
186 | 1243 |
192 | 1244 int_O_gtissue_limit = (int)(calc_lead_tissue_limit * 1000); |
1245 int_O_gtissue_press = (int)((pres_tissue[char_O_gtissue_no] + (pres_tissue+16)[char_O_gtissue_no]) * 1000); | |
1246 | |
1247 // if guiding tissue can not be exposed to surface pressure immediately | |
1248 if( calc_lead_tissue_limit > pres_surface && char_O_deco_status == 0) | |
116 | 1249 { |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1250 char_O_nullzeit = 0; // deco necessary |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1251 char_O_deco_status = 2; // calculate ascent on next iteration. |
116 | 1252 } |
171 | 1253 } |
1254 | |
116 | 1255 |
167 | 1256 ////////////////////////////////////////////////////////////////////////////// |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1257 // Compute stops. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1258 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1259 // Note: because this can be very long, break on 16 iterations, and set state |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1260 // to 0 when finished, or to 1 when needing to continue. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1261 // |
116 | 1262 void calc_hauptroutine_calc_deco(void) |
1263 { | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1264 overlay unsigned char loop; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1265 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1266 for(loop = 0; loop < 16; ++loop) |
116 | 1267 { |
197 | 1268 // Do not ascent while doing a gas switch. |
1269 if( sim_gas_delay == 0 ) | |
1270 calc_nextdecodepth(); | |
116 | 1271 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1272 //---- Finish computations once surface is reached ------------------- |
200 | 1273 if( temp_depth_limit == 0 ) |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1274 { |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1275 copy_deco_table(); |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1276 calc_ascenttime(); |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1277 char_O_deco_status = 0; // calc nullzeit next time. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1278 return; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1279 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1280 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1281 //---- Else, continue simulating the stops --------------------------- |
197 | 1282 if( sim_gas_delay == 0 ) |
1283 check_gas_switch(); // Calculate N2_ratio and He_ratio. | |
201 | 1284 if( sim_gas_delay > 0 ) |
197 | 1285 sim_gas_delay--; |
1286 | |
1287 alveolar_presures(); // Updates ppN2 and ppHe. | |
186 | 1288 |
192 | 1289 sim_tissue(1); // Simulate compartiments for 1 minute. |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1290 update_deco_table(); // Add one minute stops. |
116 | 1291 } |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1292 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1293 // Surface not reached, need more stops... |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1294 char_O_deco_status = 1; // calc more stops next time. |
116 | 1295 } |
1296 | |
167 | 1297 ////////////////////////////////////////////////////////////////////////////// |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1298 // Simulation ascention to first deco stop. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1299 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1300 // Note: because we ascent with a constant speed (10m/mn, ie. 1bar/mn), |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1301 // there is no need to break on more that 16 iterations |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1302 // (or we are already in deep shit). |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1303 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1304 void sim_ascent_to_first_stop(void) |
116 | 1305 { |
171 | 1306 update_startvalues(); |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1307 clear_deco_table(); |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1308 |
200 | 1309 temp_deco = pres_respiration; // Starts from current real depth. |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1310 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1311 // Loop until first top or surface is reached. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1312 for(;;) |
116 | 1313 { |
197 | 1314 // Do not ascent while doing a gas switch. |
1315 if( sim_gas_delay == 0 ) | |
1316 temp_deco -= 1.0; // Ascent 1 min, at 10m/min. == 1bar/min. | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1317 |
192 | 1318 // Compute sim_lead_tissue_limit at GF_low (deepest stop). |
1319 sim_limit(GF_low); | |
116 | 1320 |
192 | 1321 // Did we reach first stop ? |
1322 if( temp_deco <= sim_lead_tissue_limit ) | |
201 | 1323 { |
1324 temp_deco = sim_lead_tissue_limit; | |
1325 break; // Return stop found ! | |
1326 } | |
1327 | |
171 | 1328 // Next stop is surface ? |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1329 if( temp_deco <= pres_surface ) |
201 | 1330 break; // Yes: finished ! |
171 | 1331 |
197 | 1332 //---- Simulat gas switches |
1333 if( sim_gas_delay > 0 ) | |
1334 { | |
1335 sim_gas_delay--; // Decrement switch delay | |
1336 update_deco_table(); // And mark stop in table | |
1337 } | |
1338 else | |
1339 { | |
1340 temp_deco += 0.5; // Check gas change 5 meter below new depth. | |
171 | 1341 check_gas_switch(); |
197 | 1342 temp_deco -= 0.5; // Back to new depth. |
1343 } | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1344 |
192 | 1345 // Then simulate with the new gas pressures... (1min) |
197 | 1346 alveolar_presures(); // Calculated at true depth ! |
192 | 1347 sim_tissue(1); |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1348 } |
167 | 1349 } |
116 | 1350 |
167 | 1351 ////////////////////////////////////////////////////////////////////////////// |
1352 // calc_tissue | |
1353 // | |
116 | 1354 // optimized in v.101 |
167 | 1355 // |
184 | 1356 static void calc_tissue(PARAMETER unsigned char period) |
116 | 1357 { |
126 | 1358 for (ci=0;ci<16;ci++) |
1359 { | |
184 | 1360 read_buhlmann_coefficients(period); // 2 sec or 1 min period. |
116 | 1361 |
126 | 1362 // N2 |
200 | 1363 temp_tissue = (ppO2 - pres_tissue[ci]) * var_N2_e; |
126 | 1364 temp_tissue_safety(); |
167 | 1365 pres_tissue[ci] += temp_tissue; |
165 | 1366 |
126 | 1367 // He |
200 | 1368 temp_tissue = (ppHe - (pres_tissue+16)[ci]) * var_He_e; |
126 | 1369 temp_tissue_safety(); |
167 | 1370 (pres_tissue+16)[ci] += temp_tissue; |
192 | 1371 } |
167 | 1372 } |
116 | 1373 |
167 | 1374 ////////////////////////////////////////////////////////////////////////////// |
192 | 1375 // calc_limit |
167 | 1376 // |
192 | 1377 // New in v.111 : separated from calc_tissue(), and depends on GF value. |
167 | 1378 // |
192 | 1379 static void calc_limit(PARAMETER float GF_current) |
167 | 1380 { |
192 | 1381 char_O_gtissue_no = 255; |
1382 calc_lead_tissue_limit = 0.0; | |
1383 | |
1384 for (ci=0;ci<16;ci++) | |
1385 { | |
1386 overlay float p = pres_tissue[ci] + (pres_tissue+16)[ci]; | |
1387 | |
1388 read_buhlmann_coefficients(-1); // 2 sec or 1 min period. | |
1389 var_N2_a = (var_N2_a * pres_tissue[ci] + var_He_a * (pres_tissue+16)[ci]) / p; | |
1390 var_N2_b = (var_N2_b * pres_tissue[ci] + var_He_b * (pres_tissue+16)[ci]) / p; | |
116 | 1391 |
192 | 1392 // Apply the Eric Baker's varying gradient factor correction. |
1393 // Note: the correction factor depends both on GF and b, | |
1394 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
1395 // so that can change who is the leading gas... | |
1396 // Note: Also depends of the GF. So the calcul is different for | |
1397 // GF_low, current GF, or GF_high... | |
1398 // *BUT* calc_tissue() is used to compute bottom time, | |
1399 // hence what would happend at surface, | |
1400 // hence at GF_high. | |
1401 if( char_I_deco_model == 1 ) | |
1402 p = ( p - var_N2_a * GF_current) * var_N2_b | |
1403 / (GF_current + var_N2_b * (1.0 - GF_current)); | |
1404 else | |
1405 p = (p - var_N2_a) * var_N2_b; | |
1406 if( p < 0 ) p = 0; | |
1407 | |
1408 pres_tissue_limit[ci] = p; | |
1409 if( p > calc_lead_tissue_limit ) | |
1410 { | |
1411 char_O_gtissue_no = ci; | |
1412 calc_lead_tissue_limit = p; | |
1413 } | |
1414 } | |
197 | 1415 |
1416 assert( char_O_gtissue_no < 16 ); | |
1417 assert( 0.0 <= calc_lead_tissue_limit && calc_lead_tissue_limit <= 14.0); | |
167 | 1418 } |
1419 | |
1420 ////////////////////////////////////////////////////////////////////////////// | |
1421 // calc_nullzeit | |
1422 // | |
1423 // calculates the remaining bottom time | |
1424 // | |
1425 // unchanged in v.101 | |
1426 // | |
1427 static void calc_nullzeit(void) | |
1428 { | |
1429 overlay int loop; | |
200 | 1430 update_startvalues(); |
167 | 1431 |
116 | 1432 char_O_nullzeit = 0; |
167 | 1433 for(loop = 1; loop <= 17; loop++) |
116 | 1434 { |
1435 backup_sim_pres_tissue(); | |
197 | 1436 sim_tissue(2); // 10 min. |
192 | 1437 sim_limit(GF_high); |
167 | 1438 |
192 | 1439 if( sim_lead_tissue_limit > pres_surface ) // changed in v.102 , if guiding tissue can not be exposed to surface pressure immediately |
185 | 1440 { |
1441 restore_sim_pres_tissue(); | |
1442 break; | |
1443 } | |
1444 // Validate once we know its good. | |
1445 char_O_nullzeit += 10; | |
167 | 1446 } |
1447 | |
116 | 1448 if (char_O_nullzeit < 60) |
1449 { | |
167 | 1450 for(loop=1; loop <= 10; loop++) |
116 | 1451 { |
197 | 1452 sim_tissue(1); // 1 min |
192 | 1453 sim_limit(GF_high); |
1454 | |
1455 if( sim_lead_tissue_limit > pres_surface) // changed in v.102 , if guiding tissue can not be exposed to surface pressure immediately | |
185 | 1456 break; |
1457 char_O_nullzeit++; | |
167 | 1458 } |
185 | 1459 } |
1460 } | |
116 | 1461 |
167 | 1462 ////////////////////////////////////////////////////////////////////////////// |
1463 // backup_sim_pres_tissue | |
1464 // | |
116 | 1465 void backup_sim_pres_tissue(void) |
1466 { | |
167 | 1467 overlay unsigned char x; |
1468 | |
1469 for(x = 0; x<32; x++) | |
126 | 1470 sim_pres_tissue_backup[x] = sim_pres_tissue[x]; |
167 | 1471 } |
116 | 1472 |
167 | 1473 ////////////////////////////////////////////////////////////////////////////// |
1474 // restore_sim_pres_tissue | |
1475 // | |
116 | 1476 void restore_sim_pres_tissue(void) |
1477 { | |
167 | 1478 overlay unsigned char x; |
1479 | |
1480 for(x = 0; x<32; x++) | |
126 | 1481 sim_pres_tissue[x] = sim_pres_tissue_backup[x]; |
167 | 1482 } |
116 | 1483 |
167 | 1484 ////////////////////////////////////////////////////////////////////////////// |
1485 // calc_ascenttime | |
1486 // | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1487 static void calc_ascenttime(void) |
116 | 1488 { |
126 | 1489 if (pres_respiration > pres_surface) |
1490 { | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1491 overlay unsigned char x; |
168 | 1492 |
192 | 1493 // + 0.7 to count 1 minute ascent time from 3 meter to surface |
1494 overlay float ascent = pres_respiration - pres_surface + 0.7; | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1495 if (ascent < 0.0) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1496 ascent = 0.0; |
192 | 1497 int_O_ascenttime = (unsigned int)(ascent + 0.99); |
168 | 1498 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1499 for(x=0; x<32 && internal_deco_depth[x]; x++) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1500 int_O_ascenttime += (unsigned int)internal_deco_time[x]; |
126 | 1501 } |
1502 else | |
168 | 1503 int_O_ascenttime = 0; |
167 | 1504 } |
116 | 1505 |
167 | 1506 ////////////////////////////////////////////////////////////////////////////// |
1507 // update_startvalues | |
1508 // | |
116 | 1509 // updated in v.102 |
167 | 1510 // |
116 | 1511 void update_startvalues(void) |
1512 { | |
167 | 1513 overlay unsigned char x; |
116 | 1514 |
201 | 1515 // Start ascent simulation with current tissue partial pressures. |
116 | 1516 for (x = 0;x<16;x++) |
1517 { | |
1518 sim_pres_tissue[x] = pres_tissue[x]; | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1519 (sim_pres_tissue+16)[x] = (pres_tissue+16)[x]; |
116 | 1520 sim_pres_tissue_limit[x] = pres_tissue_limit[x]; |
1521 } | |
197 | 1522 |
201 | 1523 // No leading tissue (yet) for this ascent simulation. |
197 | 1524 sim_lead_tissue_limit = 0.0; |
1525 sim_lead_tissue_no = 255; | |
167 | 1526 } |
116 | 1527 |
167 | 1528 ////////////////////////////////////////////////////////////////////////////// |
192 | 1529 // sim_tissue |
167 | 1530 // |
116 | 1531 // optimized in v.101 |
167 | 1532 // |
1533 // Function very simular to calc_tissue, but: | |
1534 // + Use a 1min or 10min period. | |
1535 // + Do it on sim_pres_tissue, instead of pres_tissue. | |
184 | 1536 static void sim_tissue(PARAMETER unsigned char period) |
116 | 1537 { |
192 | 1538 for(ci=0; ci<16; ci++) |
126 | 1539 { |
184 | 1540 read_buhlmann_coefficients(period); // 1 or 10 minute(s) interval |
165 | 1541 |
126 | 1542 // N2 |
200 | 1543 temp_tissue = (ppO2 - sim_pres_tissue[ci]) * var_N2_e; |
126 | 1544 temp_tissue_safety(); |
167 | 1545 sim_pres_tissue[ci] += temp_tissue; |
126 | 1546 |
1547 // He | |
200 | 1548 temp_tissue = (ppHe - (sim_pres_tissue+16)[ci]) * var_He_e; |
126 | 1549 temp_tissue_safety(); |
167 | 1550 (sim_pres_tissue+16)[ci] += temp_tissue; |
192 | 1551 } |
167 | 1552 } |
116 | 1553 |
167 | 1554 ////////////////////////////////////////////////////////////////////////////// |
192 | 1555 // sim_limit() |
1556 // | |
1557 // New in v.111 | |
1558 // | |
1559 // Function separated from sim_tissue() to allow recomputing limit on | |
1560 // different depth, because it depends on current gradient factor. | |
167 | 1561 // |
192 | 1562 static void sim_limit(PARAMETER float GF_current) |
116 | 1563 { |
192 | 1564 sim_lead_tissue_limit = 0.0; |
1565 sim_lead_tissue_no = 255; | |
1566 | |
1567 for(ci=0; ci<16; ci++) | |
1568 { | |
1569 overlay float p = sim_pres_tissue[ci] + (sim_pres_tissue+16)[ci]; | |
1570 | |
1571 read_buhlmann_coefficients(-1); | |
1572 var_N2_a = (var_N2_a * sim_pres_tissue[ci] + var_He_a * (sim_pres_tissue+16)[ci]) / p; | |
1573 var_N2_b = (var_N2_b * sim_pres_tissue[ci] + var_He_b * (sim_pres_tissue+16)[ci]) / p; | |
1574 | |
1575 // Apply the Eric Baker's varying gradient factor correction. | |
1576 // Note: the correction factor depends both on GF and b, | |
1577 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
1578 // so that can change who is the leading gas... | |
1579 // Note: Also depends of the GF_current... | |
1580 // *BUT* calc_tissue() is used to compute bottom time, | |
1581 // hence what would happend at surface, | |
1582 // hence at GF_high. | |
1583 if( char_I_deco_model == 1 ) | |
1584 p = ( p - var_N2_a * GF_current) * var_N2_b | |
1585 / (GF_current + var_N2_b * (1.0 - GF_current)); | |
1586 else | |
1587 p = (p - var_N2_a) * var_N2_b; | |
1588 if( p < 0 ) p = 0; | |
1589 | |
1590 sim_pres_tissue_limit[ci] = p; | |
1591 if( p > sim_lead_tissue_limit ) | |
1592 { | |
1593 sim_lead_tissue_no = ci; | |
1594 sim_lead_tissue_limit = p; | |
1595 } | |
1596 } // for ci | |
197 | 1597 |
1598 assert( sim_lead_tissue_no < 16 ); | |
1599 assert( 0.0 <= sim_lead_tissue_limit && sim_lead_tissue_limit <= 14.0); | |
167 | 1600 } |
116 | 1601 |
167 | 1602 ////////////////////////////////////////////////////////////////////////////// |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1603 // clear_deco_table |
167 | 1604 // |
1605 // unchanged in v.101 | |
1606 // | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1607 static void clear_deco_table(void) |
116 | 1608 { |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1609 overlay unsigned char x; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1610 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1611 for(x=0; x<32; ++x) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1612 { |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1613 internal_deco_time [x] = 0; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1614 internal_deco_depth[x] = 0; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1615 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1616 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1617 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1618 ////////////////////////////////////////////////////////////////////////////// |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1619 // update_deco_table |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1620 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1621 // Add 1 min to current stop. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1622 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1623 // Inputs: |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1624 // temp_depth_limit = stop's depth, in meters. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1625 // In/Out: |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1626 // internal_deco_depth[] : depth (in meters) of each stops. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1627 // internal_deco_time [] : time (in minutes) of each stops. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1628 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1629 static void update_deco_table() |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1630 { |
192 | 1631 overlay unsigned char x; |
200 | 1632 assert( temp_depth_limit < 128 ); // Can't be negativ (overflown). |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1633 |
192 | 1634 for(x=0; x<32; ++x) |
1635 { | |
200 | 1636 if( internal_deco_depth[x] == temp_depth_limit ) |
192 | 1637 { |
1638 // Do not overflow (max 255') | |
1639 if( internal_deco_time[x] < 255 ) | |
1640 { | |
1641 internal_deco_time[x]++; | |
1642 return; | |
1643 } | |
1644 // But store extra in the next stop... | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1645 } |
192 | 1646 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1647 if( internal_deco_depth[x] == 0 ) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1648 { |
192 | 1649 internal_deco_depth[x] = temp_depth_limit; |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1650 internal_deco_time[x] = 1; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1651 return; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1652 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1653 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1654 |
192 | 1655 // Can't store stops at more than 96m. |
1656 // Or stops at less that 3m too. | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1657 // Just do nothing with that... |
167 | 1658 } |
116 | 1659 |
167 | 1660 ////////////////////////////////////////////////////////////////////////////// |
1661 // calc_gradient_factor | |
1662 // | |
165 | 1663 // optimized in v.101 (var_N2_a) |
116 | 1664 // new code in v.102 |
167 | 1665 // |
1666 static void calc_gradient_factor(void) | |
116 | 1667 { |
197 | 1668 overlay float gf; |
1669 assert( char_O_gtissue_no < 16 ); | |
1670 | |
116 | 1671 // tissue > respiration (entsaettigungsvorgang) |
1672 // gradient ist wieviel prozent an limit mit basis tissue | |
1673 // dh. 0% = respiration == tissue | |
1674 // dh. 100% = respiration == limit | |
197 | 1675 temp_tissue = pres_tissue[char_O_gtissue_no] + (pres_tissue+16)[char_O_gtissue_no]; |
1676 if( temp_tissue < pres_respiration ) | |
1677 gf = 0.0; | |
1678 else | |
1679 { | |
1680 gf = (temp_tissue - pres_respiration) | |
1681 / (temp_tissue - pres_tissue_limit[char_O_gtissue_no]) | |
1682 * 100.0; | |
1683 if( gf > 255.0 ) gf = 255.0; | |
1684 if( gf < 0.0 ) gf = 0.0; | |
1685 } | |
1686 char_O_gradient_factor = (unsigned char)gf; | |
116 | 1687 |
1688 if (char_I_deco_model == 1) // calculate relative gradient factor | |
1689 { | |
197 | 1690 overlay float rgf; |
1691 | |
1692 if( low_depth == 0 ) | |
1693 rgf = GF_high; | |
1694 else | |
1695 { | |
1696 overlay float temp1 = low_depth * 0.09995; | |
1697 overlay float temp2 = pres_respiration - pres_surface; | |
186 | 1698 |
197 | 1699 if (temp2 <= 0) |
1700 rgf = GF_high; | |
1701 else if (temp2 >= temp1) | |
1702 rgf = GF_low; | |
1703 else | |
1704 rgf = GF_low + (temp1 - temp2)/temp1*GF_delta; | |
1705 } | |
186 | 1706 |
197 | 1707 rgf = gf / rgf; // gf is already in percent |
1708 if( rgf < 0.0 ) rgf = 0.0; | |
1709 if( rgf > 255.0 ) rgf = 255.0; | |
1710 char_O_relative_gradient_GF = (unsigned char)rgf; | |
116 | 1711 } // calc relative gradient factor |
1712 else | |
1713 { | |
186 | 1714 char_O_relative_gradient_GF = char_O_gradient_factor; |
116 | 1715 } |
167 | 1716 } |
116 | 1717 |
167 | 1718 ////////////////////////////////////////////////////////////////////////////// |
1719 // deco_calc_desaturation_time | |
1720 // | |
116 | 1721 // FIXED N2_ratio |
1722 // unchanged in v.101 | |
167 | 1723 // |
116 | 1724 void deco_calc_desaturation_time(void) |
1725 { | |
184 | 1726 overlay unsigned int desat_time; // For a particular compartiment, in min. |
200 | 1727 overlay float temp1; |
197 | 1728 overlay float temp2; |
1729 overlay float temp3; | |
1730 overlay float temp4; | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
1731 RESET_C_STACK |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
1732 |
126 | 1733 N2_ratio = 0.7902; // FIXED sum as stated in b"uhlmann |
1734 pres_surface = (float)int_I_pres_surface / 1000.0; | |
200 | 1735 ppO2 = N2_ratio * (pres_surface - ppWVapour); |
126 | 1736 int_O_desaturation_time = 0; |
1737 float_desaturation_multiplier = char_I_desaturation_multiplier / 142.0; // new in v.101 (70,42%/100.=142) | |
1738 | |
1739 for (ci=0;ci<16;ci++) | |
1740 { | |
200 | 1741 overlay float var_N2_halftime = buhlmann_ht[ci]; |
1742 overlay float var_He_halftime = (buhlmann_ht+16)[ci]; | |
116 | 1743 |
126 | 1744 // saturation_time (for flight) and N2_saturation in multiples of halftime |
1745 // version v.100: 1.1 = 10 percent distance to totally clean (totally clean is not possible, would take infinite time ) | |
1746 // new in version v.101: 1.07 = 7 percent distance to totally clean (totally clean is not possible, would take infinite time ) | |
1747 // changes in v.101: 1.05 = 5 percent dist to totally clean is new desaturation point for display and noFly calculations | |
1748 // N2 | |
200 | 1749 temp1 = 1.05 * ppO2; |
126 | 1750 temp1 = temp1 - pres_tissue[ci]; |
200 | 1751 temp2 = ppO2 - pres_tissue[ci]; |
126 | 1752 if (temp2 >= 0.0) |
1753 { | |
1754 temp1 = 0; | |
1755 temp2 = 0; | |
1756 } | |
1757 else | |
1758 temp1 = temp1 / temp2; | |
1759 if (temp1 > 0.0) | |
1760 { | |
1761 temp1 = log(1.0 - temp1); | |
1762 temp1 = temp1 / -0.6931; // temp1 is the multiples of half times necessary. | |
1763 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. | |
1764 // minus because log is negative | |
165 | 1765 temp2 = var_N2_halftime * temp1 / float_desaturation_multiplier; // time necessary (in minutes ) for complete desaturation (see comment about 10 percent) , new in v.101: float_desaturation_multiplier |
126 | 1766 } |
1767 else | |
1768 { | |
1769 temp1 = 0; | |
1770 temp2 = 0; | |
1771 } | |
116 | 1772 |
126 | 1773 // He |
167 | 1774 temp3 = 0.1 - (pres_tissue+16)[ci]; |
126 | 1775 if (temp3 >= 0.0) |
1776 { | |
1777 temp3 = 0; | |
1778 temp4 = 0; | |
1779 } | |
1780 else | |
167 | 1781 temp3 = -1.0 * temp3 / (pres_tissue+16)[ci]; |
126 | 1782 if (temp3 > 0.0) |
1783 { | |
1784 temp3 = log(1.0 - temp3); | |
1785 temp3 = temp3 / -0.6931; // temp1 is the multiples of half times necessary. | |
1786 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. | |
1787 // minus because log is negative | |
165 | 1788 temp4 = var_He_halftime * temp3 / float_desaturation_multiplier; // time necessary (in minutes ) for "complete" desaturation, new in v.101 float_desaturation_multiplier |
126 | 1789 } |
1790 else | |
1791 { | |
1792 temp3 = 0; | |
1793 temp4 = 0; | |
1794 } | |
116 | 1795 |
126 | 1796 // saturation_time (for flight) |
1797 if (temp4 > temp2) | |
184 | 1798 desat_time = (unsigned int)temp4; |
126 | 1799 else |
184 | 1800 desat_time = (unsigned int)temp2; |
167 | 1801 if(desat_time > int_O_desaturation_time) |
1802 int_O_desaturation_time = desat_time; | |
116 | 1803 |
126 | 1804 // N2 saturation in multiples of halftime for display purposes |
1805 temp2 = temp1 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 | |
1806 temp2 = temp2 + 80.0; // set center | |
1807 if (temp2 < 0.0) | |
1808 temp2 = 0.0; | |
1809 if (temp2 > 255.0) | |
1810 temp2 = 255.0; | |
1811 char_O_tissue_saturation[ci] = (char)temp2; | |
1812 // He saturation in multiples of halftime for display purposes | |
1813 temp4 = temp3 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 | |
1814 temp4 = temp4 + 80.0; // set center | |
1815 if (temp4 < 0.0) | |
1816 temp4 = 0.0; | |
1817 if (temp4 > 255.0) | |
1818 temp4 = 255.0; | |
167 | 1819 (char_O_tissue_saturation+16)[ci] = (char)temp4; |
126 | 1820 } // for |
167 | 1821 } |
116 | 1822 |
167 | 1823 ////////////////////////////////////////////////////////////////////////////// |
1824 // calc_wo_deco_step_1_min | |
1825 // | |
116 | 1826 // FIXED N2 Ratio |
1827 // optimized in v.101 (...saturation_multiplier) | |
1828 // desaturation slowed down to 70,42% | |
167 | 1829 // |
1830 static void calc_wo_deco_step_1_min(void) | |
116 | 1831 { |
1832 if(flag_in_divemode) | |
1833 { | |
1834 flag_in_divemode = 0; | |
1835 set_dbg_end_of_dive(); | |
1836 } | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
1837 |
126 | 1838 N2_ratio = 0.7902; // FIXED, sum lt. buehlmann |
1839 pres_respiration = (float)int_I_pres_respiration / 1000.0; // assembler code uses different digit system | |
192 | 1840 pres_surface = (float)int_I_pres_surface / 1000.0; // the b"uhlmann formula using pres_surface does not use the N2_ratio |
200 | 1841 ppO2 = N2_ratio * (pres_respiration - ppWVapour); // ppWVapour is the extra pressure in the body |
1842 ppHe = 0.0; | |
126 | 1843 float_desaturation_multiplier = char_I_desaturation_multiplier / 142.0; // new in v.101 (70,42%/100.=142) |
197 | 1844 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; |
126 | 1845 |
192 | 1846 calc_tissue(1); // update the pressure in the 32 tissues in accordance with the new ambient pressure |
167 | 1847 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1848 clear_deco_table(); |
200 | 1849 char_O_deco_status = 3; // surface new in v.102 : stays in surface state. |
126 | 1850 char_O_nullzeit = 0; |
168 | 1851 int_O_ascenttime = 0; |
126 | 1852 calc_gradient_factor(); |
167 | 1853 } |
126 | 1854 |
167 | 1855 ////////////////////////////////////////////////////////////////////////////// |
1856 ////////////////////////////////////////////////////////////////////////////// | |
1857 ////////////////////////////////// deco_hash ///////////////////////////////// | |
1858 ////////////////////////////////////////////////////////////////////////////// | |
1859 ////////////////////////////////////////////////////////////////////////////// | |
126 | 1860 |
184 | 1861 #ifndef CROSS_COMPILE |
116 | 1862 void deco_hash(void) |
1863 { | |
167 | 1864 overlay unsigned char md_i, md_j; // Loop index. |
1865 overlay unsigned char md_t; | |
1866 overlay unsigned char md_buffer[16]; | |
1867 overlay unsigned char md_temp; | |
1868 overlay unsigned int md_pointer; | |
1869 | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
1870 RESET_C_STACK |
126 | 1871 |
1872 // init | |
164 | 1873 for(md_i=0;md_i<16;md_i++) |
126 | 1874 { |
1875 md_state[md_i] = 0; | |
164 | 1876 char_O_hash[md_i] = 0; |
126 | 1877 } // for md_i 16 |
116 | 1878 |
126 | 1879 _asm |
167 | 1880 movlw 0x01 // md_pi address. |
1881 movwf TBLPTRU,0 | |
1882 movlw 0x7E | |
1883 movwf TBLPTRH,0 | |
1884 movlw 0x00 | |
1885 movwf TBLPTRL,0 | |
126 | 1886 _endasm; |
165 | 1887 md_i = 0; |
1888 do { | |
126 | 1889 _asm |
1890 TBLRDPOSTINC | |
1891 movff TABLAT,md_temp | |
1892 _endasm | |
165 | 1893 md_pi_subst[md_i++] = md_temp; |
1894 } while( md_i != 0 ); | |
1895 | |
126 | 1896 _asm |
167 | 1897 movlw 0x00 |
1898 movwf TBLPTRU,0 | |
1899 movlw 0x00 | |
1900 movwf TBLPTRH,0 | |
1901 movlw 0x00 | |
1902 movwf TBLPTRL,0 | |
126 | 1903 _endasm |
167 | 1904 |
126 | 1905 // cycle buffers |
1906 for (md_pointer=0x0000;md_pointer<0x17f3;md_pointer++) | |
1907 { | |
1908 md_t = 0; | |
1909 for (md_i=0;md_i<16;md_i++) | |
1910 { | |
1911 if(md_pointer == 9) | |
164 | 1912 md_temp = char_O_hash[md_i]; |
126 | 1913 else |
1914 { | |
1915 _asm | |
167 | 1916 TBLRDPOSTINC |
1917 movff TABLAT,md_temp | |
126 | 1918 _endasm |
1919 } // else | |
167 | 1920 |
1921 md_buffer[md_i] = md_temp; | |
186 | 1922 md_state[md_i+16] = md_temp; |
1923 md_state[md_i+32] = (unsigned char)(md_temp ^ md_state[md_i]); | |
126 | 1924 } // for md_i 16 |
1925 | |
1926 for (md_i=0;md_i<18;md_i++) | |
1927 { | |
1928 for (md_j=0;md_j<48;md_j++) | |
1929 { | |
167 | 1930 md_state[md_j] ^= md_pi_subst[md_t]; |
126 | 1931 md_t = md_state[md_j]; |
1932 } // for md_j 48 | |
1933 md_t = (unsigned char)(md_t+1); | |
1934 } // for md_i 18 | |
164 | 1935 md_t = char_O_hash[15]; |
126 | 1936 |
1937 for (md_i=0;md_i<16;md_i++) | |
1938 { | |
167 | 1939 char_O_hash[md_i] ^= md_pi_subst[(md_buffer[md_i] ^ md_t)]; |
164 | 1940 md_t = char_O_hash[md_i]; |
126 | 1941 } // for md_i 16 |
1942 } // for md_pointer | |
116 | 1943 } // void deco_hash(void) |
184 | 1944 #endif |
116 | 1945 |
167 | 1946 ////////////////////////////////////////////////////////////////////////////// |
1947 // deco_clear_CNS_fraction | |
1948 // | |
116 | 1949 // new in v.101 |
167 | 1950 // |
116 | 1951 void deco_clear_CNS_fraction(void) |
1952 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
1953 RESET_C_STACK |
126 | 1954 CNS_fraction = 0.0; |
1955 char_O_CNS_fraction = 0; | |
167 | 1956 } |
116 | 1957 |
167 | 1958 ////////////////////////////////////////////////////////////////////////////// |
1959 // deco_calc_CNS_fraction | |
1960 // | |
116 | 1961 // new in v.101 |
1962 // optimized in v.102 : with new variables char_I_actual_ppO2 and actual_ppO2 | |
167 | 1963 // |
116 | 1964 // Input: char_I_actual_ppO2 |
1965 // Output: char_O_CNS_fraction | |
1966 // Uses and Updates: CNS_fraction | |
1967 // Uses: acutal_ppO2 | |
167 | 1968 // |
116 | 1969 void deco_calc_CNS_fraction(void) |
1970 { | |
167 | 1971 overlay float actual_ppO2; |
126 | 1972 RESET_C_STACK |
167 | 1973 |
126 | 1974 actual_ppO2 = (float)char_I_actual_ppO2 / 100.0; |
116 | 1975 |
126 | 1976 if (char_I_actual_ppO2 < 50) |
1977 CNS_fraction = CNS_fraction;// no changes | |
1978 else if (char_I_actual_ppO2 < 60) | |
1979 CNS_fraction = 1/(-54000.0 * actual_ppO2 + 54000.0) + CNS_fraction; | |
1980 else if (char_I_actual_ppO2 < 70) | |
1981 CNS_fraction = 1/(-45000.0 * actual_ppO2 + 48600.0) + CNS_fraction; | |
1982 else if (char_I_actual_ppO2 < 80) | |
1983 CNS_fraction = 1/(-36000.0 * actual_ppO2 + 42300.0) + CNS_fraction; | |
1984 else if (char_I_actual_ppO2 < 90) | |
1985 CNS_fraction = 1/(-27000.0 * actual_ppO2 + 35100.0) + CNS_fraction; | |
1986 else if (char_I_actual_ppO2 < 110) | |
1987 CNS_fraction = 1/(-18000.0 * actual_ppO2 + 27000.0) + CNS_fraction; | |
1988 else if (char_I_actual_ppO2 < 150) | |
1989 CNS_fraction = 1/(-9000.0 * actual_ppO2 + 17100.0) + CNS_fraction; | |
1990 else if (char_I_actual_ppO2 < 160) | |
1991 CNS_fraction = 1/(-22500.0 * actual_ppO2 + 37350.0) + CNS_fraction; | |
1992 else if (char_I_actual_ppO2 < 165) | |
1993 CNS_fraction = 0.000755 + CNS_fraction; // Arieli et all.(2002): Modeling pulmonary and CNS O2 toxicity... Formula (A1) based on value for 1.55 and c=20 | |
1994 else if (char_I_actual_ppO2 < 170) | |
1995 CNS_fraction = 0.00102 + CNS_fraction; // example calculation: Sqrt((1.7/1.55)^20)*0.000404 | |
1996 else if (char_I_actual_ppO2 < 175) | |
1997 CNS_fraction = 0.00136 + CNS_fraction; | |
1998 else if (char_I_actual_ppO2 < 180) | |
1999 CNS_fraction = 0.00180 + CNS_fraction; | |
2000 else if (char_I_actual_ppO2 < 185) | |
2001 CNS_fraction = 0.00237 + CNS_fraction; | |
2002 else if (char_I_actual_ppO2 < 190) | |
2003 CNS_fraction = 0.00310 + CNS_fraction; | |
2004 else if (char_I_actual_ppO2 < 195) | |
2005 CNS_fraction = 0.00401 + CNS_fraction; | |
2006 else if (char_I_actual_ppO2 < 200) | |
2007 CNS_fraction = 0.00517 + CNS_fraction; | |
2008 else if (char_I_actual_ppO2 < 230) | |
2009 CNS_fraction = 0.0209 + CNS_fraction; | |
2010 else | |
2011 CNS_fraction = 0.0482 + CNS_fraction; // value for 2.5 | |
116 | 2012 |
126 | 2013 if (CNS_fraction > 2.5) |
2014 CNS_fraction = 2.5; | |
2015 if (CNS_fraction < 0.0) | |
2016 CNS_fraction = 0.0; | |
2017 | |
2018 char_O_CNS_fraction = (char)((CNS_fraction + 0.005)* 100.0); | |
167 | 2019 } |
116 | 2020 |
167 | 2021 ////////////////////////////////////////////////////////////////////////////// |
2022 // deco_calc_CNS_decrease_15min | |
2023 // | |
116 | 2024 // new in v.101 |
167 | 2025 // |
116 | 2026 // calculates the half time of 90 minutes in 6 steps of 15 min |
200 | 2027 // (Used in sleepmode, for low battery mode). |
167 | 2028 // |
116 | 2029 // Output: char_O_CNS_fraction |
2030 // Uses and Updates: CNS_fraction | |
167 | 2031 // |
116 | 2032 void deco_calc_CNS_decrease_15min(void) |
2033 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
2034 RESET_C_STACK |
126 | 2035 CNS_fraction = 0.890899 * CNS_fraction; |
167 | 2036 char_O_CNS_fraction = (char)(CNS_fraction * 100.0 + 0.5); |
2037 } | |
116 | 2038 |
167 | 2039 ////////////////////////////////////////////////////////////////////////////// |
2040 // deco_calc_percentage | |
2041 // | |
116 | 2042 // new in v.101 |
167 | 2043 // |
116 | 2044 // calculates int_I_temp * char_I_temp / 100 |
2045 // output is int_I_temp | |
200 | 2046 // |
2047 // Used to compute NoFly remaining time. | |
116 | 2048 |
2049 void deco_calc_percentage(void) | |
2050 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
2051 RESET_C_STACK |
197 | 2052 int_I_temp = (int)(int_I_temp * (char_I_temp / 100.0)); |
116 | 2053 } |
2054 | |
167 | 2055 ////////////////////////////////////////////////////////////////////////////// |
2056 | |
116 | 2057 void deco_push_tissues_to_vault(void) |
2058 { | |
167 | 2059 overlay unsigned char x; |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
2060 RESET_C_STACK |
167 | 2061 |
116 | 2062 cns_vault = CNS_fraction; |
167 | 2063 for (x=0;x<32;x++) |
2064 pres_tissue_vault[x] = pres_tissue[x]; | |
116 | 2065 } |
167 | 2066 |
116 | 2067 void deco_pull_tissues_from_vault(void) |
2068 { | |
167 | 2069 overlay unsigned char x; |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
2070 RESET_C_STACK |
167 | 2071 |
116 | 2072 CNS_fraction = cns_vault; |
167 | 2073 for (x=0;x<32;x++) |
2074 pres_tissue[x] = pres_tissue_vault[x]; | |
116 | 2075 } |
2076 | |
167 | 2077 ////////////////////////////////////////////////////////////////////////////// |
2078 // | |
184 | 2079 #ifndef CROSS_COMPILE |
2080 void main() {} | |
2081 #endif |