]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/route_mgr.cxx
Comment out some unused references and left over debugging output.
[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     cout << "route = " << route << endl;
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::bind() { }
85 void FGRouteMgr::unbind() { }
86
87
88 static double get_ground_speed() {
89     // starts in ft/s so we convert to kts
90     static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
91
92     double ft_s = cur_fdm_state->get_V_ground_speed() 
93         * speedup_node->getIntValue();
94     double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
95
96     return kts;
97 }
98
99
100 void FGRouteMgr::update( double dt ) {
101     double accum = 0.0;
102     double wp_course, wp_distance;
103     char eta_str[128];
104
105     // first way point
106     if ( route->size() > 0 ) {
107         SGWayPoint wp = route->get_waypoint( 0 );
108         wp.CourseAndDistance( lon->getDoubleValue(), lat->getDoubleValue(),
109                               alt->getDoubleValue(), &wp_course, &wp_distance );
110
111         true_hdg_deg->setDoubleValue( wp_course );
112
113         if ( wp_distance < 200.0 ) {
114             pop_waypoint();
115         }
116     }
117
118     // next way point
119     if ( route->size() > 0 ) {
120         SGWayPoint wp = route->get_waypoint( 0 );
121         // update the property tree info
122
123         wp0_id->setStringValue( wp.get_id().c_str() );
124
125         accum += wp_distance;
126         wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
127
128         double eta = accum * SG_METER_TO_NM / get_ground_speed();
129         if ( eta >= 100.0 ) { eta = 99.999; }
130         int major, minor;
131         if ( eta < (1.0/6.0) ) {
132             // within 10 minutes, bump up to min/secs
133             eta *= 60.0;
134         }
135         major = (int)eta;
136         minor = (int)((eta - (int)eta) * 60.0);
137         snprintf( eta_str, 128, "%d:%02d", major, minor );
138         wp0_eta->setStringValue( eta_str );
139     }
140
141     // next way point
142     if ( route->size() > 1 ) {
143         SGWayPoint wp = route->get_waypoint( 1 );
144
145         // update the property tree info
146
147         wp1_id->setStringValue( wp.get_id().c_str() );
148
149         accum += wp.get_distance();
150         wp1_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         wp1_eta->setStringValue( eta_str );
163     }
164
165     // summarize remaining way points
166     if ( route->size() > 2 ) {
167         SGWayPoint wp;
168         for ( int i = 2; i < route->size(); ++i ) {
169             wp = route->get_waypoint( i );
170             accum += wp.get_distance();
171         }
172
173         // update the property tree info
174
175         wpn_id->setStringValue( wp.get_id().c_str() );
176
177         wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
178
179         double eta = accum * SG_METER_TO_NM / get_ground_speed();
180         if ( eta >= 100.0 ) { eta = 99.999; }
181         int major, minor;
182         if ( eta < (1.0/6.0) ) {
183             // within 10 minutes, bump up to min/secs
184             eta *= 60.0;
185         }
186         major = (int)eta;
187         minor = (int)((eta - (int)eta) * 60.0);
188         snprintf( eta_str, 128, "%d:%02d", major, minor );
189         wpn_eta->setStringValue( eta_str );
190     }
191 }
192
193
194 SGWayPoint FGRouteMgr::pop_waypoint() {
195     SGWayPoint wp;
196
197     if ( route->size() > 0 ) {
198         wp = route->get_first();
199         route->delete_first();
200     }
201
202     if ( route->size() <= 2 ) {
203         wpn_id->setStringValue( "" );
204         wpn_dist->setDoubleValue( 0.0 );
205         wpn_eta->setStringValue( "" );
206     }
207
208     if ( route->size() <= 1 ) {
209         wp1_id->setStringValue( "" );
210         wp1_dist->setDoubleValue( 0.0 );
211         wp1_eta->setStringValue( "" );
212     }
213
214     if ( route->size() <= 0 ) {
215         wp0_id->setStringValue( "" );
216         wp0_dist->setDoubleValue( 0.0 );
217         wp0_eta->setStringValue( "" );
218     }
219
220     return wp;
221 }
222
223
224 bool FGRouteMgr::build() {
225     return true;
226 }