2011-12-28 09:09:28 +00:00
|
|
|
# Fuel handling for the YASim FDM. Note that other FDMs (e.g. JSBSim)
|
|
|
|
# handle fuel within the FDM itself.
|
|
|
|
#
|
2004-03-27 04:07:56 +00:00
|
|
|
# Properties under /consumables/fuel/tank[n]:
|
2011-02-14 22:13:12 +00:00
|
|
|
# + level-lbs - Current fuel load. Can be set by user code.
|
2004-03-27 04:07:56 +00:00
|
|
|
# + selected - boolean indicating tank selection.
|
2007-01-31 15:53:01 +00:00
|
|
|
# + capacity-gal_us - Tank capacity
|
2004-03-27 04:07:56 +00:00
|
|
|
#
|
|
|
|
# Properties under /engines/engine[n]:
|
|
|
|
# + fuel-consumed-lbs - Output from the FDM, zeroed by this script
|
|
|
|
# + out-of-fuel - boolean, set by this code.
|
|
|
|
|
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
var UPDATE_PERIOD = 0.3;
|
|
|
|
|
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
var update = func {
|
|
|
|
if (fuel_freeze)
|
2007-01-31 15:53:01 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
var consumed_fuel = 0;
|
|
|
|
foreach (var e; engines) {
|
|
|
|
var fuel = e.getNode("fuel-consumed-lbs");
|
|
|
|
consumed_fuel += fuel.getValue();
|
|
|
|
fuel.setDoubleValue(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
var selected_tanks = [];
|
|
|
|
foreach (var t; tanks) {
|
2011-09-25 14:20:04 +00:00
|
|
|
var cap = t.getNode("capacity-gal_us",0);
|
2012-04-22 13:19:20 +00:00
|
|
|
var selected = t.getNode("selected");
|
|
|
|
if ((cap!=nil) and (cap.getValue() > 0.01) and (t.getNode("level-lbs") != nil) and
|
|
|
|
(selected != nil) and selected.getBoolValue())
|
2007-01-31 15:53:01 +00:00
|
|
|
append(selected_tanks, t);
|
|
|
|
}
|
|
|
|
|
2012-04-22 13:19:20 +00:00
|
|
|
# Subtract fuel from tanks, set auxiliary properties. Set out-of-fuel
|
2007-01-31 15:53:01 +00:00
|
|
|
# when any one tank is dry.
|
|
|
|
var out_of_fuel = 0;
|
|
|
|
if (size(selected_tanks) == 0) {
|
|
|
|
out_of_fuel = 1;
|
|
|
|
} else {
|
|
|
|
var fuel_per_tank = consumed_fuel / size(selected_tanks);
|
|
|
|
foreach (var t; selected_tanks) {
|
2012-04-22 13:19:20 +00:00
|
|
|
var lbs = t.getNode("level-lbs");
|
|
|
|
lbs.setDoubleValue(lbs.getValue() - fuel_per_tank);
|
|
|
|
var empty = t.getNode("empty");
|
|
|
|
if (empty == nil)
|
|
|
|
empty = (lbs.getValue() <= 0);
|
|
|
|
else
|
|
|
|
empty = empty.getBoolValue();
|
|
|
|
if( empty ) {
|
2007-01-31 15:53:01 +00:00
|
|
|
# Kill the engines if we're told to, otherwise simply
|
|
|
|
# deselect the tank.
|
2007-10-07 15:12:05 +00:00
|
|
|
if (t.getNode("kill-when-empty", 1).getBoolValue())
|
2007-01-31 15:53:01 +00:00
|
|
|
out_of_fuel = 1;
|
2007-10-07 15:12:05 +00:00
|
|
|
else
|
2007-01-31 15:53:01 +00:00
|
|
|
t.getNode("selected").setBoolValue(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
foreach (var e; engines)
|
2007-01-31 15:53:01 +00:00
|
|
|
e.getNode("out-of-fuel").setBoolValue(out_of_fuel);
|
2004-03-27 04:07:56 +00:00
|
|
|
}
|
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
var loop = func {
|
2007-01-31 15:53:01 +00:00
|
|
|
update();
|
|
|
|
settimer(loop, UPDATE_PERIOD);
|
2004-03-27 04:07:56 +00:00
|
|
|
}
|
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
var tanks = [];
|
|
|
|
var engines = [];
|
|
|
|
var fuel_freeze = nil;
|
|
|
|
|
2012-03-20 20:18:20 +00:00
|
|
|
var freeze_fuel_listener = nil;
|
|
|
|
var initialized = 0;
|
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
_setlistener("/sim/signals/fdm-initialized", func {
|
2012-03-20 20:18:20 +00:00
|
|
|
if (freeze_fuel_listener == nil)
|
|
|
|
{
|
|
|
|
freeze_fuel_listener = setlistener("/sim/freeze/fuel", func(n) { fuel_freeze = n.getBoolValue() }, 1);
|
|
|
|
}
|
2007-01-31 15:53:01 +00:00
|
|
|
|
2011-12-28 09:09:28 +00:00
|
|
|
# Fuel sub-system is only used by YASim. Other FDMs (e.g. JSBSim)
|
|
|
|
# handle fuel themselves.
|
|
|
|
if (getprop("/sim/flight-model") != "yasim") { return; }
|
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
engines = props.globals.getNode("engines", 1).getChildren("engine");
|
|
|
|
foreach (var e; engines) {
|
|
|
|
e.getNode("fuel-consumed-lbs", 1).setDoubleValue(0);
|
|
|
|
e.getNode("out-of-fuel", 1).setBoolValue(0);
|
|
|
|
}
|
|
|
|
|
2012-03-20 20:18:20 +00:00
|
|
|
# do the following stuff once only
|
|
|
|
if (initialized)
|
|
|
|
return;
|
|
|
|
initialized = 1;
|
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
foreach (var t; props.globals.getNode("/consumables/fuel", 1).getChildren("tank")) {
|
2008-08-10 17:53:41 +00:00
|
|
|
if (!t.getAttribute("children"))
|
2007-01-31 15:53:01 +00:00
|
|
|
continue; # skip native_fdm.cxx generated zombie tanks
|
2007-10-07 15:12:05 +00:00
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
append(tanks, t);
|
2008-11-20 19:45:40 +00:00
|
|
|
t.initNode("selected", 1, "BOOL");
|
2007-01-31 15:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loop();
|
|
|
|
});
|
|
|
|
|
|
|
|
|