1 // route_mgr.cxx - manage a route (i.e. a collection of waypoints)
3 // Written by Curtis Olson, started January 2004.
7 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
36 #include <simgear/compiler.h>
38 #include "route_mgr.hxx"
40 #include <boost/algorithm/string/case_conv.hpp>
41 #include <boost/tuple/tuple.hpp>
42 #include <boost/foreach.hpp>
44 #include <simgear/misc/strutils.hxx>
45 #include <simgear/structure/exception.hxx>
46 #include <simgear/structure/commands.hxx>
47 #include <simgear/misc/sg_path.hxx>
48 #include <simgear/sg_inlines.h>
50 #include "Main/fg_props.hxx"
51 #include "Navaids/positioned.hxx"
52 #include <Navaids/waypoint.hxx>
53 #include <Navaids/procedure.hxx>
54 #include "Airports/airport.hxx"
55 #include "Airports/runways.hxx"
56 #include <GUI/new_gui.hxx>
57 #include <GUI/dialog.hxx>
59 #define RM "/autopilot/route-manager/"
61 using namespace flightgear;
64 static bool commandLoadFlightPlan(const SGPropertyNode* arg)
66 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
67 SGPath path(arg->getStringValue("path"));
68 return self->loadRoute(path);
71 static bool commandSaveFlightPlan(const SGPropertyNode* arg)
73 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
74 SGPath path(arg->getStringValue("path"));
75 return self->saveRoute(path);
78 static bool commandActivateFlightPlan(const SGPropertyNode* arg)
80 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
81 bool activate = arg->getBoolValue("activate", true);
91 static bool commandClearFlightPlan(const SGPropertyNode*)
93 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
98 static bool commandSetActiveWaypt(const SGPropertyNode* arg)
100 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
101 int index = arg->getIntValue("index");
102 if ((index < 0) || (index >= self->numLegs())) {
106 self->jumpToIndex(index);
110 static bool commandInsertWaypt(const SGPropertyNode* arg)
112 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
113 int index = arg->getIntValue("index");
114 std::string ident(arg->getStringValue("id"));
115 int alt = arg->getIntValue("altitude-ft", -999);
116 int ias = arg->getIntValue("speed-knots", -999);
119 // lat/lon may be supplied to narrow down navaid search, or to specify
122 if (arg->hasChild("longitude-deg")) {
123 pos = SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
124 arg->getDoubleValue("latitude-deg"));
127 if (arg->hasChild("navaid")) {
128 FGPositionedRef p = FGPositioned::findClosestWithIdent(arg->getStringValue("navaid"), pos);
130 if (arg->hasChild("navaid", 1)) {
131 // intersection of two radials
132 FGPositionedRef p2 = FGPositioned::findClosestWithIdent(arg->getStringValue("navaid[1]"), pos);
134 SG_LOG( SG_AUTOPILOT, SG_INFO, "Unable to find FGPositioned with ident:" << arg->getStringValue("navaid[1]"));
138 double r1 = arg->getDoubleValue("radial"),
139 r2 = arg->getDoubleValue("radial[1]");
142 bool ok = SGGeodesy::radialIntersection(p->geod(), r1, p2->geod(), r2, intersection);
144 SG_LOG(SG_AUTOPILOT, SG_INFO, "no valid intersection for:" << p->ident()
145 << "," << p2->ident());
149 std::string name = p->ident() + "-" + p2->ident();
150 wp = new BasicWaypt(intersection, name, NULL);
151 } else if (arg->hasChild("offset-nm") && arg->hasChild("radial")) {
152 // offset radial from navaid
153 double radial = arg->getDoubleValue("radial");
154 double distanceNm = arg->getDoubleValue("offset-nm");
155 //radial += magvar->getDoubleValue(); // convert to true bearing
156 wp = new OffsetNavaidWaypoint(p, NULL, radial, distanceNm);
158 wp = new NavaidWaypoint(p, NULL);
160 } else if (arg->hasChild("airport")) {
161 const FGAirport* apt = fgFindAirportID(arg->getStringValue("airport"));
163 SG_LOG(SG_AUTOPILOT, SG_INFO, "no such airport" << arg->getStringValue("airport"));
167 if (arg->hasChild("runway")) {
168 if (!apt->hasRunwayWithIdent(arg->getStringValue("runway"))) {
169 SG_LOG(SG_AUTOPILOT, SG_INFO, "No runway: " << arg->getStringValue("runway") << " at " << apt->ident());
173 FGRunway* runway = apt->getRunwayByIdent(arg->getStringValue("runway"));
174 wp = new RunwayWaypt(runway, NULL);
176 wp = new NavaidWaypoint((FGAirport*) apt, NULL);
178 } else if (arg->hasChild("text")) {
179 wp = self->waypointFromString(arg->getStringValue("text"));
180 } else if (!(pos == SGGeod())) {
181 // just a raw lat/lon
182 wp = new BasicWaypt(pos, ident, NULL);
184 return false; // failed to build waypoint
187 FlightPlan::Leg* leg = self->flightPlan()->insertWayptAtIndex(wp, index);
189 leg->setAltitude(RESTRICT_AT, alt);
193 leg->setSpeed(RESTRICT_AT, ias);
199 static bool commandDeleteWaypt(const SGPropertyNode* arg)
201 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
202 int index = arg->getIntValue("index");
203 self->removeLegAtIndex(index);
207 /////////////////////////////////////////////////////////////////////////////
209 FGRouteMgr::FGRouteMgr() :
211 input(fgGetNode( RM "input", true )),
212 mirror(fgGetNode( RM "route", true ))
214 listener = new InputListener(this);
215 input->setStringValue("");
216 input->addChangeListener(listener);
218 SGCommandMgr::instance()->addCommand("load-flightplan", commandLoadFlightPlan);
219 SGCommandMgr::instance()->addCommand("save-flightplan", commandSaveFlightPlan);
220 SGCommandMgr::instance()->addCommand("activate-flightplan", commandActivateFlightPlan);
221 SGCommandMgr::instance()->addCommand("clear-flightplan", commandClearFlightPlan);
222 SGCommandMgr::instance()->addCommand("set-active-waypt", commandSetActiveWaypt);
223 SGCommandMgr::instance()->addCommand("insert-waypt", commandInsertWaypt);
224 SGCommandMgr::instance()->addCommand("delete-waypt", commandDeleteWaypt);
228 FGRouteMgr::~FGRouteMgr()
230 input->removeChangeListener(listener);
235 void FGRouteMgr::init() {
236 SGPropertyNode_ptr rm(fgGetNode(RM));
238 magvar = fgGetNode("/environment/magnetic-variation-deg", true);
240 departure = fgGetNode(RM "departure", true);
241 departure->tie("airport", SGRawValueMethods<FGRouteMgr, const char*>(*this,
242 &FGRouteMgr::getDepartureICAO, &FGRouteMgr::setDepartureICAO));
243 departure->tie("runway", SGRawValueMethods<FGRouteMgr, const char*>(*this,
244 &FGRouteMgr::getDepartureRunway,
245 &FGRouteMgr::setDepartureRunway));
246 departure->tie("sid", SGRawValueMethods<FGRouteMgr, const char*>(*this,
248 &FGRouteMgr::setSID));
250 departure->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this,
251 &FGRouteMgr::getDepartureName, NULL));
252 departure->tie("field-elevation-ft", SGRawValueMethods<FGRouteMgr, double>(*this,
253 &FGRouteMgr::getDestinationFieldElevation, NULL));
254 departure->getChild("etd", 0, true);
255 departure->getChild("takeoff-time", 0, true);
257 destination = fgGetNode(RM "destination", true);
258 destination->getChild("airport", 0, true);
260 destination->tie("airport", SGRawValueMethods<FGRouteMgr, const char*>(*this,
261 &FGRouteMgr::getDestinationICAO, &FGRouteMgr::setDestinationICAO));
262 destination->tie("runway", SGRawValueMethods<FGRouteMgr, const char*>(*this,
263 &FGRouteMgr::getDestinationRunway,
264 &FGRouteMgr::setDestinationRunway));
265 destination->tie("star", SGRawValueMethods<FGRouteMgr, const char*>(*this,
266 &FGRouteMgr::getSTAR,
267 &FGRouteMgr::setSTAR));
268 destination->tie("approach", SGRawValueMethods<FGRouteMgr, const char*>(*this,
269 &FGRouteMgr::getApproach,
270 &FGRouteMgr::setApproach));
272 destination->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this,
273 &FGRouteMgr::getDestinationName, NULL));
274 destination->tie("field-elevation-ft", SGRawValueMethods<FGRouteMgr, double>(*this,
275 &FGRouteMgr::getDestinationFieldElevation, NULL));
277 destination->getChild("eta", 0, true);
278 destination->getChild("touchdown-time", 0, true);
280 alternate = fgGetNode(RM "alternate", true);
281 alternate->getChild("airport", 0, true);
282 alternate->getChild("runway", 0, true);
284 cruise = fgGetNode(RM "cruise", true);
285 cruise->getChild("altitude-ft", 0, true);
286 cruise->setDoubleValue("altitude-ft", 10000.0);
287 cruise->getChild("flight-level", 0, true);
288 cruise->getChild("speed-kts", 0, true);
289 cruise->setDoubleValue("speed-kts", 160.0);
291 totalDistance = fgGetNode(RM "total-distance", true);
292 totalDistance->setDoubleValue(0.0);
293 distanceToGo = fgGetNode(RM "distance-remaining-nm", true);
294 distanceToGo->setDoubleValue(0.0);
296 ete = fgGetNode(RM "ete", true);
297 ete->setDoubleValue(0.0);
299 elapsedFlightTime = fgGetNode(RM "flight-time", true);
300 elapsedFlightTime->setDoubleValue(0.0);
302 active = fgGetNode(RM "active", true);
303 active->setBoolValue(false);
305 airborne = fgGetNode(RM "airborne", true);
306 airborne->setBoolValue(false);
308 _edited = fgGetNode(RM "signals/edited", true);
309 _finished = fgGetNode(RM "signals/finished", true);
310 _flightplanChanged = fgGetNode(RM "signals/flightplan-changed", true);
312 _currentWpt = fgGetNode(RM "current-wp", true);
313 _currentWpt->tie(SGRawValueMethods<FGRouteMgr, int>
314 (*this, &FGRouteMgr::currentIndex, &FGRouteMgr::jumpToIndex));
316 // temporary distance / eta calculations, for backward-compatability
317 wp0 = fgGetNode(RM "wp", 0, true);
318 wp0->getChild("id", 0, true);
319 wp0->getChild("dist", 0, true);
320 wp0->getChild("eta", 0, true);
321 wp0->getChild("bearing-deg", 0, true);
323 wp1 = fgGetNode(RM "wp", 1, true);
324 wp1->getChild("id", 0, true);
325 wp1->getChild("dist", 0, true);
326 wp1->getChild("eta", 0, true);
328 wpn = fgGetNode(RM "wp-last", 0, true);
329 wpn->getChild("dist", 0, true);
330 wpn->getChild("eta", 0, true);
332 _pathNode = fgGetNode(RM "file-path", 0, true);
336 void FGRouteMgr::postinit()
338 setFlightPlan(new FlightPlan());
340 SGPath path(_pathNode->getStringValue());
341 if (!path.isNull()) {
342 SG_LOG(SG_AUTOPILOT, SG_INFO, "loading flight-plan from: " << path.str());
346 // this code only matters for the --wp option now - perhaps the option
347 // should be deprecated in favour of an explicit flight-plan file?
348 // then the global initial waypoint list could die.
349 string_list *waypoints = globals->get_initial_waypoints();
351 string_list::iterator it;
352 for (it = waypoints->begin(); it != waypoints->end(); ++it) {
353 WayptRef w = waypointFromString(*it);
355 _plan->insertWayptAtIndex(w, -1);
359 SG_LOG(SG_AUTOPILOT, SG_INFO, "loaded initial waypoints:" << numLegs());
363 weightOnWheels = fgGetNode("/gear/gear[0]/wow", true);
364 groundSpeed = fgGetNode("/velocities/groundspeed-kt", true);
366 // check airbone flag agrees with presets
369 void FGRouteMgr::bind() { }
370 void FGRouteMgr::unbind() { }
372 bool FGRouteMgr::isRouteActive() const
374 return active->getBoolValue();
377 bool FGRouteMgr::saveRoute(const SGPath& p)
383 return _plan->save(p);
386 bool FGRouteMgr::loadRoute(const SGPath& p)
388 FlightPlan* fp = new FlightPlan;
398 FlightPlan* FGRouteMgr::flightPlan() const
403 void FGRouteMgr::setFlightPlan(FlightPlan* plan)
410 _plan->removeDelegate(this);
412 active->setBoolValue(false);
416 _plan->addDelegate(this);
418 _flightplanChanged->fireValueChanged();
420 // fire all the callbacks!
424 currentWaypointChanged();
427 void FGRouteMgr::update( double dt )
430 return; // paused, nothing to do here
433 double gs = groundSpeed->getDoubleValue();
434 if (airborne->getBoolValue()) {
435 time_t now = time(NULL);
436 elapsedFlightTime->setDoubleValue(difftime(now, _takeoffTime));
438 if (weightOnWheels->getBoolValue()) {
440 destination->setIntValue("touchdown-time", time(NULL));
441 airborne->setBoolValue(false);
443 } else { // not airborne
444 if (weightOnWheels->getBoolValue() || (gs < 40)) {
445 // either taking-off or rolling-out after touchdown
447 airborne->setBoolValue(true);
448 _takeoffTime = time(NULL); // start the clock
449 departure->setIntValue("takeoff-time", _takeoffTime);
453 if (!active->getBoolValue()) {
457 if (checkFinished()) {
461 // basic course/distance information
462 SGGeod currentPos = globals->get_aircraft_position();
464 FlightPlan::Leg* leg = _plan ? _plan->currentLeg() : NULL;
471 boost::tie(courseDeg, distanceM) = leg->waypoint()->courseAndDistanceFrom(currentPos);
473 // update wp0 / wp1 / wp-last
474 wp0->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
475 wp0->setDoubleValue("true-bearing-deg", courseDeg);
476 courseDeg -= magvar->getDoubleValue(); // expose magnetic bearing
477 wp0->setDoubleValue("bearing-deg", courseDeg);
478 setETAPropertyFromDistance(wp0->getChild("eta"), distanceM);
480 double totalPathDistanceNm = _plan->totalDistanceNm();
481 double totalDistanceRemaining = distanceM * SG_METER_TO_NM; // distance to current waypoint
483 // total distance to go, is direct distance to wp0, plus the remaining
484 // path distance from wp0
485 totalDistanceRemaining += (totalPathDistanceNm - leg->distanceAlongRoute());
487 wp0->setDoubleValue("distance-along-route-nm",
488 leg->distanceAlongRoute());
489 wp0->setDoubleValue("remaining-distance-nm",
490 totalPathDistanceNm - leg->distanceAlongRoute());
492 FlightPlan::Leg* nextLeg = _plan->nextLeg();
494 boost::tie(courseDeg, distanceM) = nextLeg->waypoint()->courseAndDistanceFrom(currentPos);
496 wp1->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
497 wp1->setDoubleValue("true-bearing-deg", courseDeg);
498 courseDeg -= magvar->getDoubleValue(); // expose magnetic bearing
499 wp1->setDoubleValue("bearing-deg", courseDeg);
500 setETAPropertyFromDistance(wp1->getChild("eta"), distanceM);
501 wp1->setDoubleValue("distance-along-route-nm",
502 nextLeg->distanceAlongRoute());
503 wp1->setDoubleValue("remaining-distance-nm",
504 totalPathDistanceNm - nextLeg->distanceAlongRoute());
507 distanceToGo->setDoubleValue(totalDistanceRemaining);
508 wpn->setDoubleValue("dist", totalDistanceRemaining);
509 ete->setDoubleValue(totalDistanceRemaining / gs * 3600.0);
510 setETAPropertyFromDistance(wpn->getChild("eta"), totalDistanceRemaining);
513 void FGRouteMgr::clearRoute()
520 Waypt* FGRouteMgr::currentWaypt() const
522 if (_plan && _plan->currentLeg()) {
523 return _plan->currentLeg()->waypoint();
529 int FGRouteMgr::currentIndex() const
535 return _plan->currentIndex();
538 Waypt* FGRouteMgr::wayptAtIndex(int index) const
541 throw sg_range_exception("wayptAtindex: no flightplan");
544 return _plan->legAtIndex(index)->waypoint();
547 int FGRouteMgr::numLegs() const
550 return _plan->numLegs();
556 void FGRouteMgr::setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance)
558 double speed = groundSpeed->getDoubleValue();
560 aProp->setStringValue("--:--");
565 double eta = aDistance * SG_METER_TO_NM / speed;
566 if ( eta >= 100.0 ) {
567 eta = 99.999; // clamp
570 if ( eta < (1.0/6.0) ) {
571 eta *= 60.0; // within 10 minutes, bump up to min/secs
574 int major = (int)eta,
575 minor = (int)((eta - (int)eta) * 60.0);
576 snprintf( eta_str, 64, "%d:%02d", major, minor );
577 aProp->setStringValue( eta_str );
580 void FGRouteMgr::removeLegAtIndex(int aIndex)
586 _plan->deleteIndex(aIndex);
589 void FGRouteMgr::waypointsChanged()
592 _edited->fireValueChanged();
594 // removing waypoints, deactivate if we hit the end.
595 if (currentIndex() >= numLegs()) {
600 // mirror internal route to the property system for inspection by other subsystems
601 void FGRouteMgr::update_mirror()
603 mirror->removeChildren("wp");
604 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
605 FGDialog* rmDlg = gui ? gui->getDialog("route-manager") : NULL;
608 mirror->setIntValue("num", 0);
610 rmDlg->updateValues();
615 int num = _plan->numLegs();
617 for (int i = 0; i < num; i++) {
618 FlightPlan::Leg* leg = _plan->legAtIndex(i);
619 WayptRef wp = leg->waypoint();
620 SGPropertyNode *prop = mirror->getChild("wp", i, 1);
622 const SGGeod& pos(wp->position());
623 prop->setStringValue("id", wp->ident().c_str());
624 prop->setDoubleValue("longitude-deg", pos.getLongitudeDeg());
625 prop->setDoubleValue("latitude-deg",pos.getLatitudeDeg());
627 // leg course+distance
629 prop->setDoubleValue("leg-bearing-true-deg", leg->courseDeg());
630 prop->setDoubleValue("leg-distance-nm", leg->distanceNm());
631 prop->setDoubleValue("distance-along-route-nm", leg->distanceAlongRoute());
633 if (leg->altitudeRestriction() != RESTRICT_NONE) {
634 double ft = leg->altitudeFt();
635 prop->setDoubleValue("altitude-m", ft * SG_FEET_TO_METER);
636 prop->setDoubleValue("altitude-ft", ft);
637 prop->setIntValue("flight-level", static_cast<int>(ft / 1000) * 10);
639 prop->setDoubleValue("altitude-m", -9999.9);
640 prop->setDoubleValue("altitude-ft", -9999.9);
643 if (leg->speedRestriction() == SPEED_RESTRICT_MACH) {
644 prop->setDoubleValue("speed-mach", leg->speedMach());
645 } else if (leg->speedRestriction() != RESTRICT_NONE) {
646 prop->setDoubleValue("speed-kts", leg->speedKts());
649 if (wp->flag(WPT_ARRIVAL)) {
650 prop->setBoolValue("arrival", true);
653 if (wp->flag(WPT_DEPARTURE)) {
654 prop->setBoolValue("departure", true);
657 if (wp->flag(WPT_MISS)) {
658 prop->setBoolValue("missed-approach", true);
661 prop->setBoolValue("generated", wp->flag(WPT_GENERATED));
662 } // of waypoint iteration
664 // set number as listener attachment point
665 mirror->setIntValue("num", _plan->numLegs());
668 rmDlg->updateValues();
671 totalDistance->setDoubleValue(_plan->totalDistanceNm());
674 // command interface /autopilot/route-manager/input:
676 // @CLEAR ... clear route
677 // @POP ... remove first entry
678 // @DELETE3 ... delete 4th entry
679 // @INSERT2:KSFO@900 ... insert "KSFO@900" as 3rd entry
680 // KSFO@900 ... append "KSFO@900"
682 void FGRouteMgr::InputListener::valueChanged(SGPropertyNode *prop)
684 const char *s = prop->getStringValue();
685 if (strlen(s) == 0) {
689 if (!strcmp(s, "@CLEAR"))
691 else if (!strcmp(s, "@ACTIVATE"))
693 else if (!strcmp(s, "@LOAD")) {
694 SGPath path(mgr->_pathNode->getStringValue());
695 mgr->loadRoute(path);
696 } else if (!strcmp(s, "@SAVE")) {
697 SGPath path(mgr->_pathNode->getStringValue());
698 mgr->saveRoute(path);
699 } else if (!strcmp(s, "@NEXT")) {
700 mgr->jumpToIndex(mgr->currentIndex() + 1);
701 } else if (!strcmp(s, "@PREVIOUS")) {
702 mgr->jumpToIndex(mgr->currentIndex() - 1);
703 } else if (!strncmp(s, "@JUMP", 5)) {
704 mgr->jumpToIndex(atoi(s + 5));
705 } else if (!strncmp(s, "@DELETE", 7))
706 mgr->removeLegAtIndex(atoi(s + 7));
707 else if (!strncmp(s, "@INSERT", 7)) {
709 int pos = strtol(s + 7, &r, 10);
715 mgr->flightPlan()->insertWayptAtIndex(mgr->waypointFromString(r), pos);
716 } else if (!strcmp(s, "@POSINIT")) {
717 mgr->initAtPosition();
719 mgr->flightPlan()->insertWayptAtIndex(mgr->waypointFromString(s), -1);
722 void FGRouteMgr::initAtPosition()
724 if (isRouteActive()) {
725 return; // don't mess with the active route
728 if (haveUserWaypoints()) {
729 // user has already defined, loaded or entered a route, again
730 // don't interfere with it
734 if (airborne->getBoolValue()) {
735 SG_LOG(SG_AUTOPILOT, SG_INFO, "initAtPosition: airborne, clearing departure info");
736 _plan->setDeparture((FGAirport*) NULL);
741 SGGeod pos = globals->get_aircraft_position();
742 if (!_plan->departureAirport()) {
743 _plan->setDeparture(FGAirport::findClosest(pos, 20.0));
744 if (!_plan->departureAirport()) {
745 SG_LOG(SG_AUTOPILOT, SG_INFO, "initAtPosition: couldn't find an airport within 20nm");
750 std::string rwy = departure->getStringValue("runway");
753 r = _plan->departureAirport()->getRunwayByIdent(rwy);
755 r = _plan->departureAirport()->findBestRunwayForPos(pos);
762 _plan->setDeparture(r);
763 SG_LOG(SG_AUTOPILOT, SG_INFO, "initAtPosition: starting at "
764 << _plan->departureAirport()->ident() << " on runway " << r->ident());
767 bool FGRouteMgr::haveUserWaypoints() const
773 bool FGRouteMgr::activate()
776 SG_LOG(SG_AUTOPILOT, SG_WARN, "::activate, no flight plan defined");
780 if (isRouteActive()) {
781 SG_LOG(SG_AUTOPILOT, SG_WARN, "duplicate route-activation, no-op");
785 _plan->setCurrentIndex(0);
786 active->setBoolValue(true);
787 SG_LOG(SG_AUTOPILOT, SG_INFO, "route-manager, activate route ok");
791 void FGRouteMgr::deactivate()
793 if (!isRouteActive()) {
797 SG_LOG(SG_AUTOPILOT, SG_INFO, "deactivating flight plan");
798 active->setBoolValue(false);
801 void FGRouteMgr::sequence()
803 if (!_plan || !active->getBoolValue()) {
804 SG_LOG(SG_AUTOPILOT, SG_ALERT, "trying to sequence waypoints with no active route");
808 int nextIndex = _plan->currentIndex() + 1;
809 if (nextIndex >= _plan->numLegs()) {
810 SG_LOG(SG_AUTOPILOT, SG_INFO, "sequenced on final leg, deactivating route");
815 _plan->setCurrentIndex(nextIndex);
818 void FGRouteMgr::endOfRoute()
820 SG_LOG(SG_AUTOPILOT, SG_INFO, "reached end of active route");
821 _finished->fireValueChanged();
822 active->setBoolValue(false);
825 bool FGRouteMgr::checkFinished()
832 // done if we're stopped on the destination runway
833 if (_plan->currentLeg() &&
834 (_plan->currentLeg()->waypoint()->source() == _plan->destinationRunway()))
836 double gs = groundSpeed->getDoubleValue();
837 done = weightOnWheels->getBoolValue() && (gs < 25);
841 SG_LOG(SG_AUTOPILOT, SG_INFO, "checkFinished: on the ground on destination runway, we're done");
847 void FGRouteMgr::jumpToIndex(int index)
853 _plan->setCurrentIndex(index);
856 void FGRouteMgr::currentWaypointChanged()
858 Waypt* cur = currentWaypt();
859 FlightPlan::Leg* next = _plan ? _plan->nextLeg() : NULL;
861 wp0->getChild("id")->setStringValue(cur ? cur->ident() : "");
862 wp1->getChild("id")->setStringValue(next ? next->waypoint()->ident() : "");
864 _currentWpt->fireValueChanged();
865 SG_LOG(SG_AUTOPILOT, SG_INFO, "route manager, current-wp is now " << currentIndex());
868 const char* FGRouteMgr::getDepartureICAO() const
870 if (!_plan || !_plan->departureAirport()) {
874 return _plan->departureAirport()->ident().c_str();
877 const char* FGRouteMgr::getDepartureName() const
879 if (!_plan || !_plan->departureAirport()) {
883 return _plan->departureAirport()->name().c_str();
886 const char* FGRouteMgr::getDepartureRunway() const
888 if (_plan && _plan->departureRunway()) {
889 return _plan->departureRunway()->ident().c_str();
895 void FGRouteMgr::setDepartureRunway(const char* aIdent)
897 FGAirport* apt = _plan->departureAirport();
898 if (!apt || (aIdent == NULL)) {
899 _plan->setDeparture(apt);
900 } else if (apt->hasRunwayWithIdent(aIdent)) {
901 _plan->setDeparture(apt->getRunwayByIdent(aIdent));
905 void FGRouteMgr::setDepartureICAO(const char* aIdent)
907 if ((aIdent == NULL) || (strlen(aIdent) < 4)) {
908 _plan->setDeparture((FGAirport*) NULL);
910 _plan->setDeparture(FGAirport::findByIdent(aIdent));
914 const char* FGRouteMgr::getSID() const
916 if (_plan && _plan->sid()) {
917 return _plan->sid()->ident().c_str();
923 static double headingDiffDeg(double a, double b)
925 double rawDiff = b - a;
926 SG_NORMALIZE_RANGE(rawDiff, -180.0, 180.0);
930 flightgear::SID* createDefaultSID(FGRunway* aRunway, double enrouteCourse)
936 double runwayElevFt = aRunway->end().getElevationFt();
938 std::ostringstream ss;
939 ss << aRunway->ident() << "-3";
941 SGGeod p = aRunway->pointOnCenterline(aRunway->lengthM() + (3.0 * SG_NM_TO_METER));
942 WayptRef w = new BasicWaypt(p, ss.str(), NULL);
943 w->setAltitude(runwayElevFt + 3000.0, RESTRICT_AT);
947 ss << aRunway->ident() << "-6";
948 p = aRunway->pointOnCenterline(aRunway->lengthM() + (6.0 * SG_NM_TO_METER));
949 w = new BasicWaypt(p, ss.str(), NULL);
950 w->setAltitude(runwayElevFt + 6000.0, RESTRICT_AT);
953 if (enrouteCourse >= 0.0) {
954 // valid enroute course
956 double course = aRunway->headingDeg();
958 while (fabs(diff = headingDiffDeg(course, enrouteCourse)) > 45.0) {
959 // turn in the sign of the heading change 45 degrees
960 course += copysign(45.0, diff);
962 ss << "DEP-" << index++;
963 SGGeod pos = wpts.back()->position();
964 pos = SGGeodesy::direct(pos, course, 3.0 * SG_NM_TO_METER);
965 w = new BasicWaypt(pos, ss.str(), NULL);
969 // no enroute course, just keep runway heading
971 ss << aRunway->ident() << "-9";
972 p = aRunway->pointOnCenterline(aRunway->lengthM() + (9.0 * SG_NM_TO_METER));
973 w = new BasicWaypt(p, ss.str(), NULL);
974 w->setAltitude(runwayElevFt + 9000.0, RESTRICT_AT);
978 BOOST_FOREACH(Waypt* w, wpts) {
979 w->setFlag(WPT_DEPARTURE);
980 w->setFlag(WPT_GENERATED);
983 return flightgear::SID::createTempSID("DEFAULT", aRunway, wpts);
986 void FGRouteMgr::setSID(const char* aIdent)
988 FGAirport* apt = _plan->departureAirport();
989 if (!apt || (aIdent == NULL)) {
990 _plan->setSID((flightgear::SID*) NULL);
994 if (!strcmp(aIdent, "DEFAULT")) {
995 double enrouteCourse = -1.0;
996 if (_plan->destinationAirport()) {
997 enrouteCourse = SGGeodesy::courseDeg(apt->geod(), _plan->destinationAirport()->geod());
1000 _plan->setSID(createDefaultSID(_plan->departureRunway(), enrouteCourse));
1004 string ident(aIdent);
1005 size_t hyphenPos = ident.find('-');
1006 if (hyphenPos != string::npos) {
1007 string sidIdent = ident.substr(0, hyphenPos);
1008 string transIdent = ident.substr(hyphenPos + 1);
1010 flightgear::SID* sid = apt->findSIDWithIdent(sidIdent);
1011 Transition* trans = sid ? sid->findTransitionByName(transIdent) : NULL;
1012 _plan->setSID(trans);
1014 _plan->setSID(apt->findSIDWithIdent(aIdent));
1018 const char* FGRouteMgr::getDestinationICAO() const
1020 if (!_plan || !_plan->destinationAirport()) {
1024 return _plan->destinationAirport()->ident().c_str();
1027 const char* FGRouteMgr::getDestinationName() const
1029 if (!_plan || !_plan->destinationAirport()) {
1033 return _plan->destinationAirport()->name().c_str();
1036 void FGRouteMgr::setDestinationICAO(const char* aIdent)
1038 if ((aIdent == NULL) || (strlen(aIdent) < 4)) {
1039 _plan->setDestination((FGAirport*) NULL);
1041 _plan->setDestination(FGAirport::findByIdent(aIdent));
1045 const char* FGRouteMgr::getDestinationRunway() const
1047 if (_plan && _plan->destinationRunway()) {
1048 return _plan->destinationRunway()->ident().c_str();
1054 void FGRouteMgr::setDestinationRunway(const char* aIdent)
1056 FGAirport* apt = _plan->destinationAirport();
1057 if (!apt || (aIdent == NULL)) {
1058 _plan->setDestination(apt);
1059 } else if (apt->hasRunwayWithIdent(aIdent)) {
1060 _plan->setDestination(apt->getRunwayByIdent(aIdent));
1064 const char* FGRouteMgr::getApproach() const
1066 if (_plan && _plan->approach()) {
1067 return _plan->approach()->ident().c_str();
1073 flightgear::Approach* createDefaultApproach(FGRunway* aRunway, double aEnrouteCourse)
1079 double thresholdElevFt = aRunway->threshold().getElevationFt();
1080 const double approachHeightFt = 2000.0;
1081 double glideslopeDistanceM = (approachHeightFt * SG_FEET_TO_METER) /
1082 tan(3.0 * SG_DEGREES_TO_RADIANS);
1084 std::ostringstream ss;
1085 ss << aRunway->ident() << "-12";
1087 SGGeod p = aRunway->pointOnCenterline(-12.0 * SG_NM_TO_METER);
1088 WayptRef w = new BasicWaypt(p, ss.str(), NULL);
1089 w->setAltitude(thresholdElevFt + 4000, RESTRICT_AT);
1092 // work back form the first point on the centerline
1094 if (aEnrouteCourse >= 0.0) {
1095 // valid enroute course
1097 double course = aRunway->headingDeg();
1099 while (fabs(diff = headingDiffDeg(aEnrouteCourse, course)) > 45.0) {
1100 // turn in the sign of the heading change 45 degrees
1101 course -= copysign(45.0, diff);
1103 ss << "APP-" << index++;
1104 SGGeod pos = wpts.front()->position();
1105 pos = SGGeodesy::direct(pos, course + 180.0, 3.0 * SG_NM_TO_METER);
1106 w = new BasicWaypt(pos, ss.str(), NULL);
1107 wpts.insert(wpts.begin(), w);
1111 p = aRunway->pointOnCenterline(-8.0 * SG_NM_TO_METER);
1113 ss << aRunway->ident() << "-8";
1114 w = new BasicWaypt(p, ss.str(), NULL);
1115 w->setAltitude(thresholdElevFt + approachHeightFt, RESTRICT_AT);
1118 p = aRunway->pointOnCenterline(-glideslopeDistanceM);
1120 ss << aRunway->ident() << "-GS";
1121 w = new BasicWaypt(p, ss.str(), NULL);
1122 w->setAltitude(thresholdElevFt + approachHeightFt, RESTRICT_AT);
1125 BOOST_FOREACH(Waypt* w, wpts) {
1126 w->setFlag(WPT_APPROACH);
1127 w->setFlag(WPT_GENERATED);
1130 return Approach::createTempApproach("DEFAULT", aRunway, wpts);
1133 void FGRouteMgr::setApproach(const char* aIdent)
1135 FGAirport* apt = _plan->destinationAirport();
1136 if (!strcmp(aIdent, "DEFAULT")) {
1137 double enrouteCourse = -1.0;
1138 if (_plan->departureAirport()) {
1139 enrouteCourse = SGGeodesy::courseDeg(_plan->departureAirport()->geod(), apt->geod());
1142 _plan->setApproach(createDefaultApproach(_plan->destinationRunway(), enrouteCourse));
1146 if (!apt || (aIdent == NULL)) {
1147 _plan->setApproach(NULL);
1149 _plan->setApproach(apt->findApproachWithIdent(aIdent));
1153 const char* FGRouteMgr::getSTAR() const
1155 if (_plan && _plan->star()) {
1156 return _plan->star()->ident().c_str();
1162 void FGRouteMgr::setSTAR(const char* aIdent)
1164 FGAirport* apt = _plan->destinationAirport();
1165 if (!apt || (aIdent == NULL)) {
1166 _plan->setSTAR((STAR*) NULL);
1170 string ident(aIdent);
1171 size_t hyphenPos = ident.find('-');
1172 if (hyphenPos != string::npos) {
1173 string starIdent = ident.substr(0, hyphenPos);
1174 string transIdent = ident.substr(hyphenPos + 1);
1176 STAR* star = apt->findSTARWithIdent(starIdent);
1177 Transition* trans = star ? star->findTransitionByName(transIdent) : NULL;
1178 _plan->setSTAR(trans);
1180 _plan->setSTAR(apt->findSTARWithIdent(aIdent));
1184 WayptRef FGRouteMgr::waypointFromString(const std::string& target)
1186 return _plan->waypointFromString(target);
1189 double FGRouteMgr::getDepartureFieldElevation() const
1191 if (!_plan || !_plan->departureAirport()) {
1195 return _plan->departureAirport()->elevation();
1198 double FGRouteMgr::getDestinationFieldElevation() const
1200 if (!_plan || !_plan->destinationAirport()) {
1204 return _plan->destinationAirport()->elevation();
1207 SGPropertyNode_ptr FGRouteMgr::wayptNodeAtIndex(int index) const
1209 if ((index < 0) || (index >= numWaypts())) {
1210 throw sg_range_exception("waypt index out of range", "FGRouteMgr::wayptAtIndex");
1213 return mirror->getChild("wp", index);