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 SGGeod coord = coord.fromDeg(lon, lat);
78 //FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
79 FGAIWaypoint *wpt = createOnGround(ac, string(buffer), coord, dep->getElevation(), vTaxiBackward);
80 wpt->setRouteIndex(-1);
81 pushBackWaypoint(wpt);
83 //cerr << "Success : GateId = " << gateId << endl;
84 SG_LOG(SG_INPUT, SG_WARN, "Warning: Succesfully found a parking for a " <<
86 " of flight type " << fltType <<
87 " of airline " << airline <<
88 " at airport " << dep->getId());
90 //cerr << "Push Back follow-up Flight" << endl;
91 dep->getDynamics()->getParking(gateId, &lat, &lon, &heading);
94 createPushBackFallBack(ac, firstFlight, dep, latitude, longitude,
95 radius, fltType, aircraftType, airline);
99 //cerr << "getting parking " << gateId;
100 //cerr << " for a " <<
102 // " of flight type " << fltType <<
103 // " of airline " << airline <<
104 // " at airport " << dep->getId() << endl;
105 FGParking *parking = dep->getDynamics()->getParking(gateId);
106 int pushBackNode = parking->getPushBackPoint();
109 pushBackRoute = parking->getPushBackRoute();
110 if ((pushBackNode > 0) && (pushBackRoute == 0)) { // Load the already established route for this gate
113 //cerr << "Creating push-back for " << gateId << " (" << parking->getName() << ") using push-back point " << pushBackNode << endl;
114 route = dep->getDynamics()->getGroundNetwork()->findShortestRoute(gateId, pushBackNode, false);
115 parking->setPushBackRoute(new FGTaxiRoute(route));
118 pushBackRoute = parking->getPushBackRoute();
119 int size = pushBackRoute->size();
121 SG_LOG(SG_GENERAL, SG_WARN, "Push back route from gate " << gateId << " has only " << size << " nodes.");
122 SG_LOG(SG_GENERAL, SG_WARN, "Using " << pushBackNode);
124 pushBackRoute->first();
125 while (pushBackRoute->next(&node, &rte))
127 //FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findSegment(node)->getEnd();
129 snprintf (buffer, 10, "%d", node);
130 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
132 //wpt = new waypoint;
133 SGGeod coord = coord.fromDeg(tn->getLongitude(), tn->getLatitude());
134 FGAIWaypoint *wpt = createOnGround(ac, string(buffer), coord, dep->getElevation(), vTaxiBackward);
136 wpt->setRouteIndex(rte);
137 pushBackWaypoint(wpt);
139 // some special considerations for the last point:
140 waypoints.back()->setName(string("PushBackPoint"));
141 waypoints.back()->setSpeed(vTaxi);
142 ac->setTaxiClearanceRequest(true);
143 } else { // In case of a push forward departure...
144 ac->setTaxiClearanceRequest(false);
145 double lat2 = 0.0, lon2 = 0.0, az2 = 0.0;
147 //cerr << "Creating final push forward point for gate " << gateId << endl;
148 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(gateId);
149 FGTaxiSegmentVectorIterator ts = tn->getBeginRoute();
150 FGTaxiSegmentVectorIterator te = tn->getEndRoute();
151 // if the starting node equals the ending node, then there aren't any routes for this parking.
152 // in cases like these we should flag the gate as being inoperative and return false
154 SG_LOG(SG_GENERAL, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
155 parking->setAvailable(false);
158 tn = (*ts)->getEnd();
159 lastNodeVisited = tn->getIndex();
161 SG_LOG(SG_GENERAL, SG_ALERT, "No valid taxinode found");
164 double distance = (*ts)->getLength();
165 //cerr << "Length of push forward route = " << distance << " and heading is " << heading << endl;
166 lat2 = tn->getLatitude();
167 lon2 = tn->getLongitude();
169 for (int i = 1; i < 10; i++) {
170 geo_direct_wgs_84 ( 0, lat, lon, heading,
171 ((i / 10.0) * distance), &lat2, &lon2, &az2 );
173 snprintf(buffer, 16, "pushback-%02d", i);
174 SGGeod coord = coord.fromDeg(lon2, lat2);
176 FGAIWaypoint *wpt = createOnGround(ac, string(buffer), coord, dep->getElevation(), vTaxiReduced);
178 wpt->setRouteIndex((*ts)->getIndex());
179 pushBackWaypoint(wpt);
181 // cerr << "Done " << endl;
182 waypoints.back()->setName(string("PushBackPoint"));
183 // cerr << "Done assinging new name" << endl;
188 /*******************************************************************
189 * createPushBackFallBack
190 * This is the backup function for airports that don't have a
192 ******************************************************************/
193 void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep,
197 const string& fltType,
198 const string& aircraftType,
199 const string& airline)
208 double vTaxi = ac->getPerformance()->vTaxi();
209 double vTaxiBackward = vTaxi * (-2.0/3.0);
210 double vTaxiReduced = vTaxi * (2.0/3.0);
214 dep->getDynamics()->getParking(-1, &lat, &lon, &heading);
220 SGGeod coord = coord.fromDeg(lon, lat);
221 FGAIWaypoint *wpt = createOnGround(ac, string("park"), coord, dep->getElevation(), vTaxiBackward);
223 pushBackWaypoint(wpt);
225 geo_direct_wgs_84 ( 0, lat, lon, heading,
227 &lat2, &lon2, &az2 );
228 coord = coord.fromDeg(lon2, lat2);
229 wpt = createOnGround(ac, string("park2"), coord, dep->getElevation(), vTaxiBackward);
231 pushBackWaypoint(wpt);
233 geo_direct_wgs_84 ( 0, lat, lon, heading,
235 &lat2, &lon2, &az2 );
236 coord = coord.fromDeg(lon2, lat2);
237 wpt = createOnGround(ac, string("taxiStart"), coord, dep->getElevation(), vTaxiReduced);
238 pushBackWaypoint(wpt);