2020-06-09 14:05:10 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#
|
|
|
|
# NOTE! This copyright does *not* cover user models that use these Nasal
|
|
|
|
# services by normal function calls - this is merely considered normal use
|
|
|
|
# of the code, and does *not* fall under the heading of "derived work."
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-202 by James Turner
|
|
|
|
|
2013-04-07 20:45:29 +00:00
|
|
|
# route_manager.nas - FlightPlan delegate(s) corresponding to the built-
|
2014-12-19 17:02:55 +00:00
|
|
|
# in route-manager dialog and GPS. Intended to provide a sensible default behaviour,
|
2013-04-07 20:45:29 +00:00
|
|
|
# but can be disabled by an aircraft-specific FMS / GPS system.
|
2012-05-15 16:51:48 +00:00
|
|
|
|
2020-05-27 20:51:11 +00:00
|
|
|
# This delegate corresponds to functionality of the built-in route-manager dialog.
|
|
|
|
# if you disable it, the built-in route-manager dialog may not work as expected.
|
|
|
|
# Especially, this dialog is responsible for building departure, approach and
|
|
|
|
# arrival waypoints corresponding to the requested SID/STAR/approach,
|
|
|
|
# and replacing them when the inputs change (eg, user seelcted a different
|
|
|
|
# destination or STAR while enroute)
|
|
|
|
#
|
|
|
|
# You can disable the default GPS behaviour *without* touching this delegate : they are
|
|
|
|
# kept seperate since this first one is less likely to need changes
|
|
|
|
|
2012-05-15 16:51:48 +00:00
|
|
|
var RouteManagerDelegate = {
|
2015-01-10 19:37:58 +00:00
|
|
|
new: func(fp) {
|
2013-04-07 20:45:29 +00:00
|
|
|
# if this property is set, don't build a delegate at all
|
|
|
|
if (getprop('/autopilot/route-manager/disable-route-manager'))
|
|
|
|
return nil;
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2015-01-10 19:37:58 +00:00
|
|
|
var m = { parents: [RouteManagerDelegate] };
|
|
|
|
m.flightplan = fp;
|
|
|
|
return m;
|
|
|
|
},
|
2012-05-15 16:51:48 +00:00
|
|
|
|
|
|
|
departureChanged: func
|
|
|
|
{
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, 'saw departure changed');
|
2012-05-15 16:51:48 +00:00
|
|
|
me.flightplan.clearWPType('sid');
|
|
|
|
if (me.flightplan.departure == nil)
|
|
|
|
return;
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2012-05-15 16:51:48 +00:00
|
|
|
if (me.flightplan.departure_runway == nil) {
|
|
|
|
# no runway, only an airport, use that
|
|
|
|
var wp = createWPFrom(me.flightplan.departure);
|
|
|
|
wp.wp_role = 'sid';
|
|
|
|
me.flightplan.insertWP(wp, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
# first, insert the runway itself
|
|
|
|
var wp = createWPFrom(me.flightplan.departure_runway);
|
|
|
|
wp.wp_role = 'sid';
|
|
|
|
me.flightplan.insertWP(wp, 0);
|
|
|
|
if (me.flightplan.sid == nil)
|
|
|
|
return;
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2012-05-15 16:51:48 +00:00
|
|
|
# and we have a SID
|
|
|
|
var sid = me.flightplan.sid;
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, 'routing via SID ' ~ sid.id);
|
2020-05-21 21:34:28 +00:00
|
|
|
me.flightplan.insertWaypoints(sid.route(me.flightplan.departure_runway, me.flightplan.sid_trans), 1);
|
2012-05-15 16:51:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
arrivalChanged: func
|
|
|
|
{
|
|
|
|
me.flightplan.clearWPType('star');
|
|
|
|
me.flightplan.clearWPType('approach');
|
|
|
|
if (me.flightplan.destination == nil)
|
|
|
|
return;
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2012-05-15 16:51:48 +00:00
|
|
|
if (me.flightplan.destination_runway == nil) {
|
|
|
|
# no runway, only an airport, use that
|
|
|
|
var wp = createWPFrom(me.flightplan.destination);
|
|
|
|
wp.wp_role = 'approach';
|
|
|
|
me.flightplan.appendWP(wp);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-19 17:02:55 +00:00
|
|
|
|
|
|
|
var initialApproachFix = nil;
|
2012-05-15 16:51:48 +00:00
|
|
|
if (me.flightplan.star != nil) {
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, 'routing via STAR ' ~ me.flightplan.star.id);
|
2020-05-21 21:34:28 +00:00
|
|
|
var wps = me.flightplan.star.route(me.flightplan.destination_runway, me.flightplan.star_trans);
|
2019-06-24 15:41:45 +00:00
|
|
|
if (wps != nil) {
|
|
|
|
me.flightplan.insertWaypoints(wps, -1);
|
|
|
|
initialApproachFix = wps[-1]; # final waypoint of STAR
|
|
|
|
}
|
2012-05-15 16:51:48 +00:00
|
|
|
}
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2012-05-15 16:51:48 +00:00
|
|
|
if (me.flightplan.approach != nil) {
|
2020-05-27 20:51:11 +00:00
|
|
|
var wps = nil;
|
|
|
|
var approachIdent = me.flightplan.approach.id;
|
|
|
|
|
|
|
|
if (me.flightplan.approach_trans != nil) {
|
|
|
|
# if an approach transition was specified, let's use it explicitly
|
|
|
|
wps = me.flightplan.approach.route(me.flightplan.destination_runway, me.flightplan.approach_trans);
|
|
|
|
if (wps == nil) {
|
|
|
|
logprint(LOG_WARN, "couldn't route approach " ~ approachIdent ~ " based on specified transition:" ~ me.flightplan.approach_trans);
|
|
|
|
}
|
|
|
|
} else if (initialApproachFix != nil) {
|
|
|
|
# no explicit approach transition, let's use the IAF to guess one
|
|
|
|
wps = me.flightplan.approach.route(me.flightplan.destination_runway, initialApproachFix);
|
|
|
|
if (wps == nil) {
|
|
|
|
logprint(LOG_INFO, "couldn't route approach " ~ approachIdent ~ " based on IAF:" ~ initialApproachFix.wp_name);
|
|
|
|
}
|
|
|
|
}
|
2015-01-10 19:37:58 +00:00
|
|
|
|
2020-05-27 20:51:11 +00:00
|
|
|
# depending on the order the user selects the approach or STAR, we might get into
|
|
|
|
# a mess here. If we failed to route so far, just try a direct to the approach
|
|
|
|
if (wps == nil) {
|
|
|
|
# route direct
|
|
|
|
wps = me.flightplan.approach.route(me.flightplan.destination_runway);
|
|
|
|
}
|
2015-01-10 19:37:58 +00:00
|
|
|
|
|
|
|
if (wps == nil) {
|
2020-05-27 20:51:11 +00:00
|
|
|
logprint(LOG_WARN, 'routing via approach ' ~ approachIdent ~ ' failed entirely.');
|
2015-01-10 19:37:58 +00:00
|
|
|
} else {
|
|
|
|
me.flightplan.insertWaypoints(wps, -1);
|
|
|
|
}
|
2012-05-15 16:51:48 +00:00
|
|
|
} else {
|
|
|
|
# no approach, just use the runway waypoint
|
|
|
|
var wp = createWPFrom(me.flightplan.destination_runway);
|
|
|
|
wp.wp_role = 'approach';
|
|
|
|
me.flightplan.appendWP(wp);
|
|
|
|
}
|
|
|
|
},
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2012-12-31 17:38:18 +00:00
|
|
|
cleared: func
|
|
|
|
{
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, "saw active flightplan cleared, deactivating");
|
2012-12-31 17:38:18 +00:00
|
|
|
# see http://https://code.google.com/p/flightgear-bugs/issues/detail?id=885
|
|
|
|
fgcommand("activate-flightplan", props.Node.new({"activate": 0}));
|
|
|
|
},
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2013-03-20 12:24:14 +00:00
|
|
|
endOfFlightPlan: func
|
|
|
|
{
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, "end of flight-plan, deactivating");
|
2013-03-20 12:24:14 +00:00
|
|
|
fgcommand("activate-flightplan", props.Node.new({"activate": 0}));
|
2013-04-07 20:45:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-18 14:01:34 +00:00
|
|
|
var GPSPath = "/instrumentation/gps";
|
2013-04-07 20:45:29 +00:00
|
|
|
|
2019-09-18 14:01:34 +00:00
|
|
|
# this delegate corresponds to the default behaviour of the built-in GPS.
|
|
|
|
# depending on the real GPS/FMS you are modelling, you probably need to
|
|
|
|
# replace this with your own.
|
|
|
|
#
|
|
|
|
# To do that, just set /autopilot/route-manager/disable-fms to true, which
|
|
|
|
# will block creation of this delegate.
|
|
|
|
#
|
|
|
|
# Of course you are then responsible for many basic FMS functions, such as
|
|
|
|
# route sequencing and activation
|
|
|
|
#
|
|
|
|
|
|
|
|
var DefaultGPSDeleagte = {
|
2015-01-10 19:37:58 +00:00
|
|
|
new: func(fp) {
|
2019-09-18 14:01:34 +00:00
|
|
|
# if this property is set, don't build a delegate at all
|
|
|
|
if (getprop('/autopilot/route-manager/disable-fms'))
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
var m = { parents: [DefaultGPSDeleagte], flightplan:fp, landingCheck:nil };
|
|
|
|
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, 'creating default GPS FPDelegate');
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2019-09-18 14:01:34 +00:00
|
|
|
# tell the GPS C++ code we will do sequencing ourselves, so it can disable
|
|
|
|
# its legacy logic for this
|
|
|
|
setprop(GPSPath ~ '/config/delegate-sequencing', 1);
|
2015-01-08 19:48:17 +00:00
|
|
|
|
|
|
|
# make FlightPlan behaviour match GPS config state
|
2019-09-18 14:01:34 +00:00
|
|
|
fp.followLegTrackToFix = getprop(GPSPath ~ '/config/follow-leg-track-to-fix') or 0;
|
2015-01-08 19:48:17 +00:00
|
|
|
|
|
|
|
# similarly, make FlightPlan follow the performance category settings
|
2019-09-18 14:01:34 +00:00
|
|
|
fp.aircraftCategory = getprop('/autopilot/settings/icao-aircraft-category') or 'A';
|
2015-01-08 19:48:17 +00:00
|
|
|
|
2019-09-18 14:01:34 +00:00
|
|
|
m._modeProp = props.globals.getNode(GPSPath ~ '/mode');
|
2015-01-10 19:37:58 +00:00
|
|
|
return m;
|
|
|
|
},
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2013-06-01 10:12:23 +00:00
|
|
|
_landingCheckTimeout: func
|
|
|
|
{
|
|
|
|
var wow = getprop('gear/gear[0]/wow');
|
|
|
|
var gs = getprop('velocities/groundspeed-kt');
|
|
|
|
if (wow and (gs < 25)) {
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, 'GPS saw speed < 25kts on destination runway, end of route.');
|
2013-06-01 10:12:23 +00:00
|
|
|
me.landingCheck.stop();
|
|
|
|
# record touch-down time?
|
|
|
|
me.flightplan.finish();
|
|
|
|
}
|
|
|
|
},
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2020-04-19 16:05:29 +00:00
|
|
|
_captureCurrentCourse: func
|
|
|
|
{
|
|
|
|
var crs = getprop(GPSPath ~ "/desired-course-deg");
|
|
|
|
setprop(GPSPath ~ "/selected-course-deg", crs);
|
|
|
|
},
|
|
|
|
|
2019-09-18 14:01:34 +00:00
|
|
|
_selectOBSMode: func
|
|
|
|
{
|
|
|
|
setprop(GPSPath ~ "/command", "obs");
|
|
|
|
},
|
|
|
|
|
2012-05-15 16:51:48 +00:00
|
|
|
waypointsChanged: func
|
|
|
|
{
|
|
|
|
},
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2019-09-18 14:01:34 +00:00
|
|
|
activated: func
|
|
|
|
{
|
|
|
|
if (!me.flightplan.active)
|
|
|
|
return;
|
|
|
|
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO,'flightplan activated, default GPS to LEG mode');
|
2019-09-18 14:01:34 +00:00
|
|
|
setprop(GPSPath ~ "/command", "leg");
|
|
|
|
|
|
|
|
if (getprop(GPSPath ~ '/wp/wp[1]/from-flag')) {
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, '\tat GPS activation, already passed active WP, sequencing');
|
2019-09-18 14:01:34 +00:00
|
|
|
me.sequence();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
deactivated: func
|
|
|
|
{
|
|
|
|
if (me._modeProp.getValue() == 'leg') {
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, 'flightplan deactivated, default GPS to OBS mode');
|
2020-04-19 16:05:29 +00:00
|
|
|
me._captureCurrentCourse();
|
2019-09-18 14:01:34 +00:00
|
|
|
me._selectOBSMode();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-04-07 20:45:29 +00:00
|
|
|
endOfFlightPlan: func
|
|
|
|
{
|
2019-09-18 14:01:34 +00:00
|
|
|
if (me._modeProp.getValue() == 'leg') {
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, 'end of flight-plan, switching GPS to OBS mode');
|
2020-04-19 16:05:29 +00:00
|
|
|
me._captureCurrentCourse();
|
2019-09-18 14:01:34 +00:00
|
|
|
me._selectOBSMode();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
cleared: func
|
|
|
|
{
|
|
|
|
if (!me.flightplan.active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (me._modeProp.getValue() == 'leg') {
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, 'flight-plan cleared, switching GPS to OBS mode');
|
2020-04-19 16:05:29 +00:00
|
|
|
me._captureCurrentCourse();
|
2019-09-18 14:01:34 +00:00
|
|
|
me._selectOBSMode();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
sequence: func
|
|
|
|
{
|
|
|
|
if (!me.flightplan.active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var mode = me._modeProp.getValue();
|
|
|
|
if (mode == 'dto') {
|
|
|
|
# direct-to is done, check if we should resume the following leg
|
|
|
|
var index = me.flightplan.indexOfWP(getprop(GPSPath ~ '/wp/wp[1]/latitude-deg'),
|
|
|
|
getprop(GPSPath ~ '/wp/wp[1]/longitude-deg'));
|
|
|
|
if (index >= 0) {
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, "default GPS reached Direct-To, resuming FP leg at " ~ index);
|
2019-09-18 14:01:34 +00:00
|
|
|
me.flightplan.current = index + 1;
|
|
|
|
setprop(GPSPath ~ "/command", "leg");
|
|
|
|
} else {
|
|
|
|
# revert to OBS mode
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, "default GPS reached Direct-To, resuming to OBS");
|
|
|
|
|
2020-04-19 16:05:29 +00:00
|
|
|
me._captureCurrentCourse();
|
2019-09-18 14:01:34 +00:00
|
|
|
me._selectOBSMode();
|
|
|
|
}
|
|
|
|
} else if (mode == 'leg') {
|
|
|
|
# standard leq sequencing
|
|
|
|
var nextIndex = me.flightplan.current + 1;
|
|
|
|
if (nextIndex >= me.flightplan.numWaypoints()) {
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, "default GPS sequencing, finishing flightplan");
|
2019-09-18 14:01:34 +00:00
|
|
|
me.flightplan.finish();
|
2020-04-19 16:05:29 +00:00
|
|
|
} elsif (me.flightplan.nextWP().wp_type == 'discontinuity') {
|
|
|
|
logprint(LOG_INFO, "default GPS sequencing DISCONTINUITY in flightplan, switching to OBS mode");
|
|
|
|
|
|
|
|
me._captureCurrentCourse();
|
|
|
|
me._selectOBSMode();
|
2019-09-18 14:01:34 +00:00
|
|
|
} else {
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, "default GPS sequencing to next WP");
|
2019-09-18 14:01:34 +00:00
|
|
|
me.flightplan.current = nextIndex;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
# OBS, do nothing
|
|
|
|
}
|
2013-04-07 20:45:29 +00:00
|
|
|
},
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2012-05-15 16:51:48 +00:00
|
|
|
currentWaypointChanged: func
|
|
|
|
{
|
2019-09-18 14:01:34 +00:00
|
|
|
if (!me.flightplan.active)
|
|
|
|
return;
|
|
|
|
|
2013-04-07 20:45:29 +00:00
|
|
|
if (me.landingCheck != nil) {
|
2013-06-01 10:12:23 +00:00
|
|
|
me.landingCheck.stop();
|
|
|
|
me.landingCheck = nil; # delete timer
|
2013-04-07 20:45:29 +00:00
|
|
|
}
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2020-04-20 14:24:49 +00:00
|
|
|
#logprint(LOG_INFO, 'saw current WP changed, now ' ~ me.flightplan.current);
|
2013-04-07 20:45:29 +00:00
|
|
|
var active = me.flightplan.currentWP();
|
|
|
|
if (active == nil) return;
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2014-01-18 08:45:20 +00:00
|
|
|
if (active.alt_cstr_type == "at") {
|
2020-04-19 15:46:57 +00:00
|
|
|
logprint(LOG_INFO, 'Default GPS: new WP has valid altitude restriction, setting on AP');
|
2013-06-01 10:12:23 +00:00
|
|
|
setprop('/autopilot/settings/target-altitude-ft', active.alt_cstr);
|
2013-04-07 20:45:29 +00:00
|
|
|
}
|
2014-12-19 17:02:55 +00:00
|
|
|
|
2013-04-07 20:45:29 +00:00
|
|
|
var activeRunway = active.runway();
|
2013-06-01 10:12:23 +00:00
|
|
|
# this check is needed to avoid problems with circular routes; when
|
|
|
|
# activating the FP we end up here, and without this check, immediately
|
|
|
|
# detect that we've 'landed' and finish the FP again.
|
|
|
|
var wow = getprop('gear/gear[0]/wow');
|
2014-12-19 17:02:55 +00:00
|
|
|
|
|
|
|
if (!wow and
|
|
|
|
(activeRunway != nil) and
|
2013-06-01 10:12:23 +00:00
|
|
|
(activeRunway.id == me.flightplan.destination_runway.id))
|
|
|
|
{
|
2019-09-18 14:01:34 +00:00
|
|
|
me.landingCheck = maketimer(2.0, me, DefaultGPSDeleagte._landingCheckTimeout);
|
2013-06-01 10:12:23 +00:00
|
|
|
me.landingCheck.start();
|
2013-04-07 20:45:29 +00:00
|
|
|
}
|
2012-05-15 16:51:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-18 14:01:34 +00:00
|
|
|
registerFlightPlanDelegate(DefaultGPSDeleagte.new);
|
2012-05-15 16:51:48 +00:00
|
|
|
registerFlightPlanDelegate(RouteManagerDelegate.new);
|
|
|
|
|