1 // tcas.cxx -- Traffic Alert and Collision Avoidance System (TCAS) Emulation
3 // Written by Thorsten Brehm, started December 2010.
5 // Copyright (C) 2010 Thorsten Brehm - brehmt (at) gmail com
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 ///////////////////////////////////////////////////////////////////////////////
25 * [TCASII] Introduction to TCAS II Version 7, Federal Aviation Administration, November 2000
26 * http://www.arinc.com/downloads/tcas/tcas.pdf
28 * [EUROACAS] Eurocontrol Airborne Collision Avoidance System (ACAS),
29 * http://www.eurocontrol.int/msa/public/standard_page/ACAS_Startpage.html
33 * ALIM: Altitude Limit
35 * CPA: Closest point of approach as computed from a threat's range and range rate.
37 * DMOD: Distance MODification
39 * Intruder: A target that has satisfied the traffic detection criteria.
41 * Proximity target: Any target that is less than 6 nmi in range and within +/-1200ft
42 * vertically, but that does not meet the intruder or threat criteria.
44 * RA: Resolution advisory. An indication given by TCAS II to a flight crew that a
45 * vertical maneuver should, or in some cases should not, be performed to attain or
46 * maintain safe separation from a threat.
48 * SL: Sensitivity Level. A value used in defining the size of the protected volume
49 * around the own aircraft.
51 * TA: Traffic Advisory. An indication given by TCAS to the pilot when an aircraft has
52 * entered, or is projected to enter, the protected volume around the own aircraft.
54 * Tau: Approximation of the time, in seconds, to CPA or to the aircraft being at the
57 * TCAS: Traffic alert and Collision Avoidance System
62 * serviceable enable/disable TCAS processing
64 * voice/file-prefix path (and optional prefix) for sound sample files
65 * (only evaluated at start-up)
67 * inputs/mode TCAS mode selection: 0=off,1=standby,2=TA only,3=auto(TA/RA)
68 * inputs/self-test trigger self-test sequence
70 * outputs/traffic-alert intruder detected (true=TA-threat is active, includes RA-threats)
71 * outputs/advisory-alert resolution advisory is issued (true=advisory is valid)
72 * outputs/vertical-speed vertical speed required by advisory (+/-2000/1500/500/0)
74 * speaker/max-dist Max. distance where speaker is heard
75 * speaker/reference-dist Distance to pilot
76 * speaker/volume Volume at reference distance
78 * debug/threat-trigger trigger debugging test (in debug mode only)
79 * debug/threat-RA debugging RA value (in debug mode only)
80 * debug/threat-level debugging threat level (in debug mode only)
84 # pragma warning( disable: 4355 )
99 #include <simgear/constants.h>
100 #include <simgear/sg_inlines.h>
101 #include <simgear/debug/logstream.hxx>
102 #include <simgear/math/sg_geodesy.hxx>
103 #include <simgear/math/sg_random.h>
104 #include <simgear/misc/sg_path.hxx>
105 #include <simgear/sound/soundmgr_openal.hxx>
106 #include <simgear/structure/exception.hxx>
110 #if defined( HAVE_VERSION_H ) && HAVE_VERSION_H
111 # include <Include/version.h>
113 # include <Include/no_version.h>
116 #include <Main/fg_props.hxx>
117 #include <Main/globals.hxx>
118 #include <AIModel/submodel.hxx>
119 #include "instrument_mgr.hxx"
122 ///////////////////////////////////////////////////////////////////////////////
123 // debug switches /////////////////////////////////////////////////////////////
124 ///////////////////////////////////////////////////////////////////////////////
125 //#define FEATURE_TCAS_DEBUG_ANNUNCIATOR
126 //#define FEATURE_TCAS_DEBUG_COORDINATOR
127 //#define FEATURE_TCAS_DEBUG_THREAT_DETECTOR
128 //#define FEATURE_TCAS_DEBUG_TRACKER
129 //#define FEATURE_TCAS_DEBUG_ADV_GENERATOR
130 //#define FEATURE_TCAS_DEBUG_PROPERTIES
132 ///////////////////////////////////////////////////////////////////////////////
133 // constants //////////////////////////////////////////////////////////////////
134 ///////////////////////////////////////////////////////////////////////////////
136 /** Sensitivity Level Definition and Alarm Thresholds (TCASII, Version 7)
137 * Max Own | |TA-level |RA-level
138 * Altitude(ft) | SL |Tau(s),DMOD(nm),ALIM(ft)|Tau(s),DMOD(nm),ALIM(ft) */
139 const TCAS::SensitivityLevel
140 TCAS::ThreatDetector::sensitivityLevels[] = {
141 { 1000, 2, {20, 0.30, 850}, {0, 0, 0 }},
142 { 2350, 3, {25, 0.33, 850}, {15, 0.20, 300}},
143 { 5000, 4, {30, 0.48, 850}, {20, 0.35, 300}},
144 {10000, 5, {40, 0.75, 850}, {25, 0.55, 350}},
145 {20000, 6, {45, 1.00, 850}, {30, 0.80, 400}},
146 {42000, 7, {48, 1.30, 850}, {35, 1.10, 600}},
147 {0, 8, {48, 1.30, 1200}, {35, 1.10, 700}}
150 ///////////////////////////////////////////////////////////////////////////////
151 // helpers ////////////////////////////////////////////////////////////////////
152 ///////////////////////////////////////////////////////////////////////////////
154 #define ADD_VOICE(Var, Sample, SayTwice) \
155 { make_voice(&Var); \
156 append(Var, Sample); \
157 if (SayTwice) append(Var, Sample); }
159 #define AVAILABLE_RA(Options, Advisory) (Advisory == (Advisory & Options))
161 #ifdef FEATURE_TCAS_DEBUG_THREAT_DETECTOR
162 /** calculate relative angle in between two headings */
164 relAngle(float Heading1, float Heading2)
166 Heading1 -= Heading2;
168 while (Heading1 >= 360.0)
171 while (Heading1 < 0.0)
178 /** calculate range and bearing of lat2/lon2 relative to lat1/lon1 */
180 calcRangeBearing(double lat1, double lon1, double lat2, double lon2,
181 double &rangeNm, double &bearing)
183 // calculate the bearing and range of the second pos from the first
184 double az2, distanceM;
185 geo_inverse_wgs_84(lat1, lon1, lat2, lon2, &bearing, &az2, &distanceM);
186 rangeNm = distanceM * SG_METER_TO_NM;
189 ///////////////////////////////////////////////////////////////////////////////
190 // VoicePlayer ////////////////////////////////////////////////////////////////
191 ///////////////////////////////////////////////////////////////////////////////
194 TCAS::VoicePlayer::init(void)
196 FGVoicePlayer::init();
198 ADD_VOICE(Voices.pTrafficTraffic, "traffic", true);
199 ADD_VOICE(Voices.pClear, "clear", false);
200 ADD_VOICE(Voices.pClimb, "climb", true);
201 ADD_VOICE(Voices.pClimbNow, "climb_now", true);
202 ADD_VOICE(Voices.pClimbCrossing, "climb_crossing", true);
203 ADD_VOICE(Voices.pClimbIncrease, "increase_climb", false);
204 ADD_VOICE(Voices.pDescend, "descend", true);
205 ADD_VOICE(Voices.pDescendNow, "descend_now", true);
206 ADD_VOICE(Voices.pDescendCrossing,"descend_crossing", true);
207 ADD_VOICE(Voices.pDescendIncrease,"increase_descent", false);
208 ADD_VOICE(Voices.pAdjustVSpeed, "adjust_vertical_speed", false);
209 ADD_VOICE(Voices.pMaintVSpeed, "maintain_vertical_speed", false);
210 ADD_VOICE(Voices.pMonitorVSpeed, "monitor_vertical_speed", false);
211 ADD_VOICE(Voices.pLevelOff, "level_off", false);
212 ADD_VOICE(Voices.pTestOk, "test_ok", false);
213 ADD_VOICE(Voices.pTestFail, "test_fail", false);
215 speaker.update_configuration();
218 /////////////////////////////////////////////////////////////////////////////
219 // TCAS::Annunciator ////////////////////////////////////////////////////////
220 /////////////////////////////////////////////////////////////////////////////
222 TCAS::Annunciator::Annunciator(TCAS* _tcas) :
230 void TCAS::Annunciator::clear(void)
232 previous.threatLevel = ThreatNone;
233 previous.RA = AdvisoryClear;
234 previous.RAOption = OptionNone;
239 TCAS::Annunciator::bind(SGPropertyNode* node)
241 voicePlayer.bind(node, "Sounds/tcas/");
245 TCAS::Annunciator::init(void)
247 //TODO link to GPWS module/audio-on signal must be configurable
248 nodeGpwsAlertOn = fgGetNode("/instrumentation/mk-viii/outputs/discretes/audio-on", true);
253 TCAS::Annunciator::update(void)
255 voicePlayer.update();
257 /* [TCASII]: "The priority scheme gives ground proximity warning systems (GPWS)
258 * a higher annunciation priority than a TCAS alert. TCAS aural annunciation will
259 * be inhibited during the time that a GPWS alert is active." */
260 if (nodeGpwsAlertOn->getBoolValue())
263 voicePlayer.resume();
266 /** Trigger voice sample for current alert. */
268 TCAS::Annunciator::trigger(const ResolutionAdvisory& current, bool revertedRA)
271 int RAOption = current.RAOption;
273 if (RA == AdvisoryClear)
275 if (previous.RA != AdvisoryClear)
277 voicePlayer.play(voicePlayer.Voices.pClear, VoicePlayer::PLAY_NOW);
284 if ((previous.RA == AdvisoryClear)||
285 (tcas->tracker.newTraffic()))
287 voicePlayer.play(voicePlayer.Voices.pTrafficTraffic, VoicePlayer::PLAY_NOW);
291 VoicePlayer::Voice* pVoice = NULL;
296 pVoice = voicePlayer.Voices.pClimbNow;
298 if (AVAILABLE_RA(RAOption, OptionIncreaseClimb))
299 pVoice = voicePlayer.Voices.pClimbIncrease;
301 if (AVAILABLE_RA(RAOption, OptionCrossingClimb))
302 pVoice = voicePlayer.Voices.pClimbCrossing;
304 pVoice = voicePlayer.Voices.pClimb;
307 case AdvisoryDescend:
309 pVoice = voicePlayer.Voices.pDescendNow;
311 if (AVAILABLE_RA(RAOption, OptionIncreaseDescend))
312 pVoice = voicePlayer.Voices.pDescendIncrease;
314 if (AVAILABLE_RA(RAOption, OptionCrossingDescent))
315 pVoice = voicePlayer.Voices.pDescendCrossing;
317 pVoice = voicePlayer.Voices.pDescend;
320 case AdvisoryAdjustVSpeed:
321 pVoice = voicePlayer.Voices.pAdjustVSpeed;
324 case AdvisoryMaintVSpeed:
325 pVoice = voicePlayer.Voices.pMaintVSpeed;
328 case AdvisoryMonitorVSpeed:
329 pVoice = voicePlayer.Voices.pMonitorVSpeed;
332 case AdvisoryLevelOff:
333 pVoice = voicePlayer.Voices.pLevelOff;
336 case AdvisoryIntrusion:
340 RA = AdvisoryIntrusion;
346 if ((pLastVoice == pVoice)&&
347 (!tcas->tracker.newTraffic()))
349 // don't repeat annunciation
354 voicePlayer.play(pVoice);
356 #ifdef FEATURE_TCAS_DEBUG_ANNUNCIATOR
357 cout << "Annunciating TCAS RA " << RA << endl;
362 TCAS::Annunciator::test(bool testOk)
365 voicePlayer.play(voicePlayer.Voices.pTestOk);
367 voicePlayer.play(voicePlayer.Voices.pTestFail);
370 /////////////////////////////////////////////////////////////////////////////
371 // TCAS::AdvisoryCoordinator ////////////////////////////////////////////////
372 /////////////////////////////////////////////////////////////////////////////
374 TCAS::AdvisoryCoordinator::AdvisoryCoordinator(TCAS* _tcas) :
382 TCAS::AdvisoryCoordinator::init(void)
389 TCAS::AdvisoryCoordinator::bind(SGPropertyNode* node)
391 nodeTAWarning = node->getNode("outputs/traffic-alert", true);
392 nodeTAWarning->setBoolValue(false);
396 TCAS::AdvisoryCoordinator::clear(void)
398 current.threatLevel = ThreatNone;
399 current.RA = AdvisoryClear;
400 current.RAOption = OptionNone;
403 /** Add all suitable resolution advisories for a single threat. */
405 TCAS::AdvisoryCoordinator::add(const ResolutionAdvisory& newAdvisory)
407 if ((newAdvisory.RA == AdvisoryClear)||
408 (newAdvisory.threatLevel < current.threatLevel))
411 if (current.threatLevel == newAdvisory.threatLevel)
413 // combine with other advisories so far
414 current.RA &= newAdvisory.RA;
415 // remember any advisory modifier
416 current.RAOption |= newAdvisory.RAOption;
420 current = newAdvisory;
424 /** Pick and trigger suitable resolution advisory. */
426 TCAS::AdvisoryCoordinator::update(int mode)
428 bool revertedRA = false; // has advisory changed?
429 double currentTime = globals->get_sim_time_sec();
431 if (current.RA == AdvisoryClear)
433 // filter: wait 5 seconds after last TA/RA before announcing TA clearance
434 if ((previous.RA != AdvisoryClear)&&
435 (currentTime - lastTATime < 5.0))
442 #ifdef FEATURE_TCAS_DEBUG_COORDINATOR
443 cout << "TCAS::Annunciator::update: previous: " << previous.RA << ", new: " << current.RA << endl;
446 lastTATime = currentTime;
447 if ((previous.RA == AdvisoryClear)||
448 (previous.RA == AdvisoryIntrusion)||
449 ((current.RA & previous.RA) != previous.RA))
451 // no RA yet, or we can't keep previous RA: pick one - in order of priority
453 if (AVAILABLE_RA(current.RA, AdvisoryMonitorVSpeed))
455 // prio 1: monitor vertical speed only
456 current.RA = AdvisoryMonitorVSpeed;
459 if (AVAILABLE_RA(current.RA, AdvisoryMaintVSpeed))
461 // prio 2: maintain vertical speed
462 current.RA = AdvisoryMaintVSpeed;
465 if (AVAILABLE_RA(current.RA, AdvisoryAdjustVSpeed))
467 // prio 3: adjust vertical speed (TCAS II 7.0 only)
468 current.RA = AdvisoryAdjustVSpeed;
471 if (AVAILABLE_RA(current.RA, AdvisoryLevelOff))
473 // prio 3: adjust vertical speed (TCAS II 7.1 only, [EUROACAS]: CP115)
474 current.RA = AdvisoryLevelOff;
477 if (AVAILABLE_RA(current.RA, AdvisoryClimb))
480 current.RA = AdvisoryClimb;
483 if (AVAILABLE_RA(current.RA, AdvisoryDescend))
486 current.RA = AdvisoryDescend;
490 // no RA, issue a TA only
491 current.RA = AdvisoryIntrusion;
494 // check if earlier advisory was reverted
495 revertedRA = ((previous.RA != current.RA)&&
497 (previous.RA != AdvisoryIntrusion));
502 current.RA = previous.RA;
506 /* [TCASII]: "Aural annunciations are inhibited below 500+/-100 feet AGL." */
507 if ((tcas->threatDetector.getAlt() > 500)&&
508 (mode >= SwitchTaOnly))
509 tcas->annunciator.trigger(current, revertedRA);
511 if (current.RA == AdvisoryClear)
513 /* explicitly clear traffic alert (since aural annunciation disabled) */
514 tcas->annunciator.clear();
519 /* [TCASII] "[..] also performs the function of setting flags that control the displays.
520 * The traffic display, the RA display, [..] use these flags to alert the pilot to
521 * the presence of TAs and RAs." */
522 nodeTAWarning->setBoolValue(current.RA != AdvisoryClear);
525 ///////////////////////////////////////////////////////////////////////////////
526 // TCAS::ThreatDetector ///////////////////////////////////////////////////////
527 ///////////////////////////////////////////////////////////////////////////////
529 TCAS::ThreatDetector::ThreatDetector(TCAS* _tcas) :
531 pAlarmThresholds(&sensitivityLevels[0])
537 TCAS::ThreatDetector::init(void)
539 nodeLat = fgGetNode("/position/latitude-deg", true);
540 nodeLon = fgGetNode("/position/longitude-deg", true);
541 nodeAlt = fgGetNode("/position/altitude-ft", true);
542 nodeHeading = fgGetNode("/orientation/heading-deg", true);
543 nodeVelocity = fgGetNode("/velocities/airspeed-kt", true);
544 nodeVerticalFps = fgGetNode("/velocities/vertical-speed-fps", true);
546 tcas->advisoryGenerator.init(&self,¤tThreat);
549 /** Update local position and threat sensitivity levels. */
551 TCAS::ThreatDetector::update(void)
553 // update local position
554 self.lat = nodeLat->getDoubleValue();
555 self.lon = nodeLon->getDoubleValue();
556 self.altFt = nodeAlt->getDoubleValue();
557 self.heading = nodeHeading->getDoubleValue();
558 self.velocityKt = nodeVelocity->getDoubleValue();
559 self.verticalFps = nodeVerticalFps->getDoubleValue();
563 // determine current altitude's "Sensitivity Level Definition and Alarm Thresholds"
565 for (sl=0;((self.altFt > sensitivityLevels[sl].maxAltitude)&&
566 (sensitivityLevels[sl].maxAltitude));sl++);
567 pAlarmThresholds = &sensitivityLevels[sl];
568 tcas->advisoryGenerator.setAlarmThresholds(pAlarmThresholds);
571 /** Check if plane's transponder is enabled. */
573 TCAS::ThreatDetector::checkTransponder(const SGPropertyNode* pModel, float velocityKt)
575 const string name = pModel->getName();
576 if (name != "multiplayer" && name != "aircraft")
578 // assume non-MP/non-AI planes (e.g. ships) have no transponder
584 /* assume all pilots have their transponder switched off while taxiing/parking
589 if ((name == "multiplayer")&&
590 (pModel->getBoolValue("controls/invisible")))
592 // ignored MP plane: pretend transponder is switched off
599 /** Check if plane is a threat. */
601 TCAS::ThreatDetector::checkThreat(int mode, const SGPropertyNode* pModel)
605 float velocityKt = pModel->getDoubleValue("velocities/true-airspeed-kt");
607 if (!checkTransponder(pModel, velocityKt))
608 return ThreatInvisible;
610 int threatLevel = ThreatNone;
611 float altFt = pModel->getDoubleValue("position/altitude-ft");
612 currentThreat.relativeAltitudeFt = altFt - self.altFt;
614 // save computation time: don't care when relative altitude is excessive
615 if (fabs(currentThreat.relativeAltitudeFt) > 10000)
618 // position data of current intruder
619 double lat = pModel->getDoubleValue("position/latitude-deg");
620 double lon = pModel->getDoubleValue("position/longitude-deg");
621 float heading = pModel->getDoubleValue("orientation/true-heading-deg");
623 double distanceNm, bearing;
624 calcRangeBearing(self.lat, self.lon, lat, lon, distanceNm, bearing);
626 // save computation time: don't care for excessive distances (also captures NaNs...)
627 if ((distanceNm > 10)||(distanceNm < 0))
630 currentThreat.verticalFps = pModel->getDoubleValue("velocities/vertical-speed-fps");
632 /* Detect proximity targets
633 * [TCASII]: "Any target that is less than 6 nmi in range and within +/-1200ft
634 * vertically, but that does not meet the intruder or threat criteria." */
635 if ((distanceNm < 6)&&
636 (fabs(currentThreat.relativeAltitudeFt) < 1200))
638 // at least a proximity target
639 threatLevel = ThreatProximity;
642 /* do not detect any threats when in standby or on ground and taxiing */
643 if ((mode <= SwitchStandby)||
644 ((self.altFt < 360)&&(self.velocityKt < 40)))
649 if (tcas->tracker.active())
651 currentThreat.callsign = pModel->getStringValue("callsign");
652 currentThreat.isTracked = tcas->tracker.isTracked(currentThreat.callsign);
655 currentThreat.isTracked = false;
657 // first stage: vertical movement
658 checkVerticalThreat();
660 // stop processing when no vertical threat
661 if ((!currentThreat.verticalTA)&&
662 (!currentThreat.isTracked))
665 // second stage: horizontal movement
666 horizontalThreat(bearing, distanceNm, heading, velocityKt);
668 if (!currentThreat.isTracked)
670 // no horizontal threat?
671 if (!currentThreat.horizontalTA)
674 if ((currentThreat.horizontalTau < 0)||
675 (currentThreat.verticalTau < 0))
677 // do not trigger new alerts when Tau is negative, but keep existing alerts
678 int previousThreatLevel = pModel->getIntValue("tcas/threat-level", 0);
679 if (previousThreatLevel == 0)
684 #ifdef FEATURE_TCAS_DEBUG_THREAT_DETECTOR
685 cout << "#" << checkCount << ": " << pModel->getStringValue("callsign") << endl;
689 /* [TCASII]: "For either a TA or an RA to be issued, both the range and
690 * vertical criteria, in terms of tau or the fixed thresholds, must be
691 * satisfied only one of the criteria is satisfied, TCAS will not issue
693 if (currentThreat.horizontalTA && currentThreat.verticalTA)
694 threatLevel = ThreatTA;
695 if (currentThreat.horizontalRA && currentThreat.verticalRA)
696 threatLevel = ThreatRA;
698 if (!tcas->tracker.active())
699 currentThreat.callsign = pModel->getStringValue("callsign");
701 tcas->tracker.add(currentThreat.callsign, threatLevel);
703 // check existing threat level
704 if (currentThreat.isTracked)
706 int oldLevel = tcas->tracker.getThreatLevel(currentThreat.callsign);
707 if (oldLevel > threatLevel)
708 threatLevel = oldLevel;
711 // find all resolution options for this conflict
712 threatLevel = tcas->advisoryGenerator.resolution(mode, threatLevel, distanceNm, altFt, heading, velocityKt);
715 #ifdef FEATURE_TCAS_DEBUG_THREAT_DETECTOR
716 printf(" threat: distance: %4.1f, bearing: %4.1f, alt: %5.1f, velocity: %4.1f, heading: %4.1f, vspeed: %4.1f, "
717 "own alt: %5.1f, own heading: %4.1f, own velocity: %4.1f, vertical tau: %3.2f"
718 //", closing speed: %f"
720 distanceNm, relAngle(bearing, self.heading), altFt, velocityKt, heading, currentThreat.verticalFps,
721 self.altFt, self.heading, self.velocityKt
722 //, currentThreat.closingSpeedKt
723 ,currentThreat.verticalTau
730 /** Check if plane is a vertical threat. */
732 TCAS::ThreatDetector::checkVerticalThreat(void)
734 // calculate relative vertical speed and altitude
735 float dV = self.verticalFps - currentThreat.verticalFps;
736 float dA = currentThreat.relativeAltitudeFt;
738 currentThreat.verticalTA = false;
739 currentThreat.verticalRA = false;
740 currentThreat.verticalTau = 0;
742 /* [TCASII]: "The vertical tau is equal to the altitude separation (feet)
743 * divided by the combined vertical speed of the two aircraft (feet/minute)
749 /* [TCASII]: "When the combined vertical speed of the TCAS and the intruder aircraft
750 * is low, TCAS will use a fixed-altitude threshold to determine whether a TA or
751 * an RA should be issued." */
752 if ((fabs(dV) < 3.0)||
753 ((tau < 0) && (tau > -5)))
755 /* vertical closing speed is low (below 180fpm/3fps), check
756 * fixed altitude range. */
757 float abs_dA = fabs(dA);
758 if (abs_dA < pAlarmThresholds->RA.ALIM)
760 // continuous intrusion at RA-level
761 currentThreat.verticalTA = true;
762 currentThreat.verticalRA = true;
765 if (abs_dA < pAlarmThresholds->TA.ALIM)
767 // continuous intrusion: with TA-level, but no RA-threat
768 currentThreat.verticalTA = true;
770 // else: no RA/TA threat
774 if ((tau < pAlarmThresholds->TA.Tau)&&
777 currentThreat.verticalTA = true;
778 currentThreat.verticalRA = (tau < pAlarmThresholds->RA.Tau);
781 currentThreat.verticalTau = tau;
783 #ifdef FEATURE_TCAS_DEBUG_THREAT_DETECTOR
784 if (currentThreat.verticalTA)
785 printf(" vertical dV=%f (%f-%f), dA=%f\n", dV, self.verticalFps, currentThreat.verticalFps, dA);
789 /** Check if plane is a horizontal threat. */
791 TCAS::ThreatDetector::horizontalThreat(float bearing, float distanceNm, float heading, float velocityKt)
794 float vxKt = sin(heading*SGD_DEGREES_TO_RADIANS)*velocityKt - sin(self.heading*SGD_DEGREES_TO_RADIANS)*self.velocityKt;
795 float vyKt = cos(heading*SGD_DEGREES_TO_RADIANS)*velocityKt - cos(self.heading*SGD_DEGREES_TO_RADIANS)*self.velocityKt;
797 // calculate horizontal closing speed
798 float closingSpeedKt2 = vxKt*vxKt+vyKt*vyKt;
799 float closingSpeedKt = sqrt(closingSpeedKt2);
801 /* [TCASII]: "The range tau is equal to the slant range (nmi) divided by the closing speed
802 * (knots) multiplied by 3600."
803 * => calculate allowed slant range (nmi) based on known maximum tau */
804 float TA_rangeNm = (pAlarmThresholds->TA.Tau*closingSpeedKt)/3600;
805 float RA_rangeNm = (pAlarmThresholds->RA.Tau*closingSpeedKt)/3600;
807 if (closingSpeedKt < 100)
809 /* [TCASII]: "In events where the rate of closure is very low, [..]
810 * an intruder aircraft can come very close in range without crossing the
811 * range tau boundaries [..]. To provide protection in these types of
812 * advisories, the range tau boundaries are modified [..] to use
813 * a fixed-range threshold to issue TAs and RAs in these slow closure
815 TA_rangeNm += (100.0-closingSpeedKt)*(pAlarmThresholds->TA.DMOD/100.0);
816 RA_rangeNm += (100.0-closingSpeedKt)*(pAlarmThresholds->RA.DMOD/100.0);
818 if (TA_rangeNm < pAlarmThresholds->TA.DMOD)
819 TA_rangeNm = pAlarmThresholds->TA.DMOD;
820 if (RA_rangeNm < pAlarmThresholds->RA.DMOD)
821 RA_rangeNm = pAlarmThresholds->RA.DMOD;
823 currentThreat.horizontalTA = (distanceNm < TA_rangeNm);
824 currentThreat.horizontalRA = (distanceNm < RA_rangeNm);
825 currentThreat.horizontalTau = -1;
827 if ((currentThreat.horizontalRA)&&
828 (currentThreat.verticalRA))
830 /* an RA will be issued. Prepare extra data for the
831 * traffic resolution stage, i.e. calculate
832 * exact time tau to horizontal CPA.
835 /* relative position of intruder is
838 * horizontal distance to intruder is r(t)
839 * r(t) = sqrt( Sx(t)^2 + Sy(t)^2 )
840 * => horizontal CPA at time t=tau, where r(t) has minimum
841 * r2(t) := r^2(t) = Sx(t)^2 + Sy(t)^2
842 * since r(t)>0 for all t => minimum of r(t) is also minimum of r2(t)
843 * => (d/dt) r2(t) = r2'(t) is 0 for t=tau
844 * r2(t) = ((Sx(t)^2 + Sy(t))^2) = c + b*t + a*t^2
845 * => r2'(t) = b + a*2*t
847 * r2'(tau) = 0 = b + 2*a*tau
850 float sx = sin(bearing*SGD_DEGREES_TO_RADIANS)*distanceNm;
851 float sy = cos(bearing*SGD_DEGREES_TO_RADIANS)*distanceNm;
852 float vx = vxKt * (SG_KT_TO_MPS*SG_METER_TO_NM);
853 float vy = vyKt * (SG_KT_TO_MPS*SG_METER_TO_NM);
854 float a = vx*vx + vy*vy;
855 float b = 2*(sx*vx + sy*vy);
859 #ifdef FEATURE_TCAS_DEBUG_THREAT_DETECTOR
860 printf(" Time to horizontal CPA: %4.2f\n",tau);
862 if (tau > pAlarmThresholds->RA.Tau)
863 tau = pAlarmThresholds->RA.Tau;
865 // remember time to horizontal CPA
866 currentThreat.horizontalTau = tau;
870 /** Test threat detection logic. */
872 TCAS::ThreatDetector::unitTest(void)
874 pAlarmThresholds = &sensitivityLevels[1];
877 self.verticalFps = 0;
879 cout << "identical altitude and vspeed " << endl;
880 checkVerticalThreat(self.altFt, self.verticalFps);
881 cout << "1000ft alt offset, dV=100 " << endl;
882 checkVerticalThreat(self.altFt+1000, 100);
883 cout << "-1000ft alt offset, dV=100 " << endl;
884 checkVerticalThreat(self.altFt-1000, 100);
885 cout << "3000ft alt offset, dV=10 " << endl;
886 checkVerticalThreat(self.altFt+3000, 10);
887 cout << "500ft alt offset, dV=100 " << endl;
888 checkVerticalThreat(self.altFt+500, 100);
889 cout << "500ft alt offset, dV=-100 " << endl;
890 checkVerticalThreat(self.altFt+500, -100);
895 cout << "10nm behind, overtaking with 1Nm/s" << endl;
896 horizontalThreat(-180, 10, 0, 1/(SG_KT_TO_MPS*SG_METER_TO_NM));
898 cout << "10nm ahead, departing with 1Nm/s" << endl;
899 horizontalThreat(0, 20, 0, 1/(SG_KT_TO_MPS*SG_METER_TO_NM));
902 self.velocityKt = 1/(SG_KT_TO_MPS*SG_METER_TO_NM);
903 cout << "10nm behind, overtaking with 1Nm/s at 90 degrees" << endl;
904 horizontalThreat(-90, 20, 90, 2/(SG_KT_TO_MPS*SG_METER_TO_NM));
907 self.velocityKt = 1/(SG_KT_TO_MPS*SG_METER_TO_NM);
908 cout << "10nm behind, overtaking with 1Nm/s at 20 degrees" << endl;
909 horizontalThreat(200, 20, 20, 2/(SG_KT_TO_MPS*SG_METER_TO_NM));
913 ///////////////////////////////////////////////////////////////////////////////
914 // TCAS::AdvisoryGenerator ////////////////////////////////////////////////////
915 ///////////////////////////////////////////////////////////////////////////////
917 TCAS::AdvisoryGenerator::AdvisoryGenerator(TCAS* _tcas) :
920 pCurrentThreat(NULL),
921 pAlarmThresholds(NULL)
926 TCAS::AdvisoryGenerator::init(const LocalInfo* _pSelf, ThreatInfo* _pCurrentThreat)
928 pCurrentThreat = _pCurrentThreat;
933 TCAS::AdvisoryGenerator::setAlarmThresholds(const SensitivityLevel* _pAlarmThresholds)
935 pAlarmThresholds = _pAlarmThresholds;
938 /** Calculate projected vertical separation at horizontal CPA. */
940 TCAS::AdvisoryGenerator::verticalSeparation(float newVerticalFps)
942 // calculate relative vertical speed and altitude
943 float dV = pCurrentThreat->verticalFps - newVerticalFps;
944 float tau = pCurrentThreat->horizontalTau;
945 // don't use negative tau to project future separation...
948 return pCurrentThreat->relativeAltitudeFt + tau * dV;
951 /** Determine RA sense. */
953 TCAS::AdvisoryGenerator::determineRAsense(int& RASense, bool& isCrossing)
955 /* [TCASII]: "[..] a two step process is used to select the appropriate RA for the encounter
956 * geometry. The first step in the process is to select the RA sense, i.e., upward or downward." */
960 /* [TCASII]: "Based on the range and altitude tracks of the intruder, the CAS logic models the
961 * intruder's flight path from its present position to CPA. The CAS logic then models upward
962 * and downward sense RAs for own aircraft [..] to determine which sense provides the most
963 * vertical separation at CPA." */
964 float upSenseRelAltFt = verticalSeparation(+2000/60.0);
965 float downSenseRelAltFt = verticalSeparation(-2000/60.0);
966 if (fabs(upSenseRelAltFt) >= fabs(downSenseRelAltFt))
967 RASense = +1; // upward
969 RASense = -1; // downward
971 /* [TCASII]: "In encounters where either of the senses results in the TCAS aircraft crossing through
972 * the intruder's altitude, TCAS is designed to select the nonaltitude crossing sense if the
973 * noncrossing sense provides the desired vertical separation, known as ALIM, at CPA." */
974 /* [TCASII]: "If ALIM cannot be obtained in the nonaltitude crossing sense, an altitude
975 * crossing RA will be issued." */
977 (pCurrentThreat->relativeAltitudeFt > 200))
979 // threat is above and RA is crossing
980 if (fabs(downSenseRelAltFt) > pAlarmThresholds->TA.ALIM)
982 // non-crossing descend is sufficient
987 // keep crossing climb RA
993 (pCurrentThreat->relativeAltitudeFt < -200))
995 // threat is below and RA is crossing
996 if (fabs(upSenseRelAltFt) > pAlarmThresholds->TA.ALIM)
998 // non-crossing climb is sufficient
1003 // keep crossing descent RA
1007 // else: threat is at same altitude, keep optimal RA sense (non-crossing)
1009 pCurrentThreat->RASense = RASense;
1011 #ifdef FEATURE_TCAS_DEBUG_ADV_GENERATOR
1012 printf(" RASense: %i, crossing: %u, relAlt: %4.1f, upward separation: %4.1f, downward separation: %4.1f\n",
1014 pCurrentThreat->relativeAltitudeFt,
1015 upSenseRelAltFt,downSenseRelAltFt);
1019 /** Determine suitable resolution advisories. */
1021 TCAS::AdvisoryGenerator::resolution(int mode, int threatLevel, float rangeNm, float altFt,
1022 float heading, float velocityKt)
1024 int RAOption = OptionNone;
1025 int RA = AdvisoryIntrusion;
1027 // RAs are disabled under certain conditions
1028 if (threatLevel == ThreatRA)
1030 /* [TCASII]: "... less than 360 feet, TCAS considers the reporting aircraft
1031 * to be on the ground. If TCAS determines the intruder to be on the ground, it
1032 * inhibits the generation of advisories against this aircraft."*/
1034 threatLevel = ThreatTA;
1036 /* [EUROACAS]: "Certain RAs are inhibited at altitudes based on inputs from the radio altimeter:
1037 * [..] (c)1000ft (+/- 100ft) and below, all RAs are inhibited;" */
1038 if (pSelf->altFt < 1000)
1039 threatLevel = ThreatTA;
1041 // RAs only issued in mode "Auto" (= "TA/RA" mode)
1042 if (mode != SwitchAuto)
1043 threatLevel = ThreatTA;
1046 bool isCrossing = false;
1048 // determine suitable RAs
1049 if (threatLevel == ThreatRA)
1051 /* [TCASII]: "[..] a two step process is used to select the appropriate RA for the encounter
1052 * geometry. The first step in the process is to select the RA sense, i.e., upward or downward." */
1053 determineRAsense(RASense, isCrossing);
1055 /* second step: determine required strength */
1060 if ((pSelf->verticalFps < -1000/60.0)&&
1063 // currently descending, see if reducing current descent is sufficient
1064 float relAltFt = verticalSeparation(-500/60.0);
1065 if (relAltFt > pAlarmThresholds->TA.ALIM)
1066 RA |= AdvisoryAdjustVSpeed;
1068 RA |= AdvisoryClimb;
1070 RAOption |= OptionCrossingClimb;
1077 if ((pSelf->verticalFps > 1000/60.0)&&
1080 // currently climbing, see if reducing current climb is sufficient
1081 float relAltFt = verticalSeparation(500/60.0);
1082 if (relAltFt < -pAlarmThresholds->TA.ALIM)
1083 RA |= AdvisoryAdjustVSpeed;
1085 RA |= AdvisoryDescend;
1087 RAOption |= OptionCrossingDescent;
1091 /* [TCASII]: "When two TCAS-equipped aircraft are converging vertically with opposite rates
1092 * and are currently well separated in altitude, TCAS will first issue a vertical speed
1093 * limit (Negative) RA to reinforce the pilots' likely intention to level off at adjacent
1094 * flight levels." */
1097 /* [TCASII]: "[..] if the CAS logic determines that the response to a Positive RA has provided
1098 * ALIM feet of vertical separation before CPA, the initial RA will be weakened to either a
1099 * Do Not Descend RA (after an initial Climb RA) or a Do Not Climb RA (after an initial
1103 /* [TCASII]: "TCAS is designed to inhibit Increase Descent RAs below 1450 feet AGL; */
1105 /* [TCASII]: "Descend RAs below 1100 feet AGL;" (inhibited) */
1106 if (pSelf->altFt < 1100)
1108 RA &= ~AdvisoryDescend;
1109 //TODO Support "Do not descend" RA
1110 RA |= AdvisoryIntrusion;
1114 #ifdef FEATURE_TCAS_DEBUG_ADV_GENERATOR
1115 cout << " resolution advisory: " << RA << endl;
1118 ResolutionAdvisory newAdvisory;
1119 newAdvisory.RAOption = RAOption;
1120 newAdvisory.RA = RA;
1121 newAdvisory.threatLevel = threatLevel;
1122 tcas->advisoryCoordinator.add(newAdvisory);
1127 ///////////////////////////////////////////////////////////////////////////////
1128 // TCAS ///////////////////////////////////////////////////////////////////////
1129 ///////////////////////////////////////////////////////////////////////////////
1131 TCAS::TCAS(SGPropertyNode* pNode) :
1136 properties_handler(this),
1137 threatDetector(this),
1139 advisoryCoordinator(this),
1140 advisoryGenerator(this),
1143 for (int i = 0; i < pNode->nChildren(); ++i)
1145 SGPropertyNode* pChild = pNode->getChild(i);
1146 string cname = pChild->getName();
1147 string cval = pChild->getStringValue();
1149 if (cname == "name")
1151 else if (cname == "number")
1152 num = pChild->getIntValue();
1155 SG_LOG(SG_INSTR, SG_WARN, "Error in TCAS config logic");
1157 SG_LOG(SG_INSTR, SG_WARN, "Section = " << name);
1166 advisoryCoordinator.init();
1167 threatDetector.init();
1173 SGPropertyNode* node = fgGetNode(("/instrumentation/" + name).c_str(), num, true);
1175 nodeServiceable = node->getNode("serviceable", true);
1177 // TCAS mode selection (0=off, 1=standby, 2=TA only, 3=auto(TA/RA) )
1178 nodeModeSwitch = node->getNode("inputs/mode", true);
1180 nodeSelfTest = node->getNode("inputs/self-test", true);
1182 nodeSelfTest->setBoolValue(false);
1184 #ifdef FEATURE_TCAS_DEBUG_PROPERTIES
1185 SGPropertyNode* nodeDebug = node->getNode("debug", true);
1187 nodeDebugTrigger = nodeDebug->getNode("threat-trigger", true);
1188 nodeDebugRA = nodeDebug->getNode("threat-RA", true);
1189 nodeDebugThreat = nodeDebug->getNode("threat-level", true);
1191 nodeDebugTrigger->setBoolValue(false);
1192 nodeDebugRA->setIntValue(3);
1193 nodeDebugThreat->setIntValue(1);
1196 annunciator.bind(node);
1197 advisoryCoordinator.bind(node);
1203 properties_handler.unbind();
1206 /** Monitor traffic for safety threats. */
1208 TCAS::update(double dt)
1210 if (!nodeServiceable->getBoolValue())
1212 int mode = nodeModeSwitch->getIntValue();
1213 if (mode == SwitchOff)
1216 nextUpdateTime -= dt;
1217 if (nextUpdateTime <= 0.0 )
1219 nextUpdateTime = 1.0;
1221 // remove obsolete targets
1224 // get aircrafts current position/speed/heading
1225 threatDetector.update();
1227 // clear old threats
1228 advisoryCoordinator.clear();
1230 if (nodeSelfTest->getBoolValue())
1232 if (threatDetector.getVelocityKt() >= 40)
1234 // disable self-test when plane moves above taxiing speed
1235 nodeSelfTest->setBoolValue(false);
1241 // speed-up self test
1243 // no further TCAS processing during self-test
1248 #ifdef FEATURE_TCAS_DEBUG_PROPERTIES
1249 if (nodeDebugTrigger->getBoolValue())
1252 ResolutionAdvisory debugAdvisory;
1253 debugAdvisory.RAOption = OptionNone;
1254 debugAdvisory.RA = nodeDebugRA->getIntValue();
1255 debugAdvisory.threatLevel = nodeDebugThreat->getIntValue();
1256 advisoryCoordinator.add(debugAdvisory);
1261 SGPropertyNode* pAi = fgGetNode("/ai/models", true);
1263 // check all aircraft
1264 for (int i = pAi->nChildren() - 1; i >= -1; i--)
1266 SGPropertyNode* pModel = pAi->getChild(i);
1267 if ((pModel)&&(pModel->nChildren()))
1269 int threatLevel = threatDetector.checkThreat(mode, pModel);
1270 /* expose aircraft threat-level (to be used by other instruments,
1271 * i.e. TCAS display) */
1272 if (threatLevel==ThreatRA)
1273 pModel->setIntValue("tcas/ra-sense", -threatDetector.getRASense());
1274 pModel->setIntValue("tcas/threat-level", threatLevel);
1278 advisoryCoordinator.update(mode);
1280 annunciator.update();
1283 /** Run a single self-test iteration. */
1285 TCAS::selfTest(void)
1287 annunciator.update();
1288 if (annunciator.isPlaying())
1293 ResolutionAdvisory newAdvisory;
1294 newAdvisory.threatLevel = ThreatRA;
1295 newAdvisory.RA = AdvisoryClear;
1296 newAdvisory.RAOption = OptionNone;
1297 // TCAS audio is disabled below 500ft
1298 threatDetector.setAlt(501);
1300 // trigger various advisories
1301 switch(selfTestStep)
1304 newAdvisory.RA = AdvisoryIntrusion;
1305 newAdvisory.threatLevel = ThreatTA;
1308 newAdvisory.RA = AdvisoryClimb;
1311 newAdvisory.RA = AdvisoryClimb;
1312 newAdvisory.RAOption = OptionIncreaseClimb;
1315 newAdvisory.RA = AdvisoryClimb;
1316 newAdvisory.RAOption = OptionCrossingClimb;
1319 newAdvisory.RA = AdvisoryDescend;
1322 newAdvisory.RA = AdvisoryDescend;
1323 newAdvisory.RAOption = OptionIncreaseDescend;
1326 newAdvisory.RA = AdvisoryDescend;
1327 newAdvisory.RAOption = OptionCrossingDescent;
1330 newAdvisory.RA = AdvisoryAdjustVSpeed;
1333 newAdvisory.RA = AdvisoryMaintVSpeed;
1336 newAdvisory.RA = AdvisoryMonitorVSpeed;
1339 newAdvisory.threatLevel = ThreatNone;
1340 newAdvisory.RA = AdvisoryClear;
1343 annunciator.test(true);
1347 nodeSelfTest->setBoolValue(false);
1352 advisoryCoordinator.add(newAdvisory);
1353 advisoryCoordinator.update(SwitchAuto);
1358 ///////////////////////////////////////////////////////////////////////////////
1359 // TCAS::Tracker //////////////////////////////////////////////////////////////
1360 ///////////////////////////////////////////////////////////////////////////////
1362 TCAS::Tracker::Tracker(TCAS* _tcas) :
1372 TCAS::Tracker::update(void)
1374 currentTime = globals->get_sim_time_sec();
1379 // remove outdated targets
1380 TrackerTargets::iterator it = targets.begin();
1381 while (it != targets.end())
1383 TrackerTarget* pTarget = it->second;
1384 if (currentTime - pTarget->TAtimestamp > 10.0)
1386 TrackerTargets::iterator temp = it;
1388 #ifdef FEATURE_TCAS_DEBUG_TRACKER
1389 printf("target %s no longer a TA threat.\n",temp->first.c_str());
1391 targets.erase(temp->first);
1397 if ((pTarget->threatLevel == ThreatRA)&&
1398 (currentTime - pTarget->RAtimestamp > 7.0))
1400 pTarget->threatLevel = ThreatTA;
1401 #ifdef FEATURE_TCAS_DEBUG_TRACKER
1402 printf("target %s no longer an RA threat.\n",it->first.c_str());
1408 haveTargets = !targets.empty();
1413 TCAS::Tracker::add(const string callsign, int detectedLevel)
1415 TrackerTarget* pTarget = NULL;
1418 TrackerTargets::iterator it = targets.find(callsign);
1419 if (it != targets.end())
1421 pTarget = it->second;
1427 pTarget = new TrackerTarget();
1428 pTarget->TAtimestamp = 0;
1429 pTarget->RAtimestamp = 0;
1430 pTarget->threatLevel = 0;
1432 targets[callsign] = pTarget;
1433 #ifdef FEATURE_TCAS_DEBUG_TRACKER
1434 printf("new target: %s, level: %i\n",callsign.c_str(),detectedLevel);
1438 if (detectedLevel > pTarget->threatLevel)
1439 pTarget->threatLevel = detectedLevel;
1441 if (detectedLevel >= ThreatTA)
1442 pTarget->TAtimestamp = currentTime;
1444 if (detectedLevel >= ThreatRA)
1445 pTarget->RAtimestamp = currentTime;
1451 TCAS::Tracker::_isTracked(string callsign)
1453 return targets.find(callsign) != targets.end();
1457 TCAS::Tracker::getThreatLevel(string callsign)
1459 TrackerTargets::iterator it = targets.find(callsign);
1460 if (it != targets.end())
1461 return it->second->threatLevel;