1 // trafficrecord.cxx - Implementation of AIModels ATC code.
3 // Written by Durk Talsma, started September 2006.
5 // Copyright (C) 2006 Durk Talsma.
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.
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.
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.
33 #include <osg/Geometry>
34 #include <osg/MatrixTransform>
37 #include <simgear/scene/material/EffectGeode.hxx>
38 #include <simgear/scene/material/matlib.hxx>
39 #include <simgear/scene/material/mat.hxx>
40 #include <Scenery/scenery.hxx>
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 <AIModel/performancedb.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/simple.hxx>
53 #define WITH_POINT_TO_POINT
58 /***************************************************************************
60 **************************************************************************/
61 time_t ActiveRunway::requestTimeSlot(time_t eta)
64 time_t separation = 90;
66 if (estimatedArrivalTimes.size() == 0) {
67 estimatedArrivalTimes.push_back(eta);
70 TimeVectorIterator i = estimatedArrivalTimes.begin();
71 //cerr << "Checking eta slots " << eta << ": " << endl;
72 for (i = estimatedArrivalTimes.begin();
73 i != estimatedArrivalTimes.end(); i++) {
74 //cerr << "Stored time : " << (*i) << endl;
76 i = estimatedArrivalTimes.begin();
77 if ((eta + separation) < (*i)) {
80 //cerr << "Storing at beginning" << endl;
82 while ((i != estimatedArrivalTimes.end()) && (!found)) {
83 TimeVectorIterator j = i + 1;
84 if (j == estimatedArrivalTimes.end()) {
85 if (((*i) + separation) < eta) {
86 //cerr << "Storing at end" << endl;
89 newEta = (*i) + separation;
90 //cerr << "Storing at end + separation" << endl;
93 if ((((*j) - (*i)) > (separation * 2))) { // found a potential slot
94 // now check whether this slot is usable:
95 // 1) eta should fall between the two points
96 // i.e. eta > i AND eta < j
98 //cerr << "Found potential slot after " << (*i) << endl;
99 if (eta > (*i) && (eta < (*j))) {
101 if (eta < ((*i) + separation)) {
102 newEta = (*i) + separation;
103 //cerr << "Using original" << (*i) << " + separation " << endl;
106 //cerr << "Using original after " << (*i) << endl;
108 } else if (eta < (*i)) {
110 newEta = (*i) + separation;
111 //cerr << "Using delayed slot after " << (*i) << endl;
114 if (((*j) - separation) < eta) {
116 if (((*i) + separation) < eta) {
118 cerr << "Using original after " << (*i) << endl;
120 newEta = (*i) + separation;
121 cerr << "Using " << (*i) << " + separation " << endl;
129 //cerr << ". done. New ETA : " << newEta << endl;
131 estimatedArrivalTimes.push_back(newEta);
132 sort(estimatedArrivalTimes.begin(), estimatedArrivalTimes.end());
133 // do some housekeeping : remove any timestamps that are past
134 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
135 TimeVectorIterator i = estimatedArrivalTimes.begin();
136 while (i != estimatedArrivalTimes.end()) {
138 //cerr << "Deleting timestamp " << (*i) << " (now = " << now << "). " << endl;
139 estimatedArrivalTimes.erase(i);
140 i = estimatedArrivalTimes.begin();
148 /***************************************************************************
150 **************************************************************************/
151 FGTrafficRecord::FGTrafficRecord():
152 id(0), waitsForId(0),
157 allowTransmission(true),
158 latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0)
162 void FGTrafficRecord::setPositionAndIntentions(int pos,
163 FGAIFlightPlan * route)
167 if (intentions.size()) {
168 intVecIterator i = intentions.begin();
170 SG_LOG(SG_GENERAL, SG_ALERT,
171 "Error in FGTrafficRecord::setPositionAndIntentions");
172 //cerr << "Pos : " << pos << " Curr " << *(intentions.begin()) << endl;
173 for (intVecIterator i = intentions.begin();
174 i != intentions.end(); i++) {
175 //cerr << (*i) << " ";
181 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
182 int size = route->getNrOfWayPoints();
183 //cerr << "Setting pos" << pos << " ";
184 //cerr << "setting intentions ";
185 for (int i = 0; i < size; i++) {
186 int val = route->getRouteIndex(i);
188 if ((val) && (val != pos)) {
189 intentions.push_back(val);
194 //while (route->next(&legNr, &routeNr)) {
195 //intentions.push_back(routeNr);
197 //route->rewind(currentPos);
202 * Check if another aircraft is ahead of the current one, and on the same
203 * return true / false is the is/isn't the case.
205 ****************************************************************************/
207 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord & other)
210 //cerr << "Start check 1" << endl;
211 if (currentPos == other.currentPos) {
212 //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
215 // else if (other.intentions.size())
217 // cerr << "Start check 2" << endl;
218 // intVecIterator i = other.intentions.begin();
219 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
221 // if (i != other.intentions.end()) {
222 // cerr << "Check Position and intentions: current matches other.intentions" << endl;
225 else if (intentions.size()) {
226 //cerr << "Start check 3" << endl;
227 intVecIterator i = intentions.begin();
228 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
229 while (i != intentions.end()) {
230 if ((*i) == other.currentPos) {
235 if (i != intentions.end()) {
236 //cerr << callsign << ": Check Position and intentions: .other.current matches" << other.callsign << "Index = " << (*i) << endl;
240 //cerr << "Done !!" << endl;
244 void FGTrafficRecord::setPositionAndHeading(double lat, double lon,
245 double hdg, double spd,
255 int FGTrafficRecord::crosses(FGGroundNetwork * net,
256 FGTrafficRecord & other)
258 if (checkPositionAndIntentions(other)
259 || (other.checkPositionAndIntentions(*this)))
262 int currentTargetNode = 0, otherTargetNode = 0;
264 currentTargetNode = net->findSegment(currentPos)->getEnd()->getIndex(); // OKAY,...
265 if (other.currentPos > 0)
266 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
267 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
268 return currentTargetNode;
269 if (intentions.size()) {
270 for (i = intentions.begin(); i != intentions.end(); i++) {
272 if ((currentTargetNode ==
273 net->findSegment(*i)->getEnd()->getIndex())) {
274 //cerr << "Current crosses at " << currentTargetNode <<endl;
275 return currentTargetNode;
280 if (other.intentions.size()) {
281 for (i = other.intentions.begin(); i != other.intentions.end();
284 if (otherTargetNode ==
285 net->findSegment(*i)->getEnd()->getIndex()) {
286 //cerr << "Other crosses at " << currentTargetNode <<endl;
287 return otherTargetNode;
292 if (intentions.size() && other.intentions.size()) {
293 for (i = intentions.begin(); i != intentions.end(); i++) {
294 for (j = other.intentions.begin(); j != other.intentions.end();
296 //cerr << "finding segment " << *i << " and " << *j << endl;
297 if (((*i) > 0) && ((*j) > 0)) {
299 net->findSegment(*i)->getEnd()->getIndex();
301 net->findSegment(*j)->getEnd()->getIndex();
302 if (currentTargetNode == otherTargetNode) {
303 //cerr << "Routes will cross at " << currentTargetNode << endl;
304 return currentTargetNode;
313 bool FGTrafficRecord::onRoute(FGGroundNetwork * net,
314 FGTrafficRecord & other)
316 int node = -1, othernode = -1;
318 node = net->findSegment(currentPos)->getEnd()->getIndex();
319 if (other.currentPos > 0)
321 net->findSegment(other.currentPos)->getEnd()->getIndex();
322 if ((node == othernode) && (node != -1))
324 if (other.intentions.size()) {
325 for (intVecIterator i = other.intentions.begin();
326 i != other.intentions.end(); i++) {
328 othernode = net->findSegment(*i)->getEnd()->getIndex();
329 if ((node == othernode) && (node > -1))
334 //if (other.currentPos > 0)
335 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
336 //if (intentions.size())
338 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
342 // node = net->findSegment(*i)->getEnd()->getIndex();
343 // if ((node == othernode) && (node > -1))
352 bool FGTrafficRecord::isOpposing(FGGroundNetwork * net,
353 FGTrafficRecord & other, int node)
355 // Check if current segment is the reverse segment for the other aircraft
357 //cerr << "Current segment " << currentPos << endl;
358 if ((currentPos > 0) && (other.currentPos > 0)) {
359 opp = net->findSegment(currentPos)->opposite();
361 if (opp->getIndex() == other.currentPos)
365 for (intVecIterator i = intentions.begin(); i != intentions.end();
367 if ((opp = net->findSegment(other.currentPos)->opposite())) {
369 if (opp->getIndex() ==
370 net->findSegment(*i)->getIndex()) {
371 if (net->findSegment(*i)->getStart()->getIndex() ==
374 //cerr << "Found the node " << node << endl;
380 if (other.intentions.size()) {
381 for (intVecIterator j = other.intentions.begin();
382 j != other.intentions.end(); j++) {
383 // cerr << "Current segment 1 " << (*i) << endl;
385 if ((opp = net->findSegment(*i)->opposite())) {
386 if (opp->getIndex() ==
387 net->findSegment(*j)->getIndex()) {
388 //cerr << "Nodes " << net->findSegment(*i)->getIndex()
389 // << " and " << net->findSegment(*j)->getIndex()
390 // << " are opposites " << endl;
391 if (net->findSegment(*i)->getStart()->
392 getIndex() == node) {
394 //cerr << "Found the node " << node << endl;
408 void FGTrafficRecord::setSpeedAdjustment(double spd)
410 instruction.setChangeSpeed(true);
411 instruction.setSpeed(spd);
414 void FGTrafficRecord::setHeadingAdjustment(double heading)
416 instruction.setChangeHeading(true);
417 instruction.setHeading(heading);
420 bool FGTrafficRecord::pushBackAllowed()
422 // With the user ATC / AI integration, checking whether the user's aircraft is near no longer works, because
423 // this will effectively block the user's aircraft itself from receiving pushback clearance.
424 // So, what can we do?
426 double course, az2, dist;
427 SGGeod curr(SGGeod::fromDegM(getLongitude(),
428 getLatitude(), getAltitude()));
430 double userLatitude = fgGetDouble("/position/latitude-deg");
431 double userLongitude = fgGetDouble("/position/longitude-deg");
432 SGGeod user(SGGeod::fromDeg(userLongitude, userLatitude));
433 SGGeodesy::inverse(curr, user, course, az2, dist);
434 //cerr << "Distance to user : " << dist << endl;
439 // In essence, we should check whether the pusbback route itself, as well as the associcated
440 // taxiways near the pushback point are free of traffic.
441 // To do so, we need to
447 /***************************************************************************
450 **************************************************************************/
451 FGATCInstruction::FGATCInstruction()
454 holdPosition = false;
456 changeHeading = false;
457 changeAltitude = false;
458 resolveCircularWait = false;
466 bool FGATCInstruction::hasInstruction()
468 return (holdPattern || holdPosition || changeSpeed || changeHeading
469 || changeAltitude || resolveCircularWait);
472 /***************************************************************************
475 **************************************************************************/
480 FGATCController::FGATCController()
482 //cerr << "running FGATController constructor" << endl;
485 lastTransmission = 0;
489 FGATCController::~FGATCController()
491 //cerr << "running FGATController destructor" << endl;
494 string FGATCController::getGateName(FGAIAircraft * ref)
496 return ref->atGate();
499 bool FGATCController::isUserAircraft(FGAIAircraft* ac)
501 return (ac->getCallSign() == fgGetString("/sim/multiplay/callsign")) ? true : false;
504 void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent, AtcMsgId msgId,
505 AtcMsgDir msgDir, bool audible)
507 string sender, receiver;
512 string atisInformation;
521 string transponderCode;
524 string instructionText;
528 sender = rec->getAircraft()->getTrafficRef()->getCallSign();
529 if (rec->getAircraft()->getTaxiClearanceRequest()) {
530 instructionText = "push-back and taxi";
532 instructionText = "taxi";
534 //cerr << "transmitting for: " << sender << "Leg = " << rec->getLeg() << endl;
535 switch (rec->getLeg()) {
538 freqId = rec->getNextFrequency();
540 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
541 getDynamics()->getGroundFrequency(rec->getLeg() + freqId);
543 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
544 getDynamics()->getGroundFrequency(2);
546 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
547 getDynamics()->getTowerFrequency(2);
549 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
550 getName() + "-Ground";
552 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
553 getDynamics()->getAtisSequence();
557 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
558 getName() + "-Tower";
561 // Swap sender and receiver value in case of a ground to air transmission
562 if (msgDir == ATC_GROUND_TO_AIR) {
569 case MSG_ANNOUNCE_ENGINE_START:
570 text = sender + ". Ready to Start up";
572 case MSG_REQUEST_ENGINE_START:
574 receiver + ", This is " + sender + ". Position " +
575 getGateName(rec->getAircraft()) + ". Information " +
576 atisInformation + ". " +
577 rec->getAircraft()->getTrafficRef()->getFlightRules() +
579 rec->getAircraft()->getTrafficRef()->getArrivalAirport()->
580 getName() + ". Request start-up";
582 // Acknowledge engine startup permission
583 // Assign departure runway
584 // Assign SID, if necessery (TODO)
585 case MSG_PERMIT_ENGINE_START:
586 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
588 heading = rec->getAircraft()->getTrafficRef()->getCourse();
589 fltType = rec->getAircraft()->getTrafficRef()->getFlightType();
591 rec->getAircraft()->GetFlightPlan()->
592 getRunwayClassFromTrafficType(fltType);
594 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
595 getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
597 rec->getAircraft()->GetFlightPlan()->setRunway(activeRunway);
598 fp = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
599 getDynamics()->getSID(activeRunway, heading);
600 rec->getAircraft()->GetFlightPlan()->setSID(fp);
602 SID = fp->getName() + " departure";
604 SID = "fly runway heading ";
606 //snprintf(buffer, 7, "%3.2f", heading);
607 fltRules = rec->getAircraft()->getTrafficRef()->getFlightRules();
608 transponderCode = genTransponderCode(fltRules);
609 rec->getAircraft()->SetTransponderCode(transponderCode);
611 receiver + ". Start-up approved. " + atisInformation +
612 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
613 transponderCode + ". " +
614 "For "+ instructionText + " clearance call " + taxiFreqStr + ". " +
615 sender + " control.";
617 case MSG_DENY_ENGINE_START:
618 text = receiver + ". Standby";
620 case MSG_ACKNOWLEDGE_ENGINE_START:
621 fp = rec->getAircraft()->GetFlightPlan()->getSID();
624 rec->getAircraft()->GetFlightPlan()->getSID()->getName() +
627 SID = "fly runway heading ";
629 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
630 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
631 transponderCode = rec->getAircraft()->GetTransponderCode();
634 receiver + ". Start-up approved. " + atisInformation +
635 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
636 transponderCode + ". " +
637 "For " + instructionText + " clearance call " + taxiFreqStr + ". " +
640 case MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY:
641 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
642 text = receiver + ". Switching to " + taxiFreqStr + ". " + sender;
644 case MSG_INITIATE_CONTACT:
645 text = receiver + ". With you. " + sender;
647 case MSG_ACKNOWLEDGE_INITIATE_CONTACT:
648 text = receiver + ". Roger. " + sender;
650 case MSG_REQUEST_PUSHBACK_CLEARANCE:
651 if (rec->getAircraft()->getTaxiClearanceRequest()) {
652 text = receiver + ". Request push-back. " + sender;
654 text = receiver + ". Request Taxi clearance. " + sender;
657 case MSG_PERMIT_PUSHBACK_CLEARANCE:
658 if (rec->getAircraft()->getTaxiClearanceRequest()) {
659 text = receiver + ". Push-back approved. " + sender;
661 text = receiver + ". Cleared to Taxi." + sender;
664 case MSG_HOLD_PUSHBACK_CLEARANCE:
665 text = receiver + ". Standby. " + sender;
667 case MSG_REQUEST_TAXI_CLEARANCE:
668 text = receiver + ". Ready to Taxi. " + sender;
670 case MSG_ISSUE_TAXI_CLEARANCE:
671 text = receiver + ". Cleared to taxi. " + sender;
673 case MSG_ACKNOWLEDGE_TAXI_CLEARANCE:
674 text = receiver + ". Cleared to taxi. " + sender;
676 case MSG_HOLD_POSITION:
677 text = receiver + ". Hold Position. " + sender;
679 case MSG_ACKNOWLEDGE_HOLD_POSITION:
680 text = receiver + ". Holding Position. " + sender;
682 case MSG_RESUME_TAXI:
683 text = receiver + ". Resume Taxiing. " + sender;
685 case MSG_ACKNOWLEDGE_RESUME_TAXI:
686 text = receiver + ". Continuing Taxi. " + sender;
688 case MSG_REPORT_RUNWAY_HOLD_SHORT:
689 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
690 //activeRunway = "test";
691 text = receiver + ". Holding short runway "
695 //cerr << "1 Currently at leg " << rec->getLeg() << endl;
697 case MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT:
698 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
699 text = receiver + "Roger. Holding short runway "
703 //cerr << "2 Currently at leg " << rec->getLeg() << endl;
705 case MSG_SWITCH_TOWER_FREQUENCY:
706 towerFreqStr = formatATCFrequency3_2(towerFreq);
707 text = receiver + "Contact Tower at " + towerFreqStr + ". " + sender;
709 //cerr << "3 Currently at leg " << rec->getLeg() << endl;
711 case MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY:
712 towerFreqStr = formatATCFrequency3_2(towerFreq);
713 text = receiver + "Roger, switching to tower at " + towerFreqStr + ". " + sender;
715 //cerr << "4 Currently at leg " << rec->getLeg() << endl;
719 text = text + sender + ". Transmitting unknown Message";
723 double onBoardRadioFreq0 =
724 fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
725 double onBoardRadioFreq1 =
726 fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
727 int onBoardRadioFreqI0 = (int) floor(onBoardRadioFreq0 * 100 + 0.5);
728 int onBoardRadioFreqI1 = (int) floor(onBoardRadioFreq1 * 100 + 0.5);
729 //cerr << "Using " << onBoardRadioFreq0 << ", " << onBoardRadioFreq1 << " and " << stationFreq << " for " << text << endl;
731 // Display ATC message only when one of the radios is tuned
732 // the relevant frequency.
733 // Note that distance attenuation is currently not yet implemented
735 if ((onBoardRadioFreqI0 == stationFreq)
736 || (onBoardRadioFreqI1 == stationFreq)) {
738 if (rec->allowTransmissions()) {
739 double snr = calculate_attenuation(rec, parent, ground_to_air);
742 if (snr > 0 && snr < 12) {
743 //for low SNR values implement a way to make the conversation
744 //hard to understand but audible
745 //how this works in the real world, is the receiver AGC fails to capture the slope
746 //and the signal, due to being amplitude modulated, decreases volume after demodulation
747 //the implementation below is more akin to what would happen on a FM transmission
748 //therefore the correct way would be to work on the volume
749 string hash_noise = " ";
750 int reps = fabs((int)snr - 11);
751 int t_size = text.size();
752 for (int n=1;n<=reps * 2;n++) {
753 int pos = rand() % t_size -1;
754 text.replace(pos,1, hash_noise);
758 fgSetString("/sim/messages/atc", text.c_str());
762 FGATCManager *atc = (FGATCManager*) globals->get_subsystem("atc");
763 atc->getATCDialog()->addEntry(1, text);
768 double FGATCController::calculate_attenuation(FGTrafficRecord * rec, FGAirportDynamics *parent,
771 /// Implement radio attenuation
772 /// based on the Longley-Rice propagation model
774 FGScenery * scenery = globals->get_scenery();
775 // player aircraft position
776 double own_lat = fgGetDouble("/position/latitude-deg");
777 double own_lon = fgGetDouble("/position/longitude-deg");
778 double own_alt_ft = fgGetDouble("/position/altitude-ft");
779 double own_alt= own_alt_ft * SG_FEET_TO_METER;
781 //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
783 SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
784 SGGeod max_own_pos = SGGeod::fromDegM( own_lon, own_lat, SG_MAX_ELEVATION_M );
785 SGGeoc center = SGGeoc::fromGeod( max_own_pos );
786 SGGeoc own_pos_c = SGGeoc::fromGeod( own_pos );
788 // position of sender radio antenna (HAAT)
789 // sender can be aircraft or ground station
790 double ATC_HAAT = 30.0;
791 double Aircraft_HAAT = 5.0;
792 double sender_alt_ft,sender_alt;
793 double transmitter_height=0.0;
794 double receiver_height=0.0;
796 SGGeod max_sender_pos;
798 sender_alt_ft = parent->getElevation();
799 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
800 sender_pos= SGGeod::fromDegM( parent->getLongitude(),
801 parent->getLatitude(), sender_alt );
802 max_sender_pos = SGGeod::fromDegM( parent->getLongitude(),
803 parent->getLatitude(), SG_MAX_ELEVATION_M );
806 sender_alt_ft = rec->getAltitude();
807 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
808 sender_pos= SGGeod::fromDegM( rec->getLongitude(),
809 rec->getLatitude(), sender_alt );
810 max_sender_pos = SGGeod::fromDegM( rec->getLongitude(),
811 rec->getLatitude(), SG_MAX_ELEVATION_M );
813 SGGeoc sender_pos_c = SGGeoc::fromGeod( sender_pos );
814 //cerr << "ITM:: sender Lat: " << parent->getLatitude() << ", Lon: " << parent->getLongitude() << ", Alt: " << sender_alt << endl;
816 double point_distance= 90.0; // regular SRTM is 90 meters
817 double course = SGGeodesy::courseRad(own_pos_c, sender_pos_c);
818 double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
819 double probe_distance = 0.0;
822 double max_points = distance_m / point_distance;
823 deque<double> _elevations;
825 //SGGeod probe_pilot = SGGeod::fromGeoc(center.advanceRadM( course, 0 ));
826 SGGeod probe_pilot = max_own_pos;
827 double elevation_under_pilot = 0.0;
828 if (scenery->get_elevation_m( probe_pilot, elevation_under_pilot, NULL )) {
829 receiver_height = own_alt - elevation_under_pilot + 3; //assume antenna located 3 meters above ground
833 //SGGeod probe_sender = SGGeod::fromGeoc(center.advanceRadM( course, distance_m ));
834 SGGeod probe_sender = max_sender_pos;
835 double elevation_under_sender = 0.0;
836 if (scenery->get_elevation_m( probe_sender, elevation_under_sender, NULL )) {
837 transmitter_height = sender_alt - elevation_under_sender;
840 transmitter_height += ATC_HAAT;
842 transmitter_height += Aircraft_HAAT;
844 cerr << "ITM:: RCVhgt: " << receiver_height << ", TRXhgt: " << transmitter_height << ", Distance: " << distance_m << endl;
845 // If distance larger than this value (400 km), assume reception imposssible
846 // technically 400 km is no problem if LOS conditions exist,
847 // but we do this to spare resources
848 if (distance_m > 400000)
851 unsigned int e_size = (deque<unsigned>::size_type)max_points;
853 while (_elevations.size() <= e_size) {
854 probe_distance += point_distance;
855 SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, probe_distance ));
857 double elevation_m = 0.0;
859 if (scenery->get_elevation_m( probe, elevation_m, NULL )) {
860 _elevations.push_front(elevation_m);
861 //cerr << "ITM:: Probe elev: " << elevation_m << endl;
864 _elevations.push_back(elevation_under_pilot);
865 _elevations.push_front(elevation_under_sender);
866 double max_alt_between=0.0;
867 for( deque<double>::size_type i = 0; i < _elevations.size(); i++ ) {
868 if (_elevations[i] > max_alt_between) {
869 max_alt_between = _elevations[i];
873 double num_points= (double)_elevations.size();
874 //cerr << "ITM:: Max alt between: " << max_alt_between << ", num points:" << num_points << endl;
875 _elevations.push_front(point_distance);
876 _elevations.push_front(num_points -1);
877 int size = _elevations.size();
878 double itm_elev[size];
879 for(int i=0;i<size;i++) {
880 itm_elev[i]=_elevations[i];
881 //cerr << "ITM:: itm_elev: " << _elevations[i] << endl;
884 ////////////// ITM default parameters //////////////
885 // later perhaps take them from tile materials?
886 double eps_dielect=15.0;
887 double sgm_conductivity = 0.005;
889 double frq_mhz = 125.0; // middle of bandplan
890 int radio_climate = 5; // continental temperate
891 int pol=1; // assuming vertical polarization although this is more complex in reality
892 double conf = 0.90; // my own tests in Radiomobile have worked best with these values
893 double rel = 0.80; // ^^
898 /////////// radio parameters ///////////
899 double receiver_sensitivity = -110.0; // typical AM receiver sensitivity seems to be 0.8 microVolt at 12dB SINAD
900 // AM transmitter power in dBm.
901 // Note this value is calculated from the typical final transistor stage output
902 // !!! small aircraft have portable transmitters which operate at 36 dBm output (4 Watts)
903 // later store this value in aircraft description
904 // ATC comms usually operate high power equipment, thus making the link asymetrical; this is ignored for now
905 double transmitter_power = 43.0;
906 double antenna_gain = 2.0; //real-life gain for conventional monopole/dipole antenna
908 transmitter_power = 49.0;
910 transmitter_power = 43.0;
912 antenna_gain = 5.0; //pilot plane's antenna gain + ground station antenna gain
914 antenna_gain = 2.0; //pilot plane's antenna gain + AI aircraft antenna gain
915 double link_budget = transmitter_power - receiver_sensitivity + antenna_gain;
918 // first Fresnel zone radius
919 // frequency in the middle of the bandplan, more accuracy is not necessary
920 double fz_clr= 8.657 * sqrt(distance_m / 0.125);
922 // TODO: If we clear the first Fresnel zone, we are into line of sight teritory
924 // else we need to calculate point to point link loss
926 point_to_point(itm_elev, transmitter_height, receiver_height,
927 eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
928 pol, conf, rel, dbloss, strmode, errnum);
930 cerr << "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum << endl;
932 //if (errnum !=0 && errnum !=1)
934 double snr = link_budget - dbloss;
939 string FGATCController::formatATCFrequency3_2(int freq)
942 snprintf(buffer, 7, "%3.2f", ((float) freq / 100.0));
943 return string(buffer);
946 // TODO: Set transponder codes according to real-world routes.
947 // The current version just returns a random string of four octal numbers.
948 string FGATCController::genTransponderCode(string fltRules)
950 if (fltRules == "VFR") {
951 return string("1200");
954 snprintf(buffer, 5, "%d%d%d%d", rand() % 8, rand() % 8, rand() % 8,
956 return string(buffer);
960 void FGATCController::init()
963 FGATCManager *mgr = (FGATCManager*) globals->get_subsystem("ATC");
964 mgr->addController(this);
969 /***************************************************************************
970 * class FGTowerController
972 **************************************************************************/
973 FGTowerController::FGTowerController(FGAirportDynamics *par) :
980 void FGTowerController::announcePosition(int id,
981 FGAIFlightPlan * intendedRoute,
982 int currentPosition, double lat,
983 double lon, double heading,
984 double speed, double alt,
985 double radius, int leg,
989 TrafficVectorIterator i = activeTraffic.begin();
990 // Search whether the current id alread has an entry
991 // This might be faster using a map instead of a vector, but let's start by taking a safe route
992 if (activeTraffic.size()) {
993 //while ((i->getId() != id) && i != activeTraffic.end()) {
994 while (i != activeTraffic.end()) {
995 if (i->getId() == id) {
1001 // Add a new TrafficRecord if no one exsists for this aircraft.
1002 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1003 FGTrafficRecord rec;
1006 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1007 rec.setRunway(intendedRoute->getRunway());
1009 //rec.setCallSign(callsign);
1010 rec.setRadius(radius);
1011 rec.setAircraft(ref);
1012 activeTraffic.push_back(rec);
1013 // Don't just schedule the aircraft for the tower controller, also assign if to the correct active runway.
1014 ActiveRunwayVecIterator rwy = activeRunways.begin();
1015 if (activeRunways.size()) {
1016 while (rwy != activeRunways.end()) {
1017 if (rwy->getRunwayName() == intendedRoute->getRunway()) {
1023 if (rwy == activeRunways.end()) {
1024 ActiveRunway aRwy(intendedRoute->getRunway(), id);
1025 aRwy.addToDepartureCue(ref);
1026 activeRunways.push_back(aRwy);
1027 rwy = (activeRunways.end()-1);
1029 rwy->addToDepartureCue(ref);
1032 //cerr << ref->getTrafficRef()->getCallSign() << " You are number " << rwy->getDepartureCueSize() << " for takeoff " << endl;
1034 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1038 void FGTowerController::updateAircraftInformation(int id, double lat, double lon,
1039 double heading, double speed, double alt,
1042 TrafficVectorIterator i = activeTraffic.begin();
1043 // Search whether the current id has an entry
1044 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1045 TrafficVectorIterator current, closest;
1046 if (activeTraffic.size()) {
1047 //while ((i->getId() != id) && i != activeTraffic.end()) {
1048 while (i != activeTraffic.end()) {
1049 if (i->getId() == id) {
1055 // // update position of the current aircraft
1056 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1057 SG_LOG(SG_GENERAL, SG_ALERT,
1058 "AI error: updating aircraft without traffic record");
1060 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1063 setDt(getDt() + dt);
1065 // see if we already have a clearance record for the currently active runway
1066 // NOTE: dd. 2011-08-07: Because the active runway has been constructed in the announcePosition function, we may safely assume that is
1067 // already exists here. So, we can simplify the current code.
1068 ActiveRunwayVecIterator rwy = activeRunways.begin();
1069 while (rwy != activeRunways.end()) {
1070 if (rwy->getRunwayName() == current->getRunway()) {
1076 // only bother running the following code if the current aircraft is the
1077 // first in line for depature
1078 /* if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
1079 if (rwy->getCleared()) {
1080 if (id == rwy->getCleared()) {
1081 current->setHoldPosition(false);
1083 current->setHoldPosition(true);
1086 // For now. At later stages, this will probably be the place to check for inbound traffc.
1087 rwy->setCleared(id);
1090 // only bother with aircraft that have a takeoff status of 2, since those are essentially under tower control
1091 if (current->getAircraft()->getTakeOffStatus() == 2) {
1092 current->setHoldPosition(true);
1093 int clearanceId = rwy->getCleared();
1095 if (id == clearanceId) {
1096 current->setHoldPosition(false);
1099 if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
1100 rwy->setCleared(id);
1107 void FGTowerController::signOff(int id)
1109 TrafficVectorIterator i = activeTraffic.begin();
1110 // Search search if the current id alread has an entry
1111 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1112 if (activeTraffic.size()) {
1113 //while ((i->getId() != id) && i != activeTraffic.end()) {
1114 while (i != activeTraffic.end()) {
1115 if (i->getId() == id) {
1121 // If this aircraft has left the runway, we can clear the departure record for this runway
1122 ActiveRunwayVecIterator rwy = activeRunways.begin();
1123 if (activeRunways.size()) {
1124 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
1125 while (rwy != activeRunways.end()) {
1126 if (rwy->getRunwayName() == i->getRunway()) {
1131 if (rwy != activeRunways.end()) {
1133 rwy->updateDepartureCue();
1135 SG_LOG(SG_GENERAL, SG_ALERT,
1136 "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff");
1139 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1140 SG_LOG(SG_GENERAL, SG_ALERT,
1141 "AI error: Aircraft without traffic record is signing off from tower");
1143 i->getAircraft()->resetTakeOffStatus();
1144 i = activeTraffic.erase(i);
1145 //cerr << "Signing off from tower controller" << endl;
1150 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
1151 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
1152 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
1153 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
1154 // Note that this function is probably obsolete
1155 bool FGTowerController::hasInstruction(int id)
1157 TrafficVectorIterator i = activeTraffic.begin();
1158 // Search search if the current id has an entry
1159 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1160 if (activeTraffic.size()) {
1161 //while ((i->getId() != id) && i != activeTraffic.end()) {
1162 while (i != activeTraffic.end()) {
1163 if (i->getId() == id) {
1169 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1170 SG_LOG(SG_GENERAL, SG_ALERT,
1171 "AI error: checking ATC instruction for aircraft without traffic record");
1173 return i->hasInstruction();
1179 FGATCInstruction FGTowerController::getInstruction(int id)
1181 TrafficVectorIterator i = activeTraffic.begin();
1182 // Search search if the current id has an entry
1183 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1184 if (activeTraffic.size()) {
1185 //while ((i->getId() != id) && i != activeTraffic.end()) {
1186 while (i != activeTraffic.end()) {
1187 if (i->getId() == id) {
1193 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1194 SG_LOG(SG_GENERAL, SG_ALERT,
1195 "AI error: requesting ATC instruction for aircraft without traffic record");
1197 return i->getInstruction();
1199 return FGATCInstruction();
1202 void FGTowerController::render(bool visible) {
1203 //cerr << "FGTowerController::render function not yet implemented" << endl;
1206 string FGTowerController::getName() {
1207 return string(parent->getId() + "-tower");
1212 /***************************************************************************
1213 * class FGStartupController
1215 **************************************************************************/
1216 FGStartupController::FGStartupController(FGAirportDynamics *par):
1222 void FGStartupController::announcePosition(int id,
1223 FGAIFlightPlan * intendedRoute,
1224 int currentPosition, double lat,
1225 double lon, double heading,
1226 double speed, double alt,
1227 double radius, int leg,
1231 TrafficVectorIterator i = activeTraffic.begin();
1232 // Search whether the current id alread has an entry
1233 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1234 if (activeTraffic.size()) {
1235 //while ((i->getId() != id) && i != activeTraffic.end()) {
1236 while (i != activeTraffic.end()) {
1237 if (i->getId() == id) {
1243 // Add a new TrafficRecord if no one exsists for this aircraft.
1244 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1245 FGTrafficRecord rec;
1248 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1249 rec.setRunway(intendedRoute->getRunway());
1251 rec.setPositionAndIntentions(currentPosition, intendedRoute);
1252 //rec.setCallSign(callsign);
1253 rec.setAircraft(ref);
1254 rec.setHoldPosition(true);
1255 activeTraffic.push_back(rec);
1257 i->setPositionAndIntentions(currentPosition, intendedRoute);
1258 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1264 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
1265 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
1266 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
1267 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
1268 // Note that this function is probably obsolete
1269 bool FGStartupController::hasInstruction(int id)
1271 TrafficVectorIterator i = activeTraffic.begin();
1272 // Search search if the current id has an entry
1273 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1274 if (activeTraffic.size()) {
1275 //while ((i->getId() != id) && i != activeTraffic.end()) {
1276 while (i != activeTraffic.end()) {
1277 if (i->getId() == id) {
1283 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1284 SG_LOG(SG_GENERAL, SG_ALERT,
1285 "AI error: checking ATC instruction for aircraft without traffic record");
1287 return i->hasInstruction();
1293 FGATCInstruction FGStartupController::getInstruction(int id)
1295 TrafficVectorIterator i = activeTraffic.begin();
1296 // Search search if the current id has an entry
1297 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1298 if (activeTraffic.size()) {
1299 //while ((i->getId() != id) && i != activeTraffic.end()) {
1300 while (i != activeTraffic.end()) {
1301 if (i->getId() == id) {
1307 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1308 SG_LOG(SG_GENERAL, SG_ALERT,
1309 "AI error: requesting ATC instruction for aircraft without traffic record");
1311 return i->getInstruction();
1313 return FGATCInstruction();
1316 void FGStartupController::signOff(int id)
1318 TrafficVectorIterator i = activeTraffic.begin();
1319 // Search search if the current id alread has an entry
1320 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1321 if (activeTraffic.size()) {
1322 //while ((i->getId() != id) && i != activeTraffic.end()) {
1323 while (i != activeTraffic.end()) {
1324 if (i->getId() == id) {
1330 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1331 SG_LOG(SG_GENERAL, SG_ALERT,
1332 "AI error: Aircraft without traffic record is signing off from tower");
1334 //cerr << i->getAircraft()->getCallSign() << " signing off from startupcontroller" << endl;
1335 i = activeTraffic.erase(i);
1339 bool FGStartupController::checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
1342 int state = i->getState();
1343 if ((state == st) && available) {
1344 if ((msgDir == ATC_AIR_TO_GROUND) && isUserAircraft(i->getAircraft())) {
1346 //cerr << "Checking state " << st << " for " << i->getAircraft()->getCallSign() << endl;
1347 static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
1348 int n = trans_num->getIntValue();
1350 trans_num->setIntValue(-1);
1351 // PopupCallback(n);
1352 //cerr << "Selected transmission message " << n << endl;
1353 FGATCManager *atc = (FGATCManager*) globals->get_subsystem("atc");
1354 atc->getATCDialog()->removeEntry(1);
1356 //cerr << "creading message for " << i->getAircraft()->getCallSign() << endl;
1357 transmit(&(*i), &(*parent), msgId, msgDir, false);
1361 if (now > startTime) {
1362 //cerr << "Transmitting startup msg" << endl;
1363 transmit(&(*i), &(*parent), msgId, msgDir, true);
1365 lastTransmission = now;
1373 void FGStartupController::updateAircraftInformation(int id, double lat, double lon,
1374 double heading, double speed, double alt,
1377 TrafficVectorIterator i = activeTraffic.begin();
1378 // Search search if the current id has an entry
1379 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1380 TrafficVectorIterator current, closest;
1381 if (activeTraffic.size()) {
1382 //while ((i->getId() != id) && i != activeTraffic.end()) {
1383 while (i != activeTraffic.end()) {
1384 if (i->getId() == id) {
1390 // // update position of the current aircraft
1392 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1393 SG_LOG(SG_GENERAL, SG_ALERT,
1394 "AI error: updating aircraft without traffic record");
1396 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1399 setDt(getDt() + dt);
1401 int state = i->getState();
1403 // The user controlled aircraft should have crased here, because it doesn't have a traffic reference.
1404 // NOTE: if we create a traffic schedule for the user aircraft, we can use this to plan a flight.
1405 time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime();
1406 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1407 //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
1408 // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
1409 // << " at parking " << getGateName(i->getAircraft()) << endl;
1411 if ((now - lastTransmission) > 3 + (rand() % 15)) {
1415 checkTransmissionState(0, now, (startTime + 0 ), i, MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND);
1416 checkTransmissionState(1, now, (startTime + 60 ), i, MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND);
1417 checkTransmissionState(2, now, (startTime + 80 ), i, MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR);
1418 checkTransmissionState(3, now, (startTime + 100), i, MSG_ACKNOWLEDGE_ENGINE_START, ATC_AIR_TO_GROUND);
1419 if (checkTransmissionState(4, now, (startTime + 130), i, MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY, ATC_AIR_TO_GROUND)) {
1422 checkTransmissionState(5, now, (startTime + 140), i, MSG_INITIATE_CONTACT, ATC_AIR_TO_GROUND);
1423 checkTransmissionState(6, now, (startTime + 150), i, MSG_ACKNOWLEDGE_INITIATE_CONTACT, ATC_GROUND_TO_AIR);
1424 checkTransmissionState(7, now, (startTime + 180), i, MSG_REQUEST_PUSHBACK_CLEARANCE, ATC_AIR_TO_GROUND);
1428 if ((state == 8) && available) {
1429 if (now > startTime + 200) {
1430 if (i->pushBackAllowed()) {
1431 i->allowRepeatedTransmissions();
1432 transmit(&(*i), &(*parent), MSG_PERMIT_PUSHBACK_CLEARANCE,
1433 ATC_GROUND_TO_AIR, true);
1436 transmit(&(*i), &(*parent), MSG_HOLD_PUSHBACK_CLEARANCE,
1437 ATC_GROUND_TO_AIR, true);
1438 i->suppressRepeatedTransmissions();
1440 lastTransmission = now;
1444 if ((state == 9) && available) {
1445 i->setHoldPosition(false);
1449 // Note that this function is copied from simgear. for maintanance purposes, it's probabtl better to make a general function out of that.
1450 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
1451 double lon, double elev, double hdg, double slope)
1453 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
1454 obj_pos = geod.makeZUpFrame();
1455 // hdg is not a compass heading, but a counter-clockwise rotation
1456 // around the Z axis
1457 obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
1459 obj_pos.preMult(osg::Matrix::rotate(slope * SGD_DEGREES_TO_RADIANS,
1464 void FGStartupController::render(bool visible)
1467 SGMaterialLib *matlib = globals->get_matlib();
1470 globals->get_scenery()->get_scene_graph()->removeChild(group);
1471 //while (group->getNumChildren()) {
1472 // cerr << "Number of children: " << group->getNumChildren() << endl;
1473 //simgear::EffectGeode* geode = (simgear::EffectGeode*) group->getChild(0);
1474 //osg::MatrixTransform *obj_trans = (osg::MatrixTransform*) group->getChild(0);
1475 //geode->releaseGLObjects();
1476 //group->removeChild(geode);
1481 group = new osg::Group;
1482 FGScenery * local_scenery = globals->get_scenery();
1483 double elevation_meters = 0.0;
1484 double elevation_feet = 0.0;
1487 //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) {
1489 for (TrafficVectorIterator i = activeTraffic.begin(); i != activeTraffic.end(); i++) {
1490 // Handle start point
1491 int pos = i->getCurrentPosition();
1492 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "pos = " << pos << endl;
1494 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(pos);
1495 SGGeod start(SGGeod::fromDeg((i->getLongitude()), (i->getLatitude())));
1496 SGGeod end (SGGeod::fromDeg(segment->getEnd()->getLongitude(), segment->getEnd()->getLatitude()));
1498 double length = SGGeodesy::distanceM(start, end);
1499 //heading = SGGeodesy::headingDeg(start->getGeod(), end->getGeod());
1501 double az2, heading; //, distanceM;
1502 SGGeodesy::inverse(start, end, heading, az2, length);
1503 double coveredDistance = length * 0.5;
1505 SGGeodesy::direct(start, heading, coveredDistance, center, az2);
1506 //cerr << "Active Aircraft : Centerpoint = (" << center.getLatitudeDeg() << ", " << center.getLongitudeDeg() << "). Heading = " << heading << endl;
1507 ///////////////////////////////////////////////////////////////////////////////
1508 // Make a helper function out of this
1509 osg::Matrix obj_pos;
1510 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1511 obj_trans->setDataVariance(osg::Object::STATIC);
1512 // Experimental: Calculate slope here, based on length, and the individual elevations
1513 double elevationStart;
1514 if (isUserAircraft((i)->getAircraft())) {
1515 elevationStart = fgGetDouble("/position/ground-elev-m");
1517 elevationStart = ((i)->getAircraft()->_getAltitude());
1519 double elevationEnd = segment->getEnd()->getElevation();
1520 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1521 SGGeod center2 = end;
1522 center2.setElevationM(SG_MAX_ELEVATION_M);
1523 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1524 elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1525 //elevation_meters += 0.5;
1528 elevationEnd = parent->getElevation();
1530 segment->getEnd()->setElevation(elevationEnd);
1533 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1534 double elevDiff = elevationEnd - elevationStart;
1536 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1538 //cerr << "1. Using mean elevation : " << elevationMean << " and " << slope << endl;
1540 WorldCoordinate( obj_pos, center.getLatitudeDeg(), center.getLongitudeDeg(), elevationMean + 0.5, -(heading), slope );
1543 obj_trans->setMatrix( obj_pos );
1544 //osg::Vec3 center(0, 0, 0)
1546 float width = length /2.0;
1547 osg::Vec3 corner(-width, 0, 0.25f);
1548 osg::Vec3 widthVec(2*width + 1, 0, 0);
1549 osg::Vec3 heightVec(0, 1, 0);
1550 osg::Geometry* geometry;
1551 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1552 simgear::EffectGeode* geode = new simgear::EffectGeode;
1553 geode->setName("test");
1554 geode->addDrawable(geometry);
1555 //osg::Node *custom_obj;
1556 SGMaterial *mat = matlib->find("UnidirectionalTaper");
1558 geode->setEffect(mat->get_effect());
1559 obj_trans->addChild(geode);
1560 // wire as much of the scene graph together as we can
1561 //->addChild( obj_trans );
1562 group->addChild( obj_trans );
1563 /////////////////////////////////////////////////////////////////////
1565 //cerr << "BIG FAT WARNING: current position is here : " << pos << endl;
1567 for(intVecIterator j = (i)->getIntentions().begin(); j != (i)->getIntentions().end(); j++) {
1568 osg::Matrix obj_pos;
1571 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "intention = " << k << endl;
1572 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1573 obj_trans->setDataVariance(osg::Object::STATIC);
1574 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(k);
1576 double elevationStart = segment->getStart()->getElevation();
1577 double elevationEnd = segment->getEnd ()->getElevation();
1578 if ((elevationStart == 0) || (elevationStart == parent->getElevation())) {
1579 SGGeod center2 = segment->getStart()->getGeod();
1580 center2.setElevationM(SG_MAX_ELEVATION_M);
1581 if (local_scenery->get_elevation_m( center2, elevationStart, NULL )) {
1582 elevation_feet = elevationStart * SG_METER_TO_FEET + 0.5;
1583 //elevation_meters += 0.5;
1586 elevationStart = parent->getElevation();
1588 segment->getStart()->setElevation(elevationStart);
1590 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1591 SGGeod center2 = segment->getEnd()->getGeod();
1592 center2.setElevationM(SG_MAX_ELEVATION_M);
1593 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1594 elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1595 //elevation_meters += 0.5;
1598 elevationEnd = parent->getElevation();
1600 segment->getEnd()->setElevation(elevationEnd);
1603 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1604 double elevDiff = elevationEnd - elevationStart;
1605 double length = segment->getLength();
1606 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1608 //cerr << "2. Using mean elevation : " << elevationMean << " and " << slope << endl;
1611 WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), elevationMean + 0.5, -(segment->getHeading()), slope );
1613 //WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), parent->getElevation()+8+dx, -(segment->getHeading()) );
1615 obj_trans->setMatrix( obj_pos );
1616 //osg::Vec3 center(0, 0, 0)
1618 float width = segment->getLength() /2.0;
1619 osg::Vec3 corner(-width, 0, 0.25f);
1620 osg::Vec3 widthVec(2*width + 1, 0, 0);
1621 osg::Vec3 heightVec(0, 1, 0);
1622 osg::Geometry* geometry;
1623 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1624 simgear::EffectGeode* geode = new simgear::EffectGeode;
1625 geode->setName("test");
1626 geode->addDrawable(geometry);
1627 //osg::Node *custom_obj;
1628 SGMaterial *mat = matlib->find("UnidirectionalTaper");
1630 geode->setEffect(mat->get_effect());
1631 obj_trans->addChild(geode);
1632 // wire as much of the scene graph together as we can
1633 //->addChild( obj_trans );
1634 group->addChild( obj_trans );
1636 //cerr << "BIG FAT WARNING: k is here : " << pos << endl;
1641 globals->get_scenery()->get_scene_graph()->addChild(group);
1645 string FGStartupController::getName() {
1646 return string(parent->getId() + "-startup");
1650 /***************************************************************************
1651 * class FGApproachController
1653 **************************************************************************/
1654 FGApproachController::FGApproachController(FGAirportDynamics *par):
1661 void FGApproachController::announcePosition(int id,
1662 FGAIFlightPlan * intendedRoute,
1663 int currentPosition,
1664 double lat, double lon,
1665 double heading, double speed,
1666 double alt, double radius,
1667 int leg, FGAIAircraft * ref)
1670 TrafficVectorIterator i = activeTraffic.begin();
1671 // Search whether the current id alread has an entry
1672 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1673 if (activeTraffic.size()) {
1674 //while ((i->getId() != id) && i != activeTraffic.end()) {
1675 while (i != activeTraffic.end()) {
1676 if (i->getId() == id) {
1682 // Add a new TrafficRecord if no one exsists for this aircraft.
1683 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1684 FGTrafficRecord rec;
1687 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1688 rec.setRunway(intendedRoute->getRunway());
1690 //rec.setCallSign(callsign);
1691 rec.setAircraft(ref);
1692 activeTraffic.push_back(rec);
1694 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1698 void FGApproachController::updateAircraftInformation(int id, double lat, double lon,
1699 double heading, double speed, double alt,
1702 TrafficVectorIterator i = activeTraffic.begin();
1703 // Search search if the current id has an entry
1704 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1705 TrafficVectorIterator current, closest;
1706 if (activeTraffic.size()) {
1707 //while ((i->getId() != id) && i != activeTraffic.end()) {
1708 while (i != activeTraffic.end()) {
1709 if (i->getId() == id) {
1715 // // update position of the current aircraft
1716 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1717 SG_LOG(SG_GENERAL, SG_ALERT,
1718 "AI error: updating aircraft without traffic record");
1720 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1722 //cerr << "ApproachController: checking for speed" << endl;
1724 current->getAircraft()->
1725 checkForArrivalTime(string("final001"));
1726 if (time_diff > 15) {
1727 current->setSpeedAdjustment(current->getAircraft()->
1728 getPerformance()->vDescent() *
1730 } else if (time_diff > 5) {
1731 current->setSpeedAdjustment(current->getAircraft()->
1732 getPerformance()->vDescent() *
1734 } else if (time_diff < -15) {
1735 current->setSpeedAdjustment(current->getAircraft()->
1736 getPerformance()->vDescent() *
1738 } else if (time_diff < -5) {
1739 current->setSpeedAdjustment(current->getAircraft()->
1740 getPerformance()->vDescent() *
1743 current->clearSpeedAdjustment();
1745 //current->setSpeedAdjustment(current->getAircraft()->getPerformance()->vDescent() + time_diff);
1747 setDt(getDt() + dt);
1750 void FGApproachController::signOff(int id)
1752 TrafficVectorIterator i = activeTraffic.begin();
1753 // Search search if the current id alread has an entry
1754 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1755 if (activeTraffic.size()) {
1756 //while ((i->getId() != id) && i != activeTraffic.end()) {
1757 while (i != activeTraffic.end()) {
1758 if (i->getId() == id) {
1764 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1765 SG_LOG(SG_GENERAL, SG_ALERT,
1766 "AI error: Aircraft without traffic record is signing off from approach");
1768 i = activeTraffic.erase(i);
1775 bool FGApproachController::hasInstruction(int id)
1777 TrafficVectorIterator i = activeTraffic.begin();
1778 // Search search if the current id has an entry
1779 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1780 if (activeTraffic.size()) {
1781 //while ((i->getId() != id) && i != activeTraffic.end()) {
1782 while (i != activeTraffic.end()) {
1783 if (i->getId() == id) {
1789 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1790 SG_LOG(SG_GENERAL, SG_ALERT,
1791 "AI error: checking ATC instruction for aircraft without traffic record");
1793 return i->hasInstruction();
1799 FGATCInstruction FGApproachController::getInstruction(int id)
1801 TrafficVectorIterator i = activeTraffic.begin();
1802 // Search search if the current id has an entry
1803 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1804 if (activeTraffic.size()) {
1805 //while ((i->getId() != id) && i != activeTraffic.end()) {
1806 while (i != activeTraffic.end()) {
1807 if (i->getId() == id) {
1813 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1814 SG_LOG(SG_GENERAL, SG_ALERT,
1815 "AI error: requesting ATC instruction for aircraft without traffic record");
1817 return i->getInstruction();
1819 return FGATCInstruction();
1823 ActiveRunway *FGApproachController::getRunway(string name)
1825 ActiveRunwayVecIterator rwy = activeRunways.begin();
1826 if (activeRunways.size()) {
1827 while (rwy != activeRunways.end()) {
1828 if (rwy->getRunwayName() == name) {
1834 if (rwy == activeRunways.end()) {
1835 ActiveRunway aRwy(name, 0);
1836 activeRunways.push_back(aRwy);
1837 rwy = activeRunways.end() - 1;
1842 void FGApproachController::render(bool visible) {
1843 //cerr << "FGApproachController::render function not yet implemented" << endl;
1848 string FGApproachController::getName() {
1849 return string(parent->getId() + "-approach");