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>
30 #include <Environment/environment_mgr.hxx>
31 #include <Environment/environment.hxx>
34 /* FGAIFlightPlan::create()
35 * dynamically create a flight plan for AI traffic, based on data provided by the
36 * Traffic Manager, when reading a filed flightplan failes. (DT, 2004/07/10)
38 * This is the top-level function, and the only one that is publicly available.
43 // Check lat/lon values during initialization;
44 void FGAIFlightPlan::create(FGAirport *dep, FGAirport *arr, int legNr,
45 double alt, double speed, double latitude,
46 double longitude, bool firstFlight,double radius,
47 const string& fltType, const string& aircraftType,
48 const string& airline)
50 int currWpt = wpt_iterator - waypoints.begin();
54 createPushBack(firstFlight,dep, latitude, longitude,
55 radius, fltType, aircraftType, airline);
58 createTakeoffTaxi(firstFlight, dep, radius, fltType, aircraftType, airline);
61 createTakeOff(firstFlight, dep, speed, fltType);
64 createClimb(firstFlight, dep, speed, alt, fltType);
67 createCruise(firstFlight, dep,arr, latitude, longitude, speed, alt, fltType);
70 createDecent(arr, fltType);
76 createLandingTaxi(arr, radius, fltType, aircraftType, airline);
79 createParking(arr, radius);
83 SG_LOG(SG_INPUT, SG_ALERT, "AIFlightPlan::create() attempting to create unknown leg"
84 " this is probably an internal program error");
86 wpt_iterator = waypoints.begin()+currWpt;
90 FGAIFlightPlan::waypoint*
91 FGAIFlightPlan::createOnGround(const std::string& aName, const SGGeod& aPos, double aElev, double aSpeed)
93 waypoint* wpt = new waypoint;
95 wpt->longitude = aPos.getLongitudeDeg();
96 wpt->latitude = aPos.getLatitudeDeg();
97 wpt->altitude = aElev;
99 wpt->crossat = -10000;
100 wpt->gear_down = true;
101 wpt->flaps_down= true;
102 wpt->finished = false;
103 wpt->on_ground = true;
108 FGAIFlightPlan::waypoint*
109 FGAIFlightPlan::createInAir(const std::string& aName, const SGGeod& aPos, double aElev, double aSpeed)
111 waypoint* wpt = new waypoint;
113 wpt->longitude = aPos.getLongitudeDeg();
114 wpt->latitude = aPos.getLatitudeDeg();
115 wpt->altitude = aElev;
117 wpt->crossat = -10000;
118 wpt->gear_down = false;
119 wpt->flaps_down= false;
120 wpt->finished = false;
121 wpt->on_ground = false;
126 FGAIFlightPlan::waypoint*
127 FGAIFlightPlan::cloneWithPos(waypoint* aWpt, const std::string& aName, const SGGeod& aPos)
129 waypoint* wpt = new waypoint;
131 wpt->longitude = aPos.getLongitudeDeg();
132 wpt->latitude = aPos.getLatitudeDeg();
134 wpt->altitude = aWpt->altitude;
135 wpt->speed = aWpt->speed;
136 wpt->crossat = aWpt->crossat;
137 wpt->gear_down = aWpt->gear_down;
138 wpt->flaps_down= aWpt->flaps_down;
139 wpt->finished = aWpt->finished;
140 wpt->on_ground = aWpt->on_ground;
146 void FGAIFlightPlan::createDefaultTakeoffTaxi(FGAirport* aAirport, FGRunway* aRunway)
148 SGGeod runwayTakeoff = aRunway->pointOnCenterline(5.0);
149 double airportElev = aAirport->getElevation();
152 wpt = createOnGround("Airport Center", aAirport->geod(), airportElev, 15);
153 waypoints.push_back(wpt);
154 wpt = createOnGround("Runway Takeoff", runwayTakeoff, airportElev, 15);
155 waypoints.push_back(wpt);
158 void FGAIFlightPlan::createTakeoffTaxi(bool firstFlight,
160 double radius, const string& fltType,
161 const string& acType, const string& airline)
163 double heading, lat, lon;
165 // If this function is called during initialization,
166 // make sure we obtain a valid gate ID first
167 // and place the model at the location of the gate.
169 if (!(apt->getDynamics()->getAvailableParking(&lat, &lon,
174 SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " <<
176 " of flight type " << fltType <<
177 " of airline " << airline <<
178 " at airport " << apt->getId());
182 string rwyClass = getRunwayClassFromTrafficType(fltType);
183 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway);
184 rwy = apt->getRunwayByIdent(activeRunway);
185 SGGeod runwayTakeoff = rwy->pointOnCenterline(5.0);
187 FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
189 createDefaultTakeoffTaxi(apt, rwy);
194 int runwayId = gn->findNearestNode(runwayTakeoff);
196 // A negative gateId indicates an overflow parking, use a
197 // fallback mechanism for this.
198 // Starting from gate 0 in this case is a bit of a hack
199 // which requires a more proper solution later on.
201 taxiRoute = new FGTaxiRoute;
203 // Determine which node to start from.
205 // Find out which node to start from
206 FGParking *park = apt->getDynamics()->getParking(gateId);
208 node = park->getPushBackPoint();
215 // HAndle case where parking doens't have a node
216 if ((node == 0) && park) {
220 node = lastNodeVisited;
224 *taxiRoute = gn->findShortestRoute(node, runwayId);
227 if (taxiRoute->empty()) {
228 createDefaultTakeoffTaxi(apt, rwy);
233 //bool isPushBackPoint = false;
235 // If this is called during initialization, randomly
236 // skip a number of waypoints to get a more realistic
238 int nrWaypointsToSkip = rand() % taxiRoute->size();
239 // but make sure we always keep two active waypoints
240 // to prevent a segmentation fault
241 for (int i = 0; i < nrWaypointsToSkip-2; i++) {
242 taxiRoute->next(&node);
244 apt->getDynamics()->releaseParking(gateId);
246 if (taxiRoute->size() > 1) {
247 taxiRoute->next(&node); // chop off the first waypoint, because that is already the last of the pushback route
251 // push each node on the taxi route as a waypoint
253 while(taxiRoute->next(&node, &route)) {
255 snprintf (buffer, 10, "%d", node);
256 FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
257 waypoint* wpt = createOnGround(buffer, tn->geod(), apt->getElevation(), 15);
258 wpt->routeIndex = route;
259 waypoints.push_back(wpt);
263 void FGAIFlightPlan::createDefaultLandingTaxi(FGAirport* aAirport)
266 SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
267 double airportElev = aAirport->getElevation();
270 wpt = createOnGround("Runway Exit", lastWptPos, airportElev, 15);
271 waypoints.push_back(wpt);
272 wpt = createOnGround("Airport Center", aAirport->geod(), airportElev, 15);
273 waypoints.push_back(wpt);
275 double heading, lat, lon;
276 aAirport->getDynamics()->getParking(gateId, &lat, &lon, &heading);
277 wpt = createOnGround("END", SGGeod::fromDeg(lon, lat), airportElev, 15);
278 waypoints.push_back(wpt);
281 void FGAIFlightPlan::createLandingTaxi(FGAirport *apt,
282 double radius, const string& fltType,
283 const string& acType, const string& airline)
285 double heading, lat, lon;
286 apt->getDynamics()->getAvailableParking(&lat, &lon, &heading,
287 &gateId, radius, fltType, acType, airline);
290 SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
291 FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
293 // Find a route from runway end to parking/gate.
295 createDefaultLandingTaxi(apt);
300 int runwayId = gn->findNearestNode(lastWptPos);
301 // A negative gateId indicates an overflow parking, use a
302 // fallback mechanism for this.
303 // Starting from gate 0 is a bit of a hack...
306 taxiRoute = new FGTaxiRoute;
308 *taxiRoute = gn->findShortestRoute(runwayId, gateId);
310 *taxiRoute = gn->findShortestRoute(runwayId, 0);
313 if (taxiRoute->empty()) {
314 createDefaultLandingTaxi(apt);
320 int size = taxiRoute->size();
321 // Omit the last two waypoints, as
322 // those are created by createParking()
324 for (int i = 0; i < size-2; i++) {
325 taxiRoute->next(&node, &route);
327 snprintf (buffer, 10, "%d", node);
328 FGTaxiNode *tn = gn->findNode(node);
329 waypoint* wpt = createOnGround(buffer, tn->geod(), apt->getElevation(), 15);
330 wpt->routeIndex = route;
331 waypoints.push_back(wpt);
335 /*******************************************************************
337 * initialize the Aircraft at the parking location
338 ******************************************************************/
339 void FGAIFlightPlan::createTakeOff(bool firstFlight, FGAirport *apt, double speed, const string &fltType)
343 // Get the current active runway, based on code from David Luff
344 // This should actually be unified and extended to include
345 // Preferential runway use schema's
348 string rwyClass = getRunwayClassFromTrafficType(fltType);
349 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway);
350 rwy = apt->getRunwayByIdent(activeRunway);
353 double airportElev = apt->getElevation();
354 // Acceleration point, 105 meters into the runway,
355 SGGeod accelPoint = rwy->pointOnCenterline(105.0);
356 wpt = createOnGround("accel", accelPoint, airportElev, speed);
357 waypoints.push_back(wpt);
359 //Start Climbing to 3000 ft. Let's do this
360 // at the center of the runway for now:
361 wpt = cloneWithPos(wpt, "SOC", rwy->geod());
362 wpt->altitude = airportElev+1000;
363 wpt->on_ground = false;
364 waypoints.push_back(wpt);
366 wpt = cloneWithPos(wpt, "3000 ft", rwy->reverseThreshold());
367 wpt->altitude = airportElev+3000;
368 waypoints.push_back(wpt);
370 // Finally, add two more waypoints, so that aircraft will remain under
371 // Tower control until they have reached the 3000 ft climb point
372 SGGeod pt = rwy->pointOnCenterline(5000 + rwy->lengthM() * 0.5);
373 wpt = cloneWithPos(wpt, "5000 ft", pt);
374 wpt->altitude = airportElev+5000;
375 waypoints.push_back(wpt);
378 /*******************************************************************
380 * initialize the Aircraft at the parking location
381 ******************************************************************/
382 void FGAIFlightPlan::createClimb(bool firstFlight, FGAirport *apt, double speed, double alt, const string &fltType)
387 string rwyClass = getRunwayClassFromTrafficType(fltType);
388 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway);
389 rwy = apt->getRunwayByIdent(activeRunway);
392 SGGeod climb1 = rwy->pointOnCenterline(10*SG_NM_TO_METER);
393 wpt = createInAir("10000ft climb", climb1, speed, 10000);
394 wpt->gear_down = true;
395 wpt->flaps_down= true;
396 waypoints.push_back(wpt);
398 SGGeod climb2 = rwy->pointOnCenterline(20*SG_NM_TO_METER);
399 wpt = cloneWithPos(wpt, "18000ft climb", climb2);
400 wpt->altitude = 18000;
401 waypoints.push_back(wpt);
405 /*******************************************************************
407 * initialize the Aircraft at the parking location
408 ******************************************************************/
409 void FGAIFlightPlan::createDecent(FGAirport *apt, const string &fltType)
411 // Ten thousand ft. Slowing down to 240 kts
414 //Beginning of Decent
416 // allow "mil" and "gen" as well
417 string rwyClass = getRunwayClassFromTrafficType(fltType);
418 apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway);
419 rwy = apt->getRunwayByIdent(activeRunway);
421 SGGeod descent1 = rwy->pointOnCenterline(-100000); // 100km out
422 wpt = createInAir("Dec 10000ft", descent1, apt->getElevation(), 240);
423 wpt->crossat = 10000;
424 waypoints.push_back(wpt);
426 // Three thousand ft. Slowing down to 160 kts
427 SGGeod descent2 = rwy->pointOnCenterline(-8*SG_NM_TO_METER); // 8nm out
428 wpt = createInAir("DEC 3000ft", descent2, apt->getElevation(), 160);
430 wpt->gear_down = true;
431 wpt->flaps_down= true;
432 waypoints.push_back(wpt);
434 /*******************************************************************
436 * initialize the Aircraft at the parking location
437 ******************************************************************/
438 void FGAIFlightPlan::createLanding(FGAirport *apt)
440 // Ten thousand ft. Slowing down to 150 kts
442 double aptElev = apt->getElevation();
444 wpt = createOnGround("Threshold", rwy->displacedThreshold(), aptElev, 150);
445 wpt->crossat = apt->getElevation();
446 waypoints.push_back(wpt);
449 wpt = createOnGround("Center", rwy->geod(), aptElev, 30);
450 waypoints.push_back(wpt);
452 SGGeod rollOut = rwy->pointOnCenterline(rwy->lengthM() * 0.9);
453 wpt = createOnGround("Roll Out", rollOut, aptElev, 15);
454 wpt->crossat = apt->getElevation();
455 waypoints.push_back(wpt);
458 /*******************************************************************
460 * initialize the Aircraft at the parking location
461 ******************************************************************/
462 void FGAIFlightPlan::createParking(FGAirport *apt, double radius)
465 double aptElev = apt->getElevation();
470 apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
474 geo_direct_wgs_84 ( 0, lat, lon, heading,
476 &lat2, &lon2, &az2 );
477 wpt = createOnGround("taxiStart", SGGeod::fromDeg(lon2, lat2), aptElev, 10);
478 waypoints.push_back(wpt);
480 geo_direct_wgs_84 ( 0, lat, lon, heading,
482 &lat2, &lon2, &az2 );
484 wpt = createOnGround("taxiStart2", SGGeod::fromDeg(lon2, lat2), aptElev, 10);
485 waypoints.push_back(wpt);
487 wpt = createOnGround("END", SGGeod::fromDeg(lon, lat), aptElev, 10);
488 waypoints.push_back(wpt);
493 * @param fltType a string describing the type of
494 * traffic, normally used for gate assignments
495 * @return a converted string that gives the runway
496 * preference schedule to be used at aircraft having
497 * a preferential runway schedule implemented (i.e.
498 * having a rwyprefs.xml file
500 * Currently valid traffic types for gate assignment:
501 * - gate (commercial gate)
502 * - cargo (commercial gargo),
503 * - ga (general aviation) ,
505 * - mil-fighter (military - fighter),
506 * - mil-transport (military - transport)
508 * Valid runway classes:
509 * - com (commercial traffic: jetliners, passenger and cargo)
510 * - gen (general aviation)
511 * - ul (ultralight: I can imagine that these may share a runway with ga on some airports)
512 * - mil (all military traffic)
514 string FGAIFlightPlan::getRunwayClassFromTrafficType(string fltType)
516 if ((fltType == "gate") || (fltType == "cargo")) {
517 return string("com");
519 if (fltType == "ga") {
520 return string ("gen");
522 if (fltType == "ul") {
525 if ((fltType == "mil-fighter") || (fltType == "mil-transport")) {
526 return string("mil");
528 return string("com");