]> git.mxchange.org Git - flightgear.git/blobdiff - src/Autopilot/route_mgr.cxx
Merge branch 'jmt/gpswidget'
[flightgear.git] / src / Autopilot / route_mgr.cxx
index b5ef62d729974157f9d2c25899f24160a27fa689..64263dc25749eedfc0913bef2722a6548293da71 100644 (file)
@@ -92,15 +92,18 @@ void FGRouteMgr::init() {
   magvar = fgGetNode("/environment/magnetic-variation-deg", true);
      
   departure = fgGetNode(RM "departure", true);
+  departure->tie("airport", SGRawValueMethods<FGRouteMgr, const char*>(*this, 
+    &FGRouteMgr::getDepartureICAO, &FGRouteMgr::setDepartureICAO));
+  departure->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this, 
+    &FGRouteMgr::getDepartureName, NULL));
+    
 // init departure information from current location
   SGGeod pos = SGGeod::fromDegFt(lon->getDoubleValue(), lat->getDoubleValue(), alt->getDoubleValue());
-  FGAirport* apt = FGAirport::findClosest(pos, 20.0);
-  if (apt) {
-    departure->setStringValue("airport", apt->ident().c_str());
-    FGRunway* active = apt->getActiveRunwayForUsage();
+  _departure = FGAirport::findClosest(pos, 20.0);
+  if (_departure) {
+    FGRunway* active = _departure->getActiveRunwayForUsage();
     departure->setStringValue("runway", active->ident().c_str());
   } else {
-    departure->setStringValue("airport", "");
     departure->setStringValue("runway", "");
   }
   
@@ -109,6 +112,12 @@ void FGRouteMgr::init() {
 
   destination = fgGetNode(RM "destination", true);
   destination->getChild("airport", 0, true);
+  
+  destination->tie("airport", SGRawValueMethods<FGRouteMgr, const char*>(*this, 
+    &FGRouteMgr::getDestinationICAO, &FGRouteMgr::setDestinationICAO));
+  destination->tie("name", SGRawValueMethods<FGRouteMgr, const char*>(*this, 
+    &FGRouteMgr::getDestinationName, NULL));
+    
   destination->getChild("runway", 0, true);
   destination->getChild("eta", 0, true);
   destination->getChild("touchdown-time", 0, true);
@@ -163,6 +172,7 @@ void FGRouteMgr::init() {
   wpn->getChild("eta", 0, true);
   
   _route->clear();
+  _route->set_current(0);
   update_mirror();
   
   _pathNode = fgGetNode(RM "file-path", 0, true);
@@ -276,18 +286,33 @@ void FGRouteMgr::setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDi
     aProp->setStringValue( eta_str );
 }
 
-void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
+void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n )
+{
   _route->add_waypoint( wp, n );
     
-  if (_route->current_index() > n) {
+  if ((n >= 0) && (_route->current_index() > n)) {
     _route->set_current(_route->current_index() + 1);
   }
   
+  waypointsChanged();
+}
+
+void FGRouteMgr::waypointsChanged()
+{
+  double routeDistanceNm = _route->total_distance() * SG_METER_TO_NM;
+  totalDistance->setDoubleValue(routeDistanceNm);
+  double cruiseSpeedKts = cruise->getDoubleValue("speed", 0.0);
+  if (cruiseSpeedKts > 1.0) {
+    // very very crude approximation, doesn't allow for climb / descent
+    // performance or anything else at all
+    ete->setDoubleValue(routeDistanceNm / cruiseSpeedKts * (60.0 * 60.0));
+  }
+
   update_mirror();
   _edited->fireValueChanged();
+  checkFinished();
 }
 
-
 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
   if ( _route->size() <= 0 ) {
     return SGWayPoint();
@@ -304,10 +329,7 @@ SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
   SGWayPoint wp = _route->get_waypoint(n);
   _route->delete_waypoint(n);
     
-  update_mirror();
-  _edited->fireValueChanged();
-  checkFinished();
-  
+  waypointsChanged();
   return wp;
 }
 
@@ -331,9 +353,9 @@ void FGRouteMgr::new_waypoint( const string& target, int n ) {
 SGWayPoint* FGRouteMgr::make_waypoint(const string& tgt ) {
     string target(boost::to_upper_copy(tgt));
     
-    // extract altitude
-    double alt = cruise->getDoubleValue("altitude-ft") * SG_FEET_TO_METER;
     
+    double alt = -9999.0;
+    // extract altitude
     size_t pos = target.find( '@' );
     if ( pos != string::npos ) {
         alt = atof( target.c_str() + pos + 1 );
@@ -446,6 +468,10 @@ void FGRouteMgr::update_mirror() {
 void FGRouteMgr::InputListener::valueChanged(SGPropertyNode *prop)
 {
     const char *s = prop->getStringValue();
+    if (strlen(s) == 0) {
+      return;
+    }
+    
     if (!strcmp(s, "@CLEAR"))
         mgr->init();
     else if (!strcmp(s, "@ACTIVATE"))
@@ -454,9 +480,15 @@ void FGRouteMgr::InputListener::valueChanged(SGPropertyNode *prop)
       mgr->loadRoute();
     } else if (!strcmp(s, "@SAVE")) {
       mgr->saveRoute();
-    } else if (!strcmp(s, "@POP"))
-        mgr->pop_waypoint(0);
-    else if (!strncmp(s, "@DELETE", 7))
+    } else if (!strcmp(s, "@POP")) {
+      SG_LOG(SG_AUTOPILOT, SG_WARN, "route-manager @POP command is deprecated");
+    } else if (!strcmp(s, "@NEXT")) {
+      mgr->jumpToIndex(mgr->currentWaypoint() + 1);
+    } else if (!strcmp(s, "@PREVIOUS")) {
+      mgr->jumpToIndex(mgr->currentWaypoint() - 1);
+    } else if (!strncmp(s, "@JUMP", 5)) {
+      mgr->jumpToIndex(atoi(s + 5));
+    } else if (!strncmp(s, "@DELETE", 7))
         mgr->pop_waypoint(atoi(s + 7));
     else if (!strncmp(s, "@INSERT", 7)) {
         char *r;
@@ -477,50 +509,45 @@ void FGRouteMgr::InputListener::valueChanged(SGPropertyNode *prop)
 
 bool FGRouteMgr::activate()
 {
-  const FGAirport* depApt = fgFindAirportID(departure->getStringValue("airport"));
-  if (depApt) {
+  if (isRouteActive()) {
+    SG_LOG(SG_AUTOPILOT, SG_WARN, "duplicate route-activation, no-op");
+    return false;
+  }
+
+  // only add departure waypoint if we're not airborne, so that
+  // in-air route activation doesn't confuse matters.
+  if (weightOnWheels->getBoolValue() && _departure) {
     string runwayId(departure->getStringValue("runway"));
     FGRunway* runway = NULL;
-    if (depApt->hasRunwayWithIdent(runwayId)) {
-      runway = depApt->getRunwayByIdent(runwayId);
+    if (_departure->hasRunwayWithIdent(runwayId)) {
+      runway = _departure->getRunwayByIdent(runwayId);
     } else {
       SG_LOG(SG_AUTOPILOT, SG_INFO, 
         "route-manager, departure runway not found:" << runwayId);
-      runway = depApt->getActiveRunwayForUsage();
+      runway = _departure->getActiveRunwayForUsage();
     }
     
     SGWayPoint swp(runway->threshold(), 
-      depApt->ident() + "-" + runway->ident(), runway->name());
+      _departure->ident() + "-" + runway->ident(), runway->name());
     add_waypoint(swp, 0);
   }
   
-  const FGAirport* destApt = fgFindAirportID(destination->getStringValue("airport"));
-  if (destApt) {
+  if (_destination) {
     string runwayId = (destination->getStringValue("runway"));
-    if (destApt->hasRunwayWithIdent(runwayId)) {
-      FGRunway* runway = destApt->getRunwayByIdent(runwayId);
+    if (_destination->hasRunwayWithIdent(runwayId)) {
+      FGRunway* runway = _destination->getRunwayByIdent(runwayId);
       SGWayPoint swp(runway->end(), 
-        destApt->ident() + "-" + runway->ident(), runway->name());
+        _destination->ident() + "-" + runway->ident(), runway->name());
       add_waypoint(swp);
     } else {
       // quite likely, since destination runway may not be known until enroute
       // probably want a listener on the 'destination' node to allow an enroute
       // update
-      add_waypoint(SGWayPoint(destApt->geod(), destApt->ident(), destApt->name()));
+      add_waypoint(SGWayPoint(_destination->geod(), _destination->ident(), _destination->name()));
     }
   }
 
   _route->set_current(0);
-  
-  double routeDistanceNm = _route->total_distance() * SG_METER_TO_NM;
-  totalDistance->setDoubleValue(routeDistanceNm);
-  double cruiseSpeedKts = cruise->getDoubleValue("speed", 0.0);
-  if (cruiseSpeedKts > 1.0) {
-    // very very crude approximation, doesn't allow for climb / descent
-    // performance or anything else at all
-    ete->setDoubleValue(routeDistanceNm / cruiseSpeedKts * (60.0 * 60.0));
-  }
-  
   active->setBoolValue(true);
   SG_LOG(SG_AUTOPILOT, SG_INFO, "route-manager, activate route ok");
   return true;
@@ -558,11 +585,6 @@ bool FGRouteMgr::checkFinished()
 
 void FGRouteMgr::jumpToIndex(int index)
 {
-  if (!active->getBoolValue()) {
-    SG_LOG(SG_AUTOPILOT, SG_ALERT, "trying to sequence waypoints with no active route");
-    return;
-  }
-
   if ((index < 0) || (index >= _route->size())) {
     SG_LOG(SG_AUTOPILOT, SG_ALERT, "passed invalid index (" << 
       index << ") to FGRouteMgr::jumpToIndex");
@@ -619,6 +641,16 @@ int FGRouteMgr::currentWaypoint() const
   return _route->current_index();
 }
 
+void FGRouteMgr::setWaypointTargetAltitudeFt(unsigned int index, int altFt)
+{
+  SGWayPoint wp = _route->get_waypoint(index);
+  wp.setTargetAltFt(altFt);
+  // simplest way to update a waypoint is to remove and re-add it
+  _route->delete_waypoint(index);
+  _route->add_waypoint(wp, index);
+  waypointsChanged();
+}
+
 void FGRouteMgr::saveRoute()
 {
   SGPath path(_pathNode->getStringValue());
@@ -650,21 +682,17 @@ void FGRouteMgr::loadRoute()
     }
     
     string depIdent = dep->getStringValue("airport");
-    const FGAirport* depApt = fgFindAirportID(depIdent);
-    if (!depApt) {
-      throw sg_io_exception("bad route file, unknown airport:" + depIdent);
-    }
-    
-    departure->setStringValue("runway", dep->getStringValue("runway"));
-    
+    _departure = (FGAirport*) fgFindAirportID(depIdent);
+
+        
   // destination
     SGPropertyNode* dst = routeData->getChild("destination");
     if (!dst) {
       throw sg_io_exception("malformed route file, no destination node");
     }
     
-    destination->setStringValue("airport", dst->getStringValue("airport"));
-    destination->setStringValue("runay", dst->getStringValue("runway"));
+    _destination = (FGAirport*) fgFindAirportID(dst->getStringValue("airport"));
+    destination->setStringValue("runway", dst->getStringValue("runway"));
 
   // alternate
     SGPropertyNode* alt = routeData->getChild("alternate");
@@ -681,7 +709,7 @@ void FGRouteMgr::loadRoute()
   // route nodes
     _route->clear();
     SGPropertyNode_ptr _route = routeData->getChild("route", 0);
-    SGGeod lastPos(depApt->geod());
+    SGGeod lastPos = (_departure ? _departure->geod() : SGGeod());
     
     for (int i=0; i<_route->nChildren(); ++i) {
       SGPropertyNode_ptr wp = _route->getChild("wp", i);
@@ -706,16 +734,16 @@ void FGRouteMgr::parseRouteWaypoint(SGPropertyNode* aWP)
   }
 
   SGPropertyNode_ptr altProp = aWP->getChild("altitude-ft");
-  double alt = cruise->getDoubleValue("altitude-ft") * SG_FEET_TO_METER;
+  double altM = cruise->getDoubleValue("altitude-ft") * SG_FEET_TO_METER;
   if (altProp) {
-    alt = altProp->getDoubleValue();
+    altM = altProp->getDoubleValue() * SG_FEET_TO_METER;
   }
       
   string ident(aWP->getStringValue("ident"));
   if (aWP->hasChild("longitude-deg")) {
     // explicit longitude/latitude
     SGWayPoint swp(aWP->getDoubleValue("longitude-deg"),
-      aWP->getDoubleValue("latitude-deg"), alt, 
+      aWP->getDoubleValue("latitude-deg"), altM
       SGWayPoint::WGS84, ident, aWP->getStringValue("name"));
     add_waypoint(swp);
   } else if (aWP->hasChild("navid")) {
@@ -736,7 +764,7 @@ void FGRouteMgr::parseRouteWaypoint(SGPropertyNode* aWP)
       SGGeodesy::direct(p->geod(), radialDeg, offsetNm * SG_NM_TO_METER, pos, az2);
     }
     
-    SGWayPoint swp(pos.getLongitudeDeg(), pos.getLatitudeDeg(), alt, 
+    SGWayPoint swp(pos.getLongitudeDeg(), pos.getLatitudeDeg(), altM
       SGWayPoint::WGS84, ident, "");
     add_waypoint(swp);
   } else {
@@ -746,8 +774,63 @@ void FGRouteMgr::parseRouteWaypoint(SGPropertyNode* aWP)
       throw sg_io_exception("bad route file, unknown waypoint:" + ident);
     }
     
-    SGWayPoint swp(p->longitude(), p->latitude(), alt, 
+    SGWayPoint swp(p->longitude(), p->latitude(), altM
       SGWayPoint::WGS84, p->ident(), p->name());
     add_waypoint(swp);
   }
 }
+
+const char* FGRouteMgr::getDepartureICAO() const
+{
+  if (!_departure) {
+    return "";
+  }
+  
+  return _departure->ident().c_str();
+}
+
+const char* FGRouteMgr::getDepartureName() const
+{
+  if (!_departure) {
+    return "";
+  }
+  
+  return _departure->name().c_str();
+}
+
+void FGRouteMgr::setDepartureICAO(const char* aIdent)
+{
+  if ((aIdent == NULL) || (strlen(aIdent) < 4)) {
+    _departure = NULL;
+  } else {
+    _departure = FGAirport::findByIdent(aIdent);
+  }
+}
+
+const char* FGRouteMgr::getDestinationICAO() const
+{
+  if (!_destination) {
+    return "";
+  }
+  
+  return _destination->ident().c_str();
+}
+
+const char* FGRouteMgr::getDestinationName() const
+{
+  if (!_destination) {
+    return "";
+  }
+  
+  return _destination->name().c_str();
+}
+
+void FGRouteMgr::setDestinationICAO(const char* aIdent)
+{
+  if ((aIdent == NULL) || (strlen(aIdent) < 4)) {
+    _destination = NULL;
+  } else {
+    _destination = FGAirport::findByIdent(aIdent);
+  }
+}
+