]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIFlightPlanCreate.cxx
77032b3fac87aff29b8eb954dbd38e3df23621e2
[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 <simgear/props/props.hxx>
28 #include <simgear/props/props_io.hxx>
29
30 #include <Airports/runways.hxx>
31 #include <Airports/dynamics.hxx>
32 #include "AIAircraft.hxx"
33 #include "performancedata.hxx"
34
35 #include <Environment/environment_mgr.hxx>
36 #include <Environment/environment.hxx>
37
38
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) 
42  *
43  * This is the top-level function, and the only one that is publicly available.
44  *
45  */ 
46
47
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)
54
55   int currWpt = wpt_iterator - waypoints.begin();
56   switch(legNr)
57     {
58       case 1:
59       createPushBack(ac, firstFlight,dep, latitude, longitude, 
60                      radius, fltType, aircraftType, airline);
61       break;
62     case 2: 
63       createTakeoffTaxi(ac, firstFlight, dep, radius, fltType, aircraftType, airline);
64       break;
65     case 3: 
66       createTakeOff(ac, firstFlight, dep, speed, fltType);
67       break;
68     case 4: 
69       createClimb(ac, firstFlight, dep, speed, alt, fltType);
70       break;
71     case 5: 
72       createCruise(ac, firstFlight, dep,arr, latitude, longitude, speed, alt, fltType);
73       break;
74     case 6: 
75       createDecent(ac, arr, fltType);
76       break;
77     case 7: 
78       createLanding(ac, arr);
79       break;
80     case 8: 
81       createLandingTaxi(ac, arr, radius, fltType, aircraftType, airline);
82       break;
83       case 9: 
84         createParking(ac, arr, radius);
85       break;
86     default:
87       //exit(1);
88       SG_LOG(SG_INPUT, SG_ALERT, "AIFlightPlan::create() attempting to create unknown leg"
89              " this is probably an internal program error");
90     }
91   wpt_iterator = waypoints.begin()+currWpt;
92   leg++;
93 }
94
95 FGAIFlightPlan::waypoint*
96 FGAIFlightPlan::createOnGround(FGAIAircraft *ac, const std::string& aName, const SGGeod& aPos, double aElev, double aSpeed)
97 {
98   waypoint* wpt = new waypoint;
99   wpt->name = aName;
100   wpt->longitude = aPos.getLongitudeDeg();
101   wpt->latitude = aPos.getLatitudeDeg();
102   wpt->altitude  = aElev;
103   wpt->speed     = aSpeed; 
104   wpt->crossat   = -10000;
105   wpt->gear_down = true;
106   wpt->flaps_down= true;
107   wpt->finished  = false;
108   wpt->on_ground = true;
109   wpt->routeIndex= 0;
110   return wpt;
111 }
112
113 FGAIFlightPlan::waypoint*
114 FGAIFlightPlan::createInAir(FGAIAircraft *ac, const std::string& aName, const SGGeod& aPos, double aElev, double aSpeed)
115 {
116   waypoint* wpt = new waypoint;
117   wpt->name = aName;
118   wpt->longitude = aPos.getLongitudeDeg();
119   wpt->latitude = aPos.getLatitudeDeg();
120   wpt->altitude  = aElev;
121   wpt->speed     = aSpeed; 
122   wpt->crossat   = -10000;
123   wpt->gear_down = false;
124   wpt->flaps_down= false;
125   wpt->finished  = false;
126   wpt->on_ground = false;
127   wpt->routeIndex= 0;
128   return wpt;
129 }
130
131 FGAIFlightPlan::waypoint*
132 FGAIFlightPlan::cloneWithPos(FGAIAircraft *ac, waypoint* aWpt, const std::string& aName, const SGGeod& aPos)
133 {
134   waypoint* wpt = new waypoint;
135   wpt->name = aName;
136   wpt->longitude = aPos.getLongitudeDeg();
137   wpt->latitude = aPos.getLatitudeDeg();
138   
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;
146   wpt->routeIndex = 0;
147   
148   return wpt;
149 }
150
151 FGAIFlightPlan::waypoint*
152 FGAIFlightPlan::clone(waypoint* aWpt)
153 {
154   waypoint* wpt  = new waypoint;
155   wpt->name      = aWpt->name;
156   wpt->longitude = aWpt->longitude;
157   wpt->latitude  = aWpt->latitude;
158
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;
166   wpt->routeIndex = 0;
167   
168   return wpt;
169 }
170
171
172 void FGAIFlightPlan::createDefaultTakeoffTaxi(FGAIAircraft *ac, FGAirport* aAirport, FGRunway* aRunway)
173 {
174   SGGeod runwayTakeoff = aRunway->pointOnCenterline(5.0);
175   double airportElev = aAirport->getElevation();
176   
177   waypoint* wpt;
178   wpt = createOnGround(ac, "Airport Center", aAirport->geod(), airportElev, 15);
179   waypoints.push_back(wpt);
180   wpt = createOnGround(ac, "Runway Takeoff", runwayTakeoff, airportElev, 15);
181   waypoints.push_back(wpt);     
182 }
183
184 void FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft *ac, bool firstFlight, 
185                                 FGAirport *apt,
186                                 double radius, const string& fltType, 
187                                 const string& acType, const string& airline)
188 {
189   double heading, lat, lon;
190   
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.
194   if (firstFlight) {
195     if (!(apt->getDynamics()->getAvailableParking(&lat, &lon, 
196               &heading, &gateId, 
197               radius, fltType, 
198               acType, airline)))
199     {
200       SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " << 
201        acType <<
202        " of flight type " << fltType <<
203        " of airline     " << airline <<
204        " at airport     " << apt->getId());
205     }
206   }
207   
208   string rwyClass = getRunwayClassFromTrafficType(fltType);
209
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);
215   }
216   rwy = apt->getRunwayByIdent(activeRunway);
217   SGGeod runwayTakeoff = rwy->pointOnCenterline(5.0);
218
219   FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
220   if (!gn->exists()) {
221     createDefaultTakeoffTaxi(ac, apt, rwy);
222     return;
223   }
224   
225   intVec ids;
226   int runwayId = gn->findNearestNode(runwayTakeoff);
227
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.
232   delete taxiRoute;
233   taxiRoute = new FGTaxiRoute;
234
235   // Determine which node to start from.
236   int node = 0;
237   // Find out which node to start from
238   FGParking *park = apt->getDynamics()->getParking(gateId);
239   if (park) {
240     node = park->getPushBackPoint();
241   }
242   
243   if (node == -1) {
244     node = gateId;
245   }
246   
247   // HAndle case where parking doens't have a node
248   if ((node == 0) && park) {
249     if (firstFlight) {
250       node = gateId;
251     } else {
252       node = lastNodeVisited;
253     }
254   }
255
256   *taxiRoute = gn->findShortestRoute(node, runwayId);
257   intVecIterator i;
258          
259   if (taxiRoute->empty()) {
260     createDefaultTakeoffTaxi(ac, apt, rwy);
261     return;
262   }
263   
264   taxiRoute->first();
265   //bool isPushBackPoint = false;
266   if (firstFlight) {
267     // If this is called during initialization, randomly
268     // skip a number of waypoints to get a more realistic
269     // taxi situation.
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);
275     }
276     apt->getDynamics()->releaseParking(gateId);
277   } else {
278     if (taxiRoute->size() > 1) {
279       taxiRoute->next(&node); // chop off the first waypoint, because that is already the last of the pushback route
280     }
281   }
282   
283   // push each node on the taxi route as a waypoint
284   int route;
285   while(taxiRoute->next(&node, &route)) {
286                 char buffer[10];
287                 snprintf (buffer, 10, "%d", node);
288                 FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
289     waypoint* wpt = createOnGround(ac, buffer, tn->geod(), apt->getElevation(), 15);
290     wpt->routeIndex = route;
291                 waypoints.push_back(wpt);
292   }
293 }
294
295 void FGAIFlightPlan::createDefaultLandingTaxi(FGAIAircraft *ac, FGAirport* aAirport)
296 {
297   SGGeod lastWptPos = 
298     SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
299   double airportElev = aAirport->getElevation();
300   
301   waypoint* wpt;
302   wpt = createOnGround(ac, "Runway Exit", lastWptPos, airportElev, 15);
303   waypoints.push_back(wpt);     
304   wpt = createOnGround(ac, "Airport Center", aAirport->geod(), airportElev, 15);
305   waypoints.push_back(wpt);
306   
307   double heading, lat, lon;
308   aAirport->getDynamics()->getParking(gateId, &lat, &lon, &heading);
309   wpt = createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), airportElev, 15);
310   waypoints.push_back(wpt);
311 }
312
313 void FGAIFlightPlan::createLandingTaxi(FGAIAircraft *ac, FGAirport *apt,
314                                 double radius, const string& fltType, 
315                                 const string& acType, const string& airline)
316 {
317   double heading, lat, lon;
318   apt->getDynamics()->getAvailableParking(&lat, &lon, &heading, 
319         &gateId, radius, fltType, acType, airline);
320   
321   SGGeod lastWptPos = 
322     SGGeod::fromDeg(waypoints.back()->longitude, waypoints.back()->latitude);
323   FGGroundNetwork* gn = apt->getDynamics()->getGroundNetwork();
324   
325    // Find a route from runway end to parking/gate.
326   if (!gn->exists()) {
327     createDefaultLandingTaxi(ac, apt);
328     return;
329   }
330   
331         intVec ids;
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...
336   //FGTaxiRoute route;
337   delete taxiRoute;
338   taxiRoute = new FGTaxiRoute;
339   if (gateId >= 0)
340     *taxiRoute = gn->findShortestRoute(runwayId, gateId);
341   else
342     *taxiRoute = gn->findShortestRoute(runwayId, 0);
343   intVecIterator i;
344   
345   if (taxiRoute->empty()) {
346     createDefaultLandingTaxi(ac, apt);
347     return;
348   }
349   
350   int node;
351   taxiRoute->first();
352   int size = taxiRoute->size();
353   // Omit the last two waypoints, as 
354   // those are created by createParking()
355   int route;
356   for (int i = 0; i < size-2; i++) {
357     taxiRoute->next(&node, &route);
358     char buffer[10];
359     snprintf (buffer, 10, "%d", node);
360     FGTaxiNode *tn = gn->findNode(node);
361     waypoint* wpt = createOnGround(ac, buffer, tn->geod(), apt->getElevation(), 15);
362     wpt->routeIndex = route;
363     waypoints.push_back(wpt);
364   }
365 }
366
367 /*******************************************************************
368  * CreateTakeOff 
369  * initialize the Aircraft at the parking location
370  ******************************************************************/
371 void FGAIFlightPlan::createTakeOff(FGAIAircraft *ac, bool firstFlight, FGAirport *apt, double speed, const string &fltType)
372 {
373     double accel   = ac->getPerformance()->acceleration();
374     double vRotate = ac->getPerformance()->vRotate();
375     // Acceleration = dV / dT
376     // Acceleration X dT = dV
377     // dT = dT / Acceleration
378     //d = (Vf^2 - Vo^2) / (2*a)
379     double accelTime = (vRotate - 15) / accel;
380     //cerr << "Using " << accelTime << " as total acceleration time" << endl;
381     double accelDistance = (vRotate*vRotate - 15*15) / (2*accel);
382     //cerr << "Using " << accelDistance << " " << accel << " " << vRotate << endl;
383     waypoint *wpt;
384     // Get the current active runway, based on code from David Luff
385     // This should actually be unified and extended to include 
386     // Preferential runway use schema's 
387     // NOTE: DT (2009-01-18: IIRC, this is currently already the case, 
388     // because the getActive runway function takes care of that.
389     if (firstFlight)
390     {
391         string rwyClass = getRunwayClassFromTrafficType(fltType);
392         double heading = ac->getTrafficRef()->getCourse();
393         apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
394         rwy = apt->getRunwayByIdent(activeRunway);
395     }
396
397     double airportElev = apt->getElevation();
398     // Acceleration point, 105 meters into the runway,
399     SGGeod accelPoint = rwy->pointOnCenterline(105.0);
400     wpt = createOnGround(ac, "accel", accelPoint, airportElev, speed);
401     waypoints.push_back(wpt); 
402  
403     //Start Climbing to 3000 ft. Let's do this 
404     // at the center of the runway for now:
405     SGGeod rotate = rwy->pointOnCenterline(105.0+accelDistance);
406     wpt = cloneWithPos(ac, wpt, "SOC", rotate);
407     wpt->altitude  = airportElev+1000;
408     wpt->on_ground = false;
409     waypoints.push_back(wpt);
410
411     wpt = cloneWithPos(ac, wpt, "3000 ft", rwy->end());
412     wpt->altitude  = airportElev+3000;
413     waypoints.push_back(wpt);
414
415     // Finally, add two more waypoints, so that aircraft will remain under
416     // Tower control until they have reached the 3000 ft climb point
417     SGGeod pt = rwy->pointOnCenterline(5000 + rwy->lengthM() * 0.5);
418     wpt = cloneWithPos(ac, wpt, "5000 ft", pt);
419     wpt->altitude  = airportElev+5000;
420     waypoints.push_back(wpt);
421 }
422   
423 /*******************************************************************
424  * CreateClimb
425  * initialize the Aircraft at the parking location
426  ******************************************************************/
427 void FGAIFlightPlan::createClimb(FGAIAircraft *ac, bool firstFlight, FGAirport *apt, double speed, double alt, const string &fltType)
428 {
429   waypoint *wpt;
430   bool planLoaded = false;
431   string fPLName;
432
433   if (firstFlight) {
434     string rwyClass = getRunwayClassFromTrafficType(fltType);
435     double heading = ac->getTrafficRef()->getCourse();
436     apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
437     rwy = apt->getRunwayByIdent(activeRunway);
438   }
439   if (sid) {
440     for (wpt_vector_iterator i = sid->getFirstWayPoint(); 
441          i != sid->getLastWayPoint(); 
442          i++) {
443             waypoints.push_back(clone(*(i)));
444             //cerr << " Cloning waypoint " << endl;
445     }
446   } else  {
447       SGGeod climb1 = rwy->pointOnCenterline(10*SG_NM_TO_METER);
448       wpt = createInAir(ac, "10000ft climb", climb1, speed, 10000);
449       wpt->gear_down = true;
450       wpt->flaps_down= true;
451       waypoints.push_back(wpt); 
452
453       SGGeod climb2 = rwy->pointOnCenterline(20*SG_NM_TO_METER);
454       wpt = cloneWithPos(ac, wpt, "18000ft climb", climb2);
455       wpt->altitude  = 18000;
456       waypoints.push_back(wpt); 
457    }
458 }
459
460
461
462 /*******************************************************************
463  * CreateDecent
464  * initialize the Aircraft at the parking location
465  ******************************************************************/
466 void FGAIFlightPlan::createDecent(FGAIAircraft *ac, FGAirport *apt, const string &fltType)
467 {
468   // Ten thousand ft. Slowing down to 240 kts
469   waypoint *wpt;
470
471   //Beginning of Decent
472   //string name;
473   // allow "mil" and "gen" as well
474   string rwyClass = getRunwayClassFromTrafficType(fltType);
475   double heading = ac->getTrafficRef()->getCourse();
476   apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading);
477   rwy = apt->getRunwayByIdent(activeRunway);
478      
479   SGGeod descent1 = rwy->pointOnCenterline(-100000); // 100km out
480   wpt = createInAir(ac, "Dec 10000ft", descent1, apt->getElevation(), 240);
481   wpt->crossat   = 10000;
482   waypoints.push_back(wpt);  
483   
484   // Three thousand ft. Slowing down to 160 kts
485   SGGeod descent2 = rwy->pointOnCenterline(-8*SG_NM_TO_METER); // 8nm out
486   wpt = createInAir(ac, "DEC 3000ft", descent2, apt->getElevation(), 160);
487   wpt->crossat   = 3000;
488   wpt->gear_down = true;
489   wpt->flaps_down= true;
490   waypoints.push_back(wpt);
491 }
492 /*******************************************************************
493  * CreateLanding
494  * initialize the Aircraft at the parking location
495  ******************************************************************/
496 void FGAIFlightPlan::createLanding(FGAIAircraft *ac, FGAirport *apt)
497 {
498   // Ten thousand ft. Slowing down to 150 kts
499   waypoint *wpt;
500   double aptElev = apt->getElevation();
501   //Runway Threshold
502   wpt = createOnGround(ac, "Threshold", rwy->threshold(), aptElev, 150);
503   wpt->crossat = apt->getElevation();
504   waypoints.push_back(wpt); 
505
506  // Roll-out
507   wpt = createOnGround(ac, "Center", rwy->geod(), aptElev, 30);
508   waypoints.push_back(wpt);
509
510   SGGeod rollOut = rwy->pointOnCenterline(rwy->lengthM() * 0.9);
511   wpt = createOnGround(ac, "Roll Out", rollOut, aptElev, 15);
512   wpt->crossat   = apt->getElevation();
513   waypoints.push_back(wpt); 
514 }
515
516 /*******************************************************************
517  * CreateParking
518  * initialize the Aircraft at the parking location
519  ******************************************************************/
520 void FGAIFlightPlan::createParking(FGAIAircraft *ac, FGAirport *apt, double radius)
521 {
522   waypoint* wpt;
523   double aptElev = apt->getElevation();
524   double lat, lat2;
525   double lon, lon2;
526   double az2;
527   double heading;
528   apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
529   heading += 180.0;
530   if (heading > 360)
531     heading -= 360; 
532   geo_direct_wgs_84 ( 0, lat, lon, heading, 
533                       2.2*radius,           
534                       &lat2, &lon2, &az2 );
535   wpt = createOnGround(ac, "taxiStart", SGGeod::fromDeg(lon2, lat2), aptElev, 10);
536   waypoints.push_back(wpt);
537   
538   geo_direct_wgs_84 ( 0, lat, lon, heading, 
539                       0.1 *radius,           
540                       &lat2, &lon2, &az2 );
541           
542   wpt = createOnGround(ac, "taxiStart2", SGGeod::fromDeg(lon2, lat2), aptElev, 10);
543   waypoints.push_back(wpt);   
544
545   wpt = createOnGround(ac, "END", SGGeod::fromDeg(lon, lat), aptElev, 10);
546   waypoints.push_back(wpt);
547 }
548
549 /**
550  *
551  * @param fltType a string describing the type of
552  * traffic, normally used for gate assignments
553  * @return a converted string that gives the runway
554  * preference schedule to be used at aircraft having
555  * a preferential runway schedule implemented (i.e.
556  * having a rwyprefs.xml file
557  * 
558  * Currently valid traffic types for gate assignment:
559  * - gate (commercial gate)
560  * - cargo (commercial gargo),
561  * - ga (general aviation) ,
562  * - ul (ultralight),
563  * - mil-fighter (military - fighter),
564  * - mil-transport (military - transport)
565  *
566  * Valid runway classes:
567  * - com (commercial traffic: jetliners, passenger and cargo)
568  * - gen (general aviation)
569  * - ul (ultralight: I can imagine that these may share a runway with ga on some airports)
570  * - mil (all military traffic)
571  */
572 string FGAIFlightPlan::getRunwayClassFromTrafficType(string fltType)
573 {
574     if ((fltType == "gate") || (fltType == "cargo")) { 
575         return string("com");
576     }
577     if (fltType == "ga") {
578         return string ("gen");
579     }
580     if (fltType == "ul") {
581         return string("ul");
582     }
583     if ((fltType == "mil-fighter") || (fltType == "mil-transport")) { 
584         return string("mil");
585     }
586    return string("com");
587 }