Debian Codegraph @

Aircraft/Instruments-3d/FG1000/Nasal/Interfaces/GenericFMSPublisher.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 # FMS Driver using Emesary to publish data from the inbuilt FMS properties
18
19 var GenericFMSPublisher =
20 {
21
22 new : func (period=1.0) {
23 var obj = {
24 parents : [
25 GenericFMSPublisher,
26 ],
27 _running : 0,
28 };
29
30 # We have two publishers here:
31 #
32 # 1) a triggered publisher for properties that will change ocassionally, but which
33 # we need to update immediately. These are typically settings.
34 #
35 # 2) a periodic publisher which triggers every 0.5s to update data values.
36 obj._triggeredPublisher = TriggeredPropertyPublisher.new(notifications.PFDEventNotification.FMSData);
37 obj._periodicPublisher = PeriodicPropertyPublisher.new(notifications.PFDEventNotification.FMSData, period);
38
39 obj._triggeredPublisher.addPropMap("FMSHeadingBug", "/autopilot/settings/heading-bug-deg", 1);
40 obj._triggeredPublisher.addPropMap("FMSSelectedAlt", "/autopilot/settings/target-alt-ft", 10);
41 obj._triggeredPublisher.addPropMap("FMSFlightPlanActive", "/autopilot/route-manager/active");
42 obj._triggeredPublisher.addPropMap("FMSFlightPlanCurrentWP", "/autopilot/route-manager/current-wp");
43 obj._triggeredPublisher.addPropMap("FMSFlightPlanSequenced", "/autopilot/route-manager/signals/sequenced");
44 obj._triggeredPublisher.addPropMap("FMSFlightPlanFinished", "/autopilot/route-manager/signals/finished");
45 obj._triggeredPublisher.addPropMap("FMSFlightPlanEdited", "/autopilot/route-manager/signals/edited");
46
47 obj._triggeredPublisher.addPropMap("FMSMode", "/instrumentation/gps/mode");
48
49 obj._periodicPublisher.addPropMap("FMSLegValid", "/instrumentation/gps/wp/wp[1]/valid");
50 obj._periodicPublisher.addPropMap("FMSPreviousLegID", "/instrumentation/gps/wp/wp[0]/ID");
51 obj._periodicPublisher.addPropMap("FMSLegID", "/instrumentation/gps/wp/wp[1]/ID");
52
53 obj._periodicPublisher.addPropMap("FMSLegBearingMagDeg", "/instrumentation/gps/wp/wp[1]/bearing-mag-deg", 1);
54 obj._periodicPublisher.addPropMap("FMSLegDistanceNM", "/instrumentation/gps/wp/wp[1]/distance-nm", 0.1);
55 obj._periodicPublisher.addPropMap("FMSLegCourseError", "/instrumentation/gps/wp/wp[1]/course-error-nm", 0.1);
56 obj._periodicPublisher.addPropMap("FMSLegDesiredTrack", "/instrumentation/gps/wp/wp[1]/desired-course-deg", 1);
57 obj._periodicPublisher.addPropMap("FMSLegTrackErrorAngle", "/instrumentation/gps/wp/wp[1]/course-deviation-deg", 1);
58 obj._periodicPublisher.addPropMap("FMSWayPointCourseError", "/instrumentation/gps/wp/wp[1]/course-error-nm", 0.1);
59
60 obj._periodicPublisher.addPropMap("FMSGroundspeed", "/instrumentation/gps/indicated-ground-speed-kt", 1);
61
62 obj._periodicPublisher.addPropMap("FMSNav1From", "/instrumentation/nav/from-flag");
63 obj._periodicPublisher.addPropMap("FMSNav2From", "/instrumentation/nav[1]/from-flag");
64
65 # Custom publish method as we need to calculate some particular values manually.
66 obj._periodicPublisher.publish = func() {
67 var gpsdata = {};
68
69 foreach (var propmap; me._propmaps) {
70 var name = propmap.getName();
71 if (propmap.hasChanged()) {
72 gpsdata[name] = propmap.getValue();
73 propmap.updateValue();
74 }
75 }
76
77 # Some GPS properties have odd values to indicate that nothing is set, so
78 # remove them from the data set.
79 if (gpsdata["FMSLegBearingMagDeg"] == -9999) gpsdata["FMSLegBearingMagDeg"] = nil;
80 if (gpsdata["FMSLegDistanceNM"] == -1) gpsdata["FMSLegDistanceNM"] = nil;
81
82 # A couple of calculated values used by the MFD Header display
83 var total_fuel = getprop("/consumables/fuel/tank[0]/fg1000-indicated-level-gal_us") or 0.0;
84 total_fuel = total_fuel + (getprop("/consumables/fuel/tank[1]/fg1000-indicated-level-gal_us") or 0.0);
85 var fuel_flow = getprop("/engines/engine[0]/fuel-flow-gph") or 1.0;
86 gpsdata["FuelOnBoard"] = total_fuel;
87 gpsdata["EnduranceHrs"] = total_fuel / fuel_flow;
88
89 var plan = flightplan();
90
91 var dst = 0.0;
92 if (plan.getPlanSize() > 0) {
93 # Determine the distance to travel, based on
94 # - current distance to the next WP,
95 # - length of each subsequent leg.
96 dst = getprop("/instrumentation/gps/wp/wp[1]/distance-nm") or 0.0;
97
98 if (plan.indexOfWP(plan.currentWP()) < (plan.getPlanSize() -1)) {
99 for(var i=plan.indexOfWP(plan.currentWP()) + 1; i < plan.getPlanSize(); i = i+1) {
100 var leg = plan.getWP(i);
101 if (leg != nil ) dst = dst + leg.leg_distance;
102 }
103 }
104 }
105
106 gpsdata["FMSDistance"] = dst;
107 var spd = math.max(getprop("/instrumentation/gps/indicated-ground-speed-kt"), 20.0);
108 var time_hrs = dst / spd;
109
110 gpsdata["FMSEstimatedTimeEnroute"] = time_hrs;
111 gpsdata["FMSFuelOverDestination"] = total_fuel - time_hrs * fuel_flow;
112
113 var notification = notifications.PFDEventNotification.new(
114 "MFD",
115 1,
116 notifications.PFDEventNotification.FMSData,
117 gpsdata);
118
119 me._transmitter.NotifyAll(notification);
120 };
121
122 return obj;
123 },
124
125 start : func() {
126 me._triggeredPublisher.start();
127 me._periodicPublisher.start();
128 me._running = 1;
129 },
130
131 stop : func() {
132 me._triggeredPublisher.stop();
133 me._periodicPublisher.stop();
134 me._running = 0;
135 },
136
137 isRunning : func() {
138 return me._running;
139 },
140
141 };
142
143 # FMS Delegate which will be triggered by the underlying route manager
144 var FMSDataDelegate = {
145 new : func(fp) {
146 var obj = {
147 parents : [FMSDataDelegate],
148 flightplan : fp,
149 };
150 return obj;
151 },
152 currentWaypointChanged : func (fp) {
153 var notification = notifications.PFDEventNotification.new(
154 "MFD",
155 1,
156 notifications.PFDEventNotification.FMSData,
157 {"FMSFlightPlanSequenced" : fp.current});
158 emesary.GlobalTransmitter.NotifyAll(notification);
159 },
160 departureChanged : func (fp) {
161 var notification = notifications.PFDEventNotification.new(
162 "MFD",
163 1,
164 notifications.PFDEventNotification.FMSData,
165 {"FMSFlightPlanEdited" : 1});
166 emesary.GlobalTransmitter.NotifyAll(notification);
167 },
168 arrivalChanged : func (fp) {
169 var notification = notifications.PFDEventNotification.new(
170 "MFD",
171 1,
172 notifications.PFDEventNotification.FMSData,
173 {"FMSFlightPlanEdited" : 1});
174 emesary.GlobalTransmitter.NotifyAll(notification);
175 },
176 waypointsChanged : func (fp) {
177 var notification = notifications.PFDEventNotification.new(
178 "MFD",
179 1,
180 notifications.PFDEventNotification.FMSData,
181 {"FMSFlightPlanEdited" : 1});
182 emesary.GlobalTransmitter.NotifyAll(notification);
183 },
184 activated : func (fp) {
185 var notification = notifications.PFDEventNotification.new(
186 "MFD",
187 1,
188 notifications.PFDEventNotification.FMSData,
189 {"FMSFlightPlanEdited" : 1});
190 emesary.GlobalTransmitter.NotifyAll(notification);
191 },
192 cleared : func (fp) {
193 var notification = notifications.PFDEventNotification.new(
194 "MFD",
195 1,
196 notifications.PFDEventNotification.FMSData,
197 {"FMSFlightPlanEdited" : 1});
198 emesary.GlobalTransmitter.NotifyAll(notification);
199 },
200 endOfFlightPlan: func (fp) {
201 var notification = notifications.PFDEventNotification.new(
202 "MFD",
203 1,
204 notifications.PFDEventNotification.FMSData,
205 {"FMSFlightPlanFinished" : 1});
206 emesary.GlobalTransmitter.NotifyAll(notification);
207 },
208 };
209
210 var dd = FMSDataDelegate.new(flightplan());
211
212 registerFlightPlanDelegate(FMSDataDelegate.new);