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");
145 //cerr << "Creating direct forward departure route fragment" << endl;
146 double lat2, lon2, az2;
148 geo_direct_wgs_84 ( 0, lat, lon, heading,
149 2, &lat2, &lon2, &az2 );
152 wpt->latitude = lat2;
153 wpt->longitude = lon2;
154 wpt->altitude = dep->getElevation();
156 wpt->crossat = -10000;
157 wpt->gear_down = true;
158 wpt->flaps_down= true;
159 wpt->finished = false;
160 wpt->on_ground = true;
162 waypoints.push_back(wpt);
164 geo_direct_wgs_84 ( 0, lat, lon, heading,
165 4, &lat2, &lon2, &az2 );
168 wpt->latitude = lat2;
169 wpt->longitude = lon2;
170 wpt->altitude = dep->getElevation();
172 wpt->crossat = -10000;
173 wpt->gear_down = true;
174 wpt->flaps_down= true;
175 wpt->finished = false;
176 wpt->on_ground = true;
178 waypoints.push_back(wpt);
180 //cerr << "Creating final push forward point for gate " << gateId << endl;
181 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(gateId);
182 FGTaxiSegmentVectorIterator ts = tn->getBeginRoute();
183 FGTaxiSegmentVectorIterator te = tn->getEndRoute();
185 SG_LOG(SG_GENERAL, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
188 tn = (*ts)->getEnd();
189 lastNodeVisited = tn->getIndex();
191 SG_LOG(SG_GENERAL, SG_ALERT, "No valid taxinode found");
195 wpt->name = "PushBackPoint";
196 wpt->latitude = tn->getLatitude();
197 wpt->longitude = tn->getLongitude();
198 wpt->altitude = dep->getElevation();
200 wpt->crossat = -10000;
201 wpt->gear_down = true;
202 wpt->flaps_down= true;
203 wpt->finished = false;
204 wpt->on_ground = true;
205 wpt->routeIndex = (*ts)->getIndex();
206 waypoints.push_back(wpt);
213 /*******************************************************************
214 * createPushBackFallBack
215 * This is the backup function for airports that don't have a
217 ******************************************************************/
218 void FGAIFlightPlan::createPushBackFallBack(bool firstFlight, FGAirport *dep,
222 const string& fltType,
223 const string& aircraftType,
224 const string& airline)
235 dep->getDynamics()->getParking(-1, &lat, &lon, &heading);
240 waypoint *wpt = new waypoint;
243 wpt->longitude = lon;
244 wpt->altitude = dep->getElevation();
246 wpt->crossat = -10000;
247 wpt->gear_down = true;
248 wpt->flaps_down= true;
249 wpt->finished = false;
250 wpt->on_ground = true;
252 waypoints.push_back(wpt);
254 geo_direct_wgs_84 ( 0, lat, lon, heading,
256 &lat2, &lon2, &az2 );
259 wpt->latitude = lat2;
260 wpt->longitude = lon2;
261 wpt->altitude = dep->getElevation();
263 wpt->crossat = -10000;
264 wpt->gear_down = true;
265 wpt->flaps_down= true;
266 wpt->finished = false;
267 wpt->on_ground = true;
269 waypoints.push_back(wpt);
270 geo_direct_wgs_84 ( 0, lat, lon, heading,
272 &lat2, &lon2, &az2 );
274 wpt->name = "taxiStart";
275 wpt->latitude = lat2;
276 wpt->longitude = lon2;
277 wpt->altitude = dep->getElevation();
279 wpt->crossat = -10000;
280 wpt->gear_down = true;
281 wpt->flaps_down= true;
282 wpt->finished = false;
283 wpt->on_ground = true;
285 waypoints.push_back(wpt);