Debian Codegraph @

Aircraft/c172p/Nasal/Interfaces/SelectableFuelPublisher.nas

raw
1 # Copyright 2018 Stuart Buchanan
2 # This file is part of FlightGear.
3 #
4 # FlightGear is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # FlightGear is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with FlightGear. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Fuel Publisher, providing information about fuel tank contents
18 var SelectableFuelPublisher =
19 {
20
21 new : func (period=1.0) {
22 var obj = {
23 parents : [
24 SelectableFuelPublisher,
25 PeriodicPropertyPublisher.new(notifications.PFDEventNotification.FuelData, period)
26 ],
27 };
28
29 obj.deltaT = period;
30
31
32 # Hack to handle most aircraft not having proper engine hours
33 if (getprop("/engines/engine[0]/hours") == nil) setprop("/engines/engine[0]/hours", 157.0);
34
35 # Assume pilot has correct fuel quantities entered at Start of Day
36 var tanks = props.getNode("/consumables/fuel",1).getChildren("tank");
37
38 foreach(var tank; tanks) {
39 var actual = tank.getNode("level-gal_us", 1).getValue();
40 var indicatedNode = tank.getNode("fg1000-indicated-level-gal_us", 1);
41 if (indicatedNode.getValue() == nil) indicatedNode.setValue(actual);
42 }
43
44 return obj;
45 },
46
47 # Custom publish method as we package the values into an array of fuel tanks,
48 # assuming that fuel is drawn evenly from both tanks.
49 publish : func() {
50 var tank_data = [];
51 var tanks = props.getNode("/consumables/fuel",1).getChildren("tank");
52
53 foreach(var tank; tanks) {
54 var indicatedNode = tank.getNode("fg1000-indicated-level-gal_us", 1);
55 var fuel = indicatedNode.getValue();
56 if (fuel == nil) fuel = 0;
57 var fuel_flow = getprop("/engines/active-engine/fuel-flow-gph");
58 if (fuel_flow == nil) fuel_flow = 0;
59 fuel = fuel - fuel_flow*me.deltaT/3600.0/2;
60 indicatedNode.setValue(fuel);
61 append(tank_data, {"FuelUSGal": fuel});
62 }
63
64 var notification = notifications.PFDEventNotification.new(
65 "MFD",
66 1,
67 notifications.PFDEventNotification.FuelData,
68 { Id : "FuelData", Value : tank_data} );
69
70 me._transmitter.NotifyAll(notification);
71 },
72 };