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() :
58 input(fgGetNode( RM "input", true )),
59 listener(new Listener(this)),
60 mirror(fgGetNode( RM "route", true ))
62 input->setStringValue("");
63 input->addChangeListener(listener);
67 FGRouteMgr::~FGRouteMgr() {
69 input->removeChangeListener(listener);
73 void FGRouteMgr::init() {
74 lon = fgGetNode( "/position/longitude-deg", true );
75 lat = fgGetNode( "/position/latitude-deg", true );
76 alt = fgGetNode( "/position/altitude-ft", true );
78 true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
80 wp0_id = fgGetNode( RM "wp[0]/id", true );
81 wp0_dist = fgGetNode( RM "wp[0]/dist", true );
82 wp0_eta = fgGetNode( RM "wp[0]/eta", true );
84 wp1_id = fgGetNode( RM "wp[1]/id", true );
85 wp1_dist = fgGetNode( RM "wp[1]/dist", true );
86 wp1_eta = fgGetNode( RM "wp[1]/eta", true );
88 wpn_id = fgGetNode( RM "wp-last/id", true );
89 wpn_dist = fgGetNode( RM "wp-last/dist", true );
90 wpn_eta = fgGetNode( RM "wp-last/eta", true );
97 void FGRouteMgr::postinit() {
98 string_list *waypoints = globals->get_initial_waypoints();
102 vector<string>::iterator it;
103 for (it = waypoints->begin(); it != waypoints->end(); ++it)
108 void FGRouteMgr::bind() { }
109 void FGRouteMgr::unbind() { }
112 static double get_ground_speed() {
113 // starts in ft/s so we convert to kts
114 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
116 double ft_s = cur_fdm_state->get_V_ground_speed()
117 * speedup_node->getIntValue();
118 double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
124 void FGRouteMgr::update( double dt ) {
126 double wp_course, wp_distance;
130 if ( route->size() > 0 ) {
131 SGWayPoint wp = route->get_waypoint( 0 );
132 wp.CourseAndDistance( lon->getDoubleValue(), lat->getDoubleValue(),
133 alt->getDoubleValue(), &wp_course, &wp_distance );
135 true_hdg_deg->setDoubleValue( wp_course );
137 if ( wp_distance < 200.0 ) {
143 if ( route->size() > 0 ) {
144 SGWayPoint wp = route->get_waypoint( 0 );
145 // update the property tree info
147 wp0_id->setStringValue( wp.get_id().c_str() );
149 accum += wp_distance;
150 wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
152 double eta = accum * SG_METER_TO_NM / get_ground_speed();
153 if ( eta >= 100.0 ) { eta = 99.999; }
155 if ( eta < (1.0/6.0) ) {
156 // within 10 minutes, bump up to min/secs
160 minor = (int)((eta - (int)eta) * 60.0);
161 snprintf( eta_str, 128, "%d:%02d", major, minor );
162 wp0_eta->setStringValue( eta_str );
166 if ( route->size() > 1 ) {
167 SGWayPoint wp = route->get_waypoint( 1 );
169 // update the property tree info
171 wp1_id->setStringValue( wp.get_id().c_str() );
173 accum += wp.get_distance();
174 wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
176 double eta = accum * SG_METER_TO_NM / get_ground_speed();
177 if ( eta >= 100.0 ) { eta = 99.999; }
179 if ( eta < (1.0/6.0) ) {
180 // within 10 minutes, bump up to min/secs
184 minor = (int)((eta - (int)eta) * 60.0);
185 snprintf( eta_str, 128, "%d:%02d", major, minor );
186 wp1_eta->setStringValue( eta_str );
189 // summarize remaining way points
190 if ( route->size() > 2 ) {
192 for ( int i = 2; i < route->size(); ++i ) {
193 wp = route->get_waypoint( i );
194 accum += wp.get_distance();
197 // update the property tree info
199 wpn_id->setStringValue( wp.get_id().c_str() );
201 wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
203 double eta = accum * SG_METER_TO_NM / get_ground_speed();
204 if ( eta >= 100.0 ) { eta = 99.999; }
206 if ( eta < (1.0/6.0) ) {
207 // within 10 minutes, bump up to min/secs
211 minor = (int)((eta - (int)eta) * 60.0);
212 snprintf( eta_str, 128, "%d:%02d", major, minor );
213 wpn_eta->setStringValue( eta_str );
218 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
219 route->add_waypoint( wp, n );
224 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
227 if ( route->size() > 0 ) {
229 n = route->size() - 1;
230 wp = route->get_waypoint(n);
231 route->delete_waypoint(n);
234 if ( route->size() <= 2 ) {
235 wpn_id->setStringValue( "" );
236 wpn_dist->setDoubleValue( 0.0 );
237 wpn_eta->setStringValue( "" );
240 if ( route->size() <= 1 ) {
241 wp1_id->setStringValue( "" );
242 wp1_dist->setDoubleValue( 0.0 );
243 wp1_eta->setStringValue( "" );
246 if ( route->size() <= 0 ) {
247 wp0_id->setStringValue( "" );
248 wp0_dist->setDoubleValue( 0.0 );
249 wp0_eta->setStringValue( "" );
257 bool FGRouteMgr::build() {
263 int FGRouteMgr::new_waypoint( const string& Tgt_Alt, int n ) {
265 string target = Tgt_Alt;
268 for (unsigned int i = 0; i < target.size(); i++)
269 if (target[i] >= 'a' && target[i] <= 'z')
270 target[i] -= 'a' - 'A';
273 unsigned int pos = target.find( '@' );
274 if ( pos != string::npos ) {
275 alt = atof( target.c_str() + pos + 1 );
276 target = target.substr( 0, pos );
277 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
278 alt *= SG_FEET_TO_METER;
282 pos = target.find(',');
283 if ( pos != string::npos ) {
284 double lon = atof( target.substr(0, pos).c_str());
285 double lat = atof( target.c_str() + pos + 1);
287 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint lon = " << lon << ", lat = " << lat );
288 SGWayPoint wp( lon, lat, alt, SGWayPoint::WGS84, target );
289 add_waypoint( wp, n );
290 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
294 // check for airport id
295 const FGAirport *apt = fgFindAirportID( target );
297 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (airport) = " << target );
298 SGWayPoint wp( apt->getLongitude(), apt->getLatitude(), alt, SGWayPoint::WGS84, target );
299 add_waypoint( wp, n );
300 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
306 if ( globals->get_fixlist()->query( target, &f ) ) {
307 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (fix) = " << target );
308 SGWayPoint wp( f.get_lon(), f.get_lat(), alt, SGWayPoint::WGS84, target );
309 add_waypoint( wp, n );
310 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
314 // Try finding a nav matching the ID
316 // The base lon/lat are determined by the last WP,
317 // or the current pos if the WP list is empty.
318 const int wps = this->size();
321 SGWayPoint wp = get_waypoint(wps-1);
322 lat = wp.get_target_lat();
323 lon = wp.get_target_lon();
325 lat = fgGetNode("/position/latitude-deg")->getDoubleValue();
326 lon = fgGetNode("/position/longitude-deg")->getDoubleValue();
329 lat *= SGD_DEGREES_TO_RADIANS;
330 lon *= SGD_DEGREES_TO_RADIANS;
332 SG_LOG( SG_GENERAL, SG_INFO, "Looking for nav " << target << " at " << lon << " " << lat);
334 if (FGNavRecord* nav = globals->get_navlist()->findByIdent(target.c_str(), lon, lat)) {
335 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (nav) = " << target );
336 SGWayPoint wp( nav->get_lon(), nav->get_lat(), alt, SGWayPoint::WGS84, target );
337 add_waypoint( wp, n );
338 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
342 // target not identified
347 // mirror internal route to the property system for inspection by other subsystems
348 void FGRouteMgr::update_mirror() {
349 mirror->removeChildren("wp");
350 for (int i = 0; i < route->size(); i++) {
351 SGWayPoint wp = route->get_waypoint(i);
352 SGPropertyNode *prop = mirror->getChild("wp", i, 1);
354 prop->setStringValue("id", wp.get_id().c_str());
355 prop->setStringValue("name", wp.get_name().c_str());
356 prop->setDoubleValue("longitude-deg", wp.get_target_lon());
357 prop->setDoubleValue("latitude-deg", wp.get_target_lat());
358 prop->setDoubleValue("altitude-m", wp.get_target_alt());
359 prop->setDoubleValue("altitude-ft", wp.get_target_alt() * SG_METER_TO_FEET);
361 // set number as listener attachment point
362 mirror->setIntValue("num", route->size());
366 // command interface /autopilot/route-manager/input:
368 // @clear ... clear route
369 // @pop ... remove first entry
370 // @delete3 ... delete 4th entry
371 // @insert2:ksfo@900 ... insert "ksfo@900" as 3rd entry
372 // ksfo@900 ... append "ksfo@900"
374 void FGRouteMgr::Listener::valueChanged(SGPropertyNode *prop)
376 const char *s = prop->getStringValue();
377 if (!strcmp(s, "@clear"))
379 else if (!strcmp(s, "@pop"))
380 mgr->pop_waypoint(0);
381 else if (!strncmp(s, "@delete", 7))
382 mgr->pop_waypoint(atoi(s + 7));
383 else if (!strncmp(s, "@insert", 7)) {
385 int pos = strtol(s + 7, &r, 10);
391 mgr->new_waypoint(r, pos);
393 mgr->new_waypoint(s);