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.
24 #include <FDM/flight.hxx>
25 #include <Main/fg_props.hxx>
27 #include "route_mgr.hxx"
30 FGRouteMgr::FGRouteMgr() :
46 cout << "route = " << route << endl;
50 FGRouteMgr::~FGRouteMgr() {
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 );
60 true_hdg_deg = fgGetNode( "/autopilot/settings/true-heading-deg", true );
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 );
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 );
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 );
78 void FGRouteMgr::bind() { }
79 void FGRouteMgr::unbind() { }
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");
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;
94 void FGRouteMgr::update( double dt ) {
96 double wp_course, wp_distance;
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 );
105 true_hdg_deg->setDoubleValue( wp_course );
107 if ( wp_distance < 200.0 ) {
113 if ( route->size() > 0 ) {
114 SGWayPoint wp = route->get_waypoint( 0 );
115 // update the property tree info
117 wp0_id->setStringValue( wp.get_id().c_str() );
119 accum += wp_distance;
120 wp0_dist->setDoubleValue( accum * SG_METER_TO_NM );
122 double eta = accum * SG_METER_TO_NM / get_ground_speed();
123 if ( eta >= 100.0 ) { eta = 99.999; }
125 if ( eta < (1.0/6.0) ) {
126 // within 10 minutes, bump up to min/secs
130 minor = (int)((eta - (int)eta) * 60.0);
131 snprintf( eta_str, 128, "%d:%02d", major, minor );
132 wp0_eta->setStringValue( eta_str );
136 if ( route->size() > 1 ) {
137 SGWayPoint wp = route->get_waypoint( 1 );
139 // update the property tree info
141 wp1_id->setStringValue( wp.get_id().c_str() );
143 accum += wp.get_distance();
144 wp1_dist->setDoubleValue( accum * SG_METER_TO_NM );
146 double eta = accum * SG_METER_TO_NM / get_ground_speed();
147 if ( eta >= 100.0 ) { eta = 99.999; }
149 if ( eta < (1.0/6.0) ) {
150 // within 10 minutes, bump up to min/secs
154 minor = (int)((eta - (int)eta) * 60.0);
155 snprintf( eta_str, 128, "%d:%02d", major, minor );
156 wp1_eta->setStringValue( eta_str );
159 // summarize remaining way points
160 if ( route->size() > 2 ) {
162 for ( int i = 2; i < route->size(); ++i ) {
163 wp = route->get_waypoint( i );
164 accum += wp.get_distance();
167 // update the property tree info
169 wpn_id->setStringValue( wp.get_id().c_str() );
171 wpn_dist->setDoubleValue( accum * SG_METER_TO_NM );
173 double eta = accum * SG_METER_TO_NM / get_ground_speed();
174 if ( eta >= 100.0 ) { eta = 99.999; }
176 if ( eta < (1.0/6.0) ) {
177 // within 10 minutes, bump up to min/secs
181 minor = (int)((eta - (int)eta) * 60.0);
182 snprintf( eta_str, 128, "%d:%02d", major, minor );
183 wpn_eta->setStringValue( eta_str );
188 SGWayPoint FGRouteMgr::pop_waypoint() {
191 if ( route->size() > 0 ) {
192 wp = route->get_first();
193 route->delete_first();
196 if ( route->size() <= 2 ) {
197 wpn_id->setStringValue( "" );
198 wpn_dist->setDoubleValue( 0.0 );
199 wpn_eta->setStringValue( "" );
202 if ( route->size() <= 1 ) {
203 wp1_id->setStringValue( "" );
204 wp1_dist->setDoubleValue( 0.0 );
205 wp1_eta->setStringValue( "" );
208 if ( route->size() <= 0 ) {
209 wp0_id->setStringValue( "" );
210 wp0_dist->setDoubleValue( 0.0 );
211 wp0_eta->setStringValue( "" );
218 bool FGRouteMgr::build() {