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;
168 //cerr << "Creating direct forward departure route fragment" << endl;
169 double lat2 = 0.0, lon2 = 0.0, az2 = 0.0;
171 geo_direct_wgs_84 ( 0, lat, lon, heading,
172 2, &lat2, &lon2, &az2 );
175 wpt->latitude = lat2;
176 wpt->longitude = lon2;
177 wpt->altitude = dep->getElevation();
178 wpt->speed = vTaxiReduced;
179 wpt->crossat = -10000;
180 wpt->gear_down = true;
181 wpt->flaps_down= true;
182 wpt->finished = false;
183 wpt->on_ground = true;
185 waypoints.push_back(wpt);
187 geo_direct_wgs_84 ( 0, lat, lon, heading,
188 4, &lat2, &lon2, &az2 );
191 wpt->latitude = lat2;
192 wpt->longitude = lon2;
193 wpt->altitude = dep->getElevation();
194 wpt->speed = vTaxiReduced;
195 wpt->crossat = -10000;
196 wpt->gear_down = true;
197 wpt->flaps_down= true;
198 wpt->finished = false;
199 wpt->on_ground = true;
201 waypoints.push_back(wpt);
203 //cerr << "Creating final push forward point for gate " << gateId << endl;
204 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(gateId);
205 FGTaxiSegmentVectorIterator ts = tn->getBeginRoute();
206 FGTaxiSegmentVectorIterator te = tn->getEndRoute();
208 SG_LOG(SG_GENERAL, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
211 tn = (*ts)->getEnd();
212 lastNodeVisited = tn->getIndex();
214 SG_LOG(SG_GENERAL, SG_ALERT, "No valid taxinode found");
218 wpt->name = "PushBackPoint";
219 wpt->latitude = tn->getLatitude();
220 wpt->longitude = tn->getLongitude();
221 wpt->altitude = dep->getElevation();
222 wpt->speed = vTaxiReduced;
223 wpt->crossat = -10000;
224 wpt->gear_down = true;
225 wpt->flaps_down= true;
226 wpt->finished = false;
227 wpt->on_ground = true;
228 wpt->routeIndex = (*ts)->getIndex();
229 waypoints.push_back(wpt);
237 /*******************************************************************
238 * createPushBackFallBack
239 * This is the backup function for airports that don't have a
241 ******************************************************************/
242 void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep,
246 const string& fltType,
247 const string& aircraftType,
248 const string& airline)
257 double vTaxi = ac->getPerformance()->vTaxi();
258 double vTaxiBackward = vTaxi * (-2.0/3.0);
259 double vTaxiReduced = vTaxi * (2.0/3.0);
263 dep->getDynamics()->getParking(-1, &lat, &lon, &heading);
268 waypoint *wpt = new waypoint;
271 wpt->longitude = lon;
272 wpt->altitude = dep->getElevation();
273 wpt->speed = vTaxiBackward;
274 wpt->crossat = -10000;
275 wpt->gear_down = true;
276 wpt->flaps_down= true;
277 wpt->finished = false;
278 wpt->on_ground = true;
280 waypoints.push_back(wpt);
282 geo_direct_wgs_84 ( 0, lat, lon, heading,
284 &lat2, &lon2, &az2 );
287 wpt->latitude = lat2;
288 wpt->longitude = lon2;
289 wpt->altitude = dep->getElevation();
290 wpt->speed = vTaxiBackward;
291 wpt->crossat = -10000;
292 wpt->gear_down = true;
293 wpt->flaps_down= true;
294 wpt->finished = false;
295 wpt->on_ground = true;
297 waypoints.push_back(wpt);
298 geo_direct_wgs_84 ( 0, lat, lon, heading,
300 &lat2, &lon2, &az2 );
302 wpt->name = "taxiStart";
303 wpt->latitude = lat2;
304 wpt->longitude = lon2;
305 wpt->altitude = dep->getElevation();
306 wpt->speed = vTaxiReduced;
307 wpt->crossat = -10000;
308 wpt->gear_down = true;
309 wpt->flaps_down= true;
310 wpt->finished = false;
311 wpt->on_ground = true;
313 waypoints.push_back(wpt);