Debian Codegraph @

src/AIModel/AIFlightPlanCreate.cxx

raw symbols SCIP
1 /*
2 * SPDX-FileName: AIFlightPlanCreate.cxx
3 * SPDX-FileCopyrightText: Written by Durk Talsma, started May, 2004
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <algorithm>
12
13 #include <cstdio>
14 #include <cstdlib>
15
16 #include <simgear/math/sg_geodesy.hxx>
17 #include <simgear/props/props.hxx>
18 #include <simgear/props/props_io.hxx>
19 #include <simgear/timing/sg_time.hxx>
20
21 #include <Airports/airport.hxx>
22 #include <Airports/dynamics.hxx>
23 #include <Airports/groundnetwork.hxx>
24 #include <Airports/runways.hxx>
25
26 #include <Environment/environment.hxx>
27 #include <Environment/environment_mgr.hxx>
28 #include <FDM/LaRCsim/basic_aero.h>
29 #include <Main/fg_props.hxx>
30 #include <Navaids/navrecord.hxx>
31 #include <Traffic/Schedule.hxx>
32
33 #include "AIAircraft.hxx"
34 #include "AIFlightPlan.hxx"
35 #include "VectorMath.hxx"
36 #include "performancedata.hxx"
37
38 using std::string;
39
40
41 /* FGAIFlightPlan::create()
42 * dynamically create a flight plan for AI traffic, based on data provided by the
43 * Traffic Manager, when reading a filed flightplan fails. (DT, 2004/07/10)
44 *
45 * This is the top-level function, and the only one that is publicly available.
46 *
47 */
48
49
50 // Check lat/lon values during initialization;
51 bool FGAIFlightPlan::create(FGAIAircraft* ac, FGAirport* dep,
52 FGAirport* arr, int legNr, double alt,
53 double speed, double latitude,
54 double longitude, bool firstFlight,
55 double radius, const string& fltType,
56 const string& aircraftType,
57 const string& airline, double distance)
58 {
59 if (legNr <= AILeg::TAKEOFF)
60 SG_LOG(SG_AI, SG_BULK, "Create Leg " << legNr << " " << (firstFlight ? "First" : "") << " Old Leg " << getLeg() << " At Airport : " << dep->getId());
61 else if (legNr <= AILeg::APPROACH)
62 SG_LOG(SG_AI, SG_BULK, "Create Leg " << legNr << " " << (firstFlight ? "First" : "") << " Old Leg " << getLeg() << " Departure Airport : " << dep->getId() << " Arrival Airport : " << arr->getId());
63 else
64 SG_LOG(SG_AI, SG_BULK, "Create Leg " << legNr << " " << (firstFlight ? "First" : "") << " Old Leg " << getLeg() << " At Airport : " << arr->getId());
65
66 bool retVal = true;
67 int currWpt = wpt_iterator - waypoints.begin();
68 switch (legNr) {
69 case AILeg::STARTUP_PUSHBACK:
70 retVal = createPushBack(ac, firstFlight, dep,
71 radius, fltType, aircraftType, airline);
72 break;
73 case AILeg::TAXI:
74 retVal = createTakeoffTaxi(ac, firstFlight, dep, radius, fltType,
75 aircraftType, airline);
76 break;
77 case AILeg::TAKEOFF:
78 retVal = createTakeOff(ac, firstFlight, dep, SGGeod::fromDeg(longitude, latitude), speed, fltType);
79 break;
80 case AILeg::CLIMB:
81 retVal = createClimb(ac, firstFlight, dep, arr, speed, alt, fltType);
82 break;
83 case AILeg::CRUISE:
84 retVal = createCruise(ac, firstFlight, dep, arr, SGGeod::fromDeg(longitude, latitude), speed,
85 alt, fltType);
86 break;
87 case AILeg::APPROACH:
88 retVal = createDescent(ac, arr, SGGeod::fromDeg(longitude, latitude), speed, alt, fltType,
89 distance);
90 break;
91 case AILeg::HOLD:
92 retVal = createHold(ac, arr, SGGeod::fromDeg(longitude, latitude), speed, alt, fltType,
93 distance);
94 break;
95 case AILeg::LANDING:
96 retVal = createLanding(ac, arr, fltType);
97 break;
98 case AILeg::PARKING_TAXI:
99 retVal = createLandingTaxi(ac, arr, radius, fltType, aircraftType, airline);
100 break;
101 case AILeg::PARKING:
102 retVal = createParking(ac, arr, radius);
103 break;
104 default:
105 //exit(1);
106 SG_LOG(SG_AI, SG_ALERT,
107 "AIFlightPlan::create() attempting to create unknown leg"
108 " this is probably an internal program error");
109 break;
110 }
111 //don't increment leg right away, but only once we pass the actual last waypoint that was created.
112 // to do so, mark the last waypoint with a special status flag
113 if (retVal) {
114 if (waypoints.empty()) {
115 SG_LOG(SG_AI, SG_WARN, ac->getCallSign() << "|AIFlightPlan::create() Leg " << legNr << " created empty waypoints.");
116 return retVal;
117 }
118 wpt_iterator = waypoints.begin() + currWpt;
119 if (waypoints.back()->getName().size() == 0) {
120 SG_LOG(SG_AI, SG_WARN, ac->getCallSign() << " Empty wpt name");
121 }
122 waypoints.back()->setName(waypoints.back()->getName() + string("legend"));
123 // "It's pronounced Leg-end" (Roger Glover (Deep Purple): come Hell or High Water DvD, 1993)
124 }
125 return retVal;
126 }
127
128 FGAIWaypoint* FGAIFlightPlan::createOnGround(FGAIAircraft* ac,
129 const std::string& aName,
130 const SGGeod& aPos, double aElev,
131 double aSpeed)
132 {
133 FGAIWaypoint* wpt = new FGAIWaypoint;
134 wpt->setName(aName);
135 wpt->setLongitude(aPos.getLongitudeDeg());
136 wpt->setLatitude(aPos.getLatitudeDeg());
137 wpt->setAltitude(aElev);
138 wpt->setSpeed(aSpeed);
139 wpt->setCrossat(-10000.1);
140 wpt->setGear_down(true);
141 wpt->setFlaps(0.0f);
142 wpt->setSpoilers(0.0f);
143 wpt->setSpeedBrakes(0.0f);
144 wpt->setFinished(false);
145 wpt->setOn_ground(true);
146 wpt->setRouteIndex(0);
147
148 if (aSpeed > 0.0f) {
149 wpt->setGroundLights();
150 } else {
151 wpt->setPowerDownLights();
152 }
153
154 return wpt;
155 }
156
157 FGAIWaypoint* FGAIFlightPlan::createOnRunway(FGAIAircraft* ac,
158 const std::string& aName,
159 const SGGeod& aPos, double aElev,
160 double aSpeed)
161 {
162 FGAIWaypoint* wpt = createOnGround(ac, aName, aPos, aElev, aSpeed);
163 wpt->setTakeOffLights();
164 return wpt;
165 }
166
167 void FGAIFlightPlan::createArc(FGAIAircraft* ac, const SGGeod& center, int startAngle,
168 int endAngle, int increment, int radius, double aElev, double altDiff, double aSpeed, const char* pattern)
169 {
170 double trackSegmentLength = (2 * M_PI * radius) / 360.0;
171 double dummyAz2;
172 char buffer[20];
173
174 if (endAngle > startAngle && increment < 0) {
175 endAngle -= 360;
176 }
177 if (endAngle < startAngle && increment > 0) {
178 endAngle += 360;
179 }
180
181 int nPoints = fabs(fabs(endAngle - startAngle) / increment);
182 double currentAltitude = aElev;
183 double altDecrement = altDiff / nPoints;
184
185 for (int i = startAngle; !(endAngle <= i && i < endAngle + fabs(increment)); i += increment) {
186 if (fabs(i) > 720) {
187 SG_LOG(SG_AI, SG_WARN, "FGAIFlightPlan::createArc runaway " << startAngle << " " << endAngle << " " << increment);
188 break;
189 }
190 SGGeod result;
191 SGGeodesy::direct(center, i,
192 radius, result, dummyAz2);
193 snprintf(buffer, sizeof(buffer), pattern, i);
194 currentAltitude -= altDecrement;
195 FGAIWaypoint* wpt = createInAir(ac, buffer, result, currentAltitude, aSpeed);
196 wpt->setCrossat(currentAltitude);
197 wpt->setTrackLength(trackSegmentLength);
198 pushBackWaypoint(wpt);
199 }
200 }
201
202 void FGAIFlightPlan::createLine(FGAIAircraft* ac, const SGGeod& startPoint, double azimuth, double dist, double aElev, double dAlt, double vDescent, const char* pattern)
203 {
204 double nPoints = dist / (vDescent * 2);
205 char buffer[20];
206 double distIncrement = (dist / nPoints);
207
208 for (int i = 1; i < nPoints; i++) {
209 double currentDist = i * distIncrement;
210 double currentAltitude = aElev - (i * (dAlt / nPoints));
211 SGGeod result = SGGeodesy::direct(startPoint, azimuth, currentDist);
212 snprintf(buffer, sizeof(buffer), pattern, i);
213 FGAIWaypoint* wpt = createInAir(ac, buffer, result, currentAltitude, vDescent);
214 wpt->setCrossat(currentAltitude);
215 wpt->setTrackLength((dist / nPoints));
216 pushBackWaypoint(wpt);
217 }
218 }
219
220 FGAIWaypoint* FGAIFlightPlan::createInAir(FGAIAircraft* ac,
221 const std::string& aName,
222 const SGGeod& aPos, double aElev,
223 double aSpeed)
224 {
225 FGAIWaypoint* wpt = createOnGround(ac, aName, aPos, aElev, aSpeed);
226 wpt->setGear_down(false);
227 wpt->setFlaps(0.0f);
228 wpt->setSpoilers(0.0f);
229 wpt->setSpeedBrakes(0.0f);
230 wpt->setOn_ground(false);
231 wpt->setCrossat(aElev);
232
233 if (aPos.getElevationFt() < 10000.0f) {
234 wpt->setApproachLights();
235 } else {
236 wpt->setCruiseLights();
237 }
238 return wpt;
239 }
240
241 FGAIWaypoint* FGAIFlightPlan::clone(FGAIWaypoint* aWpt)
242 {
243 FGAIWaypoint* wpt = new FGAIWaypoint;
244 wpt->setName(aWpt->getName());
245 wpt->setLongitude(aWpt->getLongitude());
246 wpt->setLatitude(aWpt->getLatitude());
247 wpt->setAltitude(aWpt->getAltitude());
248 wpt->setSpeed(aWpt->getSpeed());
249 wpt->setCrossat(aWpt->getCrossat());
250 wpt->setGear_down(aWpt->getGear_down());
251 wpt->setFlaps(aWpt->getFlaps());
252 wpt->setFinished(aWpt->isFinished());
253 wpt->setOn_ground(aWpt->getOn_ground());
254 wpt->setLandingLight(aWpt->getLandingLight());
255 wpt->setNavLight(aWpt->getNavLight());
256 wpt->setStrobeLight(aWpt->getStrobeLight());
257 wpt->setTaxiLight(aWpt->getTaxiLight());
258 wpt->setRouteIndex(0);
259
260 return wpt;
261 }
262
263
264 FGAIWaypoint* FGAIFlightPlan::cloneWithPos(FGAIAircraft* ac, FGAIWaypoint* aWpt,
265 const std::string& aName,
266 const SGGeod& aPos)
267 {
268 FGAIWaypoint* wpt = clone(aWpt);
269 wpt->setName(aName);
270 wpt->setLongitude(aPos.getLongitudeDeg());
271 wpt->setLatitude(aPos.getLatitudeDeg());
272
273 return wpt;
274 }
275
276
277 void FGAIFlightPlan::createDefaultTakeoffTaxi(FGAIAircraft* ac,
278 FGAirport* aAirport,
279 FGRunway* aRunway)
280 {
281 SGGeod runwayTakeoff = aRunway->pointOnCenterline(5.0);
282 double airportElev = aAirport->getElevation();
283
284 FGAIWaypoint* wpt;
285 wpt =
286 createOnGround(ac, "Airport Center", aAirport->geod(), airportElev,
287 ac->getPerformance()->vTaxi());
288 pushBackWaypoint(wpt);
289 wpt =
290 createOnRunway(ac, "Runway Takeoff", runwayTakeoff, airportElev,
291 ac->getPerformance()->vTaxi());
292 wpt->setFlaps(0.5f);
293 pushBackWaypoint(wpt);
294
295
296 // Acceleration point, 105 meters into the runway,
297 SGGeod accelPoint = aRunway->pointOnCenterline(105.0);
298 wpt = createOnRunway(ac, "Accel", accelPoint, airportElev,
299 ac->getPerformance()->vRotate());
300 pushBackWaypoint(wpt);
301 }
302
303 bool FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft* ac, bool firstFlight,
304 FGAirport* apt,
305 double radius,
306 const string& fltType,
307 const string& acType,
308 const string& airline)
309 {
310 int route;
311 // If this function is called during initialization,
312 // make sure we obtain a valid gate ID first
313 // and place the model at the location of the gate.
314 if (firstFlight && apt->getDynamics()->hasParkings()) {
315 gate = apt->getDynamics()->getAvailableParking(radius, fltType,
316 acType, airline);
317 if (!gate.isValid()) {
318 SG_LOG(SG_AI, SG_WARN, "Could not find parking for a " << acType << " of flight type " << fltType << " of airline " << airline << " at airport " << apt->getId());
319 }
320 }
321
322 const string& rwyClass = getRunwayClassFromTrafficType(fltType);
323
324 // Only set this if it hasn't been set by ATC already.
325 if (activeRunway.empty()) {
326 // cerr << "Getting runway for " << ac->getTrafficRef()->getCallSign() << " at " << apt->getId() << endl;
327 double depHeading = ac->getTrafficRef()->getCourse();
328 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
329 depHeading);
330 }
331 FGRunway* rwy = apt->getRunwayByIdent(activeRunway);
332 SG_LOG(SG_AI, SG_BULK, "Taxi to " << apt->getId() << "/" << activeRunway);
333 assert(rwy != NULL);
334 SGGeod runwayTakeoff = rwy->pointOnCenterlineDisplaced(5.0);
335
336 FGGroundNetwork* gn = apt->groundNetwork();
337 if (!gn->exists()) {
338 SG_LOG(SG_AI, SG_DEBUG, "No groundnet " << apt->getId() << " creating default taxi.");
339 createDefaultTakeoffTaxi(ac, apt, rwy);
340 return true;
341 }
342
343 FGTaxiNodeRef runwayNode;
344 if (gn->getVersion() > 0) {
345 runwayNode = gn->findNearestNodeOnRunwayEntry(runwayTakeoff);
346 } else {
347 runwayNode = gn->findNearestNode(runwayTakeoff);
348 }
349
350 // A negative gateId indicates an overflow parking, use a
351 // fallback mechanism for this.
352 // Starting from gate 0 in this case is a bit of a hack
353 // which requires a more proper solution later on.
354 // delete taxiRoute;
355 // taxiRoute = new FGTaxiRoute;
356
357 // Determine which node to start from.
358 FGTaxiNodeRef node;
359 // Find out which node to start from
360 FGParking* park = gate.parking();
361 if (park) {
362 node = park->getPushBackPoint();
363 if (node == 0) {
364 // Handle case where parking doesn't have a node
365 if (firstFlight) {
366 node = park;
367 } else if (lastNodeVisited) {
368 node = lastNodeVisited;
369 } else {
370 SG_LOG(SG_AI, SG_WARN, "Taxiroute could not be constructed no lastNodeVisited at " << (apt ? apt->getId() : "????") << gate.isValid());
371 }
372 }
373 } else {
374 SG_LOG(SG_AI, SG_WARN, "Taxiroute could not be constructed no parking." << (apt ? apt->getId() : "????"));
375 }
376
377 FGTaxiRoute taxiRoute;
378 if (runwayNode && node) {
379 taxiRoute = gn->findShortestRoute(node, runwayNode);
380 } else {
381 }
382
383 // This may happen with buggy ground networks
384 if (taxiRoute.size() <= 1) {
385 SG_LOG(SG_AI, SG_DEBUG, "Taxiroute too short " << apt->getId() << "creating default taxi.");
386 createDefaultTakeoffTaxi(ac, apt, rwy);
387 return true;
388 }
389
390 taxiRoute.first();
391 FGTaxiNodeRef skipNode;
392
393 //bool isPushBackPoint = false;
394 if (firstFlight) {
395 // If this is called during initialization, randomly
396 // skip a number of waypoints to get a more realistic
397 // taxi situation.
398 int nrWaypointsToSkip = rand() % taxiRoute.size();
399 // but make sure we always keep two active waypoints
400 // to prevent a segmentation fault
401 for (int i = 0; i < nrWaypointsToSkip - 3; i++) {
402 taxiRoute.next(skipNode, &route);
403 }
404
405 gate.release(); // free up our gate as required
406 } else {
407 if (taxiRoute.size() > 1) {
408 taxiRoute.next(skipNode, &route); // chop off the first waypoint, because that is already the last of the pushback route
409 }
410 }
411
412 // push each node on the taxi route as a waypoint
413
414 //cerr << "Building taxi route" << endl;
415
416 // Note that the line wpt->setRouteIndex was commented out by revision [afcdbd] 2012-01-01,
417 // which breaks the rendering functions.
418 // These can probably be generated on the fly however.
419 while (taxiRoute.next(node, &route)) {
420 char buffer[10];
421 snprintf(buffer, sizeof(buffer), "%d", node->getIndex());
422 FGAIWaypoint* wpt =
423 createOnGround(ac, buffer, node->geod(), apt->getElevation(),
424 ac->getPerformance()->vTaxi());
425 wpt->setRouteIndex(route);
426 //cerr << "Nodes left " << taxiRoute->nodesLeft() << " ";
427 if (taxiRoute.nodesLeft() == 1) {
428 // Note that we actually have hold points in the ground network, but this is just an initial test.
429 //cerr << "Setting departurehold point: " << endl;
430 wpt->setName(wpt->getName() + string("_DepartureHold"));
431 wpt->setFlaps(0.5f);
432 wpt->setTakeOffLights();
433 }
434 if (taxiRoute.nodesLeft() == 0) {
435 wpt->setName(wpt->getName() + string("_Accel"));
436 wpt->setTakeOffLights();
437 wpt->setFlaps(0.5f);
438 }
439 pushBackWaypoint(wpt);
440 }
441 double accell_point = 105.0;
442 // Acceleration point, 105 meters into the runway,
443 SGGeod entryPoint = waypoints.back()->getPos();
444 SGGeod runwayEnd = rwy->pointOnCenterlineDisplaced(0);
445 double distM = SGGeodesy::distanceM(entryPoint, runwayEnd);
446 if (distM > accell_point) {
447 SG_LOG(SG_AI, SG_BULK, "Distance down runway " << distM << " " << accell_point);
448 accell_point += distM;
449 }
450 SGGeod accelPoint = rwy->pointOnCenterlineDisplaced(accell_point);
451 FGAIWaypoint* wpt = createOnRunway(ac, "Accel", accelPoint, apt->getElevation(), ac->getPerformance()->vRotate());
452 wpt->setFlaps(0.5f);
453 pushBackWaypoint(wpt);
454
455 //cerr << "[done]" << endl;
456 return true;
457 }
458
459 void FGAIFlightPlan::createDefaultLandingTaxi(FGAIAircraft* ac,
460 FGAirport* aAirport)
461 {
462 SGGeod lastWptPos =
463 SGGeod::fromDeg(waypoints.back()->getLongitude(),
464 waypoints.back()->getLatitude());
465 double airportElev = aAirport->getElevation();
466
467 FGAIWaypoint* wpt;
468 wpt =
469 createOnGround(ac, "Runway Exit", lastWptPos, airportElev,
470 ac->getPerformance()->vTaxi());
471 pushBackWaypoint(wpt);
472 wpt =
473 createOnGround(ac, "Airport Center", aAirport->geod(), airportElev,
474 ac->getPerformance()->vTaxi());
475 pushBackWaypoint(wpt);
476
477 if (gate.isValid()) {
478 wpt = createOnGround(ac, "END-taxi", gate.parking()->geod(), airportElev,
479 ac->getPerformance()->vTaxi());
480 pushBackWaypoint(wpt);
481 }
482 }
483
484 bool FGAIFlightPlan::createLandingTaxi(FGAIAircraft* ac,
485 FGAirport* apt,
486 double radius,
487 const string& fltType,
488 const string& acType,
489 const string& airline)
490 {
491 int route;
492 gate = apt->getDynamics()->getAvailableParking(radius, fltType,
493 acType, airline);
494 SGGeod lastWptPos = waypoints.back()->getPos();
495 FGGroundNetwork* gn = apt->groundNetwork();
496
497 // Find a route from runway end to parking/gate.
498 if (!gn->exists()) {
499 createDefaultLandingTaxi(ac, apt);
500 return true;
501 }
502
503 FGRunway* rwy = apt->getRunwayByIdent(activeRunway);
504 FGTaxiNodeRef runwayNode;
505 if (gn->getVersion() == 1) {
506 runwayNode = gn->findNearestNodeOnRunwayExit(lastWptPos, rwy);
507 } else {
508 runwayNode = gn->findNearestNode(lastWptPos);
509 }
510 // cerr << "Using network node " << runwayId << endl;
511 // A negative gateId indicates an overflow parking, use a
512 // fallback mechanism for this.
513 // Starting from gate 0 doesn't work, so don't try it
514 FGTaxiRoute taxiRoute;
515 if (runwayNode && gate.isValid()) {
516 taxiRoute = gn->findShortestRoute(runwayNode, gate.parking());
517 }
518
519 if (taxiRoute.empty()) {
520 createDefaultLandingTaxi(ac, apt);
521 return true;
522 }
523
524 FGTaxiNodeRef node;
525 taxiRoute.first();
526 int size = taxiRoute.size();
527 for (int i = 0; i < size; i++) {
528 taxiRoute.next(node, &route);
529 char buffer[32];
530 snprintf(buffer, sizeof(buffer), "landingtaxi-%d-%d", node->getIndex(), i);
531 FGAIWaypoint* wpt =
532 createOnGround(ac, buffer, node->geod(), apt->getElevation(),
533 ac->getPerformance()->vTaxi());
534
535 wpt->setRouteIndex(route);
536 // next WPT must be far enough from last and far enough from parking
537 if ((!waypoints.back() ||
538 SGGeodesy::distanceM(waypoints.back()->getPos(), wpt->getPos()) > 1) &&
539 SGGeodesy::distanceM(gate.parking()->geod(), wpt->getPos()) > 20) {
540 if (waypoints.back()) {
541 int dist = SGGeodesy::distanceM(waypoints.back()->getPos(), wpt->getPos());
542 // pretty near better set speed down
543 if (dist < 10 || (size - i) < 2) {
544 wpt->setSpeed(ac->getPerformance()->vTaxi() / 2);
545 }
546 }
547 pushBackWaypoint(wpt);
548 }
549 }
550 SG_LOG(SG_AI, SG_BULK, "Created taxi from " << runwayNode->getIndex() << " to " << gate.parking()->ident() << " at " << apt->getId());
551
552 return true;
553 }
554
555 static double accelDistance(double v0, double v1, double accel)
556 {
557 double t = fabs(v1 - v0) / accel; // time in seconds to change velocity
558 // area under the v/t graph: (t * v0) + (dV / 2t) where (dV = v1 - v0)
559 SG_LOG(SG_AI, SG_BULK, "Brakingtime " << t);
560 return t * 0.5 * (v1 + v0);
561 }
562
563 // find the horizontal distance to gain the specific altitude, holding
564 // a constant pitch angle. Used to compute distance based on standard FD/AP
565 // PITCH mode prior to VS or CLIMB engaging. Visually, we want to avoid
566 // a dip in the nose angle after rotation, during initial climb-out.
567 static double pitchDistance(double pitchAngleDeg, double altGainM)
568 {
569 return altGainM / tan(pitchAngleDeg * SG_DEGREES_TO_RADIANS);
570 }
571
572 /*******************************************************************
573 * CreateTakeOff
574 * A note on units:
575 * - Speed -> knots -> nm/hour
576 * - distance along runway =-> meters
577 * - accel / decel -> is given as knots/hour, but this is highly questionable:
578 * for a jet_transport performance class, a accel / decel rate of 5 / 2 is
579 * given respectively. According to performance data.cxx, a value of kts / second seems
580 * more likely however.
581 *
582 ******************************************************************/
583 bool FGAIFlightPlan::createTakeOff(FGAIAircraft* ac,
584 bool firstFlight,
585 FGAirport* apt,
586 const SGGeod& pos,
587 double speed,
588 const string& fltType)
589 {
590 SG_LOG(SG_AI, SG_BULK, "createTakeOff " << apt->getId() << "/" << activeRunway);
591 double accell_point = 105.0;
592 // climb-out angle in degrees. could move this to the perf-db but this
593 // value is pretty sane
594 const double INITIAL_PITCH_ANGLE = 10.0;
595
596 double accel = ac->getPerformance()->acceleration();
597 double vTaxi = ac->getPerformance()->vTaxi();
598 double vRotate = ac->getPerformance()->vRotate();
599 double vTakeoff = ac->getPerformance()->vTakeoff();
600
601 double accelMetric = accel * SG_KT_TO_MPS;
602 double vTaxiMetric = vTaxi * SG_KT_TO_MPS;
603 double vRotateMetric = vRotate * SG_KT_TO_MPS;
604 double vTakeoffMetric = vTakeoff * SG_KT_TO_MPS;
605
606 FGAIWaypoint* wpt;
607 // Get the current active runway, based on code from David Luff
608 // This should actually be unified and extended to include
609 // Preferential runway use schema's
610 // NOTE: DT (2009-01-18: IIRC, this is currently already the case,
611 // because the getActive runway function takes care of that.
612 if (firstFlight) {
613 const string& rwyClass = getRunwayClassFromTrafficType(fltType);
614 double dHeading = ac->getTrafficRef()->getCourse();
615 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, dHeading);
616 }
617
618 // this is Sentry issue FLIGHTGEAR-DS : happens after reposition,
619 // likely firstFlight is false, but activeRunway is stale
620 if (!apt->hasRunwayWithIdent(activeRunway)) {
621 SG_LOG(SG_AI, SG_WARN, "FGAIFlightPlan::createTakeOff: invalid active runway:" << activeRunway);
622 return false;
623 }
624 SG_LOG(SG_AI, SG_BULK, "Takeoff from airport " << apt->getId() << "/" << activeRunway);
625
626 FGRunway* rwy = apt->getRunwayByIdent(activeRunway);
627 if (!rwy)
628 return false;
629
630 double airportElev = apt->getElevation();
631
632 if (pos.isValid()) {
633 SGGeod runwayEnd = rwy->pointOnCenterlineDisplaced(0);
634 double distM = SGGeodesy::distanceM(pos, runwayEnd);
635 if (distM > accell_point) {
636 SG_LOG(SG_AI, SG_BULK, "Distance down runway " << distM << " " << accell_point);
637 accell_point += distM;
638 }
639 }
640
641 // distance from the runway threshold to accelerate to rotation speed.
642 double d = accelDistance(vTaxiMetric, vRotateMetric, accelMetric) + accell_point;
643 SGGeod rotatePoint = rwy->pointOnCenterlineDisplaced(d);
644 wpt = createOnRunway(ac, "rotate", rotatePoint, airportElev, vRotate);
645 wpt->setFlaps(0.5f);
646 pushBackWaypoint(wpt);
647
648 // After rotation, we still need to accelerate to the take-off speed.
649 double t = d + accelDistance(vRotateMetric, vTakeoffMetric, accelMetric);
650 SGGeod takeoffPoint = rwy->pointOnCenterlineDisplaced(t);
651 wpt = createOnRunway(ac, "takeoff", takeoffPoint, airportElev, vTakeoff);
652 wpt->setGear_down(true);
653 wpt->setFlaps(0.5f);
654 pushBackWaypoint(wpt);
655
656 double vRef = vTakeoff + 20; // climb-out at v2 + 20kts
657
658 // We want gear-up to take place at ~400ft AGL. However, the flightplan
659 // will move onto the next leg once it gets within 2x-speed of the next waypoint.
660 // With closely spaced waypoints on climb-out this can occur almost immediately,
661 // so we put the waypoint further away.
662 double gearUpDist = t + 2 * vRef * SG_FEET_TO_METER + pitchDistance(INITIAL_PITCH_ANGLE, 400 * SG_FEET_TO_METER);
663 SGGeod gearUpPoint = rwy->pointOnCenterlineDisplaced(gearUpDist);
664 wpt = createInAir(ac, "gear-up", gearUpPoint, airportElev + 400, vRef);
665 wpt->setFlaps(0.5f);
666 pushBackWaypoint(wpt);
667
668 // limit climbout speed to 240kts below 10000'
669 double vClimbBelow10000 = std::min(240.0, ac->getPerformance()->vClimb());
670
671 // create two climb-out points. This is important because the first climb point will
672 // be a (sometimes large) turn towards the destination, and we don't want to
673 // commence that turn below 2000'
674 double climbOut = t + 2 * vClimbBelow10000 * SG_FEET_TO_METER + pitchDistance(INITIAL_PITCH_ANGLE, 2000 * SG_FEET_TO_METER);
675 SGGeod climbOutPoint = rwy->pointOnCenterlineDisplaced(climbOut);
676 wpt = createInAir(ac, "2000'", climbOutPoint, airportElev + 2000, vClimbBelow10000);
677 pushBackWaypoint(wpt);
678
679 climbOut = t + 2 * vClimbBelow10000 * SG_FEET_TO_METER + pitchDistance(INITIAL_PITCH_ANGLE, 2500 * SG_FEET_TO_METER);
680 SGGeod climbOutPoint2 = rwy->pointOnCenterlineDisplaced(climbOut);
681 wpt = createInAir(ac, "2500'", climbOutPoint2, airportElev + 2500, vClimbBelow10000);
682 pushBackWaypoint(wpt);
683
684 return true;
685 }
686
687 /*******************************************************************
688 * CreateClimb
689 * initialize the Aircraft in a climb.
690 ******************************************************************/
691 bool FGAIFlightPlan::createClimb(FGAIAircraft* ac, bool firstFlight,
692 FGAirport* apt, FGAirport* arrival,
693 double speed, double alt,
694 const string& fltType)
695 {
696 double vClimb = ac->getPerformance()->vClimb();
697
698 if (firstFlight) {
699 const string& rwyClass = getRunwayClassFromTrafficType(fltType);
700 double dHeading = ac->getTrafficRef()->getCourse();
701 apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, dHeading);
702 }
703
704 if (sid) {
705 for (wpt_vector_iterator i = sid->getFirstWayPoint(); i != sid->getLastWayPoint(); ++i) {
706 pushBackWaypoint(clone(*(i)));
707 }
708 } else {
709 if (!apt->hasRunwayWithIdent(activeRunway))
710 return false;
711
712 FGRunwayRef runway = apt->getRunwayByIdent(activeRunway);
713 SGGeod cur = runway->end();
714 if (!waypoints.empty()) {
715 cur = waypoints.back()->getPos();
716 }
717
718 // compute course towards destination
719 double course = SGGeodesy::courseDeg(cur, arrival->geod());
720 // double distance = SGGeodesy::distanceM(cur, arrival->geod());
721
722 const double headingDiffRunway = SGMiscd::normalizePeriodic(-180, 180, course - runway->headingDeg());
723
724 if (fabs(headingDiffRunway) < 10) {
725 SGGeod climb1 = SGGeodesy::direct(cur, course, 10 * SG_NM_TO_METER);
726 FGAIWaypoint* wpt = createInAir(ac, "10000ft climb", climb1, 10000, vClimb);
727 pushBackWaypoint(wpt);
728
729 SGGeod climb2 = SGGeodesy::direct(cur, course, 20 * SG_NM_TO_METER);
730 wpt = createInAir(ac, "18000ft climb", climb2, 18000, vClimb);
731 pushBackWaypoint(wpt);
732 } else {
733 double initialTurnRadius = getTurnRadius(vClimb, true);
734 SGGeod climb1 = SGGeodesy::direct(cur, runway->headingDeg(), 5 * SG_NM_TO_METER);
735 FGAIWaypoint* wpt = createInAir(ac, "5000ft climb", climb1, 5000, vClimb);
736 pushBackWaypoint(wpt);
737 int rightAngle = headingDiffRunway > 0 ? 90 : -90;
738 int firstTurnIncrement = headingDiffRunway > 0 ? 2 : -2;
739
740 SGGeod firstTurnCenter = SGGeodesy::direct(climb1, ac->getTrueHeadingDeg() + rightAngle, initialTurnRadius);
741 createArc(ac, firstTurnCenter, ac->_getHeading() - rightAngle, course - rightAngle, firstTurnIncrement, initialTurnRadius, 5000, 100, vClimb, "climb-out%03d");
742 SGGeod climb2 = SGGeodesy::direct(cur, course, 20 * SG_NM_TO_METER);
743 wpt = createInAir(ac, "18000ft climb", waypoints.back()->getPos(), 18000, vClimb);
744 pushBackWaypoint(wpt);
745 }
746 }
747
748 return true;
749 }
750
751 /*******************************************************************
752 * CreateDescent
753 * Generate a flight path from the last waypoint of the cruise to
754 * the permission to land point
755 ******************************************************************/
756 bool FGAIFlightPlan::createDescent(FGAIAircraft* ac,
757 FGAirport* apt,
758 const SGGeod& current,
759 double speed,
760 double alt,
761 const string& fltType,
762 double requiredDistance)
763 {
764 // The descent path contains the following phases:
765 // 1) a semi circle towards runway end
766 // 2) a linear glide path from the initial position to
767 // 3) a semi circle turn to final
768 // 4) approach
769 double vDescent = ac->getPerformance()->vDescent();
770 // double vApproach = ac->getPerformance()->vApproach();
771
772 //Beginning of Descent
773 const string& rwyClass = getRunwayClassFromTrafficType(fltType);
774 double heading = ac->getTrueHeadingDeg();
775 apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading);
776 if (!apt->hasRunwayWithIdent(activeRunway)) {
777 SG_LOG(SG_AI, SG_WARN, ac->getCallSign() << "| FGAIFlightPlan::createDescent: No such runway " << activeRunway << " at " << apt->ident());
778 return false;
779 }
780
781 FGRunwayRef rwy = apt->getRunwayByIdent(activeRunway);
782 if (waypoints.size() == 0 && ac->getTrueHeadingDeg() == 0) {
783 // we obviously have no previous state and are free to position the aircraft
784 double courseTowardsThreshold = SGGeodesy::courseDeg(current, rwy->pointOnCenterlineDisplaced(0));
785 ac->setHeading(courseTowardsThreshold);
786 }
787
788 const SGGeod threshold = rwy->threshold();
789 const double currElev = threshold.getElevationFt();
790 const double altDiff = alt - currElev - 2000;
791
792 // depending on entry we differ approach (teardrop/direct/parallel)
793
794 const double initialTurnRadius = getTurnRadius(vDescent, true);
795 //double finalTurnRadius = getTurnRadius(vApproach, true);
796
797 // get length of the downwind leg for the intended runway
798 const double distanceOut = apt->getDynamics()->getApproachController()->getRunway(rwy->name())->getApproachDistance(); //12 * SG_NM_TO_METER;
799 //time_t previousArrivalTime= apt->getDynamics()->getApproachController()->getRunway(rwy->name())->getEstApproachTime();
800
801 // tells us the direction we have to turn
802 const double headingDiffRunway = SGMiscd::normalizePeriodic(-180, 180, ac->getTrueHeadingDeg() - rwy->headingDeg());
803 double lateralOffset = initialTurnRadius;
804 if (headingDiffRunway > 0.0) {
805 lateralOffset *= -1.0;
806 }
807
808 SGGeod initialTarget = rwy->pointOnCenterline(-distanceOut);
809 SGGeod otherRwyEnd = rwy->pointOnCenterline(rwy->lengthM());
810 SGGeod secondaryTarget =
811 rwy->pointOffCenterline(-2 * distanceOut, lateralOffset);
812 SGGeod secondHoldCenter =
813 rwy->pointOffCenterline(-3 * distanceOut, lateralOffset);
814 SGGeod refPoint = rwy->pointOnCenterline(0);
815 double distance = SGGeodesy::distanceM(current, initialTarget);
816 double azimuth = SGGeodesy::courseDeg(current, initialTarget);
817 double secondaryAzimuth = SGGeodesy::courseDeg(current, secondaryTarget);
818 double initialHeadingDiff = SGMiscd::normalizePeriodic(-180, 180, azimuth - secondaryAzimuth);
819 double courseTowardsThreshold = SGGeodesy::courseDeg(current, rwy->pointOnCenterlineDisplaced(0));
820 // double courseTowardsRwyEnd = SGGeodesy::courseDeg(current, otherRwyEnd);
821 double headingDiffToRunwayThreshold = SGMiscd::normalizePeriodic(-180, 180, ac->getTrueHeadingDeg() - courseTowardsThreshold);
822 // double headingDiffToRunwayEnd = SGMiscd::normalizePeriodic(-180, 180, ac->getTrueHeadingDeg() - courseTowardsRwyEnd);
823
824 SG_LOG(SG_AI, SG_BULK, ac->getCallSign() << "| "
825 << " WPs : " << waypoints.size() << " Heading Diff (rwy) : " << headingDiffRunway << " Distance : " << distance << " Azimuth : " << azimuth << " Heading : " << ac->getTrueHeadingDeg() << " Initial Headingdiff " << initialHeadingDiff << " Lateral : " << lateralOffset);
826 // Erase the two bogus BOD points: Note check for conflicts with scripted AI flightPlans
827 IncrementWaypoint(false);
828 IncrementWaypoint(false);
829
830 if (fabs(headingDiffRunway) >= 30 && fabs(headingDiffRunway) <= 150) {
831 if (distance < (2 * initialTurnRadius)) {
832 // Too near so we pass over and enter over other side
833 SG_LOG(SG_AI, SG_BULK, ac->getCallSign() << "| Enter near S curve");
834 secondaryTarget =
835 rwy->pointOffCenterline(-2 * distanceOut, -lateralOffset);
836 secondHoldCenter =
837 rwy->pointOffCenterline(-3 * distanceOut, -lateralOffset);
838
839 // Entering not "straight" into runway so we do a s-curve
840 int rightAngle = headingDiffRunway > 0 ? 90 : -90;
841 int firstTurnIncrement = headingDiffRunway > 0 ? 2 : -2;
842
843 SGGeod firstTurnCenter = SGGeodesy::direct(current, ac->getTrueHeadingDeg() + rightAngle, initialTurnRadius);
844 SGGeod newCurrent = current;
845 // If we are not far enough cross over
846 if (abs(headingDiffToRunwayThreshold) < 90) {
847 newCurrent = SGGeodesy::direct(current, ac->getTrueHeadingDeg(), distance + 1000);
848 firstTurnCenter = SGGeodesy::direct(newCurrent, ac->getTrueHeadingDeg() + rightAngle, initialTurnRadius);
849 createLine(ac, current, ac->getTrueHeadingDeg(), distance + 1000, waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, 0, vDescent, "move%03d");
850 }
851 int offset = 1000;
852 while (SGGeodesy::distanceM(firstTurnCenter, secondaryTarget) < 2 * initialTurnRadius) {
853 newCurrent = SGGeodesy::direct(newCurrent, ac->getTrueHeadingDeg(), offset += 100);
854 firstTurnCenter = SGGeodesy::direct(newCurrent, ac->getTrueHeadingDeg() + rightAngle, initialTurnRadius);
855 }
856 const double dHeading = VectorMath::outerTangentsAngle(firstTurnCenter, secondaryTarget, initialTurnRadius, initialTurnRadius)[0];
857 createArc(ac, firstTurnCenter, ac->_getHeading() - rightAngle, dHeading - rightAngle, firstTurnIncrement, initialTurnRadius, waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 4, vDescent, "near-initialturn%03d");
858 double length = VectorMath::innerTangentsLength(firstTurnCenter, secondaryTarget, initialTurnRadius, initialTurnRadius);
859 createLine(ac, waypoints.back()->getPos(), dHeading, length, waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 2, vDescent, "descent%03d");
860 int startVal = SGMiscd::normalizePeriodic(0, 360, dHeading - rightAngle);
861 int endVal = SGMiscd::normalizePeriodic(0, 360, rwy->headingDeg() - rightAngle);
862 // Turn into runway
863 createArc(ac, secondaryTarget, startVal, endVal, firstTurnIncrement, initialTurnRadius,
864 waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 4, vDescent, "turn%03d");
865 } else {
866 SG_LOG(SG_AI, SG_BULK, ac->getCallSign() << "| Enter far S curve");
867 // Entering not "straight" into runway so we do a s-curve
868 int rightAngle = headingDiffRunway > 0 ? 90 : -90;
869 int firstTurnIncrement = headingDiffRunway > 0 ? 2 : -2;
870 SGGeod firstTurnCenter = SGGeodesy::direct(current, ac->getTrueHeadingDeg() + rightAngle, initialTurnRadius);
871 int innerTangent = headingDiffRunway < 0 ? 0 : 1;
872 int offset = 1000;
873 while (SGGeodesy::distanceM(firstTurnCenter, secondaryTarget) < 2 * initialTurnRadius) {
874 secondaryTarget = rwy->pointOffCenterline(-2 * distanceOut + (offset += 1000), lateralOffset);
875 }
876 const double dHeading = VectorMath::innerTangentsAngle(firstTurnCenter, secondaryTarget, initialTurnRadius, initialTurnRadius)[innerTangent];
877 createArc(ac, firstTurnCenter, ac->_getHeading() - rightAngle, dHeading - rightAngle, firstTurnIncrement, initialTurnRadius, waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 8, vDescent, "far-initialturn%03d");
878 const double length = VectorMath::innerTangentsLength(firstTurnCenter, secondaryTarget, initialTurnRadius, initialTurnRadius);
879 if (waypoints.empty()) {
880 createLine(ac, current, dHeading, length, alt, altDiff * 0.75, vDescent, "descent%03d");
881 } else {
882 const auto lastPos = waypoints.back()->getPos();
883 const auto lastAlt = waypoints.back()->getAltitude();
884 createLine(ac, lastPos, dHeading, length, lastAlt, altDiff * 0.75, vDescent, "descent%03d");
885 }
886 const int startVal = SGMiscd::normalizePeriodic(0, 360, dHeading + rightAngle);
887 const int endVal = SGMiscd::normalizePeriodic(0, 360, rwy->headingDeg() + rightAngle);
888 // Turn into runway
889 createArc(ac, secondaryTarget, startVal, endVal, firstTurnIncrement * -1, initialTurnRadius,
890 waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 8, vDescent, "s-turn%03d");
891 }
892 } else if (fabs(headingDiffRunway) >= 150) {
893 // We are entering downwind
894 if (distance < (2 * initialTurnRadius)) {
895 // Too near so we pass over and enter over other side
896 SG_LOG(SG_AI, SG_BULK, ac->getCallSign() << "| Enter near downrunway");
897 secondaryTarget =
898 rwy->pointOffCenterline(-2 * distanceOut, -lateralOffset);
899 secondHoldCenter =
900 rwy->pointOffCenterline(-3 * distanceOut, -lateralOffset);
901
902 // Entering not "straight" into runway so we do a s-curve
903 int rightAngle = azimuth > 0 ? 90 : -90;
904 int firstTurnIncrement = azimuth > 0 ? 2 : -2;
905
906 SGGeod firstTurnCenter = SGGeodesy::direct(current, ac->getTrueHeadingDeg() + rightAngle, initialTurnRadius);
907 // rwy->headingDeg()-rightAngle
908 int endVal = SGMiscd::normalizePeriodic(0, 360, rwy->headingDeg() - 180);
909 SGGeod secondTurnCenter = SGGeodesy::direct(firstTurnCenter, endVal, 2 * initialTurnRadius);
910 createArc(ac, firstTurnCenter, ac->_getHeading() - rightAngle, endVal, firstTurnIncrement, initialTurnRadius, waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 4, vDescent, "d-near-initialturn%03d");
911 endVal = SGMiscd::normalizePeriodic(0, 360, rwy->headingDeg());
912 // int endVal2 = SGMiscd::normalizePeriodic(0, 360, rwy->headingDeg()-rightAngle);
913 const double endVal2 = VectorMath::outerTangentsAngle(secondTurnCenter, secondaryTarget, initialTurnRadius, initialTurnRadius)[0];
914 createArc(ac, secondTurnCenter, endVal, endVal2 + rightAngle, -firstTurnIncrement, initialTurnRadius, waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 4, vDescent, "secondturn%03d");
915 //outer
916 double length = VectorMath::outerTangentsLength(secondTurnCenter, secondaryTarget, initialTurnRadius, initialTurnRadius);
917 createLine(ac, waypoints.back()->getPos(), endVal2, length, waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 4, vDescent, "descent%03d");
918 endVal = SGMiscd::normalizePeriodic(0, 360, rwy->headingDeg() + rightAngle);
919 // Turn into runway
920 createArc(ac, secondaryTarget, endVal2 + rightAngle, endVal, -firstTurnIncrement, initialTurnRadius,
921 waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 4, vDescent, "turn%03d");
922 } else {
923 SG_LOG(SG_AI, SG_BULK, ac->getCallSign() << "| Enter far S downrunway");
924 // Entering not "straight" into runway so we do a s-curve
925 int rightAngle = headingDiffRunway > 0 ? 90 : -90;
926 int firstTurnIncrement = headingDiffRunway > 0 ? 2 : -2;
927 int innerTangent = headingDiffRunway < 0 ? 0 : 1;
928 SGGeod firstTurnCenter = SGGeodesy::direct(current, ac->getTrueHeadingDeg() + rightAngle, initialTurnRadius);
929 const double dHeading = VectorMath::innerTangentsAngle(firstTurnCenter, secondaryTarget, initialTurnRadius, initialTurnRadius)[innerTangent];
930 createArc(ac, firstTurnCenter, ac->_getHeading() - rightAngle, dHeading - rightAngle, firstTurnIncrement, initialTurnRadius, waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 3, vDescent, "d-far-initialturn%03d");
931 double length = VectorMath::innerTangentsLength(firstTurnCenter, secondaryTarget, initialTurnRadius, initialTurnRadius);
932 createLine(ac, waypoints.back()->getPos(), dHeading, length, waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 3, vDescent, "descent%03d");
933 int startVal = SGMiscd::normalizePeriodic(0, 360, dHeading + rightAngle);
934 int endVal = SGMiscd::normalizePeriodic(0, 360, rwy->headingDeg() + rightAngle);
935 // Turn into runway
936 createArc(ac, secondaryTarget, startVal, endVal, -firstTurnIncrement, initialTurnRadius,
937 waypoints.size() > 0 ? waypoints.back()->getAltitude() : alt, altDiff / 3, vDescent, "d-s-turn%03d");
938 }
939 } else {
940 SG_LOG(SG_AI, SG_BULK, ac->getCallSign() << "| Enter far straight");
941 // Entering "straight" into runway so only one turn
942 int rightAngle = headingDiffRunway > 0 ? 90 : -90;
943 SGGeod firstTurnCenter = SGGeodesy::direct(current, ac->getTrueHeadingDeg() - rightAngle, initialTurnRadius);
944 int firstTurnIncrement = headingDiffRunway > 0 ? -2 : 2;
945 const double dHeading = rwy->headingDeg();
946 createArc(ac, firstTurnCenter, ac->_getHeading() + rightAngle, dHeading + rightAngle, firstTurnIncrement, initialTurnRadius, ac->getAltitude(), altDiff / 3, vDescent, "straight_turn_%03d");
947 }
948
949 time_t now = globals->get_time_params()->get_cur_time();
950
951 arrivalTime = now;
952 //choose a distance to the runway such that it will take at least 60 seconds more
953 // time to get there than the previous aircraft.
954 // Don't bother when aircraft need to be repositioned, because that marks the initialization phased...
955 return true;
956 }
957
958 /*******************************************************************
959 * CreateHold
960 * Generate a hold flight path from the permission to land point
961 * Exactly one hold pattern
962 ******************************************************************/
963 bool FGAIFlightPlan::createHold(FGAIAircraft* ac,
964 FGAirport* apt,
965 const SGGeod& current,
966 double speed,
967 double alt,
968 const string& fltType,
969 double requiredDistance)
970 {
971 // Beginning of Descent
972 const string& rwyClass = getRunwayClassFromTrafficType(fltType);
973 double vDescent = ac->getPerformance()->vDescent();
974 // double vApproach = ac->getPerformance()->vApproach();
975 double initialTurnRadius = getTurnRadius(vDescent, true);
976 double dHeading = ac->getTrueHeadingDeg();
977 apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway,
978 dHeading);
979 if (!apt->hasRunwayWithIdent(activeRunway)) {
980 SG_LOG(SG_AI, SG_WARN, ac->getCallSign() << "| FGAIFlightPlan::createHold: No such runway " << activeRunway << " at " << apt->ident());
981 return false;
982 }
983 FGRunwayRef rwy = apt->getRunwayByIdent(activeRunway);
984 double currentAltitude = waypoints.back()->getAltitude();
985 double distanceOut = apt->getDynamics()->getApproachController()->getRunway(rwy->name())->getApproachDistance(); //12 * SG_NM_TO_METER;
986 double lateralOffset = initialTurnRadius;
987
988 SGGeod secondaryTarget = rwy->pointOffCenterline(-2 * distanceOut, lateralOffset);
989 SGGeod secondHoldCenter = rwy->pointOffCenterline(-4 * distanceOut, lateralOffset);
990
991 createArc(ac, secondaryTarget, rwy->headingDeg() - 90, rwy->headingDeg() + 90, 5, initialTurnRadius,
992 currentAltitude, 0, vDescent, "hold_1_%03d");
993 createArc(ac, secondHoldCenter, rwy->headingDeg() + 90, rwy->headingDeg() - 90, 5, initialTurnRadius,
994 currentAltitude, 0, vDescent, "hold_2_%03d");
995
996 return true;
997 }
998 /**
999 * compute the distance along the centerline, to the ILS glideslope
1000 * transmitter. Return -1 if there's no GS for the runway
1001 */
1002 static double runwayGlideslopeTouchdownDistance(FGRunway* rwy)
1003 {
1004 FGNavRecord* gs = rwy->glideslope();
1005 if (!gs) {
1006 return -1;
1007 }
1008
1009 SGVec3d runwayPosCart = SGVec3d::fromGeod(rwy->pointOnCenterline(0.0));
1010 // compute a unit vector in ECF cartesian space, from the runway beginning to the end
1011 SGVec3d runwayDirectionVec = normalize(SGVec3d::fromGeod(rwy->end()) - runwayPosCart);
1012 SGVec3d gsTransmitterVec = gs->cart() - runwayPosCart;
1013
1014 // project the gsTransmitterVec along the runwayDirctionVec to get out
1015 // final value (in metres)
1016 double dist = dot(runwayDirectionVec, gsTransmitterVec);
1017 return dist;
1018 }
1019
1020 /*******************************************************************
1021 * CreateLanding (Leg 7)
1022 * Create a flight path from the "permission to land" point (currently
1023 hardcoded at 5000 meters from the threshold) to the threshold, at
1024 a standard glide slope angle of 3 degrees.
1025 Position : 50.0354 8.52592 384 364 11112
1026 ******************************************************************/
1027 bool FGAIFlightPlan::createLanding(FGAIAircraft* ac, FGAirport* apt,
1028 const string& fltType)
1029 {
1030 double vTouchdown = ac->getPerformance()->vTouchdown();
1031 double vTaxi = ac->getPerformance()->vTaxi();
1032 double decel = ac->getPerformance()->decelerationOnGround();
1033 double vApproach = ac->getPerformance()->vApproach();
1034
1035 double vTouchdownMetric = vTouchdown * SG_KT_TO_MPS;
1036 double vTaxiMetric = vTaxi * SG_KT_TO_MPS;
1037 double decelMetric = decel * SG_KT_TO_MPS;
1038
1039 char buffer[30];
1040 if (!apt->hasRunwayWithIdent(activeRunway)) {
1041 SG_LOG(SG_AI, SG_WARN, "FGAIFlightPlan::createLanding: No such runway " << activeRunway << " at " << apt->ident());
1042 return false;
1043 }
1044
1045
1046 FGRunway* rwy = apt->getRunwayByIdent(activeRunway);
1047 if (!rwy) {
1048 return false;
1049 }
1050
1051 // depending on entry we differ approach (teardrop/direct/parallel)
1052 const double headingDiff = ac->getBearing(rwy->headingDeg());
1053 SG_LOG(SG_AI, SG_BULK, "Heading Diff " << headingDiff);
1054
1055 SGGeod threshold = rwy->threshold();
1056 double currElev = threshold.getElevationFt();
1057
1058 double touchdownDistance = runwayGlideslopeTouchdownDistance(rwy);
1059 if (touchdownDistance < 0.0) {
1060 double landingLength = rwy->lengthM() - (rwy->displacedThresholdM());
1061 // touchdown 25% of the way along the landing area
1062 touchdownDistance = rwy->displacedThresholdM() + (landingLength * 0.25);
1063 }
1064
1065 const double tanGlideslope = tan(3.0);
1066
1067 SGGeod coord;
1068 // find glideslope entry point, 2000' above touchdown elevation
1069 double glideslopeEntry = -((2000 * SG_FEET_TO_METER) / tanGlideslope) + touchdownDistance;
1070
1071 snprintf(buffer, sizeof(buffer), "Glideslope begin Rwy %s", activeRunway.c_str());
1072
1073 FGAIWaypoint* wpt = createInAir(ac, buffer, rwy->pointOnCenterline(-glideslopeEntry),
1074 currElev + 2000, vApproach);
1075 wpt->setGear_down(true);
1076 wpt->setFlaps(1.0f);
1077 wpt->setSpeedBrakes(1.0f);
1078 pushBackWaypoint(wpt);
1079
1080 // deceleration point, 500' above touchdown elevation - slow from approach speed
1081 // to touchdown speed
1082 double decelPoint = -((500 * SG_FEET_TO_METER) / tanGlideslope) + touchdownDistance;
1083 wpt = createInAir(ac, "500' decel", rwy->pointOnCenterline(-decelPoint),
1084 currElev + 500, vTouchdown);
1085 wpt->setGear_down(true);
1086 wpt->setFlaps(1.0f);
1087 wpt->setSpeedBrakes(1.0f);
1088 pushBackWaypoint(wpt);
1089
1090 // compute elevation above the runway start, based on a 3-degree glideslope
1091 double heightAboveRunwayStart = touchdownDistance *
1092 tan(3.0 * SG_DEGREES_TO_RADIANS) * SG_METER_TO_FEET;
1093 wpt = createInAir(ac, "CrossThreshold", rwy->begin(),
1094 heightAboveRunwayStart + currElev, vTouchdown);
1095 wpt->setGear_down(true);
1096 wpt->setFlaps(1.0f);
1097 wpt->setSpeedBrakes(1.0f);
1098 pushBackWaypoint(wpt);
1099
1100 double rolloutDistance = accelDistance(vTouchdownMetric, vTaxiMetric, decelMetric);
1101
1102 SG_LOG(SG_AI, SG_BULK, "Landing " << glideslopeEntry << "\t" << decelPoint << " Rollout " << rolloutDistance);
1103
1104 int nPoints = (int)(rolloutDistance / 60);
1105 for (int i = 1; i <= nPoints; i++) {
1106 snprintf(buffer, sizeof(buffer), "rollout%03d", i);
1107 double t = 1 - pow((double)(nPoints - i), 2) / pow(nPoints, 2);
1108 coord = rwy->pointOnCenterline(touchdownDistance + (rolloutDistance * t));
1109 double vel = (vTouchdownMetric * (1.0 - t)) + (vTaxiMetric * t);
1110 wpt = createOnRunway(ac, buffer, coord, currElev, vel);
1111 wpt->setFlaps(1.0f);
1112 wpt->setSpeedBrakes(1.0f);
1113 wpt->setSpoilers(1.0f);
1114 wpt->setCrossat(currElev);
1115 pushBackWaypoint(wpt);
1116 }
1117
1118 wpt->setSpeed(vTaxi);
1119 double mindist = (1.1 * rolloutDistance) + touchdownDistance;
1120
1121 FGGroundNetwork* gn = apt->groundNetwork();
1122 if (!gn) {
1123 SG_LOG(SG_AI, SG_DEBUG, "No groundnet " << apt->getId() << " no landing created.");
1124 return true;
1125 }
1126
1127 coord = rwy->pointOnCenterline(mindist);
1128 FGTaxiNodeRef tn;
1129 if (gn->getVersion() > 0) {
1130 tn = gn->findNearestNodeOnRunwayExit(coord, rwy);
1131 } else {
1132 tn = gn->findNearestNode(coord);
1133 }
1134
1135 if (tn) {
1136 wpt = createOnRunway(ac, buffer, tn->geod(), currElev, vTaxi);
1137 wpt->setFlaps(1.0f);
1138 wpt->setSpeedBrakes(1.0f);
1139 wpt->setSpoilers(0.0f);
1140 pushBackWaypoint(wpt);
1141 }
1142
1143 return true;
1144 }
1145
1146 /*******************************************************************
1147 * createParking Leg 9
1148 * initialize the Aircraft at the parking location
1149 ******************************************************************/
1150 bool FGAIFlightPlan::createParking(FGAIAircraft* ac, FGAirport* apt,
1151 double radius)
1152 {
1153 FGAIWaypoint* wpt;
1154 double aptElev = apt->getElevation();
1155 double vTaxi = ac->getPerformance()->vTaxi();
1156 double vTaxiReduced = vTaxi * (2.0 / 3.0);
1157 if (!gate.isValid()) {
1158 wpt = createOnGround(ac, "END-ParkingInvalidGate", apt->geod(), aptElev,
1159 vTaxiReduced);
1160 pushBackWaypoint(wpt);
1161 return true;
1162 }
1163
1164 FGParking* parking = gate.parking();
1165 double reverseHeading = SGMiscd::normalizePeriodic(0, 360, parking->getHeading() + 180.0);
1166 double az; // unused
1167 SGGeod pos;
1168
1169 SGGeodesy::direct(parking->geod(), reverseHeading, 18,
1170 pos, az);
1171
1172 wpt = createOnGround(ac, "parking1", pos, aptElev, 3);
1173 pushBackWaypoint(wpt);
1174
1175 SGGeodesy::direct(parking->geod(), reverseHeading, 14,
1176 pos, az);
1177 wpt = createOnGround(ac, "parking2", pos, aptElev, 3);
1178 pushBackWaypoint(wpt);
1179
1180 SGGeodesy::direct(parking->geod(), reverseHeading, 10,
1181 pos, az);
1182 wpt = createOnGround(ac, "parking3", pos, aptElev, 2);
1183 pushBackWaypoint(wpt);
1184
1185 SGGeodesy::direct(parking->geod(), reverseHeading, 6,
1186 pos, az);
1187 wpt = createOnGround(ac, "parking4", pos, aptElev, 2);
1188 pushBackWaypoint(wpt);
1189
1190 SGGeodesy::direct(parking->geod(), reverseHeading, 3,
1191 pos, az);
1192 wpt = createOnGround(ac, "parking5", pos, aptElev, 2);
1193 pushBackWaypoint(wpt);
1194
1195 char buffer[30];
1196 snprintf(buffer, sizeof(buffer), "Parking-%s", parking->getName().c_str());
1197
1198 wpt = createOnGround(ac, buffer, parking->geod(), aptElev, vTaxiReduced / 3);
1199 pushBackWaypoint(wpt);
1200 SGGeodesy::direct(parking->geod(), parking->getHeading(), 2,
1201 pos, az);
1202 wpt = createOnGround(ac, "Beyond-Parking", pos, aptElev, vTaxiReduced / 3);
1203 pushBackWaypoint(wpt);
1204 SGGeodesy::direct(parking->geod(), parking->getHeading(), 3,
1205 pos, az);
1206 wpt = createOnGround(ac, "END-Parking", pos, aptElev, vTaxiReduced / 3);
1207 pushBackWaypoint(wpt);
1208 return true;
1209 }
1210
1211 /**
1212 *
1213 * @param fltType a string describing the type of
1214 * traffic, normally used for gate assignments
1215 * @return a converted string that gives the runway
1216 * preference schedule to be used at aircraft having
1217 * a preferential runway schedule implemented (i.e.
1218 * having a rwyprefs.xml file
1219 *
1220 * Currently valid traffic types for gate assignment:
1221 * - gate (commercial gate)
1222 * - cargo (commercial cargo)
1223 * - ga (general aviation)
1224 * - ul (ultralight)
1225 * - mil-fighter (military - fighter)
1226 * - mil-transport (military - transport)
1227 *
1228 * Valid runway classes:
1229 * - com (commercial traffic: jetliners, passenger and cargo)
1230 * - gen (general aviation)
1231 * - ul (ultralight: I can imagine that these may share a runway with ga on some airports)
1232 * - mil (all military traffic)
1233 */
1234 const char* FGAIFlightPlan::getRunwayClassFromTrafficType(const string& fltType)
1235 {
1236 if ((fltType == "gate") || (fltType == "cargo")) {
1237 return "com";
1238 }
1239 if (fltType == "ga") {
1240 return "gen";
1241 }
1242 if (fltType == "ul") {
1243 return "ul";
1244 }
1245 if ((fltType == "mil-fighter") || (fltType == "mil-transport")) {
1246 return "mil";
1247 }
1248 return "com";
1249 }
1250
1251
1252 double FGAIFlightPlan::getTurnRadius(double speed, bool inAir)
1253 {
1254 double turn_radius;
1255 if (inAir == false) {
1256 turn_radius = ((360 / 30) * fabs(speed)) / (2 * M_PI);
1257 } else {
1258 turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank
1259 }
1260 return turn_radius;
1261 }