1
2
3
4#include <windows.h>
5#include <cmath>
6#include <cwchar>
7#include <cwctype>
8#include <cstdlib>
9#include <iostream>
10#include <string>
11#include <vector>
12#include "../Modules/OrbitState.h"
13#include "../Modules/OrbitAttitude.h"
14#include "../Modules/FlightPath.h"
15#include "../StringUtilities.h"
16
18#include "FmaAfcs.generated.h"
26};
31 template<
class... Args>
explicit Sprite(Args...) {}
33};
35 inline float ToDegree(
float value) {
return value * 57.29577951308232f; }
36 inline float ToRadian(
float value) {
return value / 57.29577951308232f; }
37 constexpr float Tau = 6.283185307179586f;
38}
39struct {
int reference = -1;
float mslKm = 0; }
playerships[1];
40struct Draw { std::wstring text;
int x, y;
DWORD color; };
41std::vector<Draw> draws;
43protected:
48public:
49 Instrument(
int,
int,
float,
float, Bus* b, Logger* l, DeviceObject* d, std::vector<Font*>)
53 void DrawText(
const WCHAR* value,
int x,
int y,
float,
int,
DWORD color,
int,
const char*) {
54 draws.push_back({value,
x,
y, color});
55 }
56 void DrawTextW(
const WCHAR* value,
int x,
int y,
float z,
int flags,
DWORD color,
int font,
const char* name) {
57 DrawText(value,
x,
y, z, flags, color, font, name);
58 }
59};
60#include "FmaProduction.generated.h"
61
62int checks = 0;
63void Check(bool ok, const char* message) {
64 ++checks;
65 if (!ok) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); }
66}
67bool TextAt(int x, int y, const wchar_t* text) {
68 for (const auto& draw : draws)
69 if (draw.x == x && draw.y == y && draw.text == text) return true;
70 return false;
71}
72bool HasText(const wchar_t* text) {
73 for (const auto& draw : draws) if (draw.text == text) return true;
74 return false;
75}
76DWORD ColorAt(
int x,
int y) {
77 for (const auto& draw : draws) if (draw.x == x && draw.y == y) return draw.color;
78 return 0;
79}
80
81
82
83struct CycleServices {
84 Bus* bus;
85 __declspec(noinline) void TurnOffDOR() { Check(false, "unexpected DOR cancellation"); }
86 void FlightDirectorOn() { Check(false, "unexpected flight director engagement"); }
87};
88void CycleVertical(
Bus& bus,
bool repeating =
false) {
89 CycleServices services{&bus};
90 auto* gameclass = &services;
93 [[maybe_unused]] float verticaltime = 0;
94#include "FmaVerticalCycle.generated.h"
95}
96void CycleLateral(
Bus& bus,
bool repeating =
false) {
97 CycleServices services{&bus};
98 auto* gameclass = &services;
99 const float insthdgDegrees = 90;
100 [[maybe_unused]] float lateraltime = 0;
101#include "FmaLateralCycle.generated.h"
102}
103
104void GroundStateTests() {
106 Bus bus; CycleServices services{&bus};
107 struct View {CycleServices* gameclass;} view{&services};
auto* viewscreen=&view;
111
114#include "FmaGroundState.generated.h"
115 Check(bus.
OnGround==(reference==
REF_ONGROUND),
"bus contact state follows the live reference in both directions");
117 CycleVertical(bus);
119 "live airborne publication permits FPA after PIT while ground still skips it");
120 }
121}
122
123void AxisDefaultTests() {
125
126
127 struct FlightCase { bool ground; float speed, vertical, msl; };
128 for (const auto flight : {FlightCase{true,0,0,0}, FlightCase{false,0,.002f,.02f},
129 FlightCase{false,.25f,0,5}, FlightCase{false,2,1,50},
130 FlightCase{false,4,0,150}, FlightCase{false,7.5f,0,400},
131 FlightCase{false,7,-.4f,80}})
132 for (bool engaged : {false, true})
133 for (auto vertical : {Afcs::AltitudeMsl, Afcs::Pitch, Afcs::FlightPathAngle, Afcs::VerticalSpeed,
134 Afcs::SpeedPitch, Afcs::AltitudeAgl, Afcs::Glideslope, Afcs::VerticalMode_TakeOff, Afcs::VerticalMode_Off})
135 for (auto lateral : {Afcs::Localizer, Afcs::Roll, Afcs::Heading, Afcs::Navigation, Afcs::LateralMode_TakeOff}) {
140 bool expectedEngaged = engaged;
148 CycleLateral(bus, true);
150 "lateral key repeat leaves both axes unchanged");
151
152 for (int step = 0; step < 6; ++step) {
153 CycleLateral(bus);
154 const bool fallback = vertical == Afcs::VerticalMode_Off;
156 "lateral cycle preserves active vertical guidance; only Off defaults to PIT");
160 "lateral selection captures pitch only for an unset vertical axis");
163 "lateral cycle preserves A/F engagement and armed modes");
164 if (step == 2) { expectedEngaged = !expectedEngaged; bus.
AFCS.
AutopilotEngaged = expectedEngaged; }
165 }
166
167
168 if (vertical != Afcs::VerticalMode_Off) for (float bank : {-.2f, .01f, .2f}) {
171 CycleVertical(bus);
174 "vertical selection with lateral Off supplies ROL and captures current bank");
175 if (vertical == Afcs::VerticalMode_TakeOff)
178 "cycling the takeoff vertical mode captures PIT without passing through Off");
179 }
180 }
181}
182
183void VerticalCycleTests() {
185
186
187 struct FlightCase { bool ground; float speed, vertical, msl; };
188 for (const auto flight : {
189 FlightCase{true, 0, 0, 0}, FlightCase{false, 0, .002f, .02f},
190 FlightCase{false, .25f, 0, 5}, FlightCase{false, 2, 1, 50},
191 FlightCase{false, 4, 0, 150}, FlightCase{false, 7.5f, 0, 400},
192 FlightCase{false, 7, -.4f, 80}, FlightCase{false, .0101f, 0, 10},
193 FlightCase{false, .009f, 0, 10.001f}})
194 for (bool engaged : {false, true})
195 for (auto lateral : {Afcs::Heading, Afcs::Navigation, Afcs::Localizer, Afcs::Roll, Afcs::LateralMode_TakeOff}) {
197 FMA2 fma(0, 0, 1, 1, &bus, &logger, &device, {});
213 fma.FrameMove(11); fma.FrameMove(4);
214
215 for (int cycle = 0; cycle < 2; ++cycle) {
216 int presses = 0;
217 do {
218 CycleVertical(bus);
220 "vertical cycle preserves active lateral guidance including PIT wrap");
223 "vertical cycle preserves lateral targets");
227 "vertical cycle preserves Autoflyte engagement and armed modes");
229 "vertical cycle wraps without selecting separately armed glideslope");
230 draws.clear(); fma.FrameMove(.1f); fma.Render();
231 const auto* label = lateral == Afcs::Heading ? L"HDG" :
232 lateral == Afcs::Navigation ? L"NAV" : lateral == Afcs::Localizer ? L"LOC" :
233 lateral == Afcs::LateralMode_TakeOff ? L"T/O" : L"ROL";
234 Check(TextAt(96, 13, label) && ColorAt(96, 13) == 0xff00ff00,
235 "FMA retains lateral label without a false mode-change highlight");
237 const int expected = (flight.ground || flight.speed < .010f ? 5 : 6) - (flight.msl > 10 ? 1 : 0);
238 Check(presses == expected, "complete cycle respects existing FPA and AGL availability");
240 "return to PIT captures current pitch and displays PIT");
241 }
243 CycleVertical(bus, true);
245 CycleVertical(bus);
247 "cycling out of captured glideslope preserves lateral mode");
248
251 CycleVertical(bus);
253 "PIT wrap with lateral Off initializes the normal roll fallback");
254 }
255}
256void HeadingOwnershipTests() {
258 struct Owner { bool tvm, autopilot; Afcs::LateralModes lateral; };
259 const Owner owners[] = {{false,false,Afcs::LateralMode_Off}, {true,false,Afcs::LateralMode_Off},
260 {false,true,Afcs::Heading}, {false,true,Afcs::Navigation}, {false,true,Afcs::Localizer}};
261
262
263 for (float altitude : {0.f,.02f,5.f,50.f,150.f,563.6f,80.f})
264 for (int selection = 0; selection < 3; ++selection) for (const auto owner : owners) {
266 FMA2 fma(0, 0, 1, 1, &bus, &logger, &device, {});
279 fma.FrameMove(11);
281 "engaged DOR may show its commanded orbital heading");
285 const bool course = owner.lateral == Afcs::Navigation || owner.lateral == Afcs::Localizer;
286 const float expected = course ? selectedCourse : selectedHeading;
287
288
289 for (int tick = 0; tick < 20; ++tick) {
291 fma.FrameMove(.1f);
293 "DOR OFF preview cannot replace the manual, TVM, HDG, NAV or LOC heading readout");
296 "heading presentation does not change flight-control targets");
297 }
299 fma.FrameMove(.1f);
301 "re-engaging DOR restores its commanded orbital heading");
303 fma.FrameMove(.1f);
305 "DOR assigned AFCS steering displays the assigned heading or course");
306 }
307}
308
309void DorPanelTests() {
313 FMA2 fma(0, 0, 1, 1, &bus, &logger, &device, {});
314 auto frame = [&](float dt = .1f) { draws.clear(); fma.FrameMove(dt); fma.Render(); };
315 auto phase = [&](const wchar_t* label) {
316 const int x = 51 + (28 - 5 *
int(wcslen(label))) / 2;
317 Check(TextAt(x, 13, label), "DOR phase occupies the former A/T cell");
318 Check(!HasText(L"A/T"), "DOR phase replaces A/T even if autothrottle flag is set");
319 Check(x >= 51 && x + 5 * wcslen(label) <= 79, "phase text fits existing cell");
320 int sources = 0;
321 for (const auto& d : draws) if (d.text == L"DOR") ++sources;
322 Check(sources == 1 && TextAt(56, 1, L"DOR"), "DOR appears once as source, including AFCS ascent");
323 };
324 auto blankAxes = [&] {
325 for (const auto& d : draws)
326 Check(d.text.empty() || d.y != 13 || !((d.x >= 90 && d.x < 118) || (d.x >= 151 && d.x < 179)),
327 "released DOR attitude leaves both lower guidance cells blank");
328 };
329 auto align = [&] {
330 Check(TextAt(91, 13, L"ALIGN") && TextAt(153, 13, L"ALIGN"), "DOR attitude ownership shows ALIGN on both axes");
331 Check(91 + 25 <= 118 && 153 + 25 <= 179, "ALIGN fits both existing guidance cells");
332 };
333 frame(); Check(TextAt(56, 1, L"A/F") && TextAt(56, 13, L"A/T") && TextAt(181,25,L"0.000"), "lamp test retains normal labels and zero altitude");
334 frame(11); Check(!HasText(L"ALT") && !TextAt(181,25,L"0.000"), "unused zero altitude label and value are hidden");
336 Check(TextAt(56,1,L"TVM") && TextAt(96,13,L"TVM"), "TVM displays normally before DOR selection");
341 bus.
OnGround =
true; frame(); phase(L
"WAIT"); blankAxes();
342 Check(!HasText(L"TVM"), "switch from TVM clears cached label");
343 Check(ColorAt(55,13) == 0xff000000, "phase changes start highlight");
344 frame(4); Check(ColorAt(55,13) == 0xff00ff00, "phase highlight expires");
345 frame(); Check(ColorAt(55,13) == 0xff00ff00, "stable phase does not retrigger highlight");
346 g.ascentWaiting = false; g.ascentLiftoff = true; g.attitude = g.thrust = true;
349 g.ascentLiftoff = false; g.ascentMain = 1;
353 Check(TextAt(96,13,L"HDG") && TextAt(158,13,L"PIT") && !HasText(L"A/F"), "DOR source remains visible during AFCS HDG/PIT ascent");
355 Check(TextAt(158,13,L"FPA") && TextAt(151,25,L"+5.7"), "DOR aircraft FPA retains its angle readout");
358 Check(TextAt(91,13,L
"ALIGN") && bus.
AFCS.
DesiredRadians == 1.2f,
"assigned navigation remains attitude-controlled without replacing selected course");
361 g = {}; g.valid = true;
365 g = {}; g.valid = true; g.phase = coast; g.pointing = {0,0,-1};
366 Check(controller.
Step(g,rotation,{0,1,0},.1),
"real controller accepts wait/coast attitude release");
367 frame(); phase(coast ==
Phase::Wait ? L
"WAIT" : L
"COAST"); blankAxes();
368 Check(!g.attitude && !
ManualAttitudeDisconnect(g),
"blank axes agree with real controller's pilot attitude freedom");
369 }
370 g = {}; g.valid = g.thrust =
true; g.phase =
Phase::Depart; g.pointing = {1,0,0}; g.remainingDeltaV = 1;
371 Check(controller.
Step(g,rotation,{0,1,0},.1),
"real controller produces initial alignment");
372 frame(); phase(L"RCS"); align();
375 for (int n=0;n<10;++n) {
376 g = {}; g.valid = g.thrust =
true; g.phase =
Phase::Depart; g.pointing = {0,0,1}; g.remainingDeltaV = 1;
377 controller.
Step(g,rotation,{0,1,0},.1);
378 }
379 Check(g.thrust && g.attitude && g.phase ==
Phase::Depart,
"real controller captures and holds burn attitude");
380 frame(); phase(L"BURN"); align();
383 auto& a = bus.
orbit.
actuation; a.
dor = a.valid =
true; a.rotationEffort = {.2,0,0};
385 frame(); phase(L"RCS"); blankAxes();
388 a.programTime = 10.1; frame(); phase(L"COAST");
389 a.programTime = 10; a.dor = false; frame(); phase(L"COAST");
391 frame();phase(L"COAST");
392 a = {};
393 g.poweredRendezvous=g.attitude=g.thrust=
true;g.pointing={0,0,1};g.phase=
Phase::Depart;
395 g.accelerationCommand={0,-.005,.024};frame();phase(L"BURN");align();
396 g.accelerationCommand={0,-.005,0};frame();phase(L"RCS");align();
397 g.accelerationCommand={0,-.005,-.024};frame();phase(L"BRAKE");align();
398 g.poweredRendezvous=false;
399 g.entryControl = g.attitude = g.thrust =
true; g.phase =
Phase::Arrive;
400 g.pointing = {0,0,1}; g.accelerationCommand = {0,.002,-.01};
401 frame(); phase(L"BRAKE"); align();
402 g.accelerationCommand = {0,.002,.01}; frame(); phase(L"BURN");
403 g.accelerationCommand = {}; frame(); phase(L"COAST"); align();
404 bus.
FlightFreeze =
true; frame(); phase(L
"WAIT"); blankAxes();
407 g.valid = false; frame(); phase(L"WAIT"); blankAxes();
408 g.valid = true; g.entryLimited = true; g.accelerationCommand = {0,.002,.01}; frame(); phase(L"BRAKE");
410 frame(); Check(TextAt(96,1,L"LOC") && TextAt(158,1,L"G/S"), "upper row keeps armed modes during DOR");
412 Check(TextAt(56,1,L"TVM") && TextAt(56,13,L"A/T") && TextAt(96,13,L"TVM") && !HasText(L"BRAKE") && !HasText(L"ALIGN"), "DOR-to-TVM handoff clears phases and restores A/T");
415 frame();Check(TextAt(151,25,L"-2.5"),"selected PIT angle is visible below the active vertical cell");
418 frame(); Check(TextAt(56,1,L"A/F") && TextAt(56,13,L"A/T") && TextAt(96,13,L"HDG") && TextAt(184,13,L"ALT") && TextAt(181,25,L"0.000"), "active sea-level altitude hold remains visible at entry handoff");
420 Check(TextAt(198,17,L"A") && TextAt(181,25,L"0.000"), "active zero AGL hold remains visible");
422 Check(TextAt(184,1,L"ALT") && TextAt(181,25,L"30.00"), "nonzero selected altitude remains visible outside altitude hold");
424 Check(!HasText(L"ALT") && !TextAt(181,25,L"0.000"), "clearing unused altitude hides label and value together");
426 Check(TextAt(184,1,L"ALT") && TextAt(181,25,L"0.000"), "armed zero altitude remains a meaningful selection");
428 Check(!HasText(L"A/T") && !HasText(L"BURN"), "inactive throttle and DOR show no thrust indication");
429}
430
432 GroundStateTests();
433 AxisDefaultTests();
434 HeadingOwnershipTests();
435 VerticalCycleTests();
436 DorPanelTests();
437 std::cout << "PASS " << checks << " FMA rendered-text and transition checks\n";
438}
SCRIPT_CONTROL SCRIPT_STATE const void int
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
float RollAttitudeRadians
float PitchAttitudeRadians
float IndicatedAirspeedKms
void DrawText(const WCHAR *str, int relativeX, int relativeY, float prmZ, int flags, RiseMath::Color color, int font=-1, std::string elementName="", int width=0, int height=0) const
virtual void FrameMove(float fElapsed)
Instrument(int prmX, int prmY, float prmXScale, float prmYScale, Bus *prmBus, Logger *prmLogger, DeviceObject *prmDevice, std::vector< Font * > prmFonts)
void AddToCallStack(const char *msg)
bool Step(Guidance &g, const RotationState &in, Vec radial, double dt)
Sprite(Logger *prmLogger, DeviceObject *prmpDevice, std::string prmPathAndFilename, int prmX, int prmY, float prmZ, int prmTop, int prmLeft, int prmWidth, int prmHeight, DWORD prmColor, float scaleX=1.0f, float scaleY=1.0f)
void Draw(RiseMath::Color prmColor)
s_network_objects playerships[MAX_SCAN]
bool ManualAttitudeDisconnect(const Guidance &g)
HRESULT Draw(Rendering::RenderDevice *device, Rendering::RenderTexture *texture, Rendering::RenderBuffer *vertices, const RiseMath::Matrix &world, UINT segments, Rendering::RenderMaterial *volumeEffect=nullptr, float physicalScale=1)
(25) Automatic Flight Control System (AFCS) modes and engagement status, including autothrottle;
float DesiredPitchRadians
float DesiredHeadingRadians
enum Bus::Afcs::LateralModes CurrentLateralMode
float DesiredCourseRadians
enum Bus::Afcs::VerticalModes StandbyVerticalMode
enum Bus::Afcs::LateralModes StandbyLateralMode
enum Bus::Afcs::VerticalModes CurrentVerticalMode
double entryForwardAcceleration
ActuationTelemetry actuation
double ascentOrbitHeading