Mirror from SNAPKITTYWEST: src/sim/guidance.ts
Browse files- src/sim/guidance.ts +22 -0
src/sim/guidance.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {clamp,RAD} from './math';
|
| 2 |
+
import {navigation} from './navigation';
|
| 3 |
+
import type {FlightState} from './state';
|
| 4 |
+
export function guide(s:FlightState){
|
| 5 |
+
if(!s.guidance||!s.systems.navigation||!s.systems.computers||s.phase==='ABORT')return;
|
| 6 |
+
const n=navigation(s);
|
| 7 |
+
if(s.phase==='PRELAUNCH'||s.phase==='COUNTDOWN')return;
|
| 8 |
+
// Target a 220 km apogee; radial velocity closes as apogee approaches.
|
| 9 |
+
const targetApo=220000;
|
| 10 |
+
let targetVertical=n.altitude<1500?150:Math.sqrt(Math.max(0,2*n.gravity*(targetApo-n.altitude)))*0.55;
|
| 11 |
+
targetVertical=clamp(targetVertical,0,1050);
|
| 12 |
+
if(n.apoapsis>targetApo)targetVertical=Math.min(targetVertical,Math.max(-50,(targetApo-n.altitude)/200));
|
| 13 |
+
const thrustAccel=(s.stage===0?7600000:1300000)/s.mass;
|
| 14 |
+
const radial=clamp((targetVertical-n.vertical)/35+n.gravity-n.horizontal*n.horizontal/(n.altitude+6371000),-thrustAccel,thrustAccel);
|
| 15 |
+
const targetPitch=n.altitude<700?0:Math.acos(clamp(radial/thrustAccel,-1,1))/RAD;
|
| 16 |
+
s.pitch+=clamp(targetPitch-s.pitch,-0.65,0.65);
|
| 17 |
+
s.yaw=90;s.roll*=0.99;
|
| 18 |
+
s.throttle=clamp(3.5*9.80665/thrustAccel,0.2,1);
|
| 19 |
+
if(s.q>34000)s.throttle=Math.min(s.throttle,0.55);
|
| 20 |
+
if(n.periapsis>180000&&n.apoapsis<400000)s.throttle=0;
|
| 21 |
+
if(s.orbitAchieved)s.throttle=0;
|
| 22 |
+
}
|