1 /******************************************************************************
2 * AIFlightPlanCreate.cxx
3 * Written by Durk Talsma, started May, 2004.
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.
19 **************************************************************************/
20 #include "AIFlightPlan.hxx"
21 #include <simgear/math/sg_geodesy.hxx>
22 #include <Airports/runways.hxx>
24 #include <Environment/environment_mgr.hxx>
25 #include <Environment/environment.hxx>
28 /* FGAIFlightPlan::create()
29 * dynamically create a flight plan for AI traffic, based on data provided by the
30 * Traffic Manager, when reading a filed flightplan failes. (DT, 2004/07/10)
32 * This is the top-level function, and the only one that is publicly available.
37 // Check lat/lon values during initialization;
38 void FGAIFlightPlan::create(FGAirport *dep, FGAirport *arr, int legNr,
39 double alt, double speed, double latitude,
40 double longitude, bool firstFlight,double radius,
41 const string& fltType, const string& aircraftType,
42 const string& airline)
44 int currWpt = wpt_iterator - waypoints.begin();
48 createPushBack(firstFlight,dep, latitude, longitude,
49 radius, fltType, aircraftType, airline);
52 createTaxi(firstFlight, 1, dep, latitude, longitude,
53 radius, fltType, aircraftType, airline);
56 createTakeOff(firstFlight, dep, speed);
59 createClimb(firstFlight, dep, speed, alt);
62 createCruise(firstFlight, dep,arr, latitude, longitude, speed, alt);
71 createTaxi(false, 2, arr, latitude, longitude, radius,
72 fltType, aircraftType, airline);
75 createParking(arr, radius);
79 SG_LOG(SG_INPUT, SG_ALERT, "AIFlightPlan::create() attempting to create unknown leg"
80 " this is probably an internal program error");
82 wpt_iterator = waypoints.begin()+currWpt;
86 /*******************************************************************
88 * initialize the Aircraft at the parking location
89 ******************************************************************/
90 void FGAIFlightPlan::createPushBack(bool firstFlight, FGAirport *dep,
94 const string& fltType,
95 const string& aircraftType,
96 const string& airline)
105 //int currWpt = wpt_iterator - waypoints.begin();
106 // Erase all existing waypoints.
109 // We only need to get a valid parking if this is the first leg.
110 // Otherwise use the current aircraft position.
113 if (!(dep->getDynamics()->getAvailableParking(&lat, &lon,
116 aircraftType, airline)))
118 SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " <<
120 " of flight type " << fltType <<
121 " of airline " << airline <<
122 " at airport " << dep->getId());
127 dep->getDynamics()->getParking(gateId, &lat, &lon, &heading);
132 waypoint *wpt = new waypoint;
135 wpt->longitude = lon;
136 wpt->altitude = dep->getElevation();
138 wpt->crossat = -10000;
139 wpt->gear_down = true;
140 wpt->flaps_down= true;
141 wpt->finished = false;
142 wpt->on_ground = true;
143 waypoints.push_back(wpt);
145 // Add park twice, because it uses park once for initialization and once
146 // to trigger the departure ATC message
147 geo_direct_wgs_84 ( 0, lat, lon, heading,
149 &lat2, &lon2, &az2 );
152 wpt->latitude = lat2;
153 wpt->longitude = lon2;
154 wpt->altitude = dep->getElevation();
156 wpt->crossat = -10000;
157 wpt->gear_down = true;
158 wpt->flaps_down= true;
159 wpt->finished = false;
160 wpt->on_ground = true;
161 waypoints.push_back(wpt);
162 geo_direct_wgs_84 ( 0, lat, lon, heading,
164 &lat2, &lon2, &az2 );
166 wpt->name = "taxiStart";
167 wpt->latitude = lat2;
168 wpt->longitude = lon2;
169 wpt->altitude = dep->getElevation();
171 wpt->crossat = -10000;
172 wpt->gear_down = true;
173 wpt->flaps_down= true;
174 wpt->finished = false;
175 wpt->on_ground = true;
176 waypoints.push_back(wpt);
179 /*******************************************************************
181 * initialize the Aircraft at the parking location
182 ******************************************************************/
183 void FGAIFlightPlan::createTaxi(bool firstFlight, int direction,
184 FGAirport *apt, double latitude, double longitude,
185 double radius, const string& fltType,
186 const string& acType, const string& airline)
192 double lat2, lon2, az2;
197 // If this function is called during initialization,
198 // make sure we obtain a valid gate ID first
199 // and place the model at the location of the gate.
202 if (!(apt->getDynamics()->getAvailableParking(&lat, &lon,
207 SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " <<
209 " of flight type " << fltType <<
210 " of airline " << airline <<
211 " at airport " << apt->getId());
213 //waypoint *wpt = new waypoint;
214 //wpt->name = "park";
215 //wpt->latitude = lat;
216 //wpt->longitude = lon;
217 //wpt->altitude = apt->getElevation();
219 //wpt->crossat = -10000;
220 //wpt->gear_down = true;
221 //wpt->flaps_down= true;
222 //wpt->finished = false;
223 //wpt->on_ground = true;
224 //waypoints.push_back(wpt);
226 // "NOTE: this is currently fixed to "com" for commercial traffic
227 // Should be changed to be used dynamically to allow "gen" and "mil"
229 apt->getDynamics()->getActiveRunway("com", 1, activeRunway);
230 if (!(globals->get_runways()->search(apt->getId(),
234 SG_LOG(SG_INPUT, SG_ALERT, "Failed to find runway " <<
236 " at airport " << apt->getId());
240 // Determine the beginning of he runway
241 heading = rwy._heading;
242 double azimuth = heading + 180.0;
243 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
244 geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth,
245 rwy._length * SG_FEET_TO_METER * 0.5 - 5.0,
246 &lat2, &lon2, &az2 );
248 if (apt->getDynamics()->getGroundNetwork()->exists())
251 int runwayId = apt->getDynamics()->getGroundNetwork()->findNearestNode(lat2,
255 // A negative gateId indicates an overflow parking, use a
256 // fallback mechanism for this.
257 // Starting from gate 0 in this case is a bit of a hack
258 // which requires a more proper solution later on.
261 route = apt->getDynamics()->getGroundNetwork()->findShortestRoute(gateId,
264 route = apt->getDynamics()->getGroundNetwork()->findShortestRoute(0, runwayId);
268 //Add the runway startpoint;
270 wpt->name = "Airport Center";
271 wpt->latitude = latitude;
272 wpt->longitude = longitude;
273 wpt->altitude = apt->getElevation();
275 wpt->crossat = -10000;
276 wpt->gear_down = true;
277 wpt->flaps_down= true;
278 wpt->finished = false;
279 wpt->on_ground = true;
280 waypoints.push_back(wpt);
282 //Add the runway startpoint;
284 wpt->name = "Runway Takeoff";
285 wpt->latitude = lat2;
286 wpt->longitude = lon2;
287 wpt->altitude = apt->getElevation();
289 wpt->crossat = -10000;
290 wpt->gear_down = true;
291 wpt->flaps_down= true;
292 wpt->finished = false;
293 wpt->on_ground = true;
294 waypoints.push_back(wpt);
298 bool isPushBackPoint = false;
300 // If this is called during initialization, randomly
301 // skip a number of waypoints to get a more realistic
303 isPushBackPoint = true;
304 int nrWaypoints = route.size();
305 int nrWaypointsToSkip = rand() % nrWaypoints;
306 for (int i = 0; i < nrWaypointsToSkip; i++) {
307 isPushBackPoint = false;
312 //chop off the first two waypoints, because
313 // those have already been created
314 // by create pushback
315 int size = route.size();
321 while(route.next(&node))
323 FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
326 wpt->name = "taxiway"; // fixme: should be the name of the taxiway
327 wpt->latitude = tn->getLatitude();
328 wpt->longitude = tn->getLongitude();
329 // Elevation is currently disregarded when on_ground is true
330 // because the AIModel obtains a periodic ground elevation estimate.
331 wpt->altitude = apt->getElevation();
332 if (isPushBackPoint) {
334 isPushBackPoint = false;
339 wpt->crossat = -10000;
340 wpt->gear_down = true;
341 wpt->flaps_down= true;
342 wpt->finished = false;
343 wpt->on_ground = true;
344 waypoints.push_back(wpt);
351 // This is the fallback mechanism, in case no ground network is available
352 //Add the runway startpoint;
354 wpt->name = "Airport Center";
355 wpt->latitude = apt->getLatitude();
356 wpt->longitude = apt->getLongitude();
357 wpt->altitude = apt->getElevation();
359 wpt->crossat = -10000;
360 wpt->gear_down = true;
361 wpt->flaps_down= true;
362 wpt->finished = false;
363 wpt->on_ground = true;
364 waypoints.push_back(wpt);
366 //Add the runway startpoint;
368 wpt->name = "Runway Takeoff";
369 wpt->latitude = lat2;
370 wpt->longitude = lon2;
371 wpt->altitude = apt->getElevation();
373 wpt->crossat = -10000;
374 wpt->gear_down = true;
375 wpt->flaps_down= true;
376 wpt->finished = false;
377 wpt->on_ground = true;
378 waypoints.push_back(wpt);
383 apt->getDynamics()->getAvailableParking(&lat, &lon, &heading,
384 &gateId, radius, fltType,
387 double lat3 = (*(waypoints.end()-1))->latitude;
388 double lon3 = (*(waypoints.end()-1))->longitude;
389 //cerr << (*(waypoints.end()-1))->name << endl;
391 // Find a route from runway end to parking/gate.
392 if (apt->getDynamics()->getGroundNetwork()->exists())
395 int runwayId = apt->getDynamics()->getGroundNetwork()->findNearestNode(lat3,
397 // A negative gateId indicates an overflow parking, use a
398 // fallback mechanism for this.
399 // Starting from gate 0 is a bit of a hack...
402 route = apt->getDynamics()->getGroundNetwork()->findShortestRoute(runwayId,
405 route = apt->getDynamics()->getGroundNetwork()->findShortestRoute(runwayId, 0);
408 // No route found: go from gate directly to runway
410 //Add the runway startpoint;
412 wpt->name = "Airport Center";
413 wpt->latitude = latitude;
414 wpt->longitude = longitude;
415 wpt->altitude = apt->getElevation();
417 wpt->crossat = -10000;
418 wpt->gear_down = true;
419 wpt->flaps_down= true;
420 wpt->finished = false;
421 wpt->on_ground = true;
422 waypoints.push_back(wpt);
424 //Add the runway startpoint;
426 wpt->name = "Runway Takeoff";
427 wpt->latitude = lat2;
428 wpt->longitude = lon2;
429 wpt->altitude = apt->getElevation();
431 wpt->crossat = -10000;
432 wpt->gear_down = true;
433 wpt->flaps_down= true;
434 wpt->finished = false;
435 wpt->on_ground = true;
436 waypoints.push_back(wpt);
440 int size = route.size();
441 // Omit the last two waypoints, as
442 // those are created by createParking()
443 for (int i = 0; i < size-2; i++)
446 FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
448 wpt->name = "taxiway"; // fixme: should be the name of the taxiway
449 wpt->latitude = tn->getLatitude();
450 wpt->longitude = tn->getLongitude();
451 wpt->altitude = apt->getElevation();
453 wpt->crossat = -10000;
454 wpt->gear_down = true;
455 wpt->flaps_down= true;
456 wpt->finished = false;
457 wpt->on_ground = true;
458 waypoints.push_back(wpt);
464 // Use a fallback mechanism in case no ground network is available
465 // obtain the location of the gate entrance point
469 geo_direct_wgs_84 ( 0, lat, lon, heading,
471 &lat2, &lon2, &az2 );
473 wpt->name = "Airport Center";
474 wpt->latitude = apt->getLatitude();
475 wpt->longitude = apt->getLongitude();
476 wpt->altitude = apt->getElevation();
478 wpt->crossat = -10000;
479 wpt->gear_down = true;
480 wpt->flaps_down= true;
481 wpt->finished = false;
482 wpt->on_ground = true;
483 waypoints.push_back(wpt);
486 wpt->name = "Begin Parking"; //apt->getId(); //wpt_node->getStringValue("name", "END");
487 wpt->latitude = lat2;
488 wpt->longitude = lon2;
489 wpt->altitude = apt->getElevation();
491 wpt->crossat = -10000;
492 wpt->gear_down = true;
493 wpt->flaps_down= true;
494 wpt->finished = false;
495 wpt->on_ground = true;
496 waypoints.push_back(wpt);
502 apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
508 wpt->name = "END"; //wpt_node->getStringValue("name", "END");
510 wpt->longitude = lon;
513 wpt->crossat = -10000;
514 wpt->gear_down = true;
515 wpt->flaps_down= true;
516 wpt->finished = false;
517 wpt->on_ground = true;
518 waypoints.push_back(wpt);
524 /*******************************************************************
526 * initialize the Aircraft at the parking location
527 ******************************************************************/
528 void FGAIFlightPlan::createTakeOff(bool firstFlight, FGAirport *apt, double speed)
534 double lat2, lon2, az2;
537 // Get the current active runway, based on code from David Luff
538 // This should actually be unified and extended to include
539 // Preferential runway use schema's
543 // "NOTE: this is currently fixed to "com" for commercial traffic
544 // Should be changed to be used dynamically to allow "gen" and "mil"
546 apt->getDynamics()->getActiveRunway("com", 1, activeRunway);
547 if (!(globals->get_runways()->search(apt->getId(),
551 SG_LOG(SG_INPUT, SG_ALERT, "Failed to find runway " <<
553 " at airport " << apt->getId());
557 heading = rwy._heading;
558 double azimuth = heading + 180.0;
559 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
560 geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth,
561 rwy._length * SG_FEET_TO_METER * 0.5 - 105.0,
562 &lat2, &lon2, &az2 );
565 wpt->latitude = lat2;
566 wpt->longitude = lon2;
567 wpt->altitude = apt->getElevation();
569 wpt->crossat = -10000;
570 wpt->gear_down = true;
571 wpt->flaps_down= true;
572 wpt->finished = false;
573 wpt->on_ground = true;
574 waypoints.push_back(wpt);
580 //Next: the Start of Climb
581 geo_direct_wgs_84 ( 0, lat, lon, heading,
582 2560 * SG_FEET_TO_METER,
583 &lat2, &lon2, &az2 );
587 wpt->latitude = lat2;
588 wpt->longitude = lon2;
589 wpt->altitude = apt->getElevation()+3000;
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);
599 /*******************************************************************
601 * initialize the Aircraft at the parking location
602 ******************************************************************/
603 void FGAIFlightPlan::createClimb(bool firstFlight, FGAirport *apt, double speed, double alt)
610 double lat2, lon2, az2;
618 // "NOTE: this is currently fixed to "com" for commercial traffic
619 // Should be changed to be used dynamically to allow "gen" and "mil"
621 apt->getDynamics()->getActiveRunway("com", 1, activeRunway);
622 if (!(globals->get_runways()->search(apt->getId(),
626 SG_LOG(SG_INPUT, SG_ALERT, "Failed to find runway " <<
628 " at airport " << apt->getId());
634 heading = rwy._heading;
635 double azimuth = heading + 180.0;
636 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
637 geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, heading,
639 &lat2, &lon2, &az2 );
641 wpt->name = "10000ft climb";
642 wpt->latitude = lat2;
643 wpt->longitude = lon2;
644 wpt->altitude = 10000;
646 wpt->crossat = -10000;
647 wpt->gear_down = true;
648 wpt->flaps_down= true;
649 wpt->finished = false;
650 wpt->on_ground = false;
651 waypoints.push_back(wpt);
654 geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, heading,
656 &lat2, &lon2, &az2 );
658 wpt->name = "18000ft climb";
659 wpt->latitude = lat2;
660 wpt->longitude = lon2;
661 wpt->altitude = 18000;
663 wpt->crossat = -10000;
664 wpt->gear_down = true;
665 wpt->flaps_down= true;
666 wpt->finished = false;
667 wpt->on_ground = false;
668 waypoints.push_back(wpt);
672 /*******************************************************************
674 * initialize the Aircraft at the parking location
675 ******************************************************************/
676 void FGAIFlightPlan::createCruise(bool firstFlight, FGAirport *dep,
677 FGAirport *arr, double latitude,
678 double longitude, double speed,
685 double lat2, lon2, az2;
690 wpt->name = "Cruise"; //wpt_node->getStringValue("name", "END");
691 wpt->latitude = latitude;
692 wpt->longitude = longitude;
695 wpt->crossat = -10000;
696 wpt->gear_down = false;
697 wpt->flaps_down= false;
698 wpt->finished = false;
699 wpt->on_ground = false;
700 waypoints.push_back(wpt);
703 // should be changed dynamically to allow "gen" and "mil"
704 arr->getDynamics()->getActiveRunway("com", 2, activeRunway);
705 if (!(globals->get_runways()->search(arr->getId(),
709 SG_LOG(SG_INPUT, SG_ALERT, "Failed to find runway " <<
711 " at airport " << arr->getId());
714 heading = rwy._heading;
715 azimuth = heading + 180.0;
716 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
719 geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth,
721 &lat2, &lon2, &az2 );
724 wpt->latitude = lat2;
725 wpt->longitude = lon2;
729 wpt->gear_down = false;
730 wpt->flaps_down= false;
731 wpt->finished = false;
732 wpt->on_ground = false;
733 waypoints.push_back(wpt);
736 /*******************************************************************
738 * initialize the Aircraft at the parking location
739 ******************************************************************/
740 void FGAIFlightPlan::createDecent(FGAirport *apt)
743 // Ten thousand ft. Slowing down to 240 kts
749 double lat2, lon2, az2;
754 //Beginning of Decent
756 // allow "mil" and "gen" as well
757 apt->getDynamics()->getActiveRunway("com", 2, activeRunway);
758 if (!(globals->get_runways()->search(apt->getId(),
762 SG_LOG(SG_INPUT, SG_ALERT, "Failed to find runway " <<
764 " at airport " << apt->getId());
768 heading = rwy._heading;
769 azimuth = heading + 180.0;
770 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
771 geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth,
773 &lat2, &lon2, &az2 );
776 wpt->name = "Dec 10000ft"; //wpt_node->getStringValue("name", "END");
777 wpt->latitude = lat2;
778 wpt->longitude = lon2;
779 wpt->altitude = apt->getElevation();
781 wpt->crossat = 10000;
782 wpt->gear_down = false;
783 wpt->flaps_down= false;
784 wpt->finished = false;
785 wpt->on_ground = false;
786 waypoints.push_back(wpt);
788 // Three thousand ft. Slowing down to 160 kts
789 geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth,
791 &lat2, &lon2, &az2 );
793 wpt->name = "DEC 3000ft"; //wpt_node->getStringValue("name", "END");
794 wpt->latitude = lat2;
795 wpt->longitude = lon2;
796 wpt->altitude = apt->getElevation();
799 wpt->gear_down = true;
800 wpt->flaps_down= true;
801 wpt->finished = false;
802 wpt->on_ground = false;
803 waypoints.push_back(wpt);
805 /*******************************************************************
807 * initialize the Aircraft at the parking location
808 ******************************************************************/
809 void FGAIFlightPlan::createLanding(FGAirport *apt)
811 // Ten thousand ft. Slowing down to 150 kts
817 double lat2, lon2, az2;
823 heading = rwy._heading;
824 azimuth = heading + 180.0;
825 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
828 geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth,
829 rwy._length*0.45 * SG_FEET_TO_METER,
830 &lat2, &lon2, &az2 );
832 wpt->name = "Threshold"; //wpt_node->getStringValue("name", "END");
833 wpt->latitude = lat2;
834 wpt->longitude = lon2;
835 wpt->altitude = apt->getElevation();
837 wpt->crossat = apt->getElevation();
838 wpt->gear_down = true;
839 wpt->flaps_down= true;
840 wpt->finished = false;
841 wpt->on_ground = true;
842 waypoints.push_back(wpt);
844 //Full stop at the runway centerpoint
845 geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth,
847 &lat2, &lon2, &az2 );
849 wpt->name = "Center"; //wpt_node->getStringValue("name", "END");
850 wpt->latitude = rwy._lat;
851 wpt->longitude = rwy._lon;
852 wpt->altitude = apt->getElevation();
854 wpt->crossat = -10000;
855 wpt->gear_down = true;
856 wpt->flaps_down= true;
857 wpt->finished = false;
858 wpt->on_ground = true;
859 waypoints.push_back(wpt);
861 geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, heading,
862 rwy._length*0.45 * SG_FEET_TO_METER,
863 &lat2, &lon2, &az2 );
865 wpt->name = "Threshold"; //wpt_node->getStringValue("name", "END");
866 wpt->latitude = lat2;
867 wpt->longitude = lon2;
868 wpt->altitude = apt->getElevation();
870 wpt->crossat = apt->getElevation();
871 wpt->gear_down = true;
872 wpt->flaps_down= true;
873 wpt->finished = false;
874 wpt->on_ground = true;
875 waypoints.push_back(wpt);
878 /*******************************************************************
880 * initialize the Aircraft at the parking location
881 ******************************************************************/
882 void FGAIFlightPlan::createParking(FGAirport *apt, double radius)
889 apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
893 geo_direct_wgs_84 ( 0, lat, lon, heading,
895 &lat2, &lon2, &az2 );
897 wpt->name = "taxiStart";
898 wpt->latitude = lat2;
899 wpt->longitude = lon2;
900 wpt->altitude = apt->getElevation();
902 wpt->crossat = -10000;
903 wpt->gear_down = true;
904 wpt->flaps_down= true;
905 wpt->finished = false;
906 wpt->on_ground = true;
907 waypoints.push_back(wpt);
908 geo_direct_wgs_84 ( 0, lat, lon, heading,
910 &lat2, &lon2, &az2 );
912 wpt->name = "taxiStart";
913 wpt->latitude = lat2;
914 wpt->longitude = lon2;
915 wpt->altitude = apt->getElevation();
917 wpt->crossat = -10000;
918 wpt->gear_down = true;
919 wpt->flaps_down= true;
920 wpt->finished = false;
921 wpt->on_ground = true;
922 waypoints.push_back(wpt);
925 wpt->name = "END"; //wpt_node->getStringValue("name", "END");
927 wpt->longitude = lon;
928 wpt->altitude = apt->getElevation();
930 wpt->crossat = -10000;
931 wpt->gear_down = true;
932 wpt->flaps_down= true;
933 wpt->finished = false;
934 wpt->on_ground = true;
935 waypoints.push_back(wpt);