]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIFlightPlanCreate.cxx
87c2531482a2e2c69bd08b0bc837605d342945cf
[flightgear.git] / src / AIModel / AIFlightPlanCreate.cxx
1 /******************************************************************************
2  * AIFlightPlanCreate.cxx
3  * Written by Durk Talsma, started May, 2004.
4  *
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.
9  *
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.
14  *
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.
18  *
19  **************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
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"
31
32 #include <Environment/environment_mgr.hxx>
33 #include <Environment/environment.hxx>
34
35
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) 
39  *
40  * This is the top-level function, and the only one that is publicly available.
41  *
42  */ 
43
44
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)
51
52   int currWpt = wpt_iterator - waypoints.begin();
53   switch(legNr)
54     {
55       case 1:
56       createPushBack(ac, firstFlight,dep, latitude, longitude, 
57                      radius, fltType, aircraftType, airline);
58       break;
59     case 2: 
60       createTakeoffTaxi(ac, firstFlight, dep, radius, fltType, aircraftType, airline);
61       break;
62     case 3: 
63       createTakeOff(ac, firstFlight, dep, speed, fltType);
64       break;
65     case 4: 
66       createClimb(ac, firstFlight, dep, speed, alt, fltType);
67       break;
68     case 5: 
69       createCruise(ac, firstFlight, dep,arr, latitude, longitude, speed, alt, fltType);
70       break;
71     case 6: 
72       createDecent(ac, arr, fltType);
73       break;
74     case 7: 
75       createLanding(ac, arr);
76       break;
77     case 8: 
78       createLandingTaxi(ac, arr, radius, fltType, aircraftType, airline);
79       break;
80       case 9: 
81         createParking(ac, arr, radius);
82       break;
83     default:
84       //exit(1);
85       SG_LOG(SG_INPUT, SG_ALERT, "AIFlightPlan::create() attempting to create unknown leg"
86              " this is probably an internal program error");
87     }
88   wpt_iterator = waypoints.begin()+currWpt;
89   leg++;
90 }
91
92 FGAIFlightPlan::waypoint*
93 FGAIFlightPlan::createOnGround(FGAIAircraft *ac, const std::string& aName, const SGGeod& aPos, double aElev, double aSpeed)
94 {
95   waypoint* wpt = new waypoint;
96   wpt->name = aName;
97   wpt->longitude = aPos.getLongitudeDeg();
98   wpt->latitude = aPos.getLatitudeDeg();
99   wpt->altitude  = aElev;
100   wpt->speed     = aSpeed; 
101   wpt->crossat   = -10000;
102   wpt->gear_down = true;
103   wpt->flaps_down= true;
104   wpt->finished  = false;
105   wpt->on_ground = true;
106   wpt->routeIndex= 0;
107   return wpt;
108 }
109
110 FGAIFlightPlan::waypoint*
111 FGAIFlightPlan::createInAir(FGAIAircraft *ac, const std::string& aName, const SGGeod& aPos, double aElev, double aSpeed)
112 {
113   waypoint* wpt = new waypoint;
114   wpt->name = aName;
115   wpt->longitude = aPos.getLongitudeDeg();
116   wpt->latitude = aPos.getLatitudeDeg();
117   wpt->altitude  = aElev;
118   wpt->speed     = aSpeed; 
119   wpt->crossat   = -10000;
120   wpt->gear_down = false;
121   wpt->flaps_down= false;
122   wpt->finished  = false;
123   wpt->on_ground = false;
124   wpt->routeIndex= 0;
125   return wpt;
126 }
127
128 FGAIFlightPlan::waypoint*
129 FGAIFlightPlan::cloneWithPos(FGAIAircraft *ac, waypoint* aWpt, const std::string& aName, const SGGeod& aPos)
130 {
131   waypoint* wpt = new waypoint;
132   wpt->name = aName;
133   wpt->longitude = aPos.getLongitudeDeg();
134   wpt->latitude = aPos.getLatitudeDeg();
135   
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;
143   wpt->routeIndex = 0;
144   
145   return wpt;
146 }
147
148 void FGAIFlightPlan::createDefaultTakeoffTaxi(FGAIAircraft *ac, FGAirport* aAirport, FGRunway* aRunway)
149 {
150   SGGeod runwayTakeoff = aRunway->pointOnCenterline(5.0);
151   double airportElev = aAirport->getElevation();
152   
153   waypoint* wpt;
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);     
158 }
159
160 void FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft *ac, bool firstFlight, 
161                                 FGAirport *apt,
162                                 double radius, const string& fltType, 
163                                 const string& acType, const string& airline)
164 {
165   double heading, lat, lon;
166   
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.
170   if (firstFlight) {
171     if (!(apt->getDynamics()->getAvailableParking(&lat, &lon, 
172               &heading, &gateId, 
173               radius, fltType, 
174               acType, airline)))
175     {
176       SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " << 
177        acType <<
178        " of flight type " << fltType <<
179        " of airline     " << airline <<
180        " at airport     " << apt->getId());
181     }
182   }
183   
184   string rwyClass = getRunwayClassFromTrafficType(fltType);
185
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);
191   }
192   rwy = apt->getRunwayByIdent(activeRunway);
193   SGGeod runwayTakeoff = rwy->pointOnCenterline(5.0);
194
195   FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
196   if (!gn->exists()) {
197     createDefaultTakeoffTaxi(ac, apt, rwy);
198     return;
199   }
200   
201   intVec ids;
202   int runwayId = gn->findNearestNode(runwayTakeoff);
203
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.
208   delete taxiRoute;
209   taxiRoute = new FGTaxiRoute;
210
211   // Determine which node to start from.
212   int node = 0;
213   // Find out which node to start from
214   FGParking *park = apt->getDynamics()->getParking(gateId);
215   if (park) {
216     node = park->getPushBackPoint();
217   }
218   
219   if (node == -1) {
220     node = gateId;
221   }
222   
223   // HAndle case where parking doens't have a node
224   if ((node == 0) && park) {
225     if (firstFlight) {
226       node = gateId;
227     } else {
228       node = lastNodeVisited;
229     }
230   }
231
232   *taxiRoute = gn->findShortestRoute(node, runwayId);
233   intVecIterator i;
234          
235   if (taxiRoute->empty()) {
236     createDefaultTakeoffTaxi(ac, apt, rwy);
237     return;
238   }
239   
240   taxiRoute->first();
241   //bool isPushBackPoint = false;
242   if (firstFlight) {
243     // If this is called during initialization, randomly
244     // skip a number of waypoints to get a more realistic
245     // taxi situation.
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);
251     }
252     apt->getDynamics()->releaseParking(gateId);
253   } else {
254     if (taxiRoute->size() > 1) {
255       taxiRoute->next(&node); // chop off the first waypoint, because that is already the last of the pushback route
256     }
257   }
258   
259   // push each node on the taxi route as a waypoint
260   int route;
261   while(taxiRoute->next(&node, &route)) {
262                 char buffer[10];
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);
268   }
269 }
270
271 void FGAIFlightPlan::createDefaultLandingTaxi(FGAIAircraft *ac, FGAirport* aAirport)
272 {
273   SGGeod lastWptPos = 
274     SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
275   double airportElev = aAirport->getElevation();
276   
277   waypoint* wpt;
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);
282   
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);
287 }
288
289 void FGAIFlightPlan::createLandingTaxi(FGAIAircraft *ac, FGAirport *apt,
290                                 double radius, const string& fltType, 
291                                 const string& acType, const string& airline)
292 {
293   double heading, lat, lon;
294   apt->getDynamics()->getAvailableParking(&lat, &lon, &heading, 
295         &gateId, radius, fltType, acType, airline);
296   
297   SGGeod lastWptPos = 
298     SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
299   FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
300   
301    // Find a route from runway end to parking/gate.
302   if (!gn->exists()) {
303     createDefaultLandingTaxi(ac, apt);
304     return;
305   }
306   
307         intVec ids;
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...
312   //FGTaxiRoute route;
313   delete taxiRoute;
314   taxiRoute = new FGTaxiRoute;
315   if (gateId >= 0)
316     *taxiRoute = gn->findShortestRoute(runwayId, gateId);
317   else
318     *taxiRoute = gn->findShortestRoute(runwayId, 0);
319   intVecIterator i;
320   
321   if (taxiRoute->empty()) {
322     createDefaultLandingTaxi(ac, apt);
323     return;
324   }
325   
326   int node;
327   taxiRoute->first();
328   int size = taxiRoute->size();
329   // Omit the last two waypoints, as 
330   // those are created by createParking()
331   int route;
332   for (int i = 0; i < size-2; i++) {
333     taxiRoute->next(&node, &route);
334     char buffer[10];
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);
340   }
341 }
342
343 /*******************************************************************
344  * CreateTakeOff 
345  * initialize the Aircraft at the parking location
346  ******************************************************************/
347 void FGAIFlightPlan::createTakeOff(FGAIAircraft *ac, bool firstFlight, FGAirport *apt, double speed, const string &fltType)
348 {
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;
359     waypoint *wpt;
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.
365     if (firstFlight)
366     {
367         string rwyClass = getRunwayClassFromTrafficType(fltType);
368         double heading = ac->getTrafficRef()->getCourse();
369         apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
370         rwy = apt->getRunwayByIdent(activeRunway);
371     }
372
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); 
378  
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);
386
387     wpt = cloneWithPos(ac, wpt, "3000 ft", rwy->end());
388     wpt->altitude  = airportElev+3000;
389     waypoints.push_back(wpt);
390
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);
397 }
398   
399 /*******************************************************************
400  * CreateClimb
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)
404 {
405   waypoint *wpt;
406
407   if (firstFlight) {
408     string rwyClass = getRunwayClassFromTrafficType(fltType);
409     double heading = ac->getTrafficRef()->getCourse();
410     apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
411     rwy = apt->getRunwayByIdent(activeRunway);
412   }
413   
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); 
419
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); 
424 }
425
426
427 /*******************************************************************
428  * CreateDecent
429  * initialize the Aircraft at the parking location
430  ******************************************************************/
431 void FGAIFlightPlan::createDecent(FGAIAircraft *ac, FGAirport *apt, const string &fltType)
432 {
433   // Ten thousand ft. Slowing down to 240 kts
434   waypoint *wpt;
435
436   //Beginning of Decent
437   //string name;
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);
443      
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);  
448   
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);
452   wpt->crossat   = 3000;
453   wpt->gear_down = true;
454   wpt->flaps_down= true;
455   waypoints.push_back(wpt);
456 }
457 /*******************************************************************
458  * CreateLanding
459  * initialize the Aircraft at the parking location
460  ******************************************************************/
461 void FGAIFlightPlan::createLanding(FGAIAircraft *ac, FGAirport *apt)
462 {
463   // Ten thousand ft. Slowing down to 150 kts
464   waypoint *wpt;
465   double aptElev = apt->getElevation();
466   //Runway Threshold
467   wpt = createOnGround(ac, "Threshold", rwy->threshold(), aptElev, 150);
468   wpt->crossat = apt->getElevation();
469   waypoints.push_back(wpt); 
470
471  // Roll-out
472   wpt = createOnGround(ac, "Center", rwy->geod(), aptElev, 30);
473   waypoints.push_back(wpt);
474
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); 
479 }
480
481 /*******************************************************************
482  * CreateParking
483  * initialize the Aircraft at the parking location
484  ******************************************************************/
485 void FGAIFlightPlan::createParking(FGAIAircraft *ac, FGAirport *apt, double radius)
486 {
487   waypoint* wpt;
488   double aptElev = apt->getElevation();
489   double lat, lat2;
490   double lon, lon2;
491   double az2;
492   double heading;
493   apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
494   heading += 180.0;
495   if (heading > 360)
496     heading -= 360; 
497   geo_direct_wgs_84 ( 0, lat, lon, heading, 
498                       2.2*radius,           
499                       &lat2, &lon2, &az2 );
500   wpt = createOnGround(ac, "taxiStart", SGGeod::fromDeg(lon2, lat2), aptElev, 10);
501   waypoints.push_back(wpt);
502   
503   geo_direct_wgs_84 ( 0, lat, lon, heading, 
504                       0.1 *radius,           
505                       &lat2, &lon2, &az2 );
506           
507   wpt = createOnGround(ac, "taxiStart2", SGGeod::fromDeg(lon2, lat2), aptElev, 10);
508   waypoints.push_back(wpt);   
509
510   wpt = createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), aptElev, 10);
511   waypoints.push_back(wpt);
512 }
513
514 /**
515  *
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
522  * 
523  * Currently valid traffic types for gate assignment:
524  * - gate (commercial gate)
525  * - cargo (commercial gargo),
526  * - ga (general aviation) ,
527  * - ul (ultralight),
528  * - mil-fighter (military - fighter),
529  * - mil-transport (military - transport)
530  *
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)
536  */
537 string FGAIFlightPlan::getRunwayClassFromTrafficType(string fltType)
538 {
539     if ((fltType == "gate") || (fltType == "cargo")) { 
540         return string("com");
541     }
542     if (fltType == "ga") {
543         return string ("gen");
544     }
545     if (fltType == "ul") {
546         return string("ul");
547     }
548     if ((fltType == "mil-fighter") || (fltType == "mil-transport")) { 
549         return string("mil");
550     }
551    return string("com");
552 }