Debian Codegraph @

src/Autopilot/route_mgr.cxx

raw symbols SCIP
1 // route_mgr.cxx - manage a route (i.e. a collection of waypoints)
2 /*
3 * SPDX-FileCopyrightText: (C) 2004 Curtis L. Olson http://www.flightgear.org/~curt
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7
8 #include <config.h>
9
10 #include <cstdio>
11
12 #include <simgear/compiler.h>
13 #include "route_mgr.hxx"
14
15 #include <simgear/misc/sg_path.hxx>
16 #include <simgear/misc/strutils.hxx>
17 #include <simgear/structure/commands.hxx>
18 #include <simgear/structure/exception.hxx>
19
20 #include <simgear/timing/sg_time.hxx>
21 #include <simgear/sg_inlines.h>
22
23 #include <Main/globals.hxx>
24 #include "Main/fg_props.hxx"
25 #include "Navaids/positioned.hxx"
26 #include <Navaids/waypoint.hxx>
27 #include <Navaids/procedure.hxx>
28 #include <Navaids/routePath.hxx>
29
30 #include "Airports/airport.hxx"
31 #include "Airports/runways.hxx"
32 #include <GUI/new_gui.hxx>
33 #include <GUI/dialog.hxx>
34 #include <GUI/MessageBox.hxx>
35
36 #define RM "/autopilot/route-manager/"
37
38 using namespace flightgear;
39 using namespace std::string_literals;
40 using std::string;
41 namespace su = simgear::strutils;
42
43 // we add this entry to some combo-boxes in route-manager.xml (the dialog),
44 // and hence PUI will send them to us.
45 const std::string kNoneString = "(none)"s;
46
47 static bool commandLoadFlightPlan(const SGPropertyNode* arg, SGPropertyNode *)
48 {
49 auto self = globals->get_subsystem<FGRouteMgr>();
50 SGPath path = SGPath::fromUtf8(arg->getStringValue("path"));
51 return self->loadRoute(path);
52 }
53
54 static bool commandSaveFlightPlan(const SGPropertyNode* arg, SGPropertyNode *)
55 {
56 auto self = globals->get_subsystem<FGRouteMgr>();
57 SGPath path = SGPath::fromUtf8(arg->getStringValue("path"));
58 const SGPath authorizedPath = SGPath(path).validate(true /* write */);
59
60 if (!authorizedPath.isNull()) {
61 return self->saveRoute(authorizedPath);
62 } else {
63 std::string msg =
64 "The route manager was asked to write the flightplan to '" +
65 path.utf8Str() + "', but this path is not authorized for writing. " +
66 "Please choose another location, for instance in the $FG_HOME/Export "
67 "folder (" + (globals->get_fg_home() / "Export").utf8Str() + ").";
68
69 SG_LOG(SG_AUTOPILOT, SG_ALERT, msg);
70 modalMessageBox("FlightGear", "Unable to write to the specified file",
71 msg);
72 return false;
73 }
74 }
75
76 static bool commandActivateFlightPlan(const SGPropertyNode* arg, SGPropertyNode *)
77 {
78 auto self = globals->get_subsystem<FGRouteMgr>();
79 bool activate = arg->getBoolValue("activate", true);
80 if (activate) {
81 self->activate();
82 } else {
83 self->deactivate();
84 }
85
86 return true;
87 }
88
89 static bool commandClearFlightPlan(const SGPropertyNode*, SGPropertyNode *)
90 {
91 auto self = globals->get_subsystem<FGRouteMgr>();
92 self->clearRoute();
93 return true;
94 }
95
96 static bool commandSetActiveWaypt(const SGPropertyNode* arg, SGPropertyNode *)
97 {
98 auto self = globals->get_subsystem<FGRouteMgr>();
99 int index = arg->getIntValue("index");
100 if ((index < 0) || (index >= self->numLegs())) {
101 return false;
102 }
103
104 self->jumpToIndex(index);
105 return true;
106 }
107
108 static bool commandInsertWaypt(const SGPropertyNode* arg, SGPropertyNode *)
109 {
110 auto self = globals->get_subsystem<FGRouteMgr>();
111 const bool haveIndex = arg->hasChild("index");
112 int index = arg->getIntValue("index");
113
114 std::string ident(arg->getStringValue("id"));
115 int alt = arg->getIntValue("altitude-ft", -999);
116 int ias = arg->getIntValue("speed-knots", -999);
117
118 WayptRef wp;
119 // lat/lon may be supplied to narrow down navaid search, or to specify
120 // a raw waypoint
121 SGGeod pos = SGGeod::invalid();
122 if (arg->hasChild("longitude-deg")) {
123 pos = SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
124 arg->getDoubleValue("latitude-deg"));
125 }
126
127 if (arg->hasChild("navaid")) {
128 if (!pos.isValid()) {
129 pos = self->flightPlan()->vicinityForInsertIndex(haveIndex ? index : -1 /* append */);
130 }
131
132 FGPositioned::TypeFilter filter({FGPositioned::Type::NDB, FGPositioned::Type::VOR,
133 FGPositioned::Type::FIX, FGPositioned::Type::WAYPOINT});
134
135 FGPositionedRef p = FGPositioned::findClosestWithIdent(arg->getStringValue("navaid"), pos, &filter);
136 if (!p) {
137 SG_LOG(SG_AUTOPILOT, SG_WARN, "Unable to find navaid with ident:" << arg->getStringValue("navaid"));
138 return false;
139 }
140
141 if (arg->hasChild("navaid", 1)) {
142 // intersection of two radials
143 FGPositionedRef p2 = FGPositioned::findClosestWithIdent(arg->getStringValue("navaid[1]"), pos, &filter);
144 if (!p2) {
145 SG_LOG( SG_AUTOPILOT, SG_INFO, "Unable to find FGPositioned with ident:" << arg->getStringValue("navaid[1]"));
146 return false;
147 }
148
149 double r1 = arg->getDoubleValue("radial"),
150 r2 = arg->getDoubleValue("radial[1]");
151
152 SGGeod intersection;
153 bool ok = SGGeodesy::radialIntersection(p->geod(), r1, p2->geod(), r2, intersection);
154 if (!ok) {
155 SG_LOG(SG_AUTOPILOT, SG_INFO, "no valid intersection for:" << p->ident()
156 << "," << p2->ident());
157 return false;
158 }
159
160 std::string name = p->ident() + "-" + p2->ident();
161 wp = new BasicWaypt(intersection, name, NULL);
162 } else if (arg->hasChild("offset-nm") && arg->hasChild("radial")) {
163 // offset radial from navaid
164 double radial = arg->getDoubleValue("radial");
165 double distanceNm = arg->getDoubleValue("offset-nm");
166 //radial += magvar->getDoubleValue(); // convert to true bearing
167 wp = new OffsetNavaidWaypoint(p, NULL, radial, distanceNm);
168 } else {
169 wp = new NavaidWaypoint(p, NULL);
170 }
171 } else if (arg->hasChild("airport")) {
172 const FGAirport* apt = fgFindAirportID(arg->getStringValue("airport"));
173 if (!apt) {
174 SG_LOG(SG_AUTOPILOT, SG_INFO, "no such airport" << arg->getStringValue("airport"));
175 return false;
176 }
177
178 if (arg->hasChild("runway")) {
179 if (!apt->hasRunwayWithIdent(arg->getStringValue("runway"))) {
180 SG_LOG(SG_AUTOPILOT, SG_INFO, "No runway: " << arg->getStringValue("runway") << " at " << apt->ident());
181 return false;
182 }
183
184 FGRunway* runway = apt->getRunwayByIdent(arg->getStringValue("runway"));
185 wp = new RunwayWaypt(runway, NULL);
186 } else {
187 wp = new NavaidWaypoint((FGAirport*) apt, NULL);
188 }
189 } else if (arg->hasChild("text")) {
190 const auto t = su::strip(arg->getStringValue("text"));
191 if (pos.isValid()) {
192 // if 'pos' is valid, use it as the search vicinity
193 wp = self->flightPlan()->waypointFromString(t, pos);
194 } else {
195 const int searchIndex = haveIndex ? index : -1;
196 wp = self->waypointFromString(t, searchIndex);
197 }
198
199 if (!wp) { // failed to build waypoint
200 SG_LOG(SG_AUTOPILOT, SG_WARN, "insert-waypoint failed: couldn't parse waypoint from '" << t << "'");
201 return false;
202 }
203 } else if (pos.isValid()) {
204 // just a raw lat/lon
205 wp = new BasicWaypt(pos, ident, NULL);
206 } else {
207 return false; // failed to build waypoint
208 }
209
210 FlightPlan::Leg* leg = self->flightPlan()->insertWayptAtIndex(wp, index);
211 if (alt >= 0) {
212 leg->setAltitude(RESTRICT_AT, alt);
213 }
214
215 if (ias > 0) {
216 leg->setSpeed(RESTRICT_AT, ias);
217 }
218
219 return true;
220 }
221
222 static bool commandDeleteWaypt(const SGPropertyNode* arg, SGPropertyNode *)
223 {
224 auto self = globals->get_subsystem<FGRouteMgr>();
225 int index = arg->getIntValue("index");
226 self->removeLegAtIndex(index);
227 return true;
228 }
229
230 /////////////////////////////////////////////////////////////////////////////
231
232 FGRouteMgr::FGRouteMgr() :
233 input(fgGetNode( RM "input", true )),
234 mirror(fgGetNode( RM "route", true ))
235 {
236 listener = new InputListener(this);
237 input->setStringValue("");
238 input->addChangeListener(listener);
239
240 SGCommandMgr* cmdMgr = globals->get_commands();
241 cmdMgr->addCommand("define-user-waypoint", this, &FGRouteMgr::commandDefineUserWaypoint);
242 cmdMgr->addCommand("delete-user-waypoint", this, &FGRouteMgr::commandDeleteUserWaypoint);
243
244 cmdMgr->addCommand("load-flightplan", commandLoadFlightPlan);
245 cmdMgr->addCommand("save-flightplan", commandSaveFlightPlan);
246 cmdMgr->addCommand("activate-flightplan", commandActivateFlightPlan);
247 cmdMgr->addCommand("clear-flightplan", commandClearFlightPlan);
248 cmdMgr->addCommand("set-active-waypt", commandSetActiveWaypt);
249 cmdMgr->addCommand("insert-waypt", commandInsertWaypt);
250 cmdMgr->addCommand("delete-waypt", commandDeleteWaypt);
251 }
252
253
254 FGRouteMgr::~FGRouteMgr()
255 {
256 input->removeChangeListener(listener);
257 delete listener;
258
259 if (_plan) {
260 _plan->removeDelegate(this);
261 }
262
263 SGCommandMgr* cmdMgr = globals->get_commands();
264 cmdMgr->removeCommand("define-user-waypoint");
265 cmdMgr->removeCommand("delete-user-waypoint");
266 cmdMgr->removeCommand("load-flightplan");
267 cmdMgr->removeCommand("save-flightplan");
268 cmdMgr->removeCommand("activate-flightplan");
269 cmdMgr->removeCommand("clear-flightplan");
270 cmdMgr->removeCommand("set-active-waypt");
271 cmdMgr->removeCommand("insert-waypt");
272 cmdMgr->removeCommand("delete-waypt");
273 }
274
275
276 void FGRouteMgr::init() {
277 SGPropertyNode_ptr rm(fgGetNode(RM));
278
279 magvar = fgGetNode("/environment/magnetic-variation-deg", true);
280
281 departure = fgGetNode(RM "departure", true);
282 departure->tie("airport", SGStringValueMethods<FGRouteMgr>(*this,
283 &FGRouteMgr::getDepartureICAO, &FGRouteMgr::setDepartureICAO));
284 departure->tie("runway", SGStringValueMethods<FGRouteMgr>(*this,
285 &FGRouteMgr::getDepartureRunway,
286 &FGRouteMgr::setDepartureRunway));
287 departure->tie("sid", SGStringValueMethods<FGRouteMgr>(*this,
288 &FGRouteMgr::getSID,
289 &FGRouteMgr::setSID));
290
291 departure->tie("sid-with-trans", SGStringValueMethods<FGRouteMgr>(*this,
292 &FGRouteMgr::getSIDWithTransition,
293 &FGRouteMgr::setSID));
294
295 departure->tie("name", SGStringValueMethods<FGRouteMgr>(*this,
296 &FGRouteMgr::getDepartureName, nullptr));
297 departure->tie("field-elevation-ft", SGRawValueMethods<FGRouteMgr, double>(*this,
298 &FGRouteMgr::getDepartureFieldElevation, nullptr));
299 departure->getChild("etd", 0, true);
300 departure->getChild("takeoff-time", 0, true);
301
302 destination = fgGetNode(RM "destination", true);
303 destination->getChild("airport", 0, true);
304
305 destination->tie("airport", SGStringValueMethods<FGRouteMgr>(*this,
306 &FGRouteMgr::getDestinationICAO, &FGRouteMgr::setDestinationICAO));
307 destination->tie("runway", SGStringValueMethods<FGRouteMgr>(*this,
308 &FGRouteMgr::getDestinationRunway,
309 &FGRouteMgr::setDestinationRunway));
310 destination->tie("star", SGStringValueMethods<FGRouteMgr>(*this,
311 &FGRouteMgr::getSTAR,
312 &FGRouteMgr::setSTAR));
313 destination->tie("star-with-trans", SGStringValueMethods<FGRouteMgr>(*this,
314 &FGRouteMgr::getSTARWithTransition,
315 &FGRouteMgr::setSTAR));
316 destination->tie("approach", SGStringValueMethods<FGRouteMgr>(*this,
317 &FGRouteMgr::getApproach,
318 &FGRouteMgr::setApproach));
319 destination->tie("approach-with-trans", SGStringValueMethods<FGRouteMgr>(*this,
320 &FGRouteMgr::getApproachWithTransition,
321 &FGRouteMgr::setApproach));
322
323 destination->tie("name", SGStringValueMethods<FGRouteMgr>(*this,
324 &FGRouteMgr::getDestinationName, nullptr));
325 destination->tie("field-elevation-ft", SGRawValueMethods<FGRouteMgr, double>(*this,
326 &FGRouteMgr::getDestinationFieldElevation, nullptr));
327
328 destination->getChild("eta", 0, true);
329 destination->getChild("eta-seconds", 0, true);
330 destination->getChild("touchdown-time", 0, true);
331
332 alternate = fgGetNode(RM "alternate", true);
333 alternate->tie("airport", SGStringValueMethods<FGRouteMgr>(*this,
334 &FGRouteMgr::getAlternate,
335 &FGRouteMgr::setAlternate));
336 alternate->tie("name", SGStringValueMethods<FGRouteMgr>(*this,
337 &FGRouteMgr::getAlternateName, nullptr));
338
339 cruise = fgGetNode(RM "cruise", true);
340 cruise->tie("altitude-ft", SGRawValueMethods<FGRouteMgr, int>(*this,
341 &FGRouteMgr::getCruiseAltitudeFt,
342 &FGRouteMgr::setCruiseAltitudeFt));
343 cruise->tie("flight-level", SGRawValueMethods<FGRouteMgr, int>(*this,
344 &FGRouteMgr::getCruiseFlightLevel,
345 &FGRouteMgr::setCruiseFlightLevel));
346 cruise->tie("speed-kts", SGRawValueMethods<FGRouteMgr, int>(*this,
347 &FGRouteMgr::getCruiseSpeedKnots,
348 &FGRouteMgr::setCruiseSpeedKnots));
349 cruise->tie("mach", SGRawValueMethods<FGRouteMgr, double>(*this,
350 &FGRouteMgr::getCruiseSpeedMach,
351 &FGRouteMgr::setCruiseSpeedMach));
352
353 totalDistance = fgGetNode(RM "total-distance", true);
354 totalDistance->setDoubleValue(0.0);
355 distanceToGo = fgGetNode(RM "distance-remaining-nm", true);
356 distanceToGo->setDoubleValue(0.0);
357
358 ete = fgGetNode(RM "ete", true);
359 ete->setDoubleValue(0.0);
360
361 elapsedFlightTime = fgGetNode(RM "flight-time", true);
362 elapsedFlightTime->setDoubleValue(0.0);
363
364 active = fgGetNode(RM "active", true);
365 active->setBoolValue(false);
366
367 airborne = fgGetNode(RM "airborne", true);
368 airborne->setBoolValue(false);
369
370 _edited = fgGetNode(RM "signals/edited", true);
371 _flightplanChanged = fgGetNode(RM "signals/flightplan-changed", true);
372 _isRoute = fgGetNode(RM "is-route", true);
373
374 _currentWpt = fgGetNode(RM "current-wp", true);
375 _currentWpt->setAttribute(SGPropertyNode::LISTENER_SAFE, true);
376 _currentWpt->tie(SGRawValueMethods<FGRouteMgr, int>
377 (*this, &FGRouteMgr::currentIndex, &FGRouteMgr::jumpToIndex));
378
379 wp0 = fgGetNode(RM "wp", 0, true);
380 wp0->getChild("id", 0, true);
381 wp0->getChild("dist", 0, true);
382 wp0->getChild("eta", 0, true);
383 wp0->getChild("eta-seconds", 0, true);
384 wp0->getChild("bearing-deg", 0, true);
385
386 wp1 = fgGetNode(RM "wp", 1, true);
387 wp1->getChild("id", 0, true);
388 wp1->getChild("dist", 0, true);
389 wp1->getChild("eta", 0, true);
390 wp1->getChild("eta-seconds", 0, true);
391
392 wpn = fgGetNode(RM "wp-last", 0, true);
393 wpn->getChild("dist", 0, true);
394 wpn->getChild("eta", 0, true);
395 wpn->getChild("eta-seconds", 0, true);
396
397 _pathNode = fgGetNode(RM "file-path", 0, true);
398 }
399
400
401 void FGRouteMgr::postinit()
402 {
403 setFlightPlan(FlightPlan::create());
404 _plan->setIdent("default-flightplan");
405
406 SGPath path = SGPath::fromUtf8(_pathNode->getStringValue());
407 if (!path.isNull()) {
408 SG_LOG(SG_AUTOPILOT, SG_INFO, "loading flight-plan from: " << path);
409 loadRoute(path);
410 }
411
412 _isRoute->setBoolValue(_plan->isRoute());
413
414 // this code only matters for the --wp option now - perhaps the option
415 // should be deprecated in favour of an explicit flight-plan file?
416 // then the global initial waypoint list could die.
417 string_list *waypoints = globals->get_initial_waypoints();
418 if (waypoints) {
419 for (const auto& wpStr : *waypoints) {
420 WayptRef w = waypointFromString(wpStr, -1);
421 if (w) {
422 _plan->insertWayptAtIndex(w, -1);
423 } else {
424 SG_LOG(SG_AUTOPILOT, SG_WARN, "Failed to create waypoint from '" << wpStr << "'");
425 }
426 }
427
428 update_mirror();
429 }
430
431 weightOnWheels = fgGetNode("/gear/gear[0]/wow", true);
432 groundSpeed = fgGetNode("/velocities/groundspeed-kt", true);
433
434 // check airbone flag agrees with presets
435 }
436
437 void FGRouteMgr::bind() { }
438 void FGRouteMgr::unbind() { }
439
440 bool FGRouteMgr::isRouteActive() const
441 {
442 return active->getBoolValue();
443 }
444
445 bool FGRouteMgr::saveRoute(const SGPath& p)
446 {
447 if (!_plan) {
448 return false;
449 }
450
451 return _plan->save(p);
452 }
453
454 bool FGRouteMgr::loadRoute(const SGPath& p)
455 {
456 FlightPlan* fp = FlightPlan::create();
457 if (!fp->load(p)) {
458 return false;
459 }
460
461 setFlightPlan(fp);
462 return true;
463 }
464
465 FlightPlanRef FGRouteMgr::flightPlan() const
466 {
467 return _plan;
468 }
469
470 void FGRouteMgr::setFlightPlan(const FlightPlanRef& plan)
471 {
472 if (plan == _plan) {
473 return;
474 }
475
476 if (_plan) {
477 _plan->removeDelegate(this);
478
479 if (isRouteActive()) {
480 _plan->finish();
481 }
482
483 active->setBoolValue(false);
484 }
485
486 _plan = plan;
487 _plan->addDelegate(this);
488 _isRoute->setBoolValue(_plan->isRoute());
489 _flightplanChanged->fireValueChanged();
490
491 // fire all the callbacks!
492 departureChanged();
493 arrivalChanged();
494 waypointsChanged();
495 currentWaypointChanged();
496 }
497
498 void FGRouteMgr::departureChanged()
499 {
500 auto gui = globals->get_subsystem<NewGUI>();
501 FGDialog* rmDlg = gui ? gui->getDialog("route-manager") : NULL;
502 if (rmDlg) {
503 rmDlg->runCallback("departure-changed");
504 }
505 }
506
507 void FGRouteMgr::arrivalChanged()
508 {
509 auto gui = globals->get_subsystem<NewGUI>();
510 FGDialog* rmDlg = gui ? gui->getDialog("route-manager") : NULL;
511 if (rmDlg) {
512 rmDlg->runCallback("arrival-changed");
513 }
514 }
515
516
517 void FGRouteMgr::update( double dt )
518 {
519 if (dt <= 0.0) {
520 return; // paused, nothing to do here
521 }
522
523 double gs = groundSpeed->getDoubleValue();
524 if (airborne->getBoolValue()) {
525 time_t now = globals->get_time_params()->get_cur_time();
526 elapsedFlightTime->setDoubleValue(difftime(now, _takeoffTime));
527
528 if (weightOnWheels->getBoolValue()) {
529 // touch down
530 destination->setIntValue("touchdown-time", now);
531 airborne->setBoolValue(false);
532 }
533 } else { // not airborne
534 if (weightOnWheels->getBoolValue() || (gs < 40)) {
535 // either taking-off or rolling-out after touchdown
536 } else {
537 airborne->setBoolValue(true);
538 _takeoffTime = globals->get_time_params()->get_cur_time(); // start the clock
539 departure->setIntValue("takeoff-time", _takeoffTime);
540 }
541 }
542
543 if (!active->getBoolValue()) {
544 return;
545 }
546
547 // basic course/distance information
548 SGGeod currentPos = globals->get_aircraft_position();
549
550 FlightPlan::Leg* leg = _plan ? _plan->currentLeg() : NULL;
551 if (!leg) {
552 return;
553 }
554
555 // use RoutePath to compute location of active WP
556 if (!_routePath) {
557 _routePath.reset(new RoutePath{_plan});
558 }
559
560 SGGeod wpPos = _routePath->positionForIndex(_plan->currentIndex());
561 double courseDeg, az2, distanceM;
562 SGGeodesy::inverse(currentPos, wpPos, courseDeg, az2, distanceM);
563
564 // update wp0 / wp1 / wp-last
565 wp0->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
566 wp0->setDoubleValue("true-bearing-deg", courseDeg);
567 courseDeg -= magvar->getDoubleValue(); // expose magnetic bearing
568 wp0->setDoubleValue("bearing-deg", courseDeg);
569 setETAPropertyFromDistance(wp0, distanceM);
570
571 double totalPathDistanceNm = _plan->totalDistanceNm();
572 double totalDistanceRemaining = distanceM * SG_METER_TO_NM; // distance to current waypoint
573
574 // total distance to go, is direct distance to wp0, plus the remaining
575 // path distance from wp0
576 totalDistanceRemaining += (totalPathDistanceNm - leg->distanceAlongRoute());
577
578 wp0->setDoubleValue("distance-along-route-nm",
579 leg->distanceAlongRoute());
580 wp0->setDoubleValue("remaining-distance-nm",
581 totalPathDistanceNm - leg->distanceAlongRoute());
582
583 FlightPlan::Leg* nextLeg = _plan->nextLeg();
584 if (nextLeg) {
585 wpPos = _routePath->positionForIndex(_plan->currentIndex() + 1);
586 SGGeodesy::inverse(currentPos, wpPos, courseDeg, az2, distanceM);
587
588 wp1->setDoubleValue("dist", distanceM * SG_METER_TO_NM);
589 wp1->setDoubleValue("true-bearing-deg", courseDeg);
590 courseDeg -= magvar->getDoubleValue(); // expose magnetic bearing
591 wp1->setDoubleValue("bearing-deg", courseDeg);
592 setETAPropertyFromDistance(wp1, distanceM);
593 wp1->setDoubleValue("distance-along-route-nm",
594 nextLeg->distanceAlongRoute());
595 wp1->setDoubleValue("remaining-distance-nm",
596 totalPathDistanceNm - nextLeg->distanceAlongRoute());
597 }
598
599 distanceToGo->setDoubleValue(totalDistanceRemaining);
600 wpn->setDoubleValue("dist", totalDistanceRemaining);
601 ete->setDoubleValue(totalDistanceRemaining / gs * 3600.0);
602 setETAPropertyFromDistance(wpn, totalDistanceRemaining);
603 }
604
605 void FGRouteMgr::clearRoute()
606 {
607 _routePath.reset();
608 if (_plan) {
609 _plan->clearLegs();
610 }
611 }
612
613 Waypt* FGRouteMgr::currentWaypt() const
614 {
615 if (_plan && _plan->currentLeg()) {
616 return _plan->currentLeg()->waypoint();
617 }
618
619 return NULL;
620 }
621
622 int FGRouteMgr::currentIndex() const
623 {
624 if (!_plan) {
625 return 0;
626 }
627
628 return _plan->currentIndex();
629 }
630
631 Waypt* FGRouteMgr::wayptAtIndex(int index) const
632 {
633 if (!_plan) {
634 throw sg_range_exception("wayptAtindex: no flightplan");
635 }
636
637 return _plan->legAtIndex(index)->waypoint();
638 }
639
640 int FGRouteMgr::numLegs() const
641 {
642 if (_plan) {
643 return _plan->numLegs();
644 }
645
646 return 0;
647 }
648
649 void FGRouteMgr::setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance)
650 {
651 double speed = groundSpeed->getDoubleValue();
652 if (speed < 1.0) {
653 aProp->setStringValue("--:--");
654 return;
655 }
656
657 char eta_str[64];
658 double eta = aDistance * SG_METER_TO_NM / speed;
659 aProp->getChild("eta-seconds")->setIntValue( eta * 3600 );
660 if ( eta >= 100.0 ) {
661 eta = 99.999; // clamp
662 }
663
664 if ( eta < (1.0/6.0) ) {
665 eta *= 60.0; // within 10 minutes, bump up to min/secs
666 }
667
668 int major = (int)eta,
669 minor = (int)((eta - (int)eta) * 60.0);
670 snprintf( eta_str, 64, "%d:%02d", major, minor );
671 aProp->getChild("eta")->setStringValue( eta_str );
672 }
673
674 void FGRouteMgr::removeLegAtIndex(int aIndex)
675 {
676 if (!_plan) {
677 return;
678 }
679
680 _plan->deleteIndex(aIndex);
681 }
682
683 void FGRouteMgr::waypointsChanged()
684 {
685 update_mirror();
686 _edited->fireValueChanged();
687 }
688
689 // mirror internal route to the property system for inspection by other subsystems
690 void FGRouteMgr::update_mirror()
691 {
692 _routePath.reset(); // wipe this so we re-compute on next update()
693 mirror->removeChildren("wp");
694 auto gui = globals->get_subsystem<NewGUI>();
695 FGDialog* rmDlg = gui ? gui->getDialog("route-manager") : NULL;
696
697 if (!_plan) {
698 mirror->setIntValue("num", 0);
699 if (rmDlg) {
700 rmDlg->updateValues();
701 }
702 return;
703 }
704
705 int num = _plan->numLegs();
706
707 for (int i = 0; i < num; i++) {
708 FlightPlan::Leg* leg = _plan->legAtIndex(i);
709 WayptRef wp = leg->waypoint();
710 SGPropertyNode *prop = mirror->getChild("wp", i, 1);
711
712 const SGGeod& pos(wp->position());
713 prop->setStringValue("id", wp->ident());
714 prop->setDoubleValue("longitude-deg", pos.getLongitudeDeg());
715 prop->setDoubleValue("latitude-deg",pos.getLatitudeDeg());
716
717 // leg course+distance
718
719 prop->setDoubleValue("leg-bearing-true-deg", leg->courseDeg());
720 prop->setDoubleValue("leg-distance-nm", leg->distanceNm());
721 prop->setDoubleValue("distance-along-route-nm", leg->distanceAlongRoute());
722
723 if (leg->altitudeRestriction() != RESTRICT_NONE) {
724 double ft = leg->altitudeFt();
725 prop->setDoubleValue("altitude-m", ft * SG_FEET_TO_METER);
726 prop->setDoubleValue("altitude-ft", ft);
727 prop->setIntValue("flight-level", static_cast<int>(ft / 1000) * 10);
728 } else {
729 prop->setDoubleValue("altitude-m", -9999.9);
730 prop->setDoubleValue("altitude-ft", -9999.9);
731 }
732
733 if (leg->speedRestriction() == SPEED_RESTRICT_MACH) {
734 prop->setDoubleValue("speed-mach", leg->speedMach());
735 } else if (leg->speedRestriction() != RESTRICT_NONE) {
736 prop->setDoubleValue("speed-kts", leg->speedKts());
737 }
738
739 if (wp->flag(WPT_ARRIVAL)) {
740 prop->setBoolValue("arrival", true);
741 }
742
743 if (wp->flag(WPT_DEPARTURE)) {
744 prop->setBoolValue("departure", true);
745 }
746
747 if (wp->flag(WPT_MISS)) {
748 prop->setBoolValue("missed-approach", true);
749 }
750
751 prop->setBoolValue("generated", wp->flag(WPT_GENERATED));
752 } // of waypoint iteration
753
754 // set number as listener attachment point
755 mirror->setIntValue("num", _plan->numLegs());
756
757 if (rmDlg) {
758 rmDlg->updateValues();
759 }
760
761 totalDistance->setDoubleValue(_plan->totalDistanceNm());
762 }
763
764 // command interface /autopilot/route-manager/input:
765 //
766 // @CLEAR ... clear route
767 // @POP ... remove first entry
768 // @DELETE3 ... delete 4th entry
769 // @INSERT2:KSFO@900 ... insert "KSFO@900" as 3rd entry
770 // KSFO@900 ... append "KSFO@900"
771 //
772 void FGRouteMgr::InputListener::valueChanged(SGPropertyNode *prop)
773 {
774 const auto input = su::uppercase(su::strip(prop->getStringValue()));
775 if (input.empty()) {
776 return;
777 }
778
779 if (input == "@CLEAR") {
780 mgr->clearRoute();
781 } else if (input == "@ACTIVATE") {
782 mgr->activate();
783 } else if (input == "@LOAD") {
784 SGPath path = SGPath::fromUtf8(mgr->_pathNode->getStringValue());
785 bool ok = mgr->loadRoute(path);
786 if (!ok) {
787 modalMessageBox("FlightGear", "Unable to load flight plan (from '" + path.utf8Str() + "')");
788 }
789 } else if (input == "@SAVE") {
790 SGPath path = SGPath::fromUtf8(mgr->_pathNode->getStringValue());
791 const SGPath authorizedPath = SGPath(path).validate(true /* write */);
792
793 if (!authorizedPath.isNull()) {
794 mgr->saveRoute(authorizedPath);
795 } else {
796 std::string msg =
797 "The route manager was asked to write the flightplan to '" +
798 path.utf8Str() + "', but this path is not authorized for writing. " +
799 "Please choose another location, for instance in the $FG_HOME/Export "
800 "folder (" +
801 (globals->get_fg_home() / "Export").utf8Str() + ").";
802
803 SG_LOG(SG_AUTOPILOT, SG_ALERT, msg);
804 modalMessageBox("FlightGear", "Unable to write to the specified file",
805 msg);
806 }
807 } else if (input == "@NEXT") {
808 mgr->jumpToIndex(mgr->currentIndex() + 1);
809 } else if (input == "@PREVIOUS") {
810 mgr->jumpToIndex(mgr->currentIndex() - 1);
811 } else if (su::starts_with(input, "@JUMP")) {
812 const int index = stoi(input.substr(5));
813 mgr->jumpToIndex(index);
814 } else if (su::starts_with(input, "@DELETE")) {
815 const int index = stoi(input.substr(7));
816 mgr->removeLegAtIndex(index);
817 } else if (su::starts_with(input, "@INSERT")) {
818 size_t pos = 0;
819 const auto arg = input.substr(7); // input without prefix
820 const int index = stoi(arg, &pos, 10);
821 if (arg.at(pos) != ':') {
822 SG_LOG(SG_AUTOPILOT, SG_WARN, "@INSERT: couldn't parse index from:'" << input << "'");
823 return;
824 }
825
826 const auto wpString = su::strip(arg.substr(pos + 1));
827 const auto newWp = mgr->waypointFromString(wpString, index);
828 mgr->flightPlan()->insertWayptAtIndex(newWp, index);
829 } else {
830 const auto newWp = mgr->waypointFromString(input, -1);
831 mgr->flightPlan()->insertWayptAtIndex(newWp, -1);
832 }
833 }
834
835 bool FGRouteMgr::activate()
836 {
837 if (!_plan) {
838 SG_LOG(SG_AUTOPILOT, SG_WARN, "::activate, no flight plan defined");
839 return false;
840 }
841
842 if (isRouteActive()) {
843 SG_LOG(SG_AUTOPILOT, SG_WARN, "duplicate route-activation, no-op");
844 return false;
845 }
846
847 _plan->activate();
848 active->setBoolValue(true);
849 SG_LOG(SG_AUTOPILOT, SG_INFO, "route-manager, activate route ok");
850 return true;
851 }
852
853 void FGRouteMgr::deactivate()
854 {
855 if (!isRouteActive()) {
856 return;
857 }
858
859 SG_LOG(SG_AUTOPILOT, SG_INFO, "deactivating flight plan");
860 active->setBoolValue(false);
861 }
862
863 void FGRouteMgr::jumpToIndex(int index)
864 {
865 if (!_plan) {
866 return;
867 }
868
869 // this method is tied() to current-wp property, but FlightPlan::setCurrentIndex
870 // will throw on invalid input, so guard against invalid values here.
871 // See Sentry FLIGHTGEAR-71
872 if ((index < -1) || (index >= _plan->numLegs())) {
873 SG_LOG(SG_AUTOPILOT, SG_WARN, "FGRouteMgr::jumpToIndex: ignoring invalid index:" << index);
874 return;
875 }
876
877 _plan->setCurrentIndex(index);
878 }
879
880 void FGRouteMgr::currentWaypointChanged()
881 {
882 Waypt* cur = currentWaypt();
883 FlightPlan::Leg* next = _plan ? _plan->nextLeg() : NULL;
884
885 wp0->getChild("id")->setStringValue(cur ? cur->ident() : "");
886 wp1->getChild("id")->setStringValue(next ? next->waypoint()->ident() : "");
887
888 _currentWpt->fireValueChanged();
889 SG_LOG(SG_AUTOPILOT, SG_INFO, "route manager, current-wp is now " << currentIndex());
890 }
891
892 std::string FGRouteMgr::getDepartureICAO() const
893 {
894 if (!_plan || !_plan->departureAirport()) {
895 return "";
896 }
897
898 return _plan->departureAirport()->ident();
899 }
900
901 std::string FGRouteMgr::getDepartureName() const
902 {
903 if (!_plan || !_plan->departureAirport()) {
904 return "";
905 }
906
907 return _plan->departureAirport()->name();
908 }
909
910 std::string FGRouteMgr::getDepartureRunway() const
911 {
912 if (_plan && _plan->departureRunway()) {
913 return _plan->departureRunway()->ident();
914 }
915
916 return "";
917 }
918
919 void FGRouteMgr::setDepartureRunway(const std::string& aIdent)
920 {
921 if (!_plan) {
922 return;
923 }
924
925 FGAirport* apt = _plan->departureAirport();
926 if (!apt || aIdent.empty()) {
927 _plan->setDeparture(apt);
928 } else if (apt->hasRunwayWithIdent(aIdent)) {
929 _plan->setDeparture(apt->getRunwayByIdent(aIdent));
930 }
931 }
932
933 void FGRouteMgr::setDepartureICAO(const std::string& aIdent)
934 {
935 if (!_plan) {
936 return;
937 }
938
939 if (aIdent.length() < 3) {
940 _plan->setDeparture((FGAirport*) nullptr);
941 } else {
942 _plan->setDeparture(FGAirport::findByIdent(aIdent));
943 }
944 }
945
946 std::string FGRouteMgr::getSID() const
947 {
948 if (_plan && _plan->sid()) {
949 return _plan->sid()->ident();
950 }
951
952 return "";
953 }
954
955 std::string FGRouteMgr::getSIDWithTransition() const
956 {
957 if (!_plan || !_plan->sid()) {
958 return {};
959 }
960
961 auto sid = _plan->sid();
962 auto trans = _plan->sidTransition();
963 if (trans) {
964 return sid->ident() + "-"s + trans->ident();
965 }
966
967 // no transition defined
968 return sid->ident();
969 }
970
971 static double headingDiffDeg(double a, double b)
972 {
973 double rawDiff = b - a;
974 SG_NORMALIZE_RANGE(rawDiff, -180.0, 180.0);
975 return rawDiff;
976 }
977
978 flightgear::SID* createDefaultSID(FGRunway* aRunway, double enrouteCourse)
979 {
980 if (!aRunway) {
981 return NULL;
982 }
983
984 double runwayElevFt = aRunway->end().getElevationFt();
985 WayptVec wpts;
986 std::ostringstream ss;
987 ss << aRunway->ident() << "-3";
988
989 SGGeod p = aRunway->pointOnCenterline(aRunway->lengthM() + (3.0 * SG_NM_TO_METER));
990 WayptRef w = new BasicWaypt(p, ss.str(), NULL);
991 w->setAltitude(runwayElevFt + 3000.0, RESTRICT_AT);
992 wpts.push_back(w);
993
994 ss.str("");
995 ss << aRunway->ident() << "-6";
996 p = aRunway->pointOnCenterline(aRunway->lengthM() + (6.0 * SG_NM_TO_METER));
997 w = new BasicWaypt(p, ss.str(), NULL);
998 w->setAltitude(runwayElevFt + 6000.0, RESTRICT_AT);
999 wpts.push_back(w);
1000
1001 if (enrouteCourse >= 0.0) {
1002 // valid enroute course
1003 int index = 3;
1004 double course = aRunway->headingDeg();
1005 double diff;
1006 while (fabs(diff = headingDiffDeg(course, enrouteCourse)) > 45.0) {
1007 // turn in the sign of the heading change 45 degrees
1008 course += copysign(45.0, diff);
1009 ss.str("");
1010 ss << "DEP-" << index++;
1011 SGGeod pos = wpts.back()->position();
1012 pos = SGGeodesy::direct(pos, course, 3.0 * SG_NM_TO_METER);
1013 w = new BasicWaypt(pos, ss.str(), NULL);
1014 wpts.push_back(w);
1015 }
1016 } else {
1017 // no enroute course, just keep runway heading
1018 ss.str("");
1019 ss << aRunway->ident() << "-9";
1020 p = aRunway->pointOnCenterline(aRunway->lengthM() + (9.0 * SG_NM_TO_METER));
1021 w = new BasicWaypt(p, ss.str(), NULL);
1022 w->setAltitude(runwayElevFt + 9000.0, RESTRICT_AT);
1023 wpts.push_back(w);
1024 }
1025
1026 for (Waypt* w : wpts) {
1027 w->setFlag(WPT_DEPARTURE);
1028 w->setFlag(WPT_GENERATED);
1029 }
1030
1031 return flightgear::SID::createTempSID("DEFAULT", aRunway, wpts);
1032 }
1033
1034 void FGRouteMgr::setSID(const std::string& aIdent)
1035 {
1036 if (!_plan) {
1037 return;
1038 }
1039
1040 FGAirport* apt = _plan->departureAirport();
1041 if (!apt || aIdent.empty() || (aIdent == kNoneString)) {
1042 _plan->setSID((flightgear::SID*)NULL);
1043 return;
1044 }
1045
1046 if (aIdent == "DEFAULT") {
1047 double enrouteCourse = -1.0;
1048 if (_plan->destinationAirport()) {
1049 enrouteCourse = SGGeodesy::courseDeg(apt->geod(), _plan->destinationAirport()->geod());
1050 }
1051
1052 _plan->setSID(createDefaultSID(_plan->departureRunway(), enrouteCourse));
1053 return;
1054 }
1055
1056 size_t hyphenPos = aIdent.find('-');
1057 if (hyphenPos != string::npos) {
1058 string sidIdent = aIdent.substr(0, hyphenPos);
1059 string transIdent = aIdent.substr(hyphenPos + 1);
1060
1061 flightgear::SID* sid = apt->findSIDWithIdent(sidIdent);
1062 Transition* trans = sid ? sid->findTransitionByName(transIdent) : NULL;
1063 _plan->setSID(trans);
1064 } else {
1065 _plan->setSID(apt->findSIDWithIdent(aIdent));
1066 }
1067 }
1068
1069 std::string FGRouteMgr::getDestinationICAO() const
1070 {
1071 if (!_plan || !_plan->destinationAirport()) {
1072 return "";
1073 }
1074
1075 return _plan->destinationAirport()->ident();
1076 }
1077
1078 std::string FGRouteMgr::getDestinationName() const
1079 {
1080 if (!_plan || !_plan->destinationAirport()) {
1081 return "";
1082 }
1083
1084 return _plan->destinationAirport()->name();
1085 }
1086
1087 void FGRouteMgr::setDestinationICAO(const std::string& aIdent)
1088 {
1089 if (!_plan) {
1090 return;
1091 }
1092
1093 if (aIdent.length() < 3) {
1094 _plan->setDestination((FGAirport*) NULL);
1095 } else {
1096 _plan->setDestination(FGAirport::findByIdent(aIdent));
1097 }
1098 }
1099
1100 std::string FGRouteMgr::getDestinationRunway() const
1101 {
1102 if (_plan && _plan->destinationRunway()) {
1103 return _plan->destinationRunway()->ident();
1104 }
1105
1106 return "";
1107 }
1108
1109 void FGRouteMgr::setDestinationRunway(const std::string& aIdent)
1110 {
1111 if (!_plan) {
1112 return;
1113 }
1114
1115 FGAirport* apt = _plan->destinationAirport();
1116 if (!apt || aIdent.empty()) {
1117 _plan->setDestination(apt);
1118 } else if (apt->hasRunwayWithIdent(aIdent)) {
1119 _plan->setDestination(apt->getRunwayByIdent(aIdent));
1120 }
1121 }
1122
1123 std::string FGRouteMgr::getApproach() const
1124 {
1125 if (_plan && _plan->approach()) {
1126 return _plan->approach()->ident();
1127 }
1128
1129 return "";
1130 }
1131
1132 std::string FGRouteMgr::getApproachWithTransition() const
1133 {
1134 if (!_plan || !_plan->approach()) {
1135 return {};
1136 }
1137
1138 auto app = _plan->approach();
1139 auto trans = _plan->approachTransition();
1140 if (trans) {
1141 return app->ident() + "-"s + trans->ident();
1142 }
1143
1144 // no transition defined
1145 return app->ident();
1146 }
1147
1148 flightgear::Approach* createDefaultApproach(FGRunway* aRunway, double aEnrouteCourse)
1149 {
1150 if (!aRunway) {
1151 return NULL;
1152 }
1153
1154 double thresholdElevFt = aRunway->threshold().getElevationFt();
1155 const double approachHeightFt = 2000.0;
1156 double glideslopeDistanceM = (approachHeightFt * SG_FEET_TO_METER) /
1157 tan(3.0 * SG_DEGREES_TO_RADIANS);
1158
1159 std::ostringstream ss;
1160 ss << aRunway->ident() << "-12";
1161 WayptVec wpts;
1162 SGGeod p = aRunway->pointOnCenterline(-12.0 * SG_NM_TO_METER);
1163 WayptRef w = new BasicWaypt(p, ss.str(), NULL);
1164 w->setAltitude(thresholdElevFt + 4000, RESTRICT_AT);
1165 wpts.push_back(w);
1166
1167 // work back form the first point on the centerline
1168
1169 if (aEnrouteCourse >= 0.0) {
1170 // valid enroute course
1171 int index = 4;
1172 double course = aRunway->headingDeg();
1173 double diff;
1174 while (fabs(diff = headingDiffDeg(aEnrouteCourse, course)) > 45.0) {
1175 // turn in the sign of the heading change 45 degrees
1176 course -= copysign(45.0, diff);
1177 ss.str("");
1178 ss << "APP-" << index++;
1179 SGGeod pos = wpts.front()->position();
1180 pos = SGGeodesy::direct(pos, course + 180.0, 3.0 * SG_NM_TO_METER);
1181 w = new BasicWaypt(pos, ss.str(), NULL);
1182 wpts.insert(wpts.begin(), w);
1183 }
1184 }
1185
1186 p = aRunway->pointOnCenterline(-8.0 * SG_NM_TO_METER);
1187 ss.str("");
1188 ss << aRunway->ident() << "-8";
1189 w = new BasicWaypt(p, ss.str(), NULL);
1190 w->setAltitude(thresholdElevFt + approachHeightFt, RESTRICT_AT);
1191 wpts.push_back(w);
1192
1193 p = aRunway->pointOnCenterline(-glideslopeDistanceM);
1194 ss.str("");
1195 ss << aRunway->ident() << "-GS";
1196 w = new BasicWaypt(p, ss.str(), NULL);
1197 w->setAltitude(thresholdElevFt + approachHeightFt, RESTRICT_AT);
1198 wpts.push_back(w);
1199
1200 for (Waypt* w : wpts) {
1201 w->setFlag(WPT_APPROACH);
1202 w->setFlag(WPT_GENERATED);
1203 }
1204
1205 return Approach::createTempApproach("DEFAULT", aRunway, wpts);
1206 }
1207
1208 void FGRouteMgr::setApproach(const std::string& aIdent)
1209 {
1210 if (!_plan) {
1211 return;
1212 }
1213
1214 FGAirport* apt = _plan->destinationAirport();
1215 if (aIdent == "DEFAULT") {
1216 double enrouteCourse = -1.0;
1217 if (_plan->departureAirport()) {
1218 enrouteCourse = SGGeodesy::courseDeg(_plan->departureAirport()->geod(), apt->geod());
1219 }
1220
1221 _plan->setApproach(createDefaultApproach(_plan->destinationRunway(), enrouteCourse));
1222 return;
1223 }
1224
1225 if (!apt || aIdent.empty() || (aIdent == kNoneString)) {
1226 _plan->setApproach(static_cast<Approach*>(nullptr));
1227 } else {
1228 _plan->setApproach(apt->findApproachWithIdent(aIdent));
1229 }
1230 }
1231
1232 std::string FGRouteMgr::getSTAR() const
1233 {
1234 if (_plan && _plan->star()) {
1235 return _plan->star()->ident();
1236 }
1237
1238 return "";
1239 }
1240
1241 std::string FGRouteMgr::getSTARWithTransition() const
1242 {
1243 if (!_plan || !_plan->star()) {
1244 return {};
1245 }
1246
1247 auto star = _plan->star();
1248 auto trans = _plan->starTransition();
1249 if (trans) {
1250 return star->ident() + "-"s + trans->ident();
1251 }
1252
1253 // no transition defined
1254 return star->ident();
1255 }
1256
1257 void FGRouteMgr::setSTAR(const std::string& aIdent)
1258 {
1259 if (!_plan) {
1260 return;
1261 }
1262
1263 FGAirport* apt = _plan->destinationAirport();
1264 if (!apt || aIdent.empty() || (aIdent == kNoneString)) {
1265 _plan->setSTAR((STAR*)NULL);
1266 return;
1267 }
1268
1269 string ident(aIdent);
1270 size_t hyphenPos = ident.find('-');
1271 if (hyphenPos != string::npos) {
1272 string starIdent = ident.substr(0, hyphenPos);
1273 string transIdent = ident.substr(hyphenPos + 1);
1274
1275 STAR* star = apt->findSTARWithIdent(starIdent);
1276 Transition* trans = star ? star->findTransitionByName(transIdent) : NULL;
1277 _plan->setSTAR(trans);
1278 } else {
1279 _plan->setSTAR(apt->findSTARWithIdent(aIdent));
1280 }
1281 }
1282
1283 WayptRef FGRouteMgr::waypointFromString(const std::string& target, int insertPosition)
1284 {
1285 return _plan->waypointFromString(target, _plan->vicinityForInsertIndex(insertPosition));
1286 }
1287
1288 double FGRouteMgr::getDepartureFieldElevation() const
1289 {
1290 if (!_plan || !_plan->departureAirport()) {
1291 return 0.0;
1292 }
1293
1294 return _plan->departureAirport()->elevation();
1295 }
1296
1297 double FGRouteMgr::getDestinationFieldElevation() const
1298 {
1299 if (!_plan || !_plan->destinationAirport()) {
1300 return 0.0;
1301 }
1302
1303 return _plan->destinationAirport()->elevation();
1304 }
1305
1306 int FGRouteMgr::getCruiseAltitudeFt() const
1307 {
1308 if (!_plan)
1309 return 0;
1310
1311 return _plan->cruiseAltitudeFt();
1312 }
1313
1314 void FGRouteMgr::setCruiseAltitudeFt(int ft)
1315 {
1316 if (!_plan)
1317 return;
1318
1319 _plan->setCruiseAltitudeFt(ft);
1320 }
1321
1322 int FGRouteMgr::getCruiseFlightLevel() const
1323 {
1324 if (!_plan)
1325 return 0;
1326
1327 return _plan->cruiseFlightLevel();
1328 }
1329
1330 void FGRouteMgr::setCruiseFlightLevel(int fl)
1331 {
1332 if (!_plan)
1333 return;
1334
1335 _plan->setCruiseFlightLevel(fl);
1336 }
1337
1338 int FGRouteMgr::getCruiseSpeedKnots() const
1339 {
1340 if (!_plan)
1341 return 0;
1342
1343 return _plan->cruiseSpeedKnots();
1344 }
1345
1346 void FGRouteMgr::setCruiseSpeedKnots(int kts)
1347 {
1348 if (!_plan)
1349 return;
1350
1351 _plan->setCruiseSpeedKnots(kts);
1352 }
1353
1354 double FGRouteMgr::getCruiseSpeedMach() const
1355 {
1356 if (!_plan)
1357 return 0.0;
1358
1359 return _plan->cruiseSpeedMach();
1360 }
1361
1362 void FGRouteMgr::setCruiseSpeedMach(double m)
1363 {
1364 if (!_plan)
1365 return;
1366
1367 _plan->setCruiseSpeedMach(m);
1368 }
1369
1370 string FGRouteMgr::getAlternate() const
1371 {
1372 if (!_plan || !_plan->alternate())
1373 return {};
1374
1375 return _plan->alternate()->ident();
1376 }
1377
1378 std::string FGRouteMgr::getAlternateName() const
1379 {
1380 if (!_plan || !_plan->alternate())
1381 return {};
1382
1383 return _plan->alternate()->name();
1384 }
1385
1386 void FGRouteMgr::setAlternate(const string &icao)
1387 {
1388 if (!_plan)
1389 return;
1390
1391 _plan->setAlternate(FGAirport::findByIdent(icao));
1392 alternate->fireValueChanged();
1393 }
1394
1395 SGPropertyNode_ptr FGRouteMgr::wayptNodeAtIndex(int index) const
1396 {
1397 if ((index < 0) || (index >= numWaypts())) {
1398 throw sg_range_exception("waypt index out of range", "FGRouteMgr::wayptAtIndex");
1399 }
1400
1401 return mirror->getChild("wp", index);
1402 }
1403
1404 bool FGRouteMgr::commandDefineUserWaypoint(const SGPropertyNode * arg, SGPropertyNode * root)
1405 {
1406 std::string ident = arg->getStringValue("ident");
1407 if (ident.empty()) {
1408 SG_LOG(SG_AUTOPILOT, SG_WARN, "missing ident defining user waypoint");
1409 return false;
1410 }
1411
1412 // check for duplicate idents
1413 FGPositioned::TypeFilter f(FGPositioned::WAYPOINT);
1414 FGPositionedList dups = FGPositioned::findAllWithIdent(ident, &f);
1415 if (!dups.empty()) {
1416 SG_LOG(SG_AUTOPILOT, SG_WARN, "defineUserWaypoint: non-unique waypoint identifier:" << ident);
1417 return false;
1418 }
1419
1420 const bool temporary = arg->getBoolValue("temporary");
1421 SGGeod pos(SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
1422 arg->getDoubleValue("latitude-deg")));
1423 const auto name = arg->getStringValue("name");
1424 FGPositioned::createWaypoint(FGPositioned::WAYPOINT, ident, pos,
1425 temporary, name);
1426 return true;
1427 }
1428
1429 bool FGRouteMgr::commandDeleteUserWaypoint(const SGPropertyNode * arg, SGPropertyNode * root)
1430 {
1431 std::string ident = arg->getStringValue("ident");
1432 if (ident.empty()) {
1433 SG_LOG(SG_AUTOPILOT, SG_WARN, "missing ident deleting user waypoint");
1434 return false;
1435 }
1436
1437 FGPositioned::TypeFilter f(FGPositioned::WAYPOINT);
1438 auto existing = FGPositioned::findFirstWithIdent(ident, &f);
1439 if (!existing) {
1440 SG_LOG(SG_AUTOPILOT, SG_WARN, "no user waypoint with ident:" << ident);
1441 return false;
1442 }
1443
1444 return FGPositioned::deleteWaypoint(existing);
1445 }
1446
1447
1448 // Register the subsystem.
1449 SGSubsystemMgr::Registrant<FGRouteMgr> registrantFGRouteMgr(
1450 SGSubsystemMgr::GENERAL,
1451 {{"gui", SGSubsystemMgr::Dependency::HARD}});