Debian Codegraph @

Aircraft/Generic/Oxygen/oxygen.nas

raw
1 ###############################################################################
2 ##
3 ## Oxygen system module for FlightGear.
4 ##
5 ## Copyright (C) 2010 Vivian Meazza (vivia.meazza(at)lineone.net)
6 ## This file is licensed under the GPL license v2 or later.
7 ##
8 ###############################################################################
9
10
11 # Properties under /consumables/fuel/tank[n]:
12 # + level_cu_ft - Current free oxygen content. Must be set by user code.
13 # + capacity_cu_ft - Tank volume
14 # + selected - boolean indicating tank selection.
15 # + name ...........- string
16 # + pressure - OUTPUT ONLY property, do not try to set
17
18 # Properties under /controls/oxygen/
19 # + altitude-norm - the selected supply altitude normalized 0 - 100% oxygen
20 # + flowrate_cu_ft_ps - Max (100%) Oxygen flow rate
21
22 # + flowrate considerations:
23 # ref http://en.wikipedia.org/wiki/Human_lung
24 #
25 # when maximum (100%) oxygen is selected, we wish to deliver enough oxygen to fill
26 # the pilot's lungs, with slight overpressure.
27 #
28 # let the tidal flow volume - that is the amount of gas which flows
29 # into and out of the lungs on each breath = T ft^3;
30 # and the number of breaths per minute at rest= N min^-1;
31 # but we need to consider a pilot under stress factor = 1.5
32 #
33 # so flowrate (ft^3.sec^-1) = (T*1.5*N)/60
34 #
35 # substituting the values from the reference
36 #
37 # flowrate = 0.01765 * 1.5 * 20 / 60 = 0.008828
38 #
39 # rounding up to provide overpressure
40 #
41 # flowrate = 0.01 (ft^3.sec^-1)
42
43
44 #========================= Initialize ===============================
45 var MAXTANKS = 20;
46 var INHG2PSI = 0.491154077497;
47
48 var initialize = func {
49
50 print( "Initializing Oxygen System ..." );
51
52 props.globals.initNode("/systems/oxygen/serviceable", 1, "BOOL");
53 props.globals.initNode("/sim/freeze/oxygen", 0, "BOOL");
54 props.globals.initNode("/controls/oxygen/altitude-norm", 0.0, "DOUBLE");
55 props.globals.initNode("/controls/oxygen/flowrate-cu-ft-ps", 0.01, "DOUBLE");
56
57 for (var i = 0; i < MAXTANKS; i += 1){
58 props.globals.initNode("/consumables/oxygen/tank["~ i ~ "]/capacity-cu-ft", 0.01, "DOUBLE");
59 props.globals.initNode("/consumables/oxygen/tank["~ i ~ "]/level-cu-ft", 0, "DOUBLE");
60 props.globals.initNode("/consumables/oxygen/tank["~ i ~ "]/selected", 0, "BOOL");
61 props.globals.initNode("/consumables/oxygen/tank["~ i ~ "]/pressure-psi", 50, "DOUBLE");
62 }
63
64 oxygen();
65
66 } #end init
67
68 #========================= Oxygen System ============================
69 var oxygen = func {
70
71 var freeze = getprop("/sim/freeze/oxygen");
72 var serviceable =getprop("/systems/oxygen/serviceable");
73
74 if(freeze or !serviceable) { return; }
75
76 var dt = getprop("sim/time/delta-sec");
77 var oxygen_alt = getprop("controls/oxygen/altitude-norm");
78 var flowrate_cu_ft_ps = getprop("controls/oxygen/flowrate-cu-ft-ps");
79 var Pa = getprop("environment/pressure-inhg") * INHG2PSI;
80
81 var flow_cu_ft = flowrate_cu_ft_ps * oxygen_alt * dt;
82
83 var contents = 0;
84 var cap = 0;
85 var availableTanks = [];
86 var selected = 0;
87 var pressure = 2000;
88
89 # Build a list of available tanks. An available tank is both selected, has
90 # oxygen remaining.and pressure < ambient.
91 var AllTanks = props.globals.getNode("consumables/oxygen").getChildren("tank");
92
93 foreach( var t; AllTanks) {
94 cap = t.getNode("capacity-cu-ft", 1).getValue();
95 contents = t.getNode("level-cu-ft", 1).getValue();
96 selected = t.getNode("selected", 1).getBoolValue();
97 pressure = t.getNode("pressure-psi", 1).getValue();
98
99 if(cap != nil and cap > 0.01 ) {
100 # print ("Pressure ", pressure, " " , Pa);
101
102 if(selected and pressure > Pa) {
103 append(availableTanks, t);
104 }
105
106 }
107
108 }
109
110 # print("flow_cu_ft ", flow_cu_ft," " ,size(availableTanks));
111
112 # Subtract flow_cu_ft from tanks, set auxilliary properties. Set out-of-gas
113 # when all tanks are empty.
114 var outOfGas = 0;
115
116 if(size(availableTanks) == 0) {
117 outOfGas = 1;
118 } else {
119 flowPerTank = flow_cu_ft / size(availableTanks);
120 foreach( var t; availableTanks ) {
121 cu_ft = t.getNode("level-cu-ft").getValue();
122 cu_ft -= flowPerTank;
123 cap = t.getNode("capacity-cu-ft", 1).getValue();
124
125 if(cu_ft < 0) { cu_ft = 0;}
126
127 # print ("pressure ", calcPressure(cu_ft, cap));
128
129 t.getNode("level-cu-ft").setDoubleValue(cu_ft);
130 t.getNode("pressure-psi").setDoubleValue(calcPressure(cu_ft, cap));
131 }
132 }
133
134 settimer(oxygen, 0.3);
135
136 } #end oxygen
137
138 # We apply Boyle's Law to derive the pressure in the tank fom the capacity of the
139 # tank and the contents. We ignore the effects of temperature.
140
141 var calcPressure = func (cu_ft, cap){
142 var Vc = cap;
143 var Va = cu_ft;
144 var Pa = 14.7;
145
146 # print (Vc, " ", Va, " ", Pa);
147
148 Pc = (Pa * Va)/Vc;
149 return Pc;
150 } #end calcPressure
151
152 setlistener("sim/signals/fdm-initialized", initialize);
153
154 # end
155