Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/GpwsWindshearTests.h

Source at documentation-generation time. See Test catalog for fixture associations.

1// Production sensor projection, detector and GPWS/VMU integration.
2static void WindshearRegressions()
3{
4 using namespace GpwsWindshear;
5 using G = Bus::Gpws;
6 const auto sampleAt = [](double time, float tailwind, float vertical = 0.f) {
7 Sample s; s.seconds=time; s.sequence=uint64_t(time*1000000)+1;
8 s.valid=true; s.vehicle=0; s.up=Vector3(0,1,0); s.forward=Vector3(0,0,1);
9 s.windToMs=Vector3(0,vertical,tailwind); return s;
10 };
11 RiseMath::Matrix identity; RiseMath::MatrixIdentity(&identity);
12 {
13 Sample s;
14 Publish(s,.1f,Vector3(.02f,0,0),identity,Vector3(0,0,.1f),Vector3(0,3000,0),0,false,true);
15 Detector d;
16 Check(s.valid && d.Update(s,.1f,true)==Alert::None && d.UpdraftMs()==0,
17 "horizontal wind is not an updraft");
18 Publish(s,.1f,Vector3(0,.02f,0),identity,Vector3(0,0,.1f),Vector3(3000,0,0),0,false,true);
19 d.Update(s,.1f,true);
20 Check(std::fabs(d.UpdraftMs())<1.e-5f,"global Y is horizontal at another planetary location");
21 Publish(s,.1f,Vector3(-.01f,0,0),identity,Vector3(0,0,.1f),Vector3(3000,0,0),0,false,true);
22 Check(d.Update(s,.1f,true)==Alert::Warning && std::fabs(d.UpdraftMs()+10)<1.e-4f,
23 "negative local radial wind produces a downdraft warning away from world Y");
24 // Rotate an ambient+gust body vector into the same world wind.
25 RiseMath::Matrix rotation,inverse; RiseMath::MatrixRotationZ(&rotation,.7f);
26 RiseMath::MatrixInverse(&inverse,nullptr,&rotation);
27 Vector3 windWorld(0,-.01f,-.02f), body;
28 RiseMath::Vec3TransformNormal(&body,&windWorld,&inverse);
29 Publish(s,.1f,body,rotation,Vector3(0,0,.1f),Vector3(0,3000,0),0,false,true);
30 Check(std::fabs(s.windToMs.y+10)<1.e-4f && std::fabs(s.windToMs.z+20)<1.e-4f,
31 "body attitude does not rotate measured gusts away from their world direction");
32 Publish(s,.1f,Vector3(0,-.01f,0),identity,Vector3{},Vector3(0,3000,0),0,false,true);
33 Check(s.valid && d.Update(s,.1f,true)==Alert::Warning,"zero-speed hover retains downdraft protection");
34 Publish(s,.1f,Vector3{},identity,Vector3{},Vector3{},0,false,true);
35 Check(!s.valid,"unknown planetary position invalidates wind sample");
36 }
37 // Rise's physical straight-ahead vector is body -Z (propulsion.cpp).
38 // With no ground track, a front-to-back wind reversal must still warn.
39 for (float yaw : {0.f,.6f,-1.4f})
40 {
41 Sample s; Detector d; RiseMath::Matrix attitude;
42 RiseMath::MatrixRotationY(&attitude,yaw);
43 Publish(s,1.f,Vector3(0,0,.01f),attitude,Vector3{},Vector3(0,3000,0),0,false,true);
44 d.Update(s,1.f,true);
45 Publish(s,1.f,Vector3(0,0,-.01f),attitude,Vector3{},Vector3(0,3000,0),0,false,true);
46 Check(d.Update(s,1.f,true)==Alert::Warning,
47 "hover headwind to tailwind reversal follows the physical minus-Z nose at every heading");
48 Check(std::fabs(d.HeadwindRateKnotsPerSecond()+20*1.94384449f)<.001f,
49 "hover headwind loss has the same signed rate as moving-flight headwind loss");
50 Publish(s,1.f,Vector3(0,0,.01f),attitude,Vector3{},Vector3(0,3000,0),0,false,true);
51 Check(d.Update(s,1.f,true)==Alert::Caution,
52 "hover tailwind to headwind recovery cautions instead of reversing severity");
53 }
54 for (float sign : {-1.f,1.f})
55 {
56 Detector d;
57 d.Update(sampleAt(0,-sign*10),.1f,true);
58 const auto s=sampleAt(1,sign*10);
59 Check(d.Update(s,.001f,true)==(sign>0?Alert::Warning:Alert::Caution),
60 "equal-magnitude headwind/tailwind reversal has the correct severity");
61 Check(std::fabs(d.HeadwindRateKnotsPerSecond()+sign*20*1.94384449f)<.001f,
62 "derivative uses sample timestamps rather than the frame interval");
63 Check(d.Update(s,.1f,true)==(sign>0?Alert::Warning:Alert::Caution),
64 "unchanged bus sample holds its result without injecting a false zero derivative");
65 Check(d.Update(s,2.1f,true)==Alert::None && d.Update(s,.1f,true)==Alert::None,
66 "stale input expires and the same stale sample cannot rearm");
67 Check(d.Update(sampleAt(4,sign*10),.1f,true)==Alert::None,
68 "recovered sensor primes a new history without a false shear spike");
69 }
70 {
71 Detector d;
72 for (int i=0;i<=120;++i) {
73 Sample s=sampleAt(i/30.,10); s.forward=Vector3(std::sin(float(i)/20),0,std::cos(float(i)/20));
74 Check(d.Update(s,1.f/30,true)==Alert::None,"steady wind during a turn is not windshear");
75 }
76 auto s=sampleAt(5,10); s.windToMs.x=40;
77 Check(d.Update(s,.1f,true)==Alert::None,"a pure crosswind change is not longitudinal shear");
78 s=sampleAt(6,10,-10);
79 Check(d.Update(s,.1f,true)==Alert::Warning,"downdraft alone can warn");
80 s=sampleAt(7,10,10);
81 Check(d.Update(s,.1f,true)==Alert::Caution,"updraft alone can caution");
82 s.windToMs.y=std::numeric_limits<float>::quiet_NaN(); ++s.sequence;
83 Check(d.Update(s,.1f,true)==Alert::None,"invalid wind clears the detector");
84 }
85 // Identical time-defined encounters at independent sensor and renderer rates.
86 // Each check uses the same physical samples; render cadence must not scale shear.
87 for (int sensorHz : {1,5,20,60}) for (int fps : {15,30,60,144,240})
88 {
89 Detector d; Sample sample; int sensor=0;
90 float worstRateError=0;
91 bool warning=false,caution=false,clear=false,statesMatch=true;
92 for (int frame=0;frame<=fps*9;++frame)
93 {
94 const double t=double(frame)/fps;
95 while (double(sensor)/sensorHz<=t+1.e-9) {
96 const double st=double(sensor++)/sensorHz;
97 const float tailwind=st<2?-10.f:st<4?float(-10+10*(st-2)):st<6?10.f:st<8?float(10-10*(st-6)):-10.f;
98 sample=sampleAt(st,tailwind);
99 }
100 const auto alert=d.Update(sample,1.f/fps,true);
101 if (t>=3.25 && t<=3.75) {
102 warning|=alert==Alert::Warning;
103 statesMatch &= alert==Alert::Warning;
104 worstRateError=(std::max)(worstRateError,std::fabs(d.HeadwindRateKnotsPerSecond()+19.4384449f));
105 }
106 if (t>=5.25 && t<=5.75) { clear|=alert==Alert::None; statesMatch &= alert==Alert::None; }
107 if (t>=7.25 && t<=7.75) { caution|=alert==Alert::Caution; statesMatch &= alert==Alert::Caution; }
108 }
109 Check(warning&&caution&&clear&&statesMatch&&worstRateError<.001f,
110 "15/30/60/144/240 FPS and 1/5/20/60 Hz sensing produce the same shear rate and encounter states");
111 std::cout << "Windshear replay: " << fps << " FPS / " << sensorHz << " Hz sensor; max rate error " << worstRateError << " kt/s\n";
112 }
113 {
114 Detector d; Sample s;
115 const float steps[] = {1.f/144,1.f/30,.1f,1.f/60,1.f/15};
116 bool stable=true; float error=0;
117 for (int i=0;i<300;++i) {
118 const float dt=steps[i%5]; const float wind=float(s.seconds+dt)*.005f;
119 Publish(s,dt,Vector3(0,0,wind),identity,Vector3(0,0,.1f),Vector3(0,3000,0),0,false,true);
120 const auto a=d.Update(s,dt,true);
121 if(s.seconds>1.2) {
122 stable &= a==Alert::Warning;
123 error=(std::max)(error,std::fabs(d.HeadwindRateKnotsPerSecond()+5*1.94384449f));
124 }
125 }
126 Check(stable&&error<.001f,"production timestamp publisher is stable with alternating 7-100 ms frame times");
127 std::cout << "Windshear jitter replay: max rate error " << error << " kt/s\n";
128 Publish(s,0,Vector3{},identity,Vector3{},Vector3(0,3000,0),0,false,true);
129 Check(!s.valid && d.Update(s,.1f,true)==Alert::None,"flight freeze invalidates the sample and clears shear");
130 }
131 for (int transition=0;transition<5;++transition)
132 {
133 Detector d; d.Update(sampleAt(0,-10),.1f,true);
134 Check(d.Update(sampleAt(1,10),.1f,true)==Alert::Warning,"reset fixture enters shear");
135 auto s=sampleAt(2,-10);
136 if (transition==0) ++s.vehicle;
137 if (transition==1) s.simulator=true;
138 if (transition==2) s.seconds=.2;
139 if (transition==3) s.seconds=5;
140 if (transition==4) d.Update(sampleAt(1.5,10),.1f,false);
141 Check(d.Update(s,.1f,true)==Alert::None,"vehicle, simulator, clock, sample-gap and disarm transitions discard history");
142 }
143 Check(!HeightAllowed(.003047f)&&HeightAllowed(.003048f)&&HeightAllowed(.4572f)&&!HeightAllowed(.45721f),
144 "reactive windshear height band is 10 through 1500 feet AGL");
145 // Actual alert dispatch, normal VMU queue, power/inhibit/spaceflight lifecycle.
146 for (bool autopilot : {false,true}) for (auto reference : {REF_APNMODE,REF_ACLMODE})
147 {
148 Fixture f; f.bus->RadioAltitudeKm=.3f; playerships[0].geardown=true;
149 playerships[0].reference=reference; f.bus->AFCS.AutopilotEngaged=autopilot;
150 ourcockpit.dntranslimit=1; f.bus->TrueAirspeedKms=f.bus->IndicatedAirspeedKms=0;
151 for(int i=0;i<55;++i) {
152 f.bus->WindshearWind=sampleAt(i*.1,0,-10);
153 f.Step(.1f);
154 }
155 Check(f.bus->GPWS.Alert==G::Mode7_Windshear_Warning && f.bus->GPWS.warningLamp,
156 "manual/Autoflyte and APN/ACL hover use the same operational windshear warning");
157 Check(std::find(played.begin(),played.end(),SOUND_GPWSWINDSHEARWARNING)!=played.end() && stopped.empty(),
158 "windshear goes through existing bus/VMU without restarting active speech");
159 f.bus->RadioAltitudeKm=900; f.Step(.1f);
160 Check(f.bus->GPWS.Alert!=G::Mode7_Windshear_Warning,"orbit has no reactive low-altitude windshear alert");
161 f.bus->RadioAltitudeKm=.3f; f.bus->WindshearWind=sampleAt(6,0); f.Step(.1f);
162 Check(f.bus->GPWS.Alert!=G::Mode7_Windshear_Warning,"reentry re-arms without replaying the old event");
163 f.bus->WindshearWind=sampleAt(7,0,-10); f.Step(.1f);
164 Check(f.bus->GPWS.Alert==G::Mode7_Windshear_Warning,"new near-ground encounter after reentry is detected");
165 f.bus->Mach=12; f.bus->TrueAirspeedKms=4; f.bus->IndicatedAirspeedKms=0;
166 f.bus->WindshearWind=sampleAt(7.1,0,-10); f.Step(.1f);
167 Check(f.bus->GPWS.Alert==G::Mode7_Windshear_Warning,"high-speed near-ground flight does not inherit the bank spaceflight exclusion");
168 playerships[0].reference=REF_ONGROUND; f.Step(.1f);
169 Check(f.bus->GPWS.Alert!=G::Mode7_Windshear_Warning && f.bus->commandStream.empty(),
170 "touchdown clears windshear alert and queued speech");
171 playerships[0].reference=reference; f.bus->GPWS.inhibited=true; f.Step(.1f);
172 Check(f.bus->GPWS.Alert!=G::Mode7_Windshear_Warning,"explicit hazard inhibit disarms windshear");
173 f.bus->GPWS.inhibited=false; ourcockpit.power=0; f.Step(.1f);
174 Check(f.bus->GPWS.Alert==G::NoAlert,"power loss clears windshear indication");
176 }
177 {
178 Fixture f; f.bus->RadioAltitudeKm=.3f; playerships[0].geardown=true;
179 f.bus->vmuHold=10;
180 f.bus->WindshearWind=sampleAt(1,0,-10); f.gpws.FrameMove(4.1f);
181 Check(std::any_of(f.bus->commandStream.begin(),f.bus->commandStream.end(),[](const Command& c){
182 return c.name=="GPWSMode7WindshearWarning";}),"busy VMU leaves windshear queued normally");
183 f.bus->WindshearWind=sampleAt(1.1,0); f.gpws.FrameMove(.1f);
184 f.bus->vmuHold=0; f.vmu.FrameMove(.1f);
185 Check(played.empty()&&f.bus->commandStream.empty(),"recovered windshear cannot play stale queued warning");
186 }
187}
@ REF_ACLMODE
Definition ServerTypes.h:44
@ REF_APNMODE
Definition ServerTypes.h:42
@ REF_ONGROUND
Definition ServerTypes.h:43
@ SOUND_GPWSWINDSHEARWARNING
Definition Sound.h:66
Alert Update(const Sample &sample, float elapsed, bool enabled)
float HeadwindRateKnotsPerSecond() const
s_network_objects playerships[MAX_SCAN]
Definition globals.cpp:112
Scockpit ourcockpit
Definition globals.cpp:114
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)
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:107
Matrix * MatrixRotationY(Matrix *out, float value)
Definition ClientMath.h:127
Matrix * MatrixInverse(Matrix *out, float *determinant, const Matrix *a)
Definition ClientMath.h:118
Matrix * MatrixIdentity(Matrix *out)
Definition ClientMath.h:115
Matrix * MatrixRotationZ(Matrix *out, float value)
Definition ClientMath.h:128
float dntranslimit
Definition globals.h:601
float power
Definition globals.h:599