Debian Codegraph @

src/ATC/ATCController.cxx

raw symbols SCIP
1 /*
2 * SPDX-FileName: ATCController.cxx
3 * SPDX-FileComment: Extracted from trafficrecord.cxx - Implementation of AIModels ATC code.
4 * SPDX-FileCopyrightText: Copyright (C) 2006 Durk Talsma
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include <config.h>
9
10 #include <algorithm>
11 #include <cstdio>
12 #include <random>
13
14 #include <osg/Geode>
15 #include <osg/Geometry>
16 #include <osg/MatrixTransform>
17 #include <osg/Shape>
18
19 #include <simgear/scene/material/EffectGeode.hxx>
20 #include <simgear/scene/material/mat.hxx>
21 #include <simgear/scene/material/matlib.hxx>
22 #include <simgear/scene/util/OsgMath.hxx>
23 #include <simgear/timing/sg_time.hxx>
24
25 #include <Scenery/scenery.hxx>
26
27 #include "atc_mgr.hxx"
28 #include "trafficcontrol.hxx"
29 #include <AIModel/AIAircraft.hxx>
30 #include <AIModel/AIFlightPlan.hxx>
31 #include <AIModel/performancedata.hxx>
32 #include <Airports/airport.hxx>
33 #include <Airports/dynamics.hxx>
34 #include <Airports/groundnetwork.hxx>
35 #include <Radio/radio.hxx>
36 #include <Traffic/TrafficMgr.hxx>
37 #include <signal.h>
38
39 #include <ATC/ATCController.hxx>
40 #include <ATC/atc_mgr.hxx>
41 #include <ATC/trafficcontrol.hxx>
42
43 using std::sort;
44 using std::string;
45
46 /***************************************************************************
47 * FGATCController
48 *
49 **************************************************************************/
50
51 FGATCController::FGATCController()
52 {
53 dt_count = 0;
54 available = true;
55 lastTransmission = 0;
56 initialized = false;
57 lastTransmissionDirection = ATC_AIR_TO_GROUND;
58 group = NULL;
59 }
60
61 FGATCController::~FGATCController()
62 {
63 if (initialized) {
64 auto mgr = globals->get_subsystem<FGATCManager>();
65 mgr->removeController(this);
66 }
67 _isDestroying = true;
68 clearTrafficControllers();
69 }
70
71 void FGATCController::init()
72 {
73 if (!initialized) {
74 auto mgr = globals->get_subsystem<FGATCManager>();
75 mgr->addController(this);
76 initialized = true;
77 }
78 }
79
80 string FGATCController::getGateName(FGAIAircraft* ref)
81 {
82 return ref->atGate();
83 }
84
85 bool FGATCController::isUserAircraft(FGAIAircraft* ac)
86 {
87 return (ac->getCallSign() == fgGetString("/sim/multiplay/callsign")) ? true : false;
88 };
89
90 bool FGATCController::checkTransmissionState(int minState, int maxState, TrafficVectorIterator i, time_t now, AtcMsgId msgId,
91 AtcMsgDir msgDir)
92 {
93 int state = i->getState();
94 if ((state >= minState) && (state <= maxState) && available) {
95 if ((msgDir == ATC_AIR_TO_GROUND) && isUserAircraft(i->getAircraft())) {
96 SG_LOG(SG_ATC, SG_BULK, "Checking state " << state << " for " << i->getAircraft()->getCallSign());
97 SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
98 int n = trans_num->getIntValue();
99 if (n == 0) {
100 trans_num->setIntValue(-1);
101 // PopupCallback(n);
102 SG_LOG(SG_ATC, SG_DEBUG, "Selected transmission message " << n);
103 //auto atc = globals->get_subsystem<FGATCManager>();
104 //FGATCDialogNew::instance()->removeEntry(1);
105 } else {
106 SG_LOG(SG_ATC, SG_BULK, "Sending message for " << i->getAircraft()->getCallSign());
107 transmit(&(*i), parent, msgId, msgDir, false);
108 return false;
109 }
110 }
111 transmit(&(*i), parent, msgId, msgDir, true);
112 i->updateState();
113 lastTransmission = now;
114 available = false;
115 return true;
116 }
117 return false;
118 }
119
120 void FGATCController::transmit(FGTrafficRecord* rec, FGAirportDynamics* parent, AtcMsgId msgId,
121 AtcMsgDir msgDir, bool audible)
122 {
123 string sender, receiver;
124 int stationFreq = 0;
125 int taxiFreq = 0;
126 int towerFreq = 0;
127 string atisInformation;
128 string text;
129 string taxiFreqStr;
130 string towerFreqStr;
131 double heading = 0;
132 string activeRunway;
133 string fltType;
134 string rwyClass;
135 string SID;
136 string transponderCode;
137 FGAIFlightPlan* fp;
138 string fltRules;
139 string instructionText;
140 int ground_to_air = 0;
141
142 //double commFreqD;
143 sender = rec->getCallsign();
144 if (rec->getAircraft()->getTaxiClearanceRequest()) {
145 instructionText = "push-back and taxi";
146 } else {
147 instructionText = "taxi";
148 }
149
150 SG_LOG(SG_ATC, SG_BULK, "transmitting for: " << sender << " at Leg " << rec->getLeg());
151
152 //FIXME move departure and arrival to rec
153 auto depApt = rec->getAircraft()->getTrafficRef()->getDepartureAirport();
154 auto arrApt = rec->getAircraft()->getTrafficRef()->getArrivalAirport();
155
156 if (!depApt) {
157 SG_LOG(SG_ATC, SG_DEV_ALERT, "TrafficRec has empty departure airport, can't transmit");
158 return;
159 }
160 if (!arrApt) {
161 SG_LOG(SG_ATC, SG_DEV_ALERT, "TrafficRec has empty arrival airport, can't transmit");
162 return;
163 }
164
165 stationFreq = getFrequency();
166 taxiFreq = depApt->getDynamics()->getGroundFrequency(2);
167 towerFreq = depApt->getDynamics()->getTowerFrequency(2);
168 receiver = getName();
169 atisInformation = depApt->getDynamics()->getAtisSequence();
170
171 // Swap sender and receiver value in case of a ground to air transmission
172 if (msgDir == ATC_GROUND_TO_AIR) {
173 string tmp = sender;
174 sender = receiver;
175 receiver = tmp;
176 ground_to_air = 1;
177 }
178
179 switch (msgId) {
180 case MSG_ANNOUNCE_ENGINE_START:
181 text = sender + ". Ready to Start up.";
182 break;
183 case MSG_REQUEST_ENGINE_START:
184 text =
185 receiver + ", This is " + sender + ". Position " +
186 getGateName(rec->getAircraft()) + ". Information " +
187 atisInformation + ". " +
188 rec->getAircraft()->getTrafficRef()->getFlightRules() +
189 " to " +
190 rec->getAircraft()->getTrafficRef()->getArrivalAirport()->getName() + ". Request start-up.";
191 break;
192 // Acknowledge engine startup permission
193 // Assign departure runway
194 // Assign SID, if necessary (TODO)
195 case MSG_PERMIT_ENGINE_START:
196 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
197
198 heading = rec->getAircraft()->getTrafficRef()->getCourse();
199 fltType = rec->getAircraft()->getTrafficRef()->getFlightType();
200 rwyClass =
201 rec->getAircraft()->GetFlightPlan()->getRunwayClassFromTrafficType(fltType);
202
203 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
204 heading);
205 rec->getAircraft()->GetFlightPlan()->setRunway(activeRunway);
206 fp = NULL;
207 rec->getAircraft()->GetFlightPlan()->setSID(fp);
208 if (fp) {
209 SID = fp->getName() + " departure";
210 } else {
211 SID = "fly runway heading ";
212 }
213 //snprintf(buffer, 7, "%3.2f", heading);
214 fltRules = rec->getAircraft()->getTrafficRef()->getFlightRules();
215 transponderCode = genTransponderCode(fltRules);
216 rec->getAircraft()->SetTransponderCode(transponderCode);
217 text =
218 receiver + ". Start-up approved. " + atisInformation +
219 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
220 transponderCode + ". " +
221 "For " + instructionText + " clearance call " + taxiFreqStr + ". " +
222 sender + " control.";
223 break;
224 case MSG_DENY_ENGINE_START:
225 text = receiver + ". Standby.";
226 break;
227 case MSG_ACKNOWLEDGE_ENGINE_START:
228 fp = rec->getAircraft()->GetFlightPlan()->getSID();
229 if (fp) {
230 SID =
231 rec->getAircraft()->GetFlightPlan()->getSID()->getName() +
232 " departure";
233 } else {
234 SID = "fly runway heading ";
235 }
236 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
237 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
238 transponderCode = rec->getAircraft()->GetTransponderCode();
239
240 text =
241 receiver + ". Start-up approved. " + atisInformation +
242 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
243 transponderCode + ". " +
244 "For " + instructionText + " clearance call " + taxiFreqStr + ". " +
245 sender + ".";
246 break;
247 case MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY:
248 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
249 text = receiver + ". Switching to " + taxiFreqStr + ". " + sender + ".";
250 break;
251 case MSG_INITIATE_CONTACT:
252 text = receiver + ". With you. " + sender + ".";
253 break;
254 case MSG_ACKNOWLEDGE_INITIATE_CONTACT:
255 text = receiver + ". Roger. " + sender + ".";
256 break;
257 case MSG_REQUEST_PUSHBACK_CLEARANCE:
258 if (rec->getAircraft()->getTaxiClearanceRequest()) {
259 text = receiver + ". Request push-back. " + sender + ".";
260 } else {
261 text = receiver + ". Request Taxi clearance. " + sender + ".";
262 }
263 break;
264 case MSG_PERMIT_PUSHBACK_CLEARANCE:
265 if (rec->getAircraft()->getTaxiClearanceRequest()) {
266 text = receiver + ". Push-back approved. " + sender + ".";
267 } else {
268 text = receiver + ". Cleared to Taxi. " + sender + ".";
269 }
270 break;
271 case MSG_HOLD_PUSHBACK_CLEARANCE:
272 text = receiver + ". Standby. " + sender + ".";
273 break;
274 case MSG_REQUEST_TAXI_CLEARANCE:
275 text = receiver + ". Ready to Taxi. " + sender + ".";
276 break;
277 case MSG_ISSUE_TAXI_CLEARANCE:
278 text = receiver + ". Cleared to taxi. " + sender + ".";
279 break;
280 case MSG_ACKNOWLEDGE_TAXI_CLEARANCE:
281 text = receiver + ". Cleared to taxi. " + sender + ".";
282 break;
283 case MSG_HOLD_POSITION:
284 text = receiver + ". Hold Position. " + sender + ".";
285 break;
286 case MSG_ACKNOWLEDGE_HOLD_POSITION:
287 text = receiver + ". Holding Position. " + sender + ".";
288 break;
289 case MSG_RESUME_TAXI:
290 text = receiver + ". Resume Taxiing. " + sender + ".";
291 break;
292 case MSG_ACKNOWLEDGE_RESUME_TAXI:
293 text = receiver + ". Continuing Taxi. " + sender + ".";
294 break;
295 case MSG_REPORT_RUNWAY_HOLD_SHORT:
296 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
297 text = receiver + ". Holding short runway " + activeRunway + ". " + sender + ".";
298 break;
299 case MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT:
300 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
301 text = receiver + " Roger. Holding short runway " + activeRunway + ". " + sender + ".";
302 break;
303 case MSG_CLEARED_FOR_TAKEOFF:
304 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
305 text = receiver + ". Cleared for takeoff runway " + activeRunway + ". " + sender + ".";
306 break;
307 case MSG_ACKNOWLEDGE_CLEARED_FOR_TAKEOFF:
308 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
309 text = receiver + " Roger. Cleared for takeoff runway " + activeRunway + ". " + sender + ".";
310 break;
311 case MSG_SWITCH_TOWER_FREQUENCY:
312 towerFreqStr = formatATCFrequency3_2(towerFreq);
313 text = receiver + " Contact Tower at " + towerFreqStr + ". " + sender + ".";
314 break;
315 case MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY:
316 towerFreqStr = formatATCFrequency3_2(towerFreq);
317 text = receiver + " Roger, switching to tower at " + towerFreqStr + ". " + sender + ".";
318 break;
319 case MSG_ARRIVAL:
320 text = receiver + ". " + sender + " Information delta.";
321 break;
322 case MSG_ACKNOWLEDGE_ARRIVAL:
323 activeRunway = rec->getRunway();
324 text = receiver + " expect ILS approach " + activeRunway + ". " + sender;
325 break;
326 case MSG_CLEARED_TO_LAND:
327 activeRunway = rec->getRunway();
328 // TODO: Weather
329 text = receiver + " runway " + activeRunway + " cleared to land. " + sender;
330 break;
331 case MSG_ACKNOWLEDGE_CLEARED_TO_LAND:
332 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
333 text = receiver + " runway " + activeRunway + " cleared to land. " + sender;
334 break;
335 case MSG_HOLD:
336 text = receiver + " hold as published . " + sender;
337 break;
338 case MSG_ACKNOWLEDGE_HOLD:
339 text = receiver + " holding as published . " + sender;
340 break;
341 default:
342 text = text + sender + ". Transmitting unknown Message. MsgId " + std::to_string(msgId);
343 break;
344 }
345
346 const bool atcAudioEnabled = fgGetBool("/sim/sound/atc/enabled", false);
347 if (audible && atcAudioEnabled) {
348 double onBoardRadioFreq0 =
349 fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
350 double onBoardRadioFreq1 =
351 fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
352 int onBoardRadioFreqI0 = (int)floor(onBoardRadioFreq0 * 100 + 0.5);
353 int onBoardRadioFreqI1 = (int)floor(onBoardRadioFreq1 * 100 + 0.5);
354 SG_LOG(SG_ATC, SG_DEBUG, "Using " << onBoardRadioFreq0 << ", " << onBoardRadioFreq1 << " and " << stationFreq << " for " << text << std::endl);
355 if (stationFreq == 0) {
356 SG_LOG(SG_ATC, SG_DEBUG, getName() << " stationFreq not found");
357 }
358
359 // Display ATC message only when one of the radios is tuned
360 // the relevant frequency.
361 // Note that distance attenuation is currently not yet implemented
362
363 if ((stationFreq > 0) &&
364 ((onBoardRadioFreqI0 == stationFreq) ||
365 (onBoardRadioFreqI1 == stationFreq))) {
366 if (rec->allowTransmissions()) {
367 if (fgGetBool("/sim/radio/use-itm-attenuation", false)) {
368 SG_LOG(SG_ATC, SG_DEBUG, "Using ITM radio propagation");
369 FGRadioTransmission* radio = new FGRadioTransmission();
370 SGGeod sender_pos;
371 if (ground_to_air) {
372 sender_pos = parent->parent()->geod();
373 } else {
374 sender_pos = rec->getPos();
375 }
376 double frequency = ((double)stationFreq) / 100;
377 radio->receiveATC(sender_pos, frequency, text, ground_to_air);
378 delete radio;
379 } else {
380 SG_LOG(SG_ATC, SG_BULK, "Transmitting " << text);
381 fgSetString("/sim/messages/atc", text.c_str());
382 }
383 }
384 }
385 } else {
386 //FGATCDialogNew::instance()->addEntry(1, text);
387 }
388 }
389
390 /**
391 * Sign off the aircraft with the id from this controller.
392 * @param id the id of the aircraft
393 */
394
395 void FGATCController::signOff(int id)
396 {
397 TrafficVectorIterator i = searchActiveTraffic(id);
398 if (i == activeTraffic.end()) {
399 // Dead traffic should never reach here
400 SG_LOG(SG_ATC, SG_ALERT,
401 "AI error: Aircraft without traffic record is signing off from " << getName() << " at " << SG_ORIGIN << " list " << activeTraffic.empty());
402 return;
403 }
404 SG_LOG(SG_ATC, SG_DEBUG, i->getCallsign() << " (" << i->getId() << ") signing off from " << getName() << "(" << getFrequency() << ")");
405 int oldSize = activeTraffic.size();
406 activeTraffic.erase(i);
407 if ((oldSize - activeTraffic.size()) != 1) {
408 SG_LOG(SG_ATC, SG_WARN, i->getCallsign() << " not removed ");
409 }
410 }
411
412 bool FGATCController::hasInstruction(int id)
413 {
414 // Search activeTraffic for a record matching our id
415 TrafficVectorIterator i = searchActiveTraffic(id);
416
417 if (i == activeTraffic.end()) {
418 SG_LOG(SG_ATC, SG_ALERT,
419 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
420 } else {
421 return i->hasInstruction();
422 }
423 return false;
424 }
425
426
427 FGATCInstruction FGATCController::getInstruction(int id)
428 {
429 if (activeTraffic.size() != 0) {
430 // Search activeTraffic for a record matching our id
431 TrafficVectorIterator i = searchActiveTraffic(id);
432
433 if (i != activeTraffic.end())
434 return i->getInstruction();
435 }
436
437 SG_LOG(SG_ATC, SG_ALERT, "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
438 return FGATCInstruction();
439 }
440
441
442 /*
443 * Format integer frequency xxxyy as xxx.yy
444 * @param freq - integer value
445 * @return the formatted string
446 */
447 string FGATCController::formatATCFrequency3_2(int freq)
448 {
449 char buffer[7]; // does this ever need to be freed?
450 snprintf(buffer, 7, "%3.2f", ((float)freq / 100.0));
451 return string(buffer);
452 }
453
454 // TODO: Set transponder codes according to real-world routes.
455 // The current version just returns a random string of four octal numbers.
456 string FGATCController::genTransponderCode(const string& fltRules)
457 {
458 if (fltRules == "VFR")
459 return string("1200");
460
461 std::default_random_engine generator;
462 std::uniform_int_distribution<unsigned> distribution(0, 7);
463
464 unsigned val = (distribution(generator) * 1000 +
465 distribution(generator) * 100 +
466 distribution(generator) * 10 +
467 distribution(generator));
468
469 return std::to_string(val);
470 }
471
472 void FGATCController::eraseDeadTraffic()
473 {
474 auto it = std::remove_if(activeTraffic.begin(), activeTraffic.end(), [](const FGTrafficRecord& traffic) {
475 if (traffic.isDead()) {
476 SG_LOG(SG_ATC, SG_DEBUG, "Remove dead " << traffic.getId() << " " << traffic.isDead());
477 }
478 return traffic.isDead();
479 });
480 activeTraffic.erase(it, activeTraffic.end());
481 }
482
483 /*
484 * Search activeTraffic vector to find matching id
485 * @param id integer to search for in the vector
486 * @return the matching item OR activeTraffic.end()
487 */
488 TrafficVectorIterator FGATCController::searchActiveTraffic(int id)
489 {
490 return std::find_if(activeTraffic.begin(), activeTraffic.end(),
491 [id](const FGTrafficRecord& rec) { return rec.getId() == id; });
492 }
493
494 void FGATCController::clearTrafficControllers()
495 {
496 for (const auto& traffic : activeTraffic) {
497 traffic.clearATCController();
498 }
499 }