Rise
The Vieneo Province
Loading...
Searching...
No Matches
GpwsControls.cpp
Go to the documentation of this file.
1#include "Module.h"
2#include "../MathUtilities.h"
3
4namespace
5{
6 void Remember(std::vector<std::wstring>& history, std::wstring item)
7 {
8 if (history.size() == 32) history.erase(history.begin());
9 history.push_back(std::move(item));
10 }
11 std::vector<std::wstring> FaultText(unsigned faults)
12 {
13 std::vector<std::wstring> lines;
14 if (faults&1) lines.push_back(L"RADIO ALTITUDE INOP");
15 if (faults&2) lines.push_back(L"AIR DATA INOP");
16 if (faults&4) lines.push_back(L"POSITION INOP");
17 if (faults&8) lines.push_back(L"TERR DATA LIMITED");
18 if (faults&16) lines.push_back(L"OBST DATA LIMITED");
19 if (!faults) lines.push_back(L"NO CURRENT FAULTS");
20 return lines;
21 }
22}
23
24void Gpws::ProcessControls(bool powered, bool airborne)
25{
26 auto& g = bus->GPWS;
27 if (testVehicle != playerships[0].vehicleId)
28 {
29 if (testVehicle >= 0) g.inhibited = g.flapOverride = g.steepApproach = g.mode5cancelled = false;
30 g.testLevel = 0; g.testPattern = false; g.testLines.clear();
32 }
33 for (auto i = bus->commandStream.begin(); i != bus->commandStream.end();)
34 {
35 if (i->delay > 0) { ++i; continue; }
36 const auto& c = *i;
37 if (c.name == "GPWSInhibit") g.inhibited = c.bValue;
38 else if (c.name == "GPWSInhibitToggle") g.inhibited = !g.inhibited;
39 else if (c.name == "GPWSPeaks") g.peaks = c.bValue;
40 else if (c.name == "GPWSAutoRange") g.autoRange = c.bValue;
41 else if (c.name == "GPWSObstacles") g.obstaclesEnabled = c.bValue;
42 else if (c.name == "GPWSBankCallouts") g.bankCallouts = c.bValue;
43 else if (c.name == "GPWSAuralDeclutter") g.auralDeclutter = c.bValue;
44 else if (c.name == "GPWSSlowAircraft") g.slowAircraft = c.bValue;
45 else if (c.name == "GPWSHighIntegrity") g.highIntegrity = c.bValue;
46 else if (c.name == "GPWSFlapOverride") g.flapOverride = c.bValue;
47 else if (c.name == "GPWSSteepApproach") g.steepApproach = c.bValue;
48 else if (c.name == "GPWSGlideslopeCancel" || (c.name == "GPWSTestGsCancel" && airborne))
49 {
50 if (powered && airborne) g.mode5cancelled = true;
51 }
52 else if (c.name == "GPWSDecisionHeight")
53 {
54 if (std::isfinite(c.fValue) && c.fValue >= 0 && c.fValue <= .762f) g.decisionHeightKm = c.fValue;
55 }
56 else if (c.name == "GPWSCalloutMask") g.calloutMask = unsigned(c.ivalues[0])&0x1ff;
57 else if (c.name == "GPWS500Mode") g.fiveHundredMode = (std::clamp)(c.ivalues[0],0,3);
58 else if (c.name == "GPWSTest" || c.name == "GPWSTestToggle" || c.name == "GPWSTestGsCancel")
59 {
60 const bool start = c.name != "GPWSTest" ? g.testLevel == 0 : c.bValue;
61 g.testLevel = start && powered && playerships[0].reference == REF_ONGROUND && !g.inhibited
62 ? (std::clamp)(c.ivalues[0] ? c.ivalues[0] : 1,1,6) : 0;
63 g.testStep = 0; g.testPattern = false; testSeconds = 0;
64 g.testLines.clear(); g.testMessageSeconds = start && !g.testLevel ? 5.f : 0.f;
65 if (start && !g.testLevel) g.testLines = {L"TEST REQUIRES GROUND",L"POWER / INHIBITS NORMAL"};
66 }
67 else { ++i; continue; }
68 i = bus->commandStream.erase(i);
69 }
70 // This installation's momentary GS cancel lasts until touchdown.
71 // Clear it even when power loss or a zero timestep skips normal evaluation.
72 if (playerships[0].reference == REF_ONGROUND) g.mode5cancelled = false;
73 if (!powered || playerships[0].reference != REF_ONGROUND || g.inhibited)
74 {
75 g.testLevel = 0; g.testPattern = false;
76 if (!powered) g.testLines.clear();
77 }
78 if (!g.testLevel)
79 bus->commandStream.erase(std::remove_if(bus->commandStream.begin(),bus->commandStream.end(),
80 [](const Command& c) { return c.sValue == "GPWSTestAudio"; }),bus->commandStream.end());
81}
82
84{
85 auto& g = bus->GPWS;
86 const auto& terrain = g.terrain;
87 g.faults = 0;
88 // Radio height is signed and calibrated to extended-gear contact in propulsion.cpp.
89 // Gear settling can put a healthy sample just below zero; validity is not its sign.
90 if (!g.radioAltitudeValid || !std::isfinite(bus->RadioAltitudeKm)) g.faults |= 1;
91 if (!g.airDataValid || !std::isfinite(bus->VerticalSpeedKms) || !std::isfinite(bus->IndicatedAirspeedKms)) g.faults |= 2;
92 if (!g.positionValid || !std::isfinite(playerships[0].mslKm) || playerships[0].mslKm == -1 ||
93 !TerrainAwareness::Finite({playerships[0].position.x,playerships[0].position.y,playerships[0].position.z})) g.faults |= 4;
95 {
96 if (!terrain.terrainComplete) g.faults |= 8;
97 if (g.obstaclesEnabled && !terrain.obstaclesComplete) g.faults |= 16;
98 }
99 const auto& below = terrain.cells[31*TerrainAwareness::GridSize+31];
100 const bool airborne = playerships[0].reference == REF_APNMODE || playerships[0].reference == REF_ACLMODE;
101 // A parked aircraft may be on an elevated pad, above the terrain database surface.
102 // Keep explicit input failures visible on ground, but run this cross-check in flight.
103 if (airborne && g.highIntegrity && !(g.faults&4) && terrain.enabled && below.valid)
104 {
105 const float computed = playerships[0].mslKm-below.topKm;
106 if (computed > .762f && computed-bus->RadioAltitudeKm > .6096f) g.faults |= 1;
107 }
108 if (g.faults != previousFaults)
109 {
110 for (const auto& line : FaultText(g.faults)) Remember(g.faultHistory,line);
111 previousFaults = g.faults;
112 }
113}
114
115bool Gpws::UpdateSelfTest(float seconds, bool powered, bool airborne)
116{
117 auto& g = bus->GPWS;
118 if (!g.testLevel || !powered || airborne) return false;
119 g.Alert = Bus::Gpws::NoAlert; g.terrain.ClearThreats();
120 g.warningLamp = g.cautionLamp = false;
121 // Stop competing test sequences. Real operational warnings remain owned by
122 // their modules; lift-off always terminates GPWS testing before alerting.
123 bus->commandStream.erase(std::remove_if(bus->commandStream.begin(),bus->commandStream.end(),[](const Command& c) {
124 return c.sValue == "GPWSPredictiveWind" || c.sValue == "GPWSReactive" || c.sValue == "GPWSAltitude" || c.sValue == "GPWSTerrain";
125 }),bus->commandStream.end());
126 if (g.testLevel > 1)
127 {
128 // Cycle long histories/configuration pages without hiding older items.
129 if (std::isfinite(seconds) && seconds > 0) testSeconds += seconds;
130 if (testSeconds >= 5) { testSeconds = 0; ++g.testStep; }
131 switch (g.testLevel)
132 {
133 case 2: g.testLines = FaultText(g.faults); break;
134 case 3:
135 g.testLines = {L"RISE GPWS / JET -022",L"TDB: RECEIVED VIENEO MESH",
136 g.peaks ? L"PEAKS DISPLAY ON" : L"NON-PEAKS DISPLAY",g.obstaclesEnabled ? L"OBSTACLES ON" : L"OBSTACLES OFF",
137 L"DH " + std::to_wstring(int(g.decisionHeightKm*1000)) + L" M"}; break;
138 case 4: g.testLines = g.faultHistory; if (g.testLines.empty()) g.testLines = {L"NO FAULT HISTORY"}; break;
139 case 5: g.testLines = g.warningHistory; if (g.testLines.empty()) g.testLines = {L"NO WARNING HISTORY"}; break;
140 case 6:
141 {
142 const unsigned inputs = unsigned(playerships[0].geardown) | unsigned(g.flapOverride)<<1 |
143 unsigned(g.mode5cancelled)<<2 | unsigned(g.steepApproach)<<3 | unsigned(bus->TrailingEdgeFlapsPosition>=20)<<4;
144 if (inputs != previousDiscretes || g.testLines.empty())
145 {
146 g.testLines = {playerships[0].geardown ? L"GEAR DOWN" : L"GEAR UP",
147 bus->TrailingEdgeFlapsPosition>=20 ? L"LANDING FLAPS" : L"FLAPS NOT LANDING",
148 g.flapOverride ? L"FLAP OVERRIDE ON" : L"FLAP OVERRIDE OFF",
149 (g.mode5cancelled) ? L"G/S INHIBIT ON" : L"G/S INHIBIT OFF",
150 g.steepApproach ? L"STEEP APPROACH ON" : L"STEEP APPROACH OFF"};
151 // Existing aural-unit acknowledgement accompanies the text;
152 // installation-specific discrete spoken labels need recordings.
153 Command c; c.name = "GPWSTestAcknowledge"; c.sValue = "GPWSTestAudio"; c.priority = 118;
154 bus->commandStream.push_back(c); previousDiscretes = inputs;
155 }
156 break;
157 }
158 }
159 return true;
160 }
161 g.testLines = {L"EGPWS SELF TEST"};
162 if (g.faults) { auto faults = FaultText(g.faults); g.testLines.insert(g.testLines.end(),faults.begin(),faults.end()); }
163 // State remains visibly asserted while its complete recording is playing.
164 g.cautionLamp = g.testStep == 2 || g.testStep == 3;
165 g.warningLamp = g.testStep >= 5 && g.testStep <= 8;
166 g.testPattern = g.testStep >= 7;
167 if (bus->vmuHold > 0 || std::any_of(bus->commandStream.begin(),bus->commandStream.end(),[](const Command& c) {
168 return c.sValue == "GPWSTestAudio";
169 })) return true;
170 if (!std::isfinite(seconds) || seconds <= 0) return true;
171 testSeconds += seconds;
172 if (testSeconds < (g.testStep == 8 ? 6.f : .75f)) return true;
173 testSeconds = 0;
174 const char* phrase = nullptr;
175 switch (g.testStep++)
176 {
177 case 1: phrase = "GPWSGlideslope"; break;
178 case 4: phrase = "GPWSPullUp"; break;
179 case 5: phrase = "GPWSTerrainTerrain"; break;
180 case 6: phrase = "GPWSPullUp"; break;
181 case 8:
182 g.testLevel = 0; g.testPattern = false; g.warningLamp = g.cautionLamp = false;
183 g.testLines = {g.faults ? L"TEST COMPLETE - CHECK FAULTS" : L"EGPWS TEST COMPLETE"}; g.testMessageSeconds = 5;
184 break;
185 }
186 if (phrase)
187 {
188 Command c; c.name = phrase; c.sValue = "GPWSTestAudio"; c.priority = 118; c.fValue = 1;
189 bus->commandStream.push_back(c);
190 }
191 return true;
192}
193
195{
196 auto& g = bus->GPWS;
197 floorValid = false;
198 if (!g.positionValid || playerships[0].isSimulator ||
199 (g.faults&4) || RiseMath::Vec3LengthSq(&playerships[0].position) < 1) return 0;
200 float tcf = FLT_MAX, nearest = FLT_MAX, field = 0, endRange = 0, closest = FLT_MAX, closestField = 0;
201 for (size_t i = 0; i < bus->waypoint.size(); ++i)
202 {
203 const auto& r = bus->waypoint[i];
204 if (r.type != 2 || r.heading < 0 || r.heading > 360 || !std::isfinite(r.length) || r.length <= 0 ||
205 !std::isfinite(r.elevationKm) || !TerrainAwareness::Finite({r.location.x,r.location.y,r.location.z}) || RiseMath::Vec3LengthSq(&r.location) < 1) continue;
206 RiseMath::Vector3 up, north(0,0,1), east;
207 RiseMath::Vec3Normalize(&up,&r.location); north -= up*up.z;
208 if (RiseMath::Vec3LengthSq(&north) < .00001f) continue;
209 RiseMath::Vec3Normalize(&north,&north); RiseMath::Vec3Cross(&east,&north,&up);
210 const auto course = north*std::cos(RiseMath::ToRadian(float(r.heading)))+east*std::sin(RiseMath::ToRadian(float(r.heading)));
211 const auto delta = playerships[0].position-r.location;
212 const float along = RiseMath::Vec3Dot(&delta,&course);
213 const auto acrossVector = delta-course*along-up*RiseMath::Vec3Dot(&delta,&up);
214 const float across = RiseMath::Vec3Length(&acrossVector);
215 const float beyond = along < 0 ? -along : (std::max)(0.f,along-r.length);
216 const float range = std::hypot(across,beyond);
217 const bool beside = across > beyond;
218 if (range < closest) { closest = range; closestField = r.elevationKm; }
219 tcf = (std::min)(tcf,GpwsBehavior::TerrainFloor(range,beside,g.highIntegrity));
220 const bool selected = i == size_t(bus->WPtargetC) && range < 42.6f;
221 const float score = selected ? -1.f : range;
222 if (score < nearest) { nearest = score; field = r.elevationKm; endRange = range; }
223 }
224 if (nearest == FLT_MAX) return 0;
225 const float height = playerships[0].mslKm-field;
226 const float rf = GpwsBehavior::RunwayFieldFloor(endRange,g.highIntegrity);
227 float penetration = 0;
228 if (!(g.faults&1) && tcf > 0 && bus->RadioAltitudeKm < tcf)
229 penetration = 1+(tcf-bus->RadioAltitudeKm)/tcf;
230 if (rf > -99 && height < rf) penetration = (std::max)(penetration,1+(rf-height)/(std::max)(rf,.009144f));
231 lastFloorHeight = height; fieldCalloutHeight = playerships[0].mslKm-closestField; floorValid = true;
232 return g.inhibited ? 0 : penetration;
233}
@ REF_ACLMODE
Definition ServerTypes.h:44
@ REF_APNMODE
Definition ServerTypes.h:42
@ REF_ONGROUND
Definition ServerTypes.h:43
float TrailingEdgeFlapsPosition
Definition Bus.h:424
struct Bus::Gpws GPWS
float IndicatedAirspeedKms
Definition Bus.h:34
std::vector< Swaypoint > waypoint
Definition Bus.h:500
float RadioAltitudeKm
Definition Bus.h:157
int WPtargetC
Definition Bus.h:499
float VerticalSpeedKms
Definition Bus.h:451
std::vector< Command > commandStream
Definition Bus.h:442
float vmuHold
Definition Bus.h:507
int testVehicle
Definition Module.h:83
float testSeconds
Definition Module.h:84
bool floorValid
Definition Module.h:88
float fieldCalloutHeight
Definition Module.h:86
float ClearanceFloor()
unsigned previousFaults
Definition Module.h:87
void ProcessControls(bool powered, bool airborne)
float lastFloorHeight
Definition Module.h:86
bool UpdateSelfTest(float seconds, bool powered, bool airborne)
void UpdateFaults()
unsigned previousDiscretes
Definition Module.h:87
Bus * bus
Definition Module.h:21
s_network_objects playerships[MAX_SCAN]
Definition globals.cpp:112
float TerrainFloor(float endRangeKm, bool besideRunway, bool highIntegrity)
float RunwayFieldFloor(float endRangeKm, bool highIntegrity)
auto ToRadian(T value)
Definition ClientMath.h:13
float Vec3Length(const Vector3 *a)
Definition ClientMath.h:95
Vector3 * Vec3Cross(Vector3 *out, const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:105
float Vec3LengthSq(const Vector3 *a)
Definition ClientMath.h:94
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:93
Vector3 * Vec3Normalize(Vector3 *out, const Vector3 *a)
Definition ClientMath.h:103
constexpr int GridSize
bool NeedsPrediction(float altitudeKm, float verticalKms, bool active)
bool Finite(Point p)
@ NoAlert
Definition Bus.h:190
bool mode5cancelled
Definition Bus.h:249
enum Bus::Gpws::GpwsEnum Alert
TerrainAwareness::State terrain
Definition Bus.h:264
std::string sValue
Definition Command.h:20
float fValue
Definition Command.h:25
unsigned char priority
Definition Command.h:28
std::string name
Definition Command.h:11
std::array< Cell, GridSize *GridSize > cells
RiseMath::Vector3 position
Definition globals.h:511
unsigned short vehicleId
Definition globals.h:537