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/runways.hxx>
31 #include <Airports/dynamics.hxx>
32 #include "AIAircraft.hxx"
33 #include "performancedata.hxx"
35 #include <Environment/environment_mgr.hxx>
36 #include <Environment/environment.hxx>
39 /* FGAIFlightPlan::create()
40 * dynamically create a flight plan for AI traffic, based on data provided by the
41 * Traffic Manager, when reading a filed flightplan failes. (DT, 2004/07/10)
43 * This is the top-level function, and the only one that is publicly available.
48 // Check lat/lon values during initialization;
49 void FGAIFlightPlan::create(FGAIAircraft *ac, FGAirport *dep, FGAirport *arr, int legNr,
50 double alt, double speed, double latitude,
51 double longitude, bool firstFlight,double radius,
52 const string& fltType, const string& aircraftType,
53 const string& airline)
55 int currWpt = wpt_iterator - waypoints.begin();
59 createPushBack(ac, firstFlight,dep, latitude, longitude,
60 radius, fltType, aircraftType, airline);
63 createTakeoffTaxi(ac, firstFlight, dep, radius, fltType, aircraftType, airline);
66 createTakeOff(ac, firstFlight, dep, speed, fltType);
69 createClimb(ac, firstFlight, dep, speed, alt, fltType);
72 createCruise(ac, firstFlight, dep,arr, latitude, longitude, speed, alt, fltType);
75 createDecent(ac, arr, fltType);
78 createLanding(ac, arr, fltType);
81 createLandingTaxi(ac, arr, radius, fltType, aircraftType, airline);
84 createParking(ac, arr, radius);
88 SG_LOG(SG_INPUT, SG_ALERT, "AIFlightPlan::create() attempting to create unknown leg"
89 " this is probably an internal program error");
91 wpt_iterator = waypoints.begin()+currWpt;
95 FGAIFlightPlan::waypoint*
96 FGAIFlightPlan::createOnGround(FGAIAircraft *ac, const std::string& aName, const SGGeod& aPos, double aElev, double aSpeed)
98 waypoint* wpt = new waypoint;
100 wpt->longitude = aPos.getLongitudeDeg();
101 wpt->latitude = aPos.getLatitudeDeg();
102 wpt->altitude = aElev;
104 wpt->crossat = -10000;
105 wpt->gear_down = true;
106 wpt->flaps_down= true;
107 wpt->finished = false;
108 wpt->on_ground = true;
113 FGAIFlightPlan::waypoint*
114 FGAIFlightPlan::createInAir(FGAIAircraft *ac, const std::string& aName, const SGGeod& aPos, double aElev, double aSpeed)
116 waypoint* wpt = new waypoint;
118 wpt->longitude = aPos.getLongitudeDeg();
119 wpt->latitude = aPos.getLatitudeDeg();
120 wpt->altitude = aElev;
122 wpt->crossat = -10000;
123 wpt->gear_down = false;
124 wpt->flaps_down= false;
125 wpt->finished = false;
126 wpt->on_ground = false;
131 FGAIFlightPlan::waypoint*
132 FGAIFlightPlan::cloneWithPos(FGAIAircraft *ac, waypoint* aWpt, const std::string& aName, const SGGeod& aPos)
134 waypoint* wpt = new waypoint;
136 wpt->longitude = aPos.getLongitudeDeg();
137 wpt->latitude = aPos.getLatitudeDeg();
139 wpt->altitude = aWpt->altitude;
140 wpt->speed = aWpt->speed;
141 wpt->crossat = aWpt->crossat;
142 wpt->gear_down = aWpt->gear_down;
143 wpt->flaps_down= aWpt->flaps_down;
144 wpt->finished = aWpt->finished;
145 wpt->on_ground = aWpt->on_ground;
151 FGAIFlightPlan::waypoint*
152 FGAIFlightPlan::clone(waypoint* aWpt)
154 waypoint* wpt = new waypoint;
155 wpt->name = aWpt->name;
156 wpt->longitude = aWpt->longitude;
157 wpt->latitude = aWpt->latitude;
159 wpt->altitude = aWpt->altitude;
160 wpt->speed = aWpt->speed;
161 wpt->crossat = aWpt->crossat;
162 wpt->gear_down = aWpt->gear_down;
163 wpt->flaps_down= aWpt->flaps_down;
164 wpt->finished = aWpt->finished;
165 wpt->on_ground = aWpt->on_ground;
172 void FGAIFlightPlan::createDefaultTakeoffTaxi(FGAIAircraft *ac, FGAirport* aAirport, FGRunway* aRunway)
174 SGGeod runwayTakeoff = aRunway->pointOnCenterline(5.0);
175 double airportElev = aAirport->getElevation();
178 wpt = createOnGround(ac, "Airport Center", aAirport->geod(), airportElev, ac->getPerformance()->vTaxi());
179 waypoints.push_back(wpt);
180 wpt = createOnGround(ac, "Runway Takeoff", runwayTakeoff, airportElev, ac->getPerformance()->vTaxi());
181 waypoints.push_back(wpt);
184 void FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft *ac, bool firstFlight,
186 double radius, const string& fltType,
187 const string& acType, const string& airline)
189 double heading, lat, lon;
191 // If this function is called during initialization,
192 // make sure we obtain a valid gate ID first
193 // and place the model at the location of the gate.
195 if (!(apt->getDynamics()->getAvailableParking(&lat, &lon,
200 SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " <<
202 " of flight type " << fltType <<
203 " of airline " << airline <<
204 " at airport " << apt->getId());
208 string rwyClass = getRunwayClassFromTrafficType(fltType);
210 // Only set this if it hasn't been set by ATC already.
211 if (activeRunway.empty()) {
212 //cerr << "Getting runway for " << ac->getTrafficRef()->getCallSign() << " at " << apt->getId() << endl;
213 double depHeading = ac->getTrafficRef()->getCourse();
214 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, depHeading);
216 rwy = apt->getRunwayByIdent(activeRunway);
217 SGGeod runwayTakeoff = rwy->pointOnCenterline(5.0);
219 FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
221 createDefaultTakeoffTaxi(ac, apt, rwy);
226 int runwayId = gn->findNearestNode(runwayTakeoff);
228 // A negative gateId indicates an overflow parking, use a
229 // fallback mechanism for this.
230 // Starting from gate 0 in this case is a bit of a hack
231 // which requires a more proper solution later on.
233 taxiRoute = new FGTaxiRoute;
235 // Determine which node to start from.
237 // Find out which node to start from
238 FGParking *park = apt->getDynamics()->getParking(gateId);
240 node = park->getPushBackPoint();
247 // HAndle case where parking doens't have a node
248 if ((node == 0) && park) {
252 node = lastNodeVisited;
256 *taxiRoute = gn->findShortestRoute(node, runwayId);
259 if (taxiRoute->empty()) {
260 createDefaultTakeoffTaxi(ac, apt, rwy);
265 //bool isPushBackPoint = false;
267 // If this is called during initialization, randomly
268 // skip a number of waypoints to get a more realistic
270 int nrWaypointsToSkip = rand() % taxiRoute->size();
271 // but make sure we always keep two active waypoints
272 // to prevent a segmentation fault
273 for (int i = 0; i < nrWaypointsToSkip-2; i++) {
274 taxiRoute->next(&node);
276 apt->getDynamics()->releaseParking(gateId);
278 if (taxiRoute->size() > 1) {
279 taxiRoute->next(&node); // chop off the first waypoint, because that is already the last of the pushback route
283 // push each node on the taxi route as a waypoint
285 while(taxiRoute->next(&node, &route)) {
287 snprintf (buffer, 10, "%d", node);
288 FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
289 waypoint* wpt = createOnGround(ac, buffer, tn->getGeod(), apt->getElevation(), ac->getPerformance()->vTaxi());
290 wpt->routeIndex = route;
291 waypoints.push_back(wpt);
295 void FGAIFlightPlan::createDefaultLandingTaxi(FGAIAircraft *ac, FGAirport* aAirport)
298 SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
299 double airportElev = aAirport->getElevation();
302 wpt = createOnGround(ac, "Runway Exit", lastWptPos, airportElev, ac->getPerformance()->vTaxi());
303 waypoints.push_back(wpt);
304 wpt = createOnGround(ac, "Airport Center", aAirport->geod(), airportElev, ac->getPerformance()->vTaxi());
305 waypoints.push_back(wpt);
307 double heading, lat, lon;
308 aAirport->getDynamics()->getParking(gateId, &lat, &lon, &heading);
309 wpt = createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), airportElev, ac->getPerformance()->vTaxi());
310 waypoints.push_back(wpt);
313 void FGAIFlightPlan::createLandingTaxi(FGAIAircraft *ac, FGAirport *apt,
314 double radius, const string& fltType,
315 const string& acType, const string& airline)
317 double heading, lat, lon;
318 apt->getDynamics()->getAvailableParking(&lat, &lon, &heading,
319 &gateId, radius, fltType, acType, airline);
322 SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
323 FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
325 // Find a route from runway end to parking/gate.
327 createDefaultLandingTaxi(ac, apt);
332 int runwayId = gn->findNearestNode(lastWptPos);
333 // A negative gateId indicates an overflow parking, use a
334 // fallback mechanism for this.
335 // Starting from gate 0 is a bit of a hack...
338 taxiRoute = new FGTaxiRoute;
340 *taxiRoute = gn->findShortestRoute(runwayId, gateId);
342 *taxiRoute = gn->findShortestRoute(runwayId, 0);
345 if (taxiRoute->empty()) {
346 createDefaultLandingTaxi(ac, apt);
352 int size = taxiRoute->size();
353 // Omit the last two waypoints, as
354 // those are created by createParking()
356 for (int i = 0; i < size-2; i++) {
357 taxiRoute->next(&node, &route);
359 snprintf (buffer, 10, "%d", node);
360 FGTaxiNode *tn = gn->findNode(node);
361 waypoint* wpt = createOnGround(ac, buffer, tn->getGeod(), apt->getElevation(), ac->getPerformance()->vTaxi());
362 wpt->routeIndex = route;
363 waypoints.push_back(wpt);
367 /*******************************************************************
370 * - Speed -> knots -> nm/hour
371 * - distance along runway =-> meters
372 * - accel / decel -> is given as knots/hour, but this is highly questionable:
373 * for a jet_transport performance class, a accel / decel rate of 5 / 2 is
374 * given respectively. According to performance data.cxx, a value of kts / second seems
375 * more likely however.
377 ******************************************************************/
378 void FGAIFlightPlan::createTakeOff(FGAIAircraft *ac, bool firstFlight, FGAirport *apt, double speed, const string &fltType)
380 double accel = ac->getPerformance()->acceleration();
381 double vTaxi = ac->getPerformance()->vTaxi();
382 double vRotate = ac->getPerformance()->vRotate();
383 double vTakeoff = ac->getPerformance()->vTakeoff();
384 double vClimb = ac->getPerformance()->vClimb();
386 double accelMetric = (accel * SG_NM_TO_METER) / 3600;
387 double vTaxiMetric = (vTaxi * SG_NM_TO_METER) / 3600;
388 double vRotateMetric = (vRotate * SG_NM_TO_METER) / 3600;
389 double vTakeoffMetric = (vTakeoff * SG_NM_TO_METER) / 3600;
390 double vClimbMetric = (vClimb * SG_NM_TO_METER) / 3600;
391 // Acceleration = dV / dT
392 // Acceleration X dT = dV
393 // dT = dT / Acceleration
394 //d = (Vf^2 - Vo^2) / (2*a)
395 //double accelTime = (vRotate - vTaxi) / accel;
396 //cerr << "Using " << accelTime << " as total acceleration time" << endl;
397 double accelDistance = (vRotateMetric*vRotateMetric - vTaxiMetric*vTaxiMetric) / (2*accelMetric);
398 cerr << "Using " << accelDistance << " " << accelMetric << " " << vRotateMetric << endl;
400 // Get the current active runway, based on code from David Luff
401 // This should actually be unified and extended to include
402 // Preferential runway use schema's
403 // NOTE: DT (2009-01-18: IIRC, this is currently already the case,
404 // because the getActive runway function takes care of that.
407 string rwyClass = getRunwayClassFromTrafficType(fltType);
408 double heading = ac->getTrafficRef()->getCourse();
409 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
410 rwy = apt->getRunwayByIdent(activeRunway);
413 double airportElev = apt->getElevation();
414 // Acceleration point, 105 meters into the runway,
415 SGGeod accelPoint = rwy->pointOnCenterline(105.0);
416 wpt = createOnGround(ac, "accel", accelPoint, airportElev, vRotate);
417 waypoints.push_back(wpt);
420 accelDistance = (vTakeoffMetric*vTakeoffMetric - vTaxiMetric*vTaxiMetric) / (2*accelMetric);
421 cerr << "Using " << accelDistance << " " << accelMetric << " " << vTakeoffMetric << endl;
422 accelPoint = rwy->pointOnCenterline(105.0+accelDistance);
423 wpt = createOnGround(ac, "rotate", accelPoint, airportElev, vTakeoff);
424 waypoints.push_back(wpt);
426 accelDistance = ((vTakeoffMetric*1.1)*(vTakeoffMetric*1.1) - vTaxiMetric*vTaxiMetric) / (2*accelMetric);
427 cerr << "Using " << accelDistance << " " << accelMetric << " " << vTakeoffMetric << endl;
428 accelPoint = rwy->pointOnCenterline(105.0+accelDistance);
429 wpt = createOnGround(ac, "rotate", accelPoint, airportElev+1000, vTakeoff*1.1);
430 wpt->on_ground = false;
431 waypoints.push_back(wpt);
433 wpt = cloneWithPos(ac, wpt, "3000 ft", rwy->end());
434 wpt->altitude = airportElev+3000;
435 waypoints.push_back(wpt);
437 // Finally, add two more waypoints, so that aircraft will remain under
438 // Tower control until they have reached the 3000 ft climb point
439 SGGeod pt = rwy->pointOnCenterline(5000 + rwy->lengthM() * 0.5);
440 wpt = cloneWithPos(ac, wpt, "5000 ft", pt);
441 wpt->altitude = airportElev+5000;
442 waypoints.push_back(wpt);
445 /*******************************************************************
447 * initialize the Aircraft at the parking location
448 ******************************************************************/
449 void FGAIFlightPlan::createClimb(FGAIAircraft *ac, bool firstFlight, FGAirport *apt, double speed, double alt, const string &fltType)
452 // bool planLoaded = false;
454 double vClimb = ac->getPerformance()->vClimb();
457 string rwyClass = getRunwayClassFromTrafficType(fltType);
458 double heading = ac->getTrafficRef()->getCourse();
459 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
460 rwy = apt->getRunwayByIdent(activeRunway);
463 for (wpt_vector_iterator i = sid->getFirstWayPoint();
464 i != sid->getLastWayPoint();
466 waypoints.push_back(clone(*(i)));
467 //cerr << " Cloning waypoint " << endl;
470 SGGeod climb1 = rwy->pointOnCenterline(10*SG_NM_TO_METER);
471 wpt = createInAir(ac, "10000ft climb", climb1, vClimb, 10000);
472 wpt->gear_down = true;
473 wpt->flaps_down= true;
474 waypoints.push_back(wpt);
476 SGGeod climb2 = rwy->pointOnCenterline(20*SG_NM_TO_METER);
477 wpt = cloneWithPos(ac, wpt, "18000ft climb", climb2);
478 wpt->altitude = 18000;
479 waypoints.push_back(wpt);
485 /*******************************************************************
487 * initialize the Aircraft at the parking location
488 ******************************************************************/
489 void FGAIFlightPlan::createDecent(FGAIAircraft *ac, FGAirport *apt, const string &fltType)
491 // Ten thousand ft. Slowing down to 240 kts
493 double vDecent = ac->getPerformance()->vDescent();
494 double vApproach = ac->getPerformance()->vApproach();
496 //Beginning of Decent
498 // allow "mil" and "gen" as well
499 string rwyClass = getRunwayClassFromTrafficType(fltType);
500 double heading = ac->getTrafficRef()->getCourse();
501 apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading);
502 rwy = apt->getRunwayByIdent(activeRunway);
504 SGGeod descent1 = rwy->pointOnCenterline(-100000); // 100km out
505 wpt = createInAir(ac, "Dec 10000ft", descent1, apt->getElevation(), vDecent);
506 wpt->crossat = 10000;
507 waypoints.push_back(wpt);
509 // Three thousand ft. Slowing down to 160 kts
510 SGGeod descent2 = rwy->pointOnCenterline(-8*SG_NM_TO_METER); // 8nm out
511 wpt = createInAir(ac, "DEC 3000ft", descent2, apt->getElevation(), vApproach);
513 wpt->gear_down = true;
514 wpt->flaps_down= true;
515 waypoints.push_back(wpt);
517 /*******************************************************************
519 * initialize the Aircraft at the parking location
520 ******************************************************************/
521 void FGAIFlightPlan::createLanding(FGAIAircraft *ac, FGAirport *apt, const string &fltType)
523 double vTouchdown = ac->getPerformance()->vTouchdown();
524 double vTaxi = ac->getPerformance()->vTaxi();
526 string rwyClass = getRunwayClassFromTrafficType(fltType);
527 double heading = ac->getTrafficRef()->getCourse();
528 apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading);
529 rwy = apt->getRunwayByIdent(activeRunway);
533 double aptElev = apt->getElevation();
537 for (int i = 1; i < 10; i++) {
538 snprintf(buffer, 12, "wpt%d", i);
539 coord = rwy->pointOnCenterline(rwy->lengthM() * (i/10.0));
540 wpt = createOnGround(ac, buffer, coord, aptElev, (vTouchdown/i));
541 wpt->crossat = apt->getElevation();
542 waypoints.push_back(wpt);
547 wpt = createOnGround(ac, "Threshold", rwy->threshold(), aptElev, vTouchdown);
548 wpt->crossat = apt->getElevation();
549 waypoints.push_back(wpt);
552 wpt = createOnGround(ac, "Center", rwy->geod(), aptElev, vTaxi*2);
553 waypoints.push_back(wpt);
555 SGGeod rollOut = rwy->pointOnCenterline(rwy->lengthM() * 0.9);
556 wpt = createOnGround(ac, "Roll Out", rollOut, aptElev, vTaxi);
557 wpt->crossat = apt->getElevation();
558 waypoints.push_back(wpt);
562 /*******************************************************************
564 * initialize the Aircraft at the parking location
565 ******************************************************************/
566 void FGAIFlightPlan::createParking(FGAIAircraft *ac, FGAirport *apt, double radius)
569 double aptElev = apt->getElevation();
570 double lat = 0.0, lat2 = 0.0;
571 double lon = 0.0, lon2 = 0.0;
573 double heading = 0.0;
575 double vTaxi = ac->getPerformance()->vTaxi();
576 double vTaxiReduced = vTaxi * (2.0/3.0);
577 apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
581 geo_direct_wgs_84 ( 0, lat, lon, heading,
583 &lat2, &lon2, &az2 );
584 wpt = createOnGround(ac, "taxiStart", SGGeod::fromDeg(lon2, lat2), aptElev, vTaxiReduced);
585 waypoints.push_back(wpt);
587 geo_direct_wgs_84 ( 0, lat, lon, heading,
589 &lat2, &lon2, &az2 );
591 wpt = createOnGround(ac, "taxiStart2", SGGeod::fromDeg(lon2, lat2), aptElev, vTaxiReduced);
592 waypoints.push_back(wpt);
594 wpt = createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), aptElev, vTaxiReduced);
595 waypoints.push_back(wpt);
600 * @param fltType a string describing the type of
601 * traffic, normally used for gate assignments
602 * @return a converted string that gives the runway
603 * preference schedule to be used at aircraft having
604 * a preferential runway schedule implemented (i.e.
605 * having a rwyprefs.xml file
607 * Currently valid traffic types for gate assignment:
608 * - gate (commercial gate)
609 * - cargo (commercial gargo),
610 * - ga (general aviation) ,
612 * - mil-fighter (military - fighter),
613 * - mil-transport (military - transport)
615 * Valid runway classes:
616 * - com (commercial traffic: jetliners, passenger and cargo)
617 * - gen (general aviation)
618 * - ul (ultralight: I can imagine that these may share a runway with ga on some airports)
619 * - mil (all military traffic)
621 string FGAIFlightPlan::getRunwayClassFromTrafficType(string fltType)
623 if ((fltType == "gate") || (fltType == "cargo")) {
624 return string("com");
626 if (fltType == "ga") {
627 return string ("gen");
629 if (fltType == "ul") {
632 if ((fltType == "mil-fighter") || (fltType == "mil-transport")) {
633 return string("mil");
635 return string("com");