Rise
The Vieneo Province
Loading...
Searching...
No Matches
GpwsPredictiveWindshear.h
Go to the documentation of this file.
1#pragma once
2#include "GpwsWindshear.h"
3#include <algorithm>
4#include <array>
5
6// Rise surface-grid look-ahead advisory. This is horizontal shear only, not a
7// Doppler microburst sensor or an aircraft-performance/escape calculation.
9{
14 constexpr double HorizonSeconds = 30, MaxRangeKm = 8, StepKm = .25;
15 constexpr double SourceTimeoutSeconds = 360; // server's five-minute weather epoch + margin
16 // Retain the horizontal sensitivity of Rise's reactive detector. The loss
17 // floor rejects small differences in the byte-quantized surface vectors.
18 // These are Rise tuning choices, not manufacturer-certified F-factor limits.
19 constexpr double EnterRateMs2 = 1750. / (750. * 1.94384449);
20 constexpr double ExitRateMs2 = EnterRateMs2 * .75;
21 constexpr double EnterLossMs = 5, ExitLossMs = 4, ConfirmSeconds = .5;
22 struct Sample { Vector3 windToMs{}; double surfaceKm = 0; bool valid = false; };
23 struct Input
24 {
26 double radiusKm = 0, radioKm = 0, maxFrameSeconds = 0;
27 bool enabled = false, dataAvailable = false;
28 int vehicle = -1;
29 uint64_t generation = 0;
30 uint32_t revision = 0;
31 };
32 struct State
33 {
34 bool enabled = false, available = false, complete = false, caution = false;
35 double rangeKm = 0, timeSeconds = 0, headwindLossMs = 0, lossRateMs2 = 0;
36 };
38 {
40 bool trackedSource = false, trackedMotion = false, candidate = false, active = false;
41 double sourceAge = 0, confirm = 0;
42 void ClearEncounter() { candidate = active = false; confirm = 0; trackedMotion = false; }
43 public:
44 template<class Sampler> void Update(const Input& in, double elapsed, Sampler sample, State& out)
45 {
46 out = {};
47 if (!std::isfinite(elapsed) || elapsed <= 0) { ClearEncounter(); return; }
48 // Age a source even while alerting is disarmed, so a power or height
49 // transition cannot rejuvenate an old grid. No cross-revision differencing.
50 const bool restarted = in.dataAvailable && (!trackedSource || in.vehicle != previous.vehicle ||
52 const bool changed = in.dataAvailable && (restarted || in.revision != previous.revision);
53 if (changed) { sourceAge = 0; trackedSource = true; }
54 else if (trackedSource) sourceAge += elapsed;
55 if (restarted) ClearEncounter();
56 if (in.dataAvailable)
57 {
59 }
60 Vector3 up = in.position;
61 const double radius = std::sqrt(double(Dot(up,up)));
62 if (!in.enabled || !GpwsWindshear::HeightAllowed(float(in.radioKm)) || !Unit(up) ||
63 !Finite(in.groundVelocityKms) || !std::isfinite(in.radiusKm) || in.radiusKm <= 0 ||
64 radius < in.radiusKm * .9)
65 { ClearEncounter(); return; }
66 const double vertical = Dot(in.groundVelocityKms,up);
67 Vector3 forward = in.groundVelocityKms-up*float(vertical);
68 const double speed = std::sqrt(double(Dot(forward,forward)));
69 // Hover has no projected horizontal encounter. Reactive Mode 7
70 // remains responsible for a gust/downdraft at the current position.
71 if (speed < .001 || !Unit(forward)) { ClearEncounter(); return; }
72 out.enabled = true;
73 const double frame = in.maxFrameSeconds > 0 ? in.maxFrameSeconds : elapsed;
74 if (!in.dataAvailable || sourceAge > SourceTimeoutSeconds || !std::isfinite(in.maxFrameSeconds) ||
75 in.maxFrameSeconds < 0 || frame > .5)
76 { ClearEncounter(); return; }
77 const bool relocated = trackedMotion &&
78 std::sqrt(double(Dot(in.position-previous.position,in.position-previous.position))) >
79 1 + std::sqrt(double(Dot(previous.groundVelocityKms,previous.groundVelocityKms))) * elapsed;
80 if (relocated) ClearEncounter();
82 const double requestedRange = speed * HorizonSeconds;
83 const double range = (std::min)(MaxRangeKm,requestedRange);
84 const int steps = (std::max)(1,int(std::ceil(range/StepKm)));
85 const double step = range/steps;
86 std::array<Sample,33> points{};
87 bool found = false;
88 out.complete = requestedRange <= MaxRangeKm;
89 for (int i = 0; i <= steps; ++i)
90 {
91 const double distance = step*i, time = distance/speed;
92 const double angle = distance/radius;
93 const Vector3 radial = up*float(std::cos(angle))+forward*float(std::sin(angle));
94 const Vector3 position = radial*float(radius+vertical*time);
95 points[i] = sample(position);
96 const auto& here = points[i];
97 if (!here.valid || !Finite(here.windToMs) || !std::isfinite(here.surfaceKm))
98 { out.complete = false; break; } // never compare across missing cells
99 const double agl = radius-in.radiusKm+vertical*time-here.surfaceKm;
100 if (agl < .003048 || agl > .4572)
101 { out.complete = false; break; } // no surface-wind prediction in space or through terrain
102 out.available = true;
103 if (i == 0) continue;
104 // Compare over at most a kilometre, within this single snapshot.
105 // Ground speed converts a spatial gradient into an encounter rate.
106 const int back = (std::max)(0,i-(std::max)(1,int(std::floor(1/step))));
107 const double loss = Dot(here.windToMs-points[back].windToMs,forward);
108 const double rate = loss/((i-back)*step/speed);
109 if (!found && loss >= (active ? ExitLossMs : EnterLossMs) &&
110 rate > (active ? ExitRateMs2 : EnterRateMs2))
111 {
112 found = true; out.rangeKm = distance; out.timeSeconds = time;
113 out.headwindLossMs = loss; out.lossRateMs2 = rate;
114 }
115 }
116 if (!found) { candidate = active = false; confirm = 0; return; }
117 if (!candidate) { candidate = true; confirm = 0; }
118 else confirm += elapsed;
120 out.caution = active;
121 }
122 };
123}
void Update(const Input &in, double elapsed, Sampler sample, State &out)
bool Finite(const Vector3 &v)
bool Unit(Vector3 &v)
float Dot(const Vector3 &a, const Vector3 &b)
bool Finite(const Vector3 &v)
bool HeightAllowed(float radioKm)
bool Unit(Vector3 &v)
float Dot(const Vector3 &a, const Vector3 &b)