]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/route_mgr.cxx
Various mods to allow querying for nearest airport (with optional ability to
[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 //
5 // Copyright (C) 2004  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <FDM/flight.hxx>
31 #include <Main/fg_props.hxx>
32
33 #include "route_mgr.hxx"
34
35
36 FGRouteMgr::FGRouteMgr() :
37     route( new SGRoute ),
38     lon( NULL ),
39     lat( NULL ),
40     alt( NULL ),
41     true_hdg_deg( NULL ),
42     wp0_id( NULL ),
43     wp0_dist( NULL ),
44     wp0_eta( NULL ),
45     wp1_id( NULL ),
46     wp1_dist( NULL ),
47     wp1_eta( NULL ),
48     wpn_id( NULL ),
49     wpn_dist( NULL ),
50     wpn_eta( NULL )
51 {
52 }
53
54
55 FGRouteMgr::~FGRouteMgr() {
56     delete route;
57 }
58
59
60 void FGRouteMgr::init() {
61     lon = fgGetNode( "/position/longitude-deg", true );
62     lat = fgGetNode( "/position/latitude-deg", true );
63     alt = fgGetNode( "/position/altitude-ft", true );
64
65     true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
66
67     wp0_id = fgGetNode( "/autopilot/route-manager/wp[0]/id", true );
68     wp0_dist = fgGetNode( "/autopilot/route-manager/wp[0]/dist", true );
69     wp0_eta = fgGetNode( "/autopilot/route-manager/wp[0]/eta", true );
70
71     wp1_id = fgGetNode( "/autopilot/route-manager/wp[1]/id", true );
72     wp1_dist = fgGetNode( "/autopilot/route-manager/wp[1]/dist", true );
73     wp1_eta = fgGetNode( "/autopilot/route-manager/wp[1]/eta", true );
74
75     wpn_id = fgGetNode( "/autopilot/route-manager/wp-last/id", true );
76     wpn_dist = fgGetNode( "/autopilot/route-manager/wp-last/dist", true );
77     wpn_eta = fgGetNode( "/autopilot/route-manager/wp-last/eta", true );
78
79     route->clear();
80 }
81
82
83 void FGRouteMgr::bind() { }
84 void FGRouteMgr::unbind() { }
85
86
87 static double get_ground_speed() {
88     // starts in ft/s so we convert to kts
89     static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
90
91     double ft_s = cur_fdm_state->get_V_ground_speed() 
92         * speedup_node->getIntValue();
93     double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
94
95     return kts;
96 }
97
98
99 void FGRouteMgr::update( double dt ) {
100     double accum = 0.0;
101     double wp_course, wp_distance;
102     char eta_str[128];
103
104     // first way point
105     if ( route->size() > 0 ) {
106         SGWayPoint wp = route->get_waypoint( 0 );
107         wp.CourseAndDistance( lon->getDoubleValue(), lat->getDoubleValue(),
108                               alt->getDoubleValue(), &wp_course, &wp_distance );
109
110         true_hdg_deg->setDoubleValue( wp_course );
111
112         if ( wp_distance < 200.0 ) {
113             pop_waypoint();
114         }
115     }
116
117     // next way point
118     if ( route->size() > 0 ) {
119         SGWayPoint wp = route->get_waypoint( 0 );
120         // update the property tree info
121
122         wp0_id->setStringValue( wp.get_id().c_str() );
123
124         accum += wp_distance;
125         wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
126
127         double eta = accum * SG_METER_TO_NM / get_ground_speed();
128         if ( eta >= 100.0 ) { eta = 99.999; }
129         int major, minor;
130         if ( eta < (1.0/6.0) ) {
131             // within 10 minutes, bump up to min/secs
132             eta *= 60.0;
133         }
134         major = (int)eta;
135         minor = (int)((eta - (int)eta) * 60.0);
136         snprintf( eta_str, 128, "%d:%02d", major, minor );
137         wp0_eta->setStringValue( eta_str );
138     }
139
140     // next way point
141     if ( route->size() > 1 ) {
142         SGWayPoint wp = route->get_waypoint( 1 );
143
144         // update the property tree info
145
146         wp1_id->setStringValue( wp.get_id().c_str() );
147
148         accum += wp.get_distance();
149         wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
150
151         double eta = accum * SG_METER_TO_NM / get_ground_speed();
152         if ( eta >= 100.0 ) { eta = 99.999; }
153         int major, minor;
154         if ( eta < (1.0/6.0) ) {
155             // within 10 minutes, bump up to min/secs
156             eta *= 60.0;
157         }
158         major = (int)eta;
159         minor = (int)((eta - (int)eta) * 60.0);
160         snprintf( eta_str, 128, "%d:%02d", major, minor );
161         wp1_eta->setStringValue( eta_str );
162     }
163
164     // summarize remaining way points
165     if ( route->size() > 2 ) {
166         SGWayPoint wp;
167         for ( int i = 2; i < route->size(); ++i ) {
168             wp = route->get_waypoint( i );
169             accum += wp.get_distance();
170         }
171
172         // update the property tree info
173
174         wpn_id->setStringValue( wp.get_id().c_str() );
175
176         wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
177
178         double eta = accum * SG_METER_TO_NM / get_ground_speed();
179         if ( eta >= 100.0 ) { eta = 99.999; }
180         int major, minor;
181         if ( eta < (1.0/6.0) ) {
182             // within 10 minutes, bump up to min/secs
183             eta *= 60.0;
184         }
185         major = (int)eta;
186         minor = (int)((eta - (int)eta) * 60.0);
187         snprintf( eta_str, 128, "%d:%02d", major, minor );
188         wpn_eta->setStringValue( eta_str );
189     }
190 }
191
192
193 SGWayPoint FGRouteMgr::pop_waypoint() {
194     SGWayPoint wp;
195
196     if ( route->size() > 0 ) {
197         wp = route->get_first();
198         route->delete_first();
199     }
200
201     if ( route->size() <= 2 ) {
202         wpn_id->setStringValue( "" );
203         wpn_dist->setDoubleValue( 0.0 );
204         wpn_eta->setStringValue( "" );
205     }
206
207     if ( route->size() <= 1 ) {
208         wp1_id->setStringValue( "" );
209         wp1_dist->setDoubleValue( 0.0 );
210         wp1_eta->setStringValue( "" );
211     }
212
213     if ( route->size() <= 0 ) {
214         wp0_id->setStringValue( "" );
215         wp0_dist->setDoubleValue( 0.0 );
216         wp0_eta->setStringValue( "" );
217     }
218
219     return wp;
220 }
221
222
223 bool FGRouteMgr::build() {
224     return true;
225 }