]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/gps.cxx
Merge branch 'durk/traffic'
[flightgear.git] / src / Instrumentation / gps.cxx
index f6fdbc7dffd1b7b9251a89992578d2aebe28ed47..4477dd4eddaf23e5ce1e77b7ad6a0ddfda24694a 100644 (file)
@@ -24,6 +24,7 @@
 #include <simgear/math/sg_random.h>
 #include <simgear/sg_inlines.h>
 #include <simgear/math/sg_geodesy.hxx>
+#include <simgear/structure/exception.hxx>
 
 using std::auto_ptr;
 using std::string;
@@ -135,6 +136,10 @@ public:
       _gps->routeActivated();
     } else if (prop == _gps->_ref_navaid_id_node) {
       _gps->referenceNavaidSet(prop->getStringValue(""));
+    } else if (prop == _gps->_routeEditedSignal) {
+      _gps->routeEdited();
+    } else if (prop == _gps->_routeFinishedSignal) {
+      _gps->routeFinished();
     }
         
     _guard = false;
@@ -205,13 +210,14 @@ GPS::Config::Config() :
   _turnRate(3.0), // degrees-per-second, so 180 degree turn takes 60 seconds
   _overflightArmDistance(0.5),
   _waypointAlertTime(30.0),
-  _tuneRadio1ToRefVor(true),
+  _tuneRadio1ToRefVor(false),
   _minRunwayLengthFt(0.0),
   _requireHardSurface(true),
-  _cdiMaxDeflectionNm(-1) // default to angular mode
+  _cdiMaxDeflectionNm(-1), // default to angular mode
+  _driveAutopilot(true)
 {
   _enableTurnAnticipation = false;
-  _obsCourseSource = fgGetNode("/instrumentation/nav[0]/radials/selected-deg", true);
+  _extCourseSource = fgGetNode("/instrumentation/nav[0]/radials/selected-deg", true);
 }
 
 void GPS::Config::init(SGPropertyNode* aCfgNode)
@@ -223,48 +229,62 @@ void GPS::Config::init(SGPropertyNode* aCfgNode)
   aCfgNode->tie("min-runway-length-ft", SGRawValuePointer<double>(&_minRunwayLengthFt));
   aCfgNode->tie("hard-surface-runways-only", SGRawValuePointer<bool>(&_requireHardSurface));
   
-  aCfgNode->tie("obs-course-source", SGRawValueMethods<GPS::Config, const char*>
-    (*this, &GPS::Config::getOBSCourseSource, &GPS::Config::setOBSCourseSource));
+  aCfgNode->tie("course-source", SGRawValueMethods<GPS::Config, const char*>
+    (*this, &GPS::Config::getCourseSource, &GPS::Config::setCourseSource));
     
   aCfgNode->tie("cdi-max-deflection-nm", SGRawValuePointer<double>(&_cdiMaxDeflectionNm));
+  aCfgNode->tie("drive-autopilot", SGRawValuePointer<bool>(&_driveAutopilot));
 }
 
 const char* 
-GPS::Config::getOBSCourseSource() const
+GPS::Config::getCourseSource() const
 {
-  if (!_obsCourseSource) {
+  if (!_extCourseSource) {
     return "";
   }
   
-  return _obsCourseSource->getPath(true);
+  return _extCourseSource->getPath(true);
 }
 
 void
-GPS::Config::setOBSCourseSource(const char* aPath)
+GPS::Config::setCourseSource(const char* aPath)
 {
   SGPropertyNode* nd = fgGetNode(aPath, false);
   if (!nd) {
-    SG_LOG(SG_INSTR, SG_WARN, "couldn't find OBS course source at:" << aPath);
-    _obsCourseSource = NULL;
+    SG_LOG(SG_INSTR, SG_WARN, "couldn't find course source at:" << aPath);
+    _extCourseSource = NULL;
   }
   
-  _obsCourseSource = nd;
+  _extCourseSource = nd;
 }
 
 double 
-GPS::Config::getOBSCourse() const
+GPS::Config::getExternalCourse() const
 {
-  if (!_obsCourseSource) {
+  if (!_extCourseSource) {
     return 0.0;
   }
   
-  return _obsCourseSource->getDoubleValue();
+  return _extCourseSource->getDoubleValue();
 }
       
+void
+GPS::Config::setExternalCourse(double aCourseDeg)
+{
+  if (!_extCourseSource) {
+    return;
+  }
+
+  _extCourseSource->setDoubleValue(aCourseDeg);
+}      
+      
 ////////////////////////////////////////////////////////////////////////////
 
 GPS::GPS ( SGPropertyNode *node) : 
-  _last_valid(false),
+  _selectedCourse(0.0),
+  _dataValid(false),
+  _lastPosValid(false),
+  _mode("init"),
   _name(node->getStringValue("name", "gps")),
   _num(node->getIntValue("number", 0)),
   _computeTurnData(false),
@@ -318,7 +338,6 @@ GPS::init ()
   _magnetic_bug_error_node = node->getChild("magnetic-bug-error-deg", 0, true);
 
 // command system    
-  _mode = "obs";
   node->tie("mode", SGRawValueMethods<GPS, const char*>(*this, &GPS::getMode, NULL));
   node->tie("command", SGRawValueMethods<GPS, const char*>(*this, &GPS::getCommand, &GPS::setCommand));
     
@@ -374,7 +393,9 @@ GPS::init ()
     (*this, &GPS::getWP1CourseErrorNm, NULL));
   wp1_node->tie("to-flag", SGRawValueMethods<GPS, bool>
     (*this, &GPS::getWP1ToFlag, NULL));
-  
+  wp1_node->tie("from-flag", SGRawValueMethods<GPS, bool>
+    (*this, &GPS::getWP1FromFlag, NULL));
+    
   _tracking_bug_node = node->getChild("tracking-bug", 0, true);
          
 // leg properties (only valid in DTO/LEG modes, not OBS)
@@ -398,21 +419,19 @@ GPS::init ()
   // should these move to the route manager?
   _routeDistanceNm = node->getChild("route-distance-nm", 0, true);
   _routeETE = node->getChild("ETE", 0, true);
-
-  // disable auto-sequencing in the route manager; we'll deal with it
-  // ourselves using turn anticipation
-  SGPropertyNode_ptr autoSeq = fgGetNode("/autopilot/route-manager/auto-sequence", true);
-  autoSeq->setBoolValue(false);
-
+  _routeEditedSignal = fgGetNode("/autopilot/route-manager/signals/edited", true);
+  _routeFinishedSignal = fgGetNode("/autopilot/route-manager/signals/finished", true);
+  
 // add listener to various things
   _listener = new GPSListener(this);
   _route_current_wp_node = fgGetNode("/autopilot/route-manager/current-wp", true);
   _route_current_wp_node->addChangeListener(_listener);
   _route_active_node = fgGetNode("/autopilot/route-manager/active", true);
   _route_active_node->addChangeListener(_listener);
-
   _ref_navaid_id_node->addChangeListener(_listener);
-        
+  _routeEditedSignal->addChangeListener(_listener);
+  _routeFinishedSignal->addChangeListener(_listener);
+  
 // navradio slaving properties  
   node->tie("cdi-deflection", SGRawValueMethods<GPS,double>
     (*this, &GPS::getCDIDeflection));
@@ -420,23 +439,39 @@ GPS::init ()
   SGPropertyNode* toFlag = node->getChild("to-flag", 0, true);
   toFlag->alias(wp1_node->getChild("to-flag"));
   
-  _fromFlagNode = node->getChild("from-flag", 0, true);
+  SGPropertyNode* fromFlag = node->getChild("from-flag", 0, true);
+  fromFlag->alias(wp1_node->getChild("from-flag"));
+    
+        
+// autopilot drive properties
+  _apTrueHeading = fgGetNode("/autopilot/settings/true-heading-deg",true);
+  _apTargetAltitudeFt = fgGetNode("/autopilot/settings/target-altitude-ft", true);
+  _apAltitudeLock = fgGetNode("/autopilot/locks/altitude", true);
+  
+// realism prop[s]
+  _realismSimpleGps = fgGetNode("/sim/realism/simple-gps", true);
+  if (!_realismSimpleGps->hasValue()) {
+    _realismSimpleGps->setBoolValue(true);
+  }
   
   // last thing, add the deprecated prop watcher
   new DeprecatedPropListener(node);
+  
+  clearOutput();
 }
 
 void
 GPS::clearOutput()
 {
-  _last_valid = false;
+  _dataValid = false;
   _last_speed_kts = 0.0;
   _last_pos = SGGeod();
+  _lastPosValid = false;
   _indicated_pos = SGGeod();
   _last_vertical_speed = 0.0;
   _last_true_track = 0.0;
   
-  _raim_node->setDoubleValue(false);
+  _raim_node->setDoubleValue(0.0);
   _indicated_pos = SGGeod();
   _wp1DistanceM = 0.0;
   _wp1TrueBearing = 0.0;
@@ -446,19 +481,19 @@ GPS::clearOutput()
   _tracking_bug_node->setDoubleValue(0);
   _true_bug_error_node->setDoubleValue(0);
   _magnetic_bug_error_node->setDoubleValue(0);
-  
-  _fromFlagNode->setBoolValue(false);
 }
 
 void
 GPS::update (double delta_time_sec)
 {
-  // If it's off, don't bother.
-  if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
-    clearOutput();
-    return;
+  if (!_realismSimpleGps->getBoolValue()) {
+    // If it's off, don't bother.
+    if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
+      clearOutput();
+      return;
+    }
   }
-
+  
   if (delta_time_sec <= 0.0) {
     return; // paused, don't bother
   }    
@@ -504,49 +539,58 @@ GPS::update (double delta_time_sec)
     printf("%f %f \n", error_length, error_angle);
 
 */
-    _raim_node->setBoolValue(true);
-    _indicated_pos = _position.get();
+  _raim_node->setDoubleValue(1.0);
+  _indicated_pos = _position.get();
+  updateBasicData(delta_time_sec);
 
-    if (_last_valid) {
-      updateWithValid(delta_time_sec);
+  if (_dataValid) {
+    if (_mode == "obs") {
+      _selectedCourse = _config.getExternalCourse();
     } else {
-      _last_valid = true;
-        
-        if (_route_active_node->getBoolValue()) {
-          // GPS init with active route
-          SG_LOG(SG_INSTR, SG_INFO, "GPS init with active route");
-          _listener->setGuard(true);
-          routeActivated();
-          routeManagerSequenced();
-          _listener->setGuard(false);
-        }
+      updateTurn();
     }
-
-    _last_pos = _indicated_pos;
-}
-
-void
-GPS::updateWithValid(double dt)
-{
-  assert(_last_valid);
-    
-  updateBasicData(dt);
-  
-  if (_mode == "obs") {
-    _selectedCourse = _config.getOBSCourse();
-  } else {
-    updateTurn();
+      
+    updateWaypoints();
+    updateTrackingBug();
+    updateReferenceNavaid(delta_time_sec);
+    updateRouteData();
+    driveAutopilot();
   }
+  
+  if (_dataValid && (_mode == "init")) {
+    // allow a realistic delay in the future, here
+    SG_LOG(SG_INSTR, SG_INFO, "GPS initialisation complete");
+    
+    _selectedCourse = _config.getExternalCourse();
     
-  updateWaypoints();
-  updateTrackingBug();
-  updateReferenceNavaid(dt);
-  updateRouteData();
+    if (_route_active_node->getBoolValue()) {
+      // GPS init with active route
+      SG_LOG(SG_INSTR, SG_INFO, "GPS init with active route");
+      selectLegMode();
+    } else {
+      // initialise in OBS mode, with waypt set to the nearest airport.
+      // keep in mind at this point, _dataValid is not set
+    
+      auto_ptr<FGPositioned::Filter> f(createFilter(FGPositioned::AIRPORT));
+      FGPositionedRef apt = FGPositioned::findClosest(_position.get(), 20.0, f.get());
+      if (apt) {
+        setScratchFromPositioned(apt, 0);
+        selectOBSMode();
+      }
+    }
+  } // of init mode check
+  
+  _last_pos = _indicated_pos;
+  _lastPosValid = true;
 }
 
 void
 GPS::updateBasicData(double dt)
 {
+  if (!_lastPosValid) {
+    return;
+  }
+  
   double distance_m;
   double track2_deg;
   SGGeodesy::inverse(_last_pos, _indicated_pos, _last_true_track, track2_deg, distance_m );
@@ -562,6 +606,11 @@ GPS::updateBasicData(double dt)
   _odometer_node->setDoubleValue(odometer + distance_m * SG_METER_TO_NM);
   odometer = _trip_odometer_node->getDoubleValue();
   _trip_odometer_node->setDoubleValue(odometer + distance_m * SG_METER_TO_NM);
+  
+  if (!_dataValid) {
+    SG_LOG(SG_INSTR, SG_INFO, "GPS setting data valid");
+    _dataValid = true;
+  }
 }
 
 void
@@ -584,8 +633,6 @@ GPS::updateWaypoints()
 {  
   double az2;
   SGGeodesy::inverse(_indicated_pos, _wp1_position, _wp1TrueBearing, az2,_wp1DistanceM);
-  bool toWp = getWP1ToFlag();
-  _fromFlagNode->setBoolValue(!toWp);
 }
 
 void GPS::updateReferenceNavaid(double dt)
@@ -679,11 +726,16 @@ void GPS::routeActivated()
 {
   if (_route_active_node->getBoolValue()) {
     SG_LOG(SG_INSTR, SG_INFO, "GPS::route activated, switching to LEG mode");
-    _mode = "leg";
-    _computeTurnData = true;
+    selectLegMode();
+    
+    // if we've already passed the current waypoint, sequence.
+    if (_dataValid && getWP1FromFlag()) {
+      SG_LOG(SG_INSTR, SG_INFO, "GPS::route activated, FROM wp1, sequencing");
+      _routeMgr->sequence();
+    }
   } else if (_mode == "leg") {
     SG_LOG(SG_INSTR, SG_INFO, "GPS::route deactivated, switching to OBS mode");
-    _mode = "obs";
+    selectOBSMode();
   }
 }
 
@@ -696,24 +748,50 @@ void GPS::routeManagerSequenced()
   
   int index = _routeMgr->currentWaypoint(),
     count = _routeMgr->size();
-  if ((index < 1) || (index >= count)) {
+  if ((index < 0) || (index >= count)) {
     SG_LOG(SG_INSTR, SG_ALERT, "GPS: malformed route, index=" << index);
     return;
   }
   
   SG_LOG(SG_INSTR, SG_INFO, "GPS waypoint index is now " << index);
-  SGWayPoint wp0(_routeMgr->get_waypoint(index - 1));
-  SGWayPoint wp1(_routeMgr->get_waypoint(index));
-    
-  _wp0Ident = wp0.get_id();
-  _wp0Name = wp0.get_name();
-  _wp0_position = wp0.get_target();
+  
+  if (index > 0) {
+    SGWayPoint wp0(_routeMgr->get_waypoint(index - 1));
+    _wp0Ident = wp0.get_id();
+    _wp0Name = wp0.get_name();
+    _wp0_position = wp0.get_target();
 
+  }
+  
+  SGWayPoint wp1(_routeMgr->get_waypoint(index));
   _wp1Ident = wp1.get_id();
   _wp1Name = wp1.get_name();
   _wp1_position = wp1.get_target();
 
   _selectedCourse = getLegMagCourse();
+  wp1Changed();
+}
+
+void GPS::routeEdited()
+{
+  if (_mode != "leg") {
+    return;
+  }
+  
+  SG_LOG(SG_INSTR, SG_INFO, "GPS route edited while in LEG mode, updating waypoints");
+  routeManagerSequenced();
+}
+
+void GPS::routeFinished()
+{
+  if (_mode != "leg") {
+    return;
+  }
+  
+  SG_LOG(SG_INSTR, SG_INFO, "GPS route finished, reverting to OBS");
+  _mode = "obs";
+  _wp0_position = _indicated_pos;
+  wp1Changed();
 }
 
 void GPS::updateTurn()
@@ -924,12 +1002,39 @@ void GPS::updateRouteData()
   }
 }
 
+void GPS::driveAutopilot()
+{
+  if (!_config.driveAutopilot() || !_realismSimpleGps->getBoolValue()) {
+    return;
+  }
+  // FIXME: we want to set desired track, not heading, here
+  _apTrueHeading->setDoubleValue(getWP1Bearing());
+}
+
+void GPS::wp1Changed()
+{
+  // update external HSI/CDI/NavDisplay/PFD/etc
+  _config.setExternalCourse(getLegMagCourse());
+
+  if (!_config.driveAutopilot()) {
+    return;
+  }
+  
+  double altFt = _wp1_position.getElevationFt();
+  if (altFt < -9990.0) {
+    _apTargetAltitudeFt->setDoubleValue(0.0);
+  } else {
+    _apTargetAltitudeFt->setDoubleValue(altFt);
+  }
+}
+
 /////////////////////////////////////////////////////////////////////////////
 // property getter/setters
 
 double GPS::getLegDistance() const
 {
-  if (!_last_valid || (_mode == "obs")) {
+  if (!_dataValid || (_mode == "obs")) {
     return -1;
   }
   
@@ -938,7 +1043,7 @@ double GPS::getLegDistance() const
 
 double GPS::getLegCourse() const
 {
-  if (!_last_valid || (_mode == "obs")) {
+  if (!_dataValid) {
     return -9999.0;
   }
   
@@ -947,7 +1052,7 @@ double GPS::getLegCourse() const
 
 double GPS::getLegMagCourse() const
 {
-  if (!_last_valid || (_mode == "obs")) {
+  if (!_dataValid) {
     return 0.0;
   }
   
@@ -958,7 +1063,7 @@ double GPS::getLegMagCourse() const
 
 double GPS::getAltDistanceRatio() const
 {
-  if (!_last_valid || (_mode == "obs")) {
+  if (!_dataValid || (_mode == "obs")) {
     return 0.0;
   }
   
@@ -973,7 +1078,7 @@ double GPS::getAltDistanceRatio() const
 
 double GPS::getMagTrack() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return 0.0;
   }
   
@@ -984,7 +1089,7 @@ double GPS::getMagTrack() const
 
 double GPS::getCDIDeflection() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return 0.0;
   }
   
@@ -1004,7 +1109,7 @@ double GPS::getCDIDeflection() const
 
 const char* GPS::getWP0Ident() const
 {
-  if (!_last_valid || (_mode != "leg")) {
+  if (!_dataValid || (_mode != "leg")) {
     return "";
   }
   
@@ -1013,7 +1118,7 @@ const char* GPS::getWP0Ident() const
 
 const char* GPS::getWP0Name() const
 {
-  if (!_last_valid || (_mode != "leg")) {
+  if (!_dataValid || (_mode != "leg")) {
     return "";
   }
   
@@ -1022,7 +1127,7 @@ const char* GPS::getWP0Name() const
 
 const char* GPS::getWP1Ident() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return "";
   }
   
@@ -1031,7 +1136,7 @@ const char* GPS::getWP1Ident() const
 
 const char* GPS::getWP1Name() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return "";
   }
 
@@ -1040,7 +1145,7 @@ const char* GPS::getWP1Name() const
 
 double GPS::getWP1Distance() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return -1.0;
   }
   
@@ -1049,7 +1154,7 @@ double GPS::getWP1Distance() const
 
 double GPS::getWP1TTW() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return -1.0;
   }
   
@@ -1062,7 +1167,7 @@ double GPS::getWP1TTW() const
 
 const char* GPS::getWP1TTWString() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return "";
   }
   
@@ -1071,7 +1176,7 @@ const char* GPS::getWP1TTWString() const
 
 double GPS::getWP1Bearing() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return -9999.0;
   }
   
@@ -1080,7 +1185,7 @@ double GPS::getWP1Bearing() const
 
 double GPS::getWP1MagBearing() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return -9999.0;
   }
 
@@ -1089,7 +1194,7 @@ double GPS::getWP1MagBearing() const
 
 double GPS::getWP1CourseDeviation() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return 0.0;
   }
   
@@ -1108,7 +1213,7 @@ double GPS::getWP1CourseDeviation() const
 
 double GPS::getWP1CourseErrorNm() const
 {
-  if (!_last_valid) {
+  if (!_dataValid) {
     return 0.0;
   }
   
@@ -1119,8 +1224,8 @@ double GPS::getWP1CourseErrorNm() const
 
 bool GPS::getWP1ToFlag() const
 {
-  if (!_last_valid) {
-    return 0.0;
+  if (!_dataValid) {
+    return false;
   }
   
   double dev = getWP1MagBearing() - _selectedCourse;
@@ -1129,6 +1234,15 @@ bool GPS::getWP1ToFlag() const
   return (fabs(dev) < 90.0);
 }
 
+bool GPS::getWP1FromFlag() const
+{
+  if (!_dataValid) {
+    return false;
+  }
+  
+  return !getWP1ToFlag();
+}
+
 double GPS::getScratchDistance() const
 {
   if (!_scratchValid) {
@@ -1187,8 +1301,40 @@ void GPS::setCommand(const char* aCmd)
     previousResult();
   } else if (!strcmp(aCmd, "define-user-wpt")) {
     defineWaypoint();
+  } else if (!strcmp(aCmd, "route-insert-before")) {
+    int index = _scratchNode->getIntValue("index");
+    if (index < 0 || (_routeMgr->size() == 0)) {
+      index = _routeMgr->size();
+    } else if (index >= _routeMgr->size()) {
+      SG_LOG(SG_INSTR, SG_WARN, "GPS:route-insert-before, bad index:" << index);
+      return;
+    }
+    
+    insertWaypointAtIndex(index);
+  } else if (!strcmp(aCmd, "route-insert-after")) {
+    int index = _scratchNode->getIntValue("index");
+    if (index < 0 || (_routeMgr->size() == 0)) {
+      index = _routeMgr->size();
+    } else if (index >= _routeMgr->size()) {
+      SG_LOG(SG_INSTR, SG_WARN, "GPS:route-insert-after, bad index:" << index);
+      return;
+    } else {
+      ++index; 
+    }
+  
+    insertWaypointAtIndex(index);
+  } else if (!strcmp(aCmd, "route-delete")) {
+    int index = _scratchNode->getIntValue("index");
+    if (index < 0) {
+      index = _routeMgr->size();
+    } else if (index >= _routeMgr->size()) {
+      SG_LOG(SG_INSTR, SG_WARN, "GPS:route-delete, bad index:" << index);
+      return;
+    }
+    
+    removeWaypointAtIndex(index);
   } else {
-    SG_LOG(SG_INSTR, SG_WARN, "GPS:unrecognzied command:" << aCmd);
+    SG_LOG(SG_INSTR, SG_WARN, "GPS:unrecognized command:" << aCmd);
   }
 }
 
@@ -1225,6 +1371,8 @@ void GPS::directTo()
   _mode = "dto";
   _selectedCourse = getLegMagCourse();
   clearScratch();
+  
+  wp1Changed();
 }
 
 void GPS::loadRouteWaypoint()
@@ -1344,6 +1492,12 @@ void GPS::search()
   string sty(_scratchNode->getStringValue("type"));
   _searchType = FGPositioned::typeFromName(sty);
   _searchQuery = _scratchNode->getStringValue("query");
+  if (_searchQuery.empty()) {
+    SG_LOG(SG_INSTR, SG_WARN, "empty GPS search query");
+    clearScratch();
+    return;
+  }
+  
   _searchExact = _scratchNode->getBoolValue("exact", true);
   _searchOrderByRange = _scratchNode->getBoolValue("order-by-distance", true);
   _searchResultIndex = 0;
@@ -1494,6 +1648,7 @@ void GPS::selectOBSMode()
   _wp1Name = _scratchNode->getStringValue("name");
   _wp1_position = _scratchPos;
   _wp0_position = _indicated_pos;
+  wp1Changed();
 }
 
 void GPS::selectLegMode()
@@ -1510,6 +1665,11 @@ void GPS::selectLegMode()
   SG_LOG(SG_INSTR, SG_INFO, "GPS switching to LEG mode");
   _mode = "leg";
   
+  // depending on the situation, this will either get over-written 
+  // in routeManagerSequenced or not; either way it does no harm to
+  // set it here.
+  _wp0_position = _indicated_pos;
+
   // not really sequenced, but does all the work of updating wp0/1
   routeManagerSequenced();
 }
@@ -1576,4 +1736,31 @@ void GPS::defineWaypoint()
   setScratchFromPositioned(wpt.get(), -1);
 }
 
+void GPS::insertWaypointAtIndex(int aIndex)
+{
+  // note we do allow index = routeMgr->size(), that's an append
+  if ((aIndex < 0) || (aIndex > _routeMgr->size())) {
+    throw sg_range_exception("GPS::insertWaypointAtIndex: index out of bounds");
+  }
+  
+  if (!isScratchPositionValid()) {
+    SG_LOG(SG_INSTR, SG_WARN, "GPS:insertWaypointAtIndex: invalid lat/lon");
+    return;
+  }
+  
+  string ident = _scratchNode->getStringValue("ident");
+  string name = _scratchNode->getStringValue("name");
+  
+  _routeMgr->add_waypoint(SGWayPoint(_scratchPos, ident, name), aIndex);
+}
+
+void GPS::removeWaypointAtIndex(int aIndex)
+{
+  if ((aIndex < 0) || (aIndex >= _routeMgr->size())) {
+    throw sg_range_exception("GPS::removeWaypointAtIndex: index out of bounds");
+  }
+  
+  _routeMgr->pop_waypoint(aIndex);
+}
+
 // end of gps.cxx