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: ");
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(string filename,
101 bool useInitialWayPoint = true;
102 bool useCurrentWayPoint = false;
103 SGPath path( globals->get_fg_root() );
104 path.append( ("/Data/AI/FlightPlans/" + filename).c_str() );
108 readProperties(path.str(), &root);
110 SGPropertyNode * node = root.getNode("flightplan");
112 //waypoints.push_back( init_waypoint );
113 for (int i = 0; i < node->nChildren(); i++) {
114 //cout << "Reading waypoint " << i << endl;
115 waypoint* wpt = new waypoint;
116 SGPropertyNode * wpt_node = node->getChild(i);
117 wpt->name = wpt_node->getStringValue("name", "END");
118 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
119 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
120 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
121 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
122 //wpt->speed = speed;
123 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
124 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
125 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
127 if (wpt->name == "END") wpt->finished = true;
128 else wpt->finished = false;
129 waypoints.push_back(wpt);
132 catch (const sg_exception &e) {
133 //SG_LOG(SG_GENERAL, SG_ALERT,
134 // "Error reading AI flight plan: ");
135 // cout << path.str() << endl;
136 // cout << "Trying to create this plan dynamically" << endl;
137 // cout << "Route from " << dep->id << " to " << arr->id << endl;
138 create(dep,arr, alt, speed);
139 // Now that we have dynamically created a flight plan,
140 // we need to add some code that pops any waypoints already past.
143 waypoint* init_waypoint = new waypoint;
144 init_waypoint->name = string("initial position");
145 init_waypoint->latitude = lat;
146 init_waypoint->longitude = lon;
147 init_waypoint->altitude = alt;
148 init_waypoint->speed = speed;
149 init_waypoint->crossat = - 10000;
150 init_waypoint->gear_down = false;
151 init_waypoint->flaps_down = false;
152 init_waypoint->finished = false;
154 wpt_vector_iterator i = waypoints.begin();
155 while (i != waypoints.end())
157 //cerr << "Checking status of each waypoint: " << (*i)->name << endl;
158 SGWayPoint first(init_waypoint->longitude,
159 init_waypoint->latitude,
160 init_waypoint->altitude);
161 SGWayPoint curr ((*i)->longitude,
164 double crse, crsDiff;
166 first.CourseAndDistance(curr, &crse, &dist);
168 dist *= SG_METER_TO_NM;
170 // We're only interested in the absolute value of crsDiff
171 // wich should fall in the 0-180 deg range.
172 crsDiff = fabs(crse-course);
174 crsDiff = 360-crsDiff;
175 // These are the three conditions that we consider including
176 // in our flight plan:
177 // 1) current waypoint is less then 100 miles away OR
178 // 2) curren waypoint is ahead of us, at any distance
180 if ((dist > 20.0) && (crsDiff > 90.0) && ((*i)->name != string ("EOF")))
183 // Once we start including waypoints, we have to continue, even though
184 // one of the following way point would suffice.
185 // so once is the useWpt flag is set to true, we cannot reset it to false.
186 //cerr << "Discarding waypoint: " << (*i)->name
187 // << ": Course difference = " << crsDiff
188 // << "Course = " << course
189 // << "crse = " << crse << endl;
192 useCurrentWayPoint = true;
194 if (useCurrentWayPoint)
196 if ((dist > 100.0) && (useInitialWayPoint))
198 //waypoints.push_back(init_waypoint);
199 waypoints.insert(i, init_waypoint);
200 //cerr << "Using waypoint : " << init_waypoint->name << endl;
202 //waypoints.push_back( wpt );
203 //cerr << "Using waypoint : " << (*i)->name
204 // << ": course diff : " << crsDiff
205 // << "Course = " << course
206 // << "crse = " << crse << endl
207 // << "distance : " << dist << endl;
208 useInitialWayPoint = false;
215 i = waypoints.erase(i);
218 //for (i = waypoints.begin(); i != waypoints.end(); i++)
219 // cerr << "Using waypoint : " << (*i)->name << endl;
220 wpt_iterator = waypoints.begin();
221 //cout << waypoints.size() << " waypoints read." << endl;
227 FGAIFlightPlan::~FGAIFlightPlan()
233 FGAIFlightPlan::waypoint*
234 FGAIFlightPlan::getPreviousWaypoint( void )
236 if (wpt_iterator == waypoints.begin()) {
239 wpt_vector_iterator prev = wpt_iterator;
244 FGAIFlightPlan::waypoint*
245 FGAIFlightPlan::getCurrentWaypoint( void )
247 return *wpt_iterator;
250 FGAIFlightPlan::waypoint*
251 FGAIFlightPlan::getNextWaypoint( void )
253 if (wpt_iterator == waypoints.end()) {
256 wpt_vector_iterator next = wpt_iterator;
261 void FGAIFlightPlan::IncrementWaypoint( void )
266 // gives distance in feet from a position to a waypoint
267 double FGAIFlightPlan::getDistanceToGo(double lat, double lon, waypoint* wp){
268 // get size of a degree at the present latitude
269 // this won't work over large distances
270 double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
271 double ft_per_deg_lon = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
272 double lat_diff_ft = fabs(wp->latitude - lat) * ft_per_deg_lat;
273 double lon_diff_ft = fabs(wp->longitude - lon) * ft_per_deg_lon;
274 return sqrt((lat_diff_ft * lat_diff_ft) + (lon_diff_ft * lon_diff_ft));
277 // sets distance in feet from a lead point to the current waypoint
278 void FGAIFlightPlan::setLeadDistance(double speed, double bearing,
279 waypoint* current, waypoint* next){
280 double turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank
281 double inbound = bearing;
282 double outbound = getBearing(current, next);
283 double diff = fabs(inbound - outbound);
284 if (diff > 180.0) diff = 360.0 - diff;
285 lead_distance = turn_radius * sin(diff * SG_DEGREES_TO_RADIANS);
288 void FGAIFlightPlan::setLeadDistance(double distance_ft){
289 lead_distance = distance_ft;
293 double FGAIFlightPlan::getBearing(waypoint* first, waypoint* second){
294 return getBearing(first->latitude, first->longitude, second);
298 double FGAIFlightPlan::getBearing(double lat, double lon, waypoint* wp){
299 double course, distance;
300 // double latd = lat;
301 // double lond = lon;
302 // double latt = wp->latitude;
303 // double lont = wp->longitude;
304 // double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat/SG_RADIANS_TO_DEGREES);
305 // double ft_per_deg_lon = 365228.16 * cos(lat/SG_RADIANS_TO_DEGREES);
318 // double lat_diff = (latt - latd) * ft_per_deg_lat;
319 // double lon_diff = (lont - lond) * ft_per_deg_lon;
320 // double angle = atan(fabs(lat_diff / lon_diff)) * SG_RADIANS_TO_DEGREES;
322 // bool southerly = true;
323 // if (latt > latd) southerly = false;
324 // bool easterly = false;
325 // if (lont > lond) easterly = true;
326 // if (southerly && easterly) return 90.0 + angle;
327 // if (!southerly && easterly) return 90.0 - angle;
328 // if (southerly && !easterly) return 270.0 - angle;
329 // if (!southerly && !easterly) return 270.0 + angle;
330 SGWayPoint sgWp(wp->longitude,wp->latitude, wp->altitude, SGWayPoint::WGS84, string("temp"));
331 sgWp.CourseAndDistance(lon, lat, wp->altitude, &course, &distance);
333 // Omit a compiler warning.
337 /* FGAIFlightPlan::create()
338 * dynamically create a flight plan for AI traffic, based on data provided by the
339 * Traffic Manager, when reading a filed flightplan failes. (DT, 2004/07/10)
341 * Probably need to split this into separate functions for different parts of the flight
343 * once the code matures a bit more.
346 void FGAIFlightPlan::create(FGAirport *dep, FGAirport *arr, double alt, double speed)
352 //waypoints.push_back(wpt);
353 // Create the outbound taxi leg, for now simplified as a
354 // Direct route from the airport center point to the start
356 ///////////////////////////////////////////////////////////
357 //cerr << "Cruise Alt << " << alt << endl;
358 waypoint *wpt = new waypoint;
359 wpt->name = dep->id; //wpt_node->getStringValue("name", "END");
360 wpt->latitude = dep->latitude;
361 wpt->longitude = dep->longitude;
362 wpt->altitude = dep->elevation + 19; // probably need to add some model height to it
364 wpt->crossat = -10000;
365 wpt->gear_down = true;
366 wpt->flaps_down= true;
367 wpt->finished = false;
368 waypoints.push_back(wpt);
370 // Get the current active runway, based on code from David Luff
372 stationweather = ((FGEnvironmentMgr *) globals->get_subsystem("environment"))
373 ->getEnvironment(dep->latitude, dep->longitude, dep->elevation);
375 wind_speed = stationweather.get_wind_speed_kt();
376 wind_heading = stationweather.get_wind_from_heading_deg();
377 if (wind_speed == 0) {
378 wind_heading = 270; // This forces West-facing rwys to be used in no-wind situations
379 // which is consistent with Flightgear's initial setup.
382 string rwy_no = globals->get_runways()->search(dep->id, int(wind_heading));
383 if (!(globals->get_runways()->search(dep->id, (int) wind_heading, &rwy )))
385 cout << "Failed to find runway for " << dep->id << endl;
386 // Hmm, how do we handle a potential error like this?
391 double lat2, lon2, az2;
392 double heading = rwy.heading;
393 double azimuth = heading + 180.0;
394 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
395 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
396 rwy.length * SG_FEET_TO_METER * 0.5 - 5.0,
397 &lat2, &lon2, &az2 );
399 //Add the runway startpoint;
402 wpt->latitude = lat2;
403 wpt->longitude = lon2;
404 wpt->altitude = dep->elevation + 19;
406 wpt->crossat = -10000;
407 wpt->gear_down = true;
408 wpt->flaps_down= true;
409 wpt->finished = false;
410 wpt->on_ground = true;
411 waypoints.push_back(wpt);
413 //Next: The point on the runway where we begin to accelerate to take-off speed
414 //100 meters down the runway seems to work. Shorter distances cause problems with
415 // the turn with larger aircraft
416 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
417 rwy.length * SG_FEET_TO_METER * 0.5 - 105.0,
418 &lat2, &lon2, &az2 );
421 wpt->latitude = lat2;
422 wpt->longitude = lon2;
423 wpt->altitude = dep->elevation + 19;
425 wpt->crossat = -10000;
426 wpt->gear_down = true;
427 wpt->flaps_down= true;
428 wpt->finished = false;
429 wpt->on_ground = true;
430 waypoints.push_back(wpt);
436 //Next: the Start of Climb
437 geo_direct_wgs_84 ( 0, lat, lon, heading,
438 2560 * SG_FEET_TO_METER,
439 &lat2, &lon2, &az2 );
443 wpt->latitude = lat2;
444 wpt->longitude = lon2;
445 wpt->altitude = alt + 19;
447 wpt->crossat = -10000;
448 wpt->gear_down = true;
449 wpt->flaps_down= true;
450 wpt->finished = false;
451 wpt->on_ground = false;
452 waypoints.push_back(wpt);
454 //Next: the Top of Climb
455 geo_direct_wgs_84 ( 0, lat, lon, heading,
457 &lat2, &lon2, &az2 );
459 wpt->name = "10000ft climb";
460 wpt->latitude = lat2;
461 wpt->longitude = lon2;
462 wpt->altitude = 10000;
464 wpt->crossat = -10000;
465 wpt->gear_down = true;
466 wpt->flaps_down= true;
467 wpt->finished = false;
468 wpt->on_ground = false;
469 waypoints.push_back(wpt);
473 //Beginning of Decent
474 stationweather = ((FGEnvironmentMgr *)globals->get_subsystem("environment"))
475 ->getEnvironment(arr->latitude, arr->longitude, arr->elevation);
477 wind_speed = stationweather.get_wind_speed_kt();
478 wind_heading = stationweather.get_wind_from_heading_deg();
480 if (wind_speed == 0) {
481 wind_heading = 270; // This forces West-facing rwys to be used in no-wind situations
482 // which is consistent with Flightgear's initial setup.
485 rwy_no = globals->get_runways()->search(arr->id, int(wind_heading));
486 //cout << "Using runway # " << rwy_no << " for departure at " << dep->id << endl;
488 if (!(globals->get_runways()->search(arr->id, (int) wind_heading, &rwy )))
490 cout << "Failed to find runway for " << arr->id << endl;
491 // Hmm, how do we handle a potential error like this?
494 //cerr << "Done" << endl;
495 heading = rwy.heading;
496 azimuth = heading + 180.0;
497 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
501 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
503 &lat2, &lon2, &az2 );
505 wpt->name = "BOD"; //wpt_node->getStringValue("name", "END");
506 wpt->latitude = lat2;
507 wpt->longitude = lon2;
508 wpt->altitude = 10000;
510 wpt->crossat = alt +19;
511 wpt->gear_down = false;
512 wpt->flaps_down= false;
513 wpt->finished = false;
514 wpt->on_ground = false;
515 waypoints.push_back(wpt);
517 // Ten thousand ft. Slowing down to 240 kts
518 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
520 &lat2, &lon2, &az2 );
522 wpt->name = "Dec 10000ft"; //wpt_node->getStringValue("name", "END");
523 wpt->latitude = lat2;
524 wpt->longitude = lon2;
525 wpt->altitude = arr->elevation + 19;
527 wpt->crossat = 10000;
528 wpt->gear_down = false;
529 wpt->flaps_down= false;
530 wpt->finished = false;
531 wpt->on_ground = false;
532 waypoints.push_back(wpt);
534 // Three thousand ft. Slowing down to 160 kts
535 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
537 &lat2, &lon2, &az2 );
539 wpt->name = "DEC 3000ft"; //wpt_node->getStringValue("name", "END");
540 wpt->latitude = lat2;
541 wpt->longitude = lon2;
542 wpt->altitude = arr->elevation + 19;
545 wpt->gear_down = true;
546 wpt->flaps_down= true;
547 wpt->finished = false;
548 wpt->on_ground = false;
549 waypoints.push_back(wpt);
551 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
553 &lat2, &lon2, &az2 );
555 wpt->name = "Threshold"; //wpt_node->getStringValue("name", "END");
556 wpt->latitude = lat2;
557 wpt->longitude = lon2;
558 wpt->altitude = arr->elevation + 19;
560 wpt->crossat = arr->elevation + 19;
561 wpt->gear_down = true;
562 wpt->flaps_down= true;
563 wpt->finished = false;
564 wpt->on_ground = true;
565 waypoints.push_back(wpt);
567 //Full stop at the runway centerpoint
568 geo_direct_wgs_84 ( 0, rwy.lat, rwy.lon, azimuth,
570 &lat2, &lon2, &az2 );
572 wpt->name = "Center"; //wpt_node->getStringValue("name", "END");
573 wpt->latitude = rwy.lat;
574 wpt->longitude = rwy.lon;
575 wpt->altitude = arr->elevation + 19;
577 wpt->crossat = -10000;
578 wpt->gear_down = true;
579 wpt->flaps_down= true;
580 wpt->finished = false;
581 wpt->on_ground = false;
582 waypoints.push_back(wpt);
584 // Add the final destination waypoint
586 wpt->name = arr->id; //wpt_node->getStringValue("name", "END");
587 wpt->latitude = arr->latitude;
588 wpt->longitude = arr->longitude;
589 wpt->altitude = arr->elevation+19;
591 wpt->crossat = -10000;
592 wpt->gear_down = true;
593 wpt->flaps_down= true;
594 wpt->finished = false;
595 wpt->on_ground = false;
596 waypoints.push_back(wpt);
598 // And finally one more named "END"
600 wpt->name = "END"; //wpt_node->getStringValue("name", "END");
601 wpt->latitude = arr->latitude;
602 wpt->longitude = arr->longitude;
605 wpt->crossat = -10000;
606 wpt->gear_down = true;
607 wpt->flaps_down= true;
608 wpt->finished = true;
609 wpt->on_ground = true;
610 waypoints.push_back(wpt);
612 // And finally one more named "EOF"
614 wpt->name = "EOF"; //wpt_node->getStringValue("name", "END");
615 wpt->latitude = arr->latitude;
616 wpt->longitude = arr->longitude;
619 wpt->crossat = -10000;
620 wpt->gear_down = true;
621 wpt->flaps_down= true;
622 wpt->finished = true;
623 wpt->finished = true;
624 waypoints.push_back(wpt);