]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/route_mgr.cxx
654976e1dcd94be3ddbb7761bf83c0cf00f86b3e
[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     wp0_id( NULL ),
50     wp0_dist( NULL ),
51     wp0_eta( NULL ),
52     wp1_id( NULL ),
53     wp1_dist( NULL ),
54     wp1_eta( NULL ),
55     wpn_id( NULL ),
56     wpn_dist( NULL ),
57     wpn_eta( NULL ),
58     input(fgGetNode( RM "input", true )),
59     listener(new Listener(this)),
60     mirror(fgGetNode( RM "route", true ))
61 {
62     input->setStringValue("");
63     input->addChangeListener(listener);
64 }
65
66
67 FGRouteMgr::~FGRouteMgr() {
68     delete route;
69     input->removeChangeListener(listener);
70 }
71
72
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 );
77
78     true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
79
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 );
83
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 );
87
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 );
91
92     route->clear();
93     update_mirror();
94 }
95
96
97 void FGRouteMgr::postinit() {
98     string_list *waypoints = globals->get_initial_waypoints();
99     if (!waypoints)
100         return;
101
102     vector<string>::iterator it;
103     for (it = waypoints->begin(); it != waypoints->end(); ++it)
104         new_waypoint(*it);
105 }
106
107
108 void FGRouteMgr::bind() { }
109 void FGRouteMgr::unbind() { }
110
111
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");
115
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;
119
120     return kts;
121 }
122
123
124 void FGRouteMgr::update( double dt ) {
125     double accum = 0.0;
126     double wp_course, wp_distance;
127     char eta_str[128];
128
129     // first way point
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 );
134
135         true_hdg_deg->setDoubleValue( wp_course );
136
137         if ( wp_distance < 200.0 ) {
138             pop_waypoint();
139         }
140     }
141
142     // next way point
143     if ( route->size() > 0 ) {
144         SGWayPoint wp = route->get_waypoint( 0 );
145         // update the property tree info
146
147         wp0_id->setStringValue( wp.get_id().c_str() );
148
149         accum += wp_distance;
150         wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
151
152         double eta = accum * SG_METER_TO_NM / get_ground_speed();
153         if ( eta >= 100.0 ) { eta = 99.999; }
154         int major, minor;
155         if ( eta < (1.0/6.0) ) {
156             // within 10 minutes, bump up to min/secs
157             eta *= 60.0;
158         }
159         major = (int)eta;
160         minor = (int)((eta - (int)eta) * 60.0);
161         snprintf( eta_str, 128, "%d:%02d", major, minor );
162         wp0_eta->setStringValue( eta_str );
163     }
164
165     // next way point
166     if ( route->size() > 1 ) {
167         SGWayPoint wp = route->get_waypoint( 1 );
168
169         // update the property tree info
170
171         wp1_id->setStringValue( wp.get_id().c_str() );
172
173         accum += wp.get_distance();
174         wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
175
176         double eta = accum * SG_METER_TO_NM / get_ground_speed();
177         if ( eta >= 100.0 ) { eta = 99.999; }
178         int major, minor;
179         if ( eta < (1.0/6.0) ) {
180             // within 10 minutes, bump up to min/secs
181             eta *= 60.0;
182         }
183         major = (int)eta;
184         minor = (int)((eta - (int)eta) * 60.0);
185         snprintf( eta_str, 128, "%d:%02d", major, minor );
186         wp1_eta->setStringValue( eta_str );
187     }
188
189     // summarize remaining way points
190     if ( route->size() > 2 ) {
191         SGWayPoint wp;
192         for ( int i = 2; i < route->size(); ++i ) {
193             wp = route->get_waypoint( i );
194             accum += wp.get_distance();
195         }
196
197         // update the property tree info
198
199         wpn_id->setStringValue( wp.get_id().c_str() );
200
201         wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
202
203         double eta = accum * SG_METER_TO_NM / get_ground_speed();
204         if ( eta >= 100.0 ) { eta = 99.999; }
205         int major, minor;
206         if ( eta < (1.0/6.0) ) {
207             // within 10 minutes, bump up to min/secs
208             eta *= 60.0;
209         }
210         major = (int)eta;
211         minor = (int)((eta - (int)eta) * 60.0);
212         snprintf( eta_str, 128, "%d:%02d", major, minor );
213         wpn_eta->setStringValue( eta_str );
214     }
215 }
216
217
218 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
219     route->add_waypoint( wp, n );
220     update_mirror();
221 }
222
223
224 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
225     SGWayPoint wp;
226
227     if ( route->size() > 0 ) {
228         if ( n < 0 )
229             n = route->size() - 1;
230         wp = route->get_waypoint(n);
231         route->delete_waypoint(n);
232     }
233
234     if ( route->size() <= 2 ) {
235         wpn_id->setStringValue( "" );
236         wpn_dist->setDoubleValue( 0.0 );
237         wpn_eta->setStringValue( "" );
238     }
239
240     if ( route->size() <= 1 ) {
241         wp1_id->setStringValue( "" );
242         wp1_dist->setDoubleValue( 0.0 );
243         wp1_eta->setStringValue( "" );
244     }
245
246     if ( route->size() <= 0 ) {
247         wp0_id->setStringValue( "" );
248         wp0_dist->setDoubleValue( 0.0 );
249         wp0_eta->setStringValue( "" );
250     }
251
252     update_mirror();
253     return wp;
254 }
255
256
257 bool FGRouteMgr::build() {
258     return true;
259 }
260
261
262
263 int FGRouteMgr::new_waypoint( const string& Tgt_Alt, int n ) {
264     double alt = 0.0;
265     string target = Tgt_Alt;
266
267     // make upper case
268     for (unsigned int i = 0; i < target.size(); i++)
269         if (target[i] >= 'a' && target[i] <= 'z')
270             target[i] -= 'a' - 'A';
271
272     // extract altitude
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;
279     }
280
281     // check for lon,lat
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);
286
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" );
291         return 1;
292     }
293
294     // check for airport id
295     const FGAirport *apt = fgFindAirportID( target );
296     if (apt) {
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" );
301         return 2;
302     }
303
304     // check for fix id
305     FGFix f;
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" );
311         return 3;
312     }
313
314     // Try finding a nav matching the ID
315     double lat, lon;
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();
319
320     if (wps > 0) {
321         SGWayPoint wp = get_waypoint(wps-1);
322         lat = wp.get_target_lat();
323         lon = wp.get_target_lon();
324     } else {
325         lat = fgGetNode("/position/latitude-deg")->getDoubleValue();
326         lon = fgGetNode("/position/longitude-deg")->getDoubleValue();
327     }
328
329     lat *= SGD_DEGREES_TO_RADIANS;
330     lon *= SGD_DEGREES_TO_RADIANS;
331
332     SG_LOG( SG_GENERAL, SG_INFO, "Looking for nav " << target << " at " << lon << " " << lat);
333
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" );
339         return 4;
340     }
341
342     // target not identified
343     return 0;
344 }
345
346
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);
353
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);
360     }
361     // set number as listener attachment point
362     mirror->setIntValue("num", route->size());
363 }
364
365
366 // command interface /autopilot/route-manager/input:
367 //
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"
373 //
374 void FGRouteMgr::Listener::valueChanged(SGPropertyNode *prop)
375 {
376     const char *s = prop->getStringValue();
377     if (!strcmp(s, "@clear"))
378         mgr->init();
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)) {
384         char *r;
385         int pos = strtol(s + 7, &r, 10);
386         if (*r++ == ':' && *r)
387             mgr->new_waypoint(r, pos);
388     } else
389         mgr->new_waypoint(s);
390 }
391
392