]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIFlightPlanCreatePushBack.cxx
Move some code from the header file to the source file.
[flightgear.git] / src / AIModel / AIFlightPlanCreatePushBack.cxx
1 /****************************************************************************
2 * AIFlightPlanCreatePushBack.cxx
3 * Written by Durk Talsma, started August 1, 2007.
4 *
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.
9 *
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.
14 *
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.
18 *
19 **************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/math/sg_geodesy.hxx>
26
27 #include <Airports/simple.hxx>
28 #include <Airports/runways.hxx>
29 #include <Airports/dynamics.hxx>
30
31 #include <Environment/environment_mgr.hxx>
32 #include <Environment/environment.hxx>
33
34 #include "AIFlightPlan.hxx"
35 #include "AIAircraft.hxx"
36 #include "performancedata.hxx"
37
38
39 // TODO: Use James Turner's createOnGround functions.
40 bool FGAIFlightPlan::createPushBack(FGAIAircraft *ac,
41                                     bool firstFlight, FGAirport *dep,
42                                     double latitude,
43                                     double longitude,
44                                     double radius,
45                                     const string& fltType,
46                                     const string& aircraftType,
47                                     const string& airline)
48 {
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
55     // must be reset.
56     activeRunway.clear();
57
58     if (!(dep->getDynamics()->getGroundNetwork()->exists())) {
59         //cerr << "Push Back fallback" << endl;
60         createPushBackFallBack(ac, firstFlight, dep, latitude, longitude,
61                                radius, fltType, aircraftType, airline);
62     } else {
63         if (firstFlight) {
64
65             if (!(dep->getDynamics()->getAvailableParking(&lat, &lon,
66                     &heading, &gateId,
67                     radius, fltType,
68                     aircraftType, airline))) {
69                 SG_LOG(SG_INPUT, SG_WARN, "Warning: Could not find parking for a " <<
70                        aircraftType <<
71                        " of flight type " << fltType <<
72                        " of airline     " << airline <<
73                        " at airport     " << dep->getId());
74                 return false;
75                 char buffer[10];
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);
82             }
83             //cerr << "Success : GateId = " << gateId << endl;
84             SG_LOG(SG_INPUT, SG_WARN, "Warning: Succesfully found a parking for a " <<
85                    aircraftType <<
86                    " of flight type " << fltType <<
87                    " of airline     " << airline <<
88                    " at airport     " << dep->getId());
89         } else {
90             //cerr << "Push Back follow-up Flight" << endl;
91             dep->getDynamics()->getParking(gateId, &lat, &lon, &heading);
92         }
93         if (gateId < 0) {
94             createPushBackFallBack(ac, firstFlight, dep, latitude, longitude,
95                                    radius, fltType, aircraftType, airline);
96             return true;
97
98         }
99         //cerr << "getting parking " << gateId;
100         //cerr << " for a " <<
101         //                                      aircraftType <<
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();
107
108
109         pushBackRoute = parking->getPushBackRoute();
110         if ((pushBackNode > 0) && (pushBackRoute == 0)) {  // Load the already established route for this gate
111             int node, rte;
112             FGTaxiRoute route;
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));
116
117
118             pushBackRoute = parking->getPushBackRoute();
119             int size = pushBackRoute->size();
120             if (size < 2) {
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);
123             }
124             pushBackRoute->first();
125             while (pushBackRoute->next(&node, &rte))
126             {
127                 //FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findSegment(node)->getEnd();
128                 char buffer[10];
129                 snprintf (buffer, 10, "%d", node);
130                 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
131                 //ids.pop_back();
132                 //wpt = new waypoint;
133                 SGGeod coord = coord.fromDeg(tn->getLongitude(), tn->getLatitude());
134                 FGAIWaypoint *wpt = createOnGround(ac, string(buffer), coord, dep->getElevation(), vTaxiBackward);
135
136                 wpt->setRouteIndex(rte);
137                 pushBackWaypoint(wpt);
138             }
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;
146
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
153             if (ts == te) {
154                 SG_LOG(SG_GENERAL, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
155                 parking->setAvailable(false);
156                 return false;
157             }
158             tn = (*ts)->getEnd();
159             lastNodeVisited = tn->getIndex();
160             if (tn == NULL) {
161                 SG_LOG(SG_GENERAL, SG_ALERT, "No valid taxinode found");
162                 exit(1);
163             }
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();
168
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 );
172                 char buffer[16];
173                 snprintf(buffer, 16, "pushback-%02d", i);
174                 SGGeod coord = coord.fromDeg(lon2, lat2);
175                 //cerr << i << endl;
176                 FGAIWaypoint *wpt = createOnGround(ac, string(buffer), coord, dep->getElevation(), vTaxiReduced);
177
178                 wpt->setRouteIndex((*ts)->getIndex());
179                 pushBackWaypoint(wpt);
180             }
181             // cerr << "Done " << endl;
182             waypoints.back()->setName(string("PushBackPoint"));
183             // cerr << "Done assinging new name" << endl;
184         }
185     }
186     return true;
187 }
188 /*******************************************************************
189 * createPushBackFallBack
190 * This is the backup function for airports that don't have a
191 * network yet.
192 ******************************************************************/
193 void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep,
194         double latitude,
195         double longitude,
196         double radius,
197         const string& fltType,
198         const string& aircraftType,
199         const string& airline)
200 {
201     double heading;
202     double lat;
203     double lon;
204     double lat2 = 0.0;
205     double lon2 = 0.0;
206     double az2 = 0.0;
207
208     double vTaxi = ac->getPerformance()->vTaxi();
209     double vTaxiBackward = vTaxi * (-2.0/3.0);
210     double vTaxiReduced  = vTaxi * (2.0/3.0);
211
212
213
214     dep->getDynamics()->getParking(-1, &lat, &lon, &heading);
215
216     heading += 180.0;
217     if (heading > 360)
218         heading -= 360;
219
220     SGGeod coord = coord.fromDeg(lon, lat);
221     FGAIWaypoint *wpt = createOnGround(ac, string("park"), coord, dep->getElevation(), vTaxiBackward);
222
223     pushBackWaypoint(wpt);
224
225     geo_direct_wgs_84 ( 0, lat, lon, heading,
226                         10,
227                         &lat2, &lon2, &az2 );
228     coord = coord.fromDeg(lon2, lat2);
229     wpt = createOnGround(ac, string("park2"), coord, dep->getElevation(), vTaxiBackward);
230
231     pushBackWaypoint(wpt);
232
233     geo_direct_wgs_84 ( 0, lat, lon, heading,
234                         2.2*radius,
235                         &lat2, &lon2, &az2 );
236     coord = coord.fromDeg(lon2, lat2);
237     wpt = createOnGround(ac, string("taxiStart"), coord, dep->getElevation(), vTaxiReduced);
238     pushBackWaypoint(wpt);
239
240 }