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 <FDM/flight.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Navaids/positioned.hxx>
36 #include "route_mgr.hxx"
38 #define RM "/autopilot/route-manager/"
41 FGRouteMgr::FGRouteMgr() :
47 target_altitude_ft( NULL ),
48 altitude_lock( NULL ),
58 input(fgGetNode( RM "input", true )),
59 listener(new Listener(this)),
60 mirror(fgGetNode( RM "route", true )),
63 input->setStringValue("");
64 input->addChangeListener(listener);
68 FGRouteMgr::~FGRouteMgr() {
69 input->removeChangeListener(listener);
74 void FGRouteMgr::init() {
75 lon = fgGetNode( "/position/longitude-deg", true );
76 lat = fgGetNode( "/position/latitude-deg", true );
77 alt = fgGetNode( "/position/altitude-ft", true );
79 true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
80 target_altitude_ft = fgGetNode( "/autopilot/settings/target-altitude-ft", true );
81 altitude_lock = fgGetNode( "/autopilot/locks/altitude", true );
83 wp0_id = fgGetNode( RM "wp[0]/id", true );
84 wp0_dist = fgGetNode( RM "wp[0]/dist", true );
85 wp0_eta = fgGetNode( RM "wp[0]/eta", true );
87 wp1_id = fgGetNode( RM "wp[1]/id", true );
88 wp1_dist = fgGetNode( RM "wp[1]/dist", true );
89 wp1_eta = fgGetNode( RM "wp[1]/eta", true );
91 wpn_id = fgGetNode( RM "wp-last/id", true );
92 wpn_dist = fgGetNode( RM "wp-last/dist", true );
93 wpn_eta = fgGetNode( RM "wp-last/eta", true );
100 void FGRouteMgr::postinit() {
101 string_list *waypoints = globals->get_initial_waypoints();
105 vector<string>::iterator it;
106 for (it = waypoints->begin(); it != waypoints->end(); ++it)
111 void FGRouteMgr::bind() { }
112 void FGRouteMgr::unbind() { }
115 static double get_ground_speed() {
116 // starts in ft/s so we convert to kts
117 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
119 double ft_s = cur_fdm_state->get_V_ground_speed()
120 * speedup_node->getIntValue();
121 double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
126 void FGRouteMgr::updateTargetAltitude() {
127 if (route->size() == 0) {
128 altitude_set = false;
132 SGWayPoint wp = route->get_waypoint( 0 );
133 if (wp.get_target_alt() < -9990.0) {
134 altitude_set = false;
139 target_altitude_ft->setDoubleValue( wp.get_target_alt() * SG_METER_TO_FEET );
141 if ( !near_ground() ) {
142 // James Turner [zakalawe]: there's no explanation for this logic,
143 // it feels like the autopilot should pull the target altitude out of
144 // wp0 instead of us pushing it through here. Hmmm.
145 altitude_lock->setStringValue( "altitude-hold" );
149 void FGRouteMgr::update( double dt ) {
150 if (route->size() == 0) {
151 return; // no route set, early return
154 double wp_course, wp_distance;
157 SGWayPoint wp = route->get_waypoint( 0 );
158 wp.CourseAndDistance( lon->getDoubleValue(), lat->getDoubleValue(),
159 alt->getDoubleValue(), &wp_course, &wp_distance );
160 true_hdg_deg->setDoubleValue( wp_course );
162 if ( wp_distance < 200.0 ) {
164 if (route->size() == 0) {
165 return; // end of route, we're done for the time being
168 wp = route->get_waypoint( 0 );
171 // update the property tree info for WP0
172 wp0_id->setStringValue( wp.get_id().c_str() );
173 double accum = wp_distance;
174 wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
175 setETAPropertyFromDistance(wp0_eta, accum);
178 if ( route->size() > 1 ) {
179 SGWayPoint wp = route->get_waypoint( 1 );
181 // update the property tree info
182 wp1_id->setStringValue( wp.get_id().c_str() );
183 accum += wp.get_distance();
184 wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
185 setETAPropertyFromDistance(wp1_eta, accum);
188 // summarize remaining way points
189 if ( route->size() > 2 ) {
191 for ( int i = 2; i < route->size(); ++i ) {
192 wp = route->get_waypoint( i );
193 accum += wp.get_distance();
196 // update the property tree info
197 wpn_id->setStringValue( wp.get_id().c_str() );
198 wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
199 setETAPropertyFromDistance(wpn_eta, accum);
203 void FGRouteMgr::setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance) {
205 double eta = aDistance * SG_METER_TO_NM / get_ground_speed();
206 if ( eta >= 100.0 ) {
207 eta = 99.999; // clamp
210 if ( eta < (1.0/6.0) ) {
211 // within 10 minutes, bump up to min/secs
215 int major = (int)eta,
216 minor = (int)((eta - (int)eta) * 60.0);
217 snprintf( eta_str, 64, "%d:%02d", major, minor );
218 aProp->setStringValue( eta_str );
221 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
222 route->add_waypoint( wp, n );
224 if ((n==0) || (route->size() == 1)) {
225 updateTargetAltitude();
230 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
233 if ( route->size() > 0 ) {
235 n = route->size() - 1;
236 wp = route->get_waypoint(n);
237 route->delete_waypoint(n);
240 if ( route->size() <= 2 ) {
241 wpn_id->setStringValue( "" );
242 wpn_dist->setDoubleValue( 0.0 );
243 wpn_eta->setStringValue( "" );
246 if ( route->size() <= 1 ) {
247 wp1_id->setStringValue( "" );
248 wp1_dist->setDoubleValue( 0.0 );
249 wp1_eta->setStringValue( "" );
252 if ( route->size() <= 0 ) {
253 wp0_id->setStringValue( "" );
254 wp0_dist->setDoubleValue( 0.0 );
255 wp0_eta->setStringValue( "" );
258 updateTargetAltitude();
264 bool FGRouteMgr::build() {
269 void FGRouteMgr::new_waypoint( const string& target, int n ) {
270 SGWayPoint* wp = make_waypoint( target );
275 add_waypoint( *wp, n );
278 if ( !near_ground() ) {
279 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
284 SGWayPoint* FGRouteMgr::make_waypoint(const string& tgt ) {
288 for (unsigned int i = 0; i < target.size(); i++)
289 if (target[i] >= 'a' && target[i] <= 'z')
290 target[i] -= 'a' - 'A';
293 double alt = -9999.0;
294 size_t pos = target.find( '@' );
295 if ( pos != string::npos ) {
296 alt = atof( target.c_str() + pos + 1 );
297 target = target.substr( 0, pos );
298 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
299 alt *= SG_FEET_TO_METER;
303 pos = target.find( ',' );
304 if ( pos != string::npos ) {
305 double lon = atof( target.substr(0, pos).c_str());
306 double lat = atof( target.c_str() + pos + 1);
308 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint lon = " << lon << ", lat = " << lat );
309 return new SGWayPoint( lon, lat, alt, SGWayPoint::WGS84, target );
313 if (route->size() > 0) {
314 SGWayPoint wp = get_waypoint(route->size()-1);
315 basePosition = SGGeod::fromDeg(wp.get_target_lon(), wp.get_target_lat());
317 // route is empty, use current position
318 basePosition = SGGeod::fromDeg(
319 fgGetNode("/position/longitude-deg")->getDoubleValue(),
320 fgGetNode("/position/latitude-deg")->getDoubleValue());
324 FGPositionedRef p = FGPositioned::findClosestWithIdent(target, basePosition);
326 SG_LOG( SG_GENERAL, SG_INFO, "Unable to find FGPositioned with ident:" << target);
330 return new SGWayPoint(p->longitude(), p->latitude(), alt, SGWayPoint::WGS84, target);
334 // mirror internal route to the property system for inspection by other subsystems
335 void FGRouteMgr::update_mirror() {
336 mirror->removeChildren("wp");
337 for (int i = 0; i < route->size(); i++) {
338 SGWayPoint wp = route->get_waypoint(i);
339 SGPropertyNode *prop = mirror->getChild("wp", i, 1);
341 prop->setStringValue("id", wp.get_id().c_str());
342 prop->setStringValue("name", wp.get_name().c_str());
343 prop->setDoubleValue("longitude-deg", wp.get_target_lon());
344 prop->setDoubleValue("latitude-deg", wp.get_target_lat());
345 prop->setDoubleValue("altitude-m", wp.get_target_alt());
346 prop->setDoubleValue("altitude-ft", wp.get_target_alt() * SG_METER_TO_FEET);
348 // set number as listener attachment point
349 mirror->setIntValue("num", route->size());
353 bool FGRouteMgr::near_ground() {
354 SGPropertyNode *gear = fgGetNode( "/gear/gear/wow", false );
355 if ( !gear || gear->getType() == SGPropertyNode::NONE )
356 return fgGetBool( "/sim/presets/onground", true );
358 if ( fgGetDouble("/position/altitude-agl-ft", 300.0)
359 < fgGetDouble("/autopilot/route-manager/min-lock-altitude-agl-ft") )
362 return gear->getBoolValue();
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);