Debian Codegraph @

Aircraft/Generic/Engines/generic-yasim-engine.nas

raw
1 # generic-yasim-engine.nas -- a generic Nasal-based engine control system for YASim
2 # Version 1.0.0
3 #
4 # Copyright (C) 2011 Ryan Miller
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of the
9 # License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #
20
21 var UPDATE_PERIOD = 0; # update interval for engine init() functions
22
23 # jet engine class
24 var Jet =
25 {
26 # creates a new engine object
27 new: func(n, running = 0, idle_throttle = 0.01, max_start_n1 = 5.21, start_threshold = 3, spool_time = 4, start_time = 2, shutdown_time = 4)
28 {
29 # copy the Jet object
30 var m = { parents: [Jet] };
31 # declare object variables
32 m.number = n;
33 m.autostart_status = 0;
34 m.autostart_id = -1;
35 m.loop_running = 0;
36 m.started = 0;
37 m.starting = 0;
38 m.idle_throttle = idle_throttle;
39 m.max_start_n1 = max_start_n1;
40 m.start_threshold = start_threshold;
41 m.spool_time = spool_time;
42 m.start_time = start_time;
43 m.shutdown_time = shutdown_time;
44 # create references to properties and set default values
45 m.cutoff = props.globals.getNode("controls/engines/engine[" ~ n ~ "]/cutoff", 1);
46 m.cutoff.setBoolValue(!running);
47 m.n1 = props.globals.getNode("engines/engine[" ~ n ~ "]/n1", 1);
48 m.n1.setDoubleValue(0);
49 m.out_of_fuel = props.globals.getNode("engines/engine[" ~ n ~ "]/out-of-fuel", 1);
50 m.out_of_fuel.setBoolValue(0);
51 m.reverser = props.globals.getNode("controls/engines/engine[" ~ n ~ "]/reverser", 1);
52 m.reverser.setBoolValue(0);
53 m.rpm = props.globals.getNode("engines/engine[" ~ n ~ "]/rpm", 1);
54 m.rpm.setDoubleValue(running ? 100 : 0);
55 m.running = props.globals.getNode("engines/engine[" ~ n ~ "]/running", 1);
56 m.running.setBoolValue(running);
57 m.serviceable = props.globals.getNode("engines/engine[" ~ n ~ "]/serviceable", 1);
58 m.serviceable.setBoolValue(1);
59 m.starter = props.globals.getNode("controls/engines/engine[" ~ n ~ "]/starter", 1);
60 m.starter.setBoolValue(0);
61 m.throttle = props.globals.getNode("controls/engines/engine[" ~ n ~ "]/throttle", 1);
62 m.throttle.setDoubleValue(0);
63 m.throttle_lever = props.globals.getNode("controls/engines/engine[" ~ n ~ "]/throttle-lever", 1);
64 m.throttle_lever.setDoubleValue(0);
65 # return our new object
66 return m;
67 },
68 # engine-specific autostart
69 autostart: func
70 {
71 if (me.autostart_status)
72 {
73 me.autostart_status = 0;
74 me.cutoff.setBoolValue(1);
75 }
76 else
77 {
78 me.autostart_status = 1;
79 me.starter.setBoolValue(1);
80 settimer(func
81 {
82 me.cutoff.setBoolValue(0);
83 }, me.max_start_n1 / me.start_time);
84 }
85 },
86 # creates an engine update loop (optional)
87 init: func
88 {
89 if (me.loop_running) return;
90 me.loop_running = 1;
91 var loop = func
92 {
93 me.update();
94 settimer(loop, UPDATE_PERIOD);
95 };
96 settimer(loop, 0);
97 },
98 # updates the engine
99 update: func
100 {
101 if (me.running.getBoolValue() and !me.started)
102 {
103 me.running.setBoolValue(0);
104 }
105 if (me.cutoff.getBoolValue() or !me.serviceable.getBoolValue() or me.out_of_fuel.getBoolValue())
106 {
107 var rpm = me.rpm.getValue();
108 var time_delta = getprop("sim/time/delta-realtime-sec");
109 if (me.starter.getBoolValue())
110 {
111 rpm += time_delta * me.spool_time;
112 me.rpm.setValue(rpm >= me.max_start_n1 ? me.max_start_n1 : rpm);
113 }
114 else
115 {
116 rpm -= time_delta * me.shutdown_time;
117 me.rpm.setValue(rpm <= 0 ? 0 : rpm);
118 me.running.setBoolValue(0);
119 me.throttle_lever.setDoubleValue(0);
120 me.started = 0;
121 }
122 }
123 elsif (me.starter.getBoolValue())
124 {
125 var rpm = me.rpm.getValue();
126 if (rpm >= me.start_threshold)
127 {
128 var time_delta = getprop("sim/time/delta-realtime-sec");
129 rpm += time_delta * me.spool_time;
130 me.rpm.setValue(rpm);
131 if (rpm >= me.n1.getValue())
132 {
133 me.running.setBoolValue(1);
134 me.starter.setBoolValue(0);
135 me.started = 1;
136 }
137 else
138 {
139 me.running.setBoolValue(0);
140 }
141 }
142 }
143 elsif (me.running.getBoolValue())
144 {
145 me.throttle_lever.setValue(me.idle_throttle + (1 - me.idle_throttle) * me.throttle.getValue());
146 me.rpm.setValue(me.n1.getValue());
147 }
148 }
149 };
150
151 # turboprop engine class
152 var turboprop_condition_cutoff = 0.001; # minimum condition value for YASim turboprops to start
153 var Turboprop =
154 {
155 new: func(n, running = 0, min_condition = 0.2)
156 {
157 # copy the Turboprop object
158 var m = { parents: [Turboprop] };
159 # declare object variables
160 m.number = n;
161 m.autostart_status = 0;
162 m.loop_running = 0;
163 m.min_condition = min_condition;
164 # create references to properties and set default values
165 m.condition = props.globals.getNode("controls/engines/engine[" ~ n ~ "]/condition", 1);
166 m.condition.setDoubleValue(0);
167 m.condition_lever = props.globals.getNode("controls/engines/engine[" ~ n ~ "]/condition-lever", 1);
168 m.condition_lever.setDoubleValue(running ? min_condition : 0);
169 m.cutoff = props.globals.getNode("controls/engines/engine[" ~ n ~ "]/cutoff", 1);
170 m.cutoff.setBoolValue(!running);
171 m.n1 = props.globals.getNode("engines/engine[" ~ n ~ "]/n1", 1);
172 m.n1.setDoubleValue(running ? 100 : 0);
173 m.n2 = props.globals.getNode("engines/engine[" ~ n ~ "]/n2", 1);
174 m.n2.setDoubleValue(0);
175 m.out_of_fuel = props.globals.getNode("engines/engine[" ~ n ~ "]/out-of-fuel", 1);
176 m.out_of_fuel.setBoolValue(0);
177 m.propeller_feather = props.globals.getNode("controls/engines/engine[" ~ n ~ "]/propeller-feather", 1);
178 m.propeller_feather.setBoolValue(0);
179 m.starter = props.globals.getNode("controls/engines/engine[" ~ n ~ "]/starter", 1);
180 m.starter.setBoolValue(0);
181 # return our new object
182 return m;
183 },
184 # engine-specific autostart
185 autostart: func
186 {
187 if (me.autostart_status)
188 {
189 me.autostart_status = 0;
190 me.cutoff.setBoolValue(1);
191 me.condition.setValue(0);
192 }
193 else
194 {
195 me.autostart_status = 1;
196 me.cutoff.setBoolValue(0);
197 me.starter.setBoolValue(1);
198 me.condition.setValue(me.min_condition);
199 }
200 },
201 # creates an engine update loop (optional)
202 init: func
203 {
204 if (me.loop_running) return;
205 me.loop_running = 1;
206 var loop = func
207 {
208 me.update();
209 settimer(loop, UPDATE_PERIOD);
210 };
211 settimer(loop, 0);
212 },
213 # updates the engine
214 update: func
215 {
216 if (me.cutoff.getBoolValue())
217 {
218 me.out_of_fuel.setBoolValue(1);
219 }
220 if (me.starter.getBoolValue() and me.condition_lever.getValue() < turboprop_condition_cutoff and me.condition.getValue() >= me.min_condition)
221 {
222 me.condition_lever.setValue(me._get_condition_value(me.condition.getValue()));
223 }
224 elsif (me.condition_lever.getValue() < turboprop_condition_cutoff and me.n2.getValue() < 0.5)
225 {
226 if (me.propeller_feather.getBoolValue())
227 {
228 me.n1.setValue(0);
229 }
230 me.condition_lever.setValue(0);
231 }
232 if (me.n2.getValue() >= 0.5)
233 {
234 if (me.condition_lever.getValue() >= turboprop_condition_cutoff)
235 {
236 me.condition_lever.setValue(me._get_condition_value(me.condition.getValue()));
237 }
238 else
239 {
240 me.condition_lever.setValue(0);
241 }
242 me.n1.setValue(me.n2.getValue());
243 }
244 },
245 _get_condition_value: func(v)
246 {
247 if (v >= me.min_condition)
248 {
249 return turboprop_condition_cutoff + (v - me.min_condition) / (1 - me.min_condition) * (1 - turboprop_condition_cutoff);
250 }
251 return v / me.min_condition * turboprop_condition_cutoff;
252 }
253 };