]> 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 <FDM/flight.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Navaids/positioned.hxx>
35
36 #include "route_mgr.hxx"
37
38 #define RM "/autopilot/route-manager/"
39
40
41 FGRouteMgr::FGRouteMgr() :
42     route( new SGRoute ),
43     lon( NULL ),
44     lat( NULL ),
45     alt( NULL ),
46     true_hdg_deg( NULL ),
47     target_altitude_ft( NULL ),
48     altitude_lock( 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     altitude_set( false )
62 {
63     input->setStringValue("");
64     input->addChangeListener(listener);
65 }
66
67
68 FGRouteMgr::~FGRouteMgr() {
69     input->removeChangeListener(listener);
70     delete route;
71 }
72
73
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 );
78
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 );
82
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 );
86
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 );
90
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 );
94
95     route->clear();
96     update_mirror();
97 }
98
99
100 void FGRouteMgr::postinit() {
101     string_list *waypoints = globals->get_initial_waypoints();
102     if (!waypoints)
103         return;
104
105     vector<string>::iterator it;
106     for (it = waypoints->begin(); it != waypoints->end(); ++it)
107         new_waypoint(*it);
108 }
109
110
111 void FGRouteMgr::bind() { }
112 void FGRouteMgr::unbind() { }
113
114
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");
118
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;
122
123     return kts;
124 }
125
126 void FGRouteMgr::updateTargetAltitude() {
127     if (route->size() == 0) {
128         altitude_set = false;
129         return;
130     }
131     
132     SGWayPoint wp = route->get_waypoint( 0 );
133     if (wp.get_target_alt() < -9990.0) {
134         altitude_set = false;
135         return;
136     }
137     
138     altitude_set = true;
139     target_altitude_ft->setDoubleValue( wp.get_target_alt() * SG_METER_TO_FEET );
140             
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" );
146     }
147 }
148
149 void FGRouteMgr::update( double dt ) {
150     if (route->size() == 0) {
151       return; // no route set, early return
152     }
153     
154     double wp_course, wp_distance;
155
156     // first way point
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 );
161
162     if ( wp_distance < 200.0 ) {
163         pop_waypoint();
164         if (route->size() == 0) {
165             return; // end of route, we're done for the time being
166         }
167         
168         wp = route->get_waypoint( 0 );
169     }
170
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);
176
177     // next way point
178     if ( route->size() > 1 ) {
179         SGWayPoint wp = route->get_waypoint( 1 );
180
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);
186     }
187
188     // summarize remaining way points
189     if ( route->size() > 2 ) {
190         SGWayPoint wp;
191         for ( int i = 2; i < route->size(); ++i ) {
192             wp = route->get_waypoint( i );
193             accum += wp.get_distance();
194         }
195
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);
200     }
201 }
202
203 void FGRouteMgr::setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance) {
204     char eta_str[64];
205     double eta = aDistance * SG_METER_TO_NM / get_ground_speed();
206     if ( eta >= 100.0 ) { 
207         eta = 99.999; // clamp
208     }
209     
210     if ( eta < (1.0/6.0) ) {
211         // within 10 minutes, bump up to min/secs
212         eta *= 60.0;
213     }
214     
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 );
219 }
220
221 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
222     route->add_waypoint( wp, n );
223     update_mirror();
224     if ((n==0) || (route->size() == 1)) {
225         updateTargetAltitude();
226     }
227 }
228
229
230 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
231     SGWayPoint wp;
232
233     if ( route->size() > 0 ) {
234         if ( n < 0 )
235             n = route->size() - 1;
236         wp = route->get_waypoint(n);
237         route->delete_waypoint(n);
238     }
239
240     if ( route->size() <= 2 ) {
241         wpn_id->setStringValue( "" );
242         wpn_dist->setDoubleValue( 0.0 );
243         wpn_eta->setStringValue( "" );
244     }
245
246     if ( route->size() <= 1 ) {
247         wp1_id->setStringValue( "" );
248         wp1_dist->setDoubleValue( 0.0 );
249         wp1_eta->setStringValue( "" );
250     }
251
252     if ( route->size() <= 0 ) {
253         wp0_id->setStringValue( "" );
254         wp0_dist->setDoubleValue( 0.0 );
255         wp0_eta->setStringValue( "" );
256     }
257
258     updateTargetAltitude();
259     update_mirror();
260     return wp;
261 }
262
263
264 bool FGRouteMgr::build() {
265     return true;
266 }
267
268
269 void FGRouteMgr::new_waypoint( const string& target, int n ) {
270     SGWayPoint* wp = make_waypoint( target );
271     if (!wp) {
272         return;
273     }
274     
275     add_waypoint( *wp, n );
276     delete wp;
277
278     if ( !near_ground() ) {
279         fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
280     }
281 }
282
283
284 SGWayPoint* FGRouteMgr::make_waypoint(const string& tgt ) {
285     string target = tgt;
286     
287     // make upper case
288     for (unsigned int i = 0; i < target.size(); i++)
289         if (target[i] >= 'a' && target[i] <= 'z')
290             target[i] -= 'a' - 'A';
291
292     // extract altitude
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;
300     }
301
302     // check for lon,lat
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);
307
308         SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint lon = " << lon << ", lat = " << lat );
309         return new SGWayPoint( lon, lat, alt, SGWayPoint::WGS84, target );
310     }    
311
312     SGGeod basePosition;
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());
316     } else {
317         // route is empty, use current position
318         basePosition = SGGeod::fromDeg(
319             fgGetNode("/position/longitude-deg")->getDoubleValue(), 
320             fgGetNode("/position/latitude-deg")->getDoubleValue());
321     }
322
323
324     FGPositionedRef p = FGPositioned::findClosestWithIdent(target, basePosition);
325     if (!p) {
326         SG_LOG( SG_GENERAL, SG_INFO, "Unable to find FGPositioned with ident:" << target);
327         return NULL;
328     }
329     
330     return new SGWayPoint(p->longitude(), p->latitude(), alt, SGWayPoint::WGS84, target);
331 }
332
333
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);
340
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);
347     }
348     // set number as listener attachment point
349     mirror->setIntValue("num", route->size());
350 }
351
352
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 );
357
358     if ( fgGetDouble("/position/altitude-agl-ft", 300.0)
359             < fgGetDouble("/autopilot/route-manager/min-lock-altitude-agl-ft") )
360         return true;
361
362     return gear->getBoolValue();
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++ != ':')
387             return;
388         while (isspace(*r))
389             r++;
390         if (*r)
391             mgr->new_waypoint(r, pos);
392     } else
393         mgr->new_waypoint(s);
394 }
395
396