]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/route_mgr.cxx
fix a pointer reference.
[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 listener;
71     delete route;
72 }
73
74
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 );
79
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 );
83
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 );
87
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 );
91
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 );
95
96     route->clear();
97     update_mirror();
98 }
99
100
101 void FGRouteMgr::postinit() {
102     string_list *waypoints = globals->get_initial_waypoints();
103     if (!waypoints)
104         return;
105
106     vector<string>::iterator it;
107     for (it = waypoints->begin(); it != waypoints->end(); ++it)
108         new_waypoint(*it);
109 }
110
111
112 void FGRouteMgr::bind() { }
113 void FGRouteMgr::unbind() { }
114
115
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");
119
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;
123
124     return kts;
125 }
126
127 void FGRouteMgr::updateTargetAltitude() {
128     if (route->size() == 0) {
129         altitude_set = false;
130         return;
131     }
132     
133     SGWayPoint wp = route->get_waypoint( 0 );
134     if (wp.get_target_alt() < -9990.0) {
135         altitude_set = false;
136         return;
137     }
138     
139     altitude_set = true;
140     target_altitude_ft->setDoubleValue( wp.get_target_alt() * SG_METER_TO_FEET );
141             
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" );
147     }
148 }
149
150 void FGRouteMgr::update( double dt ) {
151     if (route->size() == 0) {
152       return; // no route set, early return
153     }
154     
155     double wp_course, wp_distance;
156
157     // first way point
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 );
162
163     if ( wp_distance < 200.0 ) {
164         pop_waypoint();
165         if (route->size() == 0) {
166             return; // end of route, we're done for the time being
167         }
168         
169         wp = route->get_waypoint( 0 );
170     }
171
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);
177
178     // next way point
179     if ( route->size() > 1 ) {
180         SGWayPoint wp = route->get_waypoint( 1 );
181
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);
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         wpn_id->setStringValue( wp.get_id().c_str() );
199         wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
200         setETAPropertyFromDistance(wpn_eta, accum);
201     }
202 }
203
204 void FGRouteMgr::setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance) {
205     char eta_str[64];
206     double eta = aDistance * SG_METER_TO_NM / get_ground_speed();
207     if ( eta >= 100.0 ) { 
208         eta = 99.999; // clamp
209     }
210     
211     if ( eta < (1.0/6.0) ) {
212         // within 10 minutes, bump up to min/secs
213         eta *= 60.0;
214     }
215     
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 );
220 }
221
222 void FGRouteMgr::add_waypoint( const SGWayPoint& wp, int n ) {
223     route->add_waypoint( wp, n );
224     update_mirror();
225     if ((n==0) || (route->size() == 1)) {
226         updateTargetAltitude();
227     }
228 }
229
230
231 SGWayPoint FGRouteMgr::pop_waypoint( int n ) {
232     SGWayPoint wp;
233
234     if ( route->size() > 0 ) {
235         if ( n < 0 )
236             n = route->size() - 1;
237         wp = route->get_waypoint(n);
238         route->delete_waypoint(n);
239     }
240
241     if ( route->size() <= 2 ) {
242         wpn_id->setStringValue( "" );
243         wpn_dist->setDoubleValue( 0.0 );
244         wpn_eta->setStringValue( "" );
245     }
246
247     if ( route->size() <= 1 ) {
248         wp1_id->setStringValue( "" );
249         wp1_dist->setDoubleValue( 0.0 );
250         wp1_eta->setStringValue( "" );
251     }
252
253     if ( route->size() <= 0 ) {
254         wp0_id->setStringValue( "" );
255         wp0_dist->setDoubleValue( 0.0 );
256         wp0_eta->setStringValue( "" );
257     }
258
259     updateTargetAltitude();
260     update_mirror();
261     return wp;
262 }
263
264
265 bool FGRouteMgr::build() {
266     return true;
267 }
268
269
270 void FGRouteMgr::new_waypoint( const string& target, int n ) {
271     SGWayPoint* wp = make_waypoint( target );
272     if (!wp) {
273         return;
274     }
275     
276     add_waypoint( *wp, n );
277     delete wp;
278
279     if ( !near_ground() ) {
280         fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
281     }
282 }
283
284
285 SGWayPoint* FGRouteMgr::make_waypoint(const string& tgt ) {
286     string target = tgt;
287     
288     // make upper case
289     for (unsigned int i = 0; i < target.size(); i++)
290         if (target[i] >= 'a' && target[i] <= 'z')
291             target[i] -= 'a' - 'A';
292
293     // extract altitude
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;
301     }
302
303     // check for lon,lat
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);
308
309         SG_LOG( SG_GENERAL, SG_INFO, "Adding waypoint lon = " << lon << ", lat = " << lat );
310         return new SGWayPoint( lon, lat, alt, SGWayPoint::WGS84, target );
311     }    
312
313     SGGeod basePosition;
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());
317     } else {
318         // route is empty, use current position
319         basePosition = SGGeod::fromDeg(
320             fgGetNode("/position/longitude-deg")->getDoubleValue(), 
321             fgGetNode("/position/latitude-deg")->getDoubleValue());
322     }
323
324
325     FGPositionedRef p = FGPositioned::findClosestWithIdent(target, basePosition);
326     if (!p) {
327         SG_LOG( SG_GENERAL, SG_INFO, "Unable to find FGPositioned with ident:" << target);
328         return NULL;
329     }
330     
331     return new SGWayPoint(p->longitude(), p->latitude(), alt, SGWayPoint::WGS84, target);
332 }
333
334
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);
341
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);
348     }
349     // set number as listener attachment point
350     mirror->setIntValue("num", route->size());
351 }
352
353
354 bool FGRouteMgr::near_ground() {
355     SGPropertyNode *gear = fgGetNode( "/gear/gear/wow", false );
356     if ( !gear || gear->getType() == simgear::props::NONE )
357         return fgGetBool( "/sim/presets/onground", true );
358
359     if ( fgGetDouble("/position/altitude-agl-ft", 300.0)
360             < fgGetDouble("/autopilot/route-manager/min-lock-altitude-agl-ft") )
361         return true;
362
363     return gear->getBoolValue();
364 }
365
366
367 // command interface /autopilot/route-manager/input:
368 //
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"
374 //
375 void FGRouteMgr::Listener::valueChanged(SGPropertyNode *prop)
376 {
377     const char *s = prop->getStringValue();
378     if (!strcmp(s, "@CLEAR"))
379         mgr->init();
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)) {
385         char *r;
386         int pos = strtol(s + 7, &r, 10);
387         if (*r++ != ':')
388             return;
389         while (isspace(*r))
390             r++;
391         if (*r)
392             mgr->new_waypoint(r, pos);
393     } else
394         mgr->new_waypoint(s);
395 }
396
397