1 // FGAIFlightPlan - class for loading and storing AI flight plans
2 // Written by David Culp, started May 2004
3 // - davidculp2@comcast.net
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/route/waypoint.hxx>
26 #include <simgear/math/sg_geodesy.hxx>
27 #include <simgear/structure/exception.hxx>
28 #include <simgear/constants.h>
30 # define exception c_exception
32 #include <simgear/props/props.hxx>
33 #include <Main/globals.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Main/fg_init.hxx>
36 #include <Airports/simple.hxx>
37 #include <Airports/runways.hxx>
40 #include <Environment/environment_mgr.hxx>
41 #include <Environment/environment.hxx>
43 #include "AIFlightPlan.hxx"
46 FGAIFlightPlan::FGAIFlightPlan(const string& filename)
53 SGPath path( globals->get_fg_root() );
54 path.append( ("/AI/FlightPlans/" + filename).c_str() );
59 readProperties(path.str(), &root);
60 } catch (const sg_exception &e) {
61 SG_LOG(SG_GENERAL, SG_ALERT,
62 "Error reading AI flight plan: " << path.str());
63 // cout << path.str() << endl;
67 SGPropertyNode * node = root.getNode("flightplan");
68 for (i = 0; i < node->nChildren(); i++) {
69 //cout << "Reading waypoint " << i << endl;
70 waypoint* wpt = new waypoint;
71 SGPropertyNode * wpt_node = node->getChild(i);
72 wpt->name = wpt_node->getStringValue("name", "END");
73 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
74 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
75 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
76 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
77 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
78 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
79 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
80 wpt->on_ground = wpt_node->getBoolValue("on-ground", false);
82 if (wpt->name == "END") wpt->finished = true;
83 else wpt->finished = false;
85 waypoints.push_back( wpt );
88 wpt_iterator = waypoints.begin();
89 //cout << waypoints.size() << " waypoints read." << endl;
93 // This is a modified version of the constructor,
94 // Which not only reads the waypoints from a
95 // Flight plan file, but also adds the current
96 // Position computed by the traffic manager, as well
97 // as setting speeds and altitude computed by the
99 FGAIFlightPlan::FGAIFlightPlan(const std::string& p,
110 const string& fltType,
111 const string& acType,
112 const string& airline)
119 bool useInitialWayPoint = true;
120 bool useCurrentWayPoint = false;
121 SGPath path( globals->get_fg_root() );
122 path.append( "/AI/FlightPlans" );
127 // This is a bit of a hack:
128 // Normally the value of course will be used to evaluate whether
129 // or not a waypoint will be used for midair initialization of
130 // an AI aircraft. However, if a course value of 999 will be passed
131 // when an update request is received, which will by definition always be
132 // on the ground and should include all waypoints.
135 useInitialWayPoint = false;
136 useCurrentWayPoint = true;
143 readProperties(path.str(), &root);
145 SGPropertyNode * node = root.getNode("flightplan");
147 //waypoints.push_back( init_waypoint );
148 for (int i = 0; i < node->nChildren(); i++) {
149 //cout << "Reading waypoint " << i << endl;
150 waypoint* wpt = new waypoint;
151 SGPropertyNode * wpt_node = node->getChild(i);
152 wpt->name = wpt_node->getStringValue("name", "END");
153 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
154 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
155 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
156 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
157 //wpt->speed = speed;
158 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
159 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
160 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
162 if (wpt->name == "END") wpt->finished = true;
163 else wpt->finished = false;
164 waypoints.push_back(wpt);
167 catch (const sg_exception &e) {
168 SG_LOG(SG_GENERAL, SG_WARN,
169 "Error reading AI flight plan: ");
170 cerr << "Errno = " << errno << endl;
173 cerr << "Reason: No such file or directory" << endl;
179 // cout << path.str() << endl;
180 // cout << "Trying to create this plan dynamically" << endl;
181 // cout << "Route from " << dep->id << " to " << arr->id << endl;
182 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
183 time_t timeDiff = now-start;
185 if ((timeDiff > 300) && (timeDiff < 1200))
187 else if ((timeDiff >= 1200) && (timeDiff < 1500))
189 else if ((timeDiff >= 1500) && (timeDiff < 2000))
191 else if (timeDiff >= 2000)
194 //cerr << "Set leg to : " << leg << endl;
195 wpt_iterator = waypoints.begin();
196 create(dep,arr, leg, alt, speed, lat, lon,
197 firstLeg, radius, fltType, acType, airline);
198 wpt_iterator = waypoints.begin();
199 //cerr << "after create: " << (*wpt_iterator)->name << endl;
201 // Now that we have dynamically created a flight plan,
202 // we need to add some code that pops any waypoints already past.
206 waypoint* init_waypoint = new waypoint;
207 init_waypoint->name = string("initial position");
208 init_waypoint->latitude = entity->latitude;
209 init_waypoint->longitude = entity->longitude;
210 init_waypoint->altitude = entity->altitude;
211 init_waypoint->speed = entity->speed;
212 init_waypoint->crossat = - 10000;
213 init_waypoint->gear_down = false;
214 init_waypoint->flaps_down = false;
215 init_waypoint->finished = false;
217 wpt_vector_iterator i = waypoints.begin();
218 while (i != waypoints.end())
220 //cerr << "Checking status of each waypoint: " << (*i)->name << endl;
221 SGWayPoint first(init_waypoint->longitude,
222 init_waypoint->latitude,
223 init_waypoint->altitude);
224 SGWayPoint curr ((*i)->longitude,
227 double crse, crsDiff;
229 curr.CourseAndDistance(first, &crse, &dist);
231 dist *= SG_METER_TO_NM;
233 // We're only interested in the absolute value of crsDiff
234 // wich should fall in the 0-180 deg range.
235 crsDiff = fabs(crse-course);
237 crsDiff = 360-crsDiff;
238 // These are the three conditions that we consider including
239 // in our flight plan:
240 // 1) current waypoint is less then 100 miles away OR
241 // 2) curren waypoint is ahead of us, at any distance
243 if ((dist > 20.0) && (crsDiff > 90.0) && ((*i)->name != string ("EOF")))
246 // Once we start including waypoints, we have to continue, even though
247 // one of the following way point would suffice.
248 // so once is the useWpt flag is set to true, we cannot reset it to false.
249 //cerr << "Discarding waypoint: " << (*i)->name
250 // << ": Course difference = " << crsDiff
251 // << "Course = " << course
252 // << "crse = " << crse << endl;
255 useCurrentWayPoint = true;
257 if (useCurrentWayPoint)
259 if ((dist > 100.0) && (useInitialWayPoint))
261 //waypoints.push_back(init_waypoint);;
262 waypoints.insert(i, init_waypoint);
263 //cerr << "Using waypoint : " << init_waypoint->name << endl;
265 //if (useInitialWayPoint)
267 // (*i)->speed = dist; // A hack
269 //waypoints.push_back( wpt );
270 //cerr << "Using waypoint : " << (*i)->name
271 // << ": course diff : " << crsDiff
272 // << "Course = " << course
273 // << "crse = " << crse << endl
274 // << "distance : " << dist << endl;
275 useInitialWayPoint = false;
282 i = waypoints.erase(i);
287 //for (i = waypoints.begin(); i != waypoints.end(); i++)
288 // cerr << "Using waypoint : " << (*i)->name << endl;
289 //wpt_iterator = waypoints.begin();
290 //cout << waypoints.size() << " waypoints read." << endl;
296 FGAIFlightPlan::~FGAIFlightPlan()
300 //while (waypoints.begin() != waypoints.end())
302 // delete *(waypoints.begin());
303 // waypoints.erase (waypoints.begin());
310 FGAIFlightPlan::waypoint* const
311 FGAIFlightPlan::getPreviousWaypoint( void ) const
313 if (wpt_iterator == waypoints.begin()) {
316 wpt_vector_iterator prev = wpt_iterator;
321 FGAIFlightPlan::waypoint* const
322 FGAIFlightPlan::getCurrentWaypoint( void ) const
324 return *wpt_iterator;
327 FGAIFlightPlan::waypoint* const
328 FGAIFlightPlan::getNextWaypoint( void ) const
330 wpt_vector_iterator i = waypoints.end();
331 i--; // end() points to one element after the last one.
332 if (wpt_iterator == i) {
335 wpt_vector_iterator next = wpt_iterator;
340 void FGAIFlightPlan::IncrementWaypoint(bool eraseWaypoints )
344 if (wpt_iterator == waypoints.begin())
348 delete *(waypoints.begin());
349 waypoints.erase(waypoints.begin());
350 wpt_iterator = waypoints.begin();
358 // gives distance in feet from a position to a waypoint
359 double FGAIFlightPlan::getDistanceToGo(double lat, double lon, waypoint* wp) const{
360 // get size of a degree2 at the present latitude
361 // this won't work over large distances
362 double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
363 double ft_per_deg_lon = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
364 double lat_diff_ft = fabs(wp->latitude - lat) * ft_per_deg_lat;
365 double lon_diff_ft = fabs(wp->longitude - lon) * ft_per_deg_lon;
366 return sqrt((lat_diff_ft * lat_diff_ft) + (lon_diff_ft * lon_diff_ft));
369 // sets distance in feet from a lead point to the current waypoint
370 void FGAIFlightPlan::setLeadDistance(double speed, double bearing,
371 waypoint* current, waypoint* next){
374 turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank
378 double inbound = bearing;
379 double outbound = getBearing(current, next);
380 leadInAngle = fabs(inbound - outbound);
381 if (leadInAngle > 180.0)
382 leadInAngle = 360.0 - leadInAngle;
383 if (leadInAngle < 1.0) // To prevent lead_dist from getting so small it is skipped
386 lead_distance = turn_radius * sin(leadInAngle * SG_DEGREES_TO_RADIANS);
387 // if ((errno == EDOM) || (errno == ERANGE) || lead_distance < 1.0)
389 // cerr << "Lead Distance = " << lead_distance
390 // << "Diff = " << diff
391 // << "Turn Radius = " << turn_radius
392 // << "Speed = " << speed << endl;
396 void FGAIFlightPlan::setLeadDistance(double distance_ft){
397 lead_distance = distance_ft;
401 double FGAIFlightPlan::getBearing(waypoint* first, waypoint* second) const{
402 return getBearing(first->latitude, first->longitude, second);
406 double FGAIFlightPlan::getBearing(double lat, double lon, waypoint* wp) const{
407 double course, distance;
408 // double latd = lat;
409 // double lond = lon;
410 // double latt = wp->latitude;
411 // double lont = wp->longitude;
412 // double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat/SG_RADIANS_TO_DEGREES);
413 // double ft_per_deg_lon = 365228.16 * cos(lat/SG_RADIANS_TO_DEGREES);
426 // double lat_diff = (latt - latd) * ft_per_deg_lat;
427 // double lon_diff = (lont - lond) * ft_per_deg_lon;
428 // double angle = atan(fabs(lat_diff / lon_diff)) * SG_RADIANS_TO_DEGREES;
430 // bool southerly = true;
431 // if (latt > latd) southerly = false;
432 // bool easterly = false;
433 // if (lont > lond) easterly = true;
434 // if (southerly && easterly) return 90.0 + angle;
435 // if (!southerly && easterly) return 90.0 - angle;
436 // if (southerly && !easterly) return 270.0 - angle;
437 // if (!southerly && !easterly) return 270.0 + angle;
438 SGWayPoint sgWp(wp->longitude,wp->latitude, wp->altitude, SGWayPoint::WGS84, string("temp"));
439 sgWp.CourseAndDistance(lon, lat, wp->altitude, &course, &distance);
442 // Omit a compiler warning.
443 //if ((errno == EDOM) || (errno == ERANGE))
445 // cerr << "Lon: " << wp->longitude
446 // << "Lat = " << wp->latitude
447 // << "Tgt Lon = " <<
448 // << "TgT Lat = " << speed << endl;
455 void FGAIFlightPlan::deleteWaypoints()
457 for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end();i++)
462 // Delete all waypoints except the last,
463 // which we will recycle as the first waypoint in the next leg;
464 void FGAIFlightPlan::resetWaypoints()
466 if (waypoints.begin() == waypoints.end())
470 waypoint *wpt = new waypoint;
471 wpt_vector_iterator i = waypoints.end();
473 wpt->name = (*i)->name;
474 wpt->latitude = (*i)->latitude;
475 wpt->longitude = (*i)->longitude;
476 wpt->altitude = (*i)->altitude;
477 wpt->speed = (*i)->speed;
478 wpt->crossat = (*i)->crossat;
479 wpt->gear_down = (*i)->gear_down;
480 wpt->flaps_down= (*i)->flaps_down;
481 wpt->finished = false;
482 wpt->on_ground = (*i)->on_ground;
483 //cerr << "Recycling waypoint " << wpt->name << endl;
485 waypoints.push_back(wpt);
489 // Start flightplan over from the beginning
490 void FGAIFlightPlan::restart()
492 wpt_iterator = waypoints.begin();