manuver node execution

This commit is contained in:
Gabe Venberg 2024-07-06 11:35:41 -05:00
commit c8a839b49c
4 changed files with 128 additions and 0 deletions

7
lib/SASCheck.ks Normal file
View file

@ -0,0 +1,7 @@
function sascheck { //returns the average exaust velocity of active engines. to convert to isp, divide by constant:g0
set saslist to ship:modulesnamed("ModuleSAS").
if saslist:length = 0 {
return false.
}
return true.
}

16
lib/isp.ks Normal file
View file

@ -0,0 +1,16 @@
function evcalc { //returns the average exaust velocity of active engines. to convert to isp, divide by constant:g0
local englist is list().
local totalflow is 0.
local totalthrust is 0.
list engines in englist.
for eng in englist {
if eng:ignition and not eng:flameout {
set totalflow to totalflow + (eng:possiblethrust /(eng:isp * constant:g0)).
set totalthrust to totalthrust + eng:possiblethrust.
}
}
if totalthrust = 0 { //avoid div by 0 later
return 1.
}
return (totalthrust / totalflow).
}

5
loadmanprogram.ks Normal file
View file

@ -0,0 +1,5 @@
if not exists("1:/lib") {
createdir("1:/lib").
}
compile "0:/lib/isp.ks" to "1:/lib/isp.ksm".
compile "0:/nde.ks" to "1:/nde.ksm".

100
nde.ks Normal file
View file

@ -0,0 +1,100 @@
//import functions
run once "lib/isp".
set nd to nextnode.
//print out node's basic parameters - eta and deltav
print "node in: " + round(nd:eta) + ", deltav: " + round(nd:deltav:mag).
//calculate ship's max acceleration
set maxacc to ship:availablethrust/ship:mass.
// calculate exaust velocity of all active engines
set ev to evcalc().
set thrust to ship:availablethrust.
set dv to nd:deltav:mag.
//magic equasion to figure burnduration.
set burnduration to ((ship:mass*ev)/thrust)*(1-constant:e^(-(dv/ev))).
print "estimated burn duration: " + round(burnduration) + "s".
sas off.
set steeringmanager:maxstoppingtime to 10.
set steeringmanager:pitchpid:kd to 2.
set steeringmanager:yawpid:kd to 2.
set steeringmanager:rollpid:kd to 2.
lock steering to lookdirup(nd:deltav, sun:position). //points to node, roll so that solar panels are facing towards sun.(design craft so solar panels are pointing out of vab door and directly away from vab door.)
// sas is better at steering craft with very little control.
//sas on.
//sas needs 1 tick between turning on and setting mode.
//wait until true.
//set sasmode to "maneuver".
//now we need to wait until the burn vector and ship's facing are aligned
wait until vang(nd:deltav, ship:facing:vector) < 0.25.
//warp to 60 seconds before the burn, if we are more than 90 seconds away from it. otherwise, we wait.
if nd:eta-(burnduration/2) > 90 {
set unwarptime to time:seconds+nd:eta-(burnduration/2)-60.
kuniverse:timewarp:warpto(unwarptime).
}
//give some time for our sunroll.
wait until nd:eta <= ((burnduration/2)+55).
set steeringmanager:maxstoppingtime to 2.
set steeringmanager:pitchpid:kd to 2.
set steeringmanager:yawpid:kd to 2.
set steeringmanager:rollpid:kd to 2.
//lock the steering, so we wont be chasing the manuver node.
//points to node, roll so that solar panels are facing towards sun.(design craft so solar panels are pointing out of vab door and directly away from vab door.)
set dir to lookdirup(nd:deltav, sun:position).
lock steering to dir.
//the ship is facing the right direction, let's wait for our burn time
wait until nd:eta <= (burnduration/2).
//we only need to lock throttle once to a certain variable in the beginning of the loop, and adjust only the variable itself inside it
set tset to 0.
lock throttle to tset.
set done to false.
//initial deltav
set dv0 to nd:deltav.
until done
{
//recalculate current maxacceleration, as it changes while we burn through fuel
set maxacc to ship:availablethrust/ship:mass.
//throttle is 100% until there is less than 1 second of time left to burn
//when there is less than 1 second - decrease the throttle linearly
set tset to min(nd:deltav:mag/maxacc, 1).
//here's the tricky part, we need to cut the throttle as soon as our nd:deltav and initial deltav start facing opposite directions
//this check is done via checking the dot product of those 2 vectors
if vdot(dv0, nd:deltav) < 0
{
print "end burn, remain dv " + round(nd:deltav:mag,1) + "m/s, vdot: " + round(vdot(dv0, nd:deltav),1).
lock throttle to 0.
break.
}
//we have very little left to burn, less then 0.1m/s
if nd:deltav:mag < 0.1 {
print "finalizing burn, remain dv " + round(nd:deltav:mag,1) + "m/s, vdot: " + round(vdot(dv0, nd:deltav),1).
//we burn slowly until our node vector starts to drift significantly from initial vector
//this usually means we are on point
wait until vdot(dv0, nd:deltav) < 0.5.
lock throttle to 0.
print "end burn, remain dv " + round(nd:deltav:mag,1) + "m/s, vdot: " + round(vdot(dv0, nd:deltav),1).
set done to true.
}
}
unlock steering.
unlock throttle.
wait 1.
//we no longer need the maneuver node
remove nd.
//set throttle to 0 just in case.
set ship:control:pilotmainthrottle to 0.
sas on.