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>
41 /* FGAIFlightPlan::create()
42 * dynamically create a flight plan for AI traffic, based on data provided by the
43 * Traffic Manager, when reading a filed flightplan failes. (DT, 2004/07/10)
45 * This is the top-level function, and the only one that is publicly available.
50 // Check lat/lon values during initialization;
51 bool FGAIFlightPlan::create(FGAIAircraft * ac, FGAirport * dep,
52 FGAirport * arr, int legNr, double alt,
53 double speed, double latitude,
54 double longitude, bool firstFlight,
55 double radius, const string & fltType,
56 const string & aircraftType,
57 const string & airline, double distance)
60 int currWpt = wpt_iterator - waypoints.begin();
63 retVal = createPushBack(ac, firstFlight, dep, latitude, longitude,
64 radius, fltType, aircraftType, airline);
67 waypoints.back()->setName( waypoints.back()->getName() + string("legend"));
68 retVal = createTakeoffTaxi(ac, false, dep, radius, fltType, aircraftType, airline);
72 retVal = createTakeoffTaxi(ac, firstFlight, dep, radius, fltType,
73 aircraftType, airline);
76 retVal = createTakeOff(ac, firstFlight, dep, speed, fltType);
79 retVal = createClimb(ac, firstFlight, dep, speed, alt, fltType);
82 retVal = createCruise(ac, firstFlight, dep, arr, latitude, longitude, speed,
86 retVal = createDescent(ac, arr, latitude, longitude, speed, alt, fltType,
90 retVal = createLanding(ac, arr, fltType);
93 retVal = createLandingTaxi(ac, arr, radius, fltType, aircraftType, airline);
96 retVal = createParking(ac, arr, radius);
100 SG_LOG(SG_INPUT, SG_ALERT,
101 "AIFlightPlan::create() attempting to create unknown leg"
102 " this is probably an internal program error");
104 wpt_iterator = waypoints.begin() + currWpt;
105 //don't increment leg right away, but only once we pass the actual last waypoint that was created.
106 // to do so, mark the last waypoint with a special status flag
108 waypoints.back()->setName( waypoints.back()->getName() + string("legend"));
109 // "It's pronounced Leg-end" (Roger Glover (Deep Purple): come Hell or High Water DvD, 1993)
117 FGAIWaypoint * FGAIFlightPlan::createOnGround(FGAIAircraft * ac,
118 const std::string & aName,
119 const SGGeod & aPos, double aElev,
122 FGAIWaypoint *wpt = new FGAIWaypoint;
123 wpt->setName (aName );
124 wpt->setLongitude (aPos.getLongitudeDeg() );
125 wpt->setLatitude (aPos.getLatitudeDeg() );
126 wpt->setAltitude (aElev );
127 wpt->setSpeed (aSpeed );
128 wpt->setCrossat (-10000.1 );
129 wpt->setGear_down (true );
130 wpt->setFlaps_down (true );
131 wpt->setFinished (false );
132 wpt->setOn_ground (true );
133 wpt->setRouteIndex (0 );
137 FGAIWaypoint * FGAIFlightPlan::createInAir(FGAIAircraft * ac,
138 const std::string & aName,
139 const SGGeod & aPos, double aElev,
142 FGAIWaypoint * wpt = createOnGround(ac, aName, aPos, aElev, aSpeed);
143 wpt->setGear_down (false );
144 wpt->setFlaps_down (false );
145 wpt->setOn_ground (false );
149 FGAIWaypoint * FGAIFlightPlan::clone(FGAIWaypoint * aWpt)
151 FGAIWaypoint *wpt = new FGAIWaypoint;
152 wpt->setName ( aWpt->getName () );
153 wpt->setLongitude ( aWpt->getLongitude() );
154 wpt->setLatitude ( aWpt->getLatitude() );
155 wpt->setAltitude ( aWpt->getAltitude() );
156 wpt->setSpeed ( aWpt->getSpeed() );
157 wpt->setCrossat ( aWpt->getCrossat() );
158 wpt->setGear_down ( aWpt->getGear_down() );
159 wpt->setFlaps_down ( aWpt->getFlaps_down() );
160 wpt->setFinished ( aWpt->isFinished() );
161 wpt->setOn_ground ( aWpt->getOn_ground() );
162 wpt->setRouteIndex ( 0 );
168 FGAIWaypoint * FGAIFlightPlan::cloneWithPos(FGAIAircraft * ac, FGAIWaypoint * aWpt,
169 const std::string & aName,
172 FGAIWaypoint *wpt = clone(aWpt);
173 wpt->setName ( aName );
174 wpt->setLongitude ( aPos.getLongitudeDeg () );
175 wpt->setLatitude ( aPos.getLatitudeDeg () );
182 void FGAIFlightPlan::createDefaultTakeoffTaxi(FGAIAircraft * ac,
183 FGAirport * aAirport,
186 SGGeod runwayTakeoff = aRunway->pointOnCenterline(5.0);
187 double airportElev = aAirport->getElevation();
191 createOnGround(ac, "Airport Center", aAirport->geod(), airportElev,
192 ac->getPerformance()->vTaxi());
193 waypoints.push_back(wpt);
195 createOnGround(ac, "Runway Takeoff", runwayTakeoff, airportElev,
196 ac->getPerformance()->vTaxi());
197 waypoints.push_back(wpt);
200 bool FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft * ac, bool firstFlight,
203 const string & fltType,
204 const string & acType,
205 const string & airline)
207 double heading, lat, lon;
209 // If this function is called during initialization,
210 // make sure we obtain a valid gate ID first
211 // and place the model at the location of the gate.
213 if (!(apt->getDynamics()->getAvailableParking(&lat, &lon,
217 SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " <<
219 " of flight type " << fltType <<
220 " of airline " << airline <<
221 " at airport " << apt->getId());
225 string rwyClass = getRunwayClassFromTrafficType(fltType);
227 // Only set this if it hasn't been set by ATC already.
228 if (activeRunway.empty()) {
229 //cerr << "Getting runway for " << ac->getTrafficRef()->getCallSign() << " at " << apt->getId() << endl;
230 double depHeading = ac->getTrafficRef()->getCourse();
231 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
234 FGRunway * rwy = apt->getRunwayByIdent(activeRunway);
235 assert( rwy != NULL );
236 SGGeod runwayTakeoff = rwy->pointOnCenterline(5.0);
238 FGGroundNetwork *gn = apt->getDynamics()->getGroundNetwork();
240 createDefaultTakeoffTaxi(ac, apt, rwy);
245 int runwayId = gn->findNearestNode(runwayTakeoff);
247 // A negative gateId indicates an overflow parking, use a
248 // fallback mechanism for this.
249 // Starting from gate 0 in this case is a bit of a hack
250 // which requires a more proper solution later on.
252 taxiRoute = new FGTaxiRoute;
254 // Determine which node to start from.
256 // Find out which node to start from
257 FGParking *park = apt->getDynamics()->getParking(gateId);
259 node = park->getPushBackPoint();
265 // HAndle case where parking doens't have a node
266 if ((node == 0) && park) {
270 node = lastNodeVisited;
274 *taxiRoute = gn->findShortestRoute(node, runwayId);
277 if (taxiRoute->empty()) {
278 createDefaultTakeoffTaxi(ac, apt, rwy);
283 //bool isPushBackPoint = false;
285 // If this is called during initialization, randomly
286 // skip a number of waypoints to get a more realistic
288 int nrWaypointsToSkip = rand() % taxiRoute->size();
289 // but make sure we always keep two active waypoints
290 // to prevent a segmentation fault
291 for (int i = 0; i < nrWaypointsToSkip - 3; i++) {
292 taxiRoute->next(&node);
294 apt->getDynamics()->releaseParking(gateId);
296 if (taxiRoute->size() > 1) {
297 taxiRoute->next(&node); // chop off the first waypoint, because that is already the last of the pushback route
301 // push each node on the taxi route as a waypoint
303 //cerr << "Building taxi route" << endl;
304 while (taxiRoute->next(&node, &route)) {
306 snprintf(buffer, 10, "%d", node);
308 apt->getDynamics()->getGroundNetwork()->findNode(node);
310 createOnGround(ac, buffer, tn->getGeod(), apt->getElevation(),
311 ac->getPerformance()->vTaxi());
312 wpt->setRouteIndex(route);
313 //cerr << "Nodes left " << taxiRoute->nodesLeft() << " ";
314 if (taxiRoute->nodesLeft() == 1) {
315 // Note that we actually have hold points in the ground network, but this is just an initial test.
316 //cerr << "Setting departurehold point: " << endl;
317 wpt->setName( wpt->getName() + string("DepartureHold"));
319 waypoints.push_back(wpt);
321 // Acceleration point, 105 meters into the runway,
322 SGGeod accelPoint = rwy->pointOnCenterline(105.0);
323 FGAIWaypoint *wpt = createOnGround(ac, "accel", accelPoint, apt->getElevation(), ac->getPerformance()->vRotate());
324 waypoints.push_back(wpt);
326 //cerr << "[done]" << endl;
330 void FGAIFlightPlan::createDefaultLandingTaxi(FGAIAircraft * ac,
331 FGAirport * aAirport)
334 SGGeod::fromDeg(waypoints.back()->getLongitude(),
335 waypoints.back()->getLatitude());
336 double airportElev = aAirport->getElevation();
340 createOnGround(ac, "Runway Exit", lastWptPos, airportElev,
341 ac->getPerformance()->vTaxi());
342 waypoints.push_back(wpt);
344 createOnGround(ac, "Airport Center", aAirport->geod(), airportElev,
345 ac->getPerformance()->vTaxi());
346 waypoints.push_back(wpt);
348 double heading, lat, lon;
349 aAirport->getDynamics()->getParking(gateId, &lat, &lon, &heading);
351 createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), airportElev,
352 ac->getPerformance()->vTaxi());
353 waypoints.push_back(wpt);
356 bool FGAIFlightPlan::createLandingTaxi(FGAIAircraft * ac, FGAirport * apt,
358 const string & fltType,
359 const string & acType,
360 const string & airline)
362 double heading, lat, lon;
363 apt->getDynamics()->getAvailableParking(&lat, &lon, &heading,
364 &gateId, radius, fltType,
368 SGGeod::fromDeg(waypoints.back()->getLongitude(),
369 waypoints.back()->getLatitude());
370 FGGroundNetwork *gn = apt->getDynamics()->getGroundNetwork();
372 // Find a route from runway end to parking/gate.
374 createDefaultLandingTaxi(ac, apt);
379 int runwayId = gn->findNearestNode(lastWptPos);
380 // A negative gateId indicates an overflow parking, use a
381 // fallback mechanism for this.
382 // Starting from gate 0 is a bit of a hack...
385 taxiRoute = new FGTaxiRoute;
387 *taxiRoute = gn->findShortestRoute(runwayId, gateId);
389 *taxiRoute = gn->findShortestRoute(runwayId, 0);
392 if (taxiRoute->empty()) {
393 createDefaultLandingTaxi(ac, apt);
399 int size = taxiRoute->size();
400 // Omit the last two waypoints, as
401 // those are created by createParking()
403 for (int i = 0; i < size - 2; i++) {
404 taxiRoute->next(&node, &route);
406 snprintf(buffer, 10, "%d", node);
407 FGTaxiNode *tn = gn->findNode(node);
409 createOnGround(ac, buffer, tn->getGeod(), apt->getElevation(),
410 ac->getPerformance()->vTaxi());
411 wpt->setRouteIndex(route);
412 waypoints.push_back(wpt);
417 /*******************************************************************
420 * - Speed -> knots -> nm/hour
421 * - distance along runway =-> meters
422 * - accel / decel -> is given as knots/hour, but this is highly questionable:
423 * for a jet_transport performance class, a accel / decel rate of 5 / 2 is
424 * given respectively. According to performance data.cxx, a value of kts / second seems
425 * more likely however.
427 ******************************************************************/
428 bool FGAIFlightPlan::createTakeOff(FGAIAircraft * ac, bool firstFlight,
429 FGAirport * apt, double speed,
430 const string & fltType)
432 double accel = ac->getPerformance()->acceleration();
433 double vTaxi = ac->getPerformance()->vTaxi();
434 double vRotate = ac->getPerformance()->vRotate();
435 double vTakeoff = ac->getPerformance()->vTakeoff();
436 //double vClimb = ac->getPerformance()->vClimb();
438 double accelMetric = (accel * SG_NM_TO_METER) / 3600;
439 double vTaxiMetric = (vTaxi * SG_NM_TO_METER) / 3600;
440 double vRotateMetric = (vRotate * SG_NM_TO_METER) / 3600;
441 double vTakeoffMetric = (vTakeoff * SG_NM_TO_METER) / 3600;
442 //double vClimbMetric = (vClimb * SG_NM_TO_METER) / 3600;
443 // Acceleration = dV / dT
444 // Acceleration X dT = dV
445 // dT = dT / Acceleration
446 //d = (Vf^2 - Vo^2) / (2*a)
447 //double accelTime = (vRotate - vTaxi) / accel;
448 //cerr << "Using " << accelTime << " as total acceleration time" << endl;
449 double accelDistance =
450 (vRotateMetric * vRotateMetric -
451 vTaxiMetric * vTaxiMetric) / (2 * accelMetric);
452 //cerr << "Using " << accelDistance << " " << accelMetric << " " << vRotateMetric << endl;
454 // Get the current active runway, based on code from David Luff
455 // This should actually be unified and extended to include
456 // Preferential runway use schema's
457 // NOTE: DT (2009-01-18: IIRC, this is currently already the case,
458 // because the getActive runway function takes care of that.
460 string rwyClass = getRunwayClassFromTrafficType(fltType);
461 double heading = ac->getTrafficRef()->getCourse();
462 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
465 FGRunway * rwy = apt->getRunwayByIdent(activeRunway);
466 assert( rwy != NULL );
468 double airportElev = apt->getElevation();
472 (vTakeoffMetric * vTakeoffMetric -
473 vTaxiMetric * vTaxiMetric) / (2 * accelMetric);
474 //cerr << "Using " << accelDistance << " " << accelMetric << " " << vTakeoffMetric << endl;
475 SGGeod accelPoint = rwy->pointOnCenterline(105.0 + accelDistance);
476 wpt = createOnGround(ac, "rotate", accelPoint, airportElev, vTakeoff);
477 waypoints.push_back(wpt);
480 ((vTakeoffMetric * 1.1) * (vTakeoffMetric * 1.1) -
481 vTaxiMetric * vTaxiMetric) / (2 * accelMetric);
482 //cerr << "Using " << accelDistance << " " << accelMetric << " " << vTakeoffMetric << endl;
483 accelPoint = rwy->pointOnCenterline(105.0 + accelDistance);
485 createOnGround(ac, "rotate", accelPoint, airportElev + 1000,
487 wpt->setOn_ground(false);
488 waypoints.push_back(wpt);
490 wpt = cloneWithPos(ac, wpt, "3000 ft", rwy->end());
491 wpt->setAltitude(airportElev + 3000);
492 waypoints.push_back(wpt);
494 // Finally, add two more waypoints, so that aircraft will remain under
495 // Tower control until they have reached the 3000 ft climb point
496 SGGeod pt = rwy->pointOnCenterline(5000 + rwy->lengthM() * 0.5);
497 wpt = cloneWithPos(ac, wpt, "5000 ft", pt);
498 wpt->setAltitude(airportElev + 5000);
499 waypoints.push_back(wpt);
503 /*******************************************************************
505 * initialize the Aircraft at the parking location
506 ******************************************************************/
507 bool FGAIFlightPlan::createClimb(FGAIAircraft * ac, bool firstFlight,
508 FGAirport * apt, double speed, double alt,
509 const string & fltType)
512 // bool planLoaded = false;
514 double vClimb = ac->getPerformance()->vClimb();
517 string rwyClass = getRunwayClassFromTrafficType(fltType);
518 double heading = ac->getTrafficRef()->getCourse();
519 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
523 for (wpt_vector_iterator i = sid->getFirstWayPoint();
524 i != sid->getLastWayPoint(); i++) {
525 waypoints.push_back(clone(*(i)));
526 //cerr << " Cloning waypoint " << endl;
529 FGRunway * rwy = apt->getRunwayByIdent(activeRunway);
530 assert( rwy != NULL );
532 SGGeod climb1 = rwy->pointOnCenterline(10 * SG_NM_TO_METER);
533 wpt = createInAir(ac, "10000ft climb", climb1, vClimb, 10000);
534 wpt->setGear_down(true);
535 wpt->setFlaps_down(true);
536 waypoints.push_back(wpt);
538 SGGeod climb2 = rwy->pointOnCenterline(20 * SG_NM_TO_METER);
539 wpt = cloneWithPos(ac, wpt, "18000ft climb", climb2);
540 wpt->setAltitude(18000);
541 waypoints.push_back(wpt);
548 /*******************************************************************
550 * Generate a flight path from the last waypoint of the cruise to
551 * the permission to land point
552 ******************************************************************/
553 bool FGAIFlightPlan::createDescent(FGAIAircraft * ac, FGAirport * apt,
554 double latitude, double longitude,
555 double speed, double alt,
556 const string & fltType,
557 double requiredDistance)
559 bool reposition = false;
561 double vDescent = ac->getPerformance()->vDescent();
562 double vApproach = ac->getPerformance()->vApproach();
565 //Beginning of Descent
566 string rwyClass = getRunwayClassFromTrafficType(fltType);
567 double heading = ac->getTrafficRef()->getCourse();
568 apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway,
570 FGRunway * rwy = apt->getRunwayByIdent(activeRunway);
571 assert( rwy != NULL );
573 // Create a slow descent path that ends 250 lateral to the runway.
574 double initialTurnRadius = getTurnRadius(vDescent, true);
575 //double finalTurnRadius = getTurnRadius(vApproach, true);
577 // get length of the downwind leg for the intended runway
578 double distanceOut = apt->getDynamics()->getApproachController()->getRunway(rwy->name())->getApproachDistance(); //12 * SG_NM_TO_METER;
579 //time_t previousArrivalTime= apt->getDynamics()->getApproachController()->getRunway(rwy->name())->getEstApproachTime();
582 SGGeod current = SGGeod::fromDegM(longitude, latitude, 0);
583 SGGeod initialTarget = rwy->pointOnCenterline(-distanceOut);
584 SGGeod refPoint = rwy->pointOnCenterline(0);
585 double distance = SGGeodesy::distanceM(current, initialTarget);
586 double azimuth = SGGeodesy::courseDeg(current, initialTarget);
589 // To prevent absurdly steep approaches, compute the origin from where the approach should have started
592 if (ac->getTrafficRef()->getCallSign() ==
593 fgGetString("/ai/track-callsign")) {
594 //cerr << "Reposition information: Actual distance " << distance << ". required distance " << requiredDistance << endl;
598 if (distance < requiredDistance * 0.8) {
600 SGGeodesy::direct(initialTarget, azimuth,
601 -requiredDistance, origin, dummyAz2);
603 distance = SGGeodesy::distanceM(current, initialTarget);
604 azimuth = SGGeodesy::courseDeg(current, initialTarget);
610 double dAlt = alt - (apt->getElevation() + 2000);
612 double nPoints = 100;
616 // The descent path contains the following phases:
617 // 1) a linear glide path from the initial position to
618 // 2) a semi circle turn to final
621 //cerr << "Phase 1: Linear Descent path to runway" << rwy->name() << endl;
622 // Create an initial destination point on a semicircle
623 //cerr << "lateral offset : " << lateralOffset << endl;
624 //cerr << "Distance : " << distance << endl;
625 //cerr << "Azimuth : " << azimuth << endl;
626 //cerr << "Initial Lateral point: " << lateralOffset << endl;
627 double lat = refPoint.getLatitudeDeg();
628 double lon = refPoint.getLongitudeDeg();
629 //cerr << "Reference point (" << lat << ", " << lon << ")." << endl;
630 lat = initialTarget.getLatitudeDeg();
631 lon = initialTarget.getLongitudeDeg();
632 //cerr << "Initial Target point (" << lat << ", " << lon << ")." << endl;
634 double ratio = initialTurnRadius / distance;
640 double newHeading = asin(ratio) * SG_RADIANS_TO_DEGREES;
642 cos(newHeading * SG_DEGREES_TO_RADIANS) * distance;
643 //cerr << "new distance " << newDistance << ". additional Heading " << newHeading << endl;
644 double side = azimuth - rwy->headingDeg();
645 double lateralOffset = initialTurnRadius;
651 // Calculate the ETA at final, based on remaining distance, and approach speed.
652 // distance should really consist of flying time to terniary target, plus circle
653 // but the distance to secondary target should work as a reasonable approximation
654 // aditionally add the amount of distance covered by making a turn of "side"
655 double turnDistance = (2 * M_PI * initialTurnRadius) * (side / 360.0);
657 (turnDistance + distance) / ((vDescent * SG_NM_TO_METER) / 3600.0);
658 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
659 //if (ac->getTrafficRef()->getCallSign() == fgGetString("/ai/track-callsign")) {
660 // cerr << " Arrival time estimation: turn angle " << side << ". Turn distance " << turnDistance << ". Linear distance " << distance << ". Time to go " << remaining << endl;
664 time_t eta = now + remaining;
665 //choose a distance to the runway such that it will take at least 60 seconds more
666 // time to get there than the previous aircraft.
667 // Don't bother when aircraft need to be repositioned, because that marks the initialization phased...
671 if (reposition == false) {
673 apt->getDynamics()->getApproachController()->getRunway(rwy->
676 requestTimeSlot(eta);
680 //if ((eta < (previousArrivalTime+60)) && (reposition == false)) {
681 arrivalTime = newEta;
682 time_t additionalTimeNeeded = newEta - eta;
683 double distanceCovered =
684 ((vApproach * SG_NM_TO_METER) / 3600.0) * additionalTimeNeeded;
685 distanceOut += distanceCovered;
686 //apt->getDynamics()->getApproachController()->getRunway(rwy->name())->setEstApproachTime(eta+additionalTimeNeeded);
687 //cerr << "Adding additional distance: " << distanceCovered << " to allow " << additionalTimeNeeded << " seconds of flying time" << endl << endl;
689 //apt->getDynamics()->getApproachController()->getRunway(rwy->name())->setEstApproachTime(eta);
691 //cerr << "Timing information : Previous eta: " << previousArrivalTime << ". Current ETA : " << eta << endl;
693 SGGeod secondaryTarget =
694 rwy->pointOffCenterline(-distanceOut, lateralOffset);
695 initialTarget = rwy->pointOnCenterline(-distanceOut);
696 distance = SGGeodesy::distanceM(origin, secondaryTarget);
697 azimuth = SGGeodesy::courseDeg(origin, secondaryTarget);
700 lat = secondaryTarget.getLatitudeDeg();
701 lon = secondaryTarget.getLongitudeDeg();
702 //cerr << "Secondary Target point (" << lat << ", " << lon << ")." << endl;
703 //cerr << "Distance : " << distance << endl;
704 //cerr << "Azimuth : " << azimuth << endl;
707 ratio = initialTurnRadius / distance;
712 newHeading = asin(ratio) * SG_RADIANS_TO_DEGREES;
713 newDistance = cos(newHeading * SG_DEGREES_TO_RADIANS) * distance;
714 //cerr << "new distance realative to secondary target: " << newDistance << ". additional Heading " << newHeading << endl;
716 azimuth += newHeading;
718 azimuth -= newHeading;
721 SGGeod tertiaryTarget;
722 SGGeodesy::direct(origin, azimuth,
723 newDistance, tertiaryTarget, dummyAz2);
725 lat = tertiaryTarget.getLatitudeDeg();
726 lon = tertiaryTarget.getLongitudeDeg();
727 //cerr << "tertiary Target point (" << lat << ", " << lon << ")." << endl;
730 for (int i = 1; i < nPoints; i++) {
732 double currentDist = i * (newDistance / nPoints);
733 double currentAltitude = alt - (i * (dAlt / nPoints));
734 SGGeodesy::direct(origin, azimuth, currentDist, result, dummyAz2);
735 snprintf(buffer, 16, "descent%03d", i);
736 wpt = createInAir(ac, buffer, result, currentAltitude, vDescent);
737 wpt->setCrossat(currentAltitude);
738 wpt->setTrackLength((newDistance / nPoints));
739 waypoints.push_back(wpt);
740 //cerr << "Track Length : " << wpt->trackLength;
741 //cerr << " Position : " << result.getLatitudeDeg() << " " << result.getLongitudeDeg() << " " << currentAltitude << endl;
744 //cerr << "Phase 2: Circle " << endl;
745 double initialAzimuth =
746 SGGeodesy::courseDeg(secondaryTarget, tertiaryTarget);
747 double finalAzimuth =
748 SGGeodesy::courseDeg(secondaryTarget, initialTarget);
750 //cerr << "Angles from secondary target: " << initialAzimuth << " " << finalAzimuth << endl;
751 int increment, startval, endval;
752 // circle right around secondary target if orig of position is to the right of the runway
753 // i.e. use negative angles; else circle leftward and use postivi
756 startval = floor(initialAzimuth);
757 endval = ceil(finalAzimuth);
758 if (endval > startval) {
763 startval = ceil(initialAzimuth);
764 endval = floor(finalAzimuth);
765 if (endval < startval) {
771 //cerr << "creating circle between " << startval << " and " << endval << " using " << increment << endl;
772 double trackLength = (2 * M_PI * initialTurnRadius) / 360.0;
773 for (int i = startval; i != endval; i += increment) {
775 double currentAltitude = apt->getElevation() + 2000;
776 SGGeodesy::direct(secondaryTarget, i,
777 initialTurnRadius, result, dummyAz2);
778 snprintf(buffer, 16, "turn%03d", i);
779 wpt = createInAir(ac, buffer, result, currentAltitude, vDescent);
780 wpt->setCrossat(currentAltitude);
781 wpt->setTrackLength(trackLength);
782 //cerr << "Track Length : " << wpt->trackLength;
783 waypoints.push_back(wpt);
784 //cerr << " Position : " << result.getLatitudeDeg() << " " << result.getLongitudeDeg() << " " << currentAltitude << endl;
788 // The approach leg should bring the aircraft to approximately 4-6 out, after which the landing phase should take over.
789 //cerr << "Phase 3: Approach" << endl;
790 distanceOut -= distanceCovered;
791 for (int i = 1; i < nPoints; i++) {
793 double currentDist = i * (distanceOut / nPoints);
794 double currentAltitude =
795 apt->getElevation() + 2000 - (i * 2000 / nPoints);
796 snprintf(buffer, 16, "final%03d", i);
797 result = rwy->pointOnCenterline((-distanceOut) + currentDist);
798 wpt = createInAir(ac, buffer, result, currentAltitude, vApproach);
799 wpt->setCrossat(currentAltitude);
800 wpt->setTrackLength((distanceOut / nPoints));
801 // account for the extra distance due to an extended downwind leg
803 wpt->setTrackLength(wpt->getTrackLength() + distanceCovered);
805 //cerr << "Track Length : " << wpt->trackLength;
806 waypoints.push_back(wpt);
807 //cerr << " Position : " << result.getLatitudeDeg() << " " << result.getLongitudeDeg() << " " << currentAltitude << endl;
810 //cerr << "Done" << endl;
812 // Erase the two bogus BOD points: Note check for conflicts with scripted AI flightPlans
813 IncrementWaypoint(true);
814 IncrementWaypoint(true);
818 //double minDistance = HUGE_VAL;
820 tempDistance = SGGeodesy::distanceM(current, initialTarget);
822 tempDistance / ((vDescent * SG_NM_TO_METER) / 3600.0) + now;
824 apt->getDynamics()->getApproachController()->getRunway(rwy->
827 requestTimeSlot(eta);
828 arrivalTime = newEta;
830 ((vDescent * SG_NM_TO_METER) / 3600.0) * (newEta - now);
831 //cerr << "Repositioning information : eta" << eta << ". New ETA " << newEta << ". Diff = " << (newEta - eta) << ". Distance = " << tempDistance << ". New distance = " << newDistance << endl;
832 IncrementWaypoint(true); // remove waypoint BOD2
833 while (checkTrackLength("final001") > newDistance) {
834 IncrementWaypoint(true);
836 //cerr << "Repositioning to waypoint " << (*waypoints.begin())->name << endl;
837 ac->resetPositionFromFlightPlan();
839 waypoints[1]->setName( (waypoints[1]->getName() + string("legend")));
843 /*******************************************************************
845 * Create a flight path from the "permision to land" point (currently
846 hardcoded at 5000 meters from the threshold) to the threshold, at
847 a standard glide slope angle of 3 degrees.
848 ******************************************************************/
849 bool FGAIFlightPlan::createLanding(FGAIAircraft * ac, FGAirport * apt,
850 const string & fltType)
852 double vTouchdown = ac->getPerformance()->vTouchdown();
853 //double vTaxi = ac->getPerformance()->vTaxi();
855 //string rwyClass = getRunwayClassFromTrafficType(fltType);
856 //double heading = ac->getTrafficRef()->getCourse();
857 //apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading);
858 //rwy = apt->getRunwayByIdent(activeRunway);
862 double aptElev = apt->getElevation();
866 for (int i = 1; i < 10; i++) {
867 snprintf(buffer, 12, "wpt%d", i);
868 FGRunway * rwy = apt->getRunwayByIdent(activeRunway);
869 assert( rwy != NULL );
871 coord = rwy->pointOnCenterline(rwy->lengthM() * (i / 10.0));
872 wpt = createOnGround(ac, buffer, coord, aptElev, (vTouchdown / i));
873 wpt->setCrossat(apt->getElevation());
874 waypoints.push_back(wpt);
879 wpt = createOnGround(ac, "Threshold", rwy->threshold(), aptElev, vTouchdown);
880 wpt->crossat = apt->getElevation();
881 waypoints.push_back(wpt);
884 wpt = createOnGround(ac, "Center", rwy->geod(), aptElev, vTaxi*2);
885 waypoints.push_back(wpt);
887 SGGeod rollOut = rwy->pointOnCenterline(rwy->lengthM() * 0.9);
888 wpt = createOnGround(ac, "Roll Out", rollOut, aptElev, vTaxi);
889 wpt->crossat = apt->getElevation();
890 waypoints.push_back(wpt);
895 /*******************************************************************
897 * initialize the Aircraft at the parking location
898 ******************************************************************/
899 bool FGAIFlightPlan::createParking(FGAIAircraft * ac, FGAirport * apt,
903 double aptElev = apt->getElevation();
904 double lat = 0.0, lat2 = 0.0;
905 double lon = 0.0, lon2 = 0.0;
907 double heading = 0.0;
909 double vTaxi = ac->getPerformance()->vTaxi();
910 double vTaxiReduced = vTaxi * (2.0 / 3.0);
911 apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
915 geo_direct_wgs_84(0, lat, lon, heading,
916 2.2 * radius, &lat2, &lon2, &az2);
918 createOnGround(ac, "taxiStart", SGGeod::fromDeg(lon2, lat2),
919 aptElev, vTaxiReduced);
920 waypoints.push_back(wpt);
922 geo_direct_wgs_84(0, lat, lon, heading,
923 0.1 * radius, &lat2, &lon2, &az2);
926 createOnGround(ac, "taxiStart2", SGGeod::fromDeg(lon2, lat2),
927 aptElev, vTaxiReduced);
928 waypoints.push_back(wpt);
931 createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), aptElev,
933 waypoints.push_back(wpt);
939 * @param fltType a string describing the type of
940 * traffic, normally used for gate assignments
941 * @return a converted string that gives the runway
942 * preference schedule to be used at aircraft having
943 * a preferential runway schedule implemented (i.e.
944 * having a rwyprefs.xml file
946 * Currently valid traffic types for gate assignment:
947 * - gate (commercial gate)
948 * - cargo (commercial gargo),
949 * - ga (general aviation) ,
951 * - mil-fighter (military - fighter),
952 * - mil-transport (military - transport)
954 * Valid runway classes:
955 * - com (commercial traffic: jetliners, passenger and cargo)
956 * - gen (general aviation)
957 * - ul (ultralight: I can imagine that these may share a runway with ga on some airports)
958 * - mil (all military traffic)
960 string FGAIFlightPlan::getRunwayClassFromTrafficType(string fltType)
962 if ((fltType == "gate") || (fltType == "cargo")) {
963 return string("com");
965 if (fltType == "ga") {
966 return string("gen");
968 if (fltType == "ul") {
971 if ((fltType == "mil-fighter") || (fltType == "mil-transport")) {
972 return string("mil");
974 return string("com");
978 double FGAIFlightPlan::getTurnRadius(double speed, bool inAir)
981 if (inAir == false) {
982 turn_radius = ((360 / 30) * fabs(speed)) / (2 * M_PI);
984 turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank