]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIFlightPlanCreatePushBack.cxx
fix #416: reciprocal filter broken
[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                    waypoints.push_back(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                 waypoints.push_back(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 (ts == te) {
152                SG_LOG(SG_GENERAL, SG_ALERT, "Gate " << gateId << "doesn't seem to have routes associated with it.");
153                //exit(1);
154            }
155            tn = (*ts)->getEnd();
156            lastNodeVisited = tn->getIndex();
157            if (tn == NULL) {
158                SG_LOG(SG_GENERAL, SG_ALERT, "No valid taxinode found");
159                exit(1);
160            }
161            double distance = (*ts)->getLength();
162            //cerr << "Length of push forward route = " << distance << " and heading is " << heading << endl;
163            lat2 =  tn->getLatitude();
164            lon2 =  tn->getLongitude();
165
166            for (int i = 1; i < 10; i++) {
167                 geo_direct_wgs_84 ( 0, lat, lon, heading, 
168                                    ((i / 10.0) * distance), &lat2, &lon2, &az2 );
169                 char buffer[16];
170                 snprintf(buffer, 16, "pushback-%02d", i);
171                 SGGeod coord = coord.fromDeg(lon2, lat2);
172                 //cerr << i << endl;
173                 FGAIWaypoint *wpt = createOnGround(ac, string(buffer), coord, dep->getElevation(), vTaxiReduced);
174
175                 wpt->setRouteIndex((*ts)->getIndex());
176                 waypoints.push_back(wpt); 
177            }
178            // cerr << "Done " << endl;
179            waypoints.back()->setName(string("PushBackPoint"));
180            // cerr << "Done assinging new name" << endl;
181         }
182     }
183     return true;
184 }
185 /*******************************************************************
186  * createPushBackFallBack
187  * This is the backup function for airports that don't have a 
188  * network yet. 
189  ******************************************************************/
190 void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep, 
191                                     double latitude,
192                                     double longitude,
193                                     double radius,
194                                     const string& fltType,
195                                     const string& aircraftType,
196                                     const string& airline)
197 {
198   double heading;
199   double lat;
200   double lon;
201   double lat2 = 0.0;
202   double lon2 = 0.0;
203   double az2 = 0.0;
204
205   double vTaxi = ac->getPerformance()->vTaxi();
206   double vTaxiBackward = vTaxi * (-2.0/3.0);
207   double vTaxiReduced  = vTaxi * (2.0/3.0);
208
209
210
211   dep->getDynamics()->getParking(-1, &lat, &lon, &heading);
212
213   heading += 180.0;
214   if (heading > 360)
215         heading -= 360;
216
217   SGGeod coord = coord.fromDeg(lon, lat);
218   FGAIWaypoint *wpt = createOnGround(ac, string("park"), coord, dep->getElevation(), vTaxiBackward);
219
220   waypoints.push_back(wpt); 
221
222   geo_direct_wgs_84 ( 0, lat, lon, heading, 
223                       10, 
224                       &lat2, &lon2, &az2 );
225   coord = coord.fromDeg(lon2, lat2); 
226   wpt = createOnGround(ac, string("park2"), coord, dep->getElevation(), vTaxiBackward);
227
228   waypoints.push_back(wpt); 
229
230   geo_direct_wgs_84 ( 0, lat, lon, heading, 
231                       2.2*radius,           
232                       &lat2, &lon2, &az2 );
233   coord = coord.fromDeg(lon2, lat2); 
234   wpt = createOnGround(ac, string("taxiStart"), coord, dep->getElevation(), vTaxiReduced);
235   waypoints.push_back(wpt);
236
237 }