Debian Codegraph @

Aircraft/Generic/Systems/failures.nas

raw
1 # Failure simulation library
2 #
3 # Collection of generic Triggers and FailureActuators for programming the
4 # FailureMgr Nasal module.
5 #
6 # Copyright (C) 2014 Anton Gomez Alvedro
7 # Based on previous work by Stuart Buchanan, Erobo & John Denker
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; either version 2 of the
12 # License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
23
24 #
25 # Functions for generating FailureActuators
26 # ------------------------------------------
27
28 ##
29 # Returns an actuator object that will set the serviceable property at
30 # the given node to zero when the level of failure is > 0.
31
32 var set_unserviceable = func(path) {
33
34 var prop = path ~ "/serviceable";
35
36 if (props.globals.getNode(prop) == nil)
37 props.globals.initNode(prop, 1, "BOOL");
38
39 return {
40 parents: [FailureMgr.FailureActuator],
41 set_failure_level: func(level) setprop(prop, level > 0 ? 0 : 1),
42 get_failure_level: func { getprop(prop) ? 0 : 1 }
43 }
44 }
45
46 ##
47 # Returns an actuator object that will make the given property read only.
48 # This prevents any other system from updating it, and effectively jamming
49 # whatever it is that is controlling.
50
51 var set_readonly = func(property) {
52 return {
53 parents: [FailureMgr.FailureActuator],
54
55 set_failure_level: func(level) {
56 var pnode = props.globals.getNode(property);
57 pnode.setAttribute("writable", level > 0 ? 0 : 1);
58 },
59
60 get_failure_level: func {
61 var pnode = props.globals.getNode(property);
62 pnode.getAttribute("writable") ? 0 : 1;
63 }
64 }
65 }
66
67 ##
68 # Returns an an actuator object the manipulates engine controls (magnetos &
69 # cutoff) to simulate an engine failure. Sets these properties to read only
70 # while the system is failed.
71
72 var fail_engine = func(engine) {
73 return {
74 parents: [FailureMgr.FailureActuator],
75 level: 0,
76 magnetos: props.globals.getNode("/controls/engines/" ~ engine ~ "/magnetos", 1),
77 cutoff: props.globals.getNode("/controls/engines/" ~ engine ~ "/cutoff", 1),
78
79 get_failure_level: func me.level,
80
81 set_failure_level: func(level) {
82 if (level) {
83 # Switch off the engine, and disable writing to it.
84 me.magnetos.setValue(0);
85 me.magnetos.setAttribute("writable", 0);
86 me.cutoff.setValue(1);
87 me.cutoff.setAttribute("writable", 0);
88 }
89 else {
90 # Enable the properties, but don't set the magnetos, as they may
91 # be off for a reason.
92 me.magnetos.setAttribute("writable", 1);
93 me.cutoff.setAttribute("writable", 1);
94 me.cutoff.setValue(0);
95 }
96 me.level = level;
97 }
98 }
99 }
100
101
102 #
103 # Triggers
104 # ---------
105
106 ##
107 # Returns a random number from a Normal distribution with given mean and
108 # standard deviation.
109
110 var norm_rand = func(mean, std) {
111 var r = -2 * math.ln(1 - rand());
112 var a = 2 * math.pi * (1 - rand());
113 return mean + (math.sqrt(r) * math.sin(a) * std);
114 };
115
116 ##
117 # Trigger object that will fire when aircraft altitude is between
118 # min and max, both specified in feet. One of min or max may be nil for
119 # expressing "altitude > x" or "altitude < x" conditions.
120
121 var AltitudeTrigger = {
122
123 parents: [FailureMgr.Trigger],
124 type: "altitude",
125 requires_polling: 1,
126
127 new: func(min, max) {
128 min != nil or max != nil or
129 die("AltitudeTrigger.new: either min or max must be specified");
130
131 var m = FailureMgr.Trigger.new();
132 m.parents = [AltitudeTrigger];
133 m.params["min-altitude-ft"] = min;
134 m.params["max-altitude-ft"] = max;
135 m._altitude_prop = "/position/altitude-ft";
136 return m;
137 },
138
139 to_str: func {
140 var min = me.params["min-altitude-ft"];
141 var max = me.params["max-altitude-ft"];
142
143 if (min == nil) sprintf("Altitude below %d ft", int(max));
144 elsif (max == nil) sprintf("Altitude above %d ft", int(min));
145 else sprintf("Altitude between %d and %d ft", int(min), int(max));
146 },
147
148 update: func {
149 var alt = getprop(me._altitude_prop);
150
151 var min = me.params["min-altitude-ft"];
152 var max = me.params["max-altitude-ft"];
153
154 me.fired = min != nil ? min < alt : 1;
155 me.fired = max != nil ? me.fired and alt < max : me.fired;
156 }
157 };
158
159 ##
160 # Trigger object that fires when the aircraft's position is within a certain
161 # distance of a given waypoint.
162
163 var WaypointTrigger = {
164
165 parents: [FailureMgr.Trigger],
166 type: "waypoint",
167 requires_polling: 1,
168
169 new: func(lat, lon, distance) {
170 var wp = geo.Coord.new();
171 wp.set_latlon(lat, lon);
172
173 var m = FailureMgr.Trigger.new();
174 m.parents = [WaypointTrigger];
175 m.params["latitude-deg"] = lat;
176 m.params["longitude-deg"] = lon;
177 m.params["distance-nm"] = distance;
178 m.waypoint = wp;
179 return m;
180 },
181
182 arm: func {
183 call(FailureMgr.Trigger.arm, [], me);
184 me.waypoint.set_latlon(me.params["latitude-deg"],
185 me.params["longitude-deg"]);
186 },
187
188 to_str: func {
189 sprintf("Within %.2f miles of %s", me.params["distance-nm"],
190 geo.format(me.waypoint.lat(), me.waypoint.lon()));
191 },
192
193 update: func {
194 var d = geo.aircraft_position().distance_to(me.waypoint) * M2NM;
195 me.fired = d < me.params["distance-nm"];
196 }
197 };
198
199 ##
200 # Trigger object that will fire on average after the specified time.
201
202 var MtbfTrigger = {
203
204 parents: [FailureMgr.Trigger],
205 type: "mtbf",
206 requires_polling: 0,
207
208 new: func(mtbf) {
209 var m = FailureMgr.Trigger.new();
210 m.parents = [MtbfTrigger];
211 m.params["mtbf"] = mtbf;
212 m.timer = maketimer(0, func m.on_fire());
213 m.timer.singleShot = 1;
214 return m;
215 },
216
217 enable: func {
218 me.armed and me.timer.start();
219 me.enabled = 1;
220 },
221
222 disable: func {
223 me.timer.stop();
224 me.enabled = 0;
225 },
226
227 arm: func {
228 call(FailureMgr.Trigger.arm, [], me);
229 me.timer.restart(norm_rand(me.params["mtbf"], me.params["mtbf"] / 10));
230 me.enabled and me.timer.start();
231 },
232
233 disarm: func {
234 call(FailureMgr.Trigger.disarm, [], me);
235 me.timer.stop();
236 },
237
238 to_str: func {
239 sprintf("Mean time between failures: %.1f mins", me.params["mtbf"] / 60);
240 },
241
242 update: func {
243 me.fired = getprop(me._time_prop) > me.fire_time;
244 }
245 };
246
247 ##
248 # Trigger object that will fire exactly after the given timeout.
249
250 var TimeoutTrigger = {
251
252 parents: [FailureMgr.Trigger],
253 type: "timeout",
254 requires_polling: 0,
255
256 new: func(timeout) {
257 var m = FailureMgr.Trigger.new();
258 m.parents = [TimeoutTrigger];
259 m.params["timeout-sec"] = timeout;
260 m.timer = maketimer(0, func m.on_fire());
261 m.timer.singleShot = 1;
262 return m;
263 },
264
265 enable: func {
266 me.armed and me.timer.start();
267 me.enabled = 1;
268 },
269
270 disable: func {
271 me.timer.stop();
272 me.enabled = 0;
273 },
274
275 arm: func {
276 call(FailureMgr.Trigger.arm, [], me);
277 me.timer.restart(me.params["timeout-sec"]);
278 me.enabled and me.timer.start();
279 },
280
281 disarm: func {
282 call(FailureMgr.Trigger.disarm, [], me);
283 me.timer.stop();
284 },
285
286 to_str: func {
287 sprintf("Fixed delay: %d minutes", me.params["timeout-sec"] / 60);
288 },
289
290 update: func {
291 me.fired = getprop("/sim/time/elapsed-sec") > me.fire_time;
292 }
293 };
294
295 ##
296 # Simple approach to count usage cycles for a given property. Every time
297 # the propery variation changes in direction, we count half a cycle.
298 # If the property represents aileron angular position, for example, this
299 # would count roughly the number of times the aileron has been actuated.
300
301 var CycleCounter = {
302
303 new: func(property, on_update = nil) {
304 return {
305 parents: [CycleCounter],
306 cycles: 0,
307 _property: property,
308 _on_update: on_update,
309 _prev_value: getprop(property),
310 _prev_delta: 0,
311 _lsnr: nil
312 };
313 },
314
315 enable: func {
316 if (me._lsnr == nil)
317 me._lsnr = setlistener(me._property, func (p) me._on_prop_change(p), 0, 0);
318 },
319
320 disable: func {
321 if (me._lsnr != nil) {
322 removelistener(me._lsnr);
323 me._lsnr = nil;
324 }
325 },
326
327 reset: func {
328 me.cycles = 0;
329 me._prev_value = getprop(me._property);
330 me._prev_delta = 0;
331 },
332
333 _on_prop_change: func(prop) {
334
335 # TODO: Implement a filter for avoiding spureous values.
336
337 var value = prop.getValue();
338 var delta = value - me._prev_value;
339 if (delta == 0) return;
340
341 if (delta * me._prev_delta < 0) {
342 # Property variation has changed direction
343 me.cycles += 0.5;
344 if (me._on_update != nil) me._on_update(me.cycles);
345 }
346
347 me._prev_delta = delta;
348 me._prev_value = value;
349 }
350 };
351
352 ##
353 # Trigger object that will fire on average after a property has gone through
354 # mcbf (mean cycles between failures) cycles.
355
356 var McbfTrigger = {
357
358 parents: [FailureMgr.Trigger],
359 type: "mcbf",
360 requires_polling: 0,
361
362 new: func(property, mcbf) {
363 var m = FailureMgr.Trigger.new();
364 m.parents = [McbfTrigger];
365 m.params["mcbf"] = mcbf;
366 m.counter = CycleCounter.new(property, func(c) call(m._on_cycle, [c], m));
367 m.activation_cycles = 0;
368 return m;
369 },
370
371 enable: func {
372 me.armed and me.counter.enable();
373 me.enabled = 1;
374 },
375
376 disable: func {
377 me.counter.disable();
378 me.enabled = 0;
379 },
380
381 arm: func {
382 call(FailureMgr.Trigger.arm, [], me);
383 me.counter.reset();
384 me.activation_cycles =
385 norm_rand(me.params["mcbf"], me.params["mcbf"] / 10);
386
387 me.enabled and me.counter.enable();
388 },
389
390 disarm: func {
391 call(FailureMgr.Trigger.disarm, [], me);
392 me.counter.disable();
393 },
394
395 to_str: func {
396 sprintf("Mean cycles between failures: %.2f", me.params["mcbf"]);
397 },
398
399 _on_cycle: func(cycles) {
400 if (!me.fired and cycles > me.activation_cycles) {
401 me.fired = 1;
402 me.on_fire();
403 }
404 }
405 };