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/fix.hxx>
37 #include <Navaids/navlist.hxx>
39 #include "route_mgr.hxx"
41 #define RM "/autopilot/route-manager/"
44 FGRouteMgr::FGRouteMgr() :
50 target_altitude_ft( NULL ),
51 altitude_lock( NULL ),
61 input(fgGetNode( RM "input", true )),
62 listener(new Listener(this)),
63 mirror(fgGetNode( RM "route", true )),
66 input->setStringValue("");
67 input->addChangeListener(listener);
71 FGRouteMgr::~FGRouteMgr() {
72 input->removeChangeListener(listener);
77 void FGRouteMgr::init() {
78 lon = fgGetNode( "/position/longitude-deg", true );
79 lat = fgGetNode( "/position/latitude-deg", true );
80 alt = fgGetNode( "/position/altitude-ft", true );
82 true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
83 target_altitude_ft = fgGetNode( "/autopilot/settings/target-altitude-ft", true );
84 altitude_lock = fgGetNode( "/autopilot/locks/altitude", true );
86 wp0_id = fgGetNode( RM "wp[0]/id", true );
87 wp0_dist = fgGetNode( RM "wp[0]/dist", true );
88 wp0_eta = fgGetNode( RM "wp[0]/eta", true );
90 wp1_id = fgGetNode( RM "wp[1]/id", true );
91 wp1_dist = fgGetNode( RM "wp[1]/dist", true );
92 wp1_eta = fgGetNode( RM "wp[1]/eta", true );
94 wpn_id = fgGetNode( RM "wp-last/id", true );
95 wpn_dist = fgGetNode( RM "wp-last/dist", true );
96 wpn_eta = fgGetNode( RM "wp-last/eta", true );
103 void FGRouteMgr::postinit() {
104 string_list *waypoints = globals->get_initial_waypoints();
108 vector<string>::iterator it;
109 for (it = waypoints->begin(); it != waypoints->end(); ++it)
114 void FGRouteMgr::bind() { }
115 void FGRouteMgr::unbind() { }
118 static double get_ground_speed() {
119 // starts in ft/s so we convert to kts
120 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
122 double ft_s = cur_fdm_state->get_V_ground_speed()
123 * speedup_node->getIntValue();
124 double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
130 void FGRouteMgr::update( double dt ) {
132 double wp_course, wp_distance;
136 if ( route->size() > 0 ) {
137 SGWayPoint wp = route->get_waypoint( 0 );
138 wp.CourseAndDistance( lon->getDoubleValue(), lat->getDoubleValue(),
139 alt->getDoubleValue(), &wp_course, &wp_distance );
141 true_hdg_deg->setDoubleValue( wp_course );
142 double target_alt = wp.get_target_alt();
144 if ( !altitude_set && target_alt > -9990 ) {
145 target_altitude_ft->setDoubleValue( target_alt * SG_METER_TO_FEET );
148 if ( !near_ground() )
149 altitude_lock->setStringValue( "altitude-hold" );
152 if ( wp_distance < 200.0 ) {
154 altitude_set = false;
159 if ( route->size() > 0 ) {
160 SGWayPoint wp = route->get_waypoint( 0 );
161 // update the property tree info
163 wp0_id->setStringValue( wp.get_id().c_str() );
165 accum += wp_distance;
166 wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
168 double eta = accum * SG_METER_TO_NM / get_ground_speed();
169 if ( eta >= 100.0 ) { eta = 99.999; }
171 if ( eta < (1.0/6.0) ) {
172 // within 10 minutes, bump up to min/secs
176 minor = (int)((eta - (int)eta) * 60.0);
177 snprintf( eta_str, 128, "%d:%02d", major, minor );
178 wp0_eta->setStringValue( eta_str );
182 if ( route->size() > 1 ) {
183 SGWayPoint wp = route->get_waypoint( 1 );
185 // update the property tree info
187 wp1_id->setStringValue( wp.get_id().c_str() );
189 accum += wp.get_distance();
190 wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
192 double eta = accum * SG_METER_TO_NM / get_ground_speed();
193 if ( eta >= 100.0 ) { eta = 99.999; }
195 if ( eta < (1.0/6.0) ) {
196 // within 10 minutes, bump up to min/secs
200 minor = (int)((eta - (int)eta) * 60.0);
201 snprintf( eta_str, 128, "%d:%02d", major, minor );
202 wp1_eta->setStringValue( eta_str );
205 // summarize remaining way points
206 if ( route->size() > 2 ) {
208 for ( int i = 2; i < route->size(); ++i ) {
209 wp = route->get_waypoint( i );
210 accum += wp.get_distance();
213 // update the property tree info
215 wpn_id->setStringValue( wp.get_id().c_str() );
217 wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
219 double eta = accum * SG_METER_TO_NM / get_ground_speed();
220 if ( eta >= 100.0 ) { eta = 99.999; }
222 if ( eta < (1.0/6.0) ) {
223 // within 10 minutes, bump up to min/secs
227 minor = (int)((eta - (int)eta) * 60.0);
228 snprintf( eta_str, 128, "%d:%02d", major, minor );
229 wpn_eta->setStringValue( eta_str );
234 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
235 if ( n == 0 || !route->size() )
236 altitude_set = false;
238 route->add_waypoint( wp, n );
243 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
246 if ( route->size() > 0 ) {
248 n = route->size() - 1;
249 wp = route->get_waypoint(n);
250 route->delete_waypoint(n);
253 if ( route->size() <= 2 ) {
254 wpn_id->setStringValue( "" );
255 wpn_dist->setDoubleValue( 0.0 );
256 wpn_eta->setStringValue( "" );
259 if ( route->size() <= 1 ) {
260 wp1_id->setStringValue( "" );
261 wp1_dist->setDoubleValue( 0.0 );
262 wp1_eta->setStringValue( "" );
265 if ( route->size() <= 0 ) {
266 wp0_id->setStringValue( "" );
267 wp0_dist->setDoubleValue( 0.0 );
268 wp0_eta->setStringValue( "" );
271 if ( n == 0 && route->size() )
272 altitude_set = false;
279 bool FGRouteMgr::build() {
284 int FGRouteMgr::new_waypoint( const string& target, int n ) {
286 int type = make_waypoint( &wp, target );
289 add_waypoint( *wp, n );
292 if ( !near_ground() )
293 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
299 int FGRouteMgr::make_waypoint( SGWayPoint **wp, const string& tgt ) {
303 for (unsigned int i = 0; i < target.size(); i++)
304 if (target[i] >= 'a' && target[i] <= 'z')
305 target[i] -= 'a' - 'A';
308 double alt = -9999.0;
309 size_t pos = target.find( '@' );
310 if ( pos != string::npos ) {
311 alt = atof( target.c_str() + pos + 1 );
312 target = target.substr( 0, pos );
313 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
314 alt *= SG_FEET_TO_METER;
318 pos = target.find( ',' );
319 if ( pos != string::npos ) {
320 double lon = atof( target.substr(0, pos).c_str());
321 double lat = atof( target.c_str() + pos + 1);
323 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint lon = " << lon << ", lat = " << lat );
324 *wp = new SGWayPoint( lon, lat, alt, SGWayPoint::WGS84, target );
328 // check for airport id
329 const FGAirport *apt = fgFindAirportID( target );
331 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (airport) = " << target );
332 *wp = new SGWayPoint( apt->getLongitude(), apt->getLatitude(), alt, SGWayPoint::WGS84, target );
337 // The base lon/lat are determined by the last WP,
338 // or the current pos if the WP list is empty.
339 const int wps = this->size();
342 SGWayPoint wp = get_waypoint(wps-1);
343 lat = wp.get_target_lat();
344 lon = wp.get_target_lon();
346 lat = fgGetNode("/position/latitude-deg")->getDoubleValue();
347 lon = fgGetNode("/position/longitude-deg")->getDoubleValue();
355 if ( globals->get_fixlist()->query_and_offset( target, lon, lat, 0, f, &heading, &dist ) ) {
356 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (fix) = " << target );
357 *wp = new SGWayPoint( f->get_lon(), f->get_lat(), alt, SGWayPoint::WGS84, target );
361 // Try finding a nav matching the ID
362 lat *= SGD_DEGREES_TO_RADIANS;
363 lon *= SGD_DEGREES_TO_RADIANS;
365 SG_LOG( SG_GENERAL, SG_INFO, "Looking for nav " << target << " at " << lon << " " << lat);
367 if (FGNavRecord* nav = globals->get_navlist()->findByIdent(target.c_str(), lon, lat)) {
368 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (nav) = " << target );
369 *wp = new SGWayPoint( nav->get_lon(), nav->get_lat(), alt, SGWayPoint::WGS84, target );
378 // mirror internal route to the property system for inspection by other subsystems
379 void FGRouteMgr::update_mirror() {
380 mirror->removeChildren("wp");
381 for (int i = 0; i < route->size(); i++) {
382 SGWayPoint wp = route->get_waypoint(i);
383 SGPropertyNode *prop = mirror->getChild("wp", i, 1);
385 prop->setStringValue("id", wp.get_id().c_str());
386 prop->setStringValue("name", wp.get_name().c_str());
387 prop->setDoubleValue("longitude-deg", wp.get_target_lon());
388 prop->setDoubleValue("latitude-deg", wp.get_target_lat());
389 prop->setDoubleValue("altitude-m", wp.get_target_alt());
390 prop->setDoubleValue("altitude-ft", wp.get_target_alt() * SG_METER_TO_FEET);
392 // set number as listener attachment point
393 mirror->setIntValue("num", route->size());
397 bool FGRouteMgr::near_ground() {
398 SGPropertyNode *gear = fgGetNode( "/gear/gear/wow", false );
399 if ( !gear || gear->getType() == SGPropertyNode::NONE )
400 return fgGetBool( "/sim/presets/onground", true );
402 if ( fgGetDouble("/position/altitude-agl-ft", 300.0)
403 < fgGetDouble("/autopilot/route-manager/min-lock-altitude-agl-ft") )
406 return gear->getBoolValue();
410 // command interface /autopilot/route-manager/input:
412 // @CLEAR ... clear route
413 // @POP ... remove first entry
414 // @DELETE3 ... delete 4th entry
415 // @INSERT2:KSFO@900 ... insert "KSFO@900" as 3rd entry
416 // KSFO@900 ... append "KSFO@900"
418 void FGRouteMgr::Listener::valueChanged(SGPropertyNode *prop)
420 const char *s = prop->getStringValue();
421 if (!strcmp(s, "@CLEAR"))
423 else if (!strcmp(s, "@POP"))
424 mgr->pop_waypoint(0);
425 else if (!strncmp(s, "@DELETE", 7))
426 mgr->pop_waypoint(atoi(s + 7));
427 else if (!strncmp(s, "@INSERT", 7)) {
429 int pos = strtol(s + 7, &r, 10);
435 mgr->new_waypoint(r, pos);
437 mgr->new_waypoint(s);