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., 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() :
55 FGRouteMgr::~FGRouteMgr() {
60 void FGRouteMgr::init() {
61 lon = fgGetNode( "/position/longitude-deg", true );
62 lat = fgGetNode( "/position/latitude-deg", true );
63 alt = fgGetNode( "/position/altitude-ft", true );
65 true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
67 wp0_id = fgGetNode( "/autopilot/route-manager/wp[0]/id", true );
68 wp0_dist = fgGetNode( "/autopilot/route-manager/wp[0]/dist", true );
69 wp0_eta = fgGetNode( "/autopilot/route-manager/wp[0]/eta", true );
71 wp1_id = fgGetNode( "/autopilot/route-manager/wp[1]/id", true );
72 wp1_dist = fgGetNode( "/autopilot/route-manager/wp[1]/dist", true );
73 wp1_eta = fgGetNode( "/autopilot/route-manager/wp[1]/eta", true );
75 wpn_id = fgGetNode( "/autopilot/route-manager/wp-last/id", true );
76 wpn_dist = fgGetNode( "/autopilot/route-manager/wp-last/dist", true );
77 wpn_eta = fgGetNode( "/autopilot/route-manager/wp-last/eta", true );
83 void FGRouteMgr::bind() { }
84 void FGRouteMgr::unbind() { }
87 static double get_ground_speed() {
88 // starts in ft/s so we convert to kts
89 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
91 double ft_s = cur_fdm_state->get_V_ground_speed()
92 * speedup_node->getIntValue();
93 double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
99 void FGRouteMgr::update( double dt ) {
101 double wp_course, wp_distance;
105 if ( route->size() > 0 ) {
106 SGWayPoint wp = route->get_waypoint( 0 );
107 wp.CourseAndDistance( lon->getDoubleValue(), lat->getDoubleValue(),
108 alt->getDoubleValue(), &wp_course, &wp_distance );
110 true_hdg_deg->setDoubleValue( wp_course );
112 if ( wp_distance < 200.0 ) {
118 if ( route->size() > 0 ) {
119 SGWayPoint wp = route->get_waypoint( 0 );
120 // update the property tree info
122 wp0_id->setStringValue( wp.get_id().c_str() );
124 accum += wp_distance;
125 wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
127 double eta = accum * SG_METER_TO_NM / get_ground_speed();
128 if ( eta >= 100.0 ) { eta = 99.999; }
130 if ( eta < (1.0/6.0) ) {
131 // within 10 minutes, bump up to min/secs
135 minor = (int)((eta - (int)eta) * 60.0);
136 snprintf( eta_str, 128, "%d:%02d", major, minor );
137 wp0_eta->setStringValue( eta_str );
141 if ( route->size() > 1 ) {
142 SGWayPoint wp = route->get_waypoint( 1 );
144 // update the property tree info
146 wp1_id->setStringValue( wp.get_id().c_str() );
148 accum += wp.get_distance();
149 wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
151 double eta = accum * SG_METER_TO_NM / get_ground_speed();
152 if ( eta >= 100.0 ) { eta = 99.999; }
154 if ( eta < (1.0/6.0) ) {
155 // within 10 minutes, bump up to min/secs
159 minor = (int)((eta - (int)eta) * 60.0);
160 snprintf( eta_str, 128, "%d:%02d", major, minor );
161 wp1_eta->setStringValue( eta_str );
164 // summarize remaining way points
165 if ( route->size() > 2 ) {
167 for ( int i = 2; i < route->size(); ++i ) {
168 wp = route->get_waypoint( i );
169 accum += wp.get_distance();
172 // update the property tree info
174 wpn_id->setStringValue( wp.get_id().c_str() );
176 wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
178 double eta = accum * SG_METER_TO_NM / get_ground_speed();
179 if ( eta >= 100.0 ) { eta = 99.999; }
181 if ( eta < (1.0/6.0) ) {
182 // within 10 minutes, bump up to min/secs
186 minor = (int)((eta - (int)eta) * 60.0);
187 snprintf( eta_str, 128, "%d:%02d", major, minor );
188 wpn_eta->setStringValue( eta_str );
193 SGWayPoint FGRouteMgr::pop_waypoint() {
196 if ( route->size() > 0 ) {
197 wp = route->get_first();
198 route->delete_first();
201 if ( route->size() <= 2 ) {
202 wpn_id->setStringValue( "" );
203 wpn_dist->setDoubleValue( 0.0 );
204 wpn_eta->setStringValue( "" );
207 if ( route->size() <= 1 ) {
208 wp1_id->setStringValue( "" );
209 wp1_dist->setDoubleValue( 0.0 );
210 wp1_eta->setStringValue( "" );
213 if ( route->size() <= 0 ) {
214 wp0_id->setStringValue( "" );
215 wp0_dist->setDoubleValue( 0.0 );
216 wp0_eta->setStringValue( "" );
223 bool FGRouteMgr::build() {