1#include "../Modules/OrbitAttitude.h"
2#include "../Rendering/ClientMath.h"
3
5{
7 return {{m._11,m._12,m._13},{m._21,m._22,m._23},{m._31,m._32,m._33}};
8}
10{
11
12
16 state.frame=RotationFrame(q);
17 state.rates=state.rates+Vec{-command.x*state.acceleration.x,command.y*state.acceleration.y,
18 -command.z*state.acceleration.z}*(dt/state.effort);
19}
20void OrbitAttitudeTests()
21{
24 Check(std::abs(worn.x/full.x-.35)<1e-10&&std::abs(worn.z/full.z-.2625)<1e-10,"RCS authority includes power health wear and wing damage");
25 Check(
Norm(
RotationAuthority(0,20,30,40,10,20,1,1,1,1))==0,
"invalid inertia cannot authorize rotation");
26 for(int updateFrames:{1,10,20,40}) for(double authority:{.2,.01})
27 for(Vec target:{Vec{0,0,1},Vec{0,0,-1},Vec{1,0,0},Vec{0,1,0},
Unit(Vec{1,.3,-1})}) {
28 AttitudeController controller; RotationState state;
29 state.effort=.65;state.acceleration={authority,authority*.7,authority*.5};
31 state.frame=RotationFrame(q); state.rates={.01,-.01,.02};
32 Guidance g; bool captured=false; int captures=0;
33 for(int step=0;step<30000;++step) {
34 if(step%updateFrames==0) {
35 g={};g.valid=g.thrust=
true;g.attitude=
true;g.phase=Phase::Depart;g.pointing=
target;g.remainingDeltaV=1;
36 Check(controller.Step(g,state,{0,1,0},updateFrames*.01),"valid rotation control");
37 Check(
Finite(g.rotationCommand)&&
Norm(g.rotationCommand)<=std::sqrt(3.0)*state.effort+1e-10,
"all rotational RCS commands bounded by installed authority");
38 if(g.phase==Phase::Align) Check(!g.thrust,"ALIGN inhibits main and translation RCS demand");
39 if(g.thrust) { captured=true; ++captures;
40 Check(
Angle(state.frame.forward,target)<5*Pi/180,
"burn release points nose along delta-V");
41 }
42 }
43 RotateVehicle(q,state,g.rotationCommand,.01);
44 }
45 if(!captured||captures<10) std::cout<<
"uncaptured: "<<updateFrames<<
" "<<authority<<
" "<<
target.x<<
","<<
target.y<<
","<<
target.z<<
"\n";
46 Check(captured&&captures>10,"slew settles for delayed module updates, weak thrusters and reverse/vertical burns");
47 Check(
Angle(state.frame.forward,target)<.01,
"settled nose remains accurate");
48 if(std::abs(
target.y)<.98) Check(
Dot(state.frame.up,
Unit(Vec{0,1,0}-
target*
target.y))>.999,
"roll settles level with planet");
49 g={};g.valid=g.thrust=
true;g.phase=Phase::Depart;g.pointing=
target;g.remainingDeltaV=1;
50 state.rates={.1,0,0}; controller.Step(g,state,{0,1,0},.1);
51 Check(g.phase==Phase::Align&&!g.thrust,"excess rate returns to ALIGN");
52 }
53 AttitudeController controller; RotationState state;state.effort=1;state.acceleration={.1,.1,.1};
54 Guidance g;
55 for(int n=0;n<12;++n) { g={};g.valid=true;g.phase=Phase::Wait;g.pointing={0,0,1};controller.Step(g,state,{0,1,0},.1); }
56 Check(g.phase==Phase::Wait&&!g.thrust,"pre-alignment preserves WAIT thrust inhibition");
57 for(int n=0;n<12;++n) { g={};g.valid=true;g.phase=Phase::Coast;g.pointing={0,0,1};controller.Step(g,state,{0,1,0},.1); }
58 Check(g.phase==Phase::Coast&&!g.thrust,"coast pre-alignment never commands translation");
59 for(auto phase:{Phase::Wait,Phase::Coast}) {
60 g={};g.valid=true;g.phase=phase;g.pointing={0,0,-1};
61 controller.Step(g,state,{0,1,0},.1);
62 Check(g.phase==phase&&!g.thrust&&!g.attitude&&!
ManualAttitudeDisconnect(g),
"WAIT/COAST permit looking away without ALIGN or disconnect");
63 }
64 for(
auto phase:{Phase::Align,Phase::Depart,Phase::Arrive}) {g.phase=phase;Check(
ManualAttitudeDisconnect(g),
"manual steering takes over ALIGN and both burns");}
65 g={};g.valid=g.thrust=true;g.phase=Phase::Arrive;g.pointing={0,0,-1};g.remainingDeltaV=1;controller.Step(g,state,{0,1,0},.1);
66 Check(g.phase==Phase::Align&&g.maneuverPhase==Phase::Arrive,"arrival has its own alignment gate");
67 controller.Reset(); state={};state.effort=1;state.acceleration={.1,.1,.1};
68 for(int n=0;n<10;++n) {g={};g.valid=g.thrust=true;g.phase=Phase::Depart;g.pointing={0,0,1};g.remainingDeltaV=.001;controller.Step(g,state,{0,1,0},.1);}
69 g.pointing={0,0,-1};g.phase=Phase::Depart;controller.Step(g,state,{0,1,0},.1);
70 Check(g.thrust&&
Dot(g.pointing,Vec{0,0,1})>.99,
"sub-2m/s trims retain established attitude instead of reversing the nose");
71 g={};controller.Step(g,state,{0,1,0},.1);
72 Check(
Norm(g.rotationCommand)==0,
"invalid guidance resets controller commands");
73 g.valid=true;
74 state.acceleration.x=0;Check(!controller.Step(g,state,{0,1,0},.1),"missing pitch authority refuses control");
75 state.acceleration.x=.1;state.frame.forward={};Check(!controller.Step(g,state,{0,1,0},.1),"invalid attitude refuses control");
76 BusState bus;bus.sourceTime=1;bus.guidance.valid=bus.guidance.attitude=true;bus.guidance.rotationCommand={.2,.3,.4};
79 bus.guidance.valid=
false;Check(
Norm(
RotationCommand(bus,1.1))==0,
"disconnect clears rotational authority");
80}
81
82void CoastAttitudeTests()
83{
84 RotationState rotation;rotation.effort=1;rotation.acceleration={.1,.1,.1};
86 BusState bus;bus.guidance.valid=true;bus.guidance.phase=Phase::Coast;
87 const double radius=2939.6+937,speed=std::sqrt(97386.86/radius),rate=speed/radius;
88 const Vec initialUp{0,1,0};Vec radial=initialUp*radius;
89 for(int i=0;i<20000;++i) {
90 const double now=i*.05;
const State own{
Rotate(initialUp*radius,{1,0,0},rate*now),
Rotate({0,0,speed},{1,0,0},rate*now)};
92
93
94 const Vec pilot=now>100&&now<110?Vec{.05,.2,.05}:Vec{};
96 Check(
Finite(command)&&
Norm(command)<=std::sqrt(3.0),
"coast steering and curvature remain inside RCS limits");
97 RotateVehicle(q,rotation,command,.05);radial=own.r;
98 if(i==1500) Check(std::abs(
Dot(rotation.frame.forward,
Unit(radial)))<.002,
"forward-facing coast lowers nose as horizon curves");
99 if(i==2200) Check(
Dot(rotation.frame.forward,
Rotate({0,0,1},{1,0,0},rate*now))<.9,
"pilot can look away from flight direction during coast");
100 }
101 const double heldPitch=
Dot(rotation.frame.forward,
Unit(radial));
102 for(int i=0;i<10000;++i) {
103 const double now=1000+i*.05;
State own{
Rotate(initialUp*radius,{1,0,0},rate*now),
Rotate({0,0,speed},{1,0,0},rate*now)};
106 Check(std::abs(
Dot(rotation.frame.forward,
Unit(own.r))-heldPitch)<.003,
"curvature following preserves pilot-selected pitch after controls released");
107 }
108 Check(
Norm(
HorizonRotation({{radius,0,0},{0,radius*.0001,0}},.0001))<1e-12,
"planet-fixed hover has no invented curvature turn");
110 bus.guidance.phase=Phase::Align;Check(
Norm(
CoastRotationCommand(bus,bus.sourceTime,rotation,{1,1,1}))==0,
"free coast controller cannot override ALIGN");
111}
112
113void FlyAlignedRendezvous()
114{
115 Input in=Fixture(); in.spin=0; Request request=Settings(in);
116 in.minimumAcceleration=.00625; in.thrustKN=.0116*in.massKg;
117 in.forwardAcceleration=.0116; in.correctionSpoolSeconds=7.8;
118 const auto snapshot=
Solve(in,request); Executor executor; AttitudeController attitude;
119 auto g=executor.Begin(in,request,snapshot);
120 RotationState rotation; rotation.effort=1;
121 rotation.acceleration=
RotationAuthority(in.massKg*2.20462262,124.982147,186.511765,71.806427,
122 16.714558,48.750797,1,1,1,1);
124 rotation.frame=RotationFrame(q);
125 Vec filtered; double mainOutput=0,reverser=0; int alignFrames=0,burnFrames=0;
126 constexpr double dt=.05,spool=5.8,limit=.0116;
127 int step=0;
128 for(;step<100000&&executor.Active();++step) {
129 if(step%4==0) {
130 g=executor.Step(in,request);
132 Check(attitude.Step(g,rotation,in.own.r,.2),"rendezvous retains attitude authority");
133 if(g.phase==Phase::Align) {
134 Clearance c;
135 Check(executor.CorrectionClear(in,request,g,g.alignmentSeconds,c),
136 "live ALIGN coast followed by powered response clears finite-flight check");
137 }
138 if(step==0)
Preview(in,request,
"dor-align",&g);
139 }
140 if(g.phase==Phase::Align) ++alignFrames;
141 if(g.thrust) ++burnFrames;
142 const Vec demand=g.thrust?
InBody(rotation.frame,g.deltaVelocity):Vec{};
143 const double current=limit*(1-2*reverser)*mainOutput;
144 const double wanted=g.thrust?(std::clamp)(2*(demand.z-current*spool*.5)/std::pow(spool*.5,2),-limit,limit):0;
145 reverser=(std::clamp)(reverser+(wanted<0?dt*.5:-dt*.5),0.0,1.0);
146 mainOutput+=(std::clamp)(std::abs(wanted)/limit-mainOutput,-dt/spool,dt/spool);
148 filtered=filtered+(trim-filtered)*dt;
149 const auto& f=rotation.frame;
150 const Vec acceleration=f.right*(std::clamp)(filtered.x,-.00625,.00625)+
151 f.up*(std::clamp)(filtered.y,-.00625,.0116)+f.forward*(limit*(1-2*reverser)*mainOutput);
152 in.own=PoweredStep(in.own,acceleration,dt,in.gm); in.target=
TargetAt(in,dt); in.now+=dt;
153 BusState control;control.sourceTime=in.now;control.guidance=g;
154 const Vec pilot=g.phase==Phase::Coast&&in.now>900&&in.now<910?Vec{.05,.2,.05}:Vec{};
155 const Vec rotationCommand=g.phase==Phase::Coast||g.phase==Phase::Wait?
157 RotateVehicle(q,rotation,rotationCommand,dt);
158 Check(
Norm(in.own.r)>in.radius,
"aligned finite-burn flight clears ground");
159 }
160 std::wcout<<L"E-10 aligned rendezvous: "<<g.status<<L", "<<step*dt<<L"s, miss "<<g.positionErrorKm<<L"km, ALIGN "<<alignFrames*dt<<L"s\n";
161 Check(alignFrames>0&&burnFrames>0&&g.phase==Phase::Complete,"aligned E-10 main spool, reversers, RCS and moving target complete rendezvous");
162}
bool Finite(const Vector3 &v)
float Dot(const Vector3 &a, const Vector3 &b)
ArrivalPreview Preview(const Input &in, const Request &r)
Plan Solve(const Input &in, const Request &r, bool geometry)
double Angle(Vec a, Vec b)
Vec CoastRotationCommand(const BusState &data, double now, const RotationState &in, Vec pilot)
State TargetAt(const Input &in, double seconds)
Vec HorizonRotation(State own, double spin)
Vec InBody(const BodyFrame &f, Vec v)
Vec Rotate(Vec v, Vec axis, double angle)
Vec OrbitalRcsRequest(Vec deltaBody, double mainGrossAcceleration, bool hasMains)
Vec RotationAuthority(double weight, double pitchInertia, double yawInertia, double rollInertia, double arm, double span, double leftWing, double rightWing, double effort, double wear)
Vec RotationCommand(const BusState &data, double now, double available=1)
bool ManualAttitudeDisconnect(const Guidance &g)
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)
Quaternion * QuaternionIdentity(Quaternion *out)