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 void 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());
75 snprintf (buffer, 10, "%d", gateId);
76 //FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
79 wpt->name = string(buffer); // fixme: should be the name of the taxiway
82 // Elevation is currently disregarded when on_ground is true
83 // because the AIModel obtains a periodic ground elevation estimate.
84 wpt->altitude = dep->getElevation();
85 wpt->speed = vTaxiBackward;
86 wpt->crossat = -10000;
87 wpt->gear_down = true;
88 wpt->flaps_down= true;
89 wpt->finished = false;
90 wpt->on_ground = true;
92 waypoints.push_back(wpt);
94 //cerr << "Success : GateId = " << gateId << endl;
95 SG_LOG(SG_INPUT, SG_WARN, "Warning: Succesfully found a parking for a " <<
97 " of flight type " << fltType <<
98 " of airline " << airline <<
99 " at airport " << dep->getId());
101 //cerr << "Push Back follow-up Flight" << endl;
102 dep->getDynamics()->getParking(gateId, &lat, &lon, &heading);
105 createPushBackFallBack(ac, firstFlight, dep, latitude, longitude,
106 radius, fltType, aircraftType, airline);
110 //cerr << "getting parking " << gateId;
111 //cerr << " for a " <<
113 // " of flight type " << fltType <<
114 // " of airline " << airline <<
115 // " at airport " << dep->getId() << endl;
116 FGParking *parking = dep->getDynamics()->getParking(gateId);
117 int pushBackNode = parking->getPushBackPoint();
120 pushBackRoute = parking->getPushBackRoute();
121 if ((pushBackNode > 0) && (pushBackRoute == 0)) {
124 //cerr << "Creating push-back for " << gateId << " (" << parking->getName() << ") using push-back point " << pushBackNode << endl;
125 route = dep->getDynamics()->getGroundNetwork()->findShortestRoute(gateId, pushBackNode, false);
126 parking->setPushBackRoute(new FGTaxiRoute(route));
129 pushBackRoute = parking->getPushBackRoute();
130 int size = pushBackRoute->size();
132 SG_LOG(SG_GENERAL, SG_WARN, "Push back route from gate " << gateId << " has only " << size << " nodes.");
133 SG_LOG(SG_GENERAL, SG_WARN, "Using " << pushBackNode);
135 pushBackRoute->first();
137 while(pushBackRoute->next(&node, &rte))
139 //FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findSegment(node)->getEnd();
141 snprintf (buffer, 10, "%d", node);
142 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
145 wpt->name = string(buffer); // fixme: should be the name of the taxiway
146 wpt->latitude = tn->getLatitude();
147 wpt->longitude = tn->getLongitude();
148 // Elevation is currently disregarded when on_ground is true
149 // because the AIModel obtains a periodic ground elevation estimate.
150 wpt->altitude = dep->getElevation();
151 wpt->speed = vTaxiBackward;
152 wpt->crossat = -10000;
153 wpt->gear_down = true;
154 wpt->flaps_down= true;
155 wpt->finished = false;
156 wpt->on_ground = true;
157 wpt->routeIndex = rte;
158 waypoints.push_back(wpt);
160 // some special considerations for the last point:
161 wpt->name = string("PushBackPoint");
163 //for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end(); i++) {
164 // cerr << "Waypoint Name: " << (*i)->name << endl;
167 //cerr << "Creating direct forward departure route fragment" << endl;
168 double lat2 = 0.0, lon2 = 0.0, az2 = 0.0;
170 geo_direct_wgs_84 ( 0, lat, lon, heading,
171 2, &lat2, &lon2, &az2 );
174 wpt->latitude = lat2;
175 wpt->longitude = lon2;
176 wpt->altitude = dep->getElevation();
177 wpt->speed = vTaxiReduced;
178 wpt->crossat = -10000;
179 wpt->gear_down = true;
180 wpt->flaps_down= true;
181 wpt->finished = false;
182 wpt->on_ground = true;
184 waypoints.push_back(wpt);
186 geo_direct_wgs_84 ( 0, lat, lon, heading,
187 4, &lat2, &lon2, &az2 );
190 wpt->latitude = lat2;
191 wpt->longitude = lon2;
192 wpt->altitude = dep->getElevation();
193 wpt->speed = vTaxiReduced;
194 wpt->crossat = -10000;
195 wpt->gear_down = true;
196 wpt->flaps_down= true;
197 wpt->finished = false;
198 wpt->on_ground = true;
200 waypoints.push_back(wpt);
202 //cerr << "Creating final push forward point for gate " << gateId << endl;
203 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(gateId);
204 FGTaxiSegmentVectorIterator ts = tn->getBeginRoute();
205 FGTaxiSegmentVectorIterator te = tn->getEndRoute();
207 SG_LOG(SG_GENERAL, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
210 tn = (*ts)->getEnd();
211 lastNodeVisited = tn->getIndex();
213 SG_LOG(SG_GENERAL, SG_ALERT, "No valid taxinode found");
217 wpt->name = "PushBackPoint";
218 wpt->latitude = tn->getLatitude();
219 wpt->longitude = tn->getLongitude();
220 wpt->altitude = dep->getElevation();
221 wpt->speed = vTaxiReduced;
222 wpt->crossat = -10000;
223 wpt->gear_down = true;
224 wpt->flaps_down= true;
225 wpt->finished = false;
226 wpt->on_ground = true;
227 wpt->routeIndex = (*ts)->getIndex();
228 waypoints.push_back(wpt);
235 /*******************************************************************
236 * createPushBackFallBack
237 * This is the backup function for airports that don't have a
239 ******************************************************************/
240 void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep,
244 const string& fltType,
245 const string& aircraftType,
246 const string& airline)
255 double vTaxi = ac->getPerformance()->vTaxi();
256 double vTaxiBackward = vTaxi * (-2.0/3.0);
257 double vTaxiReduced = vTaxi * (2.0/3.0);
261 dep->getDynamics()->getParking(-1, &lat, &lon, &heading);
266 waypoint *wpt = new waypoint;
269 wpt->longitude = lon;
270 wpt->altitude = dep->getElevation();
271 wpt->speed = vTaxiBackward;
272 wpt->crossat = -10000;
273 wpt->gear_down = true;
274 wpt->flaps_down= true;
275 wpt->finished = false;
276 wpt->on_ground = true;
278 waypoints.push_back(wpt);
280 geo_direct_wgs_84 ( 0, lat, lon, heading,
282 &lat2, &lon2, &az2 );
285 wpt->latitude = lat2;
286 wpt->longitude = lon2;
287 wpt->altitude = dep->getElevation();
288 wpt->speed = vTaxiBackward;
289 wpt->crossat = -10000;
290 wpt->gear_down = true;
291 wpt->flaps_down= true;
292 wpt->finished = false;
293 wpt->on_ground = true;
295 waypoints.push_back(wpt);
296 geo_direct_wgs_84 ( 0, lat, lon, heading,
298 &lat2, &lon2, &az2 );
300 wpt->name = "taxiStart";
301 wpt->latitude = lat2;
302 wpt->longitude = lon2;
303 wpt->altitude = dep->getElevation();
304 wpt->speed = vTaxiReduced;
305 wpt->crossat = -10000;
306 wpt->gear_down = true;
307 wpt->flaps_down= true;
308 wpt->finished = false;
309 wpt->on_ground = true;
311 waypoints.push_back(wpt);