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 **************************************************************************/
20 #include "AIFlightPlan.hxx"
21 #include <simgear/math/sg_geodesy.hxx>
22 #include <Airports/runways.hxx>
23 #include <Airports/dynamics.hxx>
25 #include <Environment/environment_mgr.hxx>
26 #include <Environment/environment.hxx>
29 void FGAIFlightPlan::createPushBack(bool firstFlight, FGAirport *dep,
33 const string& fltType,
34 const string& aircraftType,
35 const string& airline)
37 double lat, lon, heading;
38 FGTaxiRoute *pushBackRoute;
39 if (!(dep->getDynamics()->getGroundNetwork()->exists())) {
40 //cerr << "Push Back fallback" << endl;
41 createPushBackFallBack(firstFlight, dep, latitude, longitude,
42 radius, fltType, aircraftType, airline);
46 if (!(dep->getDynamics()->getAvailableParking(&lat, &lon,
49 aircraftType, airline))) {
50 SG_LOG(SG_INPUT, SG_WARN, "Warning: Could not find parking for a " <<
52 " of flight type " << fltType <<
53 " of airline " << airline <<
54 " at airport " << dep->getId());
56 snprintf (buffer, 10, "%d", gateId);
57 //FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
60 wpt->name = string(buffer); // fixme: should be the name of the taxiway
63 // Elevation is currently disregarded when on_ground is true
64 // because the AIModel obtains a periodic ground elevation estimate.
65 wpt->altitude = dep->getElevation();
67 wpt->crossat = -10000;
68 wpt->gear_down = true;
69 wpt->flaps_down= true;
70 wpt->finished = false;
71 wpt->on_ground = true;
73 waypoints.push_back(wpt);
75 //cerr << "Success : GateId = " << gateId << endl;
76 SG_LOG(SG_INPUT, SG_WARN, "Warning: Succesfully found a parking for a " <<
78 " of flight type " << fltType <<
79 " of airline " << airline <<
80 " at airport " << dep->getId());
82 //cerr << "Push Back follow-up Flight" << endl;
83 dep->getDynamics()->getParking(gateId, &lat, &lon, &heading);
86 createPushBackFallBack(firstFlight, dep, latitude, longitude,
87 radius, fltType, aircraftType, airline);
91 //cerr << "getting parking " << gateId;
92 //cerr << " for a " <<
94 // " of flight type " << fltType <<
95 // " of airline " << airline <<
96 // " at airport " << dep->getId() << endl;
97 FGParking *parking = dep->getDynamics()->getParking(gateId);
98 int pushBackNode = parking->getPushBackPoint();
101 pushBackRoute = parking->getPushBackRoute();
102 if ((pushBackNode > 0) && (pushBackRoute == 0)) {
105 //cerr << "Creating push-back for " << gateId << " (" << parking->getName() << ") using push-back point " << pushBackNode << endl;
106 route = dep->getDynamics()->getGroundNetwork()->findShortestRoute(gateId, pushBackNode, false);
107 parking->setPushBackRoute(new FGTaxiRoute(route));
110 pushBackRoute = parking->getPushBackRoute();
111 int size = pushBackRoute->size();
113 SG_LOG(SG_GENERAL, SG_WARN, "Push back route from gate " << gateId << " has only " << size << " nodes.");
114 SG_LOG(SG_GENERAL, SG_WARN, "Using " << pushBackNode);
116 pushBackRoute->first();
118 while(pushBackRoute->next(&node, &rte))
120 //FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findSegment(node)->getEnd();
122 snprintf (buffer, 10, "%d", node);
123 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
126 wpt->name = string(buffer); // fixme: should be the name of the taxiway
127 wpt->latitude = tn->getLatitude();
128 wpt->longitude = tn->getLongitude();
129 // Elevation is currently disregarded when on_ground is true
130 // because the AIModel obtains a periodic ground elevation estimate.
131 wpt->altitude = dep->getElevation();
133 wpt->crossat = -10000;
134 wpt->gear_down = true;
135 wpt->flaps_down= true;
136 wpt->finished = false;
137 wpt->on_ground = true;
138 wpt->routeIndex = rte;
139 waypoints.push_back(wpt);
141 // some special considerations for the last point:
142 wpt->name = string("PushBackPoint");
144 //for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end(); i++) {
145 // cerr << "Waypoint Name: " << (*i)->name << endl;
148 //cerr << "Creating direct forward departure route fragment" << endl;
149 double lat2, lon2, az2;
151 geo_direct_wgs_84 ( 0, lat, lon, heading,
152 2, &lat2, &lon2, &az2 );
155 wpt->latitude = lat2;
156 wpt->longitude = lon2;
157 wpt->altitude = dep->getElevation();
159 wpt->crossat = -10000;
160 wpt->gear_down = true;
161 wpt->flaps_down= true;
162 wpt->finished = false;
163 wpt->on_ground = true;
165 waypoints.push_back(wpt);
167 geo_direct_wgs_84 ( 0, lat, lon, heading,
168 4, &lat2, &lon2, &az2 );
171 wpt->latitude = lat2;
172 wpt->longitude = lon2;
173 wpt->altitude = dep->getElevation();
175 wpt->crossat = -10000;
176 wpt->gear_down = true;
177 wpt->flaps_down= true;
178 wpt->finished = false;
179 wpt->on_ground = true;
181 waypoints.push_back(wpt);
183 //cerr << "Creating final push forward point for gate " << gateId << endl;
184 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(gateId);
185 FGTaxiSegmentVectorIterator ts = tn->getBeginRoute();
186 FGTaxiSegmentVectorIterator te = tn->getEndRoute();
188 SG_LOG(SG_GENERAL, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
191 tn = (*ts)->getEnd();
192 lastNodeVisited = tn->getIndex();
194 SG_LOG(SG_GENERAL, SG_ALERT, "No valid taxinode found");
198 wpt->name = "PushBackPoint";
199 wpt->latitude = tn->getLatitude();
200 wpt->longitude = tn->getLongitude();
201 wpt->altitude = dep->getElevation();
203 wpt->crossat = -10000;
204 wpt->gear_down = true;
205 wpt->flaps_down= true;
206 wpt->finished = false;
207 wpt->on_ground = true;
208 wpt->routeIndex = (*ts)->getIndex();
209 waypoints.push_back(wpt);
216 /*******************************************************************
217 * createPushBackFallBack
218 * This is the backup function for airports that don't have a
220 ******************************************************************/
221 void FGAIFlightPlan::createPushBackFallBack(bool firstFlight, FGAirport *dep,
225 const string& fltType,
226 const string& aircraftType,
227 const string& airline)
238 dep->getDynamics()->getParking(-1, &lat, &lon, &heading);
243 waypoint *wpt = new waypoint;
246 wpt->longitude = lon;
247 wpt->altitude = dep->getElevation();
249 wpt->crossat = -10000;
250 wpt->gear_down = true;
251 wpt->flaps_down= true;
252 wpt->finished = false;
253 wpt->on_ground = true;
255 waypoints.push_back(wpt);
257 geo_direct_wgs_84 ( 0, lat, lon, heading,
259 &lat2, &lon2, &az2 );
262 wpt->latitude = lat2;
263 wpt->longitude = lon2;
264 wpt->altitude = dep->getElevation();
266 wpt->crossat = -10000;
267 wpt->gear_down = true;
268 wpt->flaps_down= true;
269 wpt->finished = false;
270 wpt->on_ground = true;
272 waypoints.push_back(wpt);
273 geo_direct_wgs_84 ( 0, lat, lon, heading,
275 &lat2, &lon2, &az2 );
277 wpt->name = "taxiStart";
278 wpt->latitude = lat2;
279 wpt->longitude = lon2;
280 wpt->altitude = dep->getElevation();
282 wpt->crossat = -10000;
283 wpt->gear_down = true;
284 wpt->flaps_down= true;
285 wpt->finished = false;
286 wpt->on_ground = true;
288 waypoints.push_back(wpt);