158 if (!std::isfinite(fElapsed) || fElapsed <= 0.0f)
return;
175 if (it->delay <= 0.0f && it->name ==
"TcasSystemTest")
178 else if (testAvailable && !gpwsAlert)
197 bool testPending =
false;
208 testAvailable, gpwsAlert))
212 testPending |= it->bValue;
244 if (dt > 5 || !Finite(own.position) || !std::isfinite(own.mslKm) ||
254 std::vector<Threat> threats;
256 float trafficTime = (std::numeric_limits<float>::max)();
261 target.altitudeDiffAbs = std::fabs(target.mslKm - own.mslKm);
262 if (!target.active || !target.visible || !target.isOnScanner || !target.transponder ||
263 target.docked || target.groundvehicle || target.reference ==
REF_ONGROUND ||
266 !std::isfinite(target.mslKm))
268 track.valid =
false; target.closurerate = target.vsi = 0;
271 const auto relative = target.position - own.position;
273 const bool acquired = track.valid && track.vehicleId == target.vehicleId;
274 const auto velocity = (relative - track.relative) / dt;
275 const float verticalRate = (target.mslKm - track.altitude) / dt;
276 track = {
true, target.vehicleId, relative, target.mslKm };
277 target.tcasMsl = target.mslKm;
278 target.closurerate = acquired && range > .0001f ? -
RiseMath::Vec3Dot(&relative, &velocity) / range : 0;
279 target.vsi = acquired ? verticalRate : 0;
280 if (!acquired || !Finite(velocity) || !std::isfinite(verticalRate) || range > 74.08f || target.altitudeDiffAbs > 3.01752f)
continue;
286 const float closing = hrange > .0001f ? -dot / hrange : 0;
287 const float cpa = speed2 > 1e-8f ? (std::max)(0.0f, -dot / speed2) : limits.raTime;
288 const auto missVector = horizontal + hVelocity * cpa;
290 const float altitude = target.mslKm - own.mslKm;
292 const float verticalTau = altitude * relativeRate < 0 ? -altitude / relativeRate : 1e6f;
293 auto inRange = [&](
float dmod,
float tau) {
294 if (closing < -.0001f)
return false;
295 return miss <= dmod && (hrange <= dmod || (closing > .0001f &&
296 (hrange * hrange - dmod * dmod) / (hrange * closing) <= tau));
298 const bool ta = inRange(limits.taRange, limits.taTime) &&
299 (std::fabs(altitude) <= limits.taHeight || verticalTau <= limits.taTime);
302 const bool ra = inRange(limits.raRange, limits.raTime) && std::fabs(verticalRate) <= 10000 * Fpm &&
303 (continuing || std::fabs(altitude) <= limits.raHeight || verticalTau <= limits.raTime);
304 if (ta && cpa < trafficTime) { traffic = i; trafficTime = cpa; }
305 if (ra) threats.push_back({ i, altitude, verticalRate, (std::max)(1.0f, (std::min)(cpa, 60.0f)) });
313 const auto nearest = std::min_element(threats.begin(), threats.end(), [](
const Threat& a,
const Threat& b) { return a.time < b.time; });
314 int primary = nearest->ordinal;
315 for (
const auto& t : threats)
323 auto evaluate = [&](
int s,
float rate,
Bus::TcasEnum mode,
float delay,
bool subsequent) {
324 return Evaluate(threats, primary, s, rate, mode, ownRate, limits.separation, delay, subsequent);
326 auto initial = [&](
int s) {
327 const float rate = s > 0 ? 1500 * Fpm : (descendAllowed ? -1500 * Fpm : 0.0f);
329 Candidate best = evaluate(s, rate, mode, 5,
false);
334 const auto zero = evaluate(s, 0, zeroMode, 5,
false);
335 if ((s < 0 && !descendAllowed) || Better(zero, best, ownRate)) best = zero;
336 if (s * ownRate >= 1500 * Fpm && s * ownRate <= 4400 * Fpm && (s > 0 || descendAllowed))
339 if (maintain.margin >= 0 && (!maintain.crossing || best.crossing || best.margin < 0)) best = maintain;
343 Candidate chosen = initial(1);
344 const auto down = initial(-1);
345 if (Better(down, chosen, ownRate)) chosen = down;
346 else if (!continuing && ((down.margin >= 0 && chosen.margin >= 0) ||
347 std::fabs(down.margin - chosen.margin) < 5 * Foot) &&
348 down.crossing == chosen.crossing &&
349 std::fabs(std::fabs(down.rate - ownRate) - std::fabs(chosen.rate - ownRate)) < 50 * Fpm &&
357 bool achieved =
true;
358 for (
const auto& t : threats)
359 achieved &= (t.ordinal == primary ? -
sense * t.altitude : std::fabs(t.altitude)) >= limits.separation;
362 const auto zero = evaluate(
sense, 0, zeroMode, 2.5f,
true);
363 const bool descentInhibited = chosen.rate < 0 && (!descendAllowed ||
364 (chosen.rate < -2000 * Fpm && !increaseDescentAllowed));
365 if ((chosen.rate != 0 && !
weakened && achieved && zero.margin >= 0) || descentInhibited)
370 else if (chosen.margin < -25 * Foot &&
advisoryAge >= 2.5f)
374 (
sense > 0 || increaseDescentAllowed))
376 if (
sense < 0 && !descendAllowed) stronger = zero;
377 if (stronger.margin > chosen.margin + 25 * Foot) chosen = stronger;
379 if (chosen.margin < 0 && (
sense < 0 || descendAllowed))
381 auto reverse = evaluate(-
sense, -
sense * 1500 * Fpm,
383 if (reverse.margin > chosen.margin + 100 * Foot) { chosen = reverse;
weakened =
false; }
388 if (chosen.rate != 0)
weakened =
false;
389 sense = chosen.sense;
394 SetAdvisory(chosen.mode, primary, chosen.rate, chosen.crossing);