kOS-scripts/autopilot/node.ks
Casper V. Kristensen a8de43efa6
First commit
2018-07-19 14:42:03 +02:00

72 lines
2.3 KiB
Plaintext
Executable file

run once util.
function execute_node {
parameter node is NEXTNODE.
print "EXECUTE MANEUVER NODE".
// Calculate estimated burn time
// https://en.wikipedia.org/wiki/Tsiolkovsky_rocket_equation
// https://space.stackexchange.com/questions/27375/how-do-i-calculate-a-rockets-burn-time-from-required-velocity
local isp_sum is 0.
list ENGINES in engines_list.
for engine in engines_list {
if engine:STAGE = STAGE:NUMBER set ISP_sum to ISP_sum + engine:VISP.
}
local ve is ISP_sum * (CONSTANT:G * KERBIN:MASS). // exhaust velocity
local burn_time is ((SHIP:MASS * ve) / SHIP:MAXTHRUST) * (1 - CONSTANT:E^(-node:BURNVECTOR:MAG/ve)).
print "Estimated burn time: " + ROUND(burn_time).
print "Aligning ship with burn vector".
SAS OFF.
lock STEERING to node:BURNVECTOR.
wait until VANG(SHIP:FACING:VECTOR, node:BURNVECTOR) < 0.1.
print "Initializing warp".
unlock STEERING. // TODO: Doesnt work?
warp_for(MAX(0, node:ETA - (burn_time/2) - 5)). // Warp until 5s before node
print "Approaching".
lock STEERING to node:BURNVECTOR.
wait until node:ETA <= burn_time/2.
print "Burn!".
lock THROTTLE to 1.0.
//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
// TODO: Smooth at the end
wait until node:DELTAV:MAG < 5.0. // TODO
set THROTTLE to 0.0.
print "DONE! Removing node and unlocking steering".
remove node.
unlock STEERING.
unlock THROTTLE.
SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
}
function create_circularization_node {
// parameter where.
print "CREATE CIRCULARIZATION NODE".
local node is NODE(TIME:SECONDS + ETA:APOAPSIS, 0, 0, 0).
add node.
// Calculate required velocity to acheive orbit at altitude equal to apoapsis
// https://en.wikipedia.org/wiki/Orbital_speed#Mean_orbital_speed
local orbital_speed is SQRT((CONSTANT:G * ORBIT:BODY:MASS) / (ORBIT:BODY:RADIUS + APOAPSIS)).
// Calculate the difference between the required velocity and our velocity at apoapsis
set node:PROGRADE to orbital_speed - VELOCITYAT(SHIP, TIME:SECONDS + ETA:APOAPSIS):ORBIT:MAG.
}
function create_orbitchange_node {
parameter what.
}