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 <Airports/runways.hxx>
28 #include <Airports/dynamics.hxx>
29 #include "AIAircraft.hxx"
30 #include "performancedata.hxx"
32 #include <Environment/environment_mgr.hxx>
33 #include <Environment/environment.hxx>
36 /* FGAIFlightPlan::create()
37 * dynamically create a flight plan for AI traffic, based on data provided by the
38 * Traffic Manager, when reading a filed flightplan failes. (DT, 2004/07/10)
40 * This is the top-level function, and the only one that is publicly available.
45 // Check lat/lon values during initialization;
46 void FGAIFlightPlan::create(FGAIAircraft *ac, FGAirport *dep, FGAirport *arr, int legNr,
47 double alt, double speed, double latitude,
48 double longitude, bool firstFlight,double radius,
49 const string& fltType, const string& aircraftType,
50 const string& airline)
52 int currWpt = wpt_iterator - waypoints.begin();
56 createPushBack(ac, firstFlight,dep, latitude, longitude,
57 radius, fltType, aircraftType, airline);
60 createTakeoffTaxi(ac, firstFlight, dep, radius, fltType, aircraftType, airline);
63 createTakeOff(ac, firstFlight, dep, speed, fltType);
66 createClimb(ac, firstFlight, dep, speed, alt, fltType);
69 createCruise(ac, firstFlight, dep,arr, latitude, longitude, speed, alt, fltType);
72 createDecent(ac, arr, fltType);
75 createLanding(ac, arr);
78 createLandingTaxi(ac, arr, radius, fltType, aircraftType, airline);
81 createParking(ac, arr, radius);
85 SG_LOG(SG_INPUT, SG_ALERT, "AIFlightPlan::create() attempting to create unknown leg"
86 " this is probably an internal program error");
88 wpt_iterator = waypoints.begin()+currWpt;
92 FGAIFlightPlan::waypoint*
93 FGAIFlightPlan::createOnGround(FGAIAircraft *ac, const std::string& aName, const SGGeod& aPos, double aElev, double aSpeed)
95 waypoint* wpt = new waypoint;
97 wpt->longitude = aPos.getLongitudeDeg();
98 wpt->latitude = aPos.getLatitudeDeg();
99 wpt->altitude = aElev;
101 wpt->crossat = -10000;
102 wpt->gear_down = true;
103 wpt->flaps_down= true;
104 wpt->finished = false;
105 wpt->on_ground = true;
110 FGAIFlightPlan::waypoint*
111 FGAIFlightPlan::createInAir(FGAIAircraft *ac, const std::string& aName, const SGGeod& aPos, double aElev, double aSpeed)
113 waypoint* wpt = new waypoint;
115 wpt->longitude = aPos.getLongitudeDeg();
116 wpt->latitude = aPos.getLatitudeDeg();
117 wpt->altitude = aElev;
119 wpt->crossat = -10000;
120 wpt->gear_down = false;
121 wpt->flaps_down= false;
122 wpt->finished = false;
123 wpt->on_ground = false;
128 FGAIFlightPlan::waypoint*
129 FGAIFlightPlan::cloneWithPos(FGAIAircraft *ac, waypoint* aWpt, const std::string& aName, const SGGeod& aPos)
131 waypoint* wpt = new waypoint;
133 wpt->longitude = aPos.getLongitudeDeg();
134 wpt->latitude = aPos.getLatitudeDeg();
136 wpt->altitude = aWpt->altitude;
137 wpt->speed = aWpt->speed;
138 wpt->crossat = aWpt->crossat;
139 wpt->gear_down = aWpt->gear_down;
140 wpt->flaps_down= aWpt->flaps_down;
141 wpt->finished = aWpt->finished;
142 wpt->on_ground = aWpt->on_ground;
148 void FGAIFlightPlan::createDefaultTakeoffTaxi(FGAIAircraft *ac, FGAirport* aAirport, FGRunway* aRunway)
150 SGGeod runwayTakeoff = aRunway->pointOnCenterline(5.0);
151 double airportElev = aAirport->getElevation();
154 wpt = createOnGround(ac, "Airport Center", aAirport->geod(), airportElev, 15);
155 waypoints.push_back(wpt);
156 wpt = createOnGround(ac, "Runway Takeoff", runwayTakeoff, airportElev, 15);
157 waypoints.push_back(wpt);
160 void FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft *ac, bool firstFlight,
162 double radius, const string& fltType,
163 const string& acType, const string& airline)
165 double heading, lat, lon;
167 // If this function is called during initialization,
168 // make sure we obtain a valid gate ID first
169 // and place the model at the location of the gate.
171 if (!(apt->getDynamics()->getAvailableParking(&lat, &lon,
176 SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " <<
178 " of flight type " << fltType <<
179 " of airline " << airline <<
180 " at airport " << apt->getId());
184 string rwyClass = getRunwayClassFromTrafficType(fltType);
186 // Only set this if it hasn't been set by ATC already.
187 if (activeRunway.empty()) {
188 //cerr << "Getting runway for " << ac->getTrafficRef()->getCallSign() << " at " << apt->getId() << endl;
189 double depHeading = ac->getTrafficRef()->getCourse();
190 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, depHeading);
192 rwy = apt->getRunwayByIdent(activeRunway);
193 SGGeod runwayTakeoff = rwy->pointOnCenterline(5.0);
195 FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
197 createDefaultTakeoffTaxi(ac, apt, rwy);
202 int runwayId = gn->findNearestNode(runwayTakeoff);
204 // A negative gateId indicates an overflow parking, use a
205 // fallback mechanism for this.
206 // Starting from gate 0 in this case is a bit of a hack
207 // which requires a more proper solution later on.
209 taxiRoute = new FGTaxiRoute;
211 // Determine which node to start from.
213 // Find out which node to start from
214 FGParking *park = apt->getDynamics()->getParking(gateId);
216 node = park->getPushBackPoint();
223 // HAndle case where parking doens't have a node
224 if ((node == 0) && park) {
228 node = lastNodeVisited;
232 *taxiRoute = gn->findShortestRoute(node, runwayId);
235 if (taxiRoute->empty()) {
236 createDefaultTakeoffTaxi(ac, apt, rwy);
241 //bool isPushBackPoint = false;
243 // If this is called during initialization, randomly
244 // skip a number of waypoints to get a more realistic
246 int nrWaypointsToSkip = rand() % taxiRoute->size();
247 // but make sure we always keep two active waypoints
248 // to prevent a segmentation fault
249 for (int i = 0; i < nrWaypointsToSkip-2; i++) {
250 taxiRoute->next(&node);
252 apt->getDynamics()->releaseParking(gateId);
254 if (taxiRoute->size() > 1) {
255 taxiRoute->next(&node); // chop off the first waypoint, because that is already the last of the pushback route
259 // push each node on the taxi route as a waypoint
261 while(taxiRoute->next(&node, &route)) {
263 snprintf (buffer, 10, "%d", node);
264 FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
265 waypoint* wpt = createOnGround(ac, buffer, tn->geod(), apt->getElevation(), 15);
266 wpt->routeIndex = route;
267 waypoints.push_back(wpt);
271 void FGAIFlightPlan::createDefaultLandingTaxi(FGAIAircraft *ac, FGAirport* aAirport)
274 SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
275 double airportElev = aAirport->getElevation();
278 wpt = createOnGround(ac, "Runway Exit", lastWptPos, airportElev, 15);
279 waypoints.push_back(wpt);
280 wpt = createOnGround(ac, "Airport Center", aAirport->geod(), airportElev, 15);
281 waypoints.push_back(wpt);
283 double heading, lat, lon;
284 aAirport->getDynamics()->getParking(gateId, &lat, &lon, &heading);
285 wpt = createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), airportElev, 15);
286 waypoints.push_back(wpt);
289 void FGAIFlightPlan::createLandingTaxi(FGAIAircraft *ac, FGAirport *apt,
290 double radius, const string& fltType,
291 const string& acType, const string& airline)
293 double heading, lat, lon;
294 apt->getDynamics()->getAvailableParking(&lat, &lon, &heading,
295 &gateId, radius, fltType, acType, airline);
298 SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
299 FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
301 // Find a route from runway end to parking/gate.
303 createDefaultLandingTaxi(ac, apt);
308 int runwayId = gn->findNearestNode(lastWptPos);
309 // A negative gateId indicates an overflow parking, use a
310 // fallback mechanism for this.
311 // Starting from gate 0 is a bit of a hack...
314 taxiRoute = new FGTaxiRoute;
316 *taxiRoute = gn->findShortestRoute(runwayId, gateId);
318 *taxiRoute = gn->findShortestRoute(runwayId, 0);
321 if (taxiRoute->empty()) {
322 createDefaultLandingTaxi(ac, apt);
328 int size = taxiRoute->size();
329 // Omit the last two waypoints, as
330 // those are created by createParking()
332 for (int i = 0; i < size-2; i++) {
333 taxiRoute->next(&node, &route);
335 snprintf (buffer, 10, "%d", node);
336 FGTaxiNode *tn = gn->findNode(node);
337 waypoint* wpt = createOnGround(ac, buffer, tn->geod(), apt->getElevation(), 15);
338 wpt->routeIndex = route;
339 waypoints.push_back(wpt);
343 /*******************************************************************
345 * initialize the Aircraft at the parking location
346 ******************************************************************/
347 void FGAIFlightPlan::createTakeOff(FGAIAircraft *ac, bool firstFlight, FGAirport *apt, double speed, const string &fltType)
349 double accel = ac->getPerformance()->acceleration();
350 double vRotate = ac->getPerformance()->vRotate();
351 // Acceleration = dV / dT
352 // Acceleration X dT = dV
353 // dT = dT / Acceleration
354 //d = (Vf^2 - Vo^2) / (2*a)
355 double accelTime = (vRotate - 15) / accel;
356 //cerr << "Using " << accelTime << " as total acceleration time" << endl;
357 double accelDistance = (vRotate*vRotate - 15*15) / (2*accel);
358 //cerr << "Using " << accelDistance << " " << accel << " " << vRotate << endl;
360 // Get the current active runway, based on code from David Luff
361 // This should actually be unified and extended to include
362 // Preferential runway use schema's
363 // NOTE: DT (2009-01-18: IIRC, this is currently already the case,
364 // because the getActive runway function takes care of that.
367 string rwyClass = getRunwayClassFromTrafficType(fltType);
368 double heading = ac->getTrafficRef()->getCourse();
369 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
370 rwy = apt->getRunwayByIdent(activeRunway);
373 double airportElev = apt->getElevation();
374 // Acceleration point, 105 meters into the runway,
375 SGGeod accelPoint = rwy->pointOnCenterline(105.0);
376 wpt = createOnGround(ac, "accel", accelPoint, airportElev, speed);
377 waypoints.push_back(wpt);
379 //Start Climbing to 3000 ft. Let's do this
380 // at the center of the runway for now:
381 SGGeod rotate = rwy->pointOnCenterline(105.0+accelDistance);
382 wpt = cloneWithPos(ac, wpt, "SOC", rotate);
383 wpt->altitude = airportElev+1000;
384 wpt->on_ground = false;
385 waypoints.push_back(wpt);
387 wpt = cloneWithPos(ac, wpt, "3000 ft", rwy->end());
388 wpt->altitude = airportElev+3000;
389 waypoints.push_back(wpt);
391 // Finally, add two more waypoints, so that aircraft will remain under
392 // Tower control until they have reached the 3000 ft climb point
393 SGGeod pt = rwy->pointOnCenterline(5000 + rwy->lengthM() * 0.5);
394 wpt = cloneWithPos(ac, wpt, "5000 ft", pt);
395 wpt->altitude = airportElev+5000;
396 waypoints.push_back(wpt);
399 /*******************************************************************
401 * initialize the Aircraft at the parking location
402 ******************************************************************/
403 void FGAIFlightPlan::createClimb(FGAIAircraft *ac, bool firstFlight, FGAirport *apt, double speed, double alt, const string &fltType)
408 string rwyClass = getRunwayClassFromTrafficType(fltType);
409 double heading = ac->getTrafficRef()->getCourse();
410 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
411 rwy = apt->getRunwayByIdent(activeRunway);
414 SGGeod climb1 = rwy->pointOnCenterline(10*SG_NM_TO_METER);
415 wpt = createInAir(ac, "10000ft climb", climb1, speed, 10000);
416 wpt->gear_down = true;
417 wpt->flaps_down= true;
418 waypoints.push_back(wpt);
420 SGGeod climb2 = rwy->pointOnCenterline(20*SG_NM_TO_METER);
421 wpt = cloneWithPos(ac, wpt, "18000ft climb", climb2);
422 wpt->altitude = 18000;
423 waypoints.push_back(wpt);
427 /*******************************************************************
429 * initialize the Aircraft at the parking location
430 ******************************************************************/
431 void FGAIFlightPlan::createDecent(FGAIAircraft *ac, FGAirport *apt, const string &fltType)
433 // Ten thousand ft. Slowing down to 240 kts
436 //Beginning of Decent
438 // allow "mil" and "gen" as well
439 string rwyClass = getRunwayClassFromTrafficType(fltType);
440 double heading = ac->getTrafficRef()->getCourse();
441 apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading);
442 rwy = apt->getRunwayByIdent(activeRunway);
444 SGGeod descent1 = rwy->pointOnCenterline(-100000); // 100km out
445 wpt = createInAir(ac, "Dec 10000ft", descent1, apt->getElevation(), 240);
446 wpt->crossat = 10000;
447 waypoints.push_back(wpt);
449 // Three thousand ft. Slowing down to 160 kts
450 SGGeod descent2 = rwy->pointOnCenterline(-8*SG_NM_TO_METER); // 8nm out
451 wpt = createInAir(ac, "DEC 3000ft", descent2, apt->getElevation(), 160);
453 wpt->gear_down = true;
454 wpt->flaps_down= true;
455 waypoints.push_back(wpt);
457 /*******************************************************************
459 * initialize the Aircraft at the parking location
460 ******************************************************************/
461 void FGAIFlightPlan::createLanding(FGAIAircraft *ac, FGAirport *apt)
463 // Ten thousand ft. Slowing down to 150 kts
465 double aptElev = apt->getElevation();
467 wpt = createOnGround(ac, "Threshold", rwy->threshold(), aptElev, 150);
468 wpt->crossat = apt->getElevation();
469 waypoints.push_back(wpt);
472 wpt = createOnGround(ac, "Center", rwy->geod(), aptElev, 30);
473 waypoints.push_back(wpt);
475 SGGeod rollOut = rwy->pointOnCenterline(rwy->lengthM() * 0.9);
476 wpt = createOnGround(ac, "Roll Out", rollOut, aptElev, 15);
477 wpt->crossat = apt->getElevation();
478 waypoints.push_back(wpt);
481 /*******************************************************************
483 * initialize the Aircraft at the parking location
484 ******************************************************************/
485 void FGAIFlightPlan::createParking(FGAIAircraft *ac, FGAirport *apt, double radius)
488 double aptElev = apt->getElevation();
493 apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
497 geo_direct_wgs_84 ( 0, lat, lon, heading,
499 &lat2, &lon2, &az2 );
500 wpt = createOnGround(ac, "taxiStart", SGGeod::fromDeg(lon2, lat2), aptElev, 10);
501 waypoints.push_back(wpt);
503 geo_direct_wgs_84 ( 0, lat, lon, heading,
505 &lat2, &lon2, &az2 );
507 wpt = createOnGround(ac, "taxiStart2", SGGeod::fromDeg(lon2, lat2), aptElev, 10);
508 waypoints.push_back(wpt);
510 wpt = createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), aptElev, 10);
511 waypoints.push_back(wpt);
516 * @param fltType a string describing the type of
517 * traffic, normally used for gate assignments
518 * @return a converted string that gives the runway
519 * preference schedule to be used at aircraft having
520 * a preferential runway schedule implemented (i.e.
521 * having a rwyprefs.xml file
523 * Currently valid traffic types for gate assignment:
524 * - gate (commercial gate)
525 * - cargo (commercial gargo),
526 * - ga (general aviation) ,
528 * - mil-fighter (military - fighter),
529 * - mil-transport (military - transport)
531 * Valid runway classes:
532 * - com (commercial traffic: jetliners, passenger and cargo)
533 * - gen (general aviation)
534 * - ul (ultralight: I can imagine that these may share a runway with ga on some airports)
535 * - mil (all military traffic)
537 string FGAIFlightPlan::getRunwayClassFromTrafficType(string fltType)
539 if ((fltType == "gate") || (fltType == "cargo")) {
540 return string("com");
542 if (fltType == "ga") {
543 return string ("gen");
545 if (fltType == "ul") {
548 if ((fltType == "mil-fighter") || (fltType == "mil-transport")) {
549 return string("mil");
551 return string("com");