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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <simgear/misc/sg_path.hxx>
22 #include <simgear/debug/logstream.hxx>
23 #include <simgear/route/waypoint.hxx>
24 #include <simgear/math/sg_geodesy.hxx>
25 #include <simgear/structure/exception.hxx>
26 #include <simgear/constants.h>
28 # define exception c_exception
30 #include <simgear/props/props.hxx>
31 #include <Main/globals.hxx>
32 #include <Main/fg_props.hxx>
33 #include <Main/fg_init.hxx>
34 #include <Airports/simple.hxx>
35 #include <Airports/runways.hxx>
38 #include <Environment/environment_mgr.hxx>
39 #include <Environment/environment.hxx>
41 #include "AIFlightPlan.hxx"
44 FGAIFlightPlan::FGAIFlightPlan(const string& filename)
50 SGPath path( globals->get_fg_root() );
51 path.append( ("/Data/AI/FlightPlans/" + filename).c_str() );
56 readProperties(path.str(), &root);
57 } catch (const sg_exception &e) {
58 SG_LOG(SG_GENERAL, SG_ALERT,
59 "Error reading AI flight plan: " << path.str());
60 // cout << path.str() << endl;
64 SGPropertyNode * node = root.getNode("flightplan");
65 for (i = 0; i < node->nChildren(); i++) {
66 //cout << "Reading waypoint " << i << endl;
67 waypoint* wpt = new waypoint;
68 SGPropertyNode * wpt_node = node->getChild(i);
69 wpt->name = wpt_node->getStringValue("name", "END");
70 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
71 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
72 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
73 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
74 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
75 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
76 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
77 wpt->on_ground = wpt_node->getBoolValue("on-ground", false);
79 if (wpt->name == "END") wpt->finished = true;
80 else wpt->finished = false;
82 waypoints.push_back( wpt );
85 wpt_iterator = waypoints.begin();
86 //cout << waypoints.size() << " waypoints read." << endl;
90 // This is a modified version of the constructor,
91 // Which not only reads the waypoints from a
92 // Flight plan file, but also adds the current
93 // Position computed by the traffic manager, as well
94 // as setting speeds and altitude computed by the
96 FGAIFlightPlan::FGAIFlightPlan(FGAIModelEntity *entity,
103 const string& fltType,
104 const string& acType,
105 const string& airline)
110 bool useInitialWayPoint = true;
111 bool useCurrentWayPoint = false;
112 SGPath path( globals->get_fg_root() );
113 path.append( "/Data/AI/FlightPlans" );
114 path.append( entity->path );
117 // This is a bit of a hack:
118 // Normally the value of course will be used to evaluate whether
119 // or not a waypoint will be used for midair initialization of
120 // an AI aircraft. However, if a course value of 999 will be passed
121 // when an update request is received, which will by definition always be
122 // on the ground and should include all waypoints.
125 useInitialWayPoint = false;
126 useCurrentWayPoint = true;
133 readProperties(path.str(), &root);
135 SGPropertyNode * node = root.getNode("flightplan");
137 //waypoints.push_back( init_waypoint );
138 for (int i = 0; i < node->nChildren(); i++) {
139 //cout << "Reading waypoint " << i << endl;
140 waypoint* wpt = new waypoint;
141 SGPropertyNode * wpt_node = node->getChild(i);
142 wpt->name = wpt_node->getStringValue("name", "END");
143 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
144 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
145 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
146 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
147 //wpt->speed = speed;
148 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
149 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
150 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
152 if (wpt->name == "END") wpt->finished = true;
153 else wpt->finished = false;
154 waypoints.push_back(wpt);
157 catch (const sg_exception &e) {
158 SG_LOG(SG_GENERAL, SG_ALERT,
159 "Error reading AI flight plan: ");
160 cerr << "Errno = " << errno << endl;
163 cerr << "Reason: No such file or directory" << endl;
169 // cout << path.str() << endl;
170 // cout << "Trying to create this plan dynamically" << endl;
171 // cout << "Route from " << dep->id << " to " << arr->id << endl;
172 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
173 time_t timeDiff = now-start;
175 if ((timeDiff > 300) && (timeDiff < 1200))
177 else if ((timeDiff >= 1200) && (timeDiff < 1500))
179 else if ((timeDiff >= 1500) && (timeDiff < 2000))
181 else if (timeDiff >= 2000)
184 //cerr << "Set leg to : " << leg << endl;
185 wpt_iterator = waypoints.begin();
186 create(dep,arr, leg, entity->altitude, entity->speed, entity->latitude, entity->longitude,
187 firstLeg, radius, fltType, acType, airline);
188 wpt_iterator = waypoints.begin();
189 //cerr << "after create: " << (*wpt_iterator)->name << endl;
191 // Now that we have dynamically created a flight plan,
192 // we need to add some code that pops any waypoints already past.
196 waypoint* init_waypoint = new waypoint;
197 init_waypoint->name = string("initial position");
198 init_waypoint->latitude = entity->latitude;
199 init_waypoint->longitude = entity->longitude;
200 init_waypoint->altitude = entity->altitude;
201 init_waypoint->speed = entity->speed;
202 init_waypoint->crossat = - 10000;
203 init_waypoint->gear_down = false;
204 init_waypoint->flaps_down = false;
205 init_waypoint->finished = false;
207 wpt_vector_iterator i = waypoints.begin();
208 while (i != waypoints.end())
210 //cerr << "Checking status of each waypoint: " << (*i)->name << endl;
211 SGWayPoint first(init_waypoint->longitude,
212 init_waypoint->latitude,
213 init_waypoint->altitude);
214 SGWayPoint curr ((*i)->longitude,
217 double crse, crsDiff;
219 curr.CourseAndDistance(first, &crse, &dist);
221 dist *= SG_METER_TO_NM;
223 // We're only interested in the absolute value of crsDiff
224 // wich should fall in the 0-180 deg range.
225 crsDiff = fabs(crse-course);
227 crsDiff = 360-crsDiff;
228 // These are the three conditions that we consider including
229 // in our flight plan:
230 // 1) current waypoint is less then 100 miles away OR
231 // 2) curren waypoint is ahead of us, at any distance
233 if ((dist > 20.0) && (crsDiff > 90.0) && ((*i)->name != string ("EOF")))
236 // Once we start including waypoints, we have to continue, even though
237 // one of the following way point would suffice.
238 // so once is the useWpt flag is set to true, we cannot reset it to false.
239 //cerr << "Discarding waypoint: " << (*i)->name
240 // << ": Course difference = " << crsDiff
241 // << "Course = " << course
242 // << "crse = " << crse << endl;
245 useCurrentWayPoint = true;
247 if (useCurrentWayPoint)
249 if ((dist > 100.0) && (useInitialWayPoint))
251 //waypoints.push_back(init_waypoint);;
252 waypoints.insert(i, init_waypoint);
253 //cerr << "Using waypoint : " << init_waypoint->name << endl;
255 //if (useInitialWayPoint)
257 // (*i)->speed = dist; // A hack
259 //waypoints.push_back( wpt );
260 //cerr << "Using waypoint : " << (*i)->name
261 // << ": course diff : " << crsDiff
262 // << "Course = " << course
263 // << "crse = " << crse << endl
264 // << "distance : " << dist << endl;
265 useInitialWayPoint = false;
272 i = waypoints.erase(i);
277 //for (i = waypoints.begin(); i != waypoints.end(); i++)
278 // cerr << "Using waypoint : " << (*i)->name << endl;
279 //wpt_iterator = waypoints.begin();
280 //cout << waypoints.size() << " waypoints read." << endl;
286 FGAIFlightPlan::~FGAIFlightPlan()
290 //while (waypoints.begin() != waypoints.end())
292 // delete *(waypoints.begin());
293 // waypoints.erase (waypoints.begin());
298 FGAIFlightPlan::waypoint* const
299 FGAIFlightPlan::getPreviousWaypoint( void ) const
301 if (wpt_iterator == waypoints.begin()) {
304 wpt_vector_iterator prev = wpt_iterator;
309 FGAIFlightPlan::waypoint* const
310 FGAIFlightPlan::getCurrentWaypoint( void ) const
312 return *wpt_iterator;
315 FGAIFlightPlan::waypoint* const
316 FGAIFlightPlan::getNextWaypoint( void ) const
318 wpt_vector_iterator i = waypoints.end();
319 i--; // end() points to one element after the last one.
320 if (wpt_iterator == i) {
323 wpt_vector_iterator next = wpt_iterator;
328 void FGAIFlightPlan::IncrementWaypoint(bool eraseWaypoints )
332 if (wpt_iterator == waypoints.begin())
336 delete *(waypoints.begin());
337 waypoints.erase(waypoints.begin());
338 wpt_iterator = waypoints.begin();
346 // gives distance in feet from a position to a waypoint
347 double FGAIFlightPlan::getDistanceToGo(double lat, double lon, waypoint* wp) const{
348 // get size of a degree2 at the present latitude
349 // this won't work over large distances
350 double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
351 double ft_per_deg_lon = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
352 double lat_diff_ft = fabs(wp->latitude - lat) * ft_per_deg_lat;
353 double lon_diff_ft = fabs(wp->longitude - lon) * ft_per_deg_lon;
354 return sqrt((lat_diff_ft * lat_diff_ft) + (lon_diff_ft * lon_diff_ft));
357 // sets distance in feet from a lead point to the current waypoint
358 void FGAIFlightPlan::setLeadDistance(double speed, double bearing,
359 waypoint* current, waypoint* next){
362 turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank
366 double inbound = bearing;
367 double outbound = getBearing(current, next);
368 leadInAngle = fabs(inbound - outbound);
369 if (leadInAngle > 180.0)
370 leadInAngle = 360.0 - leadInAngle;
371 if (leadInAngle < 1.0) // To prevent lead_dist from getting so small it is skipped
374 lead_distance = turn_radius * sin(leadInAngle * SG_DEGREES_TO_RADIANS);
375 // if ((errno == EDOM) || (errno == ERANGE) || lead_distance < 1.0)
377 // cerr << "Lead Distance = " << lead_distance
378 // << "Diff = " << diff
379 // << "Turn Radius = " << turn_radius
380 // << "Speed = " << speed << endl;
384 void FGAIFlightPlan::setLeadDistance(double distance_ft){
385 lead_distance = distance_ft;
389 double FGAIFlightPlan::getBearing(waypoint* first, waypoint* second) const{
390 return getBearing(first->latitude, first->longitude, second);
394 double FGAIFlightPlan::getBearing(double lat, double lon, waypoint* wp) const{
395 double course, distance;
396 // double latd = lat;
397 // double lond = lon;
398 // double latt = wp->latitude;
399 // double lont = wp->longitude;
400 // double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat/SG_RADIANS_TO_DEGREES);
401 // double ft_per_deg_lon = 365228.16 * cos(lat/SG_RADIANS_TO_DEGREES);
414 // double lat_diff = (latt - latd) * ft_per_deg_lat;
415 // double lon_diff = (lont - lond) * ft_per_deg_lon;
416 // double angle = atan(fabs(lat_diff / lon_diff)) * SG_RADIANS_TO_DEGREES;
418 // bool southerly = true;
419 // if (latt > latd) southerly = false;
420 // bool easterly = false;
421 // if (lont > lond) easterly = true;
422 // if (southerly && easterly) return 90.0 + angle;
423 // if (!southerly && easterly) return 90.0 - angle;
424 // if (southerly && !easterly) return 270.0 - angle;
425 // if (!southerly && !easterly) return 270.0 + angle;
426 SGWayPoint sgWp(wp->longitude,wp->latitude, wp->altitude, SGWayPoint::WGS84, string("temp"));
427 sgWp.CourseAndDistance(lon, lat, wp->altitude, &course, &distance);
430 // Omit a compiler warning.
431 //if ((errno == EDOM) || (errno == ERANGE))
433 // cerr << "Lon: " << wp->longitude
434 // << "Lat = " << wp->latitude
435 // << "Tgt Lon = " <<
436 // << "TgT Lat = " << speed << endl;
443 void FGAIFlightPlan::deleteWaypoints()
445 for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end();i++)
450 // Delete all waypoints except the last,
451 // which we will recycle as the first waypoint in the next leg;
452 void FGAIFlightPlan::resetWaypoints()
454 if (waypoints.begin() == waypoints.end())
458 waypoint *wpt = new waypoint;
459 wpt_vector_iterator i = waypoints.end();
461 wpt->name = (*i)->name;
462 wpt->latitude = (*i)->latitude;
463 wpt->longitude = (*i)->longitude;
464 wpt->altitude = (*i)->altitude;
465 wpt->speed = (*i)->speed;
466 wpt->crossat = (*i)->crossat;
467 wpt->gear_down = (*i)->gear_down;
468 wpt->flaps_down= (*i)->flaps_down;
469 wpt->finished = false;
470 wpt->on_ground = (*i)->on_ground;
471 //cerr << "Recycling waypoint " << wpt->name << endl;
473 waypoints.push_back(wpt);
477 // Start flightplan over from the beginning
478 void FGAIFlightPlan::restart()
480 wpt_iterator = waypoints.begin();