Debian Codegraph @

src/ATC/TowerController.cxx

raw symbols SCIP
1 // Extracted from trafficrecord.cxx - Implementation of AIModels ATC code.
2 //
3 // Written by Durk Talsma, started September 2006.
4 //
5 // Copyright (C) 2006 Durk Talsma.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 //
21 // $Id$
22
23 #include <config.h>
24
25 #include <algorithm>
26 #include <cstdio>
27 #include <random>
28
29 #include <osg/Geode>
30 #include <osg/Geometry>
31 #include <osg/MatrixTransform>
32 #include <osg/Shape>
33
34 #include <simgear/scene/material/EffectGeode.hxx>
35 #include <simgear/scene/material/matlib.hxx>
36 #include <simgear/scene/material/mat.hxx>
37 #include <simgear/scene/util/OsgMath.hxx>
38 #include <simgear/timing/sg_time.hxx>
39
40 #include <Scenery/scenery.hxx>
41
42 #include "trafficcontrol.hxx"
43 #include "atc_mgr.hxx"
44 #include <AIModel/AIAircraft.hxx>
45 #include <AIModel/AIFlightPlan.hxx>
46 #include <AIModel/performancedata.hxx>
47 #include <Traffic/TrafficMgr.hxx>
48 #include <Airports/groundnetwork.hxx>
49 #include <Airports/dynamics.hxx>
50 #include <Airports/airport.hxx>
51 #include <Radio/radio.hxx>
52 #include <signal.h>
53
54 #include <ATC/atc_mgr.hxx>
55 #include <ATC/ATCController.hxx>
56 #include <ATC/trafficcontrol.hxx>
57 #include <ATC/TowerController.hxx>
58
59 using std::sort;
60 using std::string;
61
62 /***************************************************************************
63 * class FGTowerController
64 s * subclass of FGATCController
65 **************************************************************************/
66
67 FGTowerController::FGTowerController(FGAirportDynamics *par) :
68 FGATCController()
69 {
70 parent = par;
71 }
72
73 FGTowerController::~FGTowerController()
74 {
75 }
76
77 //
78 void FGTowerController::announcePosition(int id,
79 FGAIFlightPlan * intendedRoute,
80 int currentPosition, double lat,
81 double lon, double heading,
82 double speed, double alt,
83 double radius, int leg,
84 FGAIAircraft * ref)
85 {
86 init();
87
88 // Search activeTraffic for a record matching our id
89 TrafficVectorIterator i = FGATCController::searchActiveTraffic(id);
90
91 // Add a new TrafficRecord if no one exists for this aircraft.
92 if (i == activeTraffic.end() || (activeTraffic.empty())) {
93 FGTrafficRecord rec;
94 rec.setId(id);
95
96 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
97 rec.setRunway(intendedRoute->getRunway());
98 rec.setLeg(leg);
99 rec.setCallsign(ref->getCallSign());
100 rec.setRadius(radius);
101 rec.setAircraft(ref);
102 activeTraffic.push_back(rec);
103 // Don't just schedule the aircraft for the tower controller, also assign if to the correct active runway.
104 ActiveRunwayVecIterator rwy = activeRunways.begin();
105 if (! activeRunways.empty()) {
106 while (rwy != activeRunways.end()) {
107 if (rwy->getRunwayName() == intendedRoute->getRunway()) {
108 break;
109 }
110 ++rwy;
111 }
112 }
113 if (rwy == activeRunways.end()) {
114 ActiveRunway aRwy(intendedRoute->getRunway(), id);
115 aRwy.addToDepartureQueue(ref);
116 time_t now = globals->get_time_params()->get_cur_time();
117 rec.setPlannedArrivalTime(now);
118 rec.setRunwaySlot(aRwy.requestTimeSlot(now));
119 activeRunways.push_back(aRwy);
120 rwy = (activeRunways.end()-1);
121 } else {
122 time_t now = globals->get_time_params()->get_cur_time();
123 rec.setPlannedArrivalTime(now);
124 rec.setRunwaySlot(rwy->requestTimeSlot(now));
125 rwy->addToDepartureQueue(ref);
126 }
127
128 SG_LOG(SG_ATC, SG_DEBUG, ref->getTrafficRef()->getCallSign() << " You are number " << rwy->getdepartureQueueSize() << " for takeoff from " << rwy->getRunwayName());
129 } else {
130 i->setPositionAndHeading(lat, lon, heading, speed, alt);
131 }
132 }
133
134 void FGTowerController::updateAircraftInformation(int id, SGGeod geod,
135 double heading, double speed, double alt,
136 double dt)
137 {
138 // Search activeTraffic for a record matching our id
139 TrafficVectorIterator i = FGATCController::searchActiveTraffic(id);
140
141 setDt(getDt() + dt);
142
143 time_t now = globals->get_time_params()->get_cur_time();
144 if (i == activeTraffic.end() || (activeTraffic.empty())) {
145 SG_LOG(SG_ATC, SG_ALERT,
146 "AI error: updating aircraft without traffic record at " <<
147 SG_ORIGIN);
148 return;
149 }
150
151 // Update the position of the current aircraft
152 FGTrafficRecord& current = *i;
153 current.setPositionAndHeading(geod.getLatitudeDeg(), geod.getLongitudeDeg(), heading, speed, alt);
154
155 // see if we already have a clearance record for the currently active runway
156 // NOTE: dd. 2011-08-07: Because the active runway has been constructed in the announcePosition function, we may safely assume that is
157 // already exists here. So, we can simplify the current code.
158
159 ActiveRunwayVecIterator rwy = activeRunways.begin();
160 //if (parent->getId() == fgGetString("/sim/presets/airport-id")) {
161 // for (rwy = activeRunways.begin(); rwy != activeRunways.end(); ++rwy) {
162 // rwy->printdepartureQueue();
163 // }
164 //}
165
166 // rwy = activeRunways.begin();
167 while (rwy != activeRunways.end()) {
168 if (rwy->getRunwayName() == current.getRunway()) {
169 break;
170 }
171 ++rwy;
172 }
173
174 // only bother running the following code if the current aircraft is the
175 // first in line for departure
176 /* if (current.getAircraft() == rwy->getFirstAircraftIndepartureQueue()) {
177 if (rwy->getCleared()) {
178 if (id == rwy->getCleared()) {
179 current.setHoldPosition(false);
180 } else {
181 current.setHoldPosition(true);
182 }
183 } else {
184 // For now. At later stages, this will probably be the place to check for inbound traffic.
185 rwy->setCleared(id);
186 }
187 } */
188 // only bother with aircraft that have a takeoff status of 2, since those are essentially under tower control
189 auto ac = rwy->getFirstAircraftInDepartureQueue();
190 if (ac) {
191 if (ac->getTakeOffStatus() == AITakeOffStatus::QUEUED) {
192 // transmit takeoff clearance
193 ac->setTakeOffStatus(AITakeOffStatus::CLEARED_FOR_TAKEOFF);
194 TrafficVectorIterator first = searchActiveTraffic(ac->getID());
195 //FIXME use checkTransmissionState
196 if (first == activeTraffic.end() || activeTraffic.empty()) {
197 SG_LOG(SG_ATC, SG_ALERT,
198 "FGApproachController updating aircraft without traffic record at " << SG_ORIGIN);
199 } else {
200 first->setState(ATCMessageState::CLEARED_TAKEOFF);
201 transmit(&(*first), &(*parent), MSG_CLEARED_FOR_TAKEOFF, ATC_GROUND_TO_AIR, true);
202 }
203 }
204 }
205 //FIXME Make it an member of traffic record
206 if (current.getAircraft()->getTakeOffStatus() == AITakeOffStatus::CLEARED_FOR_TAKEOFF &&
207 current.getRunwaySlot() < now) {
208 current.setHoldPosition(false);
209 if (checkTransmissionState(ATCMessageState::CLEARED_TAKEOFF, ATCMessageState::CLEARED_TAKEOFF, i, now, MSG_ACKNOWLEDGE_CLEARED_FOR_TAKEOFF, ATC_AIR_TO_GROUND)) {
210 current.setState(ATCMessageState::ANNOUNCE_ARRIVAL);
211 }
212 } else {
213 current.setHoldPosition(true);
214 SG_LOG(SG_ATC, SG_BULK,
215 current.getCallsign() << "| Waiting for " << (current.getRunwaySlot() - now) << " seconds");
216 }
217 int clearanceId = rwy->getCleared();
218 if (clearanceId) {
219 if (id == clearanceId) {
220 SG_LOG(SG_ATC, SG_BULK, current.getCallsign() << "| Unset Hold " << clearanceId << " for rwy " << rwy->getRunwayName());
221 current.setHoldPosition(false);
222 } else {
223 SG_LOG(SG_ATC, SG_BULK, current.getCallsign() << "| Not cleared " << id << " Currently cleared " << clearanceId);
224 }
225 } else {
226 if (current.getAircraft() == rwy->getFirstAircraftInDepartureQueue()) {
227 SG_LOG(SG_ATC, SG_BULK,
228 current.getCallsign() << "| Cleared for runway " << getName() << " " << rwy->getRunwayName() << " Id " << id);
229 rwy->setCleared(id);
230 auto l_ac = rwy->getFirstOfStatus(AITakeOffStatus::QUEUED);
231 if (l_ac) {
232 l_ac->setTakeOffStatus(AITakeOffStatus::QUEUED);
233 // transmit takeoff clearance? But why twice?
234 }
235 } else {
236 #if 0 // Ticket #2770 : ATC/TowerController floods log
237 SG_LOG(SG_ATC, SG_BULK,
238 "Not cleared " << current.getAircraft()->getCallSign() << " " << rwy->getFirstAircraftInDepartureQueue()->getCallSign());
239 #endif
240 }
241 }
242 }
243
244 void FGTowerController::signOff(int id)
245 {
246 SG_LOG(SG_ATC, SG_BULK, "Signing off " << id << " from Tower");
247 // ensure we don't modify activeTraffic during destruction
248 if (_isDestroying)
249 return;
250
251 // Search activeTraffic for a record matching our id
252 TrafficVectorIterator i = FGATCController::searchActiveTraffic(id);
253 if (i == activeTraffic.end() || (activeTraffic.empty())) {
254 SG_LOG(SG_ATC, SG_ALERT,
255 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
256 return;
257 }
258
259 const auto trafficRunway = i->getRunway();
260 auto runwayIt = std::find_if(activeRunways.begin(), activeRunways.end(),
261 [&trafficRunway](const ActiveRunway& ar) {
262 return ar.getRunwayName() == trafficRunway;
263 });
264
265 if (runwayIt != activeRunways.end()) {
266 SG_LOG(SG_ATC, SG_BULK, i->getCallsign() << "|Cleared " << id << " from " << runwayIt->getRunwayName() << " cleared " << runwayIt->getCleared() );
267 runwayIt->removeFromDepartureQueue(id);
268 } else {
269 SG_LOG(SG_ATC, SG_ALERT,
270 "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff at " << SG_ORIGIN);
271 }
272
273 i->getAircraft()->resetTakeOffStatus();
274 FGATCController::signOff(id);
275 }
276
277 // Note:
278 // if we make trafficrecord a member of the base class
279 // the following three functions: signOff, hasInstruction and getInstruction can
280 // become devirtualized and be a member of the base ATCController class
281 // which would simplify code maintenance.
282 // note that this function is probably obsolete
283 bool FGTowerController::hasInstruction(int id)
284 {
285 // Search activeTraffic for a record matching our id
286 TrafficVectorIterator i = FGATCController::searchActiveTraffic(id);
287
288 if (i == activeTraffic.end() || activeTraffic.empty()) {
289 SG_LOG(SG_ATC, SG_ALERT,
290 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
291 } else {
292 return i->hasInstruction();
293 }
294 return false;
295 }
296
297
298 FGATCInstruction FGTowerController::getInstruction(int id)
299 {
300 // Search activeTraffic for a record matching our id
301 TrafficVectorIterator i = FGATCController::searchActiveTraffic(id);
302
303 if (i == activeTraffic.end() || activeTraffic.empty()) {
304 SG_LOG(SG_ATC, SG_ALERT,
305 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
306 } else {
307 return i->getInstruction();
308 }
309 return FGATCInstruction();
310 }
311
312 void FGTowerController::render(bool visible) {
313 // this should be bulk, since its called quite often
314 SG_LOG(SG_ATC, SG_BULK, "FGTowerController::render function not yet implemented");
315 }
316
317 string FGTowerController::getName() {
318 return string(parent->parent()->getName() + "-tower");
319 }
320
321
322 void FGTowerController::update(double dt)
323 {
324 FGATCController::eraseDeadTraffic();
325 }
326
327 int FGTowerController::getFrequency() {
328 int towerFreq = parent->getTowerFrequency(2);
329 return towerFreq;
330 }