Mercurial > public > mk2
annotate code_part1/OSTC_code_c_part2/p2_deco.c @ 213:d19445e0cb2f
BUGFIX Altimeter reset when exiting sleep mode (bug#&6).
author | JeanDo |
---|---|
date | Wed, 23 Feb 2011 00:18:41 +0100 |
parents | 275befc5f39d |
children | c7e32ff65636 |
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(). |
203 | 68 // 2011/01/24: [jDG] Make ascenttime an short. No more overflow! |
169
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 |
212 | 130 static void check_gas_switch(void); |
171 | 131 static void calc_nextdecodepth(void); |
116 | 132 |
200 | 133 //---- Bank 4 parameters ----------------------------------------------------- |
163 | 134 #pragma udata bank4=0x400 |
116 | 135 |
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 const_ppO2; // new in v.101 | |
179 static float deco_ppO2_change; // new in v.101 | |
180 static float deco_ppO2; // new in v.101 | |
116 | 181 |
200 | 182 static unsigned char sim_gas_last_used; // Last used gas, to detected a gas switch. |
203 | 183 static unsigned short sim_gas_delay; // Time of gas-switch-stop ends [min on dive]. |
184 static unsigned short sim_dive_mins; // Simulated dive time. | |
200 | 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 | |
212 | 193 static unsigned char deco_gas_change1; // new in v.101 |
194 static unsigned char deco_gas_change2; // new in v.109 | |
195 static unsigned char deco_gas_change3; // new in v.109 | |
196 static unsigned char deco_gas_change4; // new in v.109 | |
197 static unsigned char deco_gas_change5; // new in v.109 | |
167 | 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 |
212 | 230 static unsigned char DBG_deco_gas_change; // new in v.108 |
164 | 231 static float DBG_pres_surface; // new in v.108 |
232 static float DBG_GF_low; // new in v.108 | |
233 static float DBG_GF_high; // new in v.108 | |
234 static float DBG_const_ppO2; // new in v.108 | |
235 static float DBG_deco_ppO2_change; // new in v.108 | |
236 static float DBG_deco_ppO2; // new in v.108 | |
237 static float DBG_deco_N2_ratio; // new in v.108 | |
238 static float DBG_deco_He_ratio; // 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... | |
203 | 261 rom const rom unsigned short md_pi[] = |
116 | 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 { |
203 | 408 overlay unsigned short 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 | |
203 | 476 static short 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_depth_limt |
192 | 602 // low_depth |
167 | 603 // |
171 | 604 static void calc_nextdecodepth(void) |
167 | 605 { |
212 | 606 //---- ZH-L16 + GRADIENT FACTOR model ------------------------------------ |
116 | 607 if (char_I_deco_model == 1) |
608 { | |
192 | 609 // Recompute leading gas limit, at current depth: |
610 overlay float depth = (temp_deco - pres_surface) / 0.09995; | |
611 assert( depth >= 0.0 ); | |
203 | 612 assert( low_depth < 255 ); |
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. | |
203 | 623 overlay unsigned char first_stop = 3 * (short)(0.99 + (sim_lead_tissue_limit - pres_surface) / 0.29985); |
192 | 624 assert( first_stop < 128 ); |
171 | 625 |
212 | 626 // Apply correction for the shallowest stop. |
192 | 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 // Check all stops until one is higher than tolerated presure |
631 while(first_stop > 0) | |
632 { | |
633 overlay unsigned char next_stop; // Next index (0..30) | |
634 overlay float pres_stop; // Next depth (0m..90m) | |
171 | 635 |
192 | 636 if( first_stop <= char_I_depth_last_deco ) // new in v104 |
637 next_stop = 0; | |
638 else | |
639 next_stop = first_stop - 3; // Index of next (upper) stop. | |
640 if( first_stop == 3 ) | |
641 first_stop = char_I_depth_last_deco; | |
167 | 642 |
192 | 643 pres_stop = next_stop * 0.09995 // Meters to bar |
644 + pres_surface; | |
645 | |
212 | 646 // Keep GF_low until a first stop depth is found: |
647 if( next_stop >= low_depth ) | |
648 sim_limit( GF_low ); | |
649 else | |
650 // current GF is GF_high - alpha (GF_high - GF_low) | |
651 // With alpha = currentDepth / maxDepth, hence in [0..1] | |
652 sim_limit( GF_high - next_stop * locked_GF_step ); | |
171 | 653 |
192 | 654 // upper limit (lowest pressure tolerated): |
655 if( sim_lead_tissue_limit >= pres_stop ) // check if ascent to next deco stop is ok | |
656 break; | |
657 | |
658 // Else, validate that stop and loop... | |
659 first_stop = next_stop; | |
660 } | |
171 | 661 |
212 | 662 // Is it a new deepest first stop ? If yes this is the reference for |
663 // the varying gradient factor. So reset that: | |
664 if( first_stop > low_depth ) | |
665 { | |
666 // Store the deepest stop depth, as reference for GF_low. | |
667 low_depth = first_stop; | |
668 locked_GF_step = GF_delta / low_depth; | |
669 } | |
670 | |
671 // next stop is the last validated depth found, aka first_stop | |
192 | 672 temp_depth_limit = first_stop; // Stop depth, in meter. |
673 } | |
171 | 674 else |
192 | 675 temp_depth_limit = 0; // stop depth, in meter. |
116 | 676 } |
167 | 677 else //---- ZH-L16 model ------------------------------------------------- |
116 | 678 { |
192 | 679 overlay float pres_gradient; |
680 | |
681 // Original model | |
116 | 682 // optimized in v.101 |
171 | 683 // char_I_depth_last_deco included in v.101 |
116 | 684 |
192 | 685 // Compute sim_lead_tissue_limit too, but just once. |
686 sim_limit(1.0); | |
687 | |
688 pres_gradient = sim_lead_tissue_limit - pres_surface; | |
167 | 689 if (pres_gradient >= 0) |
116 | 690 { |
192 | 691 pres_gradient /= 0.29985; // Bar --> stop number; |
203 | 692 temp_depth_limit = 3 * (short) (pres_gradient + 0.99); // --> meter : depth for deco |
212 | 693 if (temp_depth_limit != 0) // At surface ? |
116 | 694 { |
171 | 695 if (temp_depth_limit < char_I_depth_last_deco) // Implement last stop at 4m/5m/6m... |
696 temp_depth_limit = char_I_depth_last_deco; | |
697 } | |
698 } | |
116 | 699 else |
192 | 700 temp_depth_limit = 0; // stop depth, in meter. |
171 | 701 } |
212 | 702 |
703 //---- Check gas change -------------------------------------------------- | |
704 check_gas_switch(); // Update temp_depth_limit if there is a change, | |
705 // Calculate N2_ratio and He_ratio too. | |
167 | 706 } |
116 | 707 |
167 | 708 ////////////////////////////////////////////////////////////////////////////// |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
709 // copy_deco_table |
167 | 710 // |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
711 // 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
|
712 // while computing the next set. |
167 | 713 // |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
714 static void copy_deco_table(void) |
116 | 715 { |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
716 // 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
|
717 // order, it will be hard to find... |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
718 char_O_first_deco_depth = internal_deco_depth[0]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
719 char_O_first_deco_time = internal_deco_time [0]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
720 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
721 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
|
722 { |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
723 overlay unsigned char x, y; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
724 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
725 //---- First: search the first non-null depth |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
726 for(x=31; x != 0; --x) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
727 if( internal_deco_depth[x] != 0 ) break; |
116 | 728 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
729 //---- Second: copy to output table (in reverse order) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
730 for(y=0; y<32; y++, --x) |
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 char_O_deco_depth[y] = internal_deco_depth[x]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
733 char_O_deco_time [y] = internal_deco_time [x]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
734 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
735 // Stop only once the last transfer is done. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
736 if( x == 0 ) break; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
737 } |
116 | 738 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
739 //---- Third: fill table end with null |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
740 for(y++; y<32; y++) |
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 char_O_deco_time [y] = 0; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
743 char_O_deco_depth[y] = 0; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
744 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
745 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
746 else //---- Straight copy ------------------------------------------------ |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
747 { |
167 | 748 overlay unsigned char x; |
749 | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
750 for(x=0; x<32; x++) |
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 char_O_deco_depth[x] = internal_deco_depth[x]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
753 char_O_deco_time [x] = internal_deco_time [x]; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
754 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
755 } |
167 | 756 } |
116 | 757 |
167 | 758 ////////////////////////////////////////////////////////////////////////////// |
116 | 759 // temp_tissue_safety // |
167 | 760 // |
116 | 761 // outsourced in v.102 |
167 | 762 // |
763 // Apply safety factors for brand ZH-L16 model. | |
764 // | |
765 static void temp_tissue_safety(void) | |
116 | 766 { |
197 | 767 assert( 0.0 < float_desaturation_multiplier && float_desaturation_multiplier <= 1.0 ); |
768 assert( 1.0 <= float_saturation_multiplier && float_saturation_multiplier <= 2.0 ); | |
769 | |
167 | 770 if( char_I_deco_model == 0 ) |
116 | 771 { |
772 if (temp_tissue < 0.0) | |
126 | 773 temp_tissue *= float_desaturation_multiplier; |
116 | 774 else |
126 | 775 temp_tissue *= float_saturation_multiplier; |
116 | 776 } |
167 | 777 } |
116 | 778 |
167 | 779 ////////////////////////////////////////////////////////////////////////////// |
780 ////////////////////////////////////////////////////////////////////////////// | |
116 | 781 // ** THE JUMP-IN CODE ** |
782 // ** for the asm code ** | |
167 | 783 ////////////////////////////////////////////////////////////////////////////// |
784 ////////////////////////////////////////////////////////////////////////////// | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
785 |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
786 void fillDataStack(void) |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
787 { |
184 | 788 #ifndef CROSS_COMPILE |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
789 _asm |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
790 LFSR 1,C_STACK |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
791 MOVLW 0xCC |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
792 loop: MOVWF POSTINC1,0 |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
793 TSTFSZ FSR1L,0 |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
794 BRA loop |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
795 |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
796 LFSR 1,C_STACK |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
797 LFSR 2,C_STACK |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
798 _endasm |
184 | 799 #endif |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
800 } |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
801 |
167 | 802 ////////////////////////////////////////////////////////////////////////////// |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
803 // 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
|
804 // 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
|
805 // .lkr script). |
184 | 806 #ifdef CROSS_COMPILE |
807 # define RESET_C_STACK | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
808 #else |
184 | 809 # ifdef __DEBUG |
810 # define RESET_C_STACK fillDataStack(); | |
811 # else | |
812 # define RESET_C_STACK \ | |
813 _asm \ | |
814 LFSR 1, C_STACK \ | |
815 LFSR 2, C_STACK \ | |
816 _endasm | |
817 # endif | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
818 #endif |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
819 |
167 | 820 ////////////////////////////////////////////////////////////////////////////// |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
821 // Called every 2 seconds during diving. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
822 // update tissues every time. |
184 | 823 // |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
824 // Every 6 seconds (or slower when TTS > 16): |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
825 // - 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
|
826 // - update ascent time, |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
827 // - 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
|
828 // |
116 | 829 void deco_calc_hauptroutine(void) |
830 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
831 RESET_C_STACK |
116 | 832 calc_hauptroutine(); |
833 int_O_desaturation_time = 65535; | |
834 } | |
835 | |
167 | 836 ////////////////////////////////////////////////////////////////////////////// |
184 | 837 // Reset decompression model: |
838 // + Set all tissues to equilibrium with Air at ambient pressure. | |
839 // + Reset last stop to 0m | |
840 // + Reset all model output. | |
116 | 841 void deco_clear_tissue(void) |
842 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
843 RESET_C_STACK |
116 | 844 clear_tissue(); |
845 } | |
846 | |
167 | 847 ////////////////////////////////////////////////////////////////////////////// |
848 | |
116 | 849 void deco_calc_wo_deco_step_1_min(void) |
850 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
851 RESET_C_STACK |
116 | 852 calc_wo_deco_step_1_min(); |
853 deco_calc_desaturation_time(); | |
854 } | |
855 | |
167 | 856 ////////////////////////////////////////////////////////////////////////////// |
857 | |
116 | 858 void deco_debug(void) |
859 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
860 RESET_C_STACK |
116 | 861 } |
862 | |
167 | 863 ////////////////////////////////////////////////////////////////////////////// |
201 | 864 // Calculate gas switches |
865 // | |
866 // | |
212 | 867 // Input: N2_ratio, He_ratio. |
868 // deco_gas_change* | |
869 // sim_gas_delay, sim_gas_last_used, sim_dive_mins. | |
870 // | |
871 // Output: calc_N2_ratio, calc_He_ratio | |
872 // temp_depth_limit, sim_gas_delay, sim_gas_last_used IFF the is a switch. | |
873 // | |
874 static void check_gas_switch(void) | |
201 | 875 { |
203 | 876 overlay unsigned char temp_gas_switch = 0; |
212 | 877 overlay unsigned char switch_deco = 0; |
201 | 878 |
879 calc_N2_ratio = N2_ratio; | |
880 calc_He_ratio = He_ratio; | |
881 | |
882 if (char_I_const_ppO2 == 0) | |
883 { | |
884 // Keep selecting the best gas during the ascent simulation. | |
212 | 885 // Add a one meter margin in depth comparaison. |
886 if( deco_gas_change1 && ((temp_depth_limit-1) <= deco_gas_change1)) | |
201 | 887 { |
888 calc_N2_ratio = deco_N2_ratio1; | |
889 calc_He_ratio = deco_He_ratio1; | |
890 temp_gas_switch = 1; | |
891 switch_deco = deco_gas_change1; | |
892 } | |
212 | 893 if(deco_gas_change2 && ((temp_depth_limit-1) <= deco_gas_change2)) |
201 | 894 { |
895 calc_N2_ratio = char_I_deco_N2_ratio2 * 0.01; | |
896 calc_He_ratio = char_I_deco_He_ratio2 * 0.01; | |
897 temp_gas_switch = 2; | |
898 switch_deco = deco_gas_change2; | |
899 } | |
212 | 900 if(deco_gas_change3 && ((temp_depth_limit-1) <= deco_gas_change3)) |
201 | 901 { |
902 calc_N2_ratio = char_I_deco_N2_ratio3 * 0.01; | |
903 calc_He_ratio = char_I_deco_He_ratio3 * 0.01; | |
904 temp_gas_switch = 3; | |
905 switch_deco = deco_gas_change3; | |
906 } | |
212 | 907 if(deco_gas_change4 && ((temp_depth_limit-1) <= deco_gas_change4)) |
201 | 908 { |
909 calc_N2_ratio = char_I_deco_N2_ratio4 * 0.01; | |
910 calc_He_ratio = char_I_deco_He_ratio4 * 0.01; | |
911 temp_gas_switch = 4; | |
912 switch_deco = deco_gas_change4; | |
913 } | |
212 | 914 if(deco_gas_change5 && ((temp_depth_limit-1) <= deco_gas_change5)) |
201 | 915 { |
916 calc_N2_ratio = char_I_deco_N2_ratio5 * 0.01; | |
917 calc_He_ratio = char_I_deco_He_ratio5 * 0.01; | |
918 temp_gas_switch = 5; | |
919 switch_deco = deco_gas_change5; | |
920 } | |
921 } | |
922 | |
203 | 923 // If there is a better gas available |
924 if( temp_gas_switch ) | |
201 | 925 { |
212 | 926 // Should restart gas-switch delay only when gas do changes... |
203 | 927 // sim_gas_last_used: used to detect just once in each ascent simu. |
928 // N2_ratio : used to detect when already breathing that gas. | |
929 if( sim_gas_last_used < temp_gas_switch | |
930 && sim_gas_delay <= sim_dive_mins | |
931 && (calc_N2_ratio != N2_ratio || calc_He_ratio != He_ratio)) | |
932 { | |
933 sim_gas_last_used = temp_gas_switch; | |
934 sim_gas_delay = read_custom_function(55); | |
201 | 935 |
203 | 936 // Apply depth correction ONLY if CF#55 is not null: |
937 if( sim_gas_delay > 0 ) | |
938 { | |
939 sim_gas_delay += sim_dive_mins; | |
212 | 940 temp_depth_limit = switch_deco; |
203 | 941 } |
201 | 942 } |
943 } | |
203 | 944 else |
945 sim_gas_delay = 0; | |
201 | 946 |
947 assert( 0.0 <= calc_N2_ratio && calc_N2_ratio <= 0.95 ); | |
948 assert( 0.0 <= calc_He_ratio && calc_He_ratio <= 0.95 ); | |
949 assert( (calc_N2_ratio + calc_He_ratio) <= 1.00 ); | |
950 } | |
951 | |
952 ////////////////////////////////////////////////////////////////////////////// | |
953 // | |
212 | 954 // Input: calc_N2_ratio, calc_He_ratio : simulated gas mix. |
955 // temp_deco : simulated respiration pressure + security offset (deco_distance) | |
956 // Water-vapor pressure inside lumbs (ppWVapour). | |
957 // | |
958 // Output: ppO2, ppHe. | |
959 // | |
960 static void sim_alveolar_presures(void) | |
201 | 961 { |
212 | 962 overlay float deco_diluent = temp_deco; // new in v.101 |
963 | |
964 //---- CCR mode : deco gas switch ? -------------------------------------- | |
201 | 965 if (char_I_const_ppO2 != 0) |
966 { | |
212 | 967 // In CCR mode, calc_XX_ratio == XX_ratio. |
968 if( temp_deco > deco_ppO2_change ) | |
969 deco_diluent = ((temp_deco - const_ppO2)/(calc_N2_ratio + calc_He_ratio)); | |
201 | 970 else |
212 | 971 deco_diluent = ((temp_deco - deco_ppO2)/(calc_N2_ratio + calc_He_ratio)); |
972 | |
973 if (deco_diluent > temp_deco) | |
974 deco_diluent = temp_deco; | |
201 | 975 } |
976 | |
212 | 977 // Take deco offset into account, but not at surface. |
978 if( deco_diluent > pres_surface ) | |
979 deco_diluent += float_deco_distance; | |
980 | |
981 if( deco_diluent > ppWVapour ) | |
201 | 982 { |
983 ppO2 = calc_N2_ratio * (deco_diluent - ppWVapour); | |
984 ppHe = calc_He_ratio * (deco_diluent - ppWVapour); | |
985 } | |
986 else | |
987 { | |
988 ppO2 = 0.0; | |
989 ppHe = 0.0; | |
990 } | |
991 assert( 0.0 <= ppO2 && ppO2 < 14.0 ); | |
992 assert( 0.0 <= ppHe && ppHe < 14.0 ); | |
993 } | |
994 | |
995 ////////////////////////////////////////////////////////////////////////////// | |
167 | 996 // clear_tissue |
997 // | |
165 | 998 // optimized in v.101 (var_N2_a) |
167 | 999 // |
1000 // preload tissues with standard pressure for the given ambient pressure. | |
1001 // Note: fixed N2_ratio for standard air. | |
201 | 1002 // |
167 | 1003 static void clear_tissue(void) |
116 | 1004 { |
1005 flag_in_divemode = 0; | |
1006 int_O_DBS_bitfield = 0; | |
1007 int_O_DBS2_bitfield = 0; | |
1008 int_O_DBG_pre_bitfield = 0; | |
1009 int_O_DBG_post_bitfield = 0; | |
1010 char_O_NDL_at_20mtr = 255; | |
1011 | |
167 | 1012 // Kludge: the 0.0002 of 0.7902 are missing with standard air. |
1013 N2_ratio = 0.7902; | |
212 | 1014 pres_respiration = int_I_pres_respiration * 0.001; |
164 | 1015 |
167 | 1016 for(ci=0; ci<16; ci++) |
126 | 1017 { |
197 | 1018 // cycle through the 16 Bühlmann tissues |
200 | 1019 overlay float p = N2_ratio * (pres_respiration - ppWVapour); |
164 | 1020 pres_tissue[ci] = p; |
197 | 1021 |
184 | 1022 read_buhlmann_coefficients(-1); |
165 | 1023 |
1024 p = (p - var_N2_a) * var_N2_b ; | |
164 | 1025 if( p < 0.0 ) |
1026 p = 0.0; | |
1027 pres_tissue_limit[ci] = p; | |
1028 | |
197 | 1029 // cycle through the 16 Bühlmann tissues for Helium |
167 | 1030 (pres_tissue+16)[ci] = 0.0; |
126 | 1031 } // for 0 to 16 |
116 | 1032 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1033 clear_deco_table(); |
126 | 1034 char_O_deco_status = 0; |
1035 char_O_nullzeit = 0; | |
168 | 1036 int_O_ascenttime = 0; |
126 | 1037 char_O_gradient_factor = 0; |
1038 char_O_relative_gradient_GF = 0; | |
197 | 1039 char_I_depth_last_deco = 0; // for compatibility with v.101pre_no_last_deco |
167 | 1040 } |
116 | 1041 |
167 | 1042 ////////////////////////////////////////////////////////////////////////////// |
1043 // calc_hauptroutine | |
1044 // | |
1045 // this is the major code in dive mode calculates: | |
116 | 1046 // the tissues, |
167 | 1047 // the bottom time, |
1048 // and simulates the ascend with all deco stops. | |
171 | 1049 // |
1050 // The deco_state sequence is : | |
1051 // 3 (at surface) | |
1052 // +---> 0 : calc nullzeit | |
1053 // | 2 : simulate ascent to first stop (at 10m/min, less that 16x 1min simu) | |
1054 // | +-> 1 : simulate up to 16min of stops. | |
1055 // | +------< not finished | |
1056 // +--------< finish | |
1057 // | |
167 | 1058 static void calc_hauptroutine(void) |
116 | 1059 { |
186 | 1060 static float backup_GF_step; |
192 | 1061 static unsigned char backup_low_depth; |
201 | 1062 static unsigned char backup_gas_used; |
203 | 1063 static unsigned char backup_gas_delay; |
186 | 1064 |
116 | 1065 calc_hauptroutine_data_input(); |
1066 | |
1067 if(!flag_in_divemode) | |
1068 { | |
1069 flag_in_divemode = 1; | |
1070 create_dbs_set_dbg_and_ndl20mtr(); | |
1071 } | |
1072 else | |
1073 check_pre_dbg(); | |
1074 | |
1075 calc_hauptroutine_update_tissues(); | |
1076 calc_gradient_factor(); | |
1077 | |
167 | 1078 // toggle between calculation for nullzeit (bottom time), |
1079 // deco stops | |
1080 // and more deco stops (continue) | |
1081 switch( char_O_deco_status ) | |
116 | 1082 { |
186 | 1083 case 3: //---- At surface: start a new dive ------------------------------ |
1084 clear_deco_table(); | |
1085 copy_deco_table(); | |
200 | 1086 int_O_ascenttime = 0; // Reset DTR. |
1087 char_O_nullzeit = 0; // Reset bottom time. | |
1088 char_O_deco_status = 0; // Calc bottom-time/nullzeit next iteration. | |
201 | 1089 |
1090 // Values that should be reset just once for the full real dive | |
1091 // (not every time we simulate an ascent): | |
1092 low_depth = 0; // Reset GF history. | |
192 | 1093 backup_low_depth = 255; // backup is empty... |
203 | 1094 sim_gas_last_used = 0; // Reset gas switch history. |
1095 sim_gas_delay = 0; | |
1096 sim_dive_mins = 0; | |
201 | 1097 |
203 | 1098 break; |
186 | 1099 |
167 | 1100 case 0: //---- bottom time ----------------------------------------------- |
1101 calc_nullzeit(); | |
1102 check_ndl(); | |
186 | 1103 char_O_deco_status = 2; // calc ascent next time. |
1104 break; | |
1105 | |
1106 case 2: //---- Simulate ascent to first stop ----------------------------- | |
201 | 1107 // Backup ascention state, so the simulation won't polute the real |
1108 // dive data. | |
186 | 1109 backup_GF_step = locked_GF_step; |
192 | 1110 backup_low_depth = low_depth; |
203 | 1111 |
1112 // Check proposed gas at begin of ascent simulation | |
1113 sim_dive_mins = int_I_divemins; // and time. | |
212 | 1114 temp_depth_limit = (int)(0.95 + (pres_respiration - pres_surface) / 0.09985) ; // Starts from current real depth. |
203 | 1115 check_gas_switch(); |
1116 | |
1117 backup_gas_used = sim_gas_last_used;// And save for later simu steps. | |
1118 backup_gas_delay = sim_gas_delay; | |
186 | 1119 |
1120 sim_ascent_to_first_stop(); | |
201 | 1121 |
1122 char_O_deco_status = 1; // Calc stops next time (deco or gas switch). | |
167 | 1123 break; |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1124 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1125 case 1: //---- Simulate stops -------------------------------------------- |
167 | 1126 calc_hauptroutine_calc_deco(); |
186 | 1127 |
1128 // If simulation is finished, restore the GF low reference, so that | |
1129 // next ascent simulation is done from the current depth: | |
192 | 1130 if( char_O_deco_status == 0 && backup_low_depth != 255) |
186 | 1131 { |
1132 locked_GF_step = backup_GF_step; | |
192 | 1133 low_depth = backup_low_depth; |
201 | 1134 sim_gas_last_used = backup_gas_used; |
203 | 1135 sim_gas_delay = backup_gas_delay; |
1136 | |
1137 backup_low_depth = 255; | |
186 | 1138 } |
167 | 1139 break; |
116 | 1140 } |
167 | 1141 |
116 | 1142 check_post_dbg(); |
1143 } | |
1144 | |
167 | 1145 ////////////////////////////////////////////////////////////////////////////// |
1146 // calc_hauptroutine_data_input | |
1147 // | |
1148 // Reset all C-code dive parameters from their ASM-code values. | |
1149 // Detect gas change condition. | |
1150 // | |
116 | 1151 void calc_hauptroutine_data_input(void) |
1152 { | |
203 | 1153 overlay short int_temp; |
126 | 1154 |
197 | 1155 pres_respiration = int_I_pres_respiration * 0.001; |
1156 pres_surface = int_I_pres_surface * 0.001; | |
1157 N2_ratio = char_I_N2_ratio * 0.01; | |
1158 He_ratio = char_I_He_ratio * 0.01; | |
1159 deco_N2_ratio1 = char_I_deco_N2_ratio1 * 0.01; | |
1160 deco_He_ratio1 = char_I_deco_He_ratio1 * 0.01; | |
1161 float_deco_distance = char_I_deco_distance * 0.01; // Get offset is in mbar. | |
116 | 1162 |
126 | 1163 // ____________________________________________________ |
1164 // | |
1165 // _____________ G A S _ C H A N G E S ________________ | |
1166 // ____________________________________________________ | |
1167 | |
212 | 1168 // Keep a margin of 150mbar = 1.50m |
1169 int_temp = (int_I_pres_respiration - int_I_pres_surface) | |
1170 + MBAR_REACH_GASCHANGE_AUTO_CHANGE_OFF; | |
126 | 1171 |
212 | 1172 deco_gas_change1 = 0; |
1173 deco_gas_change2 = 0; | |
1174 deco_gas_change3 = 0; | |
1175 deco_gas_change4 = 0; | |
1176 deco_gas_change5 = 0; | |
116 | 1177 |
203 | 1178 // Gas are selectable if we did not pass the change depth by more than 1.50m: |
167 | 1179 if(char_I_deco_gas_change1) |
126 | 1180 { |
203 | 1181 if( int_temp > 100 *(short)char_I_deco_gas_change1 ) |
212 | 1182 deco_gas_change1 = char_I_deco_gas_change1; |
126 | 1183 } |
1184 if(char_I_deco_gas_change2) | |
1185 { | |
203 | 1186 if( int_temp > 100 *(short)char_I_deco_gas_change2 ) |
212 | 1187 deco_gas_change2 = char_I_deco_gas_change2; |
126 | 1188 } |
1189 if(char_I_deco_gas_change3) | |
1190 { | |
203 | 1191 if( int_temp > 100 *(short)char_I_deco_gas_change3 ) |
212 | 1192 deco_gas_change3 = char_I_deco_gas_change3; |
126 | 1193 } |
1194 if(char_I_deco_gas_change4) | |
1195 { | |
203 | 1196 if( int_temp > 100 *(short)char_I_deco_gas_change4 ) |
212 | 1197 deco_gas_change4 = char_I_deco_gas_change4; |
126 | 1198 } |
1199 if(char_I_deco_gas_change5) | |
1200 { | |
203 | 1201 if( int_temp > 100 *(short)char_I_deco_gas_change5 ) |
212 | 1202 deco_gas_change5 = char_I_deco_gas_change5; |
126 | 1203 } |
116 | 1204 |
197 | 1205 const_ppO2 = char_I_const_ppO2 * 0.01; |
1206 deco_ppO2_change = char_I_deco_ppO2_change / 99.95 | |
1207 + pres_surface | |
1208 + float_deco_distance; | |
1209 deco_ppO2 = char_I_deco_ppO2 * 0.01; | |
1210 float_desaturation_multiplier = char_I_desaturation_multiplier * 0.01; | |
1211 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
1212 GF_low = char_I_GF_Low_percentage * 0.01; | |
1213 GF_high = char_I_GF_High_percentage * 0.01; | |
126 | 1214 GF_delta = GF_high - GF_low; |
116 | 1215 } |
1216 | |
167 | 1217 ////////////////////////////////////////////////////////////////////////////// |
171 | 1218 // |
1219 // | |
116 | 1220 void calc_hauptroutine_update_tissues(void) |
1221 { | |
197 | 1222 assert( 0.00 <= N2_ratio && N2_ratio <= 1.00 ); |
1223 assert( 0.00 <= He_ratio && He_ratio <= 1.00 ); | |
1224 assert( (N2_ratio + He_ratio) <= 0.95 ); | |
1225 | |
1226 if (char_I_const_ppO2 == 0) // new in v.101 | |
167 | 1227 pres_diluent = pres_respiration; // new in v.101 |
186 | 1228 else |
1229 { | |
167 | 1230 pres_diluent = ((pres_respiration - const_ppO2)/(N2_ratio + He_ratio)); // new in v.101 |
186 | 1231 if (pres_diluent > pres_respiration) // new in v.101 |
1232 pres_diluent = pres_respiration; // new in v.101 | |
1233 } | |
200 | 1234 if (pres_diluent > ppWVapour) // new in v.101 |
116 | 1235 { |
200 | 1236 ppO2 = N2_ratio * (pres_diluent - ppWVapour); // changed in v.101 |
1237 ppHe = He_ratio * (pres_diluent - ppWVapour); // changed in v.101 | |
116 | 1238 char_O_diluent = (char)(pres_diluent/pres_respiration*100.0); |
1239 } | |
167 | 1240 else // new in v.101 |
116 | 1241 { |
200 | 1242 ppO2 = 0.0; // new in v.101 |
1243 ppHe = 0.0; // new in v.101 | |
116 | 1244 char_O_diluent = 0; |
1245 } | |
1246 | |
1247 if(!char_I_step_is_1min) | |
192 | 1248 calc_tissue(0); |
116 | 1249 else |
192 | 1250 calc_tissue(1); |
186 | 1251 |
192 | 1252 // Calc limit for surface, ie. GF_high. |
1253 calc_limit(GF_high); | |
186 | 1254 |
203 | 1255 int_O_gtissue_limit = (short)(calc_lead_tissue_limit * 1000); |
1256 int_O_gtissue_press = (short)((pres_tissue[char_O_gtissue_no] + (pres_tissue+16)[char_O_gtissue_no]) * 1000); | |
192 | 1257 |
1258 // if guiding tissue can not be exposed to surface pressure immediately | |
1259 if( calc_lead_tissue_limit > pres_surface && char_O_deco_status == 0) | |
116 | 1260 { |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1261 char_O_nullzeit = 0; // deco necessary |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1262 char_O_deco_status = 2; // calculate ascent on next iteration. |
116 | 1263 } |
171 | 1264 } |
1265 | |
116 | 1266 |
167 | 1267 ////////////////////////////////////////////////////////////////////////////// |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1268 // Compute stops. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1269 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1270 // 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
|
1271 // 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
|
1272 // |
116 | 1273 void calc_hauptroutine_calc_deco(void) |
1274 { | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1275 overlay unsigned char loop; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1276 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1277 for(loop = 0; loop < 16; ++loop) |
116 | 1278 { |
212 | 1279 overlay float new_presure; |
1280 overlay unsigned char backup_gas_last_used; | |
1281 | |
197 | 1282 // Do not ascent while doing a gas switch. |
203 | 1283 if( sim_gas_delay <= sim_dive_mins ) |
1284 { | |
212 | 1285 backup_gas_last_used = sim_gas_last_used; |
197 | 1286 calc_nextdecodepth(); |
116 | 1287 |
203 | 1288 //---- Finish computations once surface is reached --------------- |
1289 if( temp_depth_limit == 0 ) | |
1290 { | |
1291 copy_deco_table(); | |
1292 calc_ascenttime(); | |
1293 char_O_deco_status = 0; // calc nullzeit next time. | |
1294 return; | |
1295 } | |
212 | 1296 } |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1297 |
212 | 1298 //---- Can we ascent to that stop at once, or is it just ascenting ? |
1299 new_presure = temp_depth_limit * 0.09985 // Convert to relative bar, | |
1300 + pres_surface; // To absolute, | |
1301 | |
1302 if( (temp_deco - new_presure) > 1.0 ) // More than 10m/mn ? | |
1303 { | |
1304 temp_deco -= 1.0; // Just keep ascending. | |
1305 sim_gas_delay = 0; // Reset gas delay, | |
1306 sim_gas_last_used = backup_gas_last_used; // and usage history. | |
1307 } | |
1308 else | |
1309 { | |
1310 temp_deco = new_presure; | |
1311 update_deco_table(); // This is a one minute stops. | |
203 | 1312 } |
197 | 1313 |
203 | 1314 //---- Then update tissue and decoplan ------------------------------- |
1315 sim_dive_mins++; // Advance simulated time by 1 minute. | |
212 | 1316 sim_alveolar_presures(); // Updates ppN2 and ppHe. |
192 | 1317 sim_tissue(1); // Simulate compartiments for 1 minute. |
116 | 1318 } |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1319 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1320 // Surface not reached, need more stops... |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1321 char_O_deco_status = 1; // calc more stops next time. |
116 | 1322 } |
1323 | |
167 | 1324 ////////////////////////////////////////////////////////////////////////////// |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1325 // Simulation ascention to first deco stop. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1326 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1327 // 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
|
1328 // 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
|
1329 // (or we are already in deep shit). |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1330 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1331 void sim_ascent_to_first_stop(void) |
116 | 1332 { |
171 | 1333 update_startvalues(); |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1334 clear_deco_table(); |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1335 |
200 | 1336 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
|
1337 |
212 | 1338 // Do we have a gas switch going on ? |
1339 if( sim_gas_delay > sim_dive_mins ) | |
1340 return; | |
1341 | |
203 | 1342 // Loop until first stop, gas switch, or surface is reached. |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1343 for(;;) |
116 | 1344 { |
203 | 1345 // No: try ascending 1 full minute. |
1346 temp_deco -= 1.0; // Ascent 1 min, at 10m/min. == 1bar/min. | |
1347 | |
212 | 1348 // Compute sim_lead_tissue_limit at GF_low (deepest stop). |
1349 sim_limit(GF_low); | |
1350 | |
1351 // Did we reach deepest remaining stop ? | |
1352 if( temp_deco <= sim_lead_tissue_limit ) | |
1353 { | |
1354 temp_deco += 1.0; // Restore last correct depth. | |
1355 break; // Return stop found ! | |
1356 } | |
1357 | |
203 | 1358 // Did we reach surface ? |
1359 if( temp_deco <= pres_surface ) | |
1360 { | |
1361 temp_deco = pres_surface; // Yes: finished ! | |
1362 break; | |
1363 } | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1364 |
212 | 1365 // Check gas change 5 meter below new depth. |
1366 temp_depth_limit = (temp_deco + 0.5 - pres_surface) / 0.09985; | |
1367 check_gas_switch(); | |
1368 if( sim_gas_delay > sim_dive_mins ) | |
1369 break; | |
171 | 1370 |
203 | 1371 sim_dive_mins++; // Advance simulated time by 1 minute. |
212 | 1372 sim_alveolar_presures(); // Still uses temp_deco. |
203 | 1373 sim_tissue(1); // and update tissues for 1 min. |
1374 } | |
167 | 1375 } |
116 | 1376 |
167 | 1377 ////////////////////////////////////////////////////////////////////////////// |
1378 // calc_tissue | |
1379 // | |
116 | 1380 // optimized in v.101 |
167 | 1381 // |
184 | 1382 static void calc_tissue(PARAMETER unsigned char period) |
116 | 1383 { |
126 | 1384 for (ci=0;ci<16;ci++) |
1385 { | |
184 | 1386 read_buhlmann_coefficients(period); // 2 sec or 1 min period. |
116 | 1387 |
126 | 1388 // N2 |
200 | 1389 temp_tissue = (ppO2 - pres_tissue[ci]) * var_N2_e; |
126 | 1390 temp_tissue_safety(); |
167 | 1391 pres_tissue[ci] += temp_tissue; |
165 | 1392 |
126 | 1393 // He |
200 | 1394 temp_tissue = (ppHe - (pres_tissue+16)[ci]) * var_He_e; |
126 | 1395 temp_tissue_safety(); |
167 | 1396 (pres_tissue+16)[ci] += temp_tissue; |
192 | 1397 } |
167 | 1398 } |
116 | 1399 |
167 | 1400 ////////////////////////////////////////////////////////////////////////////// |
192 | 1401 // calc_limit |
167 | 1402 // |
192 | 1403 // New in v.111 : separated from calc_tissue(), and depends on GF value. |
167 | 1404 // |
192 | 1405 static void calc_limit(PARAMETER float GF_current) |
167 | 1406 { |
192 | 1407 char_O_gtissue_no = 255; |
1408 calc_lead_tissue_limit = 0.0; | |
1409 | |
1410 for (ci=0;ci<16;ci++) | |
1411 { | |
1412 overlay float p = pres_tissue[ci] + (pres_tissue+16)[ci]; | |
1413 | |
1414 read_buhlmann_coefficients(-1); // 2 sec or 1 min period. | |
1415 var_N2_a = (var_N2_a * pres_tissue[ci] + var_He_a * (pres_tissue+16)[ci]) / p; | |
1416 var_N2_b = (var_N2_b * pres_tissue[ci] + var_He_b * (pres_tissue+16)[ci]) / p; | |
116 | 1417 |
192 | 1418 // Apply the Eric Baker's varying gradient factor correction. |
1419 // Note: the correction factor depends both on GF and b, | |
1420 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
1421 // so that can change who is the leading gas... | |
1422 // Note: Also depends of the GF. So the calcul is different for | |
1423 // GF_low, current GF, or GF_high... | |
1424 // *BUT* calc_tissue() is used to compute bottom time, | |
1425 // hence what would happend at surface, | |
1426 // hence at GF_high. | |
1427 if( char_I_deco_model == 1 ) | |
1428 p = ( p - var_N2_a * GF_current) * var_N2_b | |
1429 / (GF_current + var_N2_b * (1.0 - GF_current)); | |
1430 else | |
1431 p = (p - var_N2_a) * var_N2_b; | |
1432 if( p < 0 ) p = 0; | |
1433 | |
1434 pres_tissue_limit[ci] = p; | |
1435 if( p > calc_lead_tissue_limit ) | |
1436 { | |
1437 char_O_gtissue_no = ci; | |
1438 calc_lead_tissue_limit = p; | |
1439 } | |
1440 } | |
197 | 1441 |
1442 assert( char_O_gtissue_no < 16 ); | |
1443 assert( 0.0 <= calc_lead_tissue_limit && calc_lead_tissue_limit <= 14.0); | |
167 | 1444 } |
1445 | |
1446 ////////////////////////////////////////////////////////////////////////////// | |
1447 // calc_nullzeit | |
1448 // | |
1449 // calculates the remaining bottom time | |
1450 // | |
1451 // unchanged in v.101 | |
1452 // | |
1453 static void calc_nullzeit(void) | |
1454 { | |
203 | 1455 overlay unsigned char loop; |
200 | 1456 update_startvalues(); |
167 | 1457 |
116 | 1458 char_O_nullzeit = 0; |
167 | 1459 for(loop = 1; loop <= 17; loop++) |
116 | 1460 { |
1461 backup_sim_pres_tissue(); | |
197 | 1462 sim_tissue(2); // 10 min. |
192 | 1463 sim_limit(GF_high); |
167 | 1464 |
192 | 1465 if( sim_lead_tissue_limit > pres_surface ) // changed in v.102 , if guiding tissue can not be exposed to surface pressure immediately |
185 | 1466 { |
1467 restore_sim_pres_tissue(); | |
1468 break; | |
1469 } | |
1470 // Validate once we know its good. | |
1471 char_O_nullzeit += 10; | |
167 | 1472 } |
1473 | |
116 | 1474 if (char_O_nullzeit < 60) |
1475 { | |
167 | 1476 for(loop=1; loop <= 10; loop++) |
116 | 1477 { |
197 | 1478 sim_tissue(1); // 1 min |
192 | 1479 sim_limit(GF_high); |
1480 | |
1481 if( sim_lead_tissue_limit > pres_surface) // changed in v.102 , if guiding tissue can not be exposed to surface pressure immediately | |
185 | 1482 break; |
1483 char_O_nullzeit++; | |
167 | 1484 } |
185 | 1485 } |
1486 } | |
116 | 1487 |
167 | 1488 ////////////////////////////////////////////////////////////////////////////// |
1489 // backup_sim_pres_tissue | |
1490 // | |
116 | 1491 void backup_sim_pres_tissue(void) |
1492 { | |
167 | 1493 overlay unsigned char x; |
1494 | |
1495 for(x = 0; x<32; x++) | |
126 | 1496 sim_pres_tissue_backup[x] = sim_pres_tissue[x]; |
167 | 1497 } |
116 | 1498 |
167 | 1499 ////////////////////////////////////////////////////////////////////////////// |
1500 // restore_sim_pres_tissue | |
1501 // | |
116 | 1502 void restore_sim_pres_tissue(void) |
1503 { | |
167 | 1504 overlay unsigned char x; |
1505 | |
1506 for(x = 0; x<32; x++) | |
126 | 1507 sim_pres_tissue[x] = sim_pres_tissue_backup[x]; |
167 | 1508 } |
116 | 1509 |
167 | 1510 ////////////////////////////////////////////////////////////////////////////// |
1511 // calc_ascenttime | |
1512 // | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1513 static void calc_ascenttime(void) |
116 | 1514 { |
126 | 1515 if (pres_respiration > pres_surface) |
1516 { | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1517 overlay unsigned char x; |
168 | 1518 |
192 | 1519 // + 0.7 to count 1 minute ascent time from 3 meter to surface |
1520 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
|
1521 if (ascent < 0.0) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1522 ascent = 0.0; |
203 | 1523 int_O_ascenttime = (unsigned short)(ascent + 0.99); |
168 | 1524 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1525 for(x=0; x<32 && internal_deco_depth[x]; x++) |
203 | 1526 int_O_ascenttime += (unsigned short)internal_deco_time[x]; |
126 | 1527 } |
1528 else | |
168 | 1529 int_O_ascenttime = 0; |
167 | 1530 } |
116 | 1531 |
167 | 1532 ////////////////////////////////////////////////////////////////////////////// |
1533 // update_startvalues | |
1534 // | |
116 | 1535 // updated in v.102 |
167 | 1536 // |
116 | 1537 void update_startvalues(void) |
1538 { | |
167 | 1539 overlay unsigned char x; |
116 | 1540 |
201 | 1541 // Start ascent simulation with current tissue partial pressures. |
116 | 1542 for (x = 0;x<16;x++) |
1543 { | |
1544 sim_pres_tissue[x] = pres_tissue[x]; | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1545 (sim_pres_tissue+16)[x] = (pres_tissue+16)[x]; |
116 | 1546 sim_pres_tissue_limit[x] = pres_tissue_limit[x]; |
1547 } | |
197 | 1548 |
201 | 1549 // No leading tissue (yet) for this ascent simulation. |
197 | 1550 sim_lead_tissue_limit = 0.0; |
1551 sim_lead_tissue_no = 255; | |
167 | 1552 } |
116 | 1553 |
167 | 1554 ////////////////////////////////////////////////////////////////////////////// |
192 | 1555 // sim_tissue |
167 | 1556 // |
116 | 1557 // optimized in v.101 |
167 | 1558 // |
1559 // Function very simular to calc_tissue, but: | |
1560 // + Use a 1min or 10min period. | |
1561 // + Do it on sim_pres_tissue, instead of pres_tissue. | |
184 | 1562 static void sim_tissue(PARAMETER unsigned char period) |
116 | 1563 { |
192 | 1564 for(ci=0; ci<16; ci++) |
126 | 1565 { |
184 | 1566 read_buhlmann_coefficients(period); // 1 or 10 minute(s) interval |
165 | 1567 |
126 | 1568 // N2 |
200 | 1569 temp_tissue = (ppO2 - sim_pres_tissue[ci]) * var_N2_e; |
126 | 1570 temp_tissue_safety(); |
167 | 1571 sim_pres_tissue[ci] += temp_tissue; |
126 | 1572 |
1573 // He | |
200 | 1574 temp_tissue = (ppHe - (sim_pres_tissue+16)[ci]) * var_He_e; |
126 | 1575 temp_tissue_safety(); |
167 | 1576 (sim_pres_tissue+16)[ci] += temp_tissue; |
192 | 1577 } |
167 | 1578 } |
116 | 1579 |
167 | 1580 ////////////////////////////////////////////////////////////////////////////// |
192 | 1581 // sim_limit() |
1582 // | |
1583 // New in v.111 | |
1584 // | |
1585 // Function separated from sim_tissue() to allow recomputing limit on | |
1586 // different depth, because it depends on current gradient factor. | |
167 | 1587 // |
192 | 1588 static void sim_limit(PARAMETER float GF_current) |
116 | 1589 { |
212 | 1590 assert( 0.0 < GF_current && GF_current <= 1.0f); |
1591 | |
192 | 1592 sim_lead_tissue_limit = 0.0; |
212 | 1593 sim_lead_tissue_no = 0; // If no one is critic, keep first tissue. |
192 | 1594 |
1595 for(ci=0; ci<16; ci++) | |
1596 { | |
1597 overlay float p = sim_pres_tissue[ci] + (sim_pres_tissue+16)[ci]; | |
1598 | |
1599 read_buhlmann_coefficients(-1); | |
1600 var_N2_a = (var_N2_a * sim_pres_tissue[ci] + var_He_a * (sim_pres_tissue+16)[ci]) / p; | |
1601 var_N2_b = (var_N2_b * sim_pres_tissue[ci] + var_He_b * (sim_pres_tissue+16)[ci]) / p; | |
1602 | |
1603 // Apply the Eric Baker's varying gradient factor correction. | |
1604 // Note: the correction factor depends both on GF and b, | |
1605 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
1606 // so that can change who is the leading gas... | |
1607 // Note: Also depends of the GF_current... | |
1608 // *BUT* calc_tissue() is used to compute bottom time, | |
1609 // hence what would happend at surface, | |
1610 // hence at GF_high. | |
1611 if( char_I_deco_model == 1 ) | |
1612 p = ( p - var_N2_a * GF_current) * var_N2_b | |
1613 / (GF_current + var_N2_b * (1.0 - GF_current)); | |
1614 else | |
1615 p = (p - var_N2_a) * var_N2_b; | |
1616 if( p < 0 ) p = 0; | |
1617 | |
1618 sim_pres_tissue_limit[ci] = p; | |
1619 if( p > sim_lead_tissue_limit ) | |
1620 { | |
1621 sim_lead_tissue_no = ci; | |
1622 sim_lead_tissue_limit = p; | |
1623 } | |
1624 } // for ci | |
197 | 1625 |
1626 assert( sim_lead_tissue_no < 16 ); | |
1627 assert( 0.0 <= sim_lead_tissue_limit && sim_lead_tissue_limit <= 14.0); | |
167 | 1628 } |
116 | 1629 |
167 | 1630 ////////////////////////////////////////////////////////////////////////////// |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1631 // clear_deco_table |
167 | 1632 // |
1633 // unchanged in v.101 | |
1634 // | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1635 static void clear_deco_table(void) |
116 | 1636 { |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1637 overlay unsigned char x; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1638 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1639 for(x=0; x<32; ++x) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1640 { |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1641 internal_deco_time [x] = 0; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1642 internal_deco_depth[x] = 0; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1643 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1644 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1645 |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1646 ////////////////////////////////////////////////////////////////////////////// |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1647 // update_deco_table |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1648 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1649 // Add 1 min to current stop. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1650 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1651 // Inputs: |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1652 // temp_depth_limit = stop's depth, in meters. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1653 // In/Out: |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1654 // internal_deco_depth[] : depth (in meters) of each stops. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1655 // internal_deco_time [] : time (in minutes) of each stops. |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1656 // |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1657 static void update_deco_table() |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1658 { |
192 | 1659 overlay unsigned char x; |
200 | 1660 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
|
1661 |
192 | 1662 for(x=0; x<32; ++x) |
1663 { | |
212 | 1664 // Make sure deco-stops are recorded in order: |
1665 assert( !internal_deco_depth[x] || temp_depth_limit <= internal_deco_depth[x] ); | |
1666 | |
200 | 1667 if( internal_deco_depth[x] == temp_depth_limit ) |
192 | 1668 { |
1669 // Do not overflow (max 255') | |
1670 if( internal_deco_time[x] < 255 ) | |
1671 { | |
1672 internal_deco_time[x]++; | |
1673 return; | |
1674 } | |
1675 // But store extra in the next stop... | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1676 } |
192 | 1677 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1678 if( internal_deco_depth[x] == 0 ) |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1679 { |
192 | 1680 internal_deco_depth[x] = temp_depth_limit; |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1681 internal_deco_time[x] = 1; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1682 return; |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1683 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1684 } |
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1685 |
192 | 1686 // Can't store stops at more than 96m. |
1687 // Or stops at less that 3m too. | |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1688 // Just do nothing with that... |
167 | 1689 } |
116 | 1690 |
167 | 1691 ////////////////////////////////////////////////////////////////////////////// |
1692 // calc_gradient_factor | |
1693 // | |
165 | 1694 // optimized in v.101 (var_N2_a) |
116 | 1695 // new code in v.102 |
167 | 1696 // |
1697 static void calc_gradient_factor(void) | |
116 | 1698 { |
197 | 1699 overlay float gf; |
1700 assert( char_O_gtissue_no < 16 ); | |
1701 | |
116 | 1702 // tissue > respiration (entsaettigungsvorgang) |
1703 // gradient ist wieviel prozent an limit mit basis tissue | |
1704 // dh. 0% = respiration == tissue | |
1705 // dh. 100% = respiration == limit | |
197 | 1706 temp_tissue = pres_tissue[char_O_gtissue_no] + (pres_tissue+16)[char_O_gtissue_no]; |
1707 if( temp_tissue < pres_respiration ) | |
1708 gf = 0.0; | |
1709 else | |
1710 { | |
1711 gf = (temp_tissue - pres_respiration) | |
1712 / (temp_tissue - pres_tissue_limit[char_O_gtissue_no]) | |
1713 * 100.0; | |
1714 if( gf > 255.0 ) gf = 255.0; | |
1715 if( gf < 0.0 ) gf = 0.0; | |
1716 } | |
1717 char_O_gradient_factor = (unsigned char)gf; | |
116 | 1718 |
1719 if (char_I_deco_model == 1) // calculate relative gradient factor | |
1720 { | |
197 | 1721 overlay float rgf; |
1722 | |
1723 if( low_depth == 0 ) | |
1724 rgf = GF_high; | |
1725 else | |
1726 { | |
1727 overlay float temp1 = low_depth * 0.09995; | |
1728 overlay float temp2 = pres_respiration - pres_surface; | |
186 | 1729 |
197 | 1730 if (temp2 <= 0) |
1731 rgf = GF_high; | |
1732 else if (temp2 >= temp1) | |
1733 rgf = GF_low; | |
1734 else | |
1735 rgf = GF_low + (temp1 - temp2)/temp1*GF_delta; | |
1736 } | |
186 | 1737 |
197 | 1738 rgf = gf / rgf; // gf is already in percent |
1739 if( rgf < 0.0 ) rgf = 0.0; | |
1740 if( rgf > 255.0 ) rgf = 255.0; | |
1741 char_O_relative_gradient_GF = (unsigned char)rgf; | |
116 | 1742 } // calc relative gradient factor |
1743 else | |
1744 { | |
186 | 1745 char_O_relative_gradient_GF = char_O_gradient_factor; |
116 | 1746 } |
167 | 1747 } |
116 | 1748 |
167 | 1749 ////////////////////////////////////////////////////////////////////////////// |
1750 // deco_calc_desaturation_time | |
1751 // | |
116 | 1752 // FIXED N2_ratio |
1753 // unchanged in v.101 | |
167 | 1754 // |
116 | 1755 void deco_calc_desaturation_time(void) |
1756 { | |
203 | 1757 overlay unsigned short desat_time; // For a particular compartiment, in min. |
200 | 1758 overlay float temp1; |
197 | 1759 overlay float temp2; |
1760 overlay float temp3; | |
1761 overlay float temp4; | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
1762 RESET_C_STACK |
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
1763 |
126 | 1764 N2_ratio = 0.7902; // FIXED sum as stated in b"uhlmann |
1765 pres_surface = (float)int_I_pres_surface / 1000.0; | |
200 | 1766 ppO2 = N2_ratio * (pres_surface - ppWVapour); |
126 | 1767 int_O_desaturation_time = 0; |
1768 float_desaturation_multiplier = char_I_desaturation_multiplier / 142.0; // new in v.101 (70,42%/100.=142) | |
1769 | |
1770 for (ci=0;ci<16;ci++) | |
1771 { | |
200 | 1772 overlay float var_N2_halftime = buhlmann_ht[ci]; |
1773 overlay float var_He_halftime = (buhlmann_ht+16)[ci]; | |
116 | 1774 |
126 | 1775 // saturation_time (for flight) and N2_saturation in multiples of halftime |
1776 // version v.100: 1.1 = 10 percent distance to totally clean (totally clean is not possible, would take infinite time ) | |
1777 // new in version v.101: 1.07 = 7 percent distance to totally clean (totally clean is not possible, would take infinite time ) | |
1778 // changes in v.101: 1.05 = 5 percent dist to totally clean is new desaturation point for display and noFly calculations | |
1779 // N2 | |
200 | 1780 temp1 = 1.05 * ppO2; |
126 | 1781 temp1 = temp1 - pres_tissue[ci]; |
200 | 1782 temp2 = ppO2 - pres_tissue[ci]; |
126 | 1783 if (temp2 >= 0.0) |
1784 { | |
1785 temp1 = 0; | |
1786 temp2 = 0; | |
1787 } | |
1788 else | |
1789 temp1 = temp1 / temp2; | |
1790 if (temp1 > 0.0) | |
1791 { | |
1792 temp1 = log(1.0 - temp1); | |
1793 temp1 = temp1 / -0.6931; // temp1 is the multiples of half times necessary. | |
1794 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. | |
1795 // minus because log is negative | |
165 | 1796 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 | 1797 } |
1798 else | |
1799 { | |
1800 temp1 = 0; | |
1801 temp2 = 0; | |
1802 } | |
116 | 1803 |
126 | 1804 // He |
167 | 1805 temp3 = 0.1 - (pres_tissue+16)[ci]; |
126 | 1806 if (temp3 >= 0.0) |
1807 { | |
1808 temp3 = 0; | |
1809 temp4 = 0; | |
1810 } | |
1811 else | |
167 | 1812 temp3 = -1.0 * temp3 / (pres_tissue+16)[ci]; |
126 | 1813 if (temp3 > 0.0) |
1814 { | |
1815 temp3 = log(1.0 - temp3); | |
1816 temp3 = temp3 / -0.6931; // temp1 is the multiples of half times necessary. | |
1817 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. | |
1818 // minus because log is negative | |
165 | 1819 temp4 = var_He_halftime * temp3 / float_desaturation_multiplier; // time necessary (in minutes ) for "complete" desaturation, new in v.101 float_desaturation_multiplier |
126 | 1820 } |
1821 else | |
1822 { | |
1823 temp3 = 0; | |
1824 temp4 = 0; | |
1825 } | |
116 | 1826 |
126 | 1827 // saturation_time (for flight) |
1828 if (temp4 > temp2) | |
203 | 1829 desat_time = (unsigned short)temp4; |
126 | 1830 else |
203 | 1831 desat_time = (unsigned short)temp2; |
167 | 1832 if(desat_time > int_O_desaturation_time) |
1833 int_O_desaturation_time = desat_time; | |
116 | 1834 |
126 | 1835 // N2 saturation in multiples of halftime for display purposes |
1836 temp2 = temp1 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 | |
1837 temp2 = temp2 + 80.0; // set center | |
1838 if (temp2 < 0.0) | |
1839 temp2 = 0.0; | |
1840 if (temp2 > 255.0) | |
1841 temp2 = 255.0; | |
1842 char_O_tissue_saturation[ci] = (char)temp2; | |
1843 // He saturation in multiples of halftime for display purposes | |
1844 temp4 = temp3 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 | |
1845 temp4 = temp4 + 80.0; // set center | |
1846 if (temp4 < 0.0) | |
1847 temp4 = 0.0; | |
1848 if (temp4 > 255.0) | |
1849 temp4 = 255.0; | |
167 | 1850 (char_O_tissue_saturation+16)[ci] = (char)temp4; |
126 | 1851 } // for |
167 | 1852 } |
116 | 1853 |
167 | 1854 ////////////////////////////////////////////////////////////////////////////// |
1855 // calc_wo_deco_step_1_min | |
1856 // | |
116 | 1857 // FIXED N2 Ratio |
1858 // optimized in v.101 (...saturation_multiplier) | |
1859 // desaturation slowed down to 70,42% | |
167 | 1860 // |
1861 static void calc_wo_deco_step_1_min(void) | |
116 | 1862 { |
1863 if(flag_in_divemode) | |
1864 { | |
1865 flag_in_divemode = 0; | |
1866 set_dbg_end_of_dive(); | |
1867 } | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
1868 |
126 | 1869 N2_ratio = 0.7902; // FIXED, sum lt. buehlmann |
212 | 1870 pres_respiration = int_I_pres_respiration * 0.001; // assembler code uses different digit system |
1871 pres_surface = int_I_pres_surface * 0.001; // the b"uhlmann formula using pres_surface does not use the N2_ratio | |
200 | 1872 ppO2 = N2_ratio * (pres_respiration - ppWVapour); // ppWVapour is the extra pressure in the body |
1873 ppHe = 0.0; | |
126 | 1874 float_desaturation_multiplier = char_I_desaturation_multiplier / 142.0; // new in v.101 (70,42%/100.=142) |
197 | 1875 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; |
126 | 1876 |
192 | 1877 calc_tissue(1); // update the pressure in the 32 tissues in accordance with the new ambient pressure |
167 | 1878 |
169
e26f49674956
Merge decoplan display for both GF and Buhlmann models.
JeanDo
parents:
168
diff
changeset
|
1879 clear_deco_table(); |
200 | 1880 char_O_deco_status = 3; // surface new in v.102 : stays in surface state. |
126 | 1881 char_O_nullzeit = 0; |
168 | 1882 int_O_ascenttime = 0; |
126 | 1883 calc_gradient_factor(); |
167 | 1884 } |
126 | 1885 |
167 | 1886 ////////////////////////////////////////////////////////////////////////////// |
1887 ////////////////////////////////////////////////////////////////////////////// | |
1888 ////////////////////////////////// deco_hash ///////////////////////////////// | |
1889 ////////////////////////////////////////////////////////////////////////////// | |
1890 ////////////////////////////////////////////////////////////////////////////// | |
126 | 1891 |
184 | 1892 #ifndef CROSS_COMPILE |
116 | 1893 void deco_hash(void) |
1894 { | |
167 | 1895 overlay unsigned char md_i, md_j; // Loop index. |
1896 overlay unsigned char md_t; | |
1897 overlay unsigned char md_buffer[16]; | |
1898 overlay unsigned char md_temp; | |
203 | 1899 overlay unsigned short md_pointer; |
167 | 1900 |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
1901 RESET_C_STACK |
126 | 1902 |
1903 // init | |
164 | 1904 for(md_i=0;md_i<16;md_i++) |
126 | 1905 { |
1906 md_state[md_i] = 0; | |
164 | 1907 char_O_hash[md_i] = 0; |
126 | 1908 } // for md_i 16 |
116 | 1909 |
126 | 1910 _asm |
167 | 1911 movlw 0x01 // md_pi address. |
1912 movwf TBLPTRU,0 | |
1913 movlw 0x7E | |
1914 movwf TBLPTRH,0 | |
1915 movlw 0x00 | |
1916 movwf TBLPTRL,0 | |
126 | 1917 _endasm; |
165 | 1918 md_i = 0; |
1919 do { | |
126 | 1920 _asm |
1921 TBLRDPOSTINC | |
1922 movff TABLAT,md_temp | |
1923 _endasm | |
165 | 1924 md_pi_subst[md_i++] = md_temp; |
1925 } while( md_i != 0 ); | |
1926 | |
126 | 1927 _asm |
167 | 1928 movlw 0x00 |
1929 movwf TBLPTRU,0 | |
1930 movlw 0x00 | |
1931 movwf TBLPTRH,0 | |
1932 movlw 0x00 | |
1933 movwf TBLPTRL,0 | |
126 | 1934 _endasm |
167 | 1935 |
126 | 1936 // cycle buffers |
1937 for (md_pointer=0x0000;md_pointer<0x17f3;md_pointer++) | |
1938 { | |
1939 md_t = 0; | |
1940 for (md_i=0;md_i<16;md_i++) | |
1941 { | |
1942 if(md_pointer == 9) | |
164 | 1943 md_temp = char_O_hash[md_i]; |
126 | 1944 else |
1945 { | |
1946 _asm | |
167 | 1947 TBLRDPOSTINC |
1948 movff TABLAT,md_temp | |
126 | 1949 _endasm |
1950 } // else | |
167 | 1951 |
1952 md_buffer[md_i] = md_temp; | |
186 | 1953 md_state[md_i+16] = md_temp; |
1954 md_state[md_i+32] = (unsigned char)(md_temp ^ md_state[md_i]); | |
126 | 1955 } // for md_i 16 |
1956 | |
1957 for (md_i=0;md_i<18;md_i++) | |
1958 { | |
1959 for (md_j=0;md_j<48;md_j++) | |
1960 { | |
167 | 1961 md_state[md_j] ^= md_pi_subst[md_t]; |
126 | 1962 md_t = md_state[md_j]; |
1963 } // for md_j 48 | |
1964 md_t = (unsigned char)(md_t+1); | |
1965 } // for md_i 18 | |
164 | 1966 md_t = char_O_hash[15]; |
126 | 1967 |
1968 for (md_i=0;md_i<16;md_i++) | |
1969 { | |
167 | 1970 char_O_hash[md_i] ^= md_pi_subst[(md_buffer[md_i] ^ md_t)]; |
164 | 1971 md_t = char_O_hash[md_i]; |
126 | 1972 } // for md_i 16 |
1973 } // for md_pointer | |
116 | 1974 } // void deco_hash(void) |
184 | 1975 #endif |
116 | 1976 |
167 | 1977 ////////////////////////////////////////////////////////////////////////////// |
1978 // deco_clear_CNS_fraction | |
1979 // | |
116 | 1980 // new in v.101 |
167 | 1981 // |
116 | 1982 void deco_clear_CNS_fraction(void) |
1983 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
1984 RESET_C_STACK |
126 | 1985 CNS_fraction = 0.0; |
1986 char_O_CNS_fraction = 0; | |
167 | 1987 } |
116 | 1988 |
167 | 1989 ////////////////////////////////////////////////////////////////////////////// |
1990 // deco_calc_CNS_fraction | |
1991 // | |
116 | 1992 // new in v.101 |
1993 // optimized in v.102 : with new variables char_I_actual_ppO2 and actual_ppO2 | |
167 | 1994 // |
116 | 1995 // Input: char_I_actual_ppO2 |
1996 // Output: char_O_CNS_fraction | |
1997 // Uses and Updates: CNS_fraction | |
1998 // Uses: acutal_ppO2 | |
167 | 1999 // |
116 | 2000 void deco_calc_CNS_fraction(void) |
2001 { | |
167 | 2002 overlay float actual_ppO2; |
126 | 2003 RESET_C_STACK |
167 | 2004 |
126 | 2005 actual_ppO2 = (float)char_I_actual_ppO2 / 100.0; |
116 | 2006 |
126 | 2007 if (char_I_actual_ppO2 < 50) |
2008 CNS_fraction = CNS_fraction;// no changes | |
2009 else if (char_I_actual_ppO2 < 60) | |
2010 CNS_fraction = 1/(-54000.0 * actual_ppO2 + 54000.0) + CNS_fraction; | |
2011 else if (char_I_actual_ppO2 < 70) | |
2012 CNS_fraction = 1/(-45000.0 * actual_ppO2 + 48600.0) + CNS_fraction; | |
2013 else if (char_I_actual_ppO2 < 80) | |
2014 CNS_fraction = 1/(-36000.0 * actual_ppO2 + 42300.0) + CNS_fraction; | |
2015 else if (char_I_actual_ppO2 < 90) | |
2016 CNS_fraction = 1/(-27000.0 * actual_ppO2 + 35100.0) + CNS_fraction; | |
2017 else if (char_I_actual_ppO2 < 110) | |
2018 CNS_fraction = 1/(-18000.0 * actual_ppO2 + 27000.0) + CNS_fraction; | |
2019 else if (char_I_actual_ppO2 < 150) | |
2020 CNS_fraction = 1/(-9000.0 * actual_ppO2 + 17100.0) + CNS_fraction; | |
2021 else if (char_I_actual_ppO2 < 160) | |
2022 CNS_fraction = 1/(-22500.0 * actual_ppO2 + 37350.0) + CNS_fraction; | |
2023 else if (char_I_actual_ppO2 < 165) | |
2024 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 | |
2025 else if (char_I_actual_ppO2 < 170) | |
2026 CNS_fraction = 0.00102 + CNS_fraction; // example calculation: Sqrt((1.7/1.55)^20)*0.000404 | |
2027 else if (char_I_actual_ppO2 < 175) | |
2028 CNS_fraction = 0.00136 + CNS_fraction; | |
2029 else if (char_I_actual_ppO2 < 180) | |
2030 CNS_fraction = 0.00180 + CNS_fraction; | |
2031 else if (char_I_actual_ppO2 < 185) | |
2032 CNS_fraction = 0.00237 + CNS_fraction; | |
2033 else if (char_I_actual_ppO2 < 190) | |
2034 CNS_fraction = 0.00310 + CNS_fraction; | |
2035 else if (char_I_actual_ppO2 < 195) | |
2036 CNS_fraction = 0.00401 + CNS_fraction; | |
2037 else if (char_I_actual_ppO2 < 200) | |
2038 CNS_fraction = 0.00517 + CNS_fraction; | |
2039 else if (char_I_actual_ppO2 < 230) | |
2040 CNS_fraction = 0.0209 + CNS_fraction; | |
2041 else | |
2042 CNS_fraction = 0.0482 + CNS_fraction; // value for 2.5 | |
116 | 2043 |
126 | 2044 if (CNS_fraction > 2.5) |
2045 CNS_fraction = 2.5; | |
2046 if (CNS_fraction < 0.0) | |
2047 CNS_fraction = 0.0; | |
2048 | |
2049 char_O_CNS_fraction = (char)((CNS_fraction + 0.005)* 100.0); | |
167 | 2050 } |
116 | 2051 |
167 | 2052 ////////////////////////////////////////////////////////////////////////////// |
2053 // deco_calc_CNS_decrease_15min | |
2054 // | |
116 | 2055 // new in v.101 |
167 | 2056 // |
116 | 2057 // calculates the half time of 90 minutes in 6 steps of 15 min |
200 | 2058 // (Used in sleepmode, for low battery mode). |
167 | 2059 // |
116 | 2060 // Output: char_O_CNS_fraction |
2061 // Uses and Updates: CNS_fraction | |
167 | 2062 // |
116 | 2063 void deco_calc_CNS_decrease_15min(void) |
2064 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
2065 RESET_C_STACK |
126 | 2066 CNS_fraction = 0.890899 * CNS_fraction; |
167 | 2067 char_O_CNS_fraction = (char)(CNS_fraction * 100.0 + 0.5); |
2068 } | |
116 | 2069 |
167 | 2070 ////////////////////////////////////////////////////////////////////////////// |
2071 // deco_calc_percentage | |
2072 // | |
116 | 2073 // new in v.101 |
167 | 2074 // |
116 | 2075 // calculates int_I_temp * char_I_temp / 100 |
2076 // output is int_I_temp | |
200 | 2077 // |
2078 // Used to compute NoFly remaining time. | |
116 | 2079 |
2080 void deco_calc_percentage(void) | |
2081 { | |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
2082 RESET_C_STACK |
203 | 2083 int_I_temp = (short)(int_I_temp * (char_I_temp / 100.0)); |
116 | 2084 } |
2085 | |
167 | 2086 ////////////////////////////////////////////////////////////////////////////// |
2087 | |
116 | 2088 void deco_push_tissues_to_vault(void) |
2089 { | |
167 | 2090 overlay unsigned char x; |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
2091 RESET_C_STACK |
167 | 2092 |
116 | 2093 cns_vault = CNS_fraction; |
167 | 2094 for (x=0;x<32;x++) |
2095 pres_tissue_vault[x] = pres_tissue[x]; | |
116 | 2096 } |
167 | 2097 |
116 | 2098 void deco_pull_tissues_from_vault(void) |
2099 { | |
167 | 2100 overlay unsigned char x; |
122
3003a8040b78
FIX again reset C data stack and frame pointer when entering C code.
JeanDo
parents:
116
diff
changeset
|
2101 RESET_C_STACK |
167 | 2102 |
116 | 2103 CNS_fraction = cns_vault; |
167 | 2104 for (x=0;x<32;x++) |
2105 pres_tissue[x] = pres_tissue_vault[x]; | |
116 | 2106 } |
2107 | |
167 | 2108 ////////////////////////////////////////////////////////////////////////////// |
2109 // | |
184 | 2110 #ifndef CROSS_COMPILE |
2111 void main() {} | |
2112 #endif |