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.
30 #include <simgear/compiler.h>
32 #include <Airports/simple.hxx>
33 #include <FDM/flight.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Navaids/fixlist.hxx>
36 #include <Navaids/navlist.hxx>
38 #include "route_mgr.hxx"
40 #define RM "/autopilot/route-manager/"
43 FGRouteMgr::FGRouteMgr() :
49 target_altitude_ft( NULL ),
50 altitude_lock( NULL ),
60 input(fgGetNode( RM "input", true )),
61 listener(new Listener(this)),
62 mirror(fgGetNode( RM "route", true )),
65 input->setStringValue("");
66 input->addChangeListener(listener);
70 FGRouteMgr::~FGRouteMgr() {
71 input->removeChangeListener(listener);
76 void FGRouteMgr::init() {
77 lon = fgGetNode( "/position/longitude-deg", true );
78 lat = fgGetNode( "/position/latitude-deg", true );
79 alt = fgGetNode( "/position/altitude-ft", true );
81 true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
82 target_altitude_ft = fgGetNode( "/autopilot/settings/target-altitude-ft", true );
83 altitude_lock = fgGetNode( "/autopilot/locks/altitude", true );
85 wp0_id = fgGetNode( RM "wp[0]/id", true );
86 wp0_dist = fgGetNode( RM "wp[0]/dist", true );
87 wp0_eta = fgGetNode( RM "wp[0]/eta", true );
89 wp1_id = fgGetNode( RM "wp[1]/id", true );
90 wp1_dist = fgGetNode( RM "wp[1]/dist", true );
91 wp1_eta = fgGetNode( RM "wp[1]/eta", true );
93 wpn_id = fgGetNode( RM "wp-last/id", true );
94 wpn_dist = fgGetNode( RM "wp-last/dist", true );
95 wpn_eta = fgGetNode( RM "wp-last/eta", true );
102 void FGRouteMgr::postinit() {
103 string_list *waypoints = globals->get_initial_waypoints();
107 vector<string>::iterator it;
108 for (it = waypoints->begin(); it != waypoints->end(); ++it)
113 void FGRouteMgr::bind() { }
114 void FGRouteMgr::unbind() { }
117 static double get_ground_speed() {
118 // starts in ft/s so we convert to kts
119 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
121 double ft_s = cur_fdm_state->get_V_ground_speed()
122 * speedup_node->getIntValue();
123 double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
129 void FGRouteMgr::update( double dt ) {
131 double wp_course, wp_distance;
135 if ( route->size() > 0 ) {
136 SGWayPoint wp = route->get_waypoint( 0 );
137 wp.CourseAndDistance( lon->getDoubleValue(), lat->getDoubleValue(),
138 alt->getDoubleValue(), &wp_course, &wp_distance );
140 true_hdg_deg->setDoubleValue( wp_course );
141 double target_alt = wp.get_target_alt();
143 if ( !altitude_set && target_alt > -9990 ) {
144 target_altitude_ft->setDoubleValue( target_alt * SG_METER_TO_FEET );
147 if ( !near_ground() )
148 altitude_lock->setStringValue( "altitude-hold" );
151 if ( wp_distance < 200.0 ) {
153 altitude_set = false;
158 if ( route->size() > 0 ) {
159 SGWayPoint wp = route->get_waypoint( 0 );
160 // update the property tree info
162 wp0_id->setStringValue( wp.get_id().c_str() );
164 accum += wp_distance;
165 wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
167 double eta = accum * SG_METER_TO_NM / get_ground_speed();
168 if ( eta >= 100.0 ) { eta = 99.999; }
170 if ( eta < (1.0/6.0) ) {
171 // within 10 minutes, bump up to min/secs
175 minor = (int)((eta - (int)eta) * 60.0);
176 snprintf( eta_str, 128, "%d:%02d", major, minor );
177 wp0_eta->setStringValue( eta_str );
181 if ( route->size() > 1 ) {
182 SGWayPoint wp = route->get_waypoint( 1 );
184 // update the property tree info
186 wp1_id->setStringValue( wp.get_id().c_str() );
188 accum += wp.get_distance();
189 wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
191 double eta = accum * SG_METER_TO_NM / get_ground_speed();
192 if ( eta >= 100.0 ) { eta = 99.999; }
194 if ( eta < (1.0/6.0) ) {
195 // within 10 minutes, bump up to min/secs
199 minor = (int)((eta - (int)eta) * 60.0);
200 snprintf( eta_str, 128, "%d:%02d", major, minor );
201 wp1_eta->setStringValue( eta_str );
204 // summarize remaining way points
205 if ( route->size() > 2 ) {
207 for ( int i = 2; i < route->size(); ++i ) {
208 wp = route->get_waypoint( i );
209 accum += wp.get_distance();
212 // update the property tree info
214 wpn_id->setStringValue( wp.get_id().c_str() );
216 wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
218 double eta = accum * SG_METER_TO_NM / get_ground_speed();
219 if ( eta >= 100.0 ) { eta = 99.999; }
221 if ( eta < (1.0/6.0) ) {
222 // within 10 minutes, bump up to min/secs
226 minor = (int)((eta - (int)eta) * 60.0);
227 snprintf( eta_str, 128, "%d:%02d", major, minor );
228 wpn_eta->setStringValue( eta_str );
233 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
234 if ( n == 0 || !route->size() )
235 altitude_set = false;
237 route->add_waypoint( wp, n );
242 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
245 if ( route->size() > 0 ) {
247 n = route->size() - 1;
248 wp = route->get_waypoint(n);
249 route->delete_waypoint(n);
252 if ( route->size() <= 2 ) {
253 wpn_id->setStringValue( "" );
254 wpn_dist->setDoubleValue( 0.0 );
255 wpn_eta->setStringValue( "" );
258 if ( route->size() <= 1 ) {
259 wp1_id->setStringValue( "" );
260 wp1_dist->setDoubleValue( 0.0 );
261 wp1_eta->setStringValue( "" );
264 if ( route->size() <= 0 ) {
265 wp0_id->setStringValue( "" );
266 wp0_dist->setDoubleValue( 0.0 );
267 wp0_eta->setStringValue( "" );
270 if ( n == 0 && route->size() )
271 altitude_set = false;
278 bool FGRouteMgr::build() {
283 int FGRouteMgr::new_waypoint( const string& target, int n ) {
285 int type = make_waypoint( &wp, target );
288 add_waypoint( *wp, n );
291 if ( !near_ground() )
292 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
298 int FGRouteMgr::make_waypoint( SGWayPoint **wp, const string& tgt ) {
302 for (unsigned int i = 0; i < target.size(); i++)
303 if (target[i] >= 'a' && target[i] <= 'z')
304 target[i] -= 'a' - 'A';
307 double alt = -9999.0;
308 size_t pos = target.find( '@' );
309 if ( pos != string::npos ) {
310 alt = atof( target.c_str() + pos + 1 );
311 target = target.substr( 0, pos );
312 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
313 alt *= SG_FEET_TO_METER;
317 pos = target.find( ',' );
318 if ( pos != string::npos ) {
319 double lon = atof( target.substr(0, pos).c_str());
320 double lat = atof( target.c_str() + pos + 1);
322 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint lon = " << lon << ", lat = " << lat );
323 *wp = new SGWayPoint( lon, lat, alt, SGWayPoint::WGS84, target );
327 // check for airport id
328 const FGAirport *apt = fgFindAirportID( target );
330 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (airport) = " << target );
331 *wp = new SGWayPoint( apt->getLongitude(), apt->getLatitude(), alt, SGWayPoint::WGS84, target );
336 // The base lon/lat are determined by the last WP,
337 // or the current pos if the WP list is empty.
338 const int wps = this->size();
341 SGWayPoint wp = get_waypoint(wps-1);
342 lat = wp.get_target_lat();
343 lon = wp.get_target_lon();
345 lat = fgGetNode("/position/latitude-deg")->getDoubleValue();
346 lon = fgGetNode("/position/longitude-deg")->getDoubleValue();
354 if ( globals->get_fixlist()->query_and_offset( target, lon, lat, 0, &f, &heading, &dist ) ) {
355 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (fix) = " << target );
356 *wp = new SGWayPoint( f.get_lon(), f.get_lat(), alt, SGWayPoint::WGS84, target );
360 // Try finding a nav matching the ID
361 lat *= SGD_DEGREES_TO_RADIANS;
362 lon *= SGD_DEGREES_TO_RADIANS;
364 SG_LOG( SG_GENERAL, SG_INFO, "Looking for nav " << target << " at " << lon << " " << lat);
366 if (FGNavRecord* nav = globals->get_navlist()->findByIdent(target.c_str(), lon, lat)) {
367 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (nav) = " << target );
368 *wp = new SGWayPoint( nav->get_lon(), nav->get_lat(), alt, SGWayPoint::WGS84, target );
377 // mirror internal route to the property system for inspection by other subsystems
378 void FGRouteMgr::update_mirror() {
379 mirror->removeChildren("wp");
380 for (int i = 0; i < route->size(); i++) {
381 SGWayPoint wp = route->get_waypoint(i);
382 SGPropertyNode *prop = mirror->getChild("wp", i, 1);
384 prop->setStringValue("id", wp.get_id().c_str());
385 prop->setStringValue("name", wp.get_name().c_str());
386 prop->setDoubleValue("longitude-deg", wp.get_target_lon());
387 prop->setDoubleValue("latitude-deg", wp.get_target_lat());
388 prop->setDoubleValue("altitude-m", wp.get_target_alt());
389 prop->setDoubleValue("altitude-ft", wp.get_target_alt() * SG_METER_TO_FEET);
391 // set number as listener attachment point
392 mirror->setIntValue("num", route->size());
396 bool FGRouteMgr::near_ground() {
397 SGPropertyNode *gear = fgGetNode( "/gear/gear/wow", false );
398 if ( !gear || gear->getType() == SGPropertyNode::NONE )
399 return fgGetBool( "/sim/presets/onground", true );
401 if ( fgGetDouble("/position/altitude-agl-ft", 300.0)
402 < fgGetDouble("/autopilot/route-manager/min-lock-altitude-agl-ft") )
405 return gear->getBoolValue();
409 // command interface /autopilot/route-manager/input:
411 // @CLEAR ... clear route
412 // @POP ... remove first entry
413 // @DELETE3 ... delete 4th entry
414 // @INSERT2:KSFO@900 ... insert "KSFO@900" as 3rd entry
415 // KSFO@900 ... append "KSFO@900"
417 void FGRouteMgr::Listener::valueChanged(SGPropertyNode *prop)
419 const char *s = prop->getStringValue();
420 if (!strcmp(s, "@CLEAR"))
422 else if (!strcmp(s, "@POP"))
423 mgr->pop_waypoint(0);
424 else if (!strncmp(s, "@DELETE", 7))
425 mgr->pop_waypoint(atoi(s + 7));
426 else if (!strncmp(s, "@INSERT", 7)) {
428 int pos = strtol(s + 7, &r, 10);
434 mgr->new_waypoint(r, pos);
436 mgr->new_waypoint(s);