| 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 <cstdlib> |
| 26 | #include <cstdio> |
| 27 | |
| 28 | #include <simgear/math/sg_geodesy.hxx> |
| 29 | |
| 30 | #include <Airports/airport.hxx> |
| 31 | #include <Airports/runways.hxx> |
| 32 | #include <Airports/dynamics.hxx> |
| 33 | #include <Airports/groundnetwork.hxx> |
| 34 | |
| 35 | #include <Environment/environment_mgr.hxx> |
| 36 | #include <Environment/environment.hxx> |
| 37 | |
| 38 | #include <Main/sentryIntegration.hxx> |
| 39 | |
| 40 | #include "AIFlightPlan.hxx" |
| 41 | #include "AIAircraft.hxx" |
| 42 | #include "performancedata.hxx" |
| 43 | |
| 44 | using std::string; |
| 45 | |
| 46 | // TODO: Use James Turner's createOnGround functions. |
| 47 | bool FGAIFlightPlan::createPushBack(FGAIAircraft *ac, |
| 48 | bool firstFlight, |
| 49 | FGAirport *dep, |
| 50 | double radius, |
| 51 | const string& fltType, |
| 52 | const string& aircraftType, |
| 53 | const string& airline) |
| 54 | { |
| 55 | double vTaxi = ac->getPerformance()->vTaxi(); |
| 56 | double vTaxiBackward = vTaxi * (-2.0/3.0); |
| 57 | double vTaxiReduced = vTaxi * (2.0/3.0); |
| 58 | |
| 59 | // Active runway can be conditionally set by ATC, so at the start of a new flight, this |
| 60 | // must be reset. |
| 61 | activeRunway.clear(); |
| 62 | |
| 63 | if (!(dep->getDynamics()->getGroundController()->exists())) { |
| 64 | //cerr << "Push Back fallback" << endl; |
| 65 | SG_LOG(SG_AI, SG_DEV_WARN, "No groundcontroller createPushBackFallBack at " << dep->getId()); |
| 66 | createPushBackFallBack(ac, firstFlight, dep, |
| 67 | radius, fltType, aircraftType, airline); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | if (firstFlight || !dep->getDynamics()->hasParking(gate.parking())) { |
| 72 | // establish the parking position / gate |
| 73 | // if the airport has no parking positions defined, don't log |
| 74 | // the warning below. |
| 75 | if (!dep->getDynamics()->hasParkings()) { |
| 76 | return false; |
| 77 | } |
| 78 | gate = dep->getDynamics()->getAvailableParking(radius, fltType, |
| 79 | aircraftType, airline); |
| 80 | if (!gate.isValid()) { |
| 81 | SG_LOG(SG_AI, SG_DEV_WARN, "Could not find parking for a " << |
| 82 | aircraftType << |
| 83 | " of flight type " << fltType << |
| 84 | " of airline " << airline << |
| 85 | " at airport " << dep->getId()); |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | if (!gate.isValid()) { |
| 92 | SG_LOG(SG_AI, SG_DEV_WARN, "Gate " << gate.parking()->ident() << " not valid createPushBackFallBack at " << dep->getId()); |
| 93 | createPushBackFallBack(ac, firstFlight, dep, |
| 94 | radius, fltType, aircraftType, airline); |
| 95 | return true; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | // catch becuase taxiRoute creation sometimes fails, see eg |
| 100 | // https://gitlab.com/flightgear/flightgear/-/issues/3289 |
| 101 | try { |
| 102 | FGGroundNetwork* groundNet = dep->groundNetwork(); |
| 103 | FGParking *parking = gate.parking(); |
| 104 | if (parking && parking->getPushBackPoint() != nullptr) { |
| 105 | FGTaxiRoute route = groundNet->findShortestRoute(parking, parking->getPushBackPoint(), false); |
| 106 | SG_LOG(SG_AI, SG_BULK, "Creating Pushback from " << parking->ident() << " to " << parking->getPushBackPoint()->getIndex()); |
| 107 | |
| 108 | int size = route.size(); |
| 109 | if (size < 2) { |
| 110 | SG_LOG(SG_AI, SG_DEV_WARN, "Push back route from gate " << parking->ident() << " has only " << size << " nodes.\n" << "Using " << parking->getPushBackPoint()); |
| 111 | } |
| 112 | |
| 113 | route.first(); |
| 114 | FGTaxiNodeRef node; |
| 115 | int rte; |
| 116 | |
| 117 | if (waypoints.size()>0) { |
| 118 | // This will be a parking from a previous leg which still contains the forward speed |
| 119 | waypoints.back()->setSpeed(vTaxiBackward); |
| 120 | } |
| 121 | |
| 122 | while (route.next(node, &rte)) |
| 123 | { |
| 124 | char buffer[20]; |
| 125 | snprintf (buffer, sizeof(buffer), "pushback-%03d", (short)node->getIndex()); |
| 126 | FGAIWaypoint *wpt = createOnGround(ac, string(buffer), node->geod(), dep->getElevation(), vTaxiBackward); |
| 127 | |
| 128 | /* |
| 129 | if (previous) { |
| 130 | FGTaxiSegment* segment = groundNet->findSegment(previous, node); |
| 131 | wpt->setRouteIndex(segment->getIndex()); |
| 132 | } else { |
| 133 | // not on the route yet, make up a unique segment ID |
| 134 | int x = (int) tn->guid(); |
| 135 | wpt->setRouteIndex(x); |
| 136 | }*/ |
| 137 | |
| 138 | wpt->setRouteIndex(rte); |
| 139 | pushBackWaypoint(wpt); |
| 140 | //previous = node; |
| 141 | } |
| 142 | // some special considerations for the last point: |
| 143 | // This will trigger the release of parking |
| 144 | waypoints.back()->setName(string("PushBackPoint")); |
| 145 | waypoints.back()->setSpeed(vTaxi); |
| 146 | ac->setTaxiClearanceRequest(true); |
| 147 | } else { // In case of a push forward departure... |
| 148 | ac->setTaxiClearanceRequest(false); |
| 149 | double az2 = 0.0; |
| 150 | |
| 151 | FGTaxiSegment* pushForwardSegment = dep->groundNetwork()->findSegmentByHeading(parking, parking->getHeading()); |
| 152 | |
| 153 | if (!pushForwardSegment) { |
| 154 | // there aren't any routes for this parking, so create a simple segment straight ahead for 2 meters based on the parking heading |
| 155 | SG_LOG(SG_AI, SG_DEV_WARN, "Gate " << parking->ident() << " at " << dep->getId() |
| 156 | << " doesn't seem to have pushforward routes associated with it."); |
| 157 | |
| 158 | FGAIWaypoint *wpt = createOnGround(ac, string("park"), parking->geod(), dep->getElevation(), vTaxiReduced); |
| 159 | pushBackWaypoint(wpt); |
| 160 | |
| 161 | SGGeod coord; |
| 162 | SGGeodesy::direct(parking->geod(), parking->getHeading(), 2.0, coord, az2); |
| 163 | wpt = createOnGround(ac, string("taxiStart"), coord, dep->getElevation(), vTaxiReduced); |
| 164 | pushBackWaypoint(wpt); |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | lastNodeVisited = pushForwardSegment->getEnd(); |
| 169 | double distance = pushForwardSegment->getLength(); |
| 170 | |
| 171 | double parkingHeading = parking->getHeading(); |
| 172 | |
| 173 | SG_LOG(SG_AI, SG_BULK, "Creating Pushforward from ID " << pushForwardSegment->getEnd()->getIndex() << " Length : \t" << distance); |
| 174 | // Add the parking if on first leg and not repeat |
| 175 | if (waypoints.size() == 0) { |
| 176 | pushBackWaypoint( createOnGround(ac, parking->getName(), parking->geod(), dep->getElevation(), vTaxiReduced)); |
| 177 | } |
| 178 | // Make sure we have at least three WPs |
| 179 | int numSegments = distance>15?(distance/5.0):3; |
| 180 | for (int i = 1; i < numSegments; i++) { |
| 181 | SGGeod pushForwardPt; |
| 182 | |
| 183 | SGGeodesy::direct(parking->geod(), parkingHeading, |
| 184 | (((double)i / numSegments) * distance), pushForwardPt, az2); |
| 185 | char buffer[20]; |
| 186 | snprintf(buffer, sizeof(buffer), "pushforward-%03d", (short)i); |
| 187 | FGAIWaypoint *wpt = createOnGround(ac, string(buffer), pushForwardPt, dep->getElevation(), vTaxiReduced); |
| 188 | |
| 189 | wpt->setRouteIndex(pushForwardSegment->getIndex()); |
| 190 | pushBackWaypoint(wpt); |
| 191 | } |
| 192 | |
| 193 | // This will trigger the release of parking |
| 194 | waypoints.back()->setName(string("PushBackPoint-pushforward")); |
| 195 | } |
| 196 | |
| 197 | return true; |
| 198 | } catch (sg_exception& e) { |
| 199 | SG_LOG(SG_AI, SG_DEV_WARN, "Exception while creating pushback route: " << e.what() << "\nUsing fallback pushback."); |
| 200 | flightgear::sentryReportException("Failed to create pushback route for " + dep->ident() + " " + gate.parking()->ident() + |
| 201 | ": " + e.getFormattedMessage()); |
| 202 | createPushBackFallBack(ac, firstFlight, dep, |
| 203 | radius, fltType, aircraftType, airline); |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | /******************************************************************* |
| 208 | * createPushBackFallBack |
| 209 | * This is the backup function for airports that don't have a |
| 210 | * network yet. |
| 211 | ******************************************************************/ |
| 212 | void FGAIFlightPlan::createPushBackFallBack(FGAIAircraft *ac, bool firstFlight, FGAirport *dep, |
| 213 | double radius, |
| 214 | const string& fltType, |
| 215 | const string& aircraftType, |
| 216 | const string& airline) |
| 217 | { |
| 218 | double az2 = 0.0; |
| 219 | |
| 220 | double vTaxi = ac->getPerformance()->vTaxi(); |
| 221 | double vTaxiBackward = vTaxi * (-2.0/3.0); |
| 222 | double vTaxiReduced = vTaxi * (2.0/3.0); |
| 223 | |
| 224 | double heading = 180.0; // this is a completely arbitrary heading! |
| 225 | FGAIWaypoint *wpt = createOnGround(ac, string("park"), dep->geod(), dep->getElevation(), vTaxiBackward); |
| 226 | |
| 227 | pushBackWaypoint(wpt); |
| 228 | |
| 229 | SGGeod coord; |
| 230 | SGGeodesy::direct(dep->geod(), heading, 10, coord, az2); |
| 231 | wpt = createOnGround(ac, string("park2"), coord, dep->getElevation(), vTaxiBackward); |
| 232 | |
| 233 | pushBackWaypoint(wpt); |
| 234 | |
| 235 | SGGeodesy::direct(dep->geod(), heading, 2.2 * radius, coord, az2); |
| 236 | wpt = createOnGround(ac, string("taxiStart"), coord, dep->getElevation(), vTaxiReduced); |
| 237 | pushBackWaypoint(wpt); |
| 238 | |
| 239 | } |