1#pragma once
2#include "../Modules/FlightPath.h"
3void ArrivalTests()
4{
5 struct Waypoint {int type=0,heading=-1,pairedWithColonyId=0,ils=-1;bool powered=false;double length=0;Vec location;};
6 auto in=BillEntryFixture();Request r;r.entryOrigin=true;r.arrivalEpoch=in.now+4800;
7 std::vector<Waypoint> points(2);
8 points[0]={2,90,123,127,true,3,{in.radius+.2,0,0}};
9 const Vec normal=Unit(Cross(points[0].location,ArrivalPlanning::AircraftCourse(points[0].location,Pi/2)));
10 points[1]={1,90,123,-1,true,0,Rotate(points[0].location,normal,-8/in.radius)};
11 Check(!ArrivalPlanning::Site(points,-1).runway,"invalid selection does not invent airport metadata");
12 in.arrivalSite=ArrivalPlanning::Site(points,1);
13 Check(in.arrivalSite.runway&&in.arrivalSite.faf&&in.arrivalSite.ils,"LOM resolves unique associated inbound runway");
14 points.push_back(points[0]);
15 Check(!ArrivalPlanning::Site(points,1).runway,"ambiguous runway association requires explicit threshold selection");
16 points.pop_back();points[0].pairedWithColonyId=124;
17 Check(!ArrivalPlanning::Site(points,1).runway,"nearby unrelated airport is not assumed to be the selected approach");
18 points[0].pairedWithColonyId=123;
19 in.target.r=points[1].location;
20 r.final=Final::Ils;const auto ils=ArrivalPlanning::Preview(in,r);
21 Check(ils.valid&&std::abs(ils.distanceKm-10)<.001,"short outer marker preserves at least ten km of final");
22 r.final=Final::Visual;const auto visual=ArrivalPlanning::Preview(in,r);
23 const Vec tangent=ArrivalPlanning::AircraftCourse(points[0].location,Pi/2),up=
Unit(points[0].location);
24 const Vec delta=visual.gate-points[0].location;
25 Check(visual.valid&&std::abs(std::atan2(
Dot(delta,up),-
Dot(delta,tangent))-3*Pi/180)<1e-9,
26 "visual final lies on requested three-degree ray above runway tangent, with Vieneo curvature");
27 Check(std::abs(visual.altitudeKm-(.2+10*std::tan(3*Pi/180)))<.02,"ten-km final roughly 524m above runway plus curvature");
28 Check(
Dot(visual.gate-points[0].location,tangent)<0,
"final extends backwards from inbound threshold");
29 struct Controls {
30 enum Vertical {AltitudeMsl,FlightPathAngle};
enum Lateral {
Heading,Navigation};
31 bool DorOn=true,TvmOn=false,AutopilotEngaged=false,AutothrottleEngaged=false;
32 float DesiredAltitudeKm=0,DesiredTasKms=0,DesiredHeadingRadians=0,DesiredRadians=0,DesiredCourseRadians=0,DesiredFpaRadians=0;
33 Vertical CurrentVerticalMode=AltitudeMsl;Lateral CurrentLateralMode=
Heading;
34 } controls;
35 Guidance complete;complete.entryHandoff=true;complete.phase=Phase::Complete;
36 Check(
FinishEntryExecution(in,r,complete,
false,controls,[&]{controls.DorOn=
false;}),
"runway entry reaches normal AFCS handoff");
37 Check(controls.CurrentLateralMode==Controls::Navigation&&controls.CurrentVerticalMode==Controls::AltitudeMsl&&
38 controls.AutopilotEngaged&&controls.AutothrottleEngaged&&controls.DesiredAltitudeKm==30&&!controls.TvmOn,
39 "runway handoff holds altitude and TAS, follows NAV, never automatically descends or lands");
40 in.arrivalSite.ils=false;r.final=Final::Ils;
41 Check(!EntryPlanning::Solve(in,r,true).valid,"unserviceable ILS cannot be an executable arrival selection");
42 in.arrivalSite.ils=true;r.final=Final::Visual;
43 const auto p=EntryPlanning::Candidate(in,r,120,true,Route::Suborbital);
44 Check(p.arrival.valid&&!p.arrival.path.empty()&&!p.path.empty(),"entry keeps separate advisory final geometry");
45 Check(
Norm(p.entry.handoff.r)-in.radius>29.99,
"final preview never silently changes the tested entry handoff altitude");
46 const Vec handoffUp=
Unit(p.entry.handoff.r),targetNormal=
Rotate(normal,{0,0,1},in.spin*p.end);
47 Check(std::abs(
Dot(handoffUp,targetNormal))<1e-9,
"entry handoff placed on runway extended centerline");
48 Check(
Dot(p.entry.handoff.v-
Cross({0,0,in.spin},p.entry.handoff.r),
Cross(targetNormal,handoffUp))>0,
49 "entry handoff points inbound rather than away from runway");
50 r.final=Final::Vtol;in.entryUpKN=in.massKg*in.gm/(in.radius*in.radius)*.5;
51 Check(!ArrivalPlanning::Preview(in,r).valid,"hover preview rejects insufficient loaded vertical authority");
52 in.entryUpKN*=3;Check(ArrivalPlanning::Preview(in,r).valid,"hover preview uses actual loaded force, not empty ship thrust-to-weight");
53 for(Final f:{Final::Hold,Final::Ils,Final::Visual,Final::Vtol}) {
54 r.final=f;BusState display;display.request=r;display.snapshot.mode=Mode::Return;display.snapshot.plan=p;
55 const auto rows=
Rows(r,Mode::Return,
true);
56 for(
const auto& label:
Readouts(display,rows,0,
false))
57 Check(label.y<=304&&label.x+label.text.size()*5<=420,"arrival rows retain fixed instrument bounds");
58 }
59}
60void FlightPathTests()
61{
63 Check(!
Valid(0,0)&&!
Valid(.009,.01)&&!
Valid(NAN,0)&&!
Valid(.1,NAN),
"hover and bad data cannot define an FPA command");
64 Check(std::abs(
Capture(.2,-.01)-std::atan2(-.01,.2))<1e-12,
"FPA captures ground path, not nose pitch");
65 for(
double speed:{.03,.15,.5,4.8})
for(
double lag:{1.0,4.0})
for(
double target:{-20*
Degrees,-3*
Degrees,10*
Degrees}) {
67 for(int n=0;n<3000;++n) {
68 const double old=command;command=
Pitch(target,speed,speed*std::tan(gamma),pitch,command,.1);
69 Check(std::abs(command-old)<=3*
Degrees*.1+1e-12,
"FPA respects finite pitch-command slew across speed envelopes");
70 pitch+=(command-pitch)*.1/lag;gamma=pitch-2*
Degrees;
71 }
72 Check(std::abs(gamma-target)<.001,"FPA converges with finite attitude response and nonzero AoA");
73 }
74 Check(
Pitch(-3*
Degrees,0,0,0,.1,.1)==.1,
"invalid path never creates a sudden pitch command");
75 struct Afcs {
76 enum Vertical {
Pitch=1,FlightPathAngle,VerticalSpeed,SpeedPitch,AltitudeMsl,AltitudeAgl,Glideslope};
77 using VerticalModes=Vertical;
78 enum Lateral {
Heading,Navigation,Localizer,Roll};
79 Vertical CurrentVerticalMode=AltitudeMsl;Lateral CurrentLateralMode=
Heading;
80 bool AutopilotEngaged=true,DorOn=false,TvmOn=false;
81 } a;
82 for(bool ground:{false,true}) for(double speed:{0.0,.009,.010,.15,5.0}) {
84 Check(next==(!ground&&speed>=.010?Afcs::FlightPathAngle:Afcs::VerticalSpeed),
85 "V skips unavailable FPA on ground/hover and enters it in forward flight through orbital speeds");
86 auto mode=next;
88 Check(mode==Afcs::VerticalSpeed,"VS remains reachable after PIT/FPA");
89 mode=
NextVertical<Afcs>(mode,ground,speed,0);Check(mode==Afcs::SpeedPitch,
"SPD follows VS");
90 mode=
NextVertical<Afcs>(mode,ground,speed,0);Check(mode==Afcs::AltitudeMsl,
"ALTm remains reachable");
91 mode=
NextVertical<Afcs>(mode,ground,speed,0);Check(mode==Afcs::AltitudeAgl,
"ALTa remains reachable");
92 }
95 "bad path data skips FPA without trapping mode selection");
96 for(auto v:{Afcs::Pitch,Afcs::VerticalSpeed,Afcs::AltitudeMsl,Afcs::AltitudeAgl,Afcs::Glideslope,Afcs::FlightPathAngle})
97 for(auto l:{Afcs::Heading,Afcs::Navigation,Afcs::Localizer}) {
98 a.CurrentVerticalMode=v;a.CurrentLateralMode=l;Check(
SpeedHoldModes(a),
"speed hold survives approach/FPA mode transitions");
99 }
100 a.CurrentVerticalMode=Afcs::SpeedPitch;Check(!
SpeedHoldModes(a),
"pitch and throttle must not independently chase conflicting speed targets");
101 a.CurrentVerticalMode=Afcs::AltitudeMsl;a.AutopilotEngaged=
false;Check(!
SpeedHoldModes(a),
"manual takeover releases entry throttle");
102 a.AutopilotEngaged=
true;a.TvmOn=
true;Check(!
SpeedHoldModes(a),
"TVM owns translation after handoff");
103}
auto NextVertical(typename Afcs::VerticalModes current, bool onGround, double horizontal, double vertical)
double Capture(double horizontal, double vertical)
bool Valid(double horizontal, double vertical)
double Pitch(double selected, double horizontal, double vertical, double pitch, double previous, double dt)
bool SpeedHoldModes(const Afcs &a)
float Dot(const Vector3 &a, const Vector3 &b)
double Heading(Vec position, Vec direction)
Vec Rotate(Vec v, Vec axis, double angle)
std::vector< Row > Rows(const Request &r, Mode mode, bool entry=false, bool ascent=false, bool recordingAllowed=false)
std::vector< Label > Readouts(const BusState &data, const std::vector< Row > &rows, int uiRow, bool dorActive, bool showKeys=true)
bool FinishEntryExecution(const Input &in, const Request &r, Guidance &g, bool frozen, Afcs &afcs, Disconnect stopDor)