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 **************************************************************************/
26 #include "AIFlightPlan.hxx"
27 #include <simgear/math/sg_geodesy.hxx>
28 #include <simgear/props/props.hxx>
29 #include <simgear/props/props_io.hxx>
31 #include <Airports/simple.hxx>
32 #include <Airports/runways.hxx>
33 #include <Airports/dynamics.hxx>
34 #include "AIAircraft.hxx"
35 #include "performancedata.hxx"
37 #include <Environment/environment_mgr.hxx>
38 #include <Environment/environment.hxx>
39 #include <FDM/LaRCsim/basic_aero.h>
42 /* FGAIFlightPlan::create()
43 * dynamically create a flight plan for AI traffic, based on data provided by the
44 * Traffic Manager, when reading a filed flightplan failes. (DT, 2004/07/10)
46 * This is the top-level function, and the only one that is publicly available.
51 // Check lat/lon values during initialization;
52 bool FGAIFlightPlan::create(FGAIAircraft * ac, FGAirport * dep,
53 FGAirport * arr, int legNr, double alt,
54 double speed, double latitude,
55 double longitude, bool firstFlight,
56 double radius, const string & fltType,
57 const string & aircraftType,
58 const string & airline, double distance)
61 int currWpt = wpt_iterator - waypoints.begin();
64 retVal = createPushBack(ac, firstFlight, dep, latitude, longitude,
65 radius, fltType, aircraftType, airline);
66 // Pregenerate the taxi leg.
68 // waypoints.back()->setName( waypoints.back()->getName() + string("legend"));
69 // retVal = createTakeoffTaxi(ac, false, dep, radius, fltType, aircraftType, airline);
73 retVal = createTakeoffTaxi(ac, firstFlight, dep, radius, fltType,
74 aircraftType, airline);
77 retVal = createTakeOff(ac, firstFlight, dep, speed, fltType);
80 retVal = createClimb(ac, firstFlight, dep, speed, alt, fltType);
83 retVal = createCruise(ac, firstFlight, dep, arr, latitude, longitude, speed,
87 retVal = createDescent(ac, arr, latitude, longitude, speed, alt, fltType,
91 retVal = createLanding(ac, arr, fltType);
94 retVal = createLandingTaxi(ac, arr, radius, fltType, aircraftType, airline);
97 retVal = createParking(ac, arr, radius);
101 SG_LOG(SG_AI, SG_ALERT,
102 "AIFlightPlan::create() attempting to create unknown leg"
103 " this is probably an internal program error");
105 wpt_iterator = waypoints.begin() + currWpt;
106 //don't increment leg right away, but only once we pass the actual last waypoint that was created.
107 // to do so, mark the last waypoint with a special status flag
109 waypoints.back()->setName( waypoints.back()->getName() + string("legend"));
110 // "It's pronounced Leg-end" (Roger Glover (Deep Purple): come Hell or High Water DvD, 1993)
118 FGAIWaypoint * FGAIFlightPlan::createOnGround(FGAIAircraft * ac,
119 const std::string & aName,
120 const SGGeod & aPos, double aElev,
123 FGAIWaypoint *wpt = new FGAIWaypoint;
124 wpt->setName (aName );
125 wpt->setLongitude (aPos.getLongitudeDeg() );
126 wpt->setLatitude (aPos.getLatitudeDeg() );
127 wpt->setAltitude (aElev );
128 wpt->setSpeed (aSpeed );
129 wpt->setCrossat (-10000.1 );
130 wpt->setGear_down (true );
131 wpt->setFlaps_down (true );
132 wpt->setFinished (false );
133 wpt->setOn_ground (true );
134 wpt->setRouteIndex (0 );
138 FGAIWaypoint * FGAIFlightPlan::createInAir(FGAIAircraft * ac,
139 const std::string & aName,
140 const SGGeod & aPos, double aElev,
143 FGAIWaypoint * wpt = createOnGround(ac, aName, aPos, aElev, aSpeed);
144 wpt->setGear_down (false );
145 wpt->setFlaps_down (false );
146 wpt->setOn_ground (false );
150 FGAIWaypoint * FGAIFlightPlan::clone(FGAIWaypoint * aWpt)
152 FGAIWaypoint *wpt = new FGAIWaypoint;
153 wpt->setName ( aWpt->getName () );
154 wpt->setLongitude ( aWpt->getLongitude() );
155 wpt->setLatitude ( aWpt->getLatitude() );
156 wpt->setAltitude ( aWpt->getAltitude() );
157 wpt->setSpeed ( aWpt->getSpeed() );
158 wpt->setCrossat ( aWpt->getCrossat() );
159 wpt->setGear_down ( aWpt->getGear_down() );
160 wpt->setFlaps_down ( aWpt->getFlaps_down() );
161 wpt->setFinished ( aWpt->isFinished() );
162 wpt->setOn_ground ( aWpt->getOn_ground() );
163 wpt->setRouteIndex ( 0 );
169 FGAIWaypoint * FGAIFlightPlan::cloneWithPos(FGAIAircraft * ac, FGAIWaypoint * aWpt,
170 const std::string & aName,
173 FGAIWaypoint *wpt = clone(aWpt);
174 wpt->setName ( aName );
175 wpt->setLongitude ( aPos.getLongitudeDeg () );
176 wpt->setLatitude ( aPos.getLatitudeDeg () );
183 void FGAIFlightPlan::createDefaultTakeoffTaxi(FGAIAircraft * ac,
184 FGAirport * aAirport,
187 SGGeod runwayTakeoff = aRunway->pointOnCenterline(5.0);
188 double airportElev = aAirport->getElevation();
192 createOnGround(ac, "Airport Center", aAirport->geod(), airportElev,
193 ac->getPerformance()->vTaxi());
194 pushBackWaypoint(wpt);
196 createOnGround(ac, "Runway Takeoff", runwayTakeoff, airportElev,
197 ac->getPerformance()->vTaxi());
198 pushBackWaypoint(wpt);
201 bool FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft * ac, bool firstFlight,
204 const string & fltType,
205 const string & acType,
206 const string & airline)
208 double heading, lat, lon;
210 // If this function is called during initialization,
211 // make sure we obtain a valid gate ID first
212 // and place the model at the location of the gate.
214 if (!(apt->getDynamics()->getAvailableParking(&lat, &lon,
218 SG_LOG(SG_AI, SG_WARN, "Could not find parking for a " <<
220 " of flight type " << fltType <<
221 " of airline " << airline <<
222 " at airport " << apt->getId());
226 string rwyClass = getRunwayClassFromTrafficType(fltType);
228 // Only set this if it hasn't been set by ATC already.
229 if (activeRunway.empty()) {
230 //cerr << "Getting runway for " << ac->getTrafficRef()->getCallSign() << " at " << apt->getId() << endl;
231 double depHeading = ac->getTrafficRef()->getCourse();
232 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
235 FGRunway * rwy = apt->getRunwayByIdent(activeRunway);
236 assert( rwy != NULL );
237 SGGeod runwayTakeoff = rwy->pointOnCenterline(5.0);
239 FGGroundNetwork *gn = apt->getDynamics()->getGroundNetwork();
241 createDefaultTakeoffTaxi(ac, apt, rwy);
247 if (gn->getVersion() > 0) {
248 runwayId = gn->findNearestNodeOnRunway(runwayTakeoff);
250 runwayId = gn->findNearestNode(runwayTakeoff);
253 // A negative gateId indicates an overflow parking, use a
254 // fallback mechanism for this.
255 // Starting from gate 0 in this case is a bit of a hack
256 // which requires a more proper solution later on.
258 taxiRoute = new FGTaxiRoute;
260 // Determine which node to start from.
262 // Find out which node to start from
263 FGParking *park = apt->getDynamics()->getParking(gateId);
265 node = park->getPushBackPoint();
271 // HAndle case where parking doens't have a node
272 if ((node == 0) && park) {
276 node = lastNodeVisited;
280 *taxiRoute = gn->findShortestRoute(node, runwayId);
283 if (taxiRoute->empty()) {
284 createDefaultTakeoffTaxi(ac, apt, rwy);
289 //bool isPushBackPoint = false;
291 // If this is called during initialization, randomly
292 // skip a number of waypoints to get a more realistic
294 int nrWaypointsToSkip = rand() % taxiRoute->size();
295 // but make sure we always keep two active waypoints
296 // to prevent a segmentation fault
297 for (int i = 0; i < nrWaypointsToSkip - 3; i++) {
298 taxiRoute->next(&node);
300 apt->getDynamics()->releaseParking(gateId);
302 if (taxiRoute->size() > 1) {
303 taxiRoute->next(&node); // chop off the first waypoint, because that is already the last of the pushback route
307 // push each node on the taxi route as a waypoint
309 //cerr << "Building taxi route" << endl;
310 while (taxiRoute->next(&node, &route)) {
312 snprintf(buffer, 10, "%d", node);
314 apt->getDynamics()->getGroundNetwork()->findNode(node);
316 createOnGround(ac, buffer, tn->getGeod(), apt->getElevation(),
317 ac->getPerformance()->vTaxi());
318 wpt->setRouteIndex(route);
319 //cerr << "Nodes left " << taxiRoute->nodesLeft() << " ";
320 if (taxiRoute->nodesLeft() == 1) {
321 // Note that we actually have hold points in the ground network, but this is just an initial test.
322 //cerr << "Setting departurehold point: " << endl;
323 wpt->setName( wpt->getName() + string("DepartureHold"));
325 if (taxiRoute->nodesLeft() == 0) {
326 wpt->setName(wpt->getName() + string("Accel"));
328 pushBackWaypoint(wpt);
330 // Acceleration point, 105 meters into the runway,
331 SGGeod accelPoint = rwy->pointOnCenterline(105.0);
332 FGAIWaypoint *wpt = createOnGround(ac, "accel", accelPoint, apt->getElevation(), ac->getPerformance()->vRotate());
333 pushBackWaypoint(wpt);
335 //cerr << "[done]" << endl;
339 void FGAIFlightPlan::createDefaultLandingTaxi(FGAIAircraft * ac,
340 FGAirport * aAirport)
343 SGGeod::fromDeg(waypoints.back()->getLongitude(),
344 waypoints.back()->getLatitude());
345 double airportElev = aAirport->getElevation();
349 createOnGround(ac, "Runway Exit", lastWptPos, airportElev,
350 ac->getPerformance()->vTaxi());
351 pushBackWaypoint(wpt);
353 createOnGround(ac, "Airport Center", aAirport->geod(), airportElev,
354 ac->getPerformance()->vTaxi());
355 pushBackWaypoint(wpt);
357 double heading, lat, lon;
358 aAirport->getDynamics()->getParking(gateId, &lat, &lon, &heading);
360 createOnGround(ac, "ENDtaxi", SGGeod::fromDeg(lon, lat), airportElev,
361 ac->getPerformance()->vTaxi());
362 pushBackWaypoint(wpt);
365 bool FGAIFlightPlan::createLandingTaxi(FGAIAircraft * ac, FGAirport * apt,
367 const string & fltType,
368 const string & acType,
369 const string & airline)
371 double heading, lat, lon;
372 apt->getDynamics()->getAvailableParking(&lat, &lon, &heading,
373 &gateId, radius, fltType,
377 SGGeod::fromDeg(waypoints.back()->getLongitude(),
378 waypoints.back()->getLatitude());
379 FGGroundNetwork *gn = apt->getDynamics()->getGroundNetwork();
381 // Find a route from runway end to parking/gate.
383 createDefaultLandingTaxi(ac, apt);
389 if (gn->getVersion() == 1) {
390 runwayId = gn->findNearestNodeOnRunway(lastWptPos);
392 runwayId = gn->findNearestNode(lastWptPos);
394 //cerr << "Using network node " << runwayId << endl;
395 // A negative gateId indicates an overflow parking, use a
396 // fallback mechanism for this.
397 // Starting from gate 0 is a bit of a hack...
400 taxiRoute = new FGTaxiRoute;
402 *taxiRoute = gn->findShortestRoute(runwayId, gateId);
404 *taxiRoute = gn->findShortestRoute(runwayId, 0);
407 if (taxiRoute->empty()) {
408 createDefaultLandingTaxi(ac, apt);
414 int size = taxiRoute->size();
415 // Omit the last two waypoints, as
416 // those are created by createParking()
418 for (int i = 0; i < size - 2; i++) {
419 taxiRoute->next(&node, &route);
421 snprintf(buffer, 10, "%d", node);
422 FGTaxiNode *tn = gn->findNode(node);
424 createOnGround(ac, buffer, tn->getGeod(), apt->getElevation(),
425 ac->getPerformance()->vTaxi());
426 wpt->setRouteIndex(route);
427 pushBackWaypoint(wpt);
432 /*******************************************************************
435 * - Speed -> knots -> nm/hour
436 * - distance along runway =-> meters
437 * - accel / decel -> is given as knots/hour, but this is highly questionable:
438 * for a jet_transport performance class, a accel / decel rate of 5 / 2 is
439 * given respectively. According to performance data.cxx, a value of kts / second seems
440 * more likely however.
442 ******************************************************************/
443 bool FGAIFlightPlan::createTakeOff(FGAIAircraft * ac, bool firstFlight,
444 FGAirport * apt, double speed,
445 const string & fltType)
447 double accel = ac->getPerformance()->acceleration();
448 double vTaxi = ac->getPerformance()->vTaxi();
449 double vRotate = ac->getPerformance()->vRotate();
450 double vTakeoff = ac->getPerformance()->vTakeoff();
451 //double vClimb = ac->getPerformance()->vClimb();
453 double accelMetric = (accel * SG_NM_TO_METER) / 3600;
454 double vTaxiMetric = (vTaxi * SG_NM_TO_METER) / 3600;
455 double vRotateMetric = (vRotate * SG_NM_TO_METER) / 3600;
456 double vTakeoffMetric = (vTakeoff * SG_NM_TO_METER) / 3600;
457 //double vClimbMetric = (vClimb * SG_NM_TO_METER) / 3600;
458 // Acceleration = dV / dT
459 // Acceleration X dT = dV
460 // dT = dT / Acceleration
461 //d = (Vf^2 - Vo^2) / (2*a)
462 //double accelTime = (vRotate - vTaxi) / accel;
463 //cerr << "Using " << accelTime << " as total acceleration time" << endl;
464 double accelDistance =
465 (vRotateMetric * vRotateMetric -
466 vTaxiMetric * vTaxiMetric) / (2 * accelMetric);
467 //cerr << "Using " << accelDistance << " " << accelMetric << " " << vRotateMetric << endl;
469 // Get the current active runway, based on code from David Luff
470 // This should actually be unified and extended to include
471 // Preferential runway use schema's
472 // NOTE: DT (2009-01-18: IIRC, this is currently already the case,
473 // because the getActive runway function takes care of that.
475 string rwyClass = getRunwayClassFromTrafficType(fltType);
476 double heading = ac->getTrafficRef()->getCourse();
477 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
480 FGRunway * rwy = apt->getRunwayByIdent(activeRunway);
481 assert( rwy != NULL );
483 double airportElev = apt->getElevation();
487 (vTakeoffMetric * vTakeoffMetric -
488 vTaxiMetric * vTaxiMetric) / (2 * accelMetric);
489 //cerr << "Using " << accelDistance << " " << accelMetric << " " << vTakeoffMetric << endl;
490 SGGeod accelPoint = rwy->pointOnCenterline(105.0 + accelDistance);
491 wpt = createOnGround(ac, "rotate", accelPoint, airportElev, vTakeoff);
492 pushBackWaypoint(wpt);
495 ((vTakeoffMetric * 1.1) * (vTakeoffMetric * 1.1) -
496 vTaxiMetric * vTaxiMetric) / (2 * accelMetric);
497 //cerr << "Using " << accelDistance << " " << accelMetric << " " << vTakeoffMetric << endl;
498 accelPoint = rwy->pointOnCenterline(105.0 + accelDistance);
500 createOnGround(ac, "rotate", accelPoint, airportElev + 1000,
502 wpt->setOn_ground(false);
503 pushBackWaypoint(wpt);
505 wpt = cloneWithPos(ac, wpt, "3000 ft", rwy->end());
506 wpt->setAltitude(airportElev + 3000);
507 pushBackWaypoint(wpt);
509 // Finally, add two more waypoints, so that aircraft will remain under
510 // Tower control until they have reached the 3000 ft climb point
511 SGGeod pt = rwy->pointOnCenterline(5000 + rwy->lengthM() * 0.5);
512 wpt = cloneWithPos(ac, wpt, "5000 ft", pt);
513 wpt->setAltitude(airportElev + 5000);
514 pushBackWaypoint(wpt);
518 /*******************************************************************
520 * initialize the Aircraft at the parking location
521 ******************************************************************/
522 bool FGAIFlightPlan::createClimb(FGAIAircraft * ac, bool firstFlight,
523 FGAirport * apt, double speed, double alt,
524 const string & fltType)
527 // bool planLoaded = false;
529 double vClimb = ac->getPerformance()->vClimb();
532 string rwyClass = getRunwayClassFromTrafficType(fltType);
533 double heading = ac->getTrafficRef()->getCourse();
534 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
538 for (wpt_vector_iterator i = sid->getFirstWayPoint();
539 i != sid->getLastWayPoint(); i++) {
540 pushBackWaypoint(clone(*(i)));
541 //cerr << " Cloning waypoint " << endl;
544 FGRunway * rwy = apt->getRunwayByIdent(activeRunway);
545 assert( rwy != NULL );
547 SGGeod climb1 = rwy->pointOnCenterline(10 * SG_NM_TO_METER);
548 wpt = createInAir(ac, "10000ft climb", climb1, 10000, vClimb);
549 wpt->setGear_down(true);
550 wpt->setFlaps_down(true);
551 pushBackWaypoint(wpt);
553 SGGeod climb2 = rwy->pointOnCenterline(20 * SG_NM_TO_METER);
554 wpt = cloneWithPos(ac, wpt, "18000ft climb", climb2);
555 wpt->setAltitude(18000);
556 pushBackWaypoint(wpt);
563 /*******************************************************************
565 * Generate a flight path from the last waypoint of the cruise to
566 * the permission to land point
567 ******************************************************************/
568 bool FGAIFlightPlan::createDescent(FGAIAircraft * ac, FGAirport * apt,
569 double latitude, double longitude,
570 double speed, double alt,
571 const string & fltType,
572 double requiredDistance)
574 bool reposition = false;
576 double vDescent = ac->getPerformance()->vDescent();
577 double vApproach = ac->getPerformance()->vApproach();
578 double vTouchdown = ac->getPerformance()->vTouchdown();
581 //Beginning of Descent
582 string rwyClass = getRunwayClassFromTrafficType(fltType);
583 double heading = ac->getTrafficRef()->getCourse();
584 apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway,
586 FGRunway * rwy = apt->getRunwayByIdent(activeRunway);
587 assert( rwy != NULL );
589 // Create a slow descent path that ends 250 lateral to the runway.
590 double initialTurnRadius = getTurnRadius(vDescent, true);
591 //double finalTurnRadius = getTurnRadius(vApproach, true);
593 // get length of the downwind leg for the intended runway
594 double distanceOut = apt->getDynamics()->getApproachController()->getRunway(rwy->name())->getApproachDistance(); //12 * SG_NM_TO_METER;
595 //time_t previousArrivalTime= apt->getDynamics()->getApproachController()->getRunway(rwy->name())->getEstApproachTime();
598 SGGeod current = SGGeod::fromDegM(longitude, latitude, 0);
599 SGGeod initialTarget = rwy->pointOnCenterline(-distanceOut);
600 SGGeod refPoint = rwy->pointOnCenterline(0);
601 double distance = SGGeodesy::distanceM(current, initialTarget);
602 double azimuth = SGGeodesy::courseDeg(current, initialTarget);
605 // To prevent absurdly steep approaches, compute the origin from where the approach should have started
608 if (ac->getTrafficRef()->getCallSign() ==
609 fgGetString("/ai/track-callsign")) {
610 //cerr << "Reposition information: Actual distance " << distance << ". required distance " << requiredDistance << endl;
614 if (distance < requiredDistance * 0.8) {
616 SGGeodesy::direct(initialTarget, azimuth,
617 -requiredDistance, origin, dummyAz2);
619 distance = SGGeodesy::distanceM(current, initialTarget);
620 azimuth = SGGeodesy::courseDeg(current, initialTarget);
626 double dAlt = 0; // = alt - (apt->getElevation() + 2000);
628 if (apt->getDynamics()->getGroundNetwork()) {
629 int node = apt->getDynamics()->getGroundNetwork()->findNearestNode(refPoint);
630 tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
633 dAlt = alt - ((tn->getElevationFt(apt->getElevation())) + 2000);
635 dAlt = alt - (apt->getElevation() + 2000);
638 double nPoints = 100;
642 // The descent path contains the following phases:
643 // 1) a linear glide path from the initial position to
644 // 2) a semi circle turn to final
647 //cerr << "Phase 1: Linear Descent path to runway" << rwy->name() << endl;
648 // Create an initial destination point on a semicircle
649 //cerr << "lateral offset : " << lateralOffset << endl;
650 //cerr << "Distance : " << distance << endl;
651 //cerr << "Azimuth : " << azimuth << endl;
652 //cerr << "Initial Lateral point: " << lateralOffset << endl;
653 double lat = refPoint.getLatitudeDeg();
654 double lon = refPoint.getLongitudeDeg();
655 //cerr << "Reference point (" << lat << ", " << lon << ")." << endl;
656 lat = initialTarget.getLatitudeDeg();
657 lon = initialTarget.getLongitudeDeg();
658 //cerr << "Initial Target point (" << lat << ", " << lon << ")." << endl;
660 double ratio = initialTurnRadius / distance;
666 double newHeading = asin(ratio) * SG_RADIANS_TO_DEGREES;
668 cos(newHeading * SG_DEGREES_TO_RADIANS) * distance;
669 //cerr << "new distance " << newDistance << ". additional Heading " << newHeading << endl;
670 double side = azimuth - rwy->headingDeg();
671 double lateralOffset = initialTurnRadius;
677 // Calculate the ETA at final, based on remaining distance, and approach speed.
678 // distance should really consist of flying time to terniary target, plus circle
679 // but the distance to secondary target should work as a reasonable approximation
680 // aditionally add the amount of distance covered by making a turn of "side"
681 double turnDistance = (2 * M_PI * initialTurnRadius) * (side / 360.0);
683 (turnDistance + distance) / ((vDescent * SG_NM_TO_METER) / 3600.0);
684 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
685 //if (ac->getTrafficRef()->getCallSign() == fgGetString("/ai/track-callsign")) {
686 // cerr << " Arrival time estimation: turn angle " << side << ". Turn distance " << turnDistance << ". Linear distance " << distance << ". Time to go " << remaining << endl;
690 time_t eta = now + remaining;
691 //choose a distance to the runway such that it will take at least 60 seconds more
692 // time to get there than the previous aircraft.
693 // Don't bother when aircraft need to be repositioned, because that marks the initialization phased...
697 if (reposition == false) {
699 apt->getDynamics()->getApproachController()->getRunway(rwy->
702 requestTimeSlot(eta);
706 //if ((eta < (previousArrivalTime+60)) && (reposition == false)) {
707 arrivalTime = newEta;
708 time_t additionalTimeNeeded = newEta - eta;
709 double distanceCovered =
710 ((vApproach * SG_NM_TO_METER) / 3600.0) * additionalTimeNeeded;
711 distanceOut += distanceCovered;
712 //apt->getDynamics()->getApproachController()->getRunway(rwy->name())->setEstApproachTime(eta+additionalTimeNeeded);
713 //cerr << "Adding additional distance: " << distanceCovered << " to allow " << additionalTimeNeeded << " seconds of flying time" << endl << endl;
715 //apt->getDynamics()->getApproachController()->getRunway(rwy->name())->setEstApproachTime(eta);
717 //cerr << "Timing information : Previous eta: " << previousArrivalTime << ". Current ETA : " << eta << endl;
719 SGGeod secondaryTarget =
720 rwy->pointOffCenterline(-distanceOut, lateralOffset);
721 initialTarget = rwy->pointOnCenterline(-distanceOut);
722 distance = SGGeodesy::distanceM(origin, secondaryTarget);
723 azimuth = SGGeodesy::courseDeg(origin, secondaryTarget);
726 lat = secondaryTarget.getLatitudeDeg();
727 lon = secondaryTarget.getLongitudeDeg();
728 //cerr << "Secondary Target point (" << lat << ", " << lon << ")." << endl;
729 //cerr << "Distance : " << distance << endl;
730 //cerr << "Azimuth : " << azimuth << endl;
733 ratio = initialTurnRadius / distance;
738 newHeading = asin(ratio) * SG_RADIANS_TO_DEGREES;
739 newDistance = cos(newHeading * SG_DEGREES_TO_RADIANS) * distance;
740 //cerr << "new distance realative to secondary target: " << newDistance << ". additional Heading " << newHeading << endl;
742 azimuth += newHeading;
744 azimuth -= newHeading;
747 SGGeod tertiaryTarget;
748 SGGeodesy::direct(origin, azimuth,
749 newDistance, tertiaryTarget, dummyAz2);
751 lat = tertiaryTarget.getLatitudeDeg();
752 lon = tertiaryTarget.getLongitudeDeg();
753 //cerr << "tertiary Target point (" << lat << ", " << lon << ")." << endl;
756 for (int i = 1; i < nPoints; i++) {
758 double currentDist = i * (newDistance / nPoints);
759 double currentAltitude = alt - (i * (dAlt / nPoints));
760 SGGeodesy::direct(origin, azimuth, currentDist, result, dummyAz2);
761 snprintf(buffer, 16, "descent%03d", i);
762 wpt = createInAir(ac, buffer, result, currentAltitude, vDescent);
763 wpt->setCrossat(currentAltitude);
764 wpt->setTrackLength((newDistance / nPoints));
765 pushBackWaypoint(wpt);
766 //cerr << "Track Length : " << wpt->trackLength;
767 //cerr << " Position : " << result.getLatitudeDeg() << " " << result.getLongitudeDeg() << " " << currentAltitude << endl;
770 //cerr << "Phase 2: Circle " << endl;
771 double initialAzimuth =
772 SGGeodesy::courseDeg(secondaryTarget, tertiaryTarget);
773 double finalAzimuth =
774 SGGeodesy::courseDeg(secondaryTarget, initialTarget);
776 //cerr << "Angles from secondary target: " << initialAzimuth << " " << finalAzimuth << endl;
777 int increment, startval, endval;
778 // circle right around secondary target if orig of position is to the right of the runway
779 // i.e. use negative angles; else circle leftward and use postivi
782 startval = floor(initialAzimuth);
783 endval = ceil(finalAzimuth);
784 if (endval > startval) {
789 startval = ceil(initialAzimuth);
790 endval = floor(finalAzimuth);
791 if (endval < startval) {
797 //cerr << "creating circle between " << startval << " and " << endval << " using " << increment << endl;
798 //FGTaxiNode * tn = apt->getDynamics()->getGroundNetwork()->findNearestNode(initialTarget);
799 double currentAltitude = 0;
801 currentAltitude = (tn->getElevationFt(apt->getElevation())) + 2000;
803 currentAltitude = apt->getElevation() + 2000;
806 double trackLength = (2 * M_PI * initialTurnRadius) / 360.0;
807 for (int i = startval; i != endval; i += increment) {
809 //double currentAltitude = apt->getElevation() + 2000;
811 SGGeodesy::direct(secondaryTarget, i,
812 initialTurnRadius, result, dummyAz2);
813 snprintf(buffer, 16, "turn%03d", i);
814 wpt = createInAir(ac, buffer, result, currentAltitude, vDescent);
815 wpt->setCrossat(currentAltitude);
816 wpt->setTrackLength(trackLength);
817 //cerr << "Track Length : " << wpt->trackLength;
818 pushBackWaypoint(wpt);
819 //cerr << " Position : " << result.getLatitudeDeg() << " " << result.getLongitudeDeg() << " " << currentAltitude << endl;
823 // The approach leg should bring the aircraft to approximately 4-6 nm out, after which the landing phase should take over.
824 //cerr << "Phase 3: Approach" << endl;
825 double tgt_speed = vApproach;
826 distanceOut -= distanceCovered;
827 double touchDownPoint = 0; //(rwy->lengthM() * 0.1);
828 for (int i = 1; i < nPoints; i++) {
830 double currentDist = i * (distanceOut / nPoints);
831 //double currentAltitude =
832 // apt->getElevation() + 2000 - (i * 2000 / (nPoints-1));
833 double alt = currentAltitude - (i * 2000 / (nPoints - 1));
834 snprintf(buffer, 16, "final%03d", i);
835 result = rwy->pointOnCenterline((-distanceOut) + currentDist + touchDownPoint);
836 if (i == nPoints - 30) {
837 tgt_speed = vTouchdown;
839 wpt = createInAir(ac, buffer, result, alt, tgt_speed);
840 wpt->setCrossat(alt);
841 wpt->setTrackLength((distanceOut / nPoints));
842 // account for the extra distance due to an extended downwind leg
844 wpt->setTrackLength(wpt->getTrackLength() + distanceCovered);
846 //cerr << "Track Length : " << wpt->trackLength;
847 pushBackWaypoint(wpt);
848 //if (apt->ident() == fgGetString("/sim/presets/airport-id")) {
849 // cerr << " Position : " << result.getLatitudeDeg() << " " << result.getLongitudeDeg() << " " << currentAltitude << " " << apt->getElevation() << " " << distanceOut << endl;
853 //cerr << "Done" << endl;
855 // Erase the two bogus BOD points: Note check for conflicts with scripted AI flightPlans
856 IncrementWaypoint(true);
857 IncrementWaypoint(true);
861 //double minDistance = HUGE_VAL;
863 tempDistance = SGGeodesy::distanceM(current, initialTarget);
865 tempDistance / ((vDescent * SG_NM_TO_METER) / 3600.0) + now;
867 apt->getDynamics()->getApproachController()->getRunway(rwy->
870 requestTimeSlot(eta);
871 arrivalTime = newEta;
873 ((vDescent * SG_NM_TO_METER) / 3600.0) * (newEta - now);
874 //cerr << "Repositioning information : eta" << eta << ". New ETA " << newEta << ". Diff = " << (newEta - eta) << ". Distance = " << tempDistance << ". New distance = " << newDistance << endl;
875 IncrementWaypoint(true); // remove waypoint BOD2
876 while (checkTrackLength("final001") > newDistance) {
877 IncrementWaypoint(true);
879 //cerr << "Repositioning to waypoint " << (*waypoints.begin())->name << endl;
880 ac->resetPositionFromFlightPlan();
882 waypoints[1]->setName( (waypoints[1]->getName() + string("legend")));
883 waypoints.back()->setName(waypoints.back()->getName() + "LandingThreshold");
887 /*******************************************************************
889 * Create a flight path from the "permision to land" point (currently
890 hardcoded at 5000 meters from the threshold) to the threshold, at
891 a standard glide slope angle of 3 degrees.
892 Position : 50.0354 8.52592 384 364 11112
893 ******************************************************************/
894 bool FGAIFlightPlan::createLanding(FGAIAircraft * ac, FGAirport * apt,
895 const string & fltType)
897 double vTouchdown = ac->getPerformance()->vTouchdown();
898 double vTaxi = ac->getPerformance()->vTaxi();
899 double decel = ac->getPerformance()->deceleration() * 1.4;
901 double vTouchdownMetric = (vTouchdown * SG_NM_TO_METER) / 3600;
902 double vTaxiMetric = (vTaxi * SG_NM_TO_METER) / 3600;
903 double decelMetric = (decel * SG_NM_TO_METER) / 3600;
905 //string rwyClass = getRunwayClassFromTrafficType(fltType);
906 //double heading = ac->getTrafficRef()->getCourse();
907 //apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading);
908 //rwy = apt->getRunwayByIdent(activeRunway);
912 //double aptElev = apt->getElevation();
915 FGRunway * rwy = apt->getRunwayByIdent(activeRunway);
916 assert( rwy != NULL );
917 SGGeod refPoint = rwy->pointOnCenterline(0);
919 if (apt->getDynamics()->getGroundNetwork()) {
920 int node = apt->getDynamics()->getGroundNetwork()->findNearestNode(refPoint);
921 tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
924 currElev = tn->getElevationFt(apt->getElevation());
926 currElev = apt->getElevation();
933 /*double distanceOut = rwy->lengthM() * .1;
935 for (int i = 1; i < nPoints; i++) {
936 snprintf(buffer, 12, "flare%d", i);
937 double currentDist = i * (distanceOut / nPoints);
938 double currentAltitude = apt->getElevation() + 20 - (i * 20 / nPoints);
939 coord = rwy->pointOnCenterline((currentDist * (i / nPoints)));
940 wpt = createInAir(ac, buffer, coord, currentAltitude, (vTouchdown));
942 double rolloutDistance =
943 (vTouchdownMetric * vTouchdownMetric - vTaxiMetric * vTaxiMetric) / (2 * decelMetric);
944 //cerr << " touchdown speed = " << vTouchdown << ". Rollout distance " << rolloutDistance << endl;
946 for (int i = 1; i < nPoints; i++) {
947 snprintf(buffer, 12, "landing03%d", i);
949 coord = rwy->pointOnCenterline((rolloutDistance * ((double) i / (double) nPoints)));
950 wpt = createOnGround(ac, buffer, coord, currElev, 2*vTaxi);
951 wpt->setCrossat(currElev);
952 pushBackWaypoint(wpt);
954 wpt->setSpeed(vTaxi);
955 double mindist = 1.1 * rolloutDistance;
956 double maxdist = rwy->lengthM();
957 //cerr << "Finding nearest exit" << endl;
958 FGGroundNetwork *gn = apt->getDynamics()->getGroundNetwork();
961 for (int i = ceil(mindist); i < floor(maxdist); i++) {
962 coord = rwy->pointOnCenterline(mindist);
964 if (gn->getVersion() > 0) {
965 nodeId = gn->findNearestNodeOnRunway(coord);
967 nodeId = gn->findNearestNode(coord);
970 tn = gn->findNode(nodeId);
974 double dist = SGGeodesy::distanceM(coord, tn->getGeod());
975 if (dist < (min + 0.75)) {
981 wpt = createOnGround(ac, buffer, tn->getGeod(), currElev, vTaxi);
982 pushBackWaypoint(wpt);
985 //cerr << "Done. " << endl;
989 wpt = createOnGround(ac, "Threshold", rwy->threshold(), aptElev, vTouchdown);
990 wpt->crossat = apt->getElevation();
991 pushBackWaypoint(wpt);
994 wpt = createOnGround(ac, "Center", rwy->geod(), aptElev, vTaxi*2);
995 pushBackWaypoint(wpt);
997 SGGeod rollOut = rwy->pointOnCenterline(rwy->lengthM() * 0.9);
998 wpt = createOnGround(ac, "Roll Out", rollOut, aptElev, vTaxi);
999 wpt->crossat = apt->getElevation();
1000 pushBackWaypoint(wpt);
1005 /*******************************************************************
1007 * initialize the Aircraft at the parking location
1008 ******************************************************************/
1009 bool FGAIFlightPlan::createParking(FGAIAircraft * ac, FGAirport * apt,
1013 double aptElev = apt->getElevation();
1014 double lat = 0.0, lat2 = 0.0;
1015 double lon = 0.0, lon2 = 0.0;
1017 double heading = 0.0;
1019 double vTaxi = ac->getPerformance()->vTaxi();
1020 double vTaxiReduced = vTaxi * (2.0 / 3.0);
1021 apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
1025 geo_direct_wgs_84(0, lat, lon, heading,
1026 2.2 * radius, &lat2, &lon2, &az2);
1028 createOnGround(ac, "taxiStart", SGGeod::fromDeg(lon2, lat2),
1029 aptElev, vTaxiReduced);
1030 pushBackWaypoint(wpt);
1032 geo_direct_wgs_84(0, lat, lon, heading,
1033 0.1 * radius, &lat2, &lon2, &az2);
1036 createOnGround(ac, "taxiStart2", SGGeod::fromDeg(lon2, lat2),
1037 aptElev, vTaxiReduced);
1038 pushBackWaypoint(wpt);
1041 createOnGround(ac, "END-Parking", SGGeod::fromDeg(lon, lat), aptElev,
1043 pushBackWaypoint(wpt);
1049 * @param fltType a string describing the type of
1050 * traffic, normally used for gate assignments
1051 * @return a converted string that gives the runway
1052 * preference schedule to be used at aircraft having
1053 * a preferential runway schedule implemented (i.e.
1054 * having a rwyprefs.xml file
1056 * Currently valid traffic types for gate assignment:
1057 * - gate (commercial gate)
1058 * - cargo (commercial gargo),
1059 * - ga (general aviation) ,
1060 * - ul (ultralight),
1061 * - mil-fighter (military - fighter),
1062 * - mil-transport (military - transport)
1064 * Valid runway classes:
1065 * - com (commercial traffic: jetliners, passenger and cargo)
1066 * - gen (general aviation)
1067 * - ul (ultralight: I can imagine that these may share a runway with ga on some airports)
1068 * - mil (all military traffic)
1070 string FGAIFlightPlan::getRunwayClassFromTrafficType(string fltType)
1072 if ((fltType == "gate") || (fltType == "cargo")) {
1073 return string("com");
1075 if (fltType == "ga") {
1076 return string("gen");
1078 if (fltType == "ul") {
1079 return string("ul");
1081 if ((fltType == "mil-fighter") || (fltType == "mil-transport")) {
1082 return string("mil");
1084 return string("com");
1088 double FGAIFlightPlan::getTurnRadius(double speed, bool inAir)
1091 if (inAir == false) {
1092 turn_radius = ((360 / 30) * fabs(speed)) / (2 * M_PI);
1094 turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank