]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/route_mgr.cxx
only activate heading & altitude lock when in air (and even then it should
[flightgear.git] / src / Autopilot / route_mgr.cxx
1 // route_mgr.cxx - manage a route (i.e. a collection of waypoints)
2 //
3 // Written by Curtis Olson, started January 2004.
4 //            Norman Vine
5 //            Melchior FRANZ
6 //
7 // Copyright (C) 2004  Curtis L. Olson  - http://www.flightgear.org/~curt
8 //
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.
13 //
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.
18 //
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.
22 //
23 // $Id$
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <simgear/compiler.h>
31
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>
37
38 #include "route_mgr.hxx"
39
40 #define RM "/autopilot/route-manager/"
41
42
43 FGRouteMgr::FGRouteMgr() :
44     route( new SGRoute ),
45     lon( NULL ),
46     lat( NULL ),
47     alt( NULL ),
48     true_hdg_deg( NULL ),
49     target_altitude_ft( NULL ),
50     altitude_lock( NULL ),
51     wp0_id( NULL ),
52     wp0_dist( NULL ),
53     wp0_eta( NULL ),
54     wp1_id( NULL ),
55     wp1_dist( NULL ),
56     wp1_eta( NULL ),
57     wpn_id( NULL ),
58     wpn_dist( NULL ),
59     wpn_eta( NULL ),
60     input(fgGetNode( RM "input", true )),
61     listener(new Listener(this)),
62     mirror(fgGetNode( RM "route", true )),
63     altitude_set( false )
64 {
65     input->setStringValue("");
66     input->addChangeListener(listener);
67 }
68
69
70 FGRouteMgr::~FGRouteMgr() {
71     delete route;
72     input->removeChangeListener(listener);
73 }
74
75
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 );
80
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 );
84
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 );
88
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 );
92
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 );
96
97     route->clear();
98     update_mirror();
99 }
100
101
102 void FGRouteMgr::postinit() {
103     string_list *waypoints = globals->get_initial_waypoints();
104     if (!waypoints)
105         return;
106
107     vector<string>::iterator it;
108     for (it = waypoints->begin(); it != waypoints->end(); ++it)
109         new_waypoint(*it);
110 }
111
112
113 void FGRouteMgr::bind() { }
114 void FGRouteMgr::unbind() { }
115
116
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");
120
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;
124
125     return kts;
126 }
127
128
129 void FGRouteMgr::update( double dt ) {
130     double accum = 0.0;
131     double wp_course, wp_distance;
132     char eta_str[128];
133
134     // first way point
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 );
139
140         true_hdg_deg->setDoubleValue( wp_course );
141         double target_alt = wp.get_target_alt();
142
143         if ( !altitude_set && target_alt > -9990 ) {
144             target_altitude_ft->setDoubleValue( target_alt * SG_METER_TO_FEET );
145             altitude_set = true;
146
147             if ( !on_ground() )
148                 altitude_lock->setStringValue( "altitude-hold" );
149         }
150
151         if ( wp_distance < 200.0 ) {
152             pop_waypoint();
153             altitude_set = false;
154         }
155     }
156
157     // next way point
158     if ( route->size() > 0 ) {
159         SGWayPoint wp = route->get_waypoint( 0 );
160         // update the property tree info
161
162         wp0_id->setStringValue( wp.get_id().c_str() );
163
164         accum += wp_distance;
165         wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
166
167         double eta = accum * SG_METER_TO_NM / get_ground_speed();
168         if ( eta >= 100.0 ) { eta = 99.999; }
169         int major, minor;
170         if ( eta < (1.0/6.0) ) {
171             // within 10 minutes, bump up to min/secs
172             eta *= 60.0;
173         }
174         major = (int)eta;
175         minor = (int)((eta - (int)eta) * 60.0);
176         snprintf( eta_str, 128, "%d:%02d", major, minor );
177         wp0_eta->setStringValue( eta_str );
178     }
179
180     // next way point
181     if ( route->size() > 1 ) {
182         SGWayPoint wp = route->get_waypoint( 1 );
183
184         // update the property tree info
185
186         wp1_id->setStringValue( wp.get_id().c_str() );
187
188         accum += wp.get_distance();
189         wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
190
191         double eta = accum * SG_METER_TO_NM / get_ground_speed();
192         if ( eta >= 100.0 ) { eta = 99.999; }
193         int major, minor;
194         if ( eta < (1.0/6.0) ) {
195             // within 10 minutes, bump up to min/secs
196             eta *= 60.0;
197         }
198         major = (int)eta;
199         minor = (int)((eta - (int)eta) * 60.0);
200         snprintf( eta_str, 128, "%d:%02d", major, minor );
201         wp1_eta->setStringValue( eta_str );
202     }
203
204     // summarize remaining way points
205     if ( route->size() > 2 ) {
206         SGWayPoint wp;
207         for ( int i = 2; i < route->size(); ++i ) {
208             wp = route->get_waypoint( i );
209             accum += wp.get_distance();
210         }
211
212         // update the property tree info
213
214         wpn_id->setStringValue( wp.get_id().c_str() );
215
216         wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
217
218         double eta = accum * SG_METER_TO_NM / get_ground_speed();
219         if ( eta >= 100.0 ) { eta = 99.999; }
220         int major, minor;
221         if ( eta < (1.0/6.0) ) {
222             // within 10 minutes, bump up to min/secs
223             eta *= 60.0;
224         }
225         major = (int)eta;
226         minor = (int)((eta - (int)eta) * 60.0);
227         snprintf( eta_str, 128, "%d:%02d", major, minor );
228         wpn_eta->setStringValue( eta_str );
229     }
230 }
231
232
233 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
234     if ( n == 0 || !route->size() )
235         altitude_set = false;
236
237     route->add_waypoint( wp, n );
238     update_mirror();
239 }
240
241
242 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
243     SGWayPoint wp;
244
245     if ( route->size() > 0 ) {
246         if ( n < 0 )
247             n = route->size() - 1;
248         wp = route->get_waypoint(n);
249         route->delete_waypoint(n);
250     }
251
252     if ( route->size() <= 2 ) {
253         wpn_id->setStringValue( "" );
254         wpn_dist->setDoubleValue( 0.0 );
255         wpn_eta->setStringValue( "" );
256     }
257
258     if ( route->size() <= 1 ) {
259         wp1_id->setStringValue( "" );
260         wp1_dist->setDoubleValue( 0.0 );
261         wp1_eta->setStringValue( "" );
262     }
263
264     if ( route->size() <= 0 ) {
265         wp0_id->setStringValue( "" );
266         wp0_dist->setDoubleValue( 0.0 );
267         wp0_eta->setStringValue( "" );
268     }
269
270     if ( n == 0 && route->size() )
271         altitude_set = false;
272
273     update_mirror();
274     return wp;
275 }
276
277
278 bool FGRouteMgr::build() {
279     return true;
280 }
281
282
283 int FGRouteMgr::new_waypoint( const string& Tgt_Alt, int n ) {
284     string target = Tgt_Alt;
285
286     // make upper case
287     for (unsigned int i = 0; i < target.size(); i++)
288         if (target[i] >= 'a' && target[i] <= 'z')
289             target[i] -= 'a' - 'A';
290
291     SGWayPoint *wp = 0;
292     int type = make_waypoint( &wp, target );
293
294     if (wp) {
295         add_waypoint( *wp, n );
296         delete wp;
297
298         if ( !on_ground() )
299             fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
300     }
301     return type;
302 }
303
304
305 int FGRouteMgr::make_waypoint(SGWayPoint **wp, string& target) {
306     double alt = -9999.0;
307
308     // extract altitude
309     unsigned int 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;
315     }
316
317     // check for lon,lat
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);
322
323         SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint lon = " << lon << ", lat = " << lat );
324         *wp = new SGWayPoint( lon, lat, alt, SGWayPoint::WGS84, target );
325         return 1;
326     }
327
328     // check for airport id
329     const FGAirport *apt = fgFindAirportID( target );
330     if (apt) {
331         SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (airport) = " << target );
332         *wp = new SGWayPoint( apt->getLongitude(), apt->getLatitude(), alt, SGWayPoint::WGS84, target );
333         return 2;
334     }
335
336     // check for fix id
337     FGFix f;
338     if ( globals->get_fixlist()->query( target, &f ) ) {
339         SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (fix) = " << target );
340         *wp = new SGWayPoint( f.get_lon(), f.get_lat(), alt, SGWayPoint::WGS84, target );
341         return 3;
342     }
343
344     // Try finding a nav matching the ID
345     double lat, lon;
346     // The base lon/lat are determined by the last WP,
347     // or the current pos if the WP list is empty.
348     const int wps = this->size();
349
350     if (wps > 0) {
351         SGWayPoint wp = get_waypoint(wps-1);
352         lat = wp.get_target_lat();
353         lon = wp.get_target_lon();
354     } else {
355         lat = fgGetNode("/position/latitude-deg")->getDoubleValue();
356         lon = fgGetNode("/position/longitude-deg")->getDoubleValue();
357     }
358
359     lat *= SGD_DEGREES_TO_RADIANS;
360     lon *= SGD_DEGREES_TO_RADIANS;
361
362     SG_LOG( SG_GENERAL, SG_INFO, "Looking for nav " << target << " at " << lon << " " << lat);
363
364     if (FGNavRecord* nav = globals->get_navlist()->findByIdent(target.c_str(), lon, lat)) {
365         SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint (nav) = " << target );
366         *wp = new SGWayPoint( nav->get_lon(), nav->get_lat(), alt, SGWayPoint::WGS84, target );
367         return 4;
368     }
369
370     // unknown target
371     return 0;
372 }
373
374
375 // mirror internal route to the property system for inspection by other subsystems
376 void FGRouteMgr::update_mirror() {
377     mirror->removeChildren("wp");
378     for (int i = 0; i < route->size(); i++) {
379         SGWayPoint wp = route->get_waypoint(i);
380         SGPropertyNode *prop = mirror->getChild("wp", i, 1);
381
382         prop->setStringValue("id", wp.get_id().c_str());
383         prop->setStringValue("name", wp.get_name().c_str());
384         prop->setDoubleValue("longitude-deg", wp.get_target_lon());
385         prop->setDoubleValue("latitude-deg", wp.get_target_lat());
386         prop->setDoubleValue("altitude-m", wp.get_target_alt());
387         prop->setDoubleValue("altitude-ft", wp.get_target_alt() * SG_METER_TO_FEET);
388     }
389     // set number as listener attachment point
390     mirror->setIntValue("num", route->size());
391 }
392
393
394 bool FGRouteMgr::on_ground() {
395     SGPropertyNode *gear = fgGetNode( "/gear/gear/wow", false );
396     if ( !gear || gear->getType() == SGPropertyNode::NONE )
397         return fgGetBool( "/sim/presets/onground", true );
398
399     return gear->getBoolValue();
400 }
401
402
403 // command interface /autopilot/route-manager/input:
404 //
405 //   @clear             ... clear route
406 //   @pop               ... remove first entry
407 //   @delete3           ... delete 4th entry
408 //   @insert2:ksfo@900  ... insert "ksfo@900" as 3rd entry
409 //   ksfo@900           ... append "ksfo@900"
410 //
411 void FGRouteMgr::Listener::valueChanged(SGPropertyNode *prop)
412 {
413     const char *s = prop->getStringValue();
414     if (!strcmp(s, "@clear"))
415         mgr->init();
416     else if (!strcmp(s, "@pop"))
417         mgr->pop_waypoint(0);
418     else if (!strncmp(s, "@delete", 7))
419         mgr->pop_waypoint(atoi(s + 7));
420     else if (!strncmp(s, "@insert", 7)) {
421         char *r;
422         int pos = strtol(s + 7, &r, 10);
423         if (*r++ != ':')
424             return;
425         while (isspace(*r))
426             r++;
427         if (*r)
428             mgr->new_waypoint(r, pos);
429     } else
430         mgr->new_waypoint(s);
431 }
432
433