2021-08-02 01:45:47 +00:00
|
|
|
|
|
|
|
function ExecNode {
|
2021-08-04 00:35:50 +00:00
|
|
|
if not HASNODE {
|
|
|
|
print "No node to execute.".
|
|
|
|
return.
|
|
|
|
}
|
|
|
|
|
2021-08-02 01:45:47 +00:00
|
|
|
SAS off.
|
2021-08-04 00:35:50 +00:00
|
|
|
|
|
|
|
// begin the burn at leadT seconds before the node.
|
|
|
|
local leadT is BurnTime(NEXTNODE:DELTAV:MAG / 2).
|
2021-08-02 01:45:47 +00:00
|
|
|
local t is BurnTime(NEXTNODE:DELTAV:MAG).
|
|
|
|
|
2021-08-06 02:05:40 +00:00
|
|
|
if WillStage(NEXTNODE:DELTAV:MAG) {
|
2021-08-06 01:45:19 +00:00
|
|
|
print "WARNING: kOS will stage during this node execution. Safe cancellation requires reboot.".
|
2021-08-06 05:36:26 +00:00
|
|
|
when FlameOut() then {
|
2021-08-06 01:45:19 +00:00
|
|
|
print "Flameout detected. Staging.".
|
|
|
|
stage.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 01:45:47 +00:00
|
|
|
print "Adjusting heading".
|
2021-08-06 01:45:19 +00:00
|
|
|
lock STEERING to LookDirUp(NEXTNODE:DELTAV, SHIP:FACING:TOPVECTOR).
|
2021-08-02 01:45:47 +00:00
|
|
|
wait until VAng(SHIP:FACING:FOREVECTOR, STEERINGMANAGER:TARGET:FOREVECTOR) <= 0.1.
|
|
|
|
|
|
|
|
print "Warping to node.".
|
2021-08-04 00:35:50 +00:00
|
|
|
KUNIVERSE:TIMEWARP:WarpTo(NEXTNODE:TIME - leadT - 5).
|
|
|
|
wait until NEXTNODE:ETA <= leadT.
|
2021-08-02 01:45:47 +00:00
|
|
|
|
|
|
|
print "Executing burn.".
|
|
|
|
local dvMin is NEXTNODE:DELTAV:MAG.
|
|
|
|
lock THROTTLE to 1.0.
|
2021-08-04 00:35:50 +00:00
|
|
|
wait t.
|
|
|
|
lock THROTTLE to 0.0.
|
|
|
|
|
|
|
|
unlock THROTTLE.
|
|
|
|
unlock STEERING.
|
|
|
|
SAS on.
|
|
|
|
print "Node execution complete.".
|
|
|
|
}
|
|
|
|
|
2021-08-06 05:32:24 +00:00
|
|
|
// currently only works for testing against *current* stage.
|
2021-08-06 02:05:40 +00:00
|
|
|
function WillStage {
|
|
|
|
parameter dV.
|
|
|
|
if not HASNODE { return false. }
|
2021-08-06 05:32:24 +00:00
|
|
|
return dV > SHIP:StageDeltaV(SHIP:STAGENUM):VACUUM.
|
2021-08-06 02:05:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 00:35:50 +00:00
|
|
|
// Calculate the time required to burn a given dV.
|
|
|
|
// Assumes a perfectly spherical Kerbal in a vacuum.
|
|
|
|
function BurnTime {
|
2021-08-06 01:45:19 +00:00
|
|
|
parameter totaldV, s is STAGE:NUMBER.
|
2021-08-04 00:35:50 +00:00
|
|
|
local totalT is 0.0.
|
|
|
|
|
2021-08-06 01:45:19 +00:00
|
|
|
local lastStage is false.
|
|
|
|
// We allow a small tolerance to deal with potential floating point errors.
|
|
|
|
until totaldV <= 0.001 {
|
2021-08-06 05:32:24 +00:00
|
|
|
local F is stageThrust(s).
|
|
|
|
local Isp is stageISP(s).
|
2021-08-06 01:45:19 +00:00
|
|
|
local m is stageMass(s).
|
|
|
|
// TODO: handle node execution in atmosphere?
|
2021-08-04 00:35:50 +00:00
|
|
|
local dV is min(totaldV, SHIP:StageDeltaV(s):VACUUM).
|
|
|
|
local t is calcBurnTime(dV, m, Isp, F).
|
2021-08-06 05:32:24 +00:00
|
|
|
|
2021-08-04 00:35:50 +00:00
|
|
|
set totaldV to totaldV - dV.
|
|
|
|
set s to s - 1.
|
|
|
|
set totalT to totalT + t.
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalT.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience function to wrap the actual calculation for burn time.
|
|
|
|
function calcBurnTime {
|
|
|
|
parameter dV, m, Isp, F.
|
2021-08-02 01:45:47 +00:00
|
|
|
|
2021-08-06 05:32:24 +00:00
|
|
|
if F = 0 or Isp = 0 {
|
|
|
|
print "WARNING: Tried to calculate burn time with a denominator value of 0. Returning 0. Your calculations are probably wrong.".
|
|
|
|
print "F: " + F .
|
|
|
|
print "Isp: " + Isp.
|
2021-08-04 00:35:50 +00:00
|
|
|
return 0.
|
|
|
|
}
|
|
|
|
|
|
|
|
local g0 is CONSTANT:G0.
|
|
|
|
return g0 * m * Isp * (1 - CONSTANT():E^(-dV/(g0*Isp))) / F.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the ISP for a given stage.
|
|
|
|
// Defaults to current stage. Assumes your ship is designed so that
|
|
|
|
// engines are discarded immediately when they flame out.
|
|
|
|
function stageISP {
|
|
|
|
parameter s is STAGE:NUMBER.
|
2021-08-02 01:45:47 +00:00
|
|
|
|
2021-08-04 00:35:50 +00:00
|
|
|
local en is list().
|
|
|
|
list ENGINES in en.
|
|
|
|
|
|
|
|
local ispSum is 0.
|
|
|
|
local eCount is 0.
|
|
|
|
for e in en {
|
|
|
|
if e:STAGE >= s and e:DECOUPLEDIN < s {
|
2021-08-06 05:32:24 +00:00
|
|
|
set ispSum to ispSum + e:VACUUMISP.
|
2021-08-04 00:35:50 +00:00
|
|
|
set eCount to eCount + 1.
|
2021-08-02 01:45:47 +00:00
|
|
|
}
|
2021-08-04 00:35:50 +00:00
|
|
|
}
|
|
|
|
if eCount = 0 { return 0. }
|
2021-08-02 01:45:47 +00:00
|
|
|
|
2021-08-04 00:35:50 +00:00
|
|
|
return ispSum / eCount.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculates the total thrust for the given stage, in kN.
|
|
|
|
// Defaults to current stage. Assumes your ship is designed so that
|
|
|
|
// engines are discarded immediately when they flame out.
|
|
|
|
function stageThrust {
|
|
|
|
parameter s is STAGE:NUMBER.
|
|
|
|
|
|
|
|
local en is list().
|
|
|
|
list ENGINES in en.
|
|
|
|
|
|
|
|
local sum is 0.
|
|
|
|
for e in en {
|
|
|
|
if e:STAGE >= s and e:DECOUPLEDIN < s {
|
|
|
|
set sum to sum + e:POSSIBLETHRUST.
|
|
|
|
}
|
2021-08-02 01:45:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 00:35:50 +00:00
|
|
|
return sum.
|
2021-08-02 01:45:47 +00:00
|
|
|
}
|
2021-08-06 01:45:19 +00:00
|
|
|
|
|
|
|
// Determine mass at start of target stage.
|
|
|
|
// This can handle Delta V-style launchers but
|
|
|
|
// only if the central rocket remains in the stack
|
|
|
|
// for exactly two stages, one of which is the current stage.
|
|
|
|
// More complex staging with partially depleted tanks may produce
|
|
|
|
// undefined behavior.
|
|
|
|
function stageMass {
|
|
|
|
parameter s is STAGE:NUMBER.
|
|
|
|
|
|
|
|
local m is SHIP:MASS.
|
|
|
|
if s = SHIP:STAGENUM { return m. }
|
|
|
|
|
|
|
|
local ps is List().
|
|
|
|
list PARTS in ps.
|
|
|
|
for part in ps {
|
2021-08-06 05:32:24 +00:00
|
|
|
if part:DECOUPLEDIN >= s {
|
2021-08-06 01:45:19 +00:00
|
|
|
set m to m - part:MASS.
|
2021-08-06 05:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list ENGINES in ps.
|
|
|
|
for part in ps {
|
|
|
|
if part:DECOUPLEDIN < s and part:AVAILABLETHRUST > 0 {
|
2021-08-06 01:45:19 +00:00
|
|
|
set m to m - (part:MAXMASSFLOW * SHIP:StageDeltaV(SHIP:STAGENUM):DURATION).
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.
|
|
|
|
}
|
|
|
|
|
2021-08-06 05:36:26 +00:00
|
|
|
function FlameOut {
|
2021-08-06 01:45:19 +00:00
|
|
|
local ens is List().
|
|
|
|
list engines in ens.
|
|
|
|
for en in ens {
|
|
|
|
if en:FLAMEOUT {
|
|
|
|
return true.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false.
|
|
|
|
}
|