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.
34 #include <simgear/compiler.h>
36 #include "route_mgr.hxx"
38 #include <boost/algorithm/string/case_conv.hpp>
39 #include <boost/tuple/tuple.hpp>
41 #include <simgear/misc/strutils.hxx>
42 #include <simgear/structure/exception.hxx>
43 #include <simgear/structure/commands.hxx>
44 #include <simgear/misc/sg_path.hxx>
45 #include <simgear/sg_inlines.h>
47 #include "Main/fg_props.hxx"
48 #include "Navaids/positioned.hxx"
49 #include <Navaids/waypoint.hxx>
50 #include <Navaids/procedure.hxx>
51 #include "Airports/simple.hxx"
52 #include "Airports/runways.hxx"
53 #include <GUI/new_gui.hxx>
54 #include <GUI/dialog.hxx>
56 #define RM "/autopilot/route-manager/"
58 using namespace flightgear;
61 static bool commandLoadFlightPlan(const SGPropertyNode* arg)
63 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
64 SGPath path(arg->getStringValue("path"));
65 return self->loadRoute(path);
68 static bool commandSaveFlightPlan(const SGPropertyNode* arg)
70 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
71 SGPath path(arg->getStringValue("path"));
72 return self->saveRoute(path);
75 static bool commandActivateFlightPlan(const SGPropertyNode* arg)
77 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
78 bool activate = arg->getBoolValue("activate", true);
88 static bool commandClearFlightPlan(const SGPropertyNode*)
90 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
95 static bool commandSetActiveWaypt(const SGPropertyNode* arg)
97 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
98 int index = arg->getIntValue("index");
99 if ((index < 0) || (index >= self->numLegs())) {
103 self->jumpToIndex(index);
107 static bool commandInsertWaypt(const SGPropertyNode* arg)
109 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
110 int index = arg->getIntValue("index");
111 std::string ident(arg->getStringValue("id"));
112 int alt = arg->getIntValue("altitude-ft", -999);
113 int ias = arg->getIntValue("speed-knots", -999);
116 // lat/lon may be supplied to narrow down navaid search, or to specify
119 if (arg->hasChild("longitude-deg")) {
120 pos = SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
121 arg->getDoubleValue("latitude-deg"));
124 if (arg->hasChild("navaid")) {
125 FGPositionedRef p = FGPositioned::findClosestWithIdent(arg->getStringValue("navaid"), pos);
127 if (arg->hasChild("navaid", 1)) {
128 // intersection of two radials
129 FGPositionedRef p2 = FGPositioned::findClosestWithIdent(arg->getStringValue("navaid[1]"), pos);
131 SG_LOG( SG_AUTOPILOT, SG_INFO, "Unable to find FGPositioned with ident:" << arg->getStringValue("navaid[1]"));
135 double r1 = arg->getDoubleValue("radial"),
136 r2 = arg->getDoubleValue("radial[1]");
139 bool ok = SGGeodesy::radialIntersection(p->geod(), r1, p2->geod(), r2, intersection);
141 SG_LOG(SG_AUTOPILOT, SG_INFO, "no valid intersection for:" << p->ident()
142 << "," << p2->ident());
146 std::string name = p->ident() + "-" + p2->ident();
147 wp = new BasicWaypt(intersection, name, NULL);
148 } else if (arg->hasChild("offset-nm") && arg->hasChild("radial")) {
149 // offset radial from navaid
150 double radial = arg->getDoubleValue("radial");
151 double distanceNm = arg->getDoubleValue("offset-nm");
152 //radial += magvar->getDoubleValue(); // convert to true bearing
153 wp = new OffsetNavaidWaypoint(p, NULL, radial, distanceNm);
155 wp = new NavaidWaypoint(p, NULL);
157 } else if (arg->hasChild("airport")) {
158 const FGAirport* apt = fgFindAirportID(arg->getStringValue("airport"));
160 SG_LOG(SG_AUTOPILOT, SG_INFO, "no such airport" << arg->getStringValue("airport"));
164 if (arg->hasChild("runway")) {
165 if (!apt->hasRunwayWithIdent(arg->getStringValue("runway"))) {
166 SG_LOG(SG_AUTOPILOT, SG_INFO, "No runway: " << arg->getStringValue("runway") << " at " << apt->ident());
170 FGRunway* runway = apt->getRunwayByIdent(arg->getStringValue("runway"));
171 wp = new RunwayWaypt(runway, NULL);
173 wp = new NavaidWaypoint((FGAirport*) apt, NULL);
175 } else if (arg->hasChild("text")) {
176 wp = self->waypointFromString(arg->getStringValue("text"));
177 } else if (!(pos == SGGeod())) {
178 // just a raw lat/lon
179 wp = new BasicWaypt(pos, ident, NULL);
181 return false; // failed to build waypoint
184 FlightPlan::Leg* leg = self->flightPlan()->insertWayptAtIndex(wp, index);
186 leg->setAltitude(RESTRICT_AT, alt);
190 leg->setSpeed(RESTRICT_AT, ias);
196 static bool commandDeleteWaypt(const SGPropertyNode* arg)
198 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
199 int index = arg->getIntValue("index");
200 self->removeLegAtIndex(index);
204 /////////////////////////////////////////////////////////////////////////////
206 FGRouteMgr::FGRouteMgr() :
208 input(fgGetNode( RM "input", true )),
209 mirror(fgGetNode( RM "route", true ))
211 listener = new InputListener(this);
212 input->setStringValue("");
213 input->addChangeListener(listener);
215 SGCommandMgr::instance()->addCommand("load-flightplan", commandLoadFlightPlan);
216 SGCommandMgr::instance()->addCommand("save-flightplan", commandSaveFlightPlan);
217 SGCommandMgr::instance()->addCommand("activate-flightplan", commandActivateFlightPlan);
218 SGCommandMgr::instance()->addCommand("clear-flightplan", commandClearFlightPlan);
219 SGCommandMgr::instance()->addCommand("set-active-waypt", commandSetActiveWaypt);
220 SGCommandMgr::instance()->addCommand("insert-waypt", commandInsertWaypt);
221 SGCommandMgr::instance()->addCommand("delete-waypt", commandDeleteWaypt);
225 FGRouteMgr::~FGRouteMgr()
227 input->removeChangeListener(listener);
232 void FGRouteMgr::init() {
233 SGPropertyNode_ptr rm(fgGetNode(RM));
235 magvar = fgGetNode("/environment/magnetic-variation-deg", true);
237 departure = fgGetNode(RM "departure", true);
238 departure->tie("airport", SGRawValueMethods<FGRouteMgr, const char*>(*this,
239 &FGRouteMgr::getDepartureICAO, &FGRouteMgr::setDepartureICAO));
240 departure->tie("runway", SGRawValueMethods<FGRouteMgr, const char*>(*this,
241 &FGRouteMgr::getDepartureRunway,
242 &FGRouteMgr::setDepartureRunway));
243 departure->tie("sid", SGRawValueMethods<FGRouteMgr, const char*>(*this,
245 &FGRouteMgr::setSID));
247 departure->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this,
248 &FGRouteMgr::getDepartureName, NULL));
249 departure->tie("field-elevation-ft", SGRawValueMethods<FGRouteMgr, double>(*this,
250 &FGRouteMgr::getDestinationFieldElevation, NULL));
251 departure->getChild("etd", 0, true);
252 departure->getChild("takeoff-time", 0, true);
254 destination = fgGetNode(RM "destination", true);
255 destination->getChild("airport", 0, true);
257 destination->tie("airport", SGRawValueMethods<FGRouteMgr, const char*>(*this,
258 &FGRouteMgr::getDestinationICAO, &FGRouteMgr::setDestinationICAO));
259 destination->tie("runway", SGRawValueMethods<FGRouteMgr, const char*>(*this,
260 &FGRouteMgr::getDestinationRunway,
261 &FGRouteMgr::setDestinationRunway));
262 destination->tie("star", SGRawValueMethods<FGRouteMgr, const char*>(*this,
263 &FGRouteMgr::getSTAR,
264 &FGRouteMgr::setSTAR));
265 destination->tie("approach", SGRawValueMethods<FGRouteMgr, const char*>(*this,
266 &FGRouteMgr::getApproach,
267 &FGRouteMgr::setApproach));
269 destination->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this,
270 &FGRouteMgr::getDestinationName, NULL));
271 destination->tie("field-elevation-ft", SGRawValueMethods<FGRouteMgr, double>(*this,
272 &FGRouteMgr::getDestinationFieldElevation, NULL));
274 destination->getChild("eta", 0, true);
275 destination->getChild("touchdown-time", 0, true);
277 alternate = fgGetNode(RM "alternate", true);
278 alternate->getChild("airport", 0, true);
279 alternate->getChild("runway", 0, true);
281 cruise = fgGetNode(RM "cruise", true);
282 cruise->getChild("altitude-ft", 0, true);
283 cruise->setDoubleValue("altitude-ft", 10000.0);
284 cruise->getChild("flight-level", 0, true);
285 cruise->getChild("speed-kts", 0, true);
286 cruise->setDoubleValue("speed-kts", 160.0);
288 totalDistance = fgGetNode(RM "total-distance", true);
289 totalDistance->setDoubleValue(0.0);
290 distanceToGo = fgGetNode(RM "distance-remaining-nm", true);
291 distanceToGo->setDoubleValue(0.0);
293 ete = fgGetNode(RM "ete", true);
294 ete->setDoubleValue(0.0);
296 elapsedFlightTime = fgGetNode(RM "flight-time", true);
297 elapsedFlightTime->setDoubleValue(0.0);
299 active = fgGetNode(RM "active", true);
300 active->setBoolValue(false);
302 airborne = fgGetNode(RM "airborne", true);
303 airborne->setBoolValue(false);
305 _edited = fgGetNode(RM "signals/edited", true);
306 _finished = fgGetNode(RM "signals/finished", true);
307 _flightplanChanged = fgGetNode(RM "signals/flightplan-changed", true);
309 _currentWpt = fgGetNode(RM "current-wp", true);
310 _currentWpt->tie(SGRawValueMethods<FGRouteMgr, int>
311 (*this, &FGRouteMgr::currentIndex, &FGRouteMgr::jumpToIndex));
313 // temporary distance / eta calculations, for backward-compatability
314 wp0 = fgGetNode(RM "wp", 0, true);
315 wp0->getChild("id", 0, true);
316 wp0->getChild("dist", 0, true);
317 wp0->getChild("eta", 0, true);
318 wp0->getChild("bearing-deg", 0, true);
320 wp1 = fgGetNode(RM "wp", 1, true);
321 wp1->getChild("id", 0, true);
322 wp1->getChild("dist", 0, true);
323 wp1->getChild("eta", 0, true);
325 wpn = fgGetNode(RM "wp-last", 0, true);
326 wpn->getChild("dist", 0, true);
327 wpn->getChild("eta", 0, true);
329 _pathNode = fgGetNode(RM "file-path", 0, true);
333 void FGRouteMgr::postinit()
335 setFlightPlan(new FlightPlan());
337 SGPath path(_pathNode->getStringValue());
338 if (!path.isNull()) {
339 SG_LOG(SG_AUTOPILOT, SG_INFO, "loading flight-plan from: " << path.str());
343 // this code only matters for the --wp option now - perhaps the option
344 // should be deprecated in favour of an explicit flight-plan file?
345 // then the global initial waypoint list could die.
346 string_list *waypoints = globals->get_initial_waypoints();
348 string_list::iterator it;
349 for (it = waypoints->begin(); it != waypoints->end(); ++it) {
350 WayptRef w = waypointFromString(*it);
352 _plan->insertWayptAtIndex(w, -1);
356 SG_LOG(SG_AUTOPILOT, SG_INFO, "loaded initial waypoints:" << numLegs());
360 weightOnWheels = fgGetNode("/gear/gear[0]/wow", true);
361 groundSpeed = fgGetNode("/velocities/groundspeed-kt", true);
363 // check airbone flag agrees with presets
366 void FGRouteMgr::bind() { }
367 void FGRouteMgr::unbind() { }
369 bool FGRouteMgr::isRouteActive() const
371 return active->getBoolValue();
374 bool FGRouteMgr::saveRoute(const SGPath& p)
380 return _plan->save(p);
383 bool FGRouteMgr::loadRoute(const SGPath& p)
385 FlightPlan* fp = new FlightPlan;
395 FlightPlan* FGRouteMgr::flightPlan() const
400 void FGRouteMgr::setFlightPlan(FlightPlan* plan)
407 _plan->removeDelegate(this);
409 active->setBoolValue(false);
413 _plan->addDelegate(this);
415 _flightplanChanged->fireValueChanged();
417 // fire all the callbacks!
421 currentWaypointChanged();
424 void FGRouteMgr::update( double dt )
427 return; // paused, nothing to do here
430 double gs = groundSpeed->getDoubleValue();
431 if (airborne->getBoolValue()) {
432 time_t now = time(NULL);
433 elapsedFlightTime->setDoubleValue(difftime(now, _takeoffTime));
435 if (weightOnWheels->getBoolValue()) {
437 destination->setIntValue("touchdown-time", time(NULL));
438 airborne->setBoolValue(false);
440 } else { // not airborne
441 if (weightOnWheels->getBoolValue() || (gs < 40)) {
442 // either taking-off or rolling-out after touchdown
444 airborne->setBoolValue(true);
445 _takeoffTime = time(NULL); // start the clock
446 departure->setIntValue("takeoff-time", _takeoffTime);
450 if (!active->getBoolValue()) {
454 if (checkFinished()) {
458 // basic course/distance information
459 SGGeod currentPos = globals->get_aircraft_position();
461 FlightPlan::Leg* leg = _plan ? _plan->currentLeg() : NULL;
468 boost::tie(courseDeg, distanceM) = leg->waypoint()->courseAndDistanceFrom(currentPos);
470 // update wp0 / wp1 / wp-last
471 wp0->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
472 wp0->setDoubleValue("true-bearing-deg", courseDeg);
473 courseDeg -= magvar->getDoubleValue(); // expose magnetic bearing
474 wp0->setDoubleValue("bearing-deg", courseDeg);
475 setETAPropertyFromDistance(wp0->getChild("eta"), distanceM);
477 double totalPathDistanceNm = _plan->totalDistanceNm();
478 double totalDistanceRemaining = distanceM * SG_METER_TO_NM; // distance to current waypoint
480 // total distance to go, is direct distance to wp0, plus the remaining
481 // path distance from wp0
482 totalDistanceRemaining += (totalPathDistanceNm - leg->distanceAlongRoute());
484 wp0->setDoubleValue("distance-along-route-nm",
485 leg->distanceAlongRoute());
486 wp0->setDoubleValue("remaining-distance-nm",
487 totalPathDistanceNm - leg->distanceAlongRoute());
489 FlightPlan::Leg* nextLeg = _plan->nextLeg();
491 boost::tie(courseDeg, distanceM) = nextLeg->waypoint()->courseAndDistanceFrom(currentPos);
493 wp1->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
494 wp1->setDoubleValue("true-bearing-deg", courseDeg);
495 courseDeg -= magvar->getDoubleValue(); // expose magnetic bearing
496 wp1->setDoubleValue("bearing-deg", courseDeg);
497 setETAPropertyFromDistance(wp1->getChild("eta"), distanceM);
498 wp1->setDoubleValue("distance-along-route-nm",
499 nextLeg->distanceAlongRoute());
500 wp1->setDoubleValue("remaining-distance-nm",
501 totalPathDistanceNm - nextLeg->distanceAlongRoute());
504 distanceToGo->setDoubleValue(totalDistanceRemaining);
505 wpn->setDoubleValue("dist", totalDistanceRemaining);
506 ete->setDoubleValue(totalDistanceRemaining / gs * 3600.0);
507 setETAPropertyFromDistance(wpn->getChild("eta"), totalDistanceRemaining);
510 void FGRouteMgr::clearRoute()
517 Waypt* FGRouteMgr::currentWaypt() const
519 if (_plan && _plan->currentLeg()) {
520 return _plan->currentLeg()->waypoint();
526 int FGRouteMgr::currentIndex() const
532 return _plan->currentIndex();
535 Waypt* FGRouteMgr::wayptAtIndex(int index) const
538 FlightPlan::Leg* leg = _plan->legAtIndex(index);
540 return leg->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();
596 // mirror internal route to the property system for inspection by other subsystems
597 void FGRouteMgr::update_mirror()
599 mirror->removeChildren("wp");
600 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
601 FGDialog* rmDlg = gui ? gui->getDialog("route-manager") : NULL;
604 mirror->setIntValue("num", 0);
606 rmDlg->updateValues();
611 int num = _plan->numLegs();
613 for (int i = 0; i < num; i++) {
614 FlightPlan::Leg* leg = _plan->legAtIndex(i);
615 WayptRef wp = leg->waypoint();
616 SGPropertyNode *prop = mirror->getChild("wp", i, 1);
618 const SGGeod& pos(wp->position());
619 prop->setStringValue("id", wp->ident().c_str());
620 prop->setDoubleValue("longitude-deg", pos.getLongitudeDeg());
621 prop->setDoubleValue("latitude-deg",pos.getLatitudeDeg());
623 // leg course+distance
625 prop->setDoubleValue("leg-bearing-true-deg", leg->courseDeg());
626 prop->setDoubleValue("leg-distance-nm", leg->distanceNm());
627 prop->setDoubleValue("distance-along-route-nm", leg->distanceAlongRoute());
629 if (leg->altitudeRestriction() != RESTRICT_NONE) {
630 double ft = leg->altitudeFt();
631 prop->setDoubleValue("altitude-m", ft * SG_FEET_TO_METER);
632 prop->setDoubleValue("altitude-ft", ft);
633 prop->setIntValue("flight-level", static_cast<int>(ft / 1000) * 10);
635 prop->setDoubleValue("altitude-m", -9999.9);
636 prop->setDoubleValue("altitude-ft", -9999.9);
639 if (leg->speedRestriction() == SPEED_RESTRICT_MACH) {
640 prop->setDoubleValue("speed-mach", leg->speedMach());
641 } else if (leg->speedRestriction() != RESTRICT_NONE) {
642 prop->setDoubleValue("speed-kts", leg->speedKts());
645 if (wp->flag(WPT_ARRIVAL)) {
646 prop->setBoolValue("arrival", true);
649 if (wp->flag(WPT_DEPARTURE)) {
650 prop->setBoolValue("departure", true);
653 if (wp->flag(WPT_MISS)) {
654 prop->setBoolValue("missed-approach", true);
657 prop->setBoolValue("generated", wp->flag(WPT_GENERATED));
658 } // of waypoint iteration
660 // set number as listener attachment point
661 mirror->setIntValue("num", _plan->numLegs());
664 rmDlg->updateValues();
667 totalDistance->setDoubleValue(_plan->totalDistanceNm());
670 // command interface /autopilot/route-manager/input:
672 // @CLEAR ... clear route
673 // @POP ... remove first entry
674 // @DELETE3 ... delete 4th entry
675 // @INSERT2:KSFO@900 ... insert "KSFO@900" as 3rd entry
676 // KSFO@900 ... append "KSFO@900"
678 void FGRouteMgr::InputListener::valueChanged(SGPropertyNode *prop)
680 const char *s = prop->getStringValue();
681 if (strlen(s) == 0) {
685 if (!strcmp(s, "@CLEAR"))
687 else if (!strcmp(s, "@ACTIVATE"))
689 else if (!strcmp(s, "@LOAD")) {
690 SGPath path(mgr->_pathNode->getStringValue());
691 mgr->loadRoute(path);
692 } else if (!strcmp(s, "@SAVE")) {
693 SGPath path(mgr->_pathNode->getStringValue());
694 mgr->saveRoute(path);
695 } else if (!strcmp(s, "@NEXT")) {
696 mgr->jumpToIndex(mgr->currentIndex() + 1);
697 } else if (!strcmp(s, "@PREVIOUS")) {
698 mgr->jumpToIndex(mgr->currentIndex() - 1);
699 } else if (!strncmp(s, "@JUMP", 5)) {
700 mgr->jumpToIndex(atoi(s + 5));
701 } else if (!strncmp(s, "@DELETE", 7))
702 mgr->removeLegAtIndex(atoi(s + 7));
703 else if (!strncmp(s, "@INSERT", 7)) {
705 int pos = strtol(s + 7, &r, 10);
711 mgr->flightPlan()->insertWayptAtIndex(mgr->waypointFromString(r), pos);
712 } else if (!strcmp(s, "@POSINIT")) {
713 mgr->initAtPosition();
715 mgr->flightPlan()->insertWayptAtIndex(mgr->waypointFromString(s), -1);
718 void FGRouteMgr::initAtPosition()
720 if (isRouteActive()) {
721 return; // don't mess with the active route
724 if (haveUserWaypoints()) {
725 // user has already defined, loaded or entered a route, again
726 // don't interfere with it
730 if (airborne->getBoolValue()) {
731 SG_LOG(SG_AUTOPILOT, SG_INFO, "initAtPosition: airborne, clearing departure info");
732 _plan->setDeparture((FGAirport*) NULL);
737 SGGeod pos = globals->get_aircraft_position();
738 if (!_plan->departureAirport()) {
739 _plan->setDeparture(FGAirport::findClosest(pos, 20.0));
740 if (!_plan->departureAirport()) {
741 SG_LOG(SG_AUTOPILOT, SG_INFO, "initAtPosition: couldn't find an airport within 20nm");
746 std::string rwy = departure->getStringValue("runway");
749 r = _plan->departureAirport()->getRunwayByIdent(rwy);
751 r = _plan->departureAirport()->findBestRunwayForPos(pos);
758 _plan->setDeparture(r);
759 SG_LOG(SG_AUTOPILOT, SG_INFO, "initAtPosition: starting at "
760 << _plan->departureAirport()->ident() << " on runway " << r->ident());
763 bool FGRouteMgr::haveUserWaypoints() const
769 bool FGRouteMgr::activate()
772 SG_LOG(SG_AUTOPILOT, SG_WARN, "::activate, no flight plan defined");
776 if (isRouteActive()) {
777 SG_LOG(SG_AUTOPILOT, SG_WARN, "duplicate route-activation, no-op");
781 _plan->setCurrentIndex(0);
782 active->setBoolValue(true);
783 SG_LOG(SG_AUTOPILOT, SG_INFO, "route-manager, activate route ok");
787 void FGRouteMgr::deactivate()
789 if (!isRouteActive()) {
793 SG_LOG(SG_AUTOPILOT, SG_INFO, "deactivating flight plan");
794 active->setBoolValue(false);
797 void FGRouteMgr::sequence()
799 if (!_plan || !active->getBoolValue()) {
800 SG_LOG(SG_AUTOPILOT, SG_ALERT, "trying to sequence waypoints with no active route");
804 if (checkFinished()) {
808 _plan->setCurrentIndex(_plan->currentIndex() + 1);
811 bool FGRouteMgr::checkFinished()
818 // done if we're stopped on the destination runway
819 if (_plan->currentLeg() &&
820 (_plan->currentLeg()->waypoint()->source() == _plan->destinationRunway()))
822 double gs = groundSpeed->getDoubleValue();
823 done = weightOnWheels->getBoolValue() && (gs < 25);
826 // also done if we hit the final waypoint
827 if (_plan->currentIndex() >= _plan->numLegs()) {
832 SG_LOG(SG_AUTOPILOT, SG_INFO, "reached end of active route");
833 _finished->fireValueChanged();
834 active->setBoolValue(false);
840 void FGRouteMgr::jumpToIndex(int index)
846 _plan->setCurrentIndex(index);
849 void FGRouteMgr::currentWaypointChanged()
851 Waypt* cur = currentWaypt();
852 FlightPlan::Leg* next = _plan ? _plan->nextLeg() : NULL;
854 wp0->getChild("id")->setStringValue(cur ? cur->ident() : "");
855 wp1->getChild("id")->setStringValue(next ? next->waypoint()->ident() : "");
857 _currentWpt->fireValueChanged();
858 SG_LOG(SG_AUTOPILOT, SG_INFO, "route manager, current-wp is now " << currentIndex());
861 const char* FGRouteMgr::getDepartureICAO() const
863 if (!_plan || !_plan->departureAirport()) {
867 return _plan->departureAirport()->ident().c_str();
870 const char* FGRouteMgr::getDepartureName() const
872 if (!_plan || !_plan->departureAirport()) {
876 return _plan->departureAirport()->name().c_str();
879 const char* FGRouteMgr::getDepartureRunway() const
881 if (_plan && _plan->departureRunway()) {
882 return _plan->departureRunway()->ident().c_str();
888 void FGRouteMgr::setDepartureRunway(const char* aIdent)
890 FGAirport* apt = _plan->departureAirport();
891 if (!apt || (aIdent == NULL)) {
892 _plan->setDeparture(apt);
893 } else if (apt->hasRunwayWithIdent(aIdent)) {
894 _plan->setDeparture(apt->getRunwayByIdent(aIdent));
898 void FGRouteMgr::setDepartureICAO(const char* aIdent)
900 if ((aIdent == NULL) || (strlen(aIdent) < 4)) {
901 _plan->setDeparture((FGAirport*) NULL);
903 _plan->setDeparture(FGAirport::findByIdent(aIdent));
907 const char* FGRouteMgr::getSID() const
909 if (_plan && _plan->sid()) {
910 return _plan->sid()->ident().c_str();
916 void FGRouteMgr::setSID(const char* aIdent)
918 FGAirport* apt = _plan->departureAirport();
919 if (!apt || (aIdent == NULL)) {
920 _plan->setSID((flightgear::SID*) NULL);
924 string ident(aIdent);
925 size_t hyphenPos = ident.find('-');
926 if (hyphenPos != string::npos) {
927 string sidIdent = ident.substr(0, hyphenPos);
928 string transIdent = ident.substr(hyphenPos + 1);
930 flightgear::SID* sid = apt->findSIDWithIdent(sidIdent);
931 Transition* trans = sid ? sid->findTransitionByName(transIdent) : NULL;
932 _plan->setSID(trans);
934 _plan->setSID(apt->findSIDWithIdent(aIdent));
938 const char* FGRouteMgr::getDestinationICAO() const
940 if (!_plan || !_plan->destinationAirport()) {
944 return _plan->destinationAirport()->ident().c_str();
947 const char* FGRouteMgr::getDestinationName() const
949 if (!_plan || !_plan->destinationAirport()) {
953 return _plan->destinationAirport()->name().c_str();
956 void FGRouteMgr::setDestinationICAO(const char* aIdent)
958 if ((aIdent == NULL) || (strlen(aIdent) < 4)) {
959 _plan->setDestination((FGAirport*) NULL);
961 _plan->setDestination(FGAirport::findByIdent(aIdent));
965 const char* FGRouteMgr::getDestinationRunway() const
967 if (_plan && _plan->destinationRunway()) {
968 return _plan->destinationRunway()->ident().c_str();
974 void FGRouteMgr::setDestinationRunway(const char* aIdent)
976 FGAirport* apt = _plan->destinationAirport();
977 if (!apt || (aIdent == NULL)) {
978 _plan->setDestination(apt);
979 } else if (apt->hasRunwayWithIdent(aIdent)) {
980 _plan->setDestination(apt->getRunwayByIdent(aIdent));
984 const char* FGRouteMgr::getApproach() const
986 if (_plan && _plan->approach()) {
987 return _plan->approach()->ident().c_str();
993 void FGRouteMgr::setApproach(const char* aIdent)
995 FGAirport* apt = _plan->destinationAirport();
996 if (!apt || (aIdent == NULL)) {
997 _plan->setApproach(NULL);
999 _plan->setApproach(apt->findApproachWithIdent(aIdent));
1003 const char* FGRouteMgr::getSTAR() const
1005 if (_plan && _plan->star()) {
1006 return _plan->star()->ident().c_str();
1012 void FGRouteMgr::setSTAR(const char* aIdent)
1014 FGAirport* apt = _plan->destinationAirport();
1015 if (!apt || (aIdent == NULL)) {
1016 _plan->setSTAR((STAR*) NULL);
1020 string ident(aIdent);
1021 size_t hyphenPos = ident.find('-');
1022 if (hyphenPos != string::npos) {
1023 string starIdent = ident.substr(0, hyphenPos);
1024 string transIdent = ident.substr(hyphenPos + 1);
1026 STAR* star = apt->findSTARWithIdent(starIdent);
1027 Transition* trans = star ? star->findTransitionByName(transIdent) : NULL;
1028 _plan->setSTAR(trans);
1030 _plan->setSTAR(apt->findSTARWithIdent(aIdent));
1034 WayptRef FGRouteMgr::waypointFromString(const std::string& target)
1036 return _plan->waypointFromString(target);
1039 double FGRouteMgr::getDepartureFieldElevation() const
1041 if (!_plan || !_plan->departureAirport()) {
1045 return _plan->departureAirport()->elevation();
1048 double FGRouteMgr::getDestinationFieldElevation() const
1050 if (!_plan || !_plan->destinationAirport()) {
1054 return _plan->destinationAirport()->elevation();
1057 SGPropertyNode_ptr FGRouteMgr::wayptNodeAtIndex(int index) const
1059 if ((index < 0) || (index >= numWaypts())) {
1060 throw sg_range_exception("waypt index out of range", "FGRouteMgr::wayptAtIndex");
1063 return mirror->getChild("wp", index);