2014-05-13 18:31:35 +00:00
|
|
|
# Failure Manager implementation
|
|
|
|
#
|
|
|
|
# Monitors trigger conditions periodically and fires failure modes when those
|
|
|
|
# conditions are met. It also provides a central access point for publishing
|
|
|
|
# failure modes to the user interface and the property tree.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 Anton Gomez Alvedro
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation; either version 2 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
|
2014-05-13 18:31:35 +00:00
|
|
|
##
|
|
|
|
# Represents one way things can go wrong, for example "a blown tire".
|
|
|
|
|
|
|
|
var FailureMode = {
|
|
|
|
|
|
|
|
##
|
|
|
|
# id: Unique identifier for this failure mode.
|
|
|
|
# eg: "engine/carburetor-ice"
|
|
|
|
#
|
|
|
|
# description: Short text description, suitable for printing to the user.
|
|
|
|
# eg: "Ice in the carburetor"
|
|
|
|
#
|
|
|
|
# actuator: Object implementing the FailureActuator interface.
|
|
|
|
# Used by the failure manager to apply a certain level of
|
|
|
|
# failure to the failure mode.
|
|
|
|
|
|
|
|
new: func(id, description, actuator) {
|
|
|
|
return {
|
|
|
|
parents: [FailureMode],
|
|
|
|
id: id,
|
|
|
|
description: description,
|
|
|
|
actuator: actuator,
|
|
|
|
_path: nil
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
##
|
|
|
|
# Applies a certain level of failure to this failure mode.
|
|
|
|
# level: Floating point number in the range [0, 1] zero being no failure
|
|
|
|
# and 1 total failure.
|
|
|
|
|
|
|
|
set_failure_level: func(level) {
|
2014-12-21 11:39:52 +00:00
|
|
|
assert(me._path != nil, "FailureMode.set_failure_level: unbound mode");
|
2014-05-13 18:31:35 +00:00
|
|
|
setprop(me._path ~ me.id ~ "/failure-level", level);
|
|
|
|
},
|
|
|
|
|
|
|
|
##
|
|
|
|
# Internal version that actually does the job.
|
|
|
|
|
|
|
|
_set_failure_level: func(level) {
|
|
|
|
me.actuator.set_failure_level(level);
|
2014-12-21 11:39:52 +00:00
|
|
|
_failmgr.log(sprintf("%s condition %d%%", me.description, (1-level)*100));
|
2014-05-13 18:31:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the level of failure currently being simulated.
|
|
|
|
|
|
|
|
get_failure_level: func me.actuator.get_failure_level(),
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates an interface for this failure mode in the property tree at the
|
|
|
|
# given location. Currently the interface is just:
|
|
|
|
#
|
|
|
|
# path/failure-level (double, rw)
|
|
|
|
|
|
|
|
bind: func(path) {
|
2014-12-21 11:39:52 +00:00
|
|
|
assert(me._path == nil, "FailureMode.bind: mode already bound");
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
var prop = path ~ me.id ~ "/failure-level";
|
|
|
|
props.globals.initNode(prop, me.actuator.get_failure_level(), "DOUBLE");
|
|
|
|
setlistener(prop, func (p) me._set_failure_level(p.getValue()), 0, 0);
|
|
|
|
me._path = path;
|
|
|
|
},
|
|
|
|
|
|
|
|
##
|
|
|
|
# Remove bound properties from the property tree.
|
|
|
|
|
|
|
|
unbind: func {
|
|
|
|
me._path != nil and props.globals.getNode(me._path ~ me.id).remove();
|
|
|
|
me._path = nil;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
##
|
|
|
|
# Implements the FailureMgr functionality.
|
|
|
|
#
|
|
|
|
# It is wrapped into an object to leave the door open to several evolution
|
2014-12-21 11:39:52 +00:00
|
|
|
# approaches, for example moving the implementation down to the C++ engine.
|
|
|
|
# Additionally, it also serves to isolate implementation details into its own
|
|
|
|
# namespace.
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
var _failmgr = {
|
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
pollable_trigger_count: 0,
|
|
|
|
enable_after_teleport: 0,
|
|
|
|
|
2014-05-13 18:31:35 +00:00
|
|
|
timer: nil,
|
|
|
|
update_period: 10, # 0.1 Hz
|
2014-12-21 11:39:52 +00:00
|
|
|
|
2014-05-13 18:31:35 +00:00
|
|
|
failure_modes: {},
|
2014-12-21 11:39:52 +00:00
|
|
|
logbuf: events.LogBuffer.new(echo: 1),
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
init: func {
|
|
|
|
me.timer = maketimer(me.update_period, func me._update());
|
2014-12-21 11:39:52 +00:00
|
|
|
setlistener("sim/signals/reinit", func(n) me._on_teleport(n));
|
|
|
|
setlistener("sim/signals/fdm-initialized", func(n) me._on_teleport(n));
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
props.globals.initNode(proproot ~ "display-on-screen", 1, "BOOL");
|
|
|
|
props.globals.initNode(proproot ~ "enabled", 1, "BOOL");
|
|
|
|
setlistener(proproot ~ "enabled",
|
|
|
|
func (n) { n.getValue() ? me._enable() : me._disable() });
|
|
|
|
},
|
|
|
|
|
|
|
|
add_failure_mode: func(mode) {
|
|
|
|
contains(me.failure_modes, mode.id) and
|
|
|
|
die("add_failure_mode: failure mode already exists: " ~ id);
|
|
|
|
|
|
|
|
me.failure_modes[mode.id] = { mode: mode, trigger: nil };
|
|
|
|
mode.bind(proproot);
|
|
|
|
},
|
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
get_failure_modes: func {
|
|
|
|
var modes = [];
|
|
|
|
|
|
|
|
foreach (var k; keys(me.failure_modes)) {
|
|
|
|
var m = me.failure_modes[k];
|
|
|
|
append(modes, {
|
|
|
|
id: k,
|
|
|
|
description: m.mode.description });
|
|
|
|
}
|
|
|
|
|
|
|
|
return modes;
|
|
|
|
},
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
remove_failure_mode: func(id) {
|
|
|
|
contains(me.failure_modes, id) or
|
2014-12-21 11:39:52 +00:00
|
|
|
die("remove_failure_mode: failure mode does not exist: " ~ id);
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
var trigger = me.failure_modes[id].trigger;
|
|
|
|
if (trigger != nil)
|
|
|
|
me._discard_trigger(trigger);
|
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
me.failure_modes[id].mode.unbind();
|
2014-05-13 18:31:35 +00:00
|
|
|
delete(me.failure_modes, id);
|
|
|
|
},
|
|
|
|
|
|
|
|
remove_all: func {
|
|
|
|
foreach(var id; keys(me.failure_modes))
|
|
|
|
me.remove_failure_mode(id);
|
|
|
|
},
|
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
repair_all: func {
|
|
|
|
foreach(var id; keys(me.failure_modes))
|
|
|
|
me.failure_modes[id].mode.set_failure_level(0);
|
|
|
|
},
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
set_trigger: func(mode_id, trigger) {
|
|
|
|
contains(me.failure_modes, mode_id) or
|
|
|
|
die("set_trigger: failure mode does not exist: " ~ mode_id);
|
|
|
|
|
|
|
|
var mode = me.failure_modes[mode_id];
|
|
|
|
|
|
|
|
if (mode.trigger != nil)
|
|
|
|
me._discard_trigger(mode.trigger);
|
|
|
|
|
|
|
|
mode.trigger = trigger;
|
|
|
|
if (trigger == nil) return;
|
|
|
|
|
|
|
|
trigger.bind(proproot ~ mode_id);
|
2014-06-01 16:08:30 +00:00
|
|
|
trigger.on_fire = func _failmgr.on_trigger_activated(trigger);
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
if (trigger.requires_polling) {
|
|
|
|
me.pollable_trigger_count += 1;
|
|
|
|
|
|
|
|
if (me.enabled() and !me.timer.isRunning)
|
|
|
|
me.timer.start();
|
|
|
|
}
|
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
if (me.enabled())
|
|
|
|
trigger.enable();
|
2014-05-13 18:31:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get_trigger: func(mode_id) {
|
|
|
|
contains(me.failure_modes, mode_id) or
|
|
|
|
die("get_trigger: failure mode does not exist: " ~ mode_id);
|
|
|
|
|
|
|
|
return me.failure_modes[mode_id].trigger;
|
|
|
|
},
|
|
|
|
|
|
|
|
##
|
|
|
|
# Observer interface. Called from asynchronous triggers when they fire.
|
2014-12-21 11:39:52 +00:00
|
|
|
# trigger: Reference to the firing trigger.
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
on_trigger_activated: func(trigger) {
|
2014-12-21 11:39:52 +00:00
|
|
|
assert(me.enabled(), "A " ~ trigger.type ~ " trigger fired while the FailureMgr was disabled");
|
2014-05-13 18:31:35 +00:00
|
|
|
var found = 0;
|
|
|
|
|
|
|
|
foreach (var id; keys(me.failure_modes)) {
|
|
|
|
if (me.failure_modes[id].trigger == trigger) {
|
|
|
|
found = 1;
|
2014-12-21 11:39:52 +00:00
|
|
|
me.failure_modes[id].mode.set_failure_level(1);
|
|
|
|
trigger.disarm();
|
|
|
|
FailureMgr.events["trigger-fired"].notify(
|
|
|
|
{ mode_id: id, trigger: trigger });
|
2014-05-13 18:31:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
assert(found, "FailureMgr.on_trigger_activated: trigger not found");
|
2014-05-13 18:31:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
##
|
2014-12-21 11:39:52 +00:00
|
|
|
# Enable the failure manager. Starts the trigger poll timer and enables
|
|
|
|
# all triggers.
|
|
|
|
#
|
|
|
|
# Called from /sim/failure-manager/enabled and during a teleport if the
|
|
|
|
# FM was enabled when the teleport was initiated.
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
_enable: func {
|
|
|
|
foreach(var id; keys(me.failure_modes)) {
|
|
|
|
var trigger = me.failure_modes[id].trigger;
|
|
|
|
trigger != nil and trigger.enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (me.pollable_trigger_count > 0)
|
|
|
|
me.timer.start();
|
|
|
|
},
|
|
|
|
|
|
|
|
##
|
|
|
|
# Suspends failure manager activity. Pollable triggers will not be updated
|
|
|
|
# and all triggers will be disabled.
|
2014-12-21 11:39:52 +00:00
|
|
|
# Called from /sim/failure-manager/enabled and during a teleport.
|
2014-05-13 18:31:35 +00:00
|
|
|
|
|
|
|
_disable: func {
|
|
|
|
me.timer.stop();
|
|
|
|
|
|
|
|
foreach(var id; keys(me.failure_modes)) {
|
|
|
|
var trigger = me.failure_modes[id].trigger;
|
|
|
|
trigger != nil and trigger.disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
enabled: func {
|
|
|
|
getprop(proproot ~ "enabled");
|
|
|
|
},
|
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
log: func(message) {
|
|
|
|
me.logbuf.push(message);
|
|
|
|
if (getprop(proproot ~ "/display-on-screen"))
|
|
|
|
screen.log.write(message, 1.0, 0.0, 0.0);
|
|
|
|
},
|
|
|
|
|
2014-05-13 18:31:35 +00:00
|
|
|
##
|
|
|
|
# Poll loop. Updates pollable triggers and applies a failure level
|
|
|
|
# when they fire.
|
|
|
|
|
|
|
|
_update: func {
|
|
|
|
foreach (var id; keys(me.failure_modes)) {
|
|
|
|
var failure = me.failure_modes[id];
|
2014-12-21 11:39:52 +00:00
|
|
|
var trigger = failure.trigger;
|
2014-05-13 18:31:35 +00:00
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
if (trigger == nil or !trigger.requires_polling or !trigger.armed)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var level = trigger.update();
|
|
|
|
if (level == 0) continue;
|
|
|
|
|
|
|
|
if (level != failure.mode.get_failure_level())
|
|
|
|
failure.mode.set_failure_level(level);
|
|
|
|
trigger.disarm();
|
|
|
|
|
|
|
|
FailureMgr.events["trigger-fired"].notify(
|
|
|
|
{ mode_id: id, trigger: trigger });
|
2014-05-13 18:31:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_discard_trigger: func(trigger) {
|
|
|
|
trigger.disable();
|
|
|
|
trigger.unbind();
|
|
|
|
|
|
|
|
if (trigger.requires_polling) {
|
|
|
|
me.pollable_trigger_count -= 1;
|
|
|
|
me.pollable_trigger_count == 0 and me.timer.stop();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
##
|
2014-12-21 11:39:52 +00:00
|
|
|
# Teleport listener. During repositioning, all triggers are disabled to
|
|
|
|
# avoid them firing in a possibly inconsistent state.
|
2014-05-13 18:31:35 +00:00
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
_on_teleport: func(pnode) {
|
|
|
|
|
|
|
|
if (pnode.getName() == "fdm-initialized") {
|
|
|
|
if (me.enable_after_teleport) {
|
|
|
|
me._enable();
|
|
|
|
me.enable_after_teleport = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# then, it's /sim/signals/reinit
|
|
|
|
# only react when the signal raises to true.
|
|
|
|
if (pnode.getValue() == 1) {
|
|
|
|
me.enable_after_teleport = me.enabled();
|
|
|
|
me._disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
dump_status: func(mode_ids=nil) {
|
2014-05-13 18:31:35 +00:00
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
if (mode_ids == nil)
|
|
|
|
mode_ids = keys(me.failure_modes);
|
2014-05-13 18:31:35 +00:00
|
|
|
|
2014-12-21 11:39:52 +00:00
|
|
|
print("\nFailureMgr Status\n----------------------------------------");
|
|
|
|
|
|
|
|
foreach(var id; mode_ids) {
|
|
|
|
var mode = me.failure_modes[id].mode;
|
|
|
|
var trigger = me.failure_modes[id].trigger;
|
|
|
|
|
|
|
|
print(id, ": failure level ", mode.get_failure_level());
|
|
|
|
|
|
|
|
if (trigger == nil) {
|
|
|
|
print(" no trigger");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print(" ", trigger.type, " trigger (",
|
|
|
|
trigger.enabled? "enabled, " : "disabled, ",
|
|
|
|
trigger.armed? "armed)" : "disarmed)");
|
2014-05-13 18:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
##
|
|
|
|
# Module initialization
|
|
|
|
|
|
|
|
var _init = func {
|
|
|
|
removelistener(lsnr);
|
|
|
|
_failmgr.init();
|
|
|
|
|
|
|
|
# Load legacy failure modes for backwards compatibility
|
|
|
|
io.load_nasal(getprop("/sim/fg-root") ~
|
|
|
|
"/Aircraft/Generic/Systems/compat_failure_modes.nas");
|
|
|
|
}
|
|
|
|
|
|
|
|
var lsnr = setlistener("/nasal/FailureMgr/loaded", _init);
|