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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <simgear/compiler.h>
30 #include <FDM/flight.hxx>
31 #include <Main/fg_props.hxx>
33 #include "route_mgr.hxx"
36 FGRouteMgr::FGRouteMgr() :
52 cout << "route = " << route << endl;
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::bind() { }
85 void FGRouteMgr::unbind() { }
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");
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;
100 void FGRouteMgr::update( double dt ) {
102 double wp_course, wp_distance;
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 );
111 true_hdg_deg->setDoubleValue( wp_course );
113 if ( wp_distance < 200.0 ) {
119 if ( route->size() > 0 ) {
120 SGWayPoint wp = route->get_waypoint( 0 );
121 // update the property tree info
123 wp0_id->setStringValue( wp.get_id().c_str() );
125 accum += wp_distance;
126 wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
128 double eta = accum * SG_METER_TO_NM / get_ground_speed();
129 if ( eta >= 100.0 ) { eta = 99.999; }
131 if ( eta < (1.0/6.0) ) {
132 // within 10 minutes, bump up to min/secs
136 minor = (int)((eta - (int)eta) * 60.0);
137 snprintf( eta_str, 128, "%d:%02d", major, minor );
138 wp0_eta->setStringValue( eta_str );
142 if ( route->size() > 1 ) {
143 SGWayPoint wp = route->get_waypoint( 1 );
145 // update the property tree info
147 wp1_id->setStringValue( wp.get_id().c_str() );
149 accum += wp.get_distance();
150 wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
152 double eta = accum * SG_METER_TO_NM / get_ground_speed();
153 if ( eta >= 100.0 ) { eta = 99.999; }
155 if ( eta < (1.0/6.0) ) {
156 // within 10 minutes, bump up to min/secs
160 minor = (int)((eta - (int)eta) * 60.0);
161 snprintf( eta_str, 128, "%d:%02d", major, minor );
162 wp1_eta->setStringValue( eta_str );
165 // summarize remaining way points
166 if ( route->size() > 2 ) {
168 for ( int i = 2; i < route->size(); ++i ) {
169 wp = route->get_waypoint( i );
170 accum += wp.get_distance();
173 // update the property tree info
175 wpn_id->setStringValue( wp.get_id().c_str() );
177 wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
179 double eta = accum * SG_METER_TO_NM / get_ground_speed();
180 if ( eta >= 100.0 ) { eta = 99.999; }
182 if ( eta < (1.0/6.0) ) {
183 // within 10 minutes, bump up to min/secs
187 minor = (int)((eta - (int)eta) * 60.0);
188 snprintf( eta_str, 128, "%d:%02d", major, minor );
189 wpn_eta->setStringValue( eta_str );
194 SGWayPoint FGRouteMgr::pop_waypoint() {
197 if ( route->size() > 0 ) {
198 wp = route->get_first();
199 route->delete_first();
202 if ( route->size() <= 2 ) {
203 wpn_id->setStringValue( "" );
204 wpn_dist->setDoubleValue( 0.0 );
205 wpn_eta->setStringValue( "" );
208 if ( route->size() <= 1 ) {
209 wp1_id->setStringValue( "" );
210 wp1_dist->setDoubleValue( 0.0 );
211 wp1_eta->setStringValue( "" );
214 if ( route->size() <= 0 ) {
215 wp0_id->setStringValue( "" );
216 wp0_dist->setDoubleValue( 0.0 );
217 wp0_eta->setStringValue( "" );
224 bool FGRouteMgr::build() {