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)
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);
81 if (wpt->name == "END") wpt->finished = true;
82 else wpt->finished = false;
84 waypoints.push_back( wpt );
87 wpt_iterator = waypoints.begin();
88 //cout << waypoints.size() << " waypoints read." << endl;
92 // This is a modified version of the constructor,
93 // Which not only reads the waypoints from a
94 // Flight plan file, but also adds the current
95 // Position computed by the traffic manager, as well
96 // as setting speeds and altitude computed by the
98 FGAIFlightPlan::FGAIFlightPlan(const std::string& p,
109 const string& fltType,
110 const string& acType,
111 const string& airline)
117 bool useInitialWayPoint = true;
118 bool useCurrentWayPoint = false;
119 SGPath path( globals->get_fg_root() );
120 path.append( "/AI/FlightPlans" );
125 // This is a bit of a hack:
126 // Normally the value of course will be used to evaluate whether
127 // or not a waypoint will be used for midair initialization of
128 // an AI aircraft. However, if a course value of 999 will be passed
129 // when an update request is received, which will by definition always be
130 // on the ground and should include all waypoints.
133 useInitialWayPoint = false;
134 useCurrentWayPoint = true;
141 readProperties(path.str(), &root);
143 SGPropertyNode * node = root.getNode("flightplan");
145 //waypoints.push_back( init_waypoint );
146 for (int i = 0; i < node->nChildren(); i++) {
147 //cout << "Reading waypoint " << i << endl;
148 waypoint* wpt = new waypoint;
149 SGPropertyNode * wpt_node = node->getChild(i);
150 wpt->name = wpt_node->getStringValue("name", "END");
151 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
152 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
153 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
154 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
155 //wpt->speed = speed;
156 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
157 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
158 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
160 if (wpt->name == "END") wpt->finished = true;
161 else wpt->finished = false;
162 waypoints.push_back(wpt);
165 catch (const sg_exception &e) {
166 SG_LOG(SG_GENERAL, SG_WARN,
167 "Error reading AI flight plan: ");
168 cerr << "Errno = " << errno << endl;
171 cerr << "Reason: No such file or directory" << endl;
177 // cout << path.str() << endl;
178 // cout << "Trying to create this plan dynamically" << endl;
179 // cout << "Route from " << dep->id << " to " << arr->id << endl;
180 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
181 time_t timeDiff = now-start;
183 if ((timeDiff > 300) && (timeDiff < 1200))
185 else if ((timeDiff >= 1200) && (timeDiff < 1500))
187 else if ((timeDiff >= 1500) && (timeDiff < 2000))
189 else if (timeDiff >= 2000)
192 //cerr << "Set leg to : " << leg << endl;
193 wpt_iterator = waypoints.begin();
194 create(dep,arr, leg, alt, speed, lat, lon,
195 firstLeg, radius, fltType, acType, airline);
196 wpt_iterator = waypoints.begin();
197 //cerr << "after create: " << (*wpt_iterator)->name << endl;
199 // Now that we have dynamically created a flight plan,
200 // we need to add some code that pops any waypoints already past.
204 waypoint* init_waypoint = new waypoint;
205 init_waypoint->name = string("initial position");
206 init_waypoint->latitude = entity->latitude;
207 init_waypoint->longitude = entity->longitude;
208 init_waypoint->altitude = entity->altitude;
209 init_waypoint->speed = entity->speed;
210 init_waypoint->crossat = - 10000;
211 init_waypoint->gear_down = false;
212 init_waypoint->flaps_down = false;
213 init_waypoint->finished = false;
215 wpt_vector_iterator i = waypoints.begin();
216 while (i != waypoints.end())
218 //cerr << "Checking status of each waypoint: " << (*i)->name << endl;
219 SGWayPoint first(init_waypoint->longitude,
220 init_waypoint->latitude,
221 init_waypoint->altitude);
222 SGWayPoint curr ((*i)->longitude,
225 double crse, crsDiff;
227 curr.CourseAndDistance(first, &crse, &dist);
229 dist *= SG_METER_TO_NM;
231 // We're only interested in the absolute value of crsDiff
232 // wich should fall in the 0-180 deg range.
233 crsDiff = fabs(crse-course);
235 crsDiff = 360-crsDiff;
236 // These are the three conditions that we consider including
237 // in our flight plan:
238 // 1) current waypoint is less then 100 miles away OR
239 // 2) curren waypoint is ahead of us, at any distance
241 if ((dist > 20.0) && (crsDiff > 90.0) && ((*i)->name != string ("EOF")))
244 // Once we start including waypoints, we have to continue, even though
245 // one of the following way point would suffice.
246 // so once is the useWpt flag is set to true, we cannot reset it to false.
247 //cerr << "Discarding waypoint: " << (*i)->name
248 // << ": Course difference = " << crsDiff
249 // << "Course = " << course
250 // << "crse = " << crse << endl;
253 useCurrentWayPoint = true;
255 if (useCurrentWayPoint)
257 if ((dist > 100.0) && (useInitialWayPoint))
259 //waypoints.push_back(init_waypoint);;
260 waypoints.insert(i, init_waypoint);
261 //cerr << "Using waypoint : " << init_waypoint->name << endl;
263 //if (useInitialWayPoint)
265 // (*i)->speed = dist; // A hack
267 //waypoints.push_back( wpt );
268 //cerr << "Using waypoint : " << (*i)->name
269 // << ": course diff : " << crsDiff
270 // << "Course = " << course
271 // << "crse = " << crse << endl
272 // << "distance : " << dist << endl;
273 useInitialWayPoint = false;
280 i = waypoints.erase(i);
285 //for (i = waypoints.begin(); i != waypoints.end(); i++)
286 // cerr << "Using waypoint : " << (*i)->name << endl;
287 //wpt_iterator = waypoints.begin();
288 //cout << waypoints.size() << " waypoints read." << endl;
294 FGAIFlightPlan::~FGAIFlightPlan()
298 //while (waypoints.begin() != waypoints.end())
300 // delete *(waypoints.begin());
301 // waypoints.erase (waypoints.begin());
306 FGAIFlightPlan::waypoint* const
307 FGAIFlightPlan::getPreviousWaypoint( void ) const
309 if (wpt_iterator == waypoints.begin()) {
312 wpt_vector_iterator prev = wpt_iterator;
317 FGAIFlightPlan::waypoint* const
318 FGAIFlightPlan::getCurrentWaypoint( void ) const
320 return *wpt_iterator;
323 FGAIFlightPlan::waypoint* const
324 FGAIFlightPlan::getNextWaypoint( void ) const
326 wpt_vector_iterator i = waypoints.end();
327 i--; // end() points to one element after the last one.
328 if (wpt_iterator == i) {
331 wpt_vector_iterator next = wpt_iterator;
336 void FGAIFlightPlan::IncrementWaypoint(bool eraseWaypoints )
340 if (wpt_iterator == waypoints.begin())
344 delete *(waypoints.begin());
345 waypoints.erase(waypoints.begin());
346 wpt_iterator = waypoints.begin();
354 // gives distance in feet from a position to a waypoint
355 double FGAIFlightPlan::getDistanceToGo(double lat, double lon, waypoint* wp) const{
356 // get size of a degree2 at the present latitude
357 // this won't work over large distances
358 double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
359 double ft_per_deg_lon = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
360 double lat_diff_ft = fabs(wp->latitude - lat) * ft_per_deg_lat;
361 double lon_diff_ft = fabs(wp->longitude - lon) * ft_per_deg_lon;
362 return sqrt((lat_diff_ft * lat_diff_ft) + (lon_diff_ft * lon_diff_ft));
365 // sets distance in feet from a lead point to the current waypoint
366 void FGAIFlightPlan::setLeadDistance(double speed, double bearing,
367 waypoint* current, waypoint* next){
370 turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank
374 double inbound = bearing;
375 double outbound = getBearing(current, next);
376 leadInAngle = fabs(inbound - outbound);
377 if (leadInAngle > 180.0)
378 leadInAngle = 360.0 - leadInAngle;
379 if (leadInAngle < 1.0) // To prevent lead_dist from getting so small it is skipped
382 lead_distance = turn_radius * sin(leadInAngle * SG_DEGREES_TO_RADIANS);
383 // if ((errno == EDOM) || (errno == ERANGE) || lead_distance < 1.0)
385 // cerr << "Lead Distance = " << lead_distance
386 // << "Diff = " << diff
387 // << "Turn Radius = " << turn_radius
388 // << "Speed = " << speed << endl;
392 void FGAIFlightPlan::setLeadDistance(double distance_ft){
393 lead_distance = distance_ft;
397 double FGAIFlightPlan::getBearing(waypoint* first, waypoint* second) const{
398 return getBearing(first->latitude, first->longitude, second);
402 double FGAIFlightPlan::getBearing(double lat, double lon, waypoint* wp) const{
403 double course, distance;
404 // double latd = lat;
405 // double lond = lon;
406 // double latt = wp->latitude;
407 // double lont = wp->longitude;
408 // double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat/SG_RADIANS_TO_DEGREES);
409 // double ft_per_deg_lon = 365228.16 * cos(lat/SG_RADIANS_TO_DEGREES);
422 // double lat_diff = (latt - latd) * ft_per_deg_lat;
423 // double lon_diff = (lont - lond) * ft_per_deg_lon;
424 // double angle = atan(fabs(lat_diff / lon_diff)) * SG_RADIANS_TO_DEGREES;
426 // bool southerly = true;
427 // if (latt > latd) southerly = false;
428 // bool easterly = false;
429 // if (lont > lond) easterly = true;
430 // if (southerly && easterly) return 90.0 + angle;
431 // if (!southerly && easterly) return 90.0 - angle;
432 // if (southerly && !easterly) return 270.0 - angle;
433 // if (!southerly && !easterly) return 270.0 + angle;
434 SGWayPoint sgWp(wp->longitude,wp->latitude, wp->altitude, SGWayPoint::WGS84, string("temp"));
435 sgWp.CourseAndDistance(lon, lat, wp->altitude, &course, &distance);
438 // Omit a compiler warning.
439 //if ((errno == EDOM) || (errno == ERANGE))
441 // cerr << "Lon: " << wp->longitude
442 // << "Lat = " << wp->latitude
443 // << "Tgt Lon = " <<
444 // << "TgT Lat = " << speed << endl;
451 void FGAIFlightPlan::deleteWaypoints()
453 for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end();i++)
458 // Delete all waypoints except the last,
459 // which we will recycle as the first waypoint in the next leg;
460 void FGAIFlightPlan::resetWaypoints()
462 if (waypoints.begin() == waypoints.end())
466 waypoint *wpt = new waypoint;
467 wpt_vector_iterator i = waypoints.end();
469 wpt->name = (*i)->name;
470 wpt->latitude = (*i)->latitude;
471 wpt->longitude = (*i)->longitude;
472 wpt->altitude = (*i)->altitude;
473 wpt->speed = (*i)->speed;
474 wpt->crossat = (*i)->crossat;
475 wpt->gear_down = (*i)->gear_down;
476 wpt->flaps_down= (*i)->flaps_down;
477 wpt->finished = false;
478 wpt->on_ground = (*i)->on_ground;
479 //cerr << "Recycling waypoint " << wpt->name << endl;
481 waypoints.push_back(wpt);
485 // Start flightplan over from the beginning
486 void FGAIFlightPlan::restart()
488 wpt_iterator = waypoints.begin();