1 /******************************************************************************
2 * AIFlightPlanCreatePushBack.cxx
3 * Written by Durk Talsma, started August 1, 2007.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
25 #include <simgear/math/sg_geodesy.hxx>
27 #include <Airports/simple.hxx>
28 #include <Airports/runways.hxx>
29 #include <Airports/dynamics.hxx>
31 #include <Environment/environment_mgr.hxx>
32 #include <Environment/environment.hxx>
34 #include "AIFlightPlan.hxx"
35 #include "AIAircraft.hxx"
36 #include "performancedata.hxx"
39 // TODO: Use James Turner's createOnGround functions.
40 bool FGAIFlightPlan::createPushBack(FGAIAircraft *ac,
41 bool firstFlight, FGAirport *dep,
45 const string& fltType,
46 const string& aircraftType,
47 const string& airline)
49 double lat, lon, heading;
50 double vTaxi = ac->getPerformance()->vTaxi();
51 double vTaxiBackward = vTaxi * (-2.0/3.0);
52 double vTaxiReduced = vTaxi * (2.0/3.0);
53 FGTaxiRoute *pushBackRoute;
54 // Active runway can be conditionally set by ATC, so at the start of a new flight, this
58 if (!(dep->getDynamics()->getGroundNetwork()->exists())) {
59 //cerr << "Push Back fallback" << endl;
60 createPushBackFallBack(ac, firstFlight, dep, latitude, longitude,
61 radius, fltType, aircraftType, airline);
65 if (!(dep->getDynamics()->getAvailableParking(&lat, &lon,
68 aircraftType, airline))) {
69 SG_LOG(SG_INPUT, SG_WARN, "Warning: Could not find parking for a " <<
71 " of flight type " << fltType <<
72 " of airline " << airline <<
73 " at airport " << dep->getId());
76 snprintf (buffer, 10, "%d", gateId);
77 //FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
80 wpt->name = string(buffer); // fixme: should be the name of the taxiway
83 // Elevation is currently disregarded when on_ground is true
84 // because the AIModel obtains a periodic ground elevation estimate.
85 wpt->altitude = dep->getElevation();
86 wpt->speed = vTaxiBackward;
87 wpt->crossat = -10000;
88 wpt->gear_down = true;
89 wpt->flaps_down= true;
90 wpt->finished = false;
91 wpt->on_ground = true;
93 waypoints.push_back(wpt);
95 //cerr << "Success : GateId = " << gateId << endl;
96 SG_LOG(SG_INPUT, SG_WARN, "Warning: Succesfully found a parking for a " <<
98 " of flight type " << fltType <<
99 " of airline " << airline <<
100 " at airport " << dep->getId());
102 //cerr << "Push Back follow-up Flight" << endl;
103 dep->getDynamics()->getParking(gateId, &lat, &lon, &heading);
106 createPushBackFallBack(ac, firstFlight, dep, latitude, longitude,
107 radius, fltType, aircraftType, airline);
111 //cerr << "getting parking " << gateId;
112 //cerr << " for a " <<
114 // " of flight type " << fltType <<
115 // " of airline " << airline <<
116 // " at airport " << dep->getId() << endl;
117 FGParking *parking = dep->getDynamics()->getParking(gateId);
118 int pushBackNode = parking->getPushBackPoint();
121 pushBackRoute = parking->getPushBackRoute();
122 if ((pushBackNode > 0) && (pushBackRoute == 0)) {
125 //cerr << "Creating push-back for " << gateId << " (" << parking->getName() << ") using push-back point " << pushBackNode << endl;
126 route = dep->getDynamics()->getGroundNetwork()->findShortestRoute(gateId, pushBackNode, false);
127 parking->setPushBackRoute(new FGTaxiRoute(route));
130 pushBackRoute = parking->getPushBackRoute();
131 int size = pushBackRoute->size();
133 SG_LOG(SG_GENERAL, SG_WARN, "Push back route from gate " << gateId << " has only " << size << " nodes.");
134 SG_LOG(SG_GENERAL, SG_WARN, "Using " << pushBackNode);
136 pushBackRoute->first();
138 while(pushBackRoute->next(&node, &rte))
140 //FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findSegment(node)->getEnd();
142 snprintf (buffer, 10, "%d", node);
143 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
146 wpt->name = string(buffer); // fixme: should be the name of the taxiway
147 wpt->latitude = tn->getLatitude();
148 wpt->longitude = tn->getLongitude();
149 // Elevation is currently disregarded when on_ground is true
150 // because the AIModel obtains a periodic ground elevation estimate.
151 wpt->altitude = dep->getElevation();
152 wpt->speed = vTaxiBackward;
153 wpt->crossat = -10000;
154 wpt->gear_down = true;
155 wpt->flaps_down= true;
156 wpt->finished = false;
157 wpt->on_ground = true;
158 wpt->routeIndex = rte;
159 waypoints.push_back(wpt);
161 // some special considerations for the last point:
162 wpt->name = string("PushBackPoint");
164 //for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end(); i++) {
165 // cerr << "Waypoint Name: " << (*i)->name << endl;
169 string rwyClass = getRunwayClassFromTrafficType(fltType);
171 // Only set this if it hasn't been set by ATC already.
172 if (activeRunway.empty()) {
173 //cerr << "Getting runway for " << ac->getTrafficRef()->getCallSign() << " at " << apt->getId() << endl;
174 double depHeading = ac->getTrafficRef()->getCourse();
175 dep->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
178 rwy = dep->getRunwayByIdent(activeRunway);
179 SGGeod runwayTakeoff = rwy->pointOnCenterline(5.0);
181 FGGroundNetwork *gn = dep->getDynamics()->getGroundNetwork();
183 createDefaultTakeoffTaxi(ac, dep, rwy);
186 int runwayId = gn->findNearestNode(runwayTakeoff);
188 // Find out which node to start from
189 FGParking *park = dep->getDynamics()->getParking(gateId);
191 node = park->getPushBackPoint();
197 // HAndle case where parking doens't have a node
198 if ((node == 0) && park) {
206 //taxiRoute = new FGTaxiRoute;
207 FGTaxiRoute tr = gn->findShortestRoute(node, runwayId);
212 cerr << "Creating taxiroute from gate: " << gateId << " at " << dep->getId() << endl;
213 while (tr.next(&node, &route) && (nr++ < 3)) {
215 snprintf(buffer, 10, "%d", node);
216 tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
217 wpt = createOnGround(ac, buffer, tn->getGeod(), dep->getElevation(),
219 wpt->routeIndex = route;
220 waypoints.push_back(wpt);
222 wpt->name = "PushBackPoint";
223 lastNodeVisited = tn->getIndex();
224 //FGTaxiNode *firstNode = findNode(gateId);
225 //FGTaxiNode *lastNode = findNode(runwayId);
226 //cerr << "Creating direct forward departure route fragment" << endl;
228 double lat2 = 0.0, lon2 = 0.0, az2 = 0.0;
230 geo_direct_wgs_84 ( 0, lat, lon, heading,
231 2, &lat2, &lon2, &az2 );
234 wpt->latitude = lat2;
235 wpt->longitude = lon2;
236 wpt->altitude = dep->getElevation();
237 wpt->speed = vTaxiReduced;
238 wpt->crossat = -10000;
239 wpt->gear_down = true;
240 wpt->flaps_down= true;
241 wpt->finished = false;
242 wpt->on_ground = true;
244 waypoints.push_back(wpt);
246 geo_direct_wgs_84 ( 0, lat, lon, heading,
247 4, &lat2, &lon2, &az2 );
250 wpt->latitude = lat2;
251 wpt->longitude = lon2;
252 wpt->altitude = dep->getElevation();
253 wpt->speed = vTaxiReduced;
254 wpt->crossat = -10000;
255 wpt->gear_down = true;
256 wpt->flaps_down= true;
257 wpt->finished = false;
258 wpt->on_ground = true;
260 waypoints.push_back(wpt);
262 //cerr << "Creating final push forward point for gate " << gateId << endl;
263 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(gateId);
264 FGTaxiSegmentVectorIterator ts = tn->getBeginRoute();
265 FGTaxiSegmentVectorIterator te = tn->getEndRoute();
267 SG_LOG(SG_GENERAL, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
270 tn = (*ts)->getEnd();
271 lastNodeVisited = tn->getIndex();
273 SG_LOG(SG_GENERAL, SG_ALERT, "No valid taxinode found");
277 wpt->name = "PushBackPoint";
278 wpt->latitude = tn->getLatitude();
279 wpt->longitude = tn->getLongitude();
280 wpt->altitude = dep->getElevation();
281 wpt->speed = vTaxiReduced;
282 wpt->crossat = -10000;
283 wpt->gear_down = true;
284 wpt->flaps_down= true;
285 wpt->finished = false;
286 wpt->on_ground = true;
287 wpt->routeIndex = (*ts)->getIndex();
288 waypoints.push_back(wpt);
294 /*******************************************************************
295 * createPushBackFallBack
296 * This is the backup function for airports that don't have a
298 ******************************************************************/
299 void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep,
303 const string& fltType,
304 const string& aircraftType,
305 const string& airline)
314 double vTaxi = ac->getPerformance()->vTaxi();
315 double vTaxiBackward = vTaxi * (-2.0/3.0);
316 double vTaxiReduced = vTaxi * (2.0/3.0);
320 dep->getDynamics()->getParking(-1, &lat, &lon, &heading);
325 waypoint *wpt = new waypoint;
328 wpt->longitude = lon;
329 wpt->altitude = dep->getElevation();
330 wpt->speed = vTaxiBackward;
331 wpt->crossat = -10000;
332 wpt->gear_down = true;
333 wpt->flaps_down= true;
334 wpt->finished = false;
335 wpt->on_ground = true;
337 waypoints.push_back(wpt);
339 geo_direct_wgs_84 ( 0, lat, lon, heading,
341 &lat2, &lon2, &az2 );
344 wpt->latitude = lat2;
345 wpt->longitude = lon2;
346 wpt->altitude = dep->getElevation();
347 wpt->speed = vTaxiBackward;
348 wpt->crossat = -10000;
349 wpt->gear_down = true;
350 wpt->flaps_down= true;
351 wpt->finished = false;
352 wpt->on_ground = true;
354 waypoints.push_back(wpt);
355 geo_direct_wgs_84 ( 0, lat, lon, heading,
357 &lat2, &lon2, &az2 );
359 wpt->name = "taxiStart";
360 wpt->latitude = lat2;
361 wpt->longitude = lon2;
362 wpt->altitude = dep->getElevation();
363 wpt->speed = vTaxiReduced;
364 wpt->crossat = -10000;
365 wpt->gear_down = true;
366 wpt->flaps_down= true;
367 wpt->finished = false;
368 wpt->on_ground = true;
370 waypoints.push_back(wpt);