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 **************************************************************************/
25 #include "AIFlightPlan.hxx"
26 #include <simgear/math/sg_geodesy.hxx>
27 #include <simgear/props/props.hxx>
28 #include <simgear/props/props_io.hxx>
30 #include <Airports/simple.hxx>
31 #include <Airports/runways.hxx>
32 #include <Airports/dynamics.hxx>
33 #include "AIAircraft.hxx"
34 #include "performancedata.hxx"
36 #include <Environment/environment_mgr.hxx>
37 #include <Environment/environment.hxx>
40 /* FGAIFlightPlan::create()
41 * dynamically create a flight plan for AI traffic, based on data provided by the
42 * Traffic Manager, when reading a filed flightplan failes. (DT, 2004/07/10)
44 * This is the top-level function, and the only one that is publicly available.
49 // Check lat/lon values during initialization;
50 bool FGAIFlightPlan::create(FGAIAircraft * ac, FGAirport * dep,
51 FGAirport * arr, int legNr, double alt,
52 double speed, double latitude,
53 double longitude, bool firstFlight,
54 double radius, const string & fltType,
55 const string & aircraftType,
56 const string & airline, double distance)
59 int currWpt = wpt_iterator - waypoints.begin();
62 retVal = createPushBack(ac, firstFlight, dep, latitude, longitude,
63 radius, fltType, aircraftType, airline);
66 retVal = createTakeoffTaxi(ac, firstFlight, dep, radius, fltType,
67 aircraftType, airline);
70 retVal = createTakeOff(ac, firstFlight, dep, speed, fltType);
73 retVal = createClimb(ac, firstFlight, dep, speed, alt, fltType);
76 retVal = createCruise(ac, firstFlight, dep, arr, latitude, longitude, speed,
80 retVal = createDescent(ac, arr, latitude, longitude, speed, alt, fltType,
84 retVal = createLanding(ac, arr, fltType);
87 retVal = createLandingTaxi(ac, arr, radius, fltType, aircraftType, airline);
90 retVal = createParking(ac, arr, radius);
94 SG_LOG(SG_INPUT, SG_ALERT,
95 "AIFlightPlan::create() attempting to create unknown leg"
96 " this is probably an internal program error");
98 wpt_iterator = waypoints.begin() + currWpt;
103 FGAIFlightPlan::waypoint *
104 FGAIFlightPlan::createOnGround(FGAIAircraft * ac,
105 const std::string & aName,
106 const SGGeod & aPos, double aElev,
109 waypoint *wpt = new waypoint;
111 wpt->longitude = aPos.getLongitudeDeg();
112 wpt->latitude = aPos.getLatitudeDeg();
113 wpt->altitude = aElev;
115 wpt->crossat = -10000.1;
116 wpt->gear_down = true;
117 wpt->flaps_down = true;
118 wpt->finished = false;
119 wpt->on_ground = true;
124 FGAIFlightPlan::waypoint *
125 FGAIFlightPlan::createInAir(FGAIAircraft * ac,
126 const std::string & aName,
127 const SGGeod & aPos, double aElev,
130 waypoint *wpt = new waypoint;
132 wpt->longitude = aPos.getLongitudeDeg();
133 wpt->latitude = aPos.getLatitudeDeg();
134 wpt->altitude = aElev;
136 wpt->crossat = -10000.1;
137 wpt->gear_down = false;
138 wpt->flaps_down = false;
139 wpt->finished = false;
140 wpt->on_ground = false;
145 FGAIFlightPlan::waypoint *
146 FGAIFlightPlan::cloneWithPos(FGAIAircraft * ac, waypoint * aWpt,
147 const std::string & aName,
150 waypoint *wpt = new waypoint;
152 wpt->longitude = aPos.getLongitudeDeg();
153 wpt->latitude = aPos.getLatitudeDeg();
155 wpt->altitude = aWpt->altitude;
156 wpt->speed = aWpt->speed;
157 wpt->crossat = aWpt->crossat;
158 wpt->gear_down = aWpt->gear_down;
159 wpt->flaps_down = aWpt->flaps_down;
160 wpt->finished = aWpt->finished;
161 wpt->on_ground = aWpt->on_ground;
167 FGAIFlightPlan::waypoint * FGAIFlightPlan::clone(waypoint * aWpt)
169 waypoint *wpt = new waypoint;
170 wpt->name = aWpt->name;
171 wpt->longitude = aWpt->longitude;
172 wpt->latitude = aWpt->latitude;
174 wpt->altitude = aWpt->altitude;
175 wpt->speed = aWpt->speed;
176 wpt->crossat = aWpt->crossat;
177 wpt->gear_down = aWpt->gear_down;
178 wpt->flaps_down = aWpt->flaps_down;
179 wpt->finished = aWpt->finished;
180 wpt->on_ground = aWpt->on_ground;
187 void FGAIFlightPlan::createDefaultTakeoffTaxi(FGAIAircraft * ac,
188 FGAirport * aAirport,
191 SGGeod runwayTakeoff = aRunway->pointOnCenterline(5.0);
192 double airportElev = aAirport->getElevation();
196 createOnGround(ac, "Airport Center", aAirport->geod(), airportElev,
197 ac->getPerformance()->vTaxi());
198 waypoints.push_back(wpt);
200 createOnGround(ac, "Runway Takeoff", runwayTakeoff, airportElev,
201 ac->getPerformance()->vTaxi());
202 waypoints.push_back(wpt);
205 bool FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft * ac, bool firstFlight,
208 const string & fltType,
209 const string & acType,
210 const string & airline)
212 double heading, lat, lon;
214 // If this function is called during initialization,
215 // make sure we obtain a valid gate ID first
216 // and place the model at the location of the gate.
218 if (!(apt->getDynamics()->getAvailableParking(&lat, &lon,
222 SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " <<
224 " of flight type " << fltType <<
225 " of airline " << airline <<
226 " at airport " << apt->getId());
230 string rwyClass = getRunwayClassFromTrafficType(fltType);
232 // Only set this if it hasn't been set by ATC already.
233 if (activeRunway.empty()) {
234 //cerr << "Getting runway for " << ac->getTrafficRef()->getCallSign() << " at " << apt->getId() << endl;
235 double depHeading = ac->getTrafficRef()->getCourse();
236 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
239 rwy = apt->getRunwayByIdent(activeRunway);
240 SGGeod runwayTakeoff = rwy->pointOnCenterline(5.0);
242 FGGroundNetwork *gn = apt->getDynamics()->getGroundNetwork();
244 createDefaultTakeoffTaxi(ac, apt, rwy);
249 int runwayId = gn->findNearestNode(runwayTakeoff);
251 // A negative gateId indicates an overflow parking, use a
252 // fallback mechanism for this.
253 // Starting from gate 0 in this case is a bit of a hack
254 // which requires a more proper solution later on.
256 taxiRoute = new FGTaxiRoute;
258 // Determine which node to start from.
260 // Find out which node to start from
261 FGParking *park = apt->getDynamics()->getParking(gateId);
263 node = park->getPushBackPoint();
269 // HAndle case where parking doens't have a node
270 if ((node == 0) && park) {
274 node = lastNodeVisited;
278 *taxiRoute = gn->findShortestRoute(node, runwayId);
281 if (taxiRoute->empty()) {
282 createDefaultTakeoffTaxi(ac, apt, rwy);
287 //bool isPushBackPoint = false;
289 // If this is called during initialization, randomly
290 // skip a number of waypoints to get a more realistic
292 int nrWaypointsToSkip = rand() % taxiRoute->size();
293 // but make sure we always keep two active waypoints
294 // to prevent a segmentation fault
295 for (int i = 0; i < nrWaypointsToSkip - 3; i++) {
296 taxiRoute->next(&node);
298 apt->getDynamics()->releaseParking(gateId);
300 if (taxiRoute->size() > 1) {
301 taxiRoute->next(&node); // chop off the first waypoint, because that is already the last of the pushback route
305 // push each node on the taxi route as a waypoint
307 while (taxiRoute->next(&node, &route)) {
309 snprintf(buffer, 10, "%d", node);
311 apt->getDynamics()->getGroundNetwork()->findNode(node);
313 createOnGround(ac, buffer, tn->getGeod(), apt->getElevation(),
314 ac->getPerformance()->vTaxi());
315 wpt->routeIndex = route;
316 waypoints.push_back(wpt);
321 void FGAIFlightPlan::createDefaultLandingTaxi(FGAIAircraft * ac,
322 FGAirport * aAirport)
325 SGGeod::fromDeg(waypoints.back()->longitude,
326 waypoints.back()->latitude);
327 double airportElev = aAirport->getElevation();
331 createOnGround(ac, "Runway Exit", lastWptPos, airportElev,
332 ac->getPerformance()->vTaxi());
333 waypoints.push_back(wpt);
335 createOnGround(ac, "Airport Center", aAirport->geod(), airportElev,
336 ac->getPerformance()->vTaxi());
337 waypoints.push_back(wpt);
339 double heading, lat, lon;
340 aAirport->getDynamics()->getParking(gateId, &lat, &lon, &heading);
342 createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), airportElev,
343 ac->getPerformance()->vTaxi());
344 waypoints.push_back(wpt);
347 bool FGAIFlightPlan::createLandingTaxi(FGAIAircraft * ac, FGAirport * apt,
349 const string & fltType,
350 const string & acType,
351 const string & airline)
353 double heading, lat, lon;
354 apt->getDynamics()->getAvailableParking(&lat, &lon, &heading,
355 &gateId, radius, fltType,
359 SGGeod::fromDeg(waypoints.back()->longitude,
360 waypoints.back()->latitude);
361 FGGroundNetwork *gn = apt->getDynamics()->getGroundNetwork();
363 // Find a route from runway end to parking/gate.
365 createDefaultLandingTaxi(ac, apt);
370 int runwayId = gn->findNearestNode(lastWptPos);
371 // A negative gateId indicates an overflow parking, use a
372 // fallback mechanism for this.
373 // Starting from gate 0 is a bit of a hack...
376 taxiRoute = new FGTaxiRoute;
378 *taxiRoute = gn->findShortestRoute(runwayId, gateId);
380 *taxiRoute = gn->findShortestRoute(runwayId, 0);
383 if (taxiRoute->empty()) {
384 createDefaultLandingTaxi(ac, apt);
390 int size = taxiRoute->size();
391 // Omit the last two waypoints, as
392 // those are created by createParking()
394 for (int i = 0; i < size - 2; i++) {
395 taxiRoute->next(&node, &route);
397 snprintf(buffer, 10, "%d", node);
398 FGTaxiNode *tn = gn->findNode(node);
400 createOnGround(ac, buffer, tn->getGeod(), apt->getElevation(),
401 ac->getPerformance()->vTaxi());
402 wpt->routeIndex = route;
403 waypoints.push_back(wpt);
408 /*******************************************************************
411 * - Speed -> knots -> nm/hour
412 * - distance along runway =-> meters
413 * - accel / decel -> is given as knots/hour, but this is highly questionable:
414 * for a jet_transport performance class, a accel / decel rate of 5 / 2 is
415 * given respectively. According to performance data.cxx, a value of kts / second seems
416 * more likely however.
418 ******************************************************************/
419 bool FGAIFlightPlan::createTakeOff(FGAIAircraft * ac, bool firstFlight,
420 FGAirport * apt, double speed,
421 const string & fltType)
423 double accel = ac->getPerformance()->acceleration();
424 double vTaxi = ac->getPerformance()->vTaxi();
425 double vRotate = ac->getPerformance()->vRotate();
426 double vTakeoff = ac->getPerformance()->vTakeoff();
427 //double vClimb = ac->getPerformance()->vClimb();
429 double accelMetric = (accel * SG_NM_TO_METER) / 3600;
430 double vTaxiMetric = (vTaxi * SG_NM_TO_METER) / 3600;
431 double vRotateMetric = (vRotate * SG_NM_TO_METER) / 3600;
432 double vTakeoffMetric = (vTakeoff * SG_NM_TO_METER) / 3600;
433 //double vClimbMetric = (vClimb * SG_NM_TO_METER) / 3600;
434 // Acceleration = dV / dT
435 // Acceleration X dT = dV
436 // dT = dT / Acceleration
437 //d = (Vf^2 - Vo^2) / (2*a)
438 //double accelTime = (vRotate - vTaxi) / accel;
439 //cerr << "Using " << accelTime << " as total acceleration time" << endl;
440 double accelDistance =
441 (vRotateMetric * vRotateMetric -
442 vTaxiMetric * vTaxiMetric) / (2 * accelMetric);
443 //cerr << "Using " << accelDistance << " " << accelMetric << " " << vRotateMetric << endl;
445 // Get the current active runway, based on code from David Luff
446 // This should actually be unified and extended to include
447 // Preferential runway use schema's
448 // NOTE: DT (2009-01-18: IIRC, this is currently already the case,
449 // because the getActive runway function takes care of that.
451 string rwyClass = getRunwayClassFromTrafficType(fltType);
452 double heading = ac->getTrafficRef()->getCourse();
453 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
455 rwy = apt->getRunwayByIdent(activeRunway);
458 double airportElev = apt->getElevation();
459 // Acceleration point, 105 meters into the runway,
460 SGGeod accelPoint = rwy->pointOnCenterline(105.0);
461 wpt = createOnGround(ac, "accel", accelPoint, airportElev, vRotate);
462 waypoints.push_back(wpt);
466 (vTakeoffMetric * vTakeoffMetric -
467 vTaxiMetric * vTaxiMetric) / (2 * accelMetric);
468 //cerr << "Using " << accelDistance << " " << accelMetric << " " << vTakeoffMetric << endl;
469 accelPoint = rwy->pointOnCenterline(105.0 + accelDistance);
470 wpt = createOnGround(ac, "rotate", accelPoint, airportElev, vTakeoff);
471 waypoints.push_back(wpt);
474 ((vTakeoffMetric * 1.1) * (vTakeoffMetric * 1.1) -
475 vTaxiMetric * vTaxiMetric) / (2 * accelMetric);
476 //cerr << "Using " << accelDistance << " " << accelMetric << " " << vTakeoffMetric << endl;
477 accelPoint = rwy->pointOnCenterline(105.0 + accelDistance);
479 createOnGround(ac, "rotate", accelPoint, airportElev + 1000,
481 wpt->on_ground = false;
482 waypoints.push_back(wpt);
484 wpt = cloneWithPos(ac, wpt, "3000 ft", rwy->end());
485 wpt->altitude = airportElev + 3000;
486 waypoints.push_back(wpt);
488 // Finally, add two more waypoints, so that aircraft will remain under
489 // Tower control until they have reached the 3000 ft climb point
490 SGGeod pt = rwy->pointOnCenterline(5000 + rwy->lengthM() * 0.5);
491 wpt = cloneWithPos(ac, wpt, "5000 ft", pt);
492 wpt->altitude = airportElev + 5000;
493 waypoints.push_back(wpt);
497 /*******************************************************************
499 * initialize the Aircraft at the parking location
500 ******************************************************************/
501 bool FGAIFlightPlan::createClimb(FGAIAircraft * ac, bool firstFlight,
502 FGAirport * apt, double speed, double alt,
503 const string & fltType)
506 // bool planLoaded = false;
508 double vClimb = ac->getPerformance()->vClimb();
511 string rwyClass = getRunwayClassFromTrafficType(fltType);
512 double heading = ac->getTrafficRef()->getCourse();
513 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
515 rwy = apt->getRunwayByIdent(activeRunway);
518 for (wpt_vector_iterator i = sid->getFirstWayPoint();
519 i != sid->getLastWayPoint(); i++) {
520 waypoints.push_back(clone(*(i)));
521 //cerr << " Cloning waypoint " << endl;
524 SGGeod climb1 = rwy->pointOnCenterline(10 * SG_NM_TO_METER);
525 wpt = createInAir(ac, "10000ft climb", climb1, vClimb, 10000);
526 wpt->gear_down = true;
527 wpt->flaps_down = true;
528 waypoints.push_back(wpt);
530 SGGeod climb2 = rwy->pointOnCenterline(20 * SG_NM_TO_METER);
531 wpt = cloneWithPos(ac, wpt, "18000ft climb", climb2);
532 wpt->altitude = 18000;
533 waypoints.push_back(wpt);
540 /*******************************************************************
542 * Generate a flight path from the last waypoint of the cruise to
543 * the permission to land point
544 ******************************************************************/
545 bool FGAIFlightPlan::createDescent(FGAIAircraft * ac, FGAirport * apt,
546 double latitude, double longitude,
547 double speed, double alt,
548 const string & fltType,
549 double requiredDistance)
551 bool reposition = false;
553 double vDescent = ac->getPerformance()->vDescent();
554 double vApproach = ac->getPerformance()->vApproach();
557 //Beginning of Descent
558 string rwyClass = getRunwayClassFromTrafficType(fltType);
559 double heading = ac->getTrafficRef()->getCourse();
560 apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway,
562 rwy = apt->getRunwayByIdent(activeRunway);
566 // Create a slow descent path that ends 250 lateral to the runway.
567 double initialTurnRadius = getTurnRadius(vDescent, true);
568 //double finalTurnRadius = getTurnRadius(vApproach, true);
570 // get length of the downwind leg for the intended runway
571 double distanceOut = apt->getDynamics()->getApproachController()->getRunway(rwy->name())->getApproachDistance(); //12 * SG_NM_TO_METER;
572 //time_t previousArrivalTime= apt->getDynamics()->getApproachController()->getRunway(rwy->name())->getEstApproachTime();
575 SGGeod current = SGGeod::fromDegM(longitude, latitude, 0);
576 SGGeod initialTarget = rwy->pointOnCenterline(-distanceOut);
577 SGGeod refPoint = rwy->pointOnCenterline(0);
578 double distance = SGGeodesy::distanceM(current, initialTarget);
579 double azimuth = SGGeodesy::courseDeg(current, initialTarget);
582 // To prevent absurdly steep approaches, compute the origin from where the approach should have started
585 if (ac->getTrafficRef()->getCallSign() ==
586 fgGetString("/ai/track-callsign")) {
587 //cerr << "Reposition information: Actual distance " << distance << ". required distance " << requiredDistance << endl;
591 if (distance < requiredDistance * 0.8) {
593 SGGeodesy::direct(initialTarget, azimuth,
594 -requiredDistance, origin, dummyAz2);
596 distance = SGGeodesy::distanceM(current, initialTarget);
597 azimuth = SGGeodesy::courseDeg(current, initialTarget);
603 double dAlt = alt - (apt->getElevation() + 2000);
605 double nPoints = 100;
609 // The descent path contains the following phases:
610 // 1) a linear glide path from the initial position to
611 // 2) a semi circle turn to final
614 //cerr << "Phase 1: Linear Descent path to runway" << rwy->name() << endl;
615 // Create an initial destination point on a semicircle
616 //cerr << "lateral offset : " << lateralOffset << endl;
617 //cerr << "Distance : " << distance << endl;
618 //cerr << "Azimuth : " << azimuth << endl;
619 //cerr << "Initial Lateral point: " << lateralOffset << endl;
620 double lat = refPoint.getLatitudeDeg();
621 double lon = refPoint.getLongitudeDeg();
622 //cerr << "Reference point (" << lat << ", " << lon << ")." << endl;
623 lat = initialTarget.getLatitudeDeg();
624 lon = initialTarget.getLongitudeDeg();
625 //cerr << "Initial Target point (" << lat << ", " << lon << ")." << endl;
627 double ratio = initialTurnRadius / distance;
633 double newHeading = asin(ratio) * SG_RADIANS_TO_DEGREES;
635 cos(newHeading * SG_DEGREES_TO_RADIANS) * distance;
636 //cerr << "new distance " << newDistance << ". additional Heading " << newHeading << endl;
637 double side = azimuth - rwy->headingDeg();
638 double lateralOffset = initialTurnRadius;
644 // Calculate the ETA at final, based on remaining distance, and approach speed.
645 // distance should really consist of flying time to terniary target, plus circle
646 // but the distance to secondary target should work as a reasonable approximation
647 // aditionally add the amount of distance covered by making a turn of "side"
648 double turnDistance = (2 * M_PI * initialTurnRadius) * (side / 360.0);
650 (turnDistance + distance) / ((vDescent * SG_NM_TO_METER) / 3600.0);
651 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
652 //if (ac->getTrafficRef()->getCallSign() == fgGetString("/ai/track-callsign")) {
653 // cerr << " Arrival time estimation: turn angle " << side << ". Turn distance " << turnDistance << ". Linear distance " << distance << ". Time to go " << remaining << endl;
657 time_t eta = now + remaining;
658 //choose a distance to the runway such that it will take at least 60 seconds more
659 // time to get there than the previous aircraft.
660 // Don't bother when aircraft need to be repositioned, because that marks the initialization phased...
664 if (reposition == false) {
666 apt->getDynamics()->getApproachController()->getRunway(rwy->
669 requestTimeSlot(eta);
673 //if ((eta < (previousArrivalTime+60)) && (reposition == false)) {
674 arrivalTime = newEta;
675 time_t additionalTimeNeeded = newEta - eta;
676 double distanceCovered =
677 ((vApproach * SG_NM_TO_METER) / 3600.0) * additionalTimeNeeded;
678 distanceOut += distanceCovered;
679 //apt->getDynamics()->getApproachController()->getRunway(rwy->name())->setEstApproachTime(eta+additionalTimeNeeded);
680 //cerr << "Adding additional distance: " << distanceCovered << " to allow " << additionalTimeNeeded << " seconds of flying time" << endl << endl;
682 //apt->getDynamics()->getApproachController()->getRunway(rwy->name())->setEstApproachTime(eta);
684 //cerr << "Timing information : Previous eta: " << previousArrivalTime << ". Current ETA : " << eta << endl;
686 SGGeod secondaryTarget =
687 rwy->pointOffCenterline(-distanceOut, lateralOffset);
688 initialTarget = rwy->pointOnCenterline(-distanceOut);
689 distance = SGGeodesy::distanceM(origin, secondaryTarget);
690 azimuth = SGGeodesy::courseDeg(origin, secondaryTarget);
693 lat = secondaryTarget.getLatitudeDeg();
694 lon = secondaryTarget.getLongitudeDeg();
695 //cerr << "Secondary Target point (" << lat << ", " << lon << ")." << endl;
696 //cerr << "Distance : " << distance << endl;
697 //cerr << "Azimuth : " << azimuth << endl;
700 ratio = initialTurnRadius / distance;
705 newHeading = asin(ratio) * SG_RADIANS_TO_DEGREES;
706 newDistance = cos(newHeading * SG_DEGREES_TO_RADIANS) * distance;
707 //cerr << "new distance realative to secondary target: " << newDistance << ". additional Heading " << newHeading << endl;
709 azimuth += newHeading;
711 azimuth -= newHeading;
714 SGGeod tertiaryTarget;
715 SGGeodesy::direct(origin, azimuth,
716 newDistance, tertiaryTarget, dummyAz2);
718 lat = tertiaryTarget.getLatitudeDeg();
719 lon = tertiaryTarget.getLongitudeDeg();
720 //cerr << "tertiary Target point (" << lat << ", " << lon << ")." << endl;
723 for (int i = 1; i < nPoints; i++) {
725 double currentDist = i * (newDistance / nPoints);
726 double currentAltitude = alt - (i * (dAlt / nPoints));
727 SGGeodesy::direct(origin, azimuth, currentDist, result, dummyAz2);
728 snprintf(buffer, 16, "descent%03d", i);
729 wpt = createInAir(ac, buffer, result, currentAltitude, vDescent);
730 wpt->crossat = currentAltitude;
731 wpt->trackLength = (newDistance / nPoints);
732 waypoints.push_back(wpt);
733 //cerr << "Track Length : " << wpt->trackLength;
734 //cerr << " Position : " << result.getLatitudeDeg() << " " << result.getLongitudeDeg() << " " << currentAltitude << endl;
737 //cerr << "Phase 2: Circle " << endl;
738 double initialAzimuth =
739 SGGeodesy::courseDeg(secondaryTarget, tertiaryTarget);
740 double finalAzimuth =
741 SGGeodesy::courseDeg(secondaryTarget, initialTarget);
743 //cerr << "Angles from secondary target: " << initialAzimuth << " " << finalAzimuth << endl;
744 int increment, startval, endval;
745 // circle right around secondary target if orig of position is to the right of the runway
746 // i.e. use negative angles; else circle leftward and use postivi
749 startval = floor(initialAzimuth);
750 endval = ceil(finalAzimuth);
751 if (endval > startval) {
756 startval = ceil(initialAzimuth);
757 endval = floor(finalAzimuth);
758 if (endval < startval) {
764 //cerr << "creating circle between " << startval << " and " << endval << " using " << increment << endl;
765 double trackLength = (2 * M_PI * initialTurnRadius) / 360.0;
766 for (int i = startval; i != endval; i += increment) {
768 double currentAltitude = apt->getElevation() + 2000;
769 SGGeodesy::direct(secondaryTarget, i,
770 initialTurnRadius, result, dummyAz2);
771 snprintf(buffer, 16, "turn%03d", i);
772 wpt = createInAir(ac, buffer, result, currentAltitude, vDescent);
773 wpt->crossat = currentAltitude;
774 wpt->trackLength = trackLength;
775 //cerr << "Track Length : " << wpt->trackLength;
776 waypoints.push_back(wpt);
777 //cerr << " Position : " << result.getLatitudeDeg() << " " << result.getLongitudeDeg() << " " << currentAltitude << endl;
781 // The approach leg should bring the aircraft to approximately 4-6 out, after which the landing phase should take over.
782 //cerr << "Phase 3: Approach" << endl;
783 distanceOut -= distanceCovered;
784 for (int i = 1; i < nPoints; i++) {
786 double currentDist = i * (distanceOut / nPoints);
787 double currentAltitude =
788 apt->getElevation() + 2000 - (i * 2000 / nPoints);
789 snprintf(buffer, 16, "final%03d", i);
790 result = rwy->pointOnCenterline((-distanceOut) + currentDist);
791 wpt = createInAir(ac, buffer, result, currentAltitude, vApproach);
792 wpt->crossat = currentAltitude;
793 wpt->trackLength = (distanceOut / nPoints);
794 // account for the extra distance due to an extended downwind leg
796 wpt->trackLength += distanceCovered;
798 //cerr << "Track Length : " << wpt->trackLength;
799 waypoints.push_back(wpt);
800 //cerr << " Position : " << result.getLatitudeDeg() << " " << result.getLongitudeDeg() << " " << currentAltitude << endl;
803 //cerr << "Done" << endl;
805 // Erase the two bogus BOD points: Note check for conflicts with scripted AI flightPlans
806 IncrementWaypoint(true);
807 IncrementWaypoint(true);
811 //double minDistance = HUGE_VAL;
813 tempDistance = SGGeodesy::distanceM(current, initialTarget);
815 tempDistance / ((vDescent * SG_NM_TO_METER) / 3600.0) + now;
817 apt->getDynamics()->getApproachController()->getRunway(rwy->
820 requestTimeSlot(eta);
821 arrivalTime = newEta;
823 ((vDescent * SG_NM_TO_METER) / 3600.0) * (newEta - now);
824 //cerr << "Repositioning information : eta" << eta << ". New ETA " << newEta << ". Diff = " << (newEta - eta) << ". Distance = " << tempDistance << ". New distance = " << newDistance << endl;
825 IncrementWaypoint(true); // remove waypoint BOD2
826 while (checkTrackLength("final001") > newDistance) {
827 IncrementWaypoint(true);
829 //cerr << "Repositioning to waypoint " << (*waypoints.begin())->name << endl;
830 ac->resetPositionFromFlightPlan();
835 /*******************************************************************
837 * Create a flight path from the "permision to land" point (currently
838 hardcoded at 5000 meters from the threshold) to the threshold, at
839 a standard glide slope angle of 3 degrees.
840 ******************************************************************/
841 bool FGAIFlightPlan::createLanding(FGAIAircraft * ac, FGAirport * apt,
842 const string & fltType)
844 double vTouchdown = ac->getPerformance()->vTouchdown();
845 //double vTaxi = ac->getPerformance()->vTaxi();
847 //string rwyClass = getRunwayClassFromTrafficType(fltType);
848 //double heading = ac->getTrafficRef()->getCourse();
849 //apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading);
850 //rwy = apt->getRunwayByIdent(activeRunway);
854 double aptElev = apt->getElevation();
858 for (int i = 1; i < 10; i++) {
859 snprintf(buffer, 12, "wpt%d", i);
860 coord = rwy->pointOnCenterline(rwy->lengthM() * (i / 10.0));
861 wpt = createOnGround(ac, buffer, coord, aptElev, (vTouchdown / i));
862 wpt->crossat = apt->getElevation();
863 waypoints.push_back(wpt);
868 wpt = createOnGround(ac, "Threshold", rwy->threshold(), aptElev, vTouchdown);
869 wpt->crossat = apt->getElevation();
870 waypoints.push_back(wpt);
873 wpt = createOnGround(ac, "Center", rwy->geod(), aptElev, vTaxi*2);
874 waypoints.push_back(wpt);
876 SGGeod rollOut = rwy->pointOnCenterline(rwy->lengthM() * 0.9);
877 wpt = createOnGround(ac, "Roll Out", rollOut, aptElev, vTaxi);
878 wpt->crossat = apt->getElevation();
879 waypoints.push_back(wpt);
884 /*******************************************************************
886 * initialize the Aircraft at the parking location
887 ******************************************************************/
888 bool FGAIFlightPlan::createParking(FGAIAircraft * ac, FGAirport * apt,
892 double aptElev = apt->getElevation();
893 double lat = 0.0, lat2 = 0.0;
894 double lon = 0.0, lon2 = 0.0;
896 double heading = 0.0;
898 double vTaxi = ac->getPerformance()->vTaxi();
899 double vTaxiReduced = vTaxi * (2.0 / 3.0);
900 apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
904 geo_direct_wgs_84(0, lat, lon, heading,
905 2.2 * radius, &lat2, &lon2, &az2);
907 createOnGround(ac, "taxiStart", SGGeod::fromDeg(lon2, lat2),
908 aptElev, vTaxiReduced);
909 waypoints.push_back(wpt);
911 geo_direct_wgs_84(0, lat, lon, heading,
912 0.1 * radius, &lat2, &lon2, &az2);
915 createOnGround(ac, "taxiStart2", SGGeod::fromDeg(lon2, lat2),
916 aptElev, vTaxiReduced);
917 waypoints.push_back(wpt);
920 createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), aptElev,
922 waypoints.push_back(wpt);
928 * @param fltType a string describing the type of
929 * traffic, normally used for gate assignments
930 * @return a converted string that gives the runway
931 * preference schedule to be used at aircraft having
932 * a preferential runway schedule implemented (i.e.
933 * having a rwyprefs.xml file
935 * Currently valid traffic types for gate assignment:
936 * - gate (commercial gate)
937 * - cargo (commercial gargo),
938 * - ga (general aviation) ,
940 * - mil-fighter (military - fighter),
941 * - mil-transport (military - transport)
943 * Valid runway classes:
944 * - com (commercial traffic: jetliners, passenger and cargo)
945 * - gen (general aviation)
946 * - ul (ultralight: I can imagine that these may share a runway with ga on some airports)
947 * - mil (all military traffic)
949 string FGAIFlightPlan::getRunwayClassFromTrafficType(string fltType)
951 if ((fltType == "gate") || (fltType == "cargo")) {
952 return string("com");
954 if (fltType == "ga") {
955 return string("gen");
957 if (fltType == "ul") {
960 if ((fltType == "mil-fighter") || (fltType == "mil-transport")) {
961 return string("mil");
963 return string("com");
967 double FGAIFlightPlan::getTurnRadius(double speed, bool inAir)
970 if (inAir == false) {
971 turn_radius = ((360 / 30) * fabs(speed)) / (2 * M_PI);
973 turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank