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>
29 #include <simgear/props/props.hxx>
30 #include <simgear/props/props_io.hxx>
32 #include <Main/globals.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Main/fg_init.hxx>
35 #include <Airports/simple.hxx>
36 #include <Airports/runways.hxx>
39 #include <Environment/environment_mgr.hxx>
40 #include <Environment/environment.hxx>
42 #include "AIFlightPlan.hxx"
45 FGAIFlightPlan::FGAIFlightPlan(const string& filename)
52 SGPath path( globals->get_fg_root() );
53 path.append( ("/AI/FlightPlans/" + filename).c_str() );
58 readProperties(path.str(), &root);
59 } catch (const sg_exception &e) {
60 SG_LOG(SG_GENERAL, SG_ALERT,
61 "Error reading AI flight plan: " << path.str());
62 // cout << path.str() << endl;
66 SGPropertyNode * node = root.getNode("flightplan");
67 for (i = 0; i < node->nChildren(); i++) {
68 //cout << "Reading waypoint " << i << endl;
69 waypoint* wpt = new waypoint;
70 SGPropertyNode * wpt_node = node->getChild(i);
71 wpt->name = wpt_node->getStringValue("name", "END");
72 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
73 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
74 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
75 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
76 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
77 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
78 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
79 wpt->on_ground = wpt_node->getBoolValue("on-ground", false);
80 wpt->time_sec = wpt_node->getDoubleValue("time-sec", 0);
81 wpt->time = wpt_node->getStringValue("time", "");
83 if (wpt->name == "END") wpt->finished = true;
84 else wpt->finished = false;
86 waypoints.push_back( wpt );
89 wpt_iterator = waypoints.begin();
90 //cout << waypoints.size() << " waypoints read." << endl;
94 // This is a modified version of the constructor,
95 // Which not only reads the waypoints from a
96 // Flight plan file, but also adds the current
97 // Position computed by the traffic manager, as well
98 // as setting speeds and altitude computed by the
100 FGAIFlightPlan::FGAIFlightPlan(const std::string& p,
111 const string& fltType,
112 const string& acType,
113 const string& airline)
120 bool useInitialWayPoint = true;
121 bool useCurrentWayPoint = false;
122 SGPath path( globals->get_fg_root() );
123 path.append( "/AI/FlightPlans" );
128 // This is a bit of a hack:
129 // Normally the value of course will be used to evaluate whether
130 // or not a waypoint will be used for midair initialization of
131 // an AI aircraft. However, if a course value of 999 will be passed
132 // when an update request is received, which will by definition always be
133 // on the ground and should include all waypoints.
136 useInitialWayPoint = false;
137 useCurrentWayPoint = true;
144 readProperties(path.str(), &root);
146 SGPropertyNode * node = root.getNode("flightplan");
148 //waypoints.push_back( init_waypoint );
149 for (int i = 0; i < node->nChildren(); i++) {
150 //cout << "Reading waypoint " << i << endl;
151 waypoint* wpt = new waypoint;
152 SGPropertyNode * wpt_node = node->getChild(i);
153 wpt->name = wpt_node->getStringValue("name", "END");
154 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
155 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
156 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
157 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
158 //wpt->speed = speed;
159 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
160 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
161 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
163 if (wpt->name == "END") wpt->finished = true;
164 else wpt->finished = false;
165 waypoints.push_back(wpt);
168 catch (const sg_exception &e) {
169 SG_LOG(SG_GENERAL, SG_WARN,
170 "Error reading AI flight plan: ");
171 cerr << "Errno = " << errno << endl;
174 SG_LOG(SG_GENERAL, SG_WARN, "Reason: No such file or directory");
180 // cout << path.str() << endl;
181 // cout << "Trying to create this plan dynamically" << endl;
182 // cout << "Route from " << dep->id << " to " << arr->id << endl;
183 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
184 time_t timeDiff = now-start;
187 if ((timeDiff > 300) && (timeDiff < 1200))
189 else if ((timeDiff >= 1200) && (timeDiff < 1500))
191 else if ((timeDiff >= 1500) && (timeDiff < 2000))
193 else if (timeDiff >= 2000)
196 if (timeDiff >= 2000)
199 SG_LOG(SG_GENERAL, SG_INFO, "Route from " << dep->getId() << " to " << arr->getId() << ". Set leg to : " << leg);
200 wpt_iterator = waypoints.begin();
201 create(dep,arr, leg, alt, speed, lat, lon,
202 firstLeg, radius, fltType, acType, airline);
203 wpt_iterator = waypoints.begin();
204 //cerr << "after create: " << (*wpt_iterator)->name << endl;
206 // Now that we have dynamically created a flight plan,
207 // we need to add some code that pops any waypoints already past.
211 waypoint* init_waypoint = new waypoint;
212 init_waypoint->name = string("initial position");
213 init_waypoint->latitude = entity->latitude;
214 init_waypoint->longitude = entity->longitude;
215 init_waypoint->altitude = entity->altitude;
216 init_waypoint->speed = entity->speed;
217 init_waypoint->crossat = - 10000;
218 init_waypoint->gear_down = false;
219 init_waypoint->flaps_down = false;
220 init_waypoint->finished = false;
222 wpt_vector_iterator i = waypoints.begin();
223 while (i != waypoints.end())
225 //cerr << "Checking status of each waypoint: " << (*i)->name << endl;
226 SGWayPoint first(init_waypoint->longitude,
227 init_waypoint->latitude,
228 init_waypoint->altitude);
229 SGWayPoint curr ((*i)->longitude,
232 double crse, crsDiff;
234 curr.CourseAndDistance(first, &crse, &dist);
236 dist *= SG_METER_TO_NM;
238 // We're only interested in the absolute value of crsDiff
239 // wich should fall in the 0-180 deg range.
240 crsDiff = fabs(crse-course);
242 crsDiff = 360-crsDiff;
243 // These are the three conditions that we consider including
244 // in our flight plan:
245 // 1) current waypoint is less then 100 miles away OR
246 // 2) curren waypoint is ahead of us, at any distance
248 if ((dist > 20.0) && (crsDiff > 90.0) && ((*i)->name != string ("EOF")))
251 // Once we start including waypoints, we have to continue, even though
252 // one of the following way point would suffice.
253 // so once is the useWpt flag is set to true, we cannot reset it to false.
254 //cerr << "Discarding waypoint: " << (*i)->name
255 // << ": Course difference = " << crsDiff
256 // << "Course = " << course
257 // << "crse = " << crse << endl;
260 useCurrentWayPoint = true;
262 if (useCurrentWayPoint)
264 if ((dist > 100.0) && (useInitialWayPoint))
266 //waypoints.push_back(init_waypoint);;
267 waypoints.insert(i, init_waypoint);
268 //cerr << "Using waypoint : " << init_waypoint->name << endl;
270 //if (useInitialWayPoint)
272 // (*i)->speed = dist; // A hack
274 //waypoints.push_back( wpt );
275 //cerr << "Using waypoint : " << (*i)->name
276 // << ": course diff : " << crsDiff
277 // << "Course = " << course
278 // << "crse = " << crse << endl
279 // << "distance : " << dist << endl;
280 useInitialWayPoint = false;
287 i = waypoints.erase(i);
292 //for (i = waypoints.begin(); i != waypoints.end(); i++)
293 // cerr << "Using waypoint : " << (*i)->name << endl;
294 //wpt_iterator = waypoints.begin();
295 //cout << waypoints.size() << " waypoints read." << endl;
301 FGAIFlightPlan::~FGAIFlightPlan()
308 FGAIFlightPlan::waypoint* const
309 FGAIFlightPlan::getPreviousWaypoint( void ) const
311 if (wpt_iterator == waypoints.begin()) {
314 wpt_vector_iterator prev = wpt_iterator;
319 FGAIFlightPlan::waypoint* const
320 FGAIFlightPlan::getCurrentWaypoint( void ) const
322 return *wpt_iterator;
325 FGAIFlightPlan::waypoint* const
326 FGAIFlightPlan::getNextWaypoint( void ) const
328 wpt_vector_iterator i = waypoints.end();
329 i--; // end() points to one element after the last one.
330 if (wpt_iterator == i) {
333 wpt_vector_iterator next = wpt_iterator;
338 void FGAIFlightPlan::IncrementWaypoint(bool eraseWaypoints )
342 if (wpt_iterator == waypoints.begin())
346 delete *(waypoints.begin());
347 waypoints.erase(waypoints.begin());
348 wpt_iterator = waypoints.begin();
357 // gives distance in feet from a position to a waypoint
358 double FGAIFlightPlan::getDistanceToGo(double lat, double lon, waypoint* wp) const{
359 double course, distance;
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));
367 SGWayPoint sgWp(wp->longitude,wp->latitude, wp->altitude, SGWayPoint::WGS84, string("temp"));
368 sgWp.CourseAndDistance(lon, lat, wp->altitude, &course, &distance);
372 // sets distance in feet from a lead point to the current waypoint
373 void FGAIFlightPlan::setLeadDistance(double speed, double bearing,
374 waypoint* current, waypoint* next){
376 // Handle Ground steering
377 // At a turn rate of 30 degrees per second, it takes 12 seconds to do a full 360 degree turn
378 // So, to get an estimate of the turn radius, calculate the cicumference of the circle
379 // we travel on. Get the turn radius by dividing by PI (*2).
381 turn_radius = ((360/30)*15) / (2*M_PI);
383 turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank
385 double inbound = bearing;
386 double outbound = getBearing(current, next);
387 leadInAngle = fabs(inbound - outbound);
388 if (leadInAngle > 180.0)
389 leadInAngle = 360.0 - leadInAngle;
390 //if (leadInAngle < 30.0) // To prevent lead_dist from getting so small it is skipped
391 // leadInAngle = 30.0;
393 //lead_distance = turn_radius * sin(leadInAngle * SG_DEGREES_TO_RADIANS);
394 lead_distance = turn_radius * tan((leadInAngle * SG_DEGREES_TO_RADIANS)/2);
395 // if ((errno == EDOM) || (errno == ERANGE) || lead_distance < 1.0)
400 void FGAIFlightPlan::setLeadDistance(double distance_ft){
401 lead_distance = distance_ft;
405 double FGAIFlightPlan::getBearing(waypoint* first, waypoint* second) const{
406 return getBearing(first->latitude, first->longitude, second);
410 double FGAIFlightPlan::getBearing(double lat, double lon, waypoint* wp) const{
411 double course, distance;
412 // double latd = lat;
413 // double lond = lon;
414 // double latt = wp->latitude;
415 // double lont = wp->longitude;
416 // double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat/SG_RADIANS_TO_DEGREES);
417 // double ft_per_deg_lon = 365228.16 * cos(lat/SG_RADIANS_TO_DEGREES);
430 // double lat_diff = (latt - latd) * ft_per_deg_lat;
431 // double lon_diff = (lont - lond) * ft_per_deg_lon;
432 // double angle = atan(fabs(lat_diff / lon_diff)) * SG_RADIANS_TO_DEGREES;
434 // bool southerly = true;
435 // if (latt > latd) southerly = false;
436 // bool easterly = false;
437 // if (lont > lond) easterly = true;
438 // if (southerly && easterly) return 90.0 + angle;
439 // if (!southerly && easterly) return 90.0 - angle;
440 // if (southerly && !easterly) return 270.0 - angle;
441 // if (!southerly && !easterly) return 270.0 + angle;
442 SGWayPoint sgWp(wp->longitude,wp->latitude, wp->altitude, SGWayPoint::WGS84, string("temp"));
443 sgWp.CourseAndDistance(lon, lat, wp->altitude, &course, &distance);
446 // Omit a compiler warning.
447 //if ((errno == EDOM) || (errno == ERANGE))
449 // cerr << "Lon: " << wp->longitude
450 // << "Lat = " << wp->latitude
451 // << "Tgt Lon = " <<
452 // << "TgT Lat = " << speed << endl;
459 void FGAIFlightPlan::deleteWaypoints()
461 for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end();i++)
466 // Delete all waypoints except the last,
467 // which we will recycle as the first waypoint in the next leg;
468 void FGAIFlightPlan::resetWaypoints()
470 if (waypoints.begin() == waypoints.end())
474 waypoint *wpt = new waypoint;
475 wpt_vector_iterator i = waypoints.end();
477 wpt->name = (*i)->name;
478 wpt->latitude = (*i)->latitude;
479 wpt->longitude = (*i)->longitude;
480 wpt->altitude = (*i)->altitude;
481 wpt->speed = (*i)->speed;
482 wpt->crossat = (*i)->crossat;
483 wpt->gear_down = (*i)->gear_down;
484 wpt->flaps_down= (*i)->flaps_down;
485 wpt->finished = false;
486 wpt->on_ground = (*i)->on_ground;
487 //cerr << "Recycling waypoint " << wpt->name << endl;
489 waypoints.push_back(wpt);
493 // Start flightplan over from the beginning
494 void FGAIFlightPlan::restart()
496 wpt_iterator = waypoints.begin();
500 void FGAIFlightPlan::deleteTaxiRoute()
507 int FGAIFlightPlan::getRouteIndex(int i) {
508 if ((i > 0) && (i < waypoints.size())) {
509 return waypoints[i]->routeIndex;