]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIFlightPlanCreatePushBack.cxx
Merge branch 'next' into durk-atc
[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                     //FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
78                     waypoint *wpt;
79                     wpt = new waypoint;
80                     wpt->name      = string(buffer); // fixme: should be the name of the taxiway
81                     wpt->latitude  = lat;
82                     wpt->longitude = lon;
83                     // Elevation is currently disregarded when on_ground is true
84                     // because the AIModel obtains a periodic ground elevation estimate.
85                    wpt->altitude  = dep->getElevation();
86                    wpt->speed = vTaxiBackward;
87                    wpt->crossat   = -10000;
88                    wpt->gear_down = true;
89                    wpt->flaps_down= true;
90                    wpt->finished  = false;
91                    wpt->on_ground = true;
92                    wpt->routeIndex = -1;
93                    waypoints.push_back(wpt);
94             }
95            //cerr << "Success : GateId = " << gateId << endl;
96            SG_LOG(SG_INPUT, SG_WARN, "Warning: Succesfully found a parking for a " << 
97                                               aircraftType <<
98                                               " of flight type " << fltType << 
99                                               " of airline     " << airline <<
100                                               " at airport     " << dep->getId());
101         } else {
102             //cerr << "Push Back follow-up Flight" << endl;
103             dep->getDynamics()->getParking(gateId, &lat, &lon, &heading);
104         }
105         if (gateId < 0) {
106              createPushBackFallBack(ac, firstFlight, dep, latitude, longitude,
107                                     radius, fltType, aircraftType, airline);
108              return true;
109
110         }
111         //cerr << "getting parking " << gateId;
112         //cerr << " for a " << 
113         //                                      aircraftType <<
114         //                                      " of flight type " << fltType << 
115         //                                      " of airline     " << airline <<
116         //                                      " at airport     " << dep->getId() << endl;
117         FGParking *parking = dep->getDynamics()->getParking(gateId);
118         int pushBackNode = parking->getPushBackPoint();
119
120
121         pushBackRoute = parking->getPushBackRoute();
122         if ((pushBackNode > 0) && (pushBackRoute == 0)) {
123             int node, rte;
124             FGTaxiRoute route;
125             //cerr << "Creating push-back for " << gateId << " (" << parking->getName() << ") using push-back point " << pushBackNode << endl;
126             route = dep->getDynamics()->getGroundNetwork()->findShortestRoute(gateId, pushBackNode, false);
127             parking->setPushBackRoute(new FGTaxiRoute(route));
128             
129
130             pushBackRoute = parking->getPushBackRoute();
131             int size = pushBackRoute->size();
132             if (size < 2) {
133                 SG_LOG(SG_GENERAL, SG_WARN, "Push back route from gate " << gateId << " has only " << size << " nodes.");
134                 SG_LOG(SG_GENERAL, SG_WARN, "Using  " << pushBackNode);
135             }
136             pushBackRoute->first();
137             waypoint *wpt;
138             while(pushBackRoute->next(&node, &rte))
139               {
140                 //FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findSegment(node)->getEnd();
141                 char buffer[10];
142                 snprintf (buffer, 10, "%d", node);
143                 FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(node);
144                 //ids.pop_back();  
145                 wpt = new waypoint;
146                 wpt->name      = string(buffer); // fixme: should be the name of the taxiway
147                 wpt->latitude  = tn->getLatitude();
148                 wpt->longitude = tn->getLongitude();
149                 // Elevation is currently disregarded when on_ground is true
150                 // because the AIModel obtains a periodic ground elevation estimate.
151                 wpt->altitude  = dep->getElevation();
152                 wpt->speed = vTaxiBackward;
153                 wpt->crossat   = -10000;
154                 wpt->gear_down = true;
155                 wpt->flaps_down= true;
156                 wpt->finished  = false;
157                 wpt->on_ground = true;
158                 wpt->routeIndex = rte;
159                 waypoints.push_back(wpt);
160               }
161               // some special considerations for the last point:
162               wpt->name = string("PushBackPoint");
163               wpt->speed = vTaxi;
164               //for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end(); i++) {
165               //    cerr << "Waypoint Name: " << (*i)->name << endl;
166               //}
167         } else {
168            //cerr << "Creating direct forward departure route fragment" << endl;
169            double lat2 = 0.0, lon2 = 0.0, az2 = 0.0;
170            waypoint *wpt;
171            geo_direct_wgs_84 ( 0, lat, lon, heading, 
172                                2, &lat2, &lon2, &az2 );
173            wpt = new waypoint;
174            wpt->name      = "park2";
175            wpt->latitude  = lat2;
176            wpt->longitude = lon2;
177            wpt->altitude  = dep->getElevation();
178            wpt->speed     = vTaxiReduced; 
179            wpt->crossat   = -10000;
180            wpt->gear_down = true;
181            wpt->flaps_down= true;
182            wpt->finished  = false;
183            wpt->on_ground = true;
184            wpt->routeIndex = 0;
185            waypoints.push_back(wpt); 
186
187            geo_direct_wgs_84 ( 0, lat, lon, heading, 
188                                4, &lat2, &lon2, &az2 );
189            wpt = new waypoint;
190            wpt->name      = "name";
191            wpt->latitude  = lat2;
192            wpt->longitude = lon2;
193            wpt->altitude  = dep->getElevation();
194            wpt->speed     = vTaxiReduced; 
195            wpt->crossat   = -10000;
196            wpt->gear_down = true;
197            wpt->flaps_down= true;
198            wpt->finished  = false;
199            wpt->on_ground = true;
200            wpt->routeIndex = 0;
201            waypoints.push_back(wpt);
202
203            //cerr << "Creating final push forward point for gate " << gateId << endl;
204            FGTaxiNode *tn = dep->getDynamics()->getGroundNetwork()->findNode(gateId);
205            FGTaxiSegmentVectorIterator ts = tn->getBeginRoute();
206            FGTaxiSegmentVectorIterator te = tn->getEndRoute();
207            if (ts == te) {
208                SG_LOG(SG_GENERAL, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
209                //exit(1);
210            }
211            tn = (*ts)->getEnd();
212            lastNodeVisited = tn->getIndex();
213            if (tn == NULL) {
214                SG_LOG(SG_GENERAL, SG_ALERT, "No valid taxinode found");
215                exit(1);
216            }
217            wpt = new waypoint;
218            wpt->name      = "PushBackPoint";
219            wpt->latitude  = tn->getLatitude();
220            wpt->longitude = tn->getLongitude();
221            wpt->altitude  = dep->getElevation();
222            wpt->speed     = vTaxiReduced; 
223            wpt->crossat   = -10000;
224            wpt->gear_down = true;
225            wpt->flaps_down= true;
226            wpt->finished  = false;
227            wpt->on_ground = true;
228            wpt->routeIndex = (*ts)->getIndex();
229            waypoints.push_back(wpt);
230
231
232         }
233
234     }
235     return true;
236 }
237 /*******************************************************************
238  * createPushBackFallBack
239  * This is the backup function for airports that don't have a 
240  * network yet. 
241  ******************************************************************/
242 void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep, 
243                                     double latitude,
244                                     double longitude,
245                                     double radius,
246                                     const string& fltType,
247                                     const string& aircraftType,
248                                     const string& airline)
249 {
250   double heading;
251   double lat;
252   double lon;
253   double lat2 = 0.0;
254   double lon2 = 0.0;
255   double az2 = 0.0;
256
257   double vTaxi = ac->getPerformance()->vTaxi();
258   double vTaxiBackward = vTaxi * (-2.0/3.0);
259   double vTaxiReduced  = vTaxi * (2.0/3.0);
260
261
262
263   dep->getDynamics()->getParking(-1, &lat, &lon, &heading);
264
265   heading += 180.0;
266   if (heading > 360)
267     heading -= 360;
268   waypoint *wpt = new waypoint;
269   wpt->name      = "park";
270   wpt->latitude  = lat;
271   wpt->longitude = lon;
272   wpt->altitude  = dep->getElevation();
273   wpt->speed     = vTaxiBackward; 
274   wpt->crossat   = -10000;
275   wpt->gear_down = true;
276   wpt->flaps_down= true;
277   wpt->finished  = false;
278   wpt->on_ground = true;
279
280   waypoints.push_back(wpt); 
281
282   geo_direct_wgs_84 ( 0, lat, lon, heading, 
283                       10, 
284                       &lat2, &lon2, &az2 );
285   wpt = new waypoint;
286   wpt->name      = "park2";
287   wpt->latitude  = lat2;
288   wpt->longitude = lon2;
289   wpt->altitude  = dep->getElevation();
290   wpt->speed     = vTaxiBackward; 
291   wpt->crossat   = -10000;
292   wpt->gear_down = true;
293   wpt->flaps_down= true;
294   wpt->finished  = false;
295   wpt->on_ground = true;
296   wpt->routeIndex = 0;
297   waypoints.push_back(wpt); 
298   geo_direct_wgs_84 ( 0, lat, lon, heading, 
299                       2.2*radius,           
300                       &lat2, &lon2, &az2 );
301   wpt = new waypoint;
302   wpt->name      = "taxiStart";
303   wpt->latitude  = lat2;
304   wpt->longitude = lon2;
305   wpt->altitude  = dep->getElevation();
306   wpt->speed     = vTaxiReduced; 
307   wpt->crossat   = -10000;
308   wpt->gear_down = true;
309   wpt->flaps_down= true;
310   wpt->finished  = false;
311   wpt->on_ground = true;
312   wpt->routeIndex = 0;
313   waypoints.push_back(wpt);
314 }