kOS/lib/stabilize_rocket.ks

97 lines
2.4 KiB
Plaintext
Raw Permalink Normal View History

2021-08-01 05:23:39 +00:00
// Provides stabilization functions, including Hover.
2021-08-01 21:53:09 +00:00
//
// Must define a global variable 'done', which can be set to true to cancel
// control functions.
//
// Do not include both this and stabilize_helicopter.ks in one program.
2021-08-01 05:23:39 +00:00
2021-08-19 02:32:07 +00:00
RunOncePath("/lib/sensors").
// Convenience function for landing operations. Hover over a point with a negative velocity, shutting down on landing.
function Land {
set done to false.
assumeControl().
alignForHover().
if done {
restoreControl().
return.
}
local pid is PIDLoop(0.1, 0.01, 0.01, 0, 1).
lock THROTTLE to pid:Update(TIME:SECONDS, SHIP:VERTICALSPEED).
local targetVel is 0.
lock targetVel to Ceiling((-1 * (SHIP:ALTITUDE - SHIP:GEOPOSITION:TERRAINHEIGHT) / 10) - 5).
until done or SHIP:STATUS = "LANDED" {
set pid:SETPOINT to targetVel.
wait 0.001.
}
restoreControl().
set SHIP:CONTROL:PILOTMAINTHROTTLE to 0.0.
}
2021-08-01 05:23:39 +00:00
// Stabilizes a rocket relative to the surface, with optional vertical
2021-08-01 21:53:09 +00:00
// velocity.
2021-08-01 05:23:39 +00:00
function Hover {
parameter vertSpeed is 0.0.
assumeControl().
alignForHover().
if done {
restoreControl().
return.
}
local throttlePID is PIDLoop(0.1, 0.1, 0.001, 0, 1).
set throttlePID:SETPOINT to vertSpeed.
lock THROTTLE to throttlePID:Update(TIME:SECONDS, SHIP:VERTICALSPEED).
wait until done.
// Unwind everything.
restoreControl().
}
2021-08-30 01:45:50 +00:00
function hoverDirVac {
parameter top is SHIP:FACING:TOPVECTOR.
if SHIP:VERTICALSPEED > 0 {
return SHIP:UP.
}
return LookDirUp(SHIP:SRFRETROGRADE:FOREVECTOR, top).
}
function alignForHover {
2021-08-30 01:45:50 +00:00
set top to SHIP:FACING:TOPVECTOR.
2021-08-08 01:33:51 +00:00
if ReadSensor("PRES") = 0 {
// if we're in a vacuum, align with retrograde for smoother horizontal control.
lock STEERING to hoverDirVac(top).
print "Aligning with retrograde.".
2021-08-30 01:45:50 +00:00
wait until done or VAng(SHIP:FACING:FOREVECTOR, hoverDirVac(top):FOREVECTOR) < 1.
if done {
return.
}
} else {
2021-08-08 01:33:51 +00:00
// ... otherwise just align vertically.
lock STEERING to LookDirUp(SHIP:UP:FOREVECTOR, top).
print "Aligning vertical.".
wait until done or VAng(SHIP:FACING:FOREVECTOR, SHIP:UP:FOREVECTOR) < 1.
if done {
return.
}
}
}
function assumeControl {
set done to false.
SAS off.
lock THROTTLE to 0.0.
}
function restoreControl {
2021-08-01 05:23:39 +00:00
set SHIP:CONTROL:PILOTMAINTHROTTLE to THROTTLE.
unlock THROTTLE.
unlock STEERING.
SAS on.
2021-08-08 01:33:51 +00:00
print "Done.".
2021-08-01 05:23:39 +00:00
set done to false.
}