Rise
The Vieneo Province
Loading...
Searching...
No Matches
GpwsWindshear.h
Go to the documentation of this file.
1#pragma once
3#include <cmath>
4#include <cstdint>
5#include <deque>
6
7// Reactive windshear inputs and the retained Rise approximation. This is not
8// Honeywell's proprietary performance-dependent Mode 7 or predictive radar.
9namespace GpwsWindshear
10{
12 inline bool Finite(const Vector3& v)
13 { return std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z); }
14 inline float Dot(const Vector3& a, const Vector3& b) { return a.x*b.x+a.y*b.y+a.z*b.z; }
15 inline bool Unit(Vector3& v)
16 {
17 const float squared = Dot(v,v);
18 if (!Finite(v) || !std::isfinite(squared) || squared < 1.e-12f) return false;
19 v *= 1.f/std::sqrt(squared); return true;
20 }
21 struct Sample
22 {
23 // Actual air velocity in planet/world coordinates, including body gusts.
25 double seconds = 0;
26 uint64_t sequence = 0;
27 int vehicle = -1;
28 bool simulator = false, valid = false;
29 };
30 inline void Publish(Sample& sample, float elapsed, const Vector3& bodyWindKms,
31 const RiseMath::Matrix& bodyToWorld, const Vector3& groundVelocityKms,
32 const Vector3& position, int vehicle, bool simulator, bool available)
33 {
34 ++sample.sequence;
35 sample.vehicle = vehicle; sample.simulator = simulator; sample.valid = false;
36 if (!std::isfinite(elapsed) || elapsed <= 0) return;
37 sample.seconds += elapsed;
38 sample.up = position;
39 if (!available || !Unit(sample.up) || !Finite(bodyWindKms) || !Finite(groundVelocityKms)) return;
40 RiseMath::Vec3TransformNormal(&sample.windToMs,&bodyWindKms,&bodyToWorld);
41 sample.windToMs *= 1000.f;
42 sample.forward = groundVelocityKms-sample.up*Dot(groundVelocityKms,sample.up);
43 if (!Unit(sample.forward))
44 {
45 // Hover/vertical flight has no ground track. Keep vertical protection
46 // and use the horizontal nose direction when it exists; never divide by TAS.
47 const Vector3 nose(0,0,-1); // propulsion StraightAheadVector uses body -Z
48 RiseMath::Vec3TransformNormal(&sample.forward,&nose,&bodyToWorld);
49 sample.forward -= sample.up*Dot(sample.forward,sample.up);
50 if (!Unit(sample.forward)) sample.forward = Vector3{};
51 }
52 sample.valid = Finite(sample.windToMs);
53 }
54 enum class Alert { None, Caution, Warning };
55 inline bool HeightAllowed(float radioKm)
56 { return std::isfinite(radioKm) && radioKm >= .003048f && radioKm <= .4572f; }
58 {
59 struct Point { double time; Vector3 wind; };
60 std::deque<Point> history;
62 double age = 0;
64 float headwindRate = 0, updraft = 0;
65 public:
66 // One-second differencing preserves the old sensor's intended time
67 // scale. Two seconds is an input freshness limit, not an aircraft envelope.
68 static constexpr double WindowSeconds = 1., StaleSeconds = 2.;
69 void Reset() { history.clear(); previous = {}; age = 0; alert = Alert::None; headwindRate = updraft = 0; }
70 float HeadwindRateKnotsPerSecond() const { return headwindRate; }
71 float UpdraftMs() const { return updraft; }
72 Alert Update(const Sample& sample, float elapsed, bool enabled)
73 {
74 if (!enabled || !std::isfinite(elapsed) || elapsed <= 0 || !sample.valid ||
75 !std::isfinite(sample.seconds) || !Finite(sample.windToMs) ||
76 !Finite(sample.up) || !Finite(sample.forward)) { Reset(); return Alert::None; }
77 age += elapsed;
78 if (!history.empty() && sample.sequence == previous.sequence)
79 {
80 if (age > StaleSeconds) { Reset(); previous = sample; }
81 return alert;
82 }
83 // Never re-admit a stale duplicate after clearing its history.
84 if (history.empty() && sample.sequence == previous.sequence) return Alert::None;
85 if (!history.empty() && (sample.vehicle != previous.vehicle || sample.simulator != previous.simulator ||
87 age = 0; previous = sample;
88 history.push_back({sample.seconds,sample.windToMs});
89 const double target = sample.seconds-WindowSeconds;
90 while (history.size() > 2 && history[1].time <= target) history.pop_front();
91 headwindRate = 0;
92 if (history.size() >= 2 && history.front().time <= target)
93 {
94 const auto& a = history[0]; const auto& b = history[1];
95 const float blend = float((target-a.time)/(b.time-a.time));
96 const Vector3 oldWind = a.wind+(b.wind-a.wind)*blend;
97 // Difference the WORLD wind first, then project both samples
98 // along today's track. A turn in steady wind is not windshear.
99 headwindRate = -Dot(sample.windToMs-oldWind,sample.forward)/float(WindowSeconds)*1.94384449f;
100 }
101 updraft = Dot(sample.windToMs,sample.up);
102 const float score = updraft*196.850394f+750.f*headwindRate;
103 alert = score < -1750.f ? Alert::Warning : score > 1750.f ? Alert::Caution : Alert::None;
104 return alert;
105 }
106 };
107}
@ Caution
Definition Message.h:8
@ Warning
Definition Message.h:7
@ None
Definition ServerTypes.h:97
Alert Update(const Sample &sample, float elapsed, bool enabled)
static constexpr double WindowSeconds
std::deque< Point > history
static constexpr double StaleSeconds
float HeadwindRateKnotsPerSecond() const
bool Finite(const Vector3 &v)
void Publish(Sample &sample, float elapsed, const Vector3 &bodyWindKms, const RiseMath::Matrix &bodyToWorld, const Vector3 &groundVelocityKms, const Vector3 &position, int vehicle, bool simulator, bool available)
bool HeightAllowed(float radioKm)
bool Unit(Vector3 &v)
float Dot(const Vector3 &a, const Vector3 &b)
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:107