Debian Codegraph @

Nasal/route_manager.nas

raw
1 # SPDX-License-Identifier: GPL-2.0-or-later
2 #
3 # NOTE! This copyright does *not* cover user models that use these Nasal
4 # services by normal function calls - this is merely considered normal use
5 # of the code, and does *not* fall under the heading of "derived work."
6 #
7 # Copyright (C) 2012-202 by James Turner
8
9 # route_manager.nas - FlightPlan delegate(s) corresponding to the built-
10 # in route-manager dialog and GPS. Intended to provide a sensible default behaviour,
11 # but can be disabled by an aircraft-specific FMS / GPS system.
12
13 # This delegate corresponds to functionality of the built-in route-manager dialog.
14 # if you disable it, the built-in route-manager dialog may not work as expected.
15 # Especially, this dialog is responsible for building departure, approach and
16 # arrival waypoints corresponding to the requested SID/STAR/approach,
17 # and replacing them when the inputs change (eg, user seelcted a different
18 # destination or STAR while enroute)
19 #
20 # You can disable the default GPS behaviour *without* touching this delegate : they are
21 # kept seperate since this first one is less likely to need changes
22
23 var RouteManagerDelegate = {
24 new: func(fp) {
25 # if this property is set, don't build a delegate at all
26 if (getprop('/autopilot/route-manager/disable-route-manager'))
27 return nil;
28
29 var m = { parents: [RouteManagerDelegate] };
30 m.flightplan = fp;
31 return m;
32 },
33
34 departureChanged: func
35 {
36 logprint(LOG_INFO, 'saw departure changed');
37 me.flightplan.clearWPType('sid');
38 if (me.flightplan.departure == nil)
39 return;
40
41 if (me.flightplan.departure_runway == nil) {
42 # no runway, only an airport, use that
43 var wp = createWPFrom(me.flightplan.departure);
44 wp.wp_role = 'sid';
45 me.flightplan.insertWP(wp, 0);
46 return;
47 }
48 # first, insert the runway itself
49 var wp = createWPFrom(me.flightplan.departure_runway);
50 wp.wp_role = 'sid';
51 me.flightplan.insertWP(wp, 0);
52 if (me.flightplan.sid == nil)
53 return;
54
55 # and we have a SID
56 var sid = me.flightplan.sid;
57 logprint(LOG_INFO, 'routing via SID ' ~ sid.id);
58 me.flightplan.insertWaypoints(sid.route(me.flightplan.departure_runway, me.flightplan.sid_trans), 1);
59 },
60
61 arrivalChanged: func
62 {
63 me.flightplan.clearWPType('star');
64 me.flightplan.clearWPType('approach');
65 if (me.flightplan.destination == nil)
66 return;
67
68 if (me.flightplan.destination_runway == nil) {
69 # no runway, only an airport, use that
70 var wp = createWPFrom(me.flightplan.destination);
71 wp.wp_role = 'approach';
72 me.flightplan.appendWP(wp);
73 return;
74 }
75
76 var initialApproachFix = nil;
77 if (me.flightplan.star != nil) {
78 logprint(LOG_INFO, 'routing via STAR ' ~ me.flightplan.star.id);
79 var wps = me.flightplan.star.route(me.flightplan.destination_runway, me.flightplan.star_trans);
80 if (wps != nil) {
81 me.flightplan.insertWaypoints(wps, -1);
82 initialApproachFix = wps[-1]; # final waypoint of STAR
83 }
84 }
85
86 if (me.flightplan.approach != nil) {
87 var wps = nil;
88 var approachIdent = me.flightplan.approach.id;
89
90 if (me.flightplan.approach_trans != nil) {
91 # if an approach transition was specified, let's use it explicitly
92 wps = me.flightplan.approach.route(me.flightplan.destination_runway, me.flightplan.approach_trans);
93 if (wps == nil) {
94 logprint(LOG_WARN, "couldn't route approach " ~ approachIdent ~ " based on specified transition:" ~ me.flightplan.approach_trans);
95 }
96 } else if (initialApproachFix != nil) {
97 # no explicit approach transition, let's use the IAF to guess one
98 wps = me.flightplan.approach.route(me.flightplan.destination_runway, initialApproachFix);
99 if (wps == nil) {
100 logprint(LOG_INFO, "couldn't route approach " ~ approachIdent ~ " based on IAF:" ~ initialApproachFix.wp_name);
101 }
102 }
103
104 # depending on the order the user selects the approach or STAR, we might get into
105 # a mess here. If we failed to route so far, just try a direct to the approach
106 if (wps == nil) {
107 # route direct
108 wps = me.flightplan.approach.route(me.flightplan.destination_runway);
109 }
110
111 if (wps == nil) {
112 logprint(LOG_WARN, 'routing via approach ' ~ approachIdent ~ ' failed entirely.');
113 } else {
114 me.flightplan.insertWaypoints(wps, -1);
115 }
116 } else {
117 # no approach, just use the runway waypoint
118 var wp = createWPFrom(me.flightplan.destination_runway);
119 wp.wp_role = 'approach';
120 me.flightplan.appendWP(wp);
121 }
122 },
123
124 cleared: func
125 {
126 logprint(LOG_INFO, "saw active flightplan cleared, deactivating");
127 # see http://https://code.google.com/p/flightgear-bugs/issues/detail?id=885
128 fgcommand("activate-flightplan", props.Node.new({"activate": 0}));
129 },
130
131 endOfFlightPlan: func
132 {
133 logprint(LOG_INFO, "end of flight-plan, deactivating");
134 fgcommand("activate-flightplan", props.Node.new({"activate": 0}));
135 }
136 };
137
138 var GPSPath = "/instrumentation/gps";
139
140 # this delegate corresponds to the default behaviour of the built-in GPS.
141 # depending on the real GPS/FMS you are modelling, you probably need to
142 # replace this with your own.
143 #
144 # To do that, just set /autopilot/route-manager/disable-fms to true, which
145 # will block creation of this delegate.
146 #
147 # Of course you are then responsible for many basic FMS functions, such as
148 # route sequencing and activation
149 #
150
151 var DefaultGPSDeleagte = {
152 new: func(fp) {
153 # if this property is set, don't build a delegate at all
154 if (getprop('/autopilot/route-manager/disable-fms'))
155 return nil;
156
157 var m = { parents: [DefaultGPSDeleagte], flightplan:fp, landingCheck:nil };
158
159 logprint(LOG_INFO, 'creating default GPS FPDelegate');
160
161 # tell the GPS C++ code we will do sequencing ourselves, so it can disable
162 # its legacy logic for this
163 setprop(GPSPath ~ '/config/delegate-sequencing', 1);
164
165 # make FlightPlan behaviour match GPS config state
166 fp.followLegTrackToFix = getprop(GPSPath ~ '/config/follow-leg-track-to-fix') or 0;
167
168 # similarly, make FlightPlan follow the performance category settings
169 fp.aircraftCategory = getprop('/autopilot/settings/icao-aircraft-category') or 'A';
170
171 m._modeProp = props.globals.getNode(GPSPath ~ '/mode');
172 return m;
173 },
174
175 _landingCheckTimeout: func
176 {
177 var wow = getprop('gear/gear[0]/wow');
178 var gs = getprop('velocities/groundspeed-kt');
179 if (wow and (gs < 25)) {
180 logprint(LOG_INFO, 'GPS saw speed < 25kts on destination runway, end of route.');
181 me.landingCheck.stop();
182 # record touch-down time?
183 me.flightplan.finish();
184 }
185 },
186
187 _captureCurrentCourse: func
188 {
189 var crs = getprop(GPSPath ~ "/desired-course-deg");
190 setprop(GPSPath ~ "/selected-course-deg", crs);
191 },
192
193 _selectOBSMode: func
194 {
195 setprop(GPSPath ~ "/command", "obs");
196 },
197
198 waypointsChanged: func
199 {
200 },
201
202 activated: func
203 {
204 if (!me.flightplan.active)
205 return;
206
207 logprint(LOG_INFO,'flightplan activated, default GPS to LEG mode');
208 setprop(GPSPath ~ "/command", "leg");
209
210 if (getprop(GPSPath ~ '/wp/wp[1]/from-flag')) {
211 logprint(LOG_INFO, '\tat GPS activation, already passed active WP, sequencing');
212 me.sequence();
213 }
214 },
215
216 deactivated: func
217 {
218 if (me._modeProp.getValue() == 'leg') {
219 logprint(LOG_INFO, 'flightplan deactivated, default GPS to OBS mode');
220 me._captureCurrentCourse();
221 me._selectOBSMode();
222 }
223 },
224
225 endOfFlightPlan: func
226 {
227 if (me._modeProp.getValue() == 'leg') {
228 logprint(LOG_INFO, 'end of flight-plan, switching GPS to OBS mode');
229 me._captureCurrentCourse();
230 me._selectOBSMode();
231 }
232 },
233
234 cleared: func
235 {
236 if (!me.flightplan.active)
237 return;
238
239 if (me._modeProp.getValue() == 'leg') {
240 logprint(LOG_INFO, 'flight-plan cleared, switching GPS to OBS mode');
241 me._captureCurrentCourse();
242 me._selectOBSMode();
243 }
244 },
245
246 sequence: func
247 {
248 if (!me.flightplan.active)
249 return;
250
251 var mode = me._modeProp.getValue();
252 if (mode == 'dto') {
253 # direct-to is done, check if we should resume the following leg
254 var index = me.flightplan.indexOfWP(getprop(GPSPath ~ '/wp/wp[1]/latitude-deg'),
255 getprop(GPSPath ~ '/wp/wp[1]/longitude-deg'));
256 if (index >= 0) {
257 logprint(LOG_INFO, "default GPS reached Direct-To, resuming FP leg at " ~ index);
258 me.flightplan.current = index + 1;
259 setprop(GPSPath ~ "/command", "leg");
260 } else {
261 # revert to OBS mode
262 logprint(LOG_INFO, "default GPS reached Direct-To, resuming to OBS");
263
264 me._captureCurrentCourse();
265 me._selectOBSMode();
266 }
267 } else if (mode == 'leg') {
268 # standard leq sequencing
269 var nextIndex = me.flightplan.current + 1;
270 if (nextIndex >= me.flightplan.numWaypoints()) {
271 logprint(LOG_INFO, "default GPS sequencing, finishing flightplan");
272 me.flightplan.finish();
273 } elsif (me.flightplan.nextWP().wp_type == 'discontinuity') {
274 logprint(LOG_INFO, "default GPS sequencing DISCONTINUITY in flightplan, switching to OBS mode");
275
276 me._captureCurrentCourse();
277 me._selectOBSMode();
278 } else {
279 logprint(LOG_INFO, "default GPS sequencing to next WP");
280 me.flightplan.current = nextIndex;
281 }
282 } else {
283 # OBS, do nothing
284 }
285 },
286
287 currentWaypointChanged: func
288 {
289 if (!me.flightplan.active)
290 return;
291
292 if (me.landingCheck != nil) {
293 me.landingCheck.stop();
294 me.landingCheck = nil; # delete timer
295 }
296
297 #logprint(LOG_INFO, 'saw current WP changed, now ' ~ me.flightplan.current);
298 var active = me.flightplan.currentWP();
299 if (active == nil) return;
300
301 if (active.alt_cstr_type == "at") {
302 logprint(LOG_INFO, 'Default GPS: new WP has valid altitude restriction, setting on AP');
303 setprop('/autopilot/settings/target-altitude-ft', active.alt_cstr);
304 }
305
306 var activeRunway = active.runway();
307 # this check is needed to avoid problems with circular routes; when
308 # activating the FP we end up here, and without this check, immediately
309 # detect that we've 'landed' and finish the FP again.
310 var wow = getprop('gear/gear[0]/wow');
311
312 if (!wow and
313 (activeRunway != nil) and
314 (activeRunway.id == me.flightplan.destination_runway.id))
315 {
316 me.landingCheck = maketimer(2.0, me, DefaultGPSDeleagte._landingCheckTimeout);
317 me.landingCheck.start();
318 }
319 }
320 };
321
322 registerFlightPlanDelegate(DefaultGPSDeleagte.new);
323 registerFlightPlanDelegate(RouteManagerDelegate.new);
324
325 # =============================
326 # Extra time-processing methods
327 # =============================
328 var mod = func(n, m) {
329 var x = n - m * int(n/m); # int() truncates to zero, not -Inf
330 return x < 0 ? x + abs(m) : x; # ...so must handle negative n's
331 }
332 var update_time_string = func(){
333 #/autopilot/route-manager/ete-string
334 var time_remaining = getprop('/autopilot/route-manager/ete');
335 if (time_remaining <= 2147483647) {
336 setprop('/autopilot/route-manager/ete-string'
337 ,sprintf('%02d:%02d:%02d'
338 ,int(time_remaining / 3600)
339 ,mod(int(time_remaining / 60), 60)
340 ,mod(time_remaining, 60)
341 )
342 );
343 } else {
344 setprop('/autopilot/route-manager/ete-string', '00:00:00');
345 }
346
347 #/autopilot/route-manager/flight-time-string
348 var time_elapsed = getprop('/autopilot/route-manager/flight-time');
349 setprop('/autopilot/route-manager/flight-time-string',
350 sprintf(
351 '%02d:%02d:%02d'
352 ,int(time_elapsed / 3600)
353 ,mod(int(time_elapsed / 60), 60)
354 ,mod(time_elapsed, 60)
355 )
356 );
357
358 settimer(update_time_string, 0.1);
359 }
360 _setlistener("/sim/signals/nasal-dir-initialized", func {
361 update_time_string();
362 });
363