kOS/lib/throttle.ks

58 lines
1.6 KiB
Plaintext

// Functions for calculating thrust values.
@lazyglobal off.
// Returns the throttle value you should use to achieve the
// target TWR. If TWR can't be achieved, returns 1.0. (full throttle)
function ThrottleToTWR {
parameter targetTWR is 1.5.
local m is SHIP:MASS.
local g is 9.81.
if HAS_GRAV_SENSOR = true {
set g to SHIP:SENSORS:GRAV:MAG.
}
return min((targetTWR*m*g)/SHIP:AVAILABLETHRUST, 1.0).
}
// Calculates the ship's current TWR.
function TWR {
local m is ship:mass.
local g is 9.81. // fallback to this approximation that doesn't deal with
// altitude change.
if HAS_GRAV_SENSOR = true {
set g to SHIP:SENSORS:GRAV:MAG.
}
local t is THROTTLE * SHIP:AVAILABLETHRUST.
return t/(m*g).
}
// Check for various sensors and set appropriate global constants.
// Currently only checks for grav sensor, as that's the only one used by this library.
global HAS_GRAV_SENSOR is false.
function SensorCheck {
local SensorList is 0.
list SENSORS in SensorList.
for s in SensorList {
if s:type = "grav" {
print "Gravometric sensor detected".
set HAS_GRAV_SENSOR to true.
return.
}
}
set HAS_GRAV_SENSOR to false.
}
function BurnTime {
parameter dV.
list ENGINES in en.
local f is en[0]:MAXTHRUST * 1000. // Engine Thrust (kg * m/s²)
local m is SHIP:MASS * 1000. // Starting mass (kg)
local e is CONSTANT():E. // Base of natural log
local p is en[0]:ISP. // Engine ISP (s)
local g is 9.80665. // Gravitational acceleration constant (m/s²)
return g * m * p * (1 - e^(-dV/(g*p))) / f.
}