1 // route_mgr.cxx - manage a route (i.e. a collection of waypoints)
3 // Written by Curtis Olson, started January 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 #include <simgear/compiler.h>
30 #include <FDM/flight.hxx>
31 #include <Main/fg_props.hxx>
33 #include "auto_gui.hxx" // FIXME temporary dependency (NewWaypoint)
34 #include "route_mgr.hxx"
37 FGRouteMgr::FGRouteMgr() :
56 FGRouteMgr::~FGRouteMgr() {
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 );
66 true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
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 );
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 );
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 );
84 void FGRouteMgr::postinit() {
85 string_list *waypoints = globals->get_initial_waypoints();
89 vector<string>::iterator it;
90 for (it = waypoints->begin(); it != waypoints->end(); ++it)
95 void FGRouteMgr::bind() { }
96 void FGRouteMgr::unbind() { }
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");
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;
111 void FGRouteMgr::update( double dt ) {
113 double wp_course, wp_distance;
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 );
122 true_hdg_deg->setDoubleValue( wp_course );
124 if ( wp_distance < 200.0 ) {
130 if ( route->size() > 0 ) {
131 SGWayPoint wp = route->get_waypoint( 0 );
132 // update the property tree info
134 wp0_id->setStringValue( wp.get_id().c_str() );
136 accum += wp_distance;
137 wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
139 double eta = accum * SG_METER_TO_NM / get_ground_speed();
140 if ( eta >= 100.0 ) { eta = 99.999; }
142 if ( eta < (1.0/6.0) ) {
143 // within 10 minutes, bump up to min/secs
147 minor = (int)((eta - (int)eta) * 60.0);
148 snprintf( eta_str, 128, "%d:%02d", major, minor );
149 wp0_eta->setStringValue( eta_str );
153 if ( route->size() > 1 ) {
154 SGWayPoint wp = route->get_waypoint( 1 );
156 // update the property tree info
158 wp1_id->setStringValue( wp.get_id().c_str() );
160 accum += wp.get_distance();
161 wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
163 double eta = accum * SG_METER_TO_NM / get_ground_speed();
164 if ( eta >= 100.0 ) { eta = 99.999; }
166 if ( eta < (1.0/6.0) ) {
167 // within 10 minutes, bump up to min/secs
171 minor = (int)((eta - (int)eta) * 60.0);
172 snprintf( eta_str, 128, "%d:%02d", major, minor );
173 wp1_eta->setStringValue( eta_str );
176 // summarize remaining way points
177 if ( route->size() > 2 ) {
179 for ( int i = 2; i < route->size(); ++i ) {
180 wp = route->get_waypoint( i );
181 accum += wp.get_distance();
184 // update the property tree info
186 wpn_id->setStringValue( wp.get_id().c_str() );
188 wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
190 double eta = accum * SG_METER_TO_NM / get_ground_speed();
191 if ( eta >= 100.0 ) { eta = 99.999; }
193 if ( eta < (1.0/6.0) ) {
194 // within 10 minutes, bump up to min/secs
198 minor = (int)((eta - (int)eta) * 60.0);
199 snprintf( eta_str, 128, "%d:%02d", major, minor );
200 wpn_eta->setStringValue( eta_str );
205 SGWayPoint FGRouteMgr::pop_waypoint() {
208 if ( route->size() > 0 ) {
209 wp = route->get_first();
210 route->delete_first();
213 if ( route->size() <= 2 ) {
214 wpn_id->setStringValue( "" );
215 wpn_dist->setDoubleValue( 0.0 );
216 wpn_eta->setStringValue( "" );
219 if ( route->size() <= 1 ) {
220 wp1_id->setStringValue( "" );
221 wp1_dist->setDoubleValue( 0.0 );
222 wp1_eta->setStringValue( "" );
225 if ( route->size() <= 0 ) {
226 wp0_id->setStringValue( "" );
227 wp0_dist->setDoubleValue( 0.0 );
228 wp0_eta->setStringValue( "" );
235 bool FGRouteMgr::build() {