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);
75 void FGRouteMgr::init() {
76 lon = fgGetNode( "/position/longitude-deg", true );
77 lat = fgGetNode( "/position/latitude-deg", true );
78 alt = fgGetNode( "/position/altitude-ft", true );
80 true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
81 target_altitude_ft = fgGetNode( "/autopilot/settings/target-altitude-ft", true );
82 altitude_lock = fgGetNode( "/autopilot/locks/altitude", true );
84 wp0_id = fgGetNode( RM "wp[0]/id", true );
85 wp0_dist = fgGetNode( RM "wp[0]/dist", true );
86 wp0_eta = fgGetNode( RM "wp[0]/eta", true );
88 wp1_id = fgGetNode( RM "wp[1]/id", true );
89 wp1_dist = fgGetNode( RM "wp[1]/dist", true );
90 wp1_eta = fgGetNode( RM "wp[1]/eta", true );
92 wpn_id = fgGetNode( RM "wp-last/id", true );
93 wpn_dist = fgGetNode( RM "wp-last/dist", true );
94 wpn_eta = fgGetNode( RM "wp-last/eta", true );
101 void FGRouteMgr::postinit() {
102 string_list *waypoints = globals->get_initial_waypoints();
106 vector<string>::iterator it;
107 for (it = waypoints->begin(); it != waypoints->end(); ++it)
112 void FGRouteMgr::bind() { }
113 void FGRouteMgr::unbind() { }
116 static double get_ground_speed() {
117 // starts in ft/s so we convert to kts
118 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
120 double ft_s = cur_fdm_state->get_V_ground_speed()
121 * speedup_node->getIntValue();
122 double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
127 void FGRouteMgr::updateTargetAltitude() {
128 if (route->size() == 0) {
129 altitude_set = false;
133 SGWayPoint wp = route->get_waypoint( 0 );
134 if (wp.get_target_alt() < -9990.0) {
135 altitude_set = false;
140 target_altitude_ft->setDoubleValue( wp.get_target_alt() * SG_METER_TO_FEET );
142 if ( !near_ground() ) {
143 // James Turner [zakalawe]: there's no explanation for this logic,
144 // it feels like the autopilot should pull the target altitude out of
145 // wp0 instead of us pushing it through here. Hmmm.
146 altitude_lock->setStringValue( "altitude-hold" );
150 void FGRouteMgr::update( double dt ) {
151 if (route->size() == 0) {
152 return; // no route set, early return
155 double wp_course, wp_distance;
158 SGWayPoint wp = route->get_waypoint( 0 );
159 wp.CourseAndDistance( lon->getDoubleValue(), lat->getDoubleValue(),
160 alt->getDoubleValue(), &wp_course, &wp_distance );
161 true_hdg_deg->setDoubleValue( wp_course );
163 if ( wp_distance < 200.0 ) {
165 if (route->size() == 0) {
166 return; // end of route, we're done for the time being
169 wp = route->get_waypoint( 0 );
172 // update the property tree info for WP0
173 wp0_id->setStringValue( wp.get_id().c_str() );
174 double accum = wp_distance;
175 wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
176 setETAPropertyFromDistance(wp0_eta, accum);
179 if ( route->size() > 1 ) {
180 SGWayPoint wp = route->get_waypoint( 1 );
182 // update the property tree info
183 wp1_id->setStringValue( wp.get_id().c_str() );
184 accum += wp.get_distance();
185 wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
186 setETAPropertyFromDistance(wp1_eta, accum);
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
198 wpn_id->setStringValue( wp.get_id().c_str() );
199 wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
200 setETAPropertyFromDistance(wpn_eta, accum);
204 void FGRouteMgr::setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance) {
206 double eta = aDistance * SG_METER_TO_NM / get_ground_speed();
207 if ( eta >= 100.0 ) {
208 eta = 99.999; // clamp
211 if ( eta < (1.0/6.0) ) {
212 // within 10 minutes, bump up to min/secs
216 int major = (int)eta,
217 minor = (int)((eta - (int)eta) * 60.0);
218 snprintf( eta_str, 64, "%d:%02d", major, minor );
219 aProp->setStringValue( eta_str );
222 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
223 route->add_waypoint( wp, n );
225 if ((n==0) || (route->size() == 1)) {
226 updateTargetAltitude();
231 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
234 if ( route->size() > 0 ) {
236 n = route->size() - 1;
237 wp = route->get_waypoint(n);
238 route->delete_waypoint(n);
241 if ( route->size() <= 2 ) {
242 wpn_id->setStringValue( "" );
243 wpn_dist->setDoubleValue( 0.0 );
244 wpn_eta->setStringValue( "" );
247 if ( route->size() <= 1 ) {
248 wp1_id->setStringValue( "" );
249 wp1_dist->setDoubleValue( 0.0 );
250 wp1_eta->setStringValue( "" );
253 if ( route->size() <= 0 ) {
254 wp0_id->setStringValue( "" );
255 wp0_dist->setDoubleValue( 0.0 );
256 wp0_eta->setStringValue( "" );
259 updateTargetAltitude();
265 bool FGRouteMgr::build() {
270 void FGRouteMgr::new_waypoint( const string& target, int n ) {
271 SGWayPoint* wp = make_waypoint( target );
276 add_waypoint( *wp, n );
279 if ( !near_ground() ) {
280 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
285 SGWayPoint* FGRouteMgr::make_waypoint(const string& tgt ) {
289 for (unsigned int i = 0; i < target.size(); i++)
290 if (target[i] >= 'a' && target[i] <= 'z')
291 target[i] -= 'a' - 'A';
294 double alt = -9999.0;
295 size_t pos = target.find( '@' );
296 if ( pos != string::npos ) {
297 alt = atof( target.c_str() + pos + 1 );
298 target = target.substr( 0, pos );
299 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
300 alt *= SG_FEET_TO_METER;
304 pos = target.find( ',' );
305 if ( pos != string::npos ) {
306 double lon = atof( target.substr(0, pos).c_str());
307 double lat = atof( target.c_str() + pos + 1);
309 SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint lon = " << lon << ", lat = " << lat );
310 return new SGWayPoint( lon, lat, alt, SGWayPoint::WGS84, target );
314 if (route->size() > 0) {
315 SGWayPoint wp = get_waypoint(route->size()-1);
316 basePosition = SGGeod::fromDeg(wp.get_target_lon(), wp.get_target_lat());
318 // route is empty, use current position
319 basePosition = SGGeod::fromDeg(
320 fgGetNode("/position/longitude-deg")->getDoubleValue(),
321 fgGetNode("/position/latitude-deg")->getDoubleValue());
325 FGPositionedRef p = FGPositioned::findClosestWithIdent(target, basePosition);
327 SG_LOG( SG_GENERAL, SG_INFO, "Unable to find FGPositioned with ident:" << target);
331 return new SGWayPoint(p->longitude(), p->latitude(), alt, SGWayPoint::WGS84, target);
335 // mirror internal route to the property system for inspection by other subsystems
336 void FGRouteMgr::update_mirror() {
337 mirror->removeChildren("wp");
338 for (int i = 0; i < route->size(); i++) {
339 SGWayPoint wp = route->get_waypoint(i);
340 SGPropertyNode *prop = mirror->getChild("wp", i, 1);
342 prop->setStringValue("id", wp.get_id().c_str());
343 prop->setStringValue("name", wp.get_name().c_str());
344 prop->setDoubleValue("longitude-deg", wp.get_target_lon());
345 prop->setDoubleValue("latitude-deg", wp.get_target_lat());
346 prop->setDoubleValue("altitude-m", wp.get_target_alt());
347 prop->setDoubleValue("altitude-ft", wp.get_target_alt() * SG_METER_TO_FEET);
349 // set number as listener attachment point
350 mirror->setIntValue("num", route->size());
354 bool FGRouteMgr::near_ground() {
355 SGPropertyNode *gear = fgGetNode( "/gear/gear/wow", false );
356 if ( !gear || gear->getType() == SGPropertyNode::NONE )
357 return fgGetBool( "/sim/presets/onground", true );
359 if ( fgGetDouble("/position/altitude-agl-ft", 300.0)
360 < fgGetDouble("/autopilot/route-manager/min-lock-altitude-agl-ft") )
363 return gear->getBoolValue();
367 // command interface /autopilot/route-manager/input:
369 // @CLEAR ... clear route
370 // @POP ... remove first entry
371 // @DELETE3 ... delete 4th entry
372 // @INSERT2:KSFO@900 ... insert "KSFO@900" as 3rd entry
373 // KSFO@900 ... append "KSFO@900"
375 void FGRouteMgr::Listener::valueChanged(SGPropertyNode *prop)
377 const char *s = prop->getStringValue();
378 if (!strcmp(s, "@CLEAR"))
380 else if (!strcmp(s, "@POP"))
381 mgr->pop_waypoint(0);
382 else if (!strncmp(s, "@DELETE", 7))
383 mgr->pop_waypoint(atoi(s + 7));
384 else if (!strncmp(s, "@INSERT", 7)) {
386 int pos = strtol(s + 7, &r, 10);
392 mgr->new_waypoint(r, pos);
394 mgr->new_waypoint(s);