1#include "../Modules/TvmLeveling.h"
2#include "../Rendering/ClientMath.h"
3#include <cstdlib>
4#include <iostream>
5#include <limits>
6
8int checks=0;
9void Check(bool ok,const char* message) { ++checks; if(!ok) {std::cerr<<"FAIL: "<<message<<'\n';std::exit(1);} }
11{
13 return {{m._11,m._12,m._13},{m._21,m._22,m._23},{m._31,m._32,m._33}};
14}
16{
23}
24void Targets()
25{
26 struct Ship {int reference=-2,inarray=0;bool active=true,isOnScanner=true;};
27 struct Waypoint {Vec location{0,2939.6,0};};
31 Check(ground(),
"selected ground waypoint");b.
WPtargetC=-1;Check(!ground(),
"missing waypoint");
32 b.
WPtargetC=1;Check(!ground(),
"out of range waypoint");b.
WPtargetC=0;b.
waypoint[0].location={};Check(!ground(),
"uninitialized waypoint");
33 for(
int station:{-2,-3,-4}) {b.
targetC=station;Check(!ground(),
"station target cannot level");}
34 b.
targetC=0;Check(ground(),
"grounded contact");
35 for(int reference:{-3,-1,1,8,9,10}) {ships[1].reference=reference;Check(!ground(),"airborne/interior/station contact");}
36 ships[1].reference=0;ships[1].inarray=2;Check(ground(),"contact in grounded parent uses parent velocity");
37 ships[2].reference=-3;Check(!ground(),"airborne parent");ships[2].reference=-2;
38 for(int parent:{-1,0,4}) {ships[1].inarray=parent;Check(!ground(),"invalid parent");}
39 ships[1].reference=-2;ships[1].active=false;Check(!ground(),"inactive contact");ships[1].active=true;
40 ships[1].isOnScanner=false;Check(!ground(),"stale contact");ships[1].isOnScanner=true;
41 for(
int slot:{-1,0,4}) {b.
scanslot[0]=slot;Check(!ground(),
"invalid slot");}
42 b.
targetC=4;Check(!ground(),
"out of range target");b.
targetC=2;Check(!ground(),
"outside contact list");
43}
44void Policy()
45{
47 Check(c.blend==1&&c.pitch!=.12&&c.roll!=-.13,"hover commands both axes");
48 for(int condition=0;condition<18;++condition) {
49 auto x=i;
50 switch(condition) {
51 case 0:x.enabled=false;break;case 1:x.groundReference=false;break;case 2:x.airborne=false;break;
52 case 3:x.frozen=true;break;case 4:x.rotation.effort=0;break;case 5:x.stall=0;break;
53 case 6:x.ias=x.stall;break;case 7:x.ias=.2;break;case 8:x.airspeed=3;break;
54 case 9:x.airspeed=7.5;break;case 10:x.soundSpeed=0;break;case 11:x.dt=0;break;
55 case 12:x.rotation.acceleration.x=0;break;case 13:x.rotation.acceleration.z=0;break;
56 case 14:x.radial={};break;case 15:x.rotation.frame.up={};break;
57 case 16:x.ias=std::numeric_limits<double>::quiet_NaN();break;
58 case 17:x.rotation.rates.z=std::numeric_limits<double>::infinity();break;
59 }
61 Check(off.blend==0&&off.pitch==.12&&off.roll==-.13,"inhibit restores original manual augmentation");
63 }
64 for(
double speed:{0.,.005,.019}) {i.ias=speed;Check(
TvmLeveling::Command(i,0,0).blend==1,
"IAS zero does not suppress hover assist");}
65 i=Fixture();i.pilot.x=.01;c=
TvmLeveling::Command(i,.12,-.13);Check(c.pitch==.12&&c.roll!=-.13,
"pitch override independent of roll");
66 i.pilot={0,.5,.01};c=
TvmLeveling::Command(i,.12,-.13);Check(c.pitch!=.12&&c.roll==-.13,
"roll override independent of yaw/pitch");
67 i.pilot={};Check(
TvmLeveling::Command(i,.12,-.13).pitch!=.12,
"release manual control resumes leveling");
68 double last=1;
69 for(
int k=0;k<=200;++k) {i.ias=i.stall*(.9+.001*k);c=
TvmLeveling::Command(i,0,0);Check(c.blend<=last+1e-12,
"smooth fade through stall threshold");last=c.blend;}
70 i=Fixture();last=1;
71 for(
int k=0;k<=250;++k) {i.airspeed=i.soundSpeed*(.8+.001*k);c=
TvmLeveling::Command(i,0,0);Check(c.blend<=last+1e-12,
"smooth fade through Mach one");last=c.blend;}
72
73
74 for(double altitude:{0.,8.,30.,80.,120.,300.,3000.}) for(double speed:{.01,.1,2.,7.5}) {
75 i=Fixture();i.airspeed=speed;i.ias=speed*std::sqrt(std::exp(-altitude/7.5));
77 if(speed>=i.soundSpeed) Check(c.blend==0,"high-speed thin-air flight retains free pointing even with ground target");
78 i.groundReference=
false;Check(
TvmLeveling::Command(i,0,0).blend==0,
"airborne/spacecraft target never acquires landing leveling");
79 }
80}
81void Convergence()
82{
83
84
85 for(double dt:{1./240,1./60,1./20}) for(double acceleration:{.005,.03,.2})
86 for(
auto angles:{
Vec{.3,0,.5},
Vec{-.3,1,-.5},
Vec{0,0,
Pi},
Vec{
Pi/2,0,0}}) {
87 auto i=Fixture();i.dt=dt;i.rotation.acceleration={acceleration,acceleration,acceleration};
89 i.rotation.frame=Frame(q);i.rotation.rates={.01,0,-.02};
90 for(
int step=0;step<
int(240/dt);++step) {
92 Check(std::isfinite(c.pitch)&&std::isfinite(c.roll)&&std::abs(c.pitch)<=i.rotation.effort+1e-12&&
93 std::abs(c.roll)<=i.rotation.effort+1e-12,"finite bounded torque");
97 i.rotation.rates=i.rotation.rates+
Vec{-c.pitch*acceleration,0,-c.roll*acceleration}*(dt/i.rotation.effort);
98 }
99 Check(
Angle(i.rotation.frame.up,
Unit(i.radial))<.2*
Pi/180,
"nose and wings converge to horizon");
100 Check(
Norm(i.rotation.rates)<.05*
Pi/180,
"settles without continuing rotation");
101 }
102}
103int main() { Targets();Policy();Convergence();std::cout<<
"PASS: "<<checks<<
" TVM leveling checks\n"; }
SCRIPT_CONTROL SCRIPT_STATE const void int
std::vector< Swaypoint > waypoint
double Angle(Vec a, Vec b)
Quaternion * QuaternionMultiply(Quaternion *out, const Quaternion *a, const Quaternion *b)
Matrix * MatrixRotationQuaternion(Matrix *out, const Quaternion *q)
Quaternion * QuaternionNormalize(Quaternion *out, const Quaternion *q)
Quaternion * QuaternionRotationYawPitchRoll(Quaternion *out, float yaw, float pitch, float roll)
Matrix * MatrixInverse(Matrix *out, float *determinant, const Matrix *a)
Result Command(const Input &in, double manualPitch, double manualRoll)
bool GroundReference(const Bus &bus, const Ship(&ships)[Count], int onGround, int inAnother, double radius)