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 **************************************************************************/
27 #include <simgear/math/sg_geodesy.hxx>
29 #include <Airports/simple.hxx>
30 #include <Airports/runways.hxx>
31 #include <Airports/dynamics.hxx>
33 #include <Environment/environment_mgr.hxx>
34 #include <Environment/environment.hxx>
36 #include "AIFlightPlan.hxx"
37 #include "AIAircraft.hxx"
38 #include "performancedata.hxx"
41 // TODO: Use James Turner's createOnGround functions.
42 bool FGAIFlightPlan::createPushBack(FGAIAircraft *ac,
43 bool firstFlight, FGAirport *dep,
47 const string& fltType,
48 const string& aircraftType,
49 const string& airline)
51 double lat, lon, heading;
52 double vTaxi = ac->getPerformance()->vTaxi();
53 double vTaxiBackward = vTaxi * (-2.0/3.0);
54 double vTaxiReduced = vTaxi * (2.0/3.0);
55 FGTaxiRoute *pushBackRoute;
56 // Active runway can be conditionally set by ATC, so at the start of a new flight, this
60 if (!(dep->getDynamics()->getGroundNetwork()->exists())) {
61 //cerr << "Push Back fallback" << endl;
62 createPushBackFallBack(ac, firstFlight, dep, latitude, longitude,
63 radius, fltType, aircraftType, airline);
67 if (!(dep->getDynamics()->getAvailableParking(&lat, &lon,
70 aircraftType, airline))) {
71 SG_LOG(SG_AI, SG_WARN, "Warning: Could not find parking for a " <<
73 " of flight type " << fltType <<
74 " of airline " << airline <<
75 " at airport " << dep->getId());
78 snprintf (buffer, 10, "%d", gateId);
79 SGGeod coord = coord.fromDeg(lon, lat);
80 //FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
81 FGAIWaypoint *wpt = createOnGround(ac, string(buffer), coord, dep->getElevation(), vTaxiBackward);
82 wpt->setRouteIndex(-1);
83 pushBackWaypoint(wpt);
85 //cerr << "Success : GateId = " << gateId << endl;
86 SG_LOG(SG_AI, SG_WARN, "Warning: Succesfully found a parking for a " <<
88 " of flight type " << fltType <<
89 " of airline " << airline <<
90 " at airport " << dep->getId());
92 //cerr << "Push Back follow-up Flight" << endl;
93 dep->getDynamics()->getParking(gateId, &lat, &lon, &heading);
96 createPushBackFallBack(ac, firstFlight, dep, latitude, longitude,
97 radius, fltType, aircraftType, airline);
101 //cerr << "getting parking " << gateId;
102 //cerr << " for a " <<
104 // " of flight type " << fltType <<
105 // " of airline " << airline <<
106 // " at airport " << dep->getId() << endl;
107 FGParking *parking = dep->getDynamics()->getParking(gateId);
108 int pushBackNode = parking->getPushBackPoint();
111 pushBackRoute = parking->getPushBackRoute();
112 if ((pushBackNode > 0) && (pushBackRoute == 0)) { // Load the already established route for this gate
115 //cerr << "Creating push-back for " << gateId << " (" << parking->getName() << ") using push-back point " << pushBackNode << endl;
116 route = dep->getDynamics()->getGroundNetwork()->findShortestRoute(gateId, pushBackNode, false);
117 parking->setPushBackRoute(new FGTaxiRoute(route));
120 pushBackRoute = parking->getPushBackRoute();
121 int size = pushBackRoute->size();
123 SG_LOG(SG_AI, SG_ALERT, "Push back route from gate " << gateId << " has only " << size << " nodes.");
124 SG_LOG(SG_AI, SG_ALERT, "Using " << pushBackNode);
126 pushBackRoute->first();
127 while (pushBackRoute->next(&node, &rte))
129 //FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findSegment(node)->getEnd();
131 snprintf (buffer, 10, "%d", node);
132 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
134 //wpt = new waypoint;
135 SGGeod coord = coord.fromDeg(tn->getLongitude(), tn->getLatitude());
136 FGAIWaypoint *wpt = createOnGround(ac, string(buffer), coord, dep->getElevation(), vTaxiBackward);
138 wpt->setRouteIndex(rte);
139 pushBackWaypoint(wpt);
141 // some special considerations for the last point:
142 waypoints.back()->setName(string("PushBackPoint"));
143 waypoints.back()->setSpeed(vTaxi);
144 ac->setTaxiClearanceRequest(true);
145 } else { // In case of a push forward departure...
146 ac->setTaxiClearanceRequest(false);
147 double lat2 = 0.0, lon2 = 0.0, az2 = 0.0;
149 //cerr << "Creating final push forward point for gate " << gateId << endl;
150 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(gateId);
151 FGTaxiSegmentVectorIterator ts = tn->getBeginRoute();
152 FGTaxiSegmentVectorIterator te = tn->getEndRoute();
153 // if the starting node equals the ending node, then there aren't any routes for this parking.
154 // in cases like these we should flag the gate as being inoperative and return false
156 SG_LOG(SG_AI, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
157 parking->setAvailable(false);
160 tn = (*ts)->getEnd();
161 lastNodeVisited = tn->getIndex();
163 SG_LOG(SG_AI, SG_ALERT, "No valid taxinode found");
166 double distance = (*ts)->getLength();
167 //cerr << "Length of push forward route = " << distance << " and heading is " << heading << endl;
168 lat2 = tn->getLatitude();
169 lon2 = tn->getLongitude();
171 for (int i = 1; i < 10; i++) {
172 geo_direct_wgs_84 ( 0, lat, lon, heading,
173 ((i / 10.0) * distance), &lat2, &lon2, &az2 );
175 snprintf(buffer, 16, "pushback-%02d", i);
176 SGGeod coord = coord.fromDeg(lon2, lat2);
178 FGAIWaypoint *wpt = createOnGround(ac, string(buffer), coord, dep->getElevation(), vTaxiReduced);
180 wpt->setRouteIndex((*ts)->getIndex());
181 pushBackWaypoint(wpt);
183 // cerr << "Done " << endl;
184 waypoints.back()->setName(string("PushBackPoint"));
185 // cerr << "Done assinging new name" << endl;
190 /*******************************************************************
191 * createPushBackFallBack
192 * This is the backup function for airports that don't have a
194 ******************************************************************/
195 void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep,
199 const string& fltType,
200 const string& aircraftType,
201 const string& airline)
210 double vTaxi = ac->getPerformance()->vTaxi();
211 double vTaxiBackward = vTaxi * (-2.0/3.0);
212 double vTaxiReduced = vTaxi * (2.0/3.0);
216 dep->getDynamics()->getParking(-1, &lat, &lon, &heading);
222 SGGeod coord = coord.fromDeg(lon, lat);
223 FGAIWaypoint *wpt = createOnGround(ac, string("park"), coord, dep->getElevation(), vTaxiBackward);
225 pushBackWaypoint(wpt);
227 geo_direct_wgs_84 ( 0, lat, lon, heading,
229 &lat2, &lon2, &az2 );
230 coord = coord.fromDeg(lon2, lat2);
231 wpt = createOnGround(ac, string("park2"), coord, dep->getElevation(), vTaxiBackward);
233 pushBackWaypoint(wpt);
235 geo_direct_wgs_84 ( 0, lat, lon, heading,
237 &lat2, &lon2, &az2 );
238 coord = coord.fromDeg(lon2, lat2);
239 wpt = createOnGround(ac, string("taxiStart"), coord, dep->getElevation(), vTaxiReduced);
240 pushBackWaypoint(wpt);