]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/route_mgr.cxx
James Turner:
[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/fix.hxx>
37 #include <Navaids/navlist.hxx>
38
39 #include "route_mgr.hxx"
40
41 #define RM "/autopilot/route-manager/"
42
43
44 FGRouteMgr::FGRouteMgr() :
45     route( new SGRoute ),
46     lon( NULL ),
47     lat( NULL ),
48     alt( NULL ),
49     true_hdg_deg( NULL ),
50     target_altitude_ft( NULL ),
51     altitude_lock( NULL ),
52     wp0_id( NULL ),
53     wp0_dist( NULL ),
54     wp0_eta( NULL ),
55     wp1_id( NULL ),
56     wp1_dist( NULL ),
57     wp1_eta( NULL ),
58     wpn_id( NULL ),
59     wpn_dist( NULL ),
60     wpn_eta( NULL ),
61     input(fgGetNode( RM "input", true )),
62     listener(new Listener(this)),
63     mirror(fgGetNode( RM "route", true )),
64     altitude_set( false )
65 {
66     input->setStringValue("");
67     input->addChangeListener(listener);
68 }
69
70
71 FGRouteMgr::~FGRouteMgr() {
72     input->removeChangeListener(listener);
73     delete route;
74 }
75
76
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 );
81
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 );
85
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 );
89
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 );
93
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 );
97
98     route->clear();
99     update_mirror();
100 }
101
102
103 void FGRouteMgr::postinit() {
104     string_list *waypoints = globals->get_initial_waypoints();
105     if (!waypoints)
106         return;
107
108     vector<string>::iterator it;
109     for (it = waypoints->begin(); it != waypoints->end(); ++it)
110         new_waypoint(*it);
111 }
112
113
114 void FGRouteMgr::bind() { }
115 void FGRouteMgr::unbind() { }
116
117
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");
121
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;
125
126     return kts;
127 }
128
129
130 void FGRouteMgr::update( double dt ) {
131     double accum = 0.0;
132     double wp_course, wp_distance;
133     char eta_str[128];
134
135     // first way point
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 );
140
141         true_hdg_deg->setDoubleValue( wp_course );
142         double target_alt = wp.get_target_alt();
143
144         if ( !altitude_set && target_alt > -9990 ) {
145             target_altitude_ft->setDoubleValue( target_alt * SG_METER_TO_FEET );
146             altitude_set = true;
147
148             if ( !near_ground() )
149                 altitude_lock->setStringValue( "altitude-hold" );
150         }
151
152         if ( wp_distance < 200.0 ) {
153             pop_waypoint();
154             altitude_set = false;
155         }
156     }
157
158     // next way point
159     if ( route->size() > 0 ) {
160         SGWayPoint wp = route->get_waypoint( 0 );
161         // update the property tree info
162
163         wp0_id->setStringValue( wp.get_id().c_str() );
164
165         accum += wp_distance;
166         wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
167
168         double eta = accum * SG_METER_TO_NM / get_ground_speed();
169         if ( eta >= 100.0 ) { eta = 99.999; }
170         int major, minor;
171         if ( eta < (1.0/6.0) ) {
172             // within 10 minutes, bump up to min/secs
173             eta *= 60.0;
174         }
175         major = (int)eta;
176         minor = (int)((eta - (int)eta) * 60.0);
177         snprintf( eta_str, 128, "%d:%02d", major, minor );
178         wp0_eta->setStringValue( eta_str );
179     }
180
181     // next way point
182     if ( route->size() > 1 ) {
183         SGWayPoint wp = route->get_waypoint( 1 );
184
185         // update the property tree info
186
187         wp1_id->setStringValue( wp.get_id().c_str() );
188
189         accum += wp.get_distance();
190         wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
191
192         double eta = accum * SG_METER_TO_NM / get_ground_speed();
193         if ( eta >= 100.0 ) { eta = 99.999; }
194         int major, minor;
195         if ( eta < (1.0/6.0) ) {
196             // within 10 minutes, bump up to min/secs
197             eta *= 60.0;
198         }
199         major = (int)eta;
200         minor = (int)((eta - (int)eta) * 60.0);
201         snprintf( eta_str, 128, "%d:%02d", major, minor );
202         wp1_eta->setStringValue( eta_str );
203     }
204
205     // summarize remaining way points
206     if ( route->size() > 2 ) {
207         SGWayPoint wp;
208         for ( int i = 2; i < route->size(); ++i ) {
209             wp = route->get_waypoint( i );
210             accum += wp.get_distance();
211         }
212
213         // update the property tree info
214
215         wpn_id->setStringValue( wp.get_id().c_str() );
216
217         wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
218
219         double eta = accum * SG_METER_TO_NM / get_ground_speed();
220         if ( eta >= 100.0 ) { eta = 99.999; }
221         int major, minor;
222         if ( eta < (1.0/6.0) ) {
223             // within 10 minutes, bump up to min/secs
224             eta *= 60.0;
225         }
226         major = (int)eta;
227         minor = (int)((eta - (int)eta) * 60.0);
228         snprintf( eta_str, 128, "%d:%02d", major, minor );
229         wpn_eta->setStringValue( eta_str );
230     }
231 }
232
233
234 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
235     if ( n == 0 || !route->size() )
236         altitude_set = false;
237
238     route->add_waypoint( wp, n );
239     update_mirror();
240 }
241
242
243 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
244     SGWayPoint wp;
245
246     if ( route->size() > 0 ) {
247         if ( n < 0 )
248             n = route->size() - 1;
249         wp = route->get_waypoint(n);
250         route->delete_waypoint(n);
251     }
252
253     if ( route->size() <= 2 ) {
254         wpn_id->setStringValue( "" );
255         wpn_dist->setDoubleValue( 0.0 );
256         wpn_eta->setStringValue( "" );
257     }
258
259     if ( route->size() <= 1 ) {
260         wp1_id->setStringValue( "" );
261         wp1_dist->setDoubleValue( 0.0 );
262         wp1_eta->setStringValue( "" );
263     }
264
265     if ( route->size() <= 0 ) {
266         wp0_id->setStringValue( "" );
267         wp0_dist->setDoubleValue( 0.0 );
268         wp0_eta->setStringValue( "" );
269     }
270
271     if ( n == 0 && route->size() )
272         altitude_set = false;
273
274     update_mirror();
275     return wp;
276 }
277
278
279 bool FGRouteMgr::build() {
280     return true;
281 }
282
283
284 int FGRouteMgr::new_waypoint( const string& target, int n ) {
285     SGWayPoint *wp = 0;
286     int type = make_waypoint( &wp, target );
287
288     if (wp) {
289         add_waypoint( *wp, n );
290         delete wp;
291
292         if ( !near_ground() )
293             fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
294     }
295     return type;
296 }
297
298
299 int FGRouteMgr::make_waypoint( SGWayPoint **wp, const string& tgt ) {
300     string target = tgt;
301
302     // make upper case
303     for (unsigned int i = 0; i < target.size(); i++)
304         if (target[i] >= 'a' && target[i] <= 'z')
305             target[i] -= 'a' - 'A';
306
307     // extract altitude
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;
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     double lat, lon;
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();
340
341     if (wps > 0) {
342         SGWayPoint wp = get_waypoint(wps-1);
343         lat = wp.get_target_lat();
344         lon = wp.get_target_lon();
345     } else {
346         lat = fgGetNode("/position/latitude-deg")->getDoubleValue();
347         lon = fgGetNode("/position/longitude-deg")->getDoubleValue();
348     }
349
350     // check for fix id
351     FGFix* f;
352     double heading;
353     double dist;
354
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 );
358         return 3;
359     }
360
361     // Try finding a nav matching the ID
362     lat *= SGD_DEGREES_TO_RADIANS;
363     lon *= SGD_DEGREES_TO_RADIANS;
364
365     SG_LOG( SG_GENERAL, SG_INFO, "Looking for nav " << target << " at " << lon << " " << lat);
366
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 );
370         return 4;
371     }
372
373     // unknown target
374     return 0;
375 }
376
377
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);
384
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);
391     }
392     // set number as listener attachment point
393     mirror->setIntValue("num", route->size());
394 }
395
396
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 );
401
402     if ( fgGetDouble("/position/altitude-agl-ft", 300.0)
403             < fgGetDouble("/autopilot/route-manager/min-lock-altitude-agl-ft") )
404         return true;
405
406     return gear->getBoolValue();
407 }
408
409
410 // command interface /autopilot/route-manager/input:
411 //
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"
417 //
418 void FGRouteMgr::Listener::valueChanged(SGPropertyNode *prop)
419 {
420     const char *s = prop->getStringValue();
421     if (!strcmp(s, "@CLEAR"))
422         mgr->init();
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)) {
428         char *r;
429         int pos = strtol(s + 7, &r, 10);
430         if (*r++ != ':')
431             return;
432         while (isspace(*r))
433             r++;
434         if (*r)
435             mgr->new_waypoint(r, pos);
436     } else
437         mgr->new_waypoint(s);
438 }
439
440