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);
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 void FGAIFlightPlan::createDefaultTakeoffTaxi(FGAIAircraft *ac, FGAirport* aAirport, FGRunway* aRunway)
153 SGGeod runwayTakeoff = aRunway->pointOnCenterline(5.0);
154 double airportElev = aAirport->getElevation();
157 wpt = createOnGround(ac, "Airport Center", aAirport->geod(), airportElev, 15);
158 waypoints.push_back(wpt);
159 wpt = createOnGround(ac, "Runway Takeoff", runwayTakeoff, airportElev, 15);
160 waypoints.push_back(wpt);
163 void FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft *ac, bool firstFlight,
165 double radius, const string& fltType,
166 const string& acType, const string& airline)
168 double heading, lat, lon;
170 // If this function is called during initialization,
171 // make sure we obtain a valid gate ID first
172 // and place the model at the location of the gate.
174 if (!(apt->getDynamics()->getAvailableParking(&lat, &lon,
179 SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " <<
181 " of flight type " << fltType <<
182 " of airline " << airline <<
183 " at airport " << apt->getId());
187 string rwyClass = getRunwayClassFromTrafficType(fltType);
189 // Only set this if it hasn't been set by ATC already.
190 if (activeRunway.empty()) {
191 //cerr << "Getting runway for " << ac->getTrafficRef()->getCallSign() << " at " << apt->getId() << endl;
192 double depHeading = ac->getTrafficRef()->getCourse();
193 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, depHeading);
195 rwy = apt->getRunwayByIdent(activeRunway);
196 SGGeod runwayTakeoff = rwy->pointOnCenterline(5.0);
198 FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
200 createDefaultTakeoffTaxi(ac, apt, rwy);
205 int runwayId = gn->findNearestNode(runwayTakeoff);
207 // A negative gateId indicates an overflow parking, use a
208 // fallback mechanism for this.
209 // Starting from gate 0 in this case is a bit of a hack
210 // which requires a more proper solution later on.
212 taxiRoute = new FGTaxiRoute;
214 // Determine which node to start from.
216 // Find out which node to start from
217 FGParking *park = apt->getDynamics()->getParking(gateId);
219 node = park->getPushBackPoint();
226 // HAndle case where parking doens't have a node
227 if ((node == 0) && park) {
231 node = lastNodeVisited;
235 *taxiRoute = gn->findShortestRoute(node, runwayId);
238 if (taxiRoute->empty()) {
239 createDefaultTakeoffTaxi(ac, apt, rwy);
244 //bool isPushBackPoint = false;
246 // If this is called during initialization, randomly
247 // skip a number of waypoints to get a more realistic
249 int nrWaypointsToSkip = rand() % taxiRoute->size();
250 // but make sure we always keep two active waypoints
251 // to prevent a segmentation fault
252 for (int i = 0; i < nrWaypointsToSkip-2; i++) {
253 taxiRoute->next(&node);
255 apt->getDynamics()->releaseParking(gateId);
257 if (taxiRoute->size() > 1) {
258 taxiRoute->next(&node); // chop off the first waypoint, because that is already the last of the pushback route
262 // push each node on the taxi route as a waypoint
264 while(taxiRoute->next(&node, &route)) {
266 snprintf (buffer, 10, "%d", node);
267 FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
268 waypoint* wpt = createOnGround(ac, buffer, tn->geod(), apt->getElevation(), 15);
269 wpt->routeIndex = route;
270 waypoints.push_back(wpt);
274 void FGAIFlightPlan::createDefaultLandingTaxi(FGAIAircraft *ac, FGAirport* aAirport)
277 SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
278 double airportElev = aAirport->getElevation();
281 wpt = createOnGround(ac, "Runway Exit", lastWptPos, airportElev, 15);
282 waypoints.push_back(wpt);
283 wpt = createOnGround(ac, "Airport Center", aAirport->geod(), airportElev, 15);
284 waypoints.push_back(wpt);
286 double heading, lat, lon;
287 aAirport->getDynamics()->getParking(gateId, &lat, &lon, &heading);
288 wpt = createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), airportElev, 15);
289 waypoints.push_back(wpt);
292 void FGAIFlightPlan::createLandingTaxi(FGAIAircraft *ac, FGAirport *apt,
293 double radius, const string& fltType,
294 const string& acType, const string& airline)
296 double heading, lat, lon;
297 apt->getDynamics()->getAvailableParking(&lat, &lon, &heading,
298 &gateId, radius, fltType, acType, airline);
301 SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
302 FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
304 // Find a route from runway end to parking/gate.
306 createDefaultLandingTaxi(ac, apt);
311 int runwayId = gn->findNearestNode(lastWptPos);
312 // A negative gateId indicates an overflow parking, use a
313 // fallback mechanism for this.
314 // Starting from gate 0 is a bit of a hack...
317 taxiRoute = new FGTaxiRoute;
319 *taxiRoute = gn->findShortestRoute(runwayId, gateId);
321 *taxiRoute = gn->findShortestRoute(runwayId, 0);
324 if (taxiRoute->empty()) {
325 createDefaultLandingTaxi(ac, apt);
331 int size = taxiRoute->size();
332 // Omit the last two waypoints, as
333 // those are created by createParking()
335 for (int i = 0; i < size-2; i++) {
336 taxiRoute->next(&node, &route);
338 snprintf (buffer, 10, "%d", node);
339 FGTaxiNode *tn = gn->findNode(node);
340 waypoint* wpt = createOnGround(ac, buffer, tn->geod(), apt->getElevation(), 15);
341 wpt->routeIndex = route;
342 waypoints.push_back(wpt);
346 /*******************************************************************
348 * initialize the Aircraft at the parking location
349 ******************************************************************/
350 void FGAIFlightPlan::createTakeOff(FGAIAircraft *ac, bool firstFlight, FGAirport *apt, double speed, const string &fltType)
352 double accel = ac->getPerformance()->acceleration();
353 double vRotate = ac->getPerformance()->vRotate();
354 // Acceleration = dV / dT
355 // Acceleration X dT = dV
356 // dT = dT / Acceleration
357 //d = (Vf^2 - Vo^2) / (2*a)
358 double accelTime = (vRotate - 15) / accel;
359 //cerr << "Using " << accelTime << " as total acceleration time" << endl;
360 double accelDistance = (vRotate*vRotate - 15*15) / (2*accel);
361 //cerr << "Using " << accelDistance << " " << accel << " " << vRotate << endl;
363 // Get the current active runway, based on code from David Luff
364 // This should actually be unified and extended to include
365 // Preferential runway use schema's
366 // NOTE: DT (2009-01-18: IIRC, this is currently already the case,
367 // because the getActive runway function takes care of that.
370 string rwyClass = getRunwayClassFromTrafficType(fltType);
371 double heading = ac->getTrafficRef()->getCourse();
372 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
373 rwy = apt->getRunwayByIdent(activeRunway);
376 double airportElev = apt->getElevation();
377 // Acceleration point, 105 meters into the runway,
378 SGGeod accelPoint = rwy->pointOnCenterline(105.0);
379 wpt = createOnGround(ac, "accel", accelPoint, airportElev, speed);
380 waypoints.push_back(wpt);
382 //Start Climbing to 3000 ft. Let's do this
383 // at the center of the runway for now:
384 SGGeod rotate = rwy->pointOnCenterline(105.0+accelDistance);
385 wpt = cloneWithPos(ac, wpt, "SOC", rotate);
386 wpt->altitude = airportElev+1000;
387 wpt->on_ground = false;
388 waypoints.push_back(wpt);
390 wpt = cloneWithPos(ac, wpt, "3000 ft", rwy->end());
391 wpt->altitude = airportElev+3000;
392 waypoints.push_back(wpt);
394 // Finally, add two more waypoints, so that aircraft will remain under
395 // Tower control until they have reached the 3000 ft climb point
396 SGGeod pt = rwy->pointOnCenterline(5000 + rwy->lengthM() * 0.5);
397 wpt = cloneWithPos(ac, wpt, "5000 ft", pt);
398 wpt->altitude = airportElev+5000;
399 waypoints.push_back(wpt);
402 /*******************************************************************
404 * initialize the Aircraft at the parking location
405 ******************************************************************/
406 void FGAIFlightPlan::createClimb(FGAIAircraft *ac, bool firstFlight, FGAirport *apt, double speed, double alt, const string &fltType)
409 bool planLoaded = false;
413 string rwyClass = getRunwayClassFromTrafficType(fltType);
414 double heading = ac->getTrafficRef()->getCourse();
415 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
416 rwy = apt->getRunwayByIdent(activeRunway);
418 if (fgGetBool("/sim/traffic-manager/use-custom-scenery-data") == true) {
419 string_list sc = globals->get_fg_scenery();
421 // NOTE: Currently for testing only. A slightly more elaborate naming convention
422 // needs to be dropped here.
423 snprintf(buffer, 64, "%s.SID-%s-01.xml", apt->getId().c_str(), activeRunway.c_str() );
424 string airportDir = expandICAODirs(apt->getId());
425 for (string_list_iterator i = sc.begin(); i != sc.end(); i++) {
426 SGPath aptpath( *i );
427 aptpath.append( "Airports" );
428 aptpath.append ( airportDir );
429 aptpath.append( string(buffer) );
430 if (aptpath.exists()) {
431 planLoaded = loadSID(aptpath.str());
432 //cerr << "Reading " << aptpath.str() << endl;
437 SGGeod climb1 = rwy->pointOnCenterline(10*SG_NM_TO_METER);
438 wpt = createInAir(ac, "10000ft climb", climb1, speed, 10000);
439 wpt->gear_down = true;
440 wpt->flaps_down= true;
441 waypoints.push_back(wpt);
443 SGGeod climb2 = rwy->pointOnCenterline(20*SG_NM_TO_METER);
444 wpt = cloneWithPos(ac, wpt, "18000ft climb", climb2);
445 wpt->altitude = 18000;
446 waypoints.push_back(wpt);
450 bool FGAIFlightPlan::loadSID(const string& filename)
454 readProperties(filename, &root);
455 } catch (const sg_exception &e) {
456 SG_LOG(SG_GENERAL, SG_ALERT,
457 "Error reading AI flight plan: " << filename);
458 // cout << path.str() << endl;
462 SGPropertyNode * node = root.getNode("flightplan");
463 for (int i = 0; i < node->nChildren(); i++) {
464 //cout << "Reading waypoint " << i << endl;
465 waypoint* wpt = new waypoint;
466 SGPropertyNode * wpt_node = node->getChild(i);
467 wpt->name = wpt_node->getStringValue("name", "END");
468 wpt->latitude = wpt_node->getDoubleValue("lat", 0);
469 wpt->longitude = wpt_node->getDoubleValue("lon", 0);
470 wpt->altitude = wpt_node->getDoubleValue("alt", 0);
471 wpt->speed = wpt_node->getDoubleValue("ktas", 0);
472 wpt->crossat = wpt_node->getDoubleValue("crossat", -10000);
473 wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
474 wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
475 wpt->on_ground = wpt_node->getBoolValue("on-ground", false);
476 wpt->time_sec = wpt_node->getDoubleValue("time-sec", 0);
477 wpt->time = wpt_node->getStringValue("time", "");
479 if (wpt->name == "END") wpt->finished = true;
480 else wpt->finished = false;
482 waypoints.push_back( wpt );
485 //wpt_iterator = waypoints.begin();
486 //cout << waypoints.size() << " waypoints read." << endl;
490 // NOTE: This is just copied from Airports/readXML.
491 string FGAIFlightPlan::expandICAODirs(const string in){
492 //cerr << "Expanding " << in << endl;
493 if (in.size() == 4) {
495 snprintf(buffer, 11, "%c/%c/%c", in[0], in[1], in[2]);
496 //cerr << "result: " << buffer << endl;
497 return string(buffer);
505 /*******************************************************************
507 * initialize the Aircraft at the parking location
508 ******************************************************************/
509 void FGAIFlightPlan::createDecent(FGAIAircraft *ac, FGAirport *apt, const string &fltType)
511 // Ten thousand ft. Slowing down to 240 kts
514 //Beginning of Decent
516 // allow "mil" and "gen" as well
517 string rwyClass = getRunwayClassFromTrafficType(fltType);
518 double heading = ac->getTrafficRef()->getCourse();
519 apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading);
520 rwy = apt->getRunwayByIdent(activeRunway);
522 SGGeod descent1 = rwy->pointOnCenterline(-100000); // 100km out
523 wpt = createInAir(ac, "Dec 10000ft", descent1, apt->getElevation(), 240);
524 wpt->crossat = 10000;
525 waypoints.push_back(wpt);
527 // Three thousand ft. Slowing down to 160 kts
528 SGGeod descent2 = rwy->pointOnCenterline(-8*SG_NM_TO_METER); // 8nm out
529 wpt = createInAir(ac, "DEC 3000ft", descent2, apt->getElevation(), 160);
531 wpt->gear_down = true;
532 wpt->flaps_down= true;
533 waypoints.push_back(wpt);
535 /*******************************************************************
537 * initialize the Aircraft at the parking location
538 ******************************************************************/
539 void FGAIFlightPlan::createLanding(FGAIAircraft *ac, FGAirport *apt)
541 // Ten thousand ft. Slowing down to 150 kts
543 double aptElev = apt->getElevation();
545 wpt = createOnGround(ac, "Threshold", rwy->threshold(), aptElev, 150);
546 wpt->crossat = apt->getElevation();
547 waypoints.push_back(wpt);
550 wpt = createOnGround(ac, "Center", rwy->geod(), aptElev, 30);
551 waypoints.push_back(wpt);
553 SGGeod rollOut = rwy->pointOnCenterline(rwy->lengthM() * 0.9);
554 wpt = createOnGround(ac, "Roll Out", rollOut, aptElev, 15);
555 wpt->crossat = apt->getElevation();
556 waypoints.push_back(wpt);
559 /*******************************************************************
561 * initialize the Aircraft at the parking location
562 ******************************************************************/
563 void FGAIFlightPlan::createParking(FGAIAircraft *ac, FGAirport *apt, double radius)
566 double aptElev = apt->getElevation();
571 apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
575 geo_direct_wgs_84 ( 0, lat, lon, heading,
577 &lat2, &lon2, &az2 );
578 wpt = createOnGround(ac, "taxiStart", SGGeod::fromDeg(lon2, lat2), aptElev, 10);
579 waypoints.push_back(wpt);
581 geo_direct_wgs_84 ( 0, lat, lon, heading,
583 &lat2, &lon2, &az2 );
585 wpt = createOnGround(ac, "taxiStart2", SGGeod::fromDeg(lon2, lat2), aptElev, 10);
586 waypoints.push_back(wpt);
588 wpt = createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), aptElev, 10);
589 waypoints.push_back(wpt);
594 * @param fltType a string describing the type of
595 * traffic, normally used for gate assignments
596 * @return a converted string that gives the runway
597 * preference schedule to be used at aircraft having
598 * a preferential runway schedule implemented (i.e.
599 * having a rwyprefs.xml file
601 * Currently valid traffic types for gate assignment:
602 * - gate (commercial gate)
603 * - cargo (commercial gargo),
604 * - ga (general aviation) ,
606 * - mil-fighter (military - fighter),
607 * - mil-transport (military - transport)
609 * Valid runway classes:
610 * - com (commercial traffic: jetliners, passenger and cargo)
611 * - gen (general aviation)
612 * - ul (ultralight: I can imagine that these may share a runway with ga on some airports)
613 * - mil (all military traffic)
615 string FGAIFlightPlan::getRunwayClassFromTrafficType(string fltType)
617 if ((fltType == "gate") || (fltType == "cargo")) {
618 return string("com");
620 if (fltType == "ga") {
621 return string ("gen");
623 if (fltType == "ul") {
626 if ((fltType == "mil-fighter") || (fltType == "mil-transport")) {
627 return string("mil");
629 return string("com");