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/sgstream.hxx>
46 #include <simgear/props/props_io.hxx>
47 #include <simgear/misc/sg_path.hxx>
48 #include <simgear/route/route.hxx>
49 #include <simgear/sg_inlines.h>
51 #include "Main/fg_props.hxx"
52 #include "Navaids/positioned.hxx"
53 #include <Navaids/waypoint.hxx>
54 #include <Navaids/airways.hxx>
55 #include <Navaids/procedure.hxx>
56 #include "Airports/simple.hxx"
57 #include "Airports/runways.hxx"
59 #define RM "/autopilot/route-manager/"
61 #include <GUI/new_gui.hxx>
62 #include <GUI/dialog.hxx>
64 using namespace flightgear;
66 static bool commandLoadFlightPlan(const SGPropertyNode* arg)
68 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
69 SGPath path(arg->getStringValue("path"));
70 return self->loadRoute(path);
73 static bool commandSaveFlightPlan(const SGPropertyNode* arg)
75 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
76 SGPath path(arg->getStringValue("path"));
77 return self->saveRoute(path);
80 static bool commandActivateFlightPlan(const SGPropertyNode* arg)
82 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
83 bool activate = arg->getBoolValue("activate", true);
93 static bool commandClearFlightPlan(const SGPropertyNode*)
95 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
100 static bool commandSetActiveWaypt(const SGPropertyNode* arg)
102 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
103 int index = arg->getIntValue("index");
104 if ((index < 0) || (index >= self->numLegs())) {
108 self->jumpToIndex(index);
112 static bool commandInsertWaypt(const SGPropertyNode* arg)
114 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
115 int index = arg->getIntValue("index");
116 std::string ident(arg->getStringValue("id"));
117 int alt = arg->getIntValue("altitude-ft", -999);
118 int ias = arg->getIntValue("speed-knots", -999);
121 // lat/lon may be supplied to narrow down navaid search, or to specify
124 if (arg->hasChild("longitude-deg")) {
125 pos = SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
126 arg->getDoubleValue("latitude-deg"));
129 if (arg->hasChild("navaid")) {
130 FGPositionedRef p = FGPositioned::findClosestWithIdent(arg->getStringValue("navaid"), pos);
132 if (arg->hasChild("navaid", 1)) {
133 // intersection of two radials
134 FGPositionedRef p2 = FGPositioned::findClosestWithIdent(arg->getStringValue("navaid[1]"), pos);
136 SG_LOG( SG_AUTOPILOT, SG_INFO, "Unable to find FGPositioned with ident:" << arg->getStringValue("navaid[1]"));
140 double r1 = arg->getDoubleValue("radial"),
141 r2 = arg->getDoubleValue("radial[1]");
144 bool ok = SGGeodesy::radialIntersection(p->geod(), r1, p2->geod(), r2, intersection);
146 SG_LOG(SG_AUTOPILOT, SG_INFO, "no valid intersection for:" << p->ident()
147 << "," << p2->ident());
151 std::string name = p->ident() + "-" + p2->ident();
152 wp = new BasicWaypt(intersection, name, NULL);
153 } else if (arg->hasChild("offset-nm") && arg->hasChild("radial")) {
154 // offset radial from navaid
155 double radial = arg->getDoubleValue("radial");
156 double distanceNm = arg->getDoubleValue("offset-nm");
157 //radial += magvar->getDoubleValue(); // convert to true bearing
158 wp = new OffsetNavaidWaypoint(p, NULL, radial, distanceNm);
160 wp = new NavaidWaypoint(p, NULL);
162 } else if (arg->hasChild("airport")) {
163 const FGAirport* apt = fgFindAirportID(arg->getStringValue("airport"));
165 SG_LOG(SG_AUTOPILOT, SG_INFO, "no such airport" << arg->getStringValue("airport"));
169 if (arg->hasChild("runway")) {
170 if (!apt->hasRunwayWithIdent(arg->getStringValue("runway"))) {
171 SG_LOG(SG_AUTOPILOT, SG_INFO, "No runway: " << arg->getStringValue("runway") << " at " << apt->ident());
175 FGRunway* runway = apt->getRunwayByIdent(arg->getStringValue("runway"));
176 wp = new RunwayWaypt(runway, NULL);
178 wp = new NavaidWaypoint((FGAirport*) apt, NULL);
180 } else if (arg->hasChild("text")) {
181 wp = self->waypointFromString(arg->getStringValue("text"));
182 } else if (!(pos == SGGeod())) {
183 // just a raw lat/lon
184 wp = new BasicWaypt(pos, ident, NULL);
186 return false; // failed to build waypoint
189 FlightPlan::Leg* leg = self->flightPlan()->insertWayptAtIndex(wp, index);
191 leg->setAltitude(RESTRICT_AT, alt);
195 leg->setSpeed(RESTRICT_AT, ias);
201 static bool commandDeleteWaypt(const SGPropertyNode* arg)
203 FGRouteMgr* self = (FGRouteMgr*) globals->get_subsystem("route-manager");
204 int index = arg->getIntValue("index");
205 self->removeLegAtIndex(index);
209 /////////////////////////////////////////////////////////////////////////////
211 FGRouteMgr::FGRouteMgr() :
213 input(fgGetNode( RM "input", true )),
214 mirror(fgGetNode( RM "route", true ))
216 listener = new InputListener(this);
217 input->setStringValue("");
218 input->addChangeListener(listener);
220 SGCommandMgr::instance()->addCommand("load-flightplan", commandLoadFlightPlan);
221 SGCommandMgr::instance()->addCommand("save-flightplan", commandSaveFlightPlan);
222 SGCommandMgr::instance()->addCommand("activate-flightplan", commandActivateFlightPlan);
223 SGCommandMgr::instance()->addCommand("clear-flightplan", commandClearFlightPlan);
224 SGCommandMgr::instance()->addCommand("set-active-waypt", commandSetActiveWaypt);
225 SGCommandMgr::instance()->addCommand("insert-waypt", commandInsertWaypt);
226 SGCommandMgr::instance()->addCommand("delete-waypt", commandDeleteWaypt);
230 FGRouteMgr::~FGRouteMgr()
232 input->removeChangeListener(listener);
237 void FGRouteMgr::init() {
238 SGPropertyNode_ptr rm(fgGetNode(RM));
240 magvar = fgGetNode("/environment/magnetic-variation-deg", true);
242 departure = fgGetNode(RM "departure", true);
243 departure->tie("airport", SGRawValueMethods<FGRouteMgr, const char*>(*this,
244 &FGRouteMgr::getDepartureICAO, &FGRouteMgr::setDepartureICAO));
245 departure->tie("runway", SGRawValueMethods<FGRouteMgr, const char*>(*this,
246 &FGRouteMgr::getDepartureRunway,
247 &FGRouteMgr::setDepartureRunway));
248 departure->tie("sid", SGRawValueMethods<FGRouteMgr, const char*>(*this,
250 &FGRouteMgr::setSID));
252 departure->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this,
253 &FGRouteMgr::getDepartureName, NULL));
254 departure->tie("field-elevation-ft", SGRawValueMethods<FGRouteMgr, double>(*this,
255 &FGRouteMgr::getDestinationFieldElevation, NULL));
256 departure->getChild("etd", 0, true);
257 departure->getChild("takeoff-time", 0, true);
259 destination = fgGetNode(RM "destination", true);
260 destination->getChild("airport", 0, true);
262 destination->tie("airport", SGRawValueMethods<FGRouteMgr, const char*>(*this,
263 &FGRouteMgr::getDestinationICAO, &FGRouteMgr::setDestinationICAO));
264 destination->tie("runway", SGRawValueMethods<FGRouteMgr, const char*>(*this,
265 &FGRouteMgr::getDestinationRunway,
266 &FGRouteMgr::setDestinationRunway));
267 destination->tie("star", SGRawValueMethods<FGRouteMgr, const char*>(*this,
268 &FGRouteMgr::getSTAR,
269 &FGRouteMgr::setSTAR));
270 destination->tie("approach", SGRawValueMethods<FGRouteMgr, const char*>(*this,
271 &FGRouteMgr::getApproach,
272 &FGRouteMgr::setApproach));
274 destination->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this,
275 &FGRouteMgr::getDestinationName, NULL));
276 destination->tie("field-elevation-ft", SGRawValueMethods<FGRouteMgr, double>(*this,
277 &FGRouteMgr::getDestinationFieldElevation, NULL));
279 destination->getChild("eta", 0, true);
280 destination->getChild("touchdown-time", 0, true);
282 alternate = fgGetNode(RM "alternate", true);
283 alternate->getChild("airport", 0, true);
284 alternate->getChild("runway", 0, true);
286 cruise = fgGetNode(RM "cruise", true);
287 cruise->getChild("altitude-ft", 0, true);
288 cruise->setDoubleValue("altitude-ft", 10000.0);
289 cruise->getChild("flight-level", 0, true);
290 cruise->getChild("speed-kts", 0, true);
291 cruise->setDoubleValue("speed-kts", 160.0);
293 _routingType = cruise->getChild("routing", 0, true);
294 _routingType->setIntValue(ROUTE_HIGH_AIRWAYS);
296 totalDistance = fgGetNode(RM "total-distance", true);
297 totalDistance->setDoubleValue(0.0);
298 distanceToGo = fgGetNode(RM "distance-remaining-nm", true);
299 distanceToGo->setDoubleValue(0.0);
301 ete = fgGetNode(RM "ete", true);
302 ete->setDoubleValue(0.0);
304 elapsedFlightTime = fgGetNode(RM "flight-time", true);
305 elapsedFlightTime->setDoubleValue(0.0);
307 active = fgGetNode(RM "active", true);
308 active->setBoolValue(false);
310 airborne = fgGetNode(RM "airborne", true);
311 airborne->setBoolValue(false);
313 _edited = fgGetNode(RM "signals/edited", true);
314 _finished = fgGetNode(RM "signals/finished", true);
316 _currentWpt = fgGetNode(RM "current-wp", true);
317 _currentWpt->tie(SGRawValueMethods<FGRouteMgr, int>
318 (*this, &FGRouteMgr::currentIndex, &FGRouteMgr::jumpToIndex));
320 // temporary distance / eta calculations, for backward-compatability
321 wp0 = fgGetNode(RM "wp", 0, true);
322 wp0->getChild("id", 0, true);
323 wp0->getChild("dist", 0, true);
324 wp0->getChild("eta", 0, true);
325 wp0->getChild("bearing-deg", 0, true);
327 wp1 = fgGetNode(RM "wp", 1, true);
328 wp1->getChild("id", 0, true);
329 wp1->getChild("dist", 0, true);
330 wp1->getChild("eta", 0, true);
332 wpn = fgGetNode(RM "wp-last", 0, true);
333 wpn->getChild("dist", 0, true);
334 wpn->getChild("eta", 0, true);
336 _pathNode = fgGetNode(RM "file-path", 0, true);
337 setFlightPlan(new FlightPlan());
341 void FGRouteMgr::postinit()
343 SGPath path(_pathNode->getStringValue());
344 if (!path.isNull()) {
345 SG_LOG(SG_AUTOPILOT, SG_INFO, "loading flight-plan from: " << path.str());
349 // this code only matters for the --wp option now - perhaps the option
350 // should be deprecated in favour of an explicit flight-plan file?
351 // then the global initial waypoint list could die.
352 string_list *waypoints = globals->get_initial_waypoints();
354 string_list::iterator it;
355 for (it = waypoints->begin(); it != waypoints->end(); ++it) {
356 WayptRef w = waypointFromString(*it);
358 _plan->insertWayptAtIndex(w, -1);
362 SG_LOG(SG_AUTOPILOT, SG_INFO, "loaded initial waypoints:" << numLegs());
366 weightOnWheels = fgGetNode("/gear/gear[0]/wow", true);
367 // check airbone flag agrees with presets
370 void FGRouteMgr::bind() { }
371 void FGRouteMgr::unbind() { }
373 bool FGRouteMgr::isRouteActive() const
375 return active->getBoolValue();
378 bool FGRouteMgr::saveRoute(const SGPath& p)
384 return _plan->save(p);
387 bool FGRouteMgr::loadRoute(const SGPath& p)
389 FlightPlan* fp = new FlightPlan;
399 FlightPlan* FGRouteMgr::flightPlan() const
404 void FGRouteMgr::setFlightPlan(FlightPlan* plan)
412 active->setBoolValue(false);
416 _plan->setDelegate(this);
418 // fire all the callbacks!
422 currentWaypointChanged();
425 void FGRouteMgr::update( double dt )
428 return; // paused, nothing to do here
431 double groundSpeed = fgGetDouble("/velocities/groundspeed-kt", 0.0);
432 if (airborne->getBoolValue()) {
433 time_t now = time(NULL);
434 elapsedFlightTime->setDoubleValue(difftime(now, _takeoffTime));
435 } else { // not airborne
436 if (weightOnWheels->getBoolValue() || (groundSpeed < 40)) {
440 airborne->setBoolValue(true);
441 _takeoffTime = time(NULL); // start the clock
442 departure->setIntValue("takeoff-time", _takeoffTime);
445 if (!active->getBoolValue()) {
449 // basic course/distance information
450 SGGeod currentPos = globals->get_aircraft_position();
452 FlightPlan::Leg* leg = _plan ? _plan->currentLeg() : NULL;
459 boost::tie(courseDeg, distanceM) = leg->waypoint()->courseAndDistanceFrom(currentPos);
461 // update wp0 / wp1 / wp-last
462 wp0->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
463 wp0->setDoubleValue("true-bearing-deg", courseDeg);
464 courseDeg -= magvar->getDoubleValue(); // expose magnetic bearing
465 wp0->setDoubleValue("bearing-deg", courseDeg);
466 setETAPropertyFromDistance(wp0->getChild("eta"), distanceM);
468 double totalPathDistanceNm = _plan->totalDistanceNm();
469 double totalDistanceRemaining = distanceM * SG_METER_TO_NM; // distance to current waypoint
471 // total distance to go, is direct distance to wp0, plus the remaining
472 // path distance from wp0
473 totalDistanceRemaining += (totalPathDistanceNm - leg->distanceAlongRoute());
475 wp0->setDoubleValue("distance-along-route-nm",
476 leg->distanceAlongRoute());
477 wp0->setDoubleValue("remaining-distance-nm",
478 totalPathDistanceNm - leg->distanceAlongRoute());
480 FlightPlan::Leg* nextLeg = _plan->nextLeg();
482 boost::tie(courseDeg, distanceM) = nextLeg->waypoint()->courseAndDistanceFrom(currentPos);
484 wp1->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
485 wp1->setDoubleValue("true-bearing-deg", courseDeg);
486 courseDeg -= magvar->getDoubleValue(); // expose magnetic bearing
487 wp1->setDoubleValue("bearing-deg", courseDeg);
488 setETAPropertyFromDistance(wp1->getChild("eta"), distanceM);
489 wp1->setDoubleValue("distance-along-route-nm",
490 nextLeg->distanceAlongRoute());
491 wp1->setDoubleValue("remaining-distance-nm",
492 totalPathDistanceNm - nextLeg->distanceAlongRoute());
495 distanceToGo->setDoubleValue(totalDistanceRemaining);
496 wpn->setDoubleValue("dist", totalDistanceRemaining);
497 ete->setDoubleValue(totalDistanceRemaining / groundSpeed * 3600.0);
498 setETAPropertyFromDistance(wpn->getChild("eta"), totalDistanceRemaining);
501 void FGRouteMgr::clearRoute()
508 Waypt* FGRouteMgr::currentWaypt() const
510 if (_plan && _plan->currentLeg()) {
511 return _plan->currentLeg()->waypoint();
517 int FGRouteMgr::currentIndex() const
523 return _plan->currentIndex();
526 Waypt* FGRouteMgr::wayptAtIndex(int index) const
529 FlightPlan::Leg* leg = _plan->legAtIndex(index);
531 return leg->waypoint();
538 int FGRouteMgr::numLegs() const
541 return _plan->numLegs();
547 void FGRouteMgr::setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance)
549 double speed = fgGetDouble("/velocities/groundspeed-kt", 0.0);
551 aProp->setStringValue("--:--");
556 double eta = aDistance * SG_METER_TO_NM / speed;
557 if ( eta >= 100.0 ) {
558 eta = 99.999; // clamp
561 if ( eta < (1.0/6.0) ) {
562 eta *= 60.0; // within 10 minutes, bump up to min/secs
565 int major = (int)eta,
566 minor = (int)((eta - (int)eta) * 60.0);
567 snprintf( eta_str, 64, "%d:%02d", major, minor );
568 aProp->setStringValue( eta_str );
571 void FGRouteMgr::removeLegAtIndex(int aIndex)
577 _plan->deleteIndex(aIndex);
581 * route between index-1 and index, using airways.
583 bool FGRouteMgr::routeToIndex(int index, RouteType aRouteType)
593 if (!_plan->departureAirport()) {
594 SG_LOG(SG_AUTOPILOT, SG_WARN, "routeToIndex: no departure set");
598 wp1 = new NavaidWaypoint(_plan->departureAirport().get(), NULL);
600 wp1 = wayptAtIndex(index - 1);
603 if (index >= numLegs()) {
604 if (!_plan->destinationAirport()) {
605 SG_LOG(SG_AUTOPILOT, SG_WARN, "routeToIndex: no destination set");
609 wp2 = new NavaidWaypoint(_plan->destinationAirport().get(), NULL);
611 wp2 = wayptAtIndex(index);
614 double distNm = SGGeodesy::distanceNm(wp1->position(), wp2->position());
615 if (distNm < 100.0) {
616 SG_LOG(SG_AUTOPILOT, SG_INFO, "routeToIndex: existing waypoints are nearby, direct route");
621 switch (aRouteType) {
622 case ROUTE_HIGH_AIRWAYS:
623 Airway::highLevel()->route(wp1, wp2, r);
626 case ROUTE_LOW_AIRWAYS:
627 Airway::lowLevel()->route(wp1, wp2, r);
631 throw sg_exception("VOR routing not supported yet");
635 SG_LOG(SG_AUTOPILOT, SG_INFO, "routeToIndex: no route found");
639 _plan->insertWayptsAtIndex(r, index);
643 void FGRouteMgr::departureChanged()
645 _plan->clearWayptsWithFlag(WPT_DEPARTURE);
648 buildDeparture(enroute, wps);
649 _plan->insertWayptsAtIndex(wps, 0);
652 void FGRouteMgr::buildDeparture(WayptRef enroute, WayptVec& wps)
654 if (!_plan->departureAirport()) {
658 if (!_plan->departureRunway()) {
659 // valid airport, but no runway selected, so just the airport _plan itself
660 WayptRef w = new NavaidWaypoint(_plan->departureAirport(), _plan);
661 w->setFlag(WPT_DEPARTURE);
666 WayptRef rwyWaypt = new RunwayWaypt(_plan->departureRunway(), _plan);
667 rwyWaypt->setFlag(WPT_DEPARTURE);
668 wps.push_back(rwyWaypt);
674 _plan->sid()->route(_plan->departureRunway(), _plan->sidTransition(), wps);
677 void FGRouteMgr::arrivalChanged()
679 _plan->clearWayptsWithFlag(WPT_ARRIVAL);
680 _plan->clearWayptsWithFlag(WPT_APPROACH);
683 buildArrival(enroute, wps);
684 _plan->insertWayptsAtIndex(wps, -1);
687 void FGRouteMgr::buildArrival(WayptRef enroute, WayptVec& wps)
689 FGAirportRef apt = _plan->departureAirport();
694 if (!_plan->destinationRunway()) {
695 WayptRef w = new NavaidWaypoint(apt.ptr(), _plan);
696 w->setFlag(WPT_ARRIVAL);
702 _plan->star()->route(_plan->destinationRunway(), _plan->starTransition(), wps);
705 if (_plan->approach()) {
706 _plan->approach()->route(wps.back(), wps);
708 WayptRef w = new RunwayWaypt(_plan->destinationRunway(), _plan);
709 w->setFlag(WPT_APPROACH);
714 void FGRouteMgr::waypointsChanged()
717 _edited->fireValueChanged();
721 // mirror internal route to the property system for inspection by other subsystems
722 void FGRouteMgr::update_mirror()
724 mirror->removeChildren("wp");
725 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
726 FGDialog* rmDlg = gui ? gui->getDialog("route-manager") : NULL;
729 mirror->setIntValue("num", 0);
731 rmDlg->updateValues();
736 int num = _plan->numLegs();
738 for (int i = 0; i < num; i++) {
739 FlightPlan::Leg* leg = _plan->legAtIndex(i);
740 WayptRef wp = leg->waypoint();
741 SGPropertyNode *prop = mirror->getChild("wp", i, 1);
743 const SGGeod& pos(wp->position());
744 prop->setStringValue("id", wp->ident().c_str());
745 prop->setDoubleValue("longitude-deg", pos.getLongitudeDeg());
746 prop->setDoubleValue("latitude-deg",pos.getLatitudeDeg());
748 // leg course+distance
750 prop->setDoubleValue("leg-bearing-true-deg", leg->courseDeg());
751 prop->setDoubleValue("leg-distance-nm", leg->distanceNm());
752 prop->setDoubleValue("distance-along-route-nm", leg->distanceAlongRoute());
754 if (leg->altitudeRestriction() != RESTRICT_NONE) {
755 double ft = leg->altitudeFt();
756 prop->setDoubleValue("altitude-m", ft * SG_FEET_TO_METER);
757 prop->setDoubleValue("altitude-ft", ft);
758 prop->setIntValue("flight-level", static_cast<int>(ft / 1000) * 10);
760 prop->setDoubleValue("altitude-m", -9999.9);
761 prop->setDoubleValue("altitude-ft", -9999.9);
764 if (leg->speedRestriction() == SPEED_RESTRICT_MACH) {
765 prop->setDoubleValue("speed-mach", leg->speedMach());
766 } else if (leg->speedRestriction() != RESTRICT_NONE) {
767 prop->setDoubleValue("speed-kts", leg->speedKts());
770 if (wp->flag(WPT_ARRIVAL)) {
771 prop->setBoolValue("arrival", true);
774 if (wp->flag(WPT_DEPARTURE)) {
775 prop->setBoolValue("departure", true);
778 if (wp->flag(WPT_MISS)) {
779 prop->setBoolValue("missed-approach", true);
782 prop->setBoolValue("generated", wp->flag(WPT_GENERATED));
783 } // of waypoint iteration
785 // set number as listener attachment point
786 mirror->setIntValue("num", _plan->numLegs());
789 rmDlg->updateValues();
792 totalDistance->setDoubleValue(_plan->totalDistanceNm());
795 // command interface /autopilot/route-manager/input:
797 // @CLEAR ... clear route
798 // @POP ... remove first entry
799 // @DELETE3 ... delete 4th entry
800 // @INSERT2:KSFO@900 ... insert "KSFO@900" as 3rd entry
801 // KSFO@900 ... append "KSFO@900"
803 void FGRouteMgr::InputListener::valueChanged(SGPropertyNode *prop)
805 const char *s = prop->getStringValue();
806 if (strlen(s) == 0) {
810 if (!strcmp(s, "@CLEAR"))
812 else if (!strcmp(s, "@ACTIVATE"))
814 else if (!strcmp(s, "@LOAD")) {
815 SGPath path(mgr->_pathNode->getStringValue());
816 mgr->loadRoute(path);
817 } else if (!strcmp(s, "@SAVE")) {
818 SGPath path(mgr->_pathNode->getStringValue());
819 mgr->saveRoute(path);
820 } else if (!strcmp(s, "@NEXT")) {
821 mgr->jumpToIndex(mgr->currentIndex() + 1);
822 } else if (!strcmp(s, "@PREVIOUS")) {
823 mgr->jumpToIndex(mgr->currentIndex() - 1);
824 } else if (!strncmp(s, "@JUMP", 5)) {
825 mgr->jumpToIndex(atoi(s + 5));
826 } else if (!strncmp(s, "@DELETE", 7))
827 mgr->removeLegAtIndex(atoi(s + 7));
828 else if (!strncmp(s, "@INSERT", 7)) {
830 int pos = strtol(s + 7, &r, 10);
836 mgr->flightPlan()->insertWayptAtIndex(mgr->waypointFromString(r), pos);
837 } else if (!strncmp(s, "@ROUTE", 6)) {
839 int endIndex = strtol(s + 6, &r, 10);
840 RouteType rt = (RouteType) mgr->_routingType->getIntValue();
841 mgr->routeToIndex(endIndex, rt);
842 } else if (!strcmp(s, "@POSINIT")) {
843 mgr->initAtPosition();
845 mgr->flightPlan()->insertWayptAtIndex(mgr->waypointFromString(s), -1);
848 void FGRouteMgr::initAtPosition()
850 if (isRouteActive()) {
851 return; // don't mess with the active route
854 if (haveUserWaypoints()) {
855 // user has already defined, loaded or entered a route, again
856 // don't interfere with it
860 if (airborne->getBoolValue()) {
861 SG_LOG(SG_AUTOPILOT, SG_INFO, "initAtPosition: airborne, clearing departure info");
862 _plan->setDeparture((FGAirport*) NULL);
867 SGGeod pos = globals->get_aircraft_position();
868 if (!_plan->departureAirport()) {
869 _plan->setDeparture(FGAirport::findClosest(pos, 20.0));
870 if (!_plan->departureAirport()) {
871 SG_LOG(SG_AUTOPILOT, SG_INFO, "initAtPosition: couldn't find an airport within 20nm");
876 std::string rwy = departure->getStringValue("runway");
879 r = _plan->departureAirport()->getRunwayByIdent(rwy);
881 r = _plan->departureAirport()->findBestRunwayForPos(pos);
888 _plan->setDeparture(r);
889 SG_LOG(SG_AUTOPILOT, SG_INFO, "initAtPosition: starting at "
890 << _plan->departureAirport()->ident() << " on runway " << r->ident());
893 bool FGRouteMgr::haveUserWaypoints() const
899 bool FGRouteMgr::activate()
902 SG_LOG(SG_AUTOPILOT, SG_WARN, "::activate, no flight plan defined");
906 if (isRouteActive()) {
907 SG_LOG(SG_AUTOPILOT, SG_WARN, "duplicate route-activation, no-op");
911 _plan->setCurrentIndex(0);
912 active->setBoolValue(true);
913 SG_LOG(SG_AUTOPILOT, SG_INFO, "route-manager, activate route ok");
918 void FGRouteMgr::sequence()
920 if (!_plan || !active->getBoolValue()) {
921 SG_LOG(SG_AUTOPILOT, SG_ALERT, "trying to sequence waypoints with no active route");
925 if (checkFinished()) {
929 _plan->setCurrentIndex(_plan->currentIndex() + 1);
932 bool FGRouteMgr::checkFinished()
938 if (_plan->currentIndex() < _plan->numLegs()) {
942 SG_LOG(SG_AUTOPILOT, SG_INFO, "reached end of active route");
943 _finished->fireValueChanged();
944 active->setBoolValue(false);
948 void FGRouteMgr::jumpToIndex(int index)
954 _plan->setCurrentIndex(index);
957 void FGRouteMgr::currentWaypointChanged()
959 Waypt* cur = currentWaypt();
960 FlightPlan::Leg* next = _plan ? _plan->nextLeg() : NULL;
962 wp0->getChild("id")->setStringValue(cur ? cur->ident() : "");
963 wp1->getChild("id")->setStringValue(next ? next->waypoint()->ident() : "");
965 _currentWpt->fireValueChanged();
966 SG_LOG(SG_AUTOPILOT, SG_INFO, "route manager, current-wp is now " << currentIndex());
969 const char* FGRouteMgr::getDepartureICAO() const
971 if (!_plan || !_plan->departureAirport()) {
975 return _plan->departureAirport()->ident().c_str();
978 const char* FGRouteMgr::getDepartureName() const
980 if (!_plan || !_plan->departureAirport()) {
984 return _plan->departureAirport()->name().c_str();
987 const char* FGRouteMgr::getDepartureRunway() const
989 if (_plan && _plan->departureRunway()) {
990 return _plan->departureRunway()->ident().c_str();
996 void FGRouteMgr::setDepartureRunway(const char* aIdent)
998 FGAirport* apt = _plan->departureAirport();
999 if (!apt || (aIdent == NULL)) {
1000 _plan->setDeparture(apt);
1001 } else if (apt->hasRunwayWithIdent(aIdent)) {
1002 _plan->setDeparture(apt->getRunwayByIdent(aIdent));
1006 void FGRouteMgr::setDepartureICAO(const char* aIdent)
1008 if ((aIdent == NULL) || (strlen(aIdent) < 4)) {
1009 _plan->setDeparture((FGAirport*) NULL);
1011 _plan->setDeparture(FGAirport::findByIdent(aIdent));
1015 const char* FGRouteMgr::getSID() const
1017 if (_plan && _plan->sid()) {
1018 return _plan->sid()->ident().c_str();
1024 void FGRouteMgr::setSID(const char* aIdent)
1026 FGAirport* apt = _plan->departureAirport();
1027 if (!apt || (aIdent == NULL)) {
1028 _plan->setSID((flightgear::SID*) NULL);
1032 string ident(aIdent);
1033 size_t hyphenPos = ident.find('-');
1034 if (hyphenPos != string::npos) {
1035 string sidIdent = ident.substr(0, hyphenPos);
1036 string transIdent = ident.substr(hyphenPos + 1);
1038 flightgear::SID* sid = apt->findSIDWithIdent(sidIdent);
1039 Transition* trans = sid ? sid->findTransitionByName(transIdent) : NULL;
1040 _plan->setSID(trans);
1042 _plan->setSID(apt->findSIDWithIdent(aIdent));
1046 const char* FGRouteMgr::getDestinationICAO() const
1048 if (!_plan || !_plan->destinationAirport()) {
1052 return _plan->destinationAirport()->ident().c_str();
1055 const char* FGRouteMgr::getDestinationName() const
1057 if (!_plan || !_plan->destinationAirport()) {
1061 return _plan->destinationAirport()->name().c_str();
1064 void FGRouteMgr::setDestinationICAO(const char* aIdent)
1066 if ((aIdent == NULL) || (strlen(aIdent) < 4)) {
1067 _plan->setDestination((FGAirport*) NULL);
1069 _plan->setDestination(FGAirport::findByIdent(aIdent));
1073 const char* FGRouteMgr::getDestinationRunway() const
1075 if (_plan && _plan->destinationRunway()) {
1076 return _plan->destinationRunway()->ident().c_str();
1082 void FGRouteMgr::setDestinationRunway(const char* aIdent)
1084 FGAirport* apt = _plan->destinationAirport();
1085 if (!apt || (aIdent == NULL)) {
1086 _plan->setDestination(apt);
1087 } else if (apt->hasRunwayWithIdent(aIdent)) {
1088 _plan->setDestination(apt->getRunwayByIdent(aIdent));
1092 const char* FGRouteMgr::getApproach() const
1094 if (_plan && _plan->approach()) {
1095 return _plan->approach()->ident().c_str();
1101 void FGRouteMgr::setApproach(const char* aIdent)
1103 FGAirport* apt = _plan->destinationAirport();
1104 if (!apt || (aIdent == NULL)) {
1105 _plan->setApproach(NULL);
1107 _plan->setApproach(apt->findApproachWithIdent(aIdent));
1111 const char* FGRouteMgr::getSTAR() const
1113 if (_plan && _plan->star()) {
1114 return _plan->star()->ident().c_str();
1120 void FGRouteMgr::setSTAR(const char* aIdent)
1122 FGAirport* apt = _plan->destinationAirport();
1123 if (!apt || (aIdent == NULL)) {
1124 _plan->setSTAR((STAR*) NULL);
1128 string ident(aIdent);
1129 size_t hyphenPos = ident.find('-');
1130 if (hyphenPos != string::npos) {
1131 string starIdent = ident.substr(0, hyphenPos);
1132 string transIdent = ident.substr(hyphenPos + 1);
1134 STAR* star = apt->findSTARWithIdent(starIdent);
1135 Transition* trans = star ? star->findTransitionByName(transIdent) : NULL;
1136 _plan->setSTAR(trans);
1138 _plan->setSTAR(apt->findSTARWithIdent(aIdent));
1142 WayptRef FGRouteMgr::waypointFromString(const std::string& target)
1144 return _plan->waypointFromString(target);
1147 double FGRouteMgr::getDepartureFieldElevation() const
1149 if (!_plan || !_plan->departureAirport()) {
1153 return _plan->departureAirport()->elevation();
1156 double FGRouteMgr::getDestinationFieldElevation() const
1158 if (!_plan || !_plan->destinationAirport()) {
1162 return _plan->destinationAirport()->elevation();
1165 SGPropertyNode_ptr FGRouteMgr::wayptNodeAtIndex(int index) const
1167 if ((index < 0) || (index >= numWaypts())) {
1168 throw sg_range_exception("waypt index out of range", "FGRouteMgr::wayptAtIndex");
1171 return mirror->getChild("wp", index);