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