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(string filename)
47 SGPath path( globals->get_fg_root() );
48 path.append( ("/Data/AI/FlightPlans/" + filename).c_str() );
52 readProperties(path.str(), &root);
53 } catch (const sg_exception &e) {
54 SG_LOG(SG_GENERAL, SG_ALERT,
55 "Error reading AI flight plan: " << path.str());
56 // cout << path.str() << endl;
60 SGPropertyNode * node = root.getNode("flightplan");
61 for (i = 0; i < node->nChildren(); i++) {
62 //cout << "Reading waypoint " << i << endl;
63 waypoint* wpt = new waypoint;
64 SGPropertyNode * wpt_node = node->getChild(i);
65 wpt->name = wpt_node->getStringValue("name", "END");
66 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
67 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
68 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
69 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
70 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
71 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
72 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
73 wpt->on_ground = wpt_node->getBoolValue("on-ground", false);
75 if (wpt->name == "END") wpt->finished = true;
76 else wpt->finished = false;
78 waypoints.push_back( wpt );
81 wpt_iterator = waypoints.begin();
82 //cout << waypoints.size() << " waypoints read." << endl;
86 // This is a modified version of the constructor,
87 // Which not only reads the waypoints from a
88 // Flight plan file, but also adds the current
89 // Position computed by the traffic manager, as well
90 // as setting speeds and altitude computed by the
92 FGAIFlightPlan::FGAIFlightPlan(FGAIModelEntity *entity,
97 bool useInitialWayPoint = true;
98 bool useCurrentWayPoint = false;
99 SGPath path( globals->get_fg_root() );
100 path.append( "/Data/AI/FlightPlans" );
101 path.append( entity->path );
105 readProperties(path.str(), &root);
107 SGPropertyNode * node = root.getNode("flightplan");
109 //waypoints.push_back( init_waypoint );
110 for (int i = 0; i < node->nChildren(); i++) {
111 //cout << "Reading waypoint " << i << endl;
112 waypoint* wpt = new waypoint;
113 SGPropertyNode * wpt_node = node->getChild(i);
114 wpt->name = wpt_node->getStringValue("name", "END");
115 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
116 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
117 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
118 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
119 //wpt->speed = speed;
120 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
121 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
122 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
124 if (wpt->name == "END") wpt->finished = true;
125 else wpt->finished = false;
126 waypoints.push_back(wpt);
129 catch (const sg_exception &e) {
130 //SG_LOG(SG_GENERAL, SG_ALERT,
131 // "Error reading AI flight plan: ");
132 // cout << path.str() << endl;
133 // cout << "Trying to create this plan dynamically" << endl;
134 // cout << "Route from " << dep->id << " to " << arr->id << endl;
135 create(dep,arr, entity->altitude, entity->speed);
136 // Now that we have dynamically created a flight plan,
137 // we need to add some code that pops any waypoints already past.
140 waypoint* init_waypoint = new waypoint;
141 init_waypoint->name = string("initial position");
142 init_waypoint->latitude = entity->latitude;
143 init_waypoint->longitude = entity->longitude;
144 init_waypoint->altitude = entity->altitude;
145 init_waypoint->speed = entity->speed;
146 init_waypoint->crossat = - 10000;
147 init_waypoint->gear_down = false;
148 init_waypoint->flaps_down = false;
149 init_waypoint->finished = false;
151 wpt_vector_iterator i = waypoints.begin();
152 while (i != waypoints.end())
154 //cerr << "Checking status of each waypoint: " << (*i)->name << endl;
155 SGWayPoint first(init_waypoint->longitude,
156 init_waypoint->latitude,
157 init_waypoint->altitude);
158 SGWayPoint curr ((*i)->longitude,
161 double crse, crsDiff;
163 first.CourseAndDistance(curr, &crse, &dist);
165 dist *= SG_METER_TO_NM;
167 // We're only interested in the absolute value of crsDiff
168 // wich should fall in the 0-180 deg range.
169 crsDiff = fabs(crse-course);
171 crsDiff = 360-crsDiff;
172 // These are the three conditions that we consider including
173 // in our flight plan:
174 // 1) current waypoint is less then 100 miles away OR
175 // 2) curren waypoint is ahead of us, at any distance
177 if ((dist > 20.0) && (crsDiff > 90.0) && ((*i)->name != string ("EOF")))
180 // Once we start including waypoints, we have to continue, even though
181 // one of the following way point would suffice.
182 // so once is the useWpt flag is set to true, we cannot reset it to false.
183 //cerr << "Discarding waypoint: " << (*i)->name
184 // << ": Course difference = " << crsDiff
185 // << "Course = " << course
186 // << "crse = " << crse << endl;
189 useCurrentWayPoint = true;
191 if (useCurrentWayPoint)
193 if ((dist > 100.0) && (useInitialWayPoint))
195 //waypoints.push_back(init_waypoint);
196 waypoints.insert(i, init_waypoint);
197 //cerr << "Using waypoint : " << init_waypoint->name << endl;
199 //waypoints.push_back( wpt );
200 //cerr << "Using waypoint : " << (*i)->name
201 // << ": course diff : " << crsDiff
202 // << "Course = " << course
203 // << "crse = " << crse << endl
204 // << "distance : " << dist << endl;
205 useInitialWayPoint = false;
212 i = waypoints.erase(i);
215 //for (i = waypoints.begin(); i != waypoints.end(); i++)
216 // cerr << "Using waypoint : " << (*i)->name << endl;
217 wpt_iterator = waypoints.begin();
218 //cout << waypoints.size() << " waypoints read." << endl;
224 FGAIFlightPlan::~FGAIFlightPlan()
230 FGAIFlightPlan::waypoint*
231 FGAIFlightPlan::getPreviousWaypoint( void )
233 if (wpt_iterator == waypoints.begin()) {
236 wpt_vector_iterator prev = wpt_iterator;
241 FGAIFlightPlan::waypoint*
242 FGAIFlightPlan::getCurrentWaypoint( void )
244 return *wpt_iterator;
247 FGAIFlightPlan::waypoint*
248 FGAIFlightPlan::getNextWaypoint( void )
250 if (wpt_iterator == waypoints.end()) {
253 wpt_vector_iterator next = wpt_iterator;
258 void FGAIFlightPlan::IncrementWaypoint( void )
263 // gives distance in feet from a position to a waypoint
264 double FGAIFlightPlan::getDistanceToGo(double lat, double lon, waypoint* wp){
265 // get size of a degree at the present latitude
266 // this won't work over large distances
267 double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
268 double ft_per_deg_lon = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
269 double lat_diff_ft = fabs(wp->latitude - lat) * ft_per_deg_lat;
270 double lon_diff_ft = fabs(wp->longitude - lon) * ft_per_deg_lon;
271 return sqrt((lat_diff_ft * lat_diff_ft) + (lon_diff_ft * lon_diff_ft));
274 // sets distance in feet from a lead point to the current waypoint
275 void FGAIFlightPlan::setLeadDistance(double speed, double bearing,
276 waypoint* current, waypoint* next){
277 double turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank
278 double inbound = bearing;
279 double outbound = getBearing(current, next);
280 double diff = fabs(inbound - outbound);
281 if (diff > 180.0) diff = 360.0 - diff;
282 lead_distance = turn_radius * sin(diff * SG_DEGREES_TO_RADIANS);
285 void FGAIFlightPlan::setLeadDistance(double distance_ft){
286 lead_distance = distance_ft;
290 double FGAIFlightPlan::getBearing(waypoint* first, waypoint* second){
291 return getBearing(first->latitude, first->longitude, second);
295 double FGAIFlightPlan::getBearing(double lat, double lon, waypoint* wp){
296 double course, distance;
297 // double latd = lat;
298 // double lond = lon;
299 // double latt = wp->latitude;
300 // double lont = wp->longitude;
301 // double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat/SG_RADIANS_TO_DEGREES);
302 // double ft_per_deg_lon = 365228.16 * cos(lat/SG_RADIANS_TO_DEGREES);
315 // double lat_diff = (latt - latd) * ft_per_deg_lat;
316 // double lon_diff = (lont - lond) * ft_per_deg_lon;
317 // double angle = atan(fabs(lat_diff / lon_diff)) * SG_RADIANS_TO_DEGREES;
319 // bool southerly = true;
320 // if (latt > latd) southerly = false;
321 // bool easterly = false;
322 // if (lont > lond) easterly = true;
323 // if (southerly && easterly) return 90.0 + angle;
324 // if (!southerly && easterly) return 90.0 - angle;
325 // if (southerly && !easterly) return 270.0 - angle;
326 // if (!southerly && !easterly) return 270.0 + angle;
327 SGWayPoint sgWp(wp->longitude,wp->latitude, wp->altitude, SGWayPoint::WGS84, string("temp"));
328 sgWp.CourseAndDistance(lon, lat, wp->altitude, &course, &distance);
330 // Omit a compiler warning.
334 /* FGAIFlightPlan::create()
335 * dynamically create a flight plan for AI traffic, based on data provided by the
336 * Traffic Manager, when reading a filed flightplan failes. (DT, 2004/07/10)
338 * Probably need to split this into separate functions for different parts of the flight
340 * once the code matures a bit more.
343 void FGAIFlightPlan::create(FGAirport *dep, FGAirport *arr, double alt, double speed)
349 //waypoints.push_back(wpt);
350 // Create the outbound taxi leg, for now simplified as a
351 // Direct route from the airport center point to the start
353 ///////////////////////////////////////////////////////////
354 //cerr << "Cruise Alt << " << alt << endl;
355 waypoint *wpt = new waypoint;
356 wpt->name = dep->id; //wpt_node->getStringValue("name", "END");
357 wpt->latitude = dep->latitude;
358 wpt->longitude = dep->longitude;
359 wpt->altitude = dep->elevation + 19; // probably need to add some model height to it
361 wpt->crossat = -10000;
362 wpt->gear_down = true;
363 wpt->flaps_down= true;
364 wpt->finished = false;
365 waypoints.push_back(wpt);
367 // Get the current active runway, based on code from David Luff
369 stationweather = ((FGEnvironmentMgr *) globals->get_subsystem("environment"))
370 ->getEnvironment(dep->latitude, dep->longitude, dep->elevation);
372 wind_speed = stationweather.get_wind_speed_kt();
373 wind_heading = stationweather.get_wind_from_heading_deg();
374 if (wind_speed == 0) {
375 wind_heading = 270; // This forces West-facing rwys to be used in no-wind situations
376 // which is consistent with Flightgear's initial setup.
379 string rwy_no = globals->get_runways()->search(dep->id, int(wind_heading));
380 if (!(globals->get_runways()->search(dep->id, (int) wind_heading, &rwy )))
382 cout << "Failed to find runway for " << dep->id << endl;
383 // Hmm, how do we handle a potential error like this?
388 double lat2, lon2, az2;
389 double heading = rwy.heading;
390 double azimuth = heading + 180.0;
391 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
392 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
393 rwy.length * SG_FEET_TO_METER * 0.5 - 5.0,
394 &lat2, &lon2, &az2 );
396 //Add the runway startpoint;
399 wpt->latitude = lat2;
400 wpt->longitude = lon2;
401 wpt->altitude = dep->elevation + 19;
403 wpt->crossat = -10000;
404 wpt->gear_down = true;
405 wpt->flaps_down= true;
406 wpt->finished = false;
407 wpt->on_ground = true;
408 waypoints.push_back(wpt);
410 //Next: The point on the runway where we begin to accelerate to take-off speed
411 //100 meters down the runway seems to work. Shorter distances cause problems with
412 // the turn with larger aircraft
413 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
414 rwy.length * SG_FEET_TO_METER * 0.5 - 105.0,
415 &lat2, &lon2, &az2 );
418 wpt->latitude = lat2;
419 wpt->longitude = lon2;
420 wpt->altitude = dep->elevation + 19;
422 wpt->crossat = -10000;
423 wpt->gear_down = true;
424 wpt->flaps_down= true;
425 wpt->finished = false;
426 wpt->on_ground = true;
427 waypoints.push_back(wpt);
433 //Next: the Start of Climb
434 geo_direct_wgs_84 ( 0, lat, lon, heading,
435 2560 * SG_FEET_TO_METER,
436 &lat2, &lon2, &az2 );
440 wpt->latitude = lat2;
441 wpt->longitude = lon2;
442 wpt->altitude = alt + 19;
444 wpt->crossat = -10000;
445 wpt->gear_down = true;
446 wpt->flaps_down= true;
447 wpt->finished = false;
448 wpt->on_ground = false;
449 waypoints.push_back(wpt);
451 //Next: the Top of Climb
452 geo_direct_wgs_84 ( 0, lat, lon, heading,
454 &lat2, &lon2, &az2 );
456 wpt->name = "10000ft climb";
457 wpt->latitude = lat2;
458 wpt->longitude = lon2;
459 wpt->altitude = 10000;
461 wpt->crossat = -10000;
462 wpt->gear_down = true;
463 wpt->flaps_down= true;
464 wpt->finished = false;
465 wpt->on_ground = false;
466 waypoints.push_back(wpt);
470 //Beginning of Decent
471 stationweather = ((FGEnvironmentMgr *)globals->get_subsystem("environment"))
472 ->getEnvironment(arr->latitude, arr->longitude, arr->elevation);
474 wind_speed = stationweather.get_wind_speed_kt();
475 wind_heading = stationweather.get_wind_from_heading_deg();
477 if (wind_speed == 0) {
478 wind_heading = 270; // This forces West-facing rwys to be used in no-wind situations
479 // which is consistent with Flightgear's initial setup.
482 rwy_no = globals->get_runways()->search(arr->id, int(wind_heading));
483 //cout << "Using runway # " << rwy_no << " for departure at " << dep->id << endl;
485 if (!(globals->get_runways()->search(arr->id, (int) wind_heading, &rwy )))
487 cout << "Failed to find runway for " << arr->id << endl;
488 // Hmm, how do we handle a potential error like this?
491 //cerr << "Done" << endl;
492 heading = rwy.heading;
493 azimuth = heading + 180.0;
494 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
498 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
500 &lat2, &lon2, &az2 );
502 wpt->name = "BOD"; //wpt_node->getStringValue("name", "END");
503 wpt->latitude = lat2;
504 wpt->longitude = lon2;
505 wpt->altitude = 10000;
507 wpt->crossat = alt +19;
508 wpt->gear_down = false;
509 wpt->flaps_down= false;
510 wpt->finished = false;
511 wpt->on_ground = false;
512 waypoints.push_back(wpt);
514 // Ten thousand ft. Slowing down to 240 kts
515 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
517 &lat2, &lon2, &az2 );
519 wpt->name = "Dec 10000ft"; //wpt_node->getStringValue("name", "END");
520 wpt->latitude = lat2;
521 wpt->longitude = lon2;
522 wpt->altitude = arr->elevation + 19;
524 wpt->crossat = 10000;
525 wpt->gear_down = false;
526 wpt->flaps_down= false;
527 wpt->finished = false;
528 wpt->on_ground = false;
529 waypoints.push_back(wpt);
531 // Three thousand ft. Slowing down to 160 kts
532 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
534 &lat2, &lon2, &az2 );
536 wpt->name = "DEC 3000ft"; //wpt_node->getStringValue("name", "END");
537 wpt->latitude = lat2;
538 wpt->longitude = lon2;
539 wpt->altitude = arr->elevation + 19;
542 wpt->gear_down = true;
543 wpt->flaps_down= true;
544 wpt->finished = false;
545 wpt->on_ground = false;
546 waypoints.push_back(wpt);
548 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
550 &lat2, &lon2, &az2 );
552 wpt->name = "Threshold"; //wpt_node->getStringValue("name", "END");
553 wpt->latitude = lat2;
554 wpt->longitude = lon2;
555 wpt->altitude = arr->elevation + 19;
557 wpt->crossat = arr->elevation + 19;
558 wpt->gear_down = true;
559 wpt->flaps_down= true;
560 wpt->finished = false;
561 wpt->on_ground = true;
562 waypoints.push_back(wpt);
564 //Full stop at the runway centerpoint
565 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
567 &lat2, &lon2, &az2 );
569 wpt->name = "Center"; //wpt_node->getStringValue("name", "END");
570 wpt->latitude = rwy.lat;
571 wpt->longitude = rwy.lon;
572 wpt->altitude = arr->elevation + 19;
574 wpt->crossat = -10000;
575 wpt->gear_down = true;
576 wpt->flaps_down= true;
577 wpt->finished = false;
578 wpt->on_ground = false;
579 waypoints.push_back(wpt);
581 // Add the final destination waypoint
583 wpt->name = arr->id; //wpt_node->getStringValue("name", "END");
584 wpt->latitude = arr->latitude;
585 wpt->longitude = arr->longitude;
586 wpt->altitude = arr->elevation+19;
588 wpt->crossat = -10000;
589 wpt->gear_down = true;
590 wpt->flaps_down= true;
591 wpt->finished = false;
592 wpt->on_ground = false;
593 waypoints.push_back(wpt);
595 // And finally one more named "END"
597 wpt->name = "END"; //wpt_node->getStringValue("name", "END");
598 wpt->latitude = arr->latitude;
599 wpt->longitude = arr->longitude;
602 wpt->crossat = -10000;
603 wpt->gear_down = true;
604 wpt->flaps_down= true;
605 wpt->finished = true;
606 wpt->on_ground = true;
607 waypoints.push_back(wpt);
609 // And finally one more named "EOF"
611 wpt->name = "EOF"; //wpt_node->getStringValue("name", "END");
612 wpt->latitude = arr->latitude;
613 wpt->longitude = arr->longitude;
616 wpt->crossat = -10000;
617 wpt->gear_down = true;
618 wpt->flaps_down= true;
619 wpt->finished = true;
620 wpt->finished = true;
621 waypoints.push_back(wpt);