1#include "../Modules/OrbitModel.h"
2#include "../Modules/OrbitDisplay.h"
3#include "../Modules/OrbitGuidance.h"
4#include "../Modules/OrbitBurn.h"
5#include "../Rendering/ClientMath.h"
6#include <chrono>
7#include <cstdlib>
8#include <fstream>
9#include <iostream>
10#include <iomanip>
11
13int checks=0;
14void Check(bool condition,const char* message) { ++checks; if(!condition) { std::cerr<<"FAIL: "<<message<<"\n"; std::exit(1); } }
16{
21 i.
own={{2349.662601288514,-1677.0339345875998,-2587.3803218779112},{3.683554634502134,3.131307986183139,1.3191547303034297}};
22 const double inclination=60*
Pi/180,node=70*
Pi/180;
23 Vec p={std::cos(node),std::sin(node),0},q={-std::sin(node)*std::cos(inclination),std::cos(node)*std::cos(inclination),std::sin(inclination)};
27}
29{
33}
35{
36 const int steps=
int(std::ceil(std::abs(span)/.25));
const double h=span/steps;
37 auto acc=[&](
Vec r) {
return r*(-gm/std::pow(
Norm(r),3)); };
38 for(int i=0;i<steps;++i) {
39 const Vec a=acc(state.
r),b=acc(state.
r+state.
v*(h/2)),c=acc(state.
r+state.
v*(h/2)+a*(h*h/4));
40 const Vec d=acc(state.
r+state.
v*h+b*(h*h/2));
41 state.
r=state.
r+state.
v*h+(a+b+c)*(h*h/6);
42 state.
v=state.
v+(a+b*2+c*2+d)*(h/6);
43 }
44 return state;
45}
47{
55 Check(
Norm(end.r-s.
plan.
burns[1].position)<.001,
"independent RK4 transfer position within 1m");
56 }
57}
59{
60 auto acc=[&](
Vec r) {
return r*(-gm/std::pow(
Norm(r),3))+thrust; };
61 const auto a=acc(state.
r),b=acc(state.
r+state.
v*(dt/2)),c=acc(state.
r+state.
v*(dt/2)+a*(dt*dt/4));
62 const auto d=acc(state.
r+state.
v*dt+b*(dt*dt/2));
63 return {state.
r+state.
v*dt+(a+b+c)*(dt*dt/6),state.
v+(a+b*2+c*2+d)*(dt/6)};
64}
66{
68 const auto s=
Solve(in,r); Check(s.
plan.
valid,
"execution fixture plan available");
71 for(;seconds<(std::max)(5000.0,s.
plan.
end+1800)&&executor.
Active();seconds+=.2) {
72 Vec demand=g.thrust?g.deltaVelocity/2:
Vec{};
75 in.
own=PoweredStep(in.
own,output,.2,in.
gm);
77 const double rotation=-in.
spin*.2;
82 g=executor.
Step(in,r);
83 Check(
Finite(g.deltaVelocity),
"finite guidance throughout execution");
86 Check(g.attitude&&g.remainingDeltaV>0,"coast pre-aligns next burn and retains its delta-V");
88 }
89 std::wcout<<L"Flight "<<name<<L": "<<g.status<<L", "<<seconds<<L"s, miss "<<g.positionErrorKm<<L"km, dv "<<g.remainingDeltaV<<L"km/s\n";
91}
93{
95 if(guidance) data.
guidance=*guidance;
96 if(launch) data.
launch=*launch;
100 picture.labels.insert(picture.labels.end(),text.begin(),text.end());
101 std::ofstream f(std::string("x64/Release/OrbitTests/")+name+".svg");
102 f<<"<svg xmlns='http://www.w3.org/2000/svg' width='420' height='320' viewBox='0 0 420 320'><rect width='420' height='320' fill='black'/>";
103 auto color=[&](std::uint32_t c) { f<<'#'<<std::hex<<std::setw(6)<<std::setfill('0')<<(c&0xffffff)<<std::dec; };
104 for(const auto& l:picture.lines) {
105 f<<"<path d='M"<<l.a.x<<' '<<l.a.y<<" L"<<l.b.x<<' '<<l.b.y<<"' stroke='"; color(l.color); f<<"' fill='none'/>";
106 }
107 for(const auto& label:picture.labels) {
108 Check(label.x>=0&&label.x+label.text.size()*5<=420&&label.y>=0&&label.y+13<=320,"text stays within native MFD");
109 f<<"<text x='"<<label.x<<"' y='"<<label.y+10<<"' font-family='DejaVu Sans Mono,monospace' font-size='11' textLength='"<<label.text.size()*5<<"' lengthAdjust='spacingAndGlyphs' fill='";
110 color(label.color); f<<"'>";
111 for(wchar_t c:label.text) f<<"&#"<<unsigned(c)<<';';
112 f<<"</text>";
113 }
114 f<<"</svg>";
115}
116
117
118void FlyClientActuators()
119{
123 Vec bx{1,0,0},by{0,1,0},bz{0,0,1},filtered;
124 double mainOutput=0,reverser=0,seconds=0;
125 constexpr double dt=.05,spool=5.8,limit=.0116;
126 for(;seconds<5000&&executor.
Active();seconds+=dt) {
127 const Vec demand=g.thrust?
Vec{
Dot(g.deltaVelocity,bx),
Dot(g.deltaVelocity,by),
Dot(g.deltaVelocity,bz)}:
Vec{};
128 filtered=filtered+(demand-filtered)*dt;
129 const double current=limit*(1-2*reverser)*mainOutput;
130 const double wanted=g.thrust?(std::clamp)(2*(demand.
z-current*spool*.5)/std::pow(spool*.5,2),-limit,limit):0;
131 reverser=(std::clamp)(reverser+(wanted<0?dt*.5:-dt*.5),0.0,1.0);
132 mainOutput+=(std::clamp)(std::abs(wanted)/limit-mainOutput,-dt/spool,dt/spool);
133 const Vec acceleration=bx*(std::clamp)(filtered.x,-.00625,.00625)+by*(std::clamp)(filtered.y,-.00625,.0116)+bz*(limit*(1-2*reverser)*mainOutput);
135 auto rotate=[&](
Vec v) {
return Rotate(v,{0,0,1},-in.
spin*dt);};
137 bx=rotate(bx);by=rotate(by);bz=rotate(bz);in.
now+=dt;
138 g=executor.
Step(in,r);
140 }
141 std::wcout<<L"E-10 actuator rendezvous: "<<g.status<<L", "<<seconds<<L"s, miss "<<g.positionErrorKm<<L"km\n";
142 Check(g.phase==
Phase::Complete,
"E-10 spool, reversers and RCS complete rendezvous");
143}
144
145void FlyStationBubbles()
146{
147 Input in=Fixture();
const double radius=in.
radius+937.5;
150 in.
own={departure.
r+offset,departure.
v};
Request r=Settings(in);
151 Executor ex;
auto g=ex.
Begin(in,r,
Solve(in,r));Check(g.valid,
"bubble departure fixture has executable plan");
152 int bubble=0;bool departed=false,arrived=false;
153 double seconds=0;
154 for(;seconds<7000&&ex.
Active();seconds+=.2) {
159 Input parent=in;parent.
target=departure;parent.stationRate=std::sqrt(in.
gm/std::pow(radius,3));
161 const int integratedBubble=bubble;
162 if(bubble>=0) {
163
164
165 relative=relative+
output*.2;offset=offset+relative*.2;
166 const auto& center=bubble==0?departure:in.
target;
167 in.
own={center.r+offset,center.v+relative};
168 if(
Norm(offset)>40) { bubble=-1;departed=
true; }
169 } else {
170 in.
own=PoweredStep(in.
own,output,.2,in.
gm);
173 }
174 }
175 auto rotate=[&](
Vec v) {
return Rotate(v,{0,0,1},-in.
spin*.2);};
177 if(integratedBubble>=0) {
178
179 const auto& center=integratedBubble==0?departure:in.
target;
180 in.
own={center.r+offset,center.v+relative};
181 } else {
183 offset=rotate(offset);relative=rotate(relative);
184 }
189 Check(
Finite(g.deltaVelocity)&&
Norm(in.
own.
r)>in.
radius,
"bubble flight stays finite and clears surface");
190 }
191 std::wcout<<L"Station bubble flight: "<<g.status<<L", "<<seconds<<L"s, miss "<<g.positionErrorKm<<L"km\n";
192 Check(departed&&arrived&&g.phase==
Phase::Complete,
"DOR leaves one station bubble and completes in another");
193 struct Controls {bool DorOn=true,TvmOn=false;float DesiredClosingSpeed=.2f;} controls;
194 int disconnects=0;
196 controls.DorOn=false;controls.DesiredClosingSpeed=0;++disconnects;
197 }),"settled rendezvous in target station bubble hands off to TVM");
198 Check(!controls.DorOn&&controls.TvmOn&&controls.DesiredClosingSpeed==0&&disconnects==1,
199 "integrated rendezvous ends with zero TVM closing speed and one DOR disconnect");
200}
201
202void DirectionWarningTests()
203{
206 State terminal;
const double span=600;
207 auto braking=in.
own; braking.
v=braking.v*.99;
208 Check(
Propagate(braking,span,in.
gm,terminal),
"braking fixture propagates");
209 auto p=
Transfer(in,0,span,terminal.
r,{},
true,
false);
210 Check(p.valid&&
Dot(p.burns.front().vector,in.
own.
v)<0&&!p.reversesOrbit&&!p.retrograde,
211 "backward braking burn does not falsely warn of orbit reversal");
212 auto reverse=in.
own; reverse.
v=reverse.v*(-1);
213 Check(
Propagate(reverse,span,in.
gm,terminal),
"reverse fixture propagates");
214
215
218 p=
Transfer(in,0,span,terminal.
r,{},
true,
false);
219 Check(p.valid&&p.reversesOrbit&&p.retrograde&&!p.longBurn&&p.fuelEnough,"reverse transfer identified independently of performance");
222 auto roundTrip=
Transfer(in,0,span,terminal.
r,terminal.
v*(-1),
false,
false);
223 Check(roundTrip.valid&&roundTrip.goal.normal.z>0&&roundTrip.retrograde&&roundTrip.reversesOrbit,
224 "intermediate retrograde arc warns even when arrival restores a prograde orbit");
225 Check(
Propagate(in.
own,span,in.
gm,terminal),
"prograde fixture propagates");
226 auto arrival=
Transfer(in,0,span,terminal.
r,terminal.
v*(-1),
false,
false);
227 Check(arrival.valid&&arrival.retrograde&&arrival.reversesOrbit,"arrival burn reversal is also identified");
229 Executor executor; Check(executor.
Begin(in,r,s).
valid,
"direction caution does not silently prohibit a manually selected plan");
230 auto later=in; later.
own=reverse; later.now+=30;
232 Check(committed.reversesOrbit&&committed.retrograde,"committed warning persists after ownship direction changes");
233 in.
own=reverse; Check(
Propagate(in.
own,span,in.
gm,terminal),
"existing retrograde orbit propagates");
234 auto staying=
Transfer(in,0,span,terminal.
r,{},
true,
false);
235 Check(staying.valid&&staying.retrograde&&!staying.reversesOrbit,"staying in an existing retrograde orbit is not a reversal");
236 s.
plan=staying; Check(
SuitableInitialPass(s),
"existing retrograde flight is not categorically excluded from default PASS");
244 Check(!tilted.
reversesOrbit,
"orthogonal plane change is not mislabeled as opposite direction");
248 Check(!tilted.
retrograde&&!tilted.
reversesOrbit,
"zero world spin has no invented rotation-relative direction");
253 data.
clock=time; data.
request.
view=view;
bool caution=
false,atmosphere=
false;
255 if(label.text==L
"RETROGRADE / REVERSAL") caution=label.color==
Amber&&label.x==120&&label.y==22;
256 atmosphere=atmosphere||label.text.find(L"ATM PASS")!=std::wstring::npos;
257 Check(label.x>=0&&label.x+label.text.size()*5<=420&&label.y>=0&&label.y+13<=320,"direction caution stays within MFD");
258 }
259 Check(caution&&atmosphere,"steady direction warning coexists with atmospheric caution throughout EXEC blink");
260 }
263 Check(label.text.find(L"REVERSAL")==std::wstring::npos&&label.text.find(L"RETROGRADE")==std::wstring::npos,
264 "no stale direction warning after plan is cleared");
265
269 r=Settings(in); r.returnToSurface=
false; r.altitudeKm=200; r.arrivalEpoch=in.
now+10800; r.view=
View::Track;
270 const auto example=
Solve(in,r);
271 Check(!example.plan.valid&&example.plan.clearanceBlocked&&example.plan.retrograde&&example.plan.reversesOrbit,"formerly atmospheric reversal preview is now blocked for OVERFLY");
272 Preview(in,r,
"retrograde-warning");
273}
274void InitialPassTests()
275{
279 auto r=Settings(in); r.returnToSurface=false; r.altitudeKm=200;
280 r.arrivalSeconds=251; r.arrivalEpoch=in.
now+251;
281 Check(!
Solve(in,r).plan.
valid,
"short descending COAST pass has no clear continuing orbit");
283 auto finish=[&]() {
284 double worstMs=0; int ticks=0;
286 const auto start=std::chrono::steady_clock::now();
287 search.
Step(in,r,
false);
288 worstMs=(std::max)(worstMs,std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-start).count());
289 }
291 std::cout<<"Initial PASS "<<r.arrivalSeconds<<"s; ticks "<<ticks<<"; max tick "<<worstMs<<"ms\n";
292 };
293 finish();
294 Check(r.passTime==
PassTime::Unavailable,
"aggressive long-burn coast is not recommended automatically");
296 finish();
297 Check(r.passTime==
PassTime::Seeded,
"200km overfly gets a viable initial countdown");
298 auto s=
Solve(in,r); Validate(in,s);
300 Fly(in,r,"initial 200km coast pass");
301 auto quick=
Solve(in,r,
false);
302 Check(quick.plan.path.empty()&&quick.plan.goalPath.empty()&&quick.ownPath.empty(),"search trials omit rendering geometry");
303 Check(std::abs(quick.plan.deltaV-s.
plan.
deltaV)<1e-10&&quick.plan.fuel==s.
plan.
fuel,
"trial and rendered plans agree");
304 const double seedEpoch=r.arrivalEpoch; const auto seedRevision=r.revision;
305 in.
now+=10; search.
Step(in,r,
false);
306 Check(r.arrivalEpoch==seedEpoch&&r.revision==seedRevision,"initial time stays fixed instead of sliding with the clock");
307 const double remaining=r.arrivalEpoch-in.
now;
309 Check(std::abs(r.arrivalSeconds-(std::max)(30.0,remaining-300))<1e-9,"PASS decreases from displayed remaining time");
310 const double manualEpoch=r.arrivalEpoch; search.
Step(in,r,
false);
311 Check(r.passTime==
PassTime::Manual&&r.arrivalEpoch==manualEpoch,
"manual PASS is not silently repaired");
314 Check(r.passTime==
PassTime::Manual&&r.arrivalEpoch==manualEpoch,
"other edits preserve an explicit pass time");
315 r.returnToSurface=true; r.adjust=false;
319 Check(
Solve(in,r).plan.
burns.size()==2,
"circular exit search includes the arrival burn");
320 Fly(in,r,"initial circular pass");
322 Check(r.passTime==
PassTime::Pending,
"changing altitude recomputes untouched suggested time");
323 const auto pendingRevision=r.revision; const double pendingEpoch=r.arrivalEpoch;
324 for(
int tick=0;tick<100;++tick) search.
Step(in,r,
true);
325 Check(r.passTime==
PassTime::Pending&&r.revision==pendingRevision&&r.arrivalEpoch==pendingEpoch,
"automatic search cannot change an executing plan");
327 Check(r.passTime==
PassTime::Pending,
"ground keeps ascent-profile requirement instead of inventing a pass");
333 finish();
334 Check(r.passTime==
PassTime::Seeded&&r.arrivalSeconds>=730&&
Solve(in,r).plan.
start==700,
"initial pass respects delayed departure");
339 const auto exhaustedRevision=r.revision; const double exhaustedEpoch=r.arrivalEpoch;
340 search.
Step(in,r,
false);
341 Check(r.revision==exhaustedRevision&&r.arrivalEpoch==exhaustedEpoch,"exhausted search does not restart every frame or invent a deadline");
343 Check(r.passTime==
PassTime::Manual,
"manual preview remains available after search exhaustion");
344 auto low=
Solve(Fixture(),Settings(Fixture())); low.plan.atmosphere=
true;
345 Check(!
SuitableInitialPass(low),
"atmospheric previews are not suggested as warning-free defaults");
346 low.plan.atmosphere=false; low.plan.longBurn=true;
347 Check(!
SuitableInitialPass(low),
"long-burn caution is retained for manual preview, not an automatic suggestion");
348 low.plan.longBurn=false;
349 low.plan.burns.front().duration=low.plan.end-low.plan.start+1;
352 search.
Step(in,r,
false);
353 Check(r.passTime==
PassTime::Pending&&r.arrivalEpoch==in.
now+2040,
"rendezvous timing is not auto-initialized by overfly search");
354 in.
now+=600;
const double meet=r.arrivalEpoch-in.
now;
356 Check(r.arrivalSeconds==(std::max)(30.0,meet-300),"MEET also edits its displayed countdown without moving it later");
357}
358#include "SurfacePlanTests.h"
359#include "OrbitClearanceTests.h"
360#include "OrbitCorrectionTests.h"
361#include "OrbitAttitudeTests.h"
362#include "OrbitPresentationTests.h"
363#include "OrbitTraceTests.h"
364#include "OrbitEntryTests.h"
365#include "OrbitEntryControlTests.h"
366#include "OrbitEntryHandoffTests.h"
367#include "OrbitEntryReadinessTests.h"
368#include "OrbitBurnTests.h"
369#include "OrbitAscentTests.h"
370#include "OrbitAscentAfcsTests.h"
371#include "OrbitFastestLaunchTests.h"
372#include "OrbitYspOctober24Tests.h"
373#include "OrbitAscentProfileTests.h"
374#include "OrbitTargetInsertionTests.h"
375#include "OrbitEntryPhysicsReplayTests.h"
376#include "OrbitHandoffTests.h"
377#include "OrbitDockedPlanningTests.h"
378#include "OrbitArrivalTests.h"
379#include "OrbitApproachTests.h"
380#include "OrbitEntryInterruptionTests.h"
381#include "OrbitEntryBurnCoastTests.h"
382#include "OrbitDescendingHandoffTests.h"
383#include "OrbitSteepDescentTests.h"
384#include "OrbitEntryEnvelopeTests.h"
385int main(
int argc,
char** argv)
386{
387 if(argc>1&&std::string(argv[1])=="--ysp-oct24") {YspOctober24Tests();return 0;}
388 if(argc>1&&std::string(argv[1])=="--powered-experiment") {PoweredExperiment();return 0;}
389 if(argc>1&&std::string(argv[1])=="--ysp-oct24-flight") {
392 const auto e=search.
Result();Check(e.valid,
"October 24 fastest departure available");
393 FlyAscent(.4867,1,0,
true,200,
true,e.meetEpoch-in.
now,0,&in);
return 0;
394 }
395 if(argc>1&&std::string(argv[1])=="--entry-envelope") {EntryEnvelopeTests();std::cout<<"PASS "<<checks<<" envelope checks\n";return 0;}
396 if(argc>1&&std::string(argv[1])=="--steep-descent") {
397 SteepDescentTests();std::cout<<"PASS "<<checks<<" steep descent checks\n";return 0;
398 }
399 if(argc>1&&std::string(argv[1])=="--descending-handoff") {
400 DescendingHandoffTests();ApproachTests();FlightPathTests();
401 std::cout<<"PASS "<<checks<<" descending handoff checks\n";return 0;
402 }
403 if(argc>1&&std::string(argv[1])=="--entry-burn-coast") {
404 EntryBurnCoastTests();OrbitEntryTests();EntryControlTests();EntryReadinessTests();EntryInterruptionTests();
405 std::cout<<"PASS "<<checks<<" entry burn coast checks\n";return 0;
406 }
407 if(argc>1&&std::string(argv[1])=="--entry-interruption") {
408 EntryInterruptionTests();EntryControlTests();EntryReadinessTests();OrbitAttitudeTests();CoastAttitudeTests();
409 std::cout<<"PASS "<<checks<<" entry interruption checks\n";return 0;
410 }
411 if(argc>1&&std::string(argv[1])=="--approach") {
412 ArrivalTests();ApproachTests();FlightPathTests();OrbitTraceTests();
413 std::cout<<"PASS "<<checks<<" approach checks\n";return 0;
414 }
415 if(argc>1&&std::string(argv[1])=="--presentation") {
416 OrbitPresentationTests();std::cout<<"PASS "<<checks<<" presentation checks\n";return 0;
417 }
418 OrbitBurnTests();
419 if(argc>1&&std::string(argv[1])=="--entry-handoff") {EntryHandoffTests();return 0;}
420 AscentAfcsTests();YspInsertionTests();
421 AscentProfileTests();
422 TargetInsertionTests();
423 if(argc>1&&std::string(argv[1])=="--target-insertion") {
424 GroundRendezvousTests();AscentRejectionTests();
425 const auto ysp=YspAscentFixture();FlyAscent(.4867,1,0,true,200,true,FastestLaunchTests(),0,&ysp);
426 std::cout<<"PASS "<<checks<<" target insertion checks\n";return 0;
427 }
428 if(argc>1&&std::string(argv[1])=="--ascent-profile")return 0;
429 if(argc>1&&std::string(argv[1])=="--fastest") {FastestLaunchTests();return 0;}
430 if(argc>1&&std::string(argv[1])=="--ysp-fast") {const auto ysp=YspAscentFixture();FlyAscent(.4867,1,0,true,200,true,FastestLaunchTests(),0,&ysp);return 0;}
431 if(argc>1&&std::string(argv[1])=="--ysp") {const auto ysp=YspAscentFixture();FlyAscent(.4867,1,0,true,200,true,3938.8035,0,&ysp);return 0;}
432 if(argc>1&&std::string(argv[1])=="--ascent-turn") {FlyAscent(.6,1,0,true,200,false,7200,150);FlyAscent(.6,1,0,true,200,false,7200,-150);return 0;}
433 if(argc>1&&std::string(argv[1])=="--launch") {LaunchTests();return 0;}
434 if(argc>1&&std::string(argv[1])=="--ascent") {AscentTests();return 0;}
435 if(argc>1&&std::string(argv[1])=="--ascent-flight") {
436 AscentPitchRegressionTests();FlyAscent(124315.5859/255406,1,0,true,150);
437 FlyAscent();FlyAscent(1,1,0,true);FlyAscent(.6,1.1,.015,true);return 0;
438 }
439 PoweredExperiment();
440 const auto october=YspOctober24();const double octoberMeet=YspOctober24Tests();
441 FlyAscent(.4867,1,0,true,200,true,octoberMeet,0,&october);
442 const double fastestMeet=FastestLaunchTests();
443 const auto fastestOrigin=YspAscentFixture();
444 FlyAscent(.4867,1,0,true,200,true,fastestMeet,0,&fastestOrigin);
445 ArrivalTests();
446 ApproachTests();
447 SteepDescentTests();
448 DescendingHandoffTests();
449 FlightPathTests();
450 OrbitDockedPlanningTests();
451 OrbitHandoffTests();
452 OrbitTraceTests();
453 OrbitEntryTests();
454 EntryBurnCoastTests();
455 EntryControlTests();
456 EntryInterruptionTests();
457 EntryHandoffTests();
458 EntryReadinessTests();
459 AscentTests();
460 EntryPhysicsReplayTests();
461 OrbitPresentationTests();
462 OrbitAttitudeTests();
463 CoastAttitudeTests();
464 FlyAlignedRendezvous();
465 OrbitClearanceTests();
466 OrbitCorrectionTests();
467 DirectionWarningTests();
468 InitialPassTests();
469
470
471 const double radius=2939.6+937.5,gm=97386.86,rate=std::sqrt(gm/std::pow(radius,3));
472 double chordMin=1e9,chordMax=-1e9,exactMin=1e9,exactMax=-1e9;
473 auto floatVec=[](
Vec v) {
return Vec{double(
float(v.x)),double(
float(v.y)),double(
float(v.z))}; };
474 for(int step=0;step<120;++step) {
475 const double phase=1.3+rate*step*.2,meridian=.7;
477 const auto later=
CircularStation(radius,
double(
float(phase)+
float(rate)),45.84*
Pi/180,meridian,rate);
478 const State old={floatVec(exact.r),floatVec(later.r)-floatVec(exact.r)};
480 chordMin=(std::min)(chordMin,noisy.ap);chordMax=(std::max)(chordMax,noisy.ap);
481 exactMin=(std::min)(exactMin,stable.ap);exactMax=(std::max)(exactMax,stable.ap);
482 Check(stable.e<1e-12&&std::abs(stable.pe-radius)<1e-8,"analytic station state is circular without smoothing");
483 Check(std::abs(
Dot(exact.r,exact.v))<1e-8,
"instantaneous station velocity is tangent");
485 auto input=Fixture();input.own=exact;input.targetKind=kind;input.freeFlight=true;
486 auto request=Settings(input);request.adjust=true;
487 const auto readout=
Solve(input,request);
488 Check(std::abs(readout.own.ap-radius)<1e-8,"own readout stable independent of external target kind");
489 }
490 }
491 std::cout<<"Station AP span: old chord "<<(chordMax-chordMin)*1000<<"m; analytic "<<(exactMax-exactMin)*1000<<"m\n";
492 Check(chordMax-chordMin>.1&&exactMax-exactMin<1e-8,"reproduced station jitter removed at its source");
493 for(
double inclination:{0.0,
Pi/2,2.1})
for(
double direction:{-1.0,1.0}) {
494 const auto state=
CircularStation(radius,2.4,inclination,.8,rate*direction);
495 const double dt=.001;
496 const auto before=
CircularStation(radius,2.4-rate*direction*dt,inclination,.8,rate*direction);
497 const auto after=
CircularStation(radius,2.4+rate*direction*dt,inclination,.8,rate*direction);
498 Check(
Norm((after.r-before.r)/(2*dt)-state.
v)<1e-8,
"station derivative agrees for polar and retrograde motion");
499 }
502 for(
int ref:{-3,-1,8,9,10}) Check(
FreeFlightReference(ref,
false,8,3),
"atmospheric and orbital references remain available");
503 for(
int ref:{0,1,3,7,11}) Check(!
FreeFlightReference(ref,
false,8,3),
"contained and invalid references remain excluded");
504 auto bubble=Fixture();bubble.freeFlight=
FreeFlightReference(9,
false,8,3);
auto bubbleRequest=Settings(bubble);
505 Check(
Solve(bubble,bubbleRequest).plan.
valid,
"undocked bubble can solve a rendezvous");
506 Executor bubbleExecutor;Check(bubbleExecutor.
Begin(bubble,bubbleRequest,
Solve(bubble,bubbleRequest)).valid,
"DOR can begin inside bubble");
508 Check(bubbleExecutor.
Step(bubble,bubbleRequest).
valid,
"leaving bubble does not cancel DOR");
510 Check(bubbleExecutor.
Step(bubble,bubbleRequest).
valid,
"entering bubble does not cancel DOR");
512 bubble.dockedAtStation=true;
513 Check(!bubbleExecutor.
Step(bubble,bubbleRequest).
valid&&
Solve(bubble,bubbleRequest).plan.
valid,
"actual docking cancels execution but retains planning");
521 Check(std::abs(own.ap-in.
radius-937.986)<.001,
"original apoapsis readout");
522 Check(std::abs(own.pe-in.
radius-933.512)<.001,
"original periapsis readout");
523 Check(std::abs(own.period-4857.31)<.01,"original period");
524 Check(std::abs(own.inclination*180/
Pi-45.836422)<1e-5,
"original inclination");
525 State future; Check(
Propagate(in.
own,own.period,in.
gm,future),
"one revolution propagates");
528 Check(
Propagate(in.
own,1500,in.
gm,future)&&
Norm(future.
r-rk.
r)<1e-5,
"independent RK4 propagation");
531 Check(!
Describe({{0,0,0},{0,0,0}},in.
gm).
valid,
"empty state rejected");
532 Check(!
Propagate({{NAN,0,0},{0,0,0}},30,in.
gm,future),
"nonfinite rejected");
533 Vec a,b; Check(!
Lambert(circle.
r,circle.
r,100,in.
gm,
false,a,b),
"coincident Lambert rejected");
534 auto s=
Solve(in,r); Validate(in,s);
535 Check(std::abs(
Angle(own.normal,s.
target.
normal)*180/
Pi-38.03428473)<1e-5,
"mutual plane angle");
536 Check(s.
plan.
burns.size()==2,
"rendezvous has departure and matching burns");
538 Check(
Norm(s.
plan.
burns.back().position-end.r)<1e-6,
"5km staging point uses common arrival epoch");
542 s=
Solve(in,r); Validate(in,s); Check(std::abs(s.
plan.
goal.
ap-own.ap)<.001,
"AP lock preserves value without waiting");
544 s=
Solve(in,r); Validate(in,s); Check(s.
plan.
burns.size()==2,
"unreachable shape gets transfer");
548 in.
now+=30; Check(
Solve(in,r).plan.
start==570,
"deadline counts down instead of sliding");
551 const auto before=std::chrono::steady_clock::now(); s=
Solve(in,r); Validate(in,s);
552 std::cout<<"AUTO solve ms: "<<std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-before).count()<<"\n";
555 Check(s.
plan.
burns.size()==1,
"overfly coast one burn");
557 Check(
Angle(s.
plan.
path.back().r,ground)<1e-7,
"overfly accounts for rotating surface");
561 Check(surface.plan.planningOnly,"surface route is explicitly planning only");
563 Check(!previewExecutor.
Begin(in,r,surface).
valid,
"surface route cannot execute DOR");
568 Check(
Solve(in,r).status==L
"ATMOSPHERIC TARGET",
"APN target caution not blanket exclusion");
573 Check(s.
plan.
valid&&s.
status==L
"ATMOSPHERIC PASS",
"low circular orbit warns but remains previewable");
574 in.
own.
v={0,.2,0}; r=Settings(in); r.
adjust=
true;
575 s=
Solve(in,r); Check(!s.
plan.
valid,
"suborbital surface intersection not approved");
576 in=Fixture(); r=Settings(in); r.
adjust=
true; s=
Solve(in,r);
583 const auto pic=
Plot(s,v); Check(!pic.lines.empty(),
"geometry available for each view");
584 for(const auto& line:pic.lines) for(Point p:{line.a,line.b})
585 Check(p.x>=5&&p.x<=247&&p.y>=38&&p.y<=281,"graphics stay inside fixed panel");
586 }
588 Fly(in,r,"circularize");
589 in=Fixture(); r=Settings(in); Fly(in,r,"rendezvous");
591 Fly(in,r,"raise orbit");
595 FlyClientActuators();
596 FlyStationBubbles();
597 in=Fixture(); r=Settings(in);
Executor executor;
599 auto waiting=executor.
Begin(in,r,
Solve(in,r));
600 Check(waiting.phase==
Phase::Wait&&waiting.attitude&&!waiting.thrust&&waiting.remainingDeltaV>0,
"waiting pre-aligns without thrust");
601 in=Fixture(); r=Settings(in);
602 auto g=executor.
Begin(in,r,
Solve(in,r)); Check(g.valid,
"DOR begins valid plan");
607 Check(!executor.
Step(in,r).
thrust,
"power loss clears thrust");
609 Check(!executor.
Step(in,r).
thrust,
"simulator transition clears old guidance");
610 in=Fixture(); r=Settings(in); g=executor.
Begin(in,r,
Solve(in,r));
614 Check(
Solve(in,Settings(in)).ascentDetail,
"ground ascent detail");
617 groundGoal.
sizeKm+=300;
const double selectedSize=groundGoal.
sizeKm;
619 Check(groundGoal.
sizeKm==selectedSize,
"takeoff preserves selected orbital parameters");
622 Check(label.text.find(L"PLANE DIF")==std::wstring::npos,"ground has no invented orbital plane difference");
624 Check(
Solve(in,Settings(in)).ascentDetail,
"ascending below 100km");
626 Check(!
Solve(in,Settings(in)).ascentDetail&&
Solve(in,Settings(in)).flightPhase==L
"ORBIT INSERTION",
"100km changes view without claiming orbit");
627 in=Fixture(); r=Settings(in);
Preview(in,r,
"rendezvous");
633 SurfacePlanTests();
642 EntryEnvelopeTests();
643 std::cout<<"PASS "<<checks<<" orbital checks\n";
644}
SCRIPT_CONTROL SCRIPT_STATE const void int
Guidance Begin(const Input &in, const Request &r, const Snapshot &s, const LaunchEstimate *launch=nullptr)
Guidance Step(const Input &in, const Request &r, const LaunchEstimate *launch=nullptr)
Plan ReferencePlan(const Input &in) const
void Step(const Input &in, Request &r, bool executing)
void Begin(const Input &in, const Request &r)
const LaunchEstimate & Result() const
ArrivalPreview Preview(const Input &in, const Request &r)
void PlanDirections(Plan &p, Vec departureNormal, Vec transferNormal, double spin)
double Angle(Vec a, Vec b)
bool SuitableInitialPass(const Snapshot &s)
bool FreeFlightReference(int reference, bool docked, int firstStation, int stationCount)
const wchar_t * ModeName(Mode m)
State TargetAt(const Input &in, double seconds)
Plan Transfer(const Input &in, double start, double end, Vec r2, Vec v2, bool coast, bool circular, bool geometry=true, bool requireClearance=false)
Snapshot Solve(const Input &in, const Request &r, bool geometry=true)
Mode Objective(const Request &r, TargetKind target)
bool Lambert(Vec r1v, Vec r2v, double dt, double gm, bool longWay, Vec &v1, Vec &v2)
bool FinishExecution(const Input &in, Mode mode, Guidance &g, bool frozen, Afcs &afcs, Disconnect stopDor)
Vec Rotate(Vec v, Vec axis, double angle)
void RememberDepartureLeg(Request &r, const Input &in)
Vec ControlVelocityError(const BusState &data, double now)
void InitializeGoal(Request &r, const Input &in)
std::vector< Row > Rows(const Request &r, Mode mode, bool entry=false, bool ascent=false, bool recordingAllowed=false)
constexpr std::uint32_t Amber
Elements Describe(State s, double gm)
std::vector< Label > Readouts(const BusState &data, const std::vector< Row > &rows, int uiRow, bool dorActive, bool showKeys=true)
Picture Plot(const Snapshot &s, View view)
State CircularStation(double range, double phase, double inclination, double meridian, double rate)
void Edit(Request &r, Row row, int direction, const Snapshot &s, double now, bool circularize=false, bool recordingAllowed=false)
bool Propagate(State initial, double t, double gm, State &result)
std::vector< Burn > burns
std::vector< Sample > path
AscentProfile ascentProfile