Debian Codegraph @

src/ATC/trafficcontrol.cxx

raw symbols SCIP
1 // 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 #include <simgear/math/sg_geodesy.hxx>
40
41 #include <Scenery/scenery.hxx>
42
43 #include "trafficcontrol.hxx"
44 #include "atc_mgr.hxx"
45 #include <AIModel/AIAircraft.hxx>
46 #include <AIModel/AIFlightPlan.hxx>
47 #include <AIModel/performancedata.hxx>
48 #include <ATC/atc_mgr.hxx>
49 #include <Traffic/TrafficMgr.hxx>
50 #include <Airports/groundnetwork.hxx>
51 #include <Airports/dynamics.hxx>
52 #include <Airports/airport.hxx>
53 #include <Radio/radio.hxx>
54 #include <signal.h>
55
56 using std::sort;
57 using std::string;
58
59 /***************************************************************************
60 * ActiveRunway
61 **************************************************************************/
62
63 ActiveRunway::ActiveRunway(const std::string& r, int cc) :
64 rwy(r)
65 {
66 currentlyCleared = cc;
67 distanceToFinal = 6.0 * SG_NM_TO_METER;
68 };
69
70 void ActiveRunway::removeFromDepartureQueue(int id) {
71 if (id!=currentlyCleared) {
72 printDepartureQueue();
73 SG_LOG(SG_ATC, SG_WARN, "Not cleared id being removed from DepartureQueue " << id << " currently cleared " << currentlyCleared);
74 } else {
75 setCleared(0);
76 updateDepartureQueue();
77 printDepartureQueue();
78 }
79 }
80
81 void ActiveRunway::updateDepartureQueue()
82 {
83 departureQueue.erase(departureQueue.begin());
84 }
85
86 /**
87 * Fetch next slot for the active runway
88 * @param eta time of slot requested
89 * @return newEta: next slot available; starts at eta paramater
90 * and adds separation as needed
91 */
92 time_t ActiveRunway::requestTimeSlot(time_t eta)
93 {
94 time_t newEta = 0;
95
96 // if the aircraft is the first arrival, add to the vector and return eta directly
97 if (estimatedArrivalTimes.empty()) {
98 estimatedArrivalTimes.push_back(eta);
99 SG_LOG(SG_ATC, SG_BULK, getRunwayName() << " Checked eta slots, using " << eta);
100 return eta;
101 } else {
102 // First check the already assigned slots to see where we need to fit the flight in
103 SG_LOG(SG_ATC, SG_BULK, getRunwayName() << " Checking eta slots " << eta << " : " << estimatedArrivalTimes.size() << " Timediff : " << (eta - globals->get_time_params()->get_cur_time()));
104
105 // is this needed - just a debug output?
106 TimeVectorIterator i;
107 for (i = estimatedArrivalTimes.begin();
108 i != estimatedArrivalTimes.end(); ++i) {
109 SG_LOG(SG_ATC, SG_BULK, "Stored time : " << (*i));
110 }
111
112 // if the flight is before the first scheduled slot + separation
113 time_t separation = 60;
114 i = estimatedArrivalTimes.begin();
115 if ((eta + separation) < (*i)) {
116 newEta = eta;
117 SG_LOG(SG_ATC, SG_BULK, "Storing at beginning");
118 SG_LOG(SG_ATC, SG_DEBUG, "Done. New ETA : " << newEta );
119 slotHousekeeping(newEta);
120 return newEta;
121 }
122
123 // else, look through the rest of the slots
124 bool found = false;
125 while ((i != estimatedArrivalTimes.end()) && (!found)) {
126 TimeVectorIterator j = i + 1;
127
128 // if the flight is after the last scheduled slot check if separation is needed
129 if (j == estimatedArrivalTimes.end()) {
130 if (((*i) + separation) < eta) {
131 SG_LOG(SG_ATC, SG_BULK, "Storing at end");
132 newEta = eta;
133 } else {
134 newEta = (*i) + separation;
135 SG_LOG(SG_ATC, SG_BULK, "Storing at end + separation");
136 }
137 SG_LOG(SG_ATC, SG_DEBUG, "Done. New ETA : " << newEta << " Timediff : " << (newEta-eta));
138 slotHousekeeping(newEta);
139 return newEta;
140 } else {
141 // potential slot found
142 // check the distance between the previous and next slots
143 // distance must be greater than 2* separation
144 if ((((*j) - (*i)) > (separation * 2))) {
145 // now check whether this slot is usable:
146 // eta should fall between the two points
147 // i.e. eta > i AND eta < j
148 SG_LOG(SG_ATC, SG_DEBUG, "Found potential slot after " << (*i));
149 if (eta > (*i) && (eta < (*j))) {
150 found = true;
151 if (eta < ((*i) + separation)) {
152 newEta = (*i) + separation;
153 SG_LOG(SG_ATC, SG_BULK, "Using original" << (*i) << " + separation ");
154 } else {
155 newEta = eta;
156 SG_LOG(SG_ATC, SG_BULK, "Using original after " << (*i));
157 }
158 } else if (eta < (*i)) {
159 found = true;
160 newEta = (*i) + separation;
161 SG_LOG(SG_ATC, SG_BULK, "Using delayed slot after " << (*i));
162 }
163 /*
164 if (((*j) - separation) < eta) {
165 found = true;
166 if (((*i) + separation) < eta) {
167 newEta = eta;
168 SG_LOG(SG_ATC, SG_BULK, "Using original after " << (*i));
169 } else {
170 newEta = (*i) + separation;
171 SG_LOG(SG_ATC, SG_BULK, "Using " << (*i) << " + separation ");
172 }
173 } */
174 }
175 }
176 ++i;
177 }
178 }
179
180 SG_LOG(SG_ATC, SG_DEBUG, "Done. New ETA : " << newEta);
181 slotHousekeeping(newEta);
182 return newEta;
183 }
184
185 void ActiveRunway::slotHousekeeping(time_t newEta)
186 {
187 // add the slot to the vector and resort the vector
188 estimatedArrivalTimes.push_back(newEta);
189 sort(estimatedArrivalTimes.begin(), estimatedArrivalTimes.end());
190
191 // do some housekeeping : remove any slots that are past
192 time_t now = globals->get_time_params()->get_cur_time();
193
194 TimeVectorIterator i = estimatedArrivalTimes.begin();
195 while (i != estimatedArrivalTimes.end()) {
196 if ((*i) < now) {
197 SG_LOG(SG_ATC, SG_BULK, "Deleting timestamp " << (*i) << " (now = " << now << "). ");
198 estimatedArrivalTimes.erase(i);
199 i = estimatedArrivalTimes.begin();
200 } else {
201 i++;
202 }
203 }
204 }
205
206 /** Output the contents of the departure queue vector nicely formatted*/
207 void ActiveRunway::printDepartureQueue()
208 {
209 SG_LOG(SG_ATC, SG_DEBUG, "Departure queue for " << rwy << ": ");
210 for (auto acft : departureQueue) {
211 SG_LOG(SG_ATC, SG_DEBUG, " " << acft->getCallSign() << " " << acft->getTakeOffStatus());
212 SG_LOG(SG_ATC, SG_DEBUG, " " << acft->_getLatitude() << " " << acft->_getLongitude() << acft->getSpeed() << " " << acft->getAltitude());
213 }
214
215 }
216
217 /** Fetch the first aircraft in the departure queue with a certain status */
218 SGSharedPtr<FGAIAircraft>ActiveRunway::getFirstOfStatus(int stat) const
219 {
220 auto it = std::find_if(departureQueue.begin(), departureQueue.end(), [stat](const SGSharedPtr<FGAIAircraft>& acft) {
221 return acft->getTakeOffStatus() == stat;
222 });
223
224 if (it == departureQueue.end()) {
225 return {};
226 }
227
228 return *it;
229 }
230
231 SGSharedPtr<FGAIAircraft> ActiveRunway::getFirstAircraftInDepartureQueue() const
232 {
233 if (departureQueue.empty()) {
234 return {};
235 }
236
237 return departureQueue.front();
238 };
239
240 void ActiveRunway::addToDepartureQueue(FGAIAircraft *ac)
241 {
242 assert(ac);
243 assert(!ac->getDie());
244 departureQueue.push_back(ac);
245 };
246
247
248 /***************************************************************************
249 * FGTrafficRecord
250 **************************************************************************/
251
252 FGTrafficRecord::FGTrafficRecord():
253 id(0), waitsForId(0),
254 currentPos(0),
255 leg(0),
256 frequencyId(0),
257 state(0),
258 allowTransmission(true),
259 allowPushback(true),
260 priority(0),
261 timer(0),
262 heading(0), speed(0), altitude(0), radius(0)
263 {
264 }
265
266 FGTrafficRecord::~FGTrafficRecord()
267 {
268 }
269
270 void FGTrafficRecord::setPositionAndIntentions(int pos,
271 FGAIFlightPlan * route)
272 {
273 SG_LOG(SG_AI, SG_BULK, "Traffic record position: " << pos);
274 currentPos = pos;
275 if (!intentions.empty()) {
276 intVecIterator i = intentions.begin();
277 if ((*i) != currentPos) {
278 SG_LOG(SG_ATC, SG_ALERT,
279 "Error in FGTrafficRecord::setPositionAndIntentions at " << SG_ORIGIN << ", " << (*i));
280 }
281 intentions.erase(i);
282 } else {
283 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
284 int size = route->getNrOfWayPoints();
285 SG_LOG(SG_ATC, SG_DEBUG, "Setting pos to " << currentPos << " and intentions");
286 for (int i = 2; i < size; i++) {
287 int val = route->getRouteIndex(i);
288 intentions.push_back(val);
289 }
290 }
291 }
292
293 void FGTrafficRecord::setAircraft(FGAIAircraft *ref)
294 {
295 aircraft = ref;
296 }
297
298 bool FGTrafficRecord::isDead() const {
299 if (!aircraft) {
300 return true;
301 }
302 return aircraft->getDie();
303 }
304
305 void FGTrafficRecord::clearATCController() const {
306 if (aircraft) {
307 aircraft->clearATCController();
308 }
309 }
310
311 FGAIAircraft* FGTrafficRecord::getAircraft() const
312 {
313 if(aircraft.valid()) {
314 return aircraft.ptr();
315 }
316 return 0;
317 }
318 /**
319 * Check if another aircraft is ahead of the current one, and on the same taxiway
320 * @return true / false if this is/isn't the case.
321 */
322 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord & other)
323 {
324 bool result = false;
325 SG_LOG(SG_ATC, SG_BULK, getCallsign() << "| checkPositionAndIntentions CurrentPos : " << currentPos << " Other : " << other.currentPos << " Leg : " << leg << " Other Leg : " << other.leg );
326 if (currentPos == other.currentPos && getId() != other.getId() ) {
327 SG_LOG(SG_ATC, SG_BULK, getCallsign() << "| Check Position and intentions: " << other.getCallsign() << " we are on the same taxiway; Index = " << currentPos);
328 int headingTowards = SGGeodesy::courseDeg( other.getPos(), getPos() );
329 int headingDiff = SGMiscd::normalizePeriodic(-180, 180, headingTowards - getHeading() );
330 SG_LOG(SG_ATC, SG_BULK, getCallsign() << "| " << heading << "\t" << headingTowards << "\t" << headingDiff);
331 // getHeading()
332 result = abs(headingDiff) < 89;
333 }
334 // else if (! other.intentions.empty())
335 // {
336 // SG_LOG(SG_ATC, SG_BULK, "Start check 2");
337 // intVecIterator i = other.intentions.begin();
338 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
339 // i++;
340 // if (i != other.intentions.end()) {
341 // SG_LOG(SG_ATC, SG_BULK, "Check Position and intentions: current matches other.intentions");
342 // result = true;
343 // }
344 else if (!intentions.empty()) {
345 SG_LOG(SG_ATC, SG_BULK, getCallsign() << "| Itentions Size " << intentions.size());
346 intVecIterator i = intentions.begin();
347 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
348 while (i != intentions.end()) {
349 if ((*i) == other.currentPos) {
350 break;
351 }
352 ++i;
353 }
354 if (i != intentions.end()) {
355 SG_LOG(SG_ATC, SG_BULK, getCallsign() << "| Check Position and intentions: " << other.getCallsign()<< " matches Index = " << (*i));
356 int headingTowards = SGGeodesy::courseDeg( other.getPos(), getPos() );
357 int distanceM = SGGeodesy::distanceM( other.getPos(), getPos() );
358 int headingDiff = SGMiscd::normalizePeriodic(-180, 180, headingTowards - getHeading() );
359 SG_LOG(SG_ATC, SG_BULK, getCallsign() << "| Heading : " << heading << "\t Heading Other->Current" << headingTowards << "\t Heading Diff :" << headingDiff << "\t Distance : " << distanceM);
360 // difference of heading is small and it's actually near
361 result = abs(headingDiff) < 89 && distanceM < 400;
362 // result = true;
363 }
364 }
365 return result;
366 }
367
368 void FGTrafficRecord::setPositionAndHeading(double lat, double lon,
369 double hdg, double spd,
370 double alt)
371 {
372 this->pos = SGGeod::fromDegFt(lon, lat, alt);
373 heading = hdg;
374 speed = spd;
375 altitude = alt;
376 }
377
378 int FGTrafficRecord::crosses(FGGroundNetwork * net,
379 FGTrafficRecord & other)
380 {
381 if (checkPositionAndIntentions(other)
382 || (other.checkPositionAndIntentions(*this)))
383 return -1;
384 intVecIterator i, j;
385 int currentTargetNode = 0, otherTargetNode = 0;
386 if (currentPos > 0)
387 currentTargetNode = net->findSegment(currentPos)->getEnd()->getIndex(); // OKAY,...
388 if (other.currentPos > 0)
389 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
390 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
391 return currentTargetNode;
392 if (! intentions.empty()) {
393 for (i = intentions.begin(); i != intentions.end(); ++i) {
394 if ((*i) > 0) {
395 if (currentTargetNode ==
396 net->findSegment(*i)->getEnd()->getIndex()) {
397 SG_LOG(SG_ATC, SG_BULK, "Current crosses at " << currentTargetNode);
398 return currentTargetNode;
399 }
400 }
401 }
402 }
403 if (! other.intentions.empty()) {
404 for (i = other.intentions.begin(); i != other.intentions.end(); ++i) {
405 if ((*i) > 0) {
406 if (otherTargetNode ==
407 net->findSegment(*i)->getEnd()->getIndex()) {
408 SG_LOG(SG_ATC, SG_BULK, "Other crosses at " << currentTargetNode);
409 return otherTargetNode;
410 }
411 }
412 }
413 }
414 if (! intentions.empty() && ! other.intentions.empty()) {
415 for (i = intentions.begin(); i != intentions.end(); ++i) {
416 for (j = other.intentions.begin(); j != other.intentions.end(); ++j) {
417 SG_LOG(SG_ATC, SG_BULK, "finding segment " << *i << " and " << *j);
418 if (((*i) > 0) && ((*j) > 0)) {
419 currentTargetNode =
420 net->findSegment(*i)->getEnd()->getIndex();
421 otherTargetNode =
422 net->findSegment(*j)->getEnd()->getIndex();
423 if (currentTargetNode == otherTargetNode) {
424 SG_LOG(SG_ATC, SG_BULK, "Routes will cross at " << currentTargetNode);
425 return currentTargetNode;
426 }
427 }
428 }
429 }
430 }
431 return -1;
432 }
433
434 bool FGTrafficRecord::onRoute(FGGroundNetwork * net,
435 FGTrafficRecord & other)
436 {
437 int node = -1, othernode = -1;
438 if (currentPos > 0)
439 node = net->findSegment(currentPos)->getEnd()->getIndex();
440 if (other.currentPos > 0)
441 othernode =
442 net->findSegment(other.currentPos)->getEnd()->getIndex();
443 if ((node == othernode) && (node != -1))
444 return true;
445 if (! other.intentions.empty()) {
446 for (intVecIterator i = other.intentions.begin();
447 i != other.intentions.end(); ++i) {
448 if (*i > 0) {
449 othernode = net->findSegment(*i)->getEnd()->getIndex();
450 if ((node == othernode) && (node > -1))
451 return true;
452 }
453 }
454 }
455 //if (other.currentPos > 0)
456 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
457 //if (! intentions.empty())
458 // {
459 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
460 // {
461 // if (*i > 0)
462 // {
463 // node = net->findSegment(*i)->getEnd()->getIndex();
464 // if ((node == othernode) && (node > -1))
465 // return true;
466 // }
467 // }
468 // }
469 return false;
470 }
471
472
473 bool FGTrafficRecord::isOpposing(FGGroundNetwork * net,
474 FGTrafficRecord & other, int node)
475 {
476 // Check if current segment is the reverse segment for the other aircraft
477 SG_LOG(SG_ATC, SG_BULK, "Current segment " << currentPos);
478
479 if ((currentPos > 0) && (other.currentPos > 0)) {
480 FGTaxiSegment *opp = net->findSegment(currentPos)->opposite();
481 if (opp) {
482 if (opp->getIndex() == other.currentPos)
483 return true;
484 }
485
486 for (intVecIterator i = intentions.begin(); i != intentions.end(); ++i) {
487 if ((opp = net->findSegment(other.currentPos)->opposite())) {
488 if ((*i) > 0)
489 if (opp->getIndex() ==
490 net->findSegment(*i)->getIndex()) {
491 if (net->findSegment(*i)->getStart()->getIndex() ==
492 node) {
493 {
494 SG_LOG(SG_ATC, SG_BULK, "Found the node " << node);
495 return true;
496 }
497 }
498 }
499 }
500 if (! other.intentions.empty()) {
501 for (intVecIterator j = other.intentions.begin();
502 j != other.intentions.end(); ++j) {
503 SG_LOG(SG_ATC, SG_BULK, "Current segment 1 " << (*i));
504 if ((*i) > 0) {
505 if ((opp = net->findSegment(*i)->opposite())) {
506 if (opp->getIndex() ==
507 net->findSegment(*j)->getIndex()) {
508 SG_LOG(SG_ATC, SG_BULK, "Nodes " << net->findSegment(*i)->getIndex()
509 << " and " << net->findSegment(*j)->getIndex()
510 << " are opposites ");
511 if (net->findSegment(*i)->getStart()->
512 getIndex() == node) {
513 {
514 SG_LOG(SG_ATC, SG_BULK, "Found the node " << node);
515 return true;
516 }
517 }
518 }
519 }
520 }
521 }
522 }
523 }
524 }
525 return false;
526 }
527
528 bool FGTrafficRecord::isActive(int margin) const
529 {
530 if (aircraft->getDie()) {
531 return false;
532 }
533
534 time_t now = globals->get_time_params()->get_cur_time();
535 time_t deptime = aircraft->getTrafficRef()->getDepartureTime();
536 return ((now + margin) > deptime);
537 }
538
539
540 void FGTrafficRecord::setSpeedAdjustment(double spd)
541 {
542 instruction.setChangeSpeed(true);
543 instruction.setSpeed(spd);
544 }
545
546 void FGTrafficRecord::setHeadingAdjustment(double heading)
547 {
548 instruction.setChangeHeading(true);
549 instruction.setHeading(heading);
550 }
551
552 bool FGTrafficRecord::pushBackAllowed() const
553 {
554 return allowPushback;
555 }
556
557
558
559 /***************************************************************************
560 * FGATCInstruction
561 *
562 **************************************************************************/
563
564 FGATCInstruction::FGATCInstruction()
565 {
566 holdPattern = false;
567 holdPosition = false;
568 changeSpeed = false;
569 changeHeading = false;
570 changeAltitude = false;
571 resolveCircularWait = false;
572
573 speed = 0;
574 heading = 0;
575 alt = 0;
576 }
577
578 bool FGATCInstruction::hasInstruction() const
579 {
580 return (holdPattern || holdPosition || changeSpeed || changeHeading
581 || changeAltitude || resolveCircularWait);
582 }