]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/route_mgr.cxx
working on the termination of the last hardcoded dialogs in Autopilot/auto_gui.cxx:
[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  - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 "auto_gui.hxx"        // FIXME temporary dependency (NewWaypoint)
34 #include "route_mgr.hxx"
35
36
37 FGRouteMgr::FGRouteMgr() :
38     route( new SGRoute ),
39     lon( NULL ),
40     lat( NULL ),
41     alt( NULL ),
42     true_hdg_deg( NULL ),
43     wp0_id( NULL ),
44     wp0_dist( NULL ),
45     wp0_eta( NULL ),
46     wp1_id( NULL ),
47     wp1_dist( NULL ),
48     wp1_eta( NULL ),
49     wpn_id( NULL ),
50     wpn_dist( NULL ),
51     wpn_eta( NULL )
52 {
53 }
54
55
56 FGRouteMgr::~FGRouteMgr() {
57     delete route;
58 }
59
60
61 void FGRouteMgr::init() {
62     lon = fgGetNode( "/position/longitude-deg", true );
63     lat = fgGetNode( "/position/latitude-deg", true );
64     alt = fgGetNode( "/position/altitude-ft", true );
65
66     true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
67
68     wp0_id = fgGetNode( "/autopilot/route-manager/wp[0]/id", true );
69     wp0_dist = fgGetNode( "/autopilot/route-manager/wp[0]/dist", true );
70     wp0_eta = fgGetNode( "/autopilot/route-manager/wp[0]/eta", true );
71
72     wp1_id = fgGetNode( "/autopilot/route-manager/wp[1]/id", true );
73     wp1_dist = fgGetNode( "/autopilot/route-manager/wp[1]/dist", true );
74     wp1_eta = fgGetNode( "/autopilot/route-manager/wp[1]/eta", true );
75
76     wpn_id = fgGetNode( "/autopilot/route-manager/wp-last/id", true );
77     wpn_dist = fgGetNode( "/autopilot/route-manager/wp-last/dist", true );
78     wpn_eta = fgGetNode( "/autopilot/route-manager/wp-last/eta", true );
79
80     route->clear();
81 }
82
83
84 void FGRouteMgr::postinit() {
85     string_list *waypoints = globals->get_initial_waypoints();
86     if (!waypoints)
87         return;
88
89     vector<string>::iterator it;
90     for (it = waypoints->begin(); it != waypoints->end(); ++it)
91         NewWaypoint(*it);
92 }
93
94
95 void FGRouteMgr::bind() { }
96 void FGRouteMgr::unbind() { }
97
98
99 static double get_ground_speed() {
100     // starts in ft/s so we convert to kts
101     static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
102
103     double ft_s = cur_fdm_state->get_V_ground_speed() 
104         * speedup_node->getIntValue();
105     double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
106
107     return kts;
108 }
109
110
111 void FGRouteMgr::update( double dt ) {
112     double accum = 0.0;
113     double wp_course, wp_distance;
114     char eta_str[128];
115
116     // first way point
117     if ( route->size() > 0 ) {
118         SGWayPoint wp = route->get_waypoint( 0 );
119         wp.CourseAndDistance( lon->getDoubleValue(), lat->getDoubleValue(),
120                               alt->getDoubleValue(), &wp_course, &wp_distance );
121
122         true_hdg_deg->setDoubleValue( wp_course );
123
124         if ( wp_distance < 200.0 ) {
125             pop_waypoint();
126         }
127     }
128
129     // next way point
130     if ( route->size() > 0 ) {
131         SGWayPoint wp = route->get_waypoint( 0 );
132         // update the property tree info
133
134         wp0_id->setStringValue( wp.get_id().c_str() );
135
136         accum += wp_distance;
137         wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
138
139         double eta = accum * SG_METER_TO_NM / get_ground_speed();
140         if ( eta >= 100.0 ) { eta = 99.999; }
141         int major, minor;
142         if ( eta < (1.0/6.0) ) {
143             // within 10 minutes, bump up to min/secs
144             eta *= 60.0;
145         }
146         major = (int)eta;
147         minor = (int)((eta - (int)eta) * 60.0);
148         snprintf( eta_str, 128, "%d:%02d", major, minor );
149         wp0_eta->setStringValue( eta_str );
150     }
151
152     // next way point
153     if ( route->size() > 1 ) {
154         SGWayPoint wp = route->get_waypoint( 1 );
155
156         // update the property tree info
157
158         wp1_id->setStringValue( wp.get_id().c_str() );
159
160         accum += wp.get_distance();
161         wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
162
163         double eta = accum * SG_METER_TO_NM / get_ground_speed();
164         if ( eta >= 100.0 ) { eta = 99.999; }
165         int major, minor;
166         if ( eta < (1.0/6.0) ) {
167             // within 10 minutes, bump up to min/secs
168             eta *= 60.0;
169         }
170         major = (int)eta;
171         minor = (int)((eta - (int)eta) * 60.0);
172         snprintf( eta_str, 128, "%d:%02d", major, minor );
173         wp1_eta->setStringValue( eta_str );
174     }
175
176     // summarize remaining way points
177     if ( route->size() > 2 ) {
178         SGWayPoint wp;
179         for ( int i = 2; i < route->size(); ++i ) {
180             wp = route->get_waypoint( i );
181             accum += wp.get_distance();
182         }
183
184         // update the property tree info
185
186         wpn_id->setStringValue( wp.get_id().c_str() );
187
188         wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
189
190         double eta = accum * SG_METER_TO_NM / get_ground_speed();
191         if ( eta >= 100.0 ) { eta = 99.999; }
192         int major, minor;
193         if ( eta < (1.0/6.0) ) {
194             // within 10 minutes, bump up to min/secs
195             eta *= 60.0;
196         }
197         major = (int)eta;
198         minor = (int)((eta - (int)eta) * 60.0);
199         snprintf( eta_str, 128, "%d:%02d", major, minor );
200         wpn_eta->setStringValue( eta_str );
201     }
202 }
203
204
205 SGWayPoint FGRouteMgr::pop_waypoint() {
206     SGWayPoint wp;
207
208     if ( route->size() > 0 ) {
209         wp = route->get_first();
210         route->delete_first();
211     }
212
213     if ( route->size() <= 2 ) {
214         wpn_id->setStringValue( "" );
215         wpn_dist->setDoubleValue( 0.0 );
216         wpn_eta->setStringValue( "" );
217     }
218
219     if ( route->size() <= 1 ) {
220         wp1_id->setStringValue( "" );
221         wp1_dist->setDoubleValue( 0.0 );
222         wp1_eta->setStringValue( "" );
223     }
224
225     if ( route->size() <= 0 ) {
226         wp0_id->setStringValue( "" );
227         wp0_dist->setDoubleValue( 0.0 );
228         wp0_eta->setStringValue( "" );
229     }
230
231     return wp;
232 }
233
234
235 bool FGRouteMgr::build() {
236     return true;
237 }