2021-07-19 07:43:07 +00:00
|
|
|
// Functions for calculating thrust values.
|
2021-07-18 19:05:26 +00:00
|
|
|
@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 {
|
2021-07-18 20:06:38 +00:00
|
|
|
parameter targetTWR is 1.5.
|
2021-07-18 19:05:26 +00:00
|
|
|
|
|
|
|
local m is SHIP:MASS.
|
|
|
|
local g is 9.81.
|
|
|
|
if HAS_GRAV_SENSOR = true {
|
|
|
|
set g to SHIP:SENSORS:GRAV:MAG.
|
|
|
|
}
|
2021-07-18 20:06:38 +00:00
|
|
|
return min((targetTWR*m*g)/SHIP:AVAILABLETHRUST, 1.0).
|
2021-07-18 19:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
}
|
|
|
|
|
2021-07-19 07:43:07 +00:00
|
|
|
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.
|
2021-07-18 19:05:26 +00:00
|
|
|
}
|