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-06 08:57:07 +00:00
|
|
|
runoncepath("/lib/sensors").
|
2021-08-08 20:23:20 +00:00
|
|
|
runoncepath("/lib/node"). // TODO: move BurnTime to a more sensible library than 'node'
|
2021-08-06 08:57:07 +00:00
|
|
|
|
|
|
|
// Convenience function for landing operations. Hover over a point with a negative velocity, shutting down on landing.
|
|
|
|
function Land {
|
|
|
|
set done to false.
|
|
|
|
|
2021-08-07 00:13:27 +00:00
|
|
|
assumeControl().
|
2021-08-06 08:57:07 +00:00
|
|
|
alignForHover().
|
2021-08-08 20:23:20 +00:00
|
|
|
|
2021-08-07 00:13:27 +00:00
|
|
|
local pid is PIDLoop(0.1, 0.01, 0.01, 0, 1).
|
|
|
|
|
2021-08-06 08:57:07 +00:00
|
|
|
lock THROTTLE to pid:Update(TIME:SECONDS, SHIP:VERTICALSPEED).
|
2021-08-07 00:13:27 +00:00
|
|
|
local targetVel is 0.
|
|
|
|
lock targetVel to Ceiling((-1 * (SHIP:ALTITUDE - SHIP:GEOPOSITION:TERRAINHEIGHT) / 10) - 5).
|
2021-08-06 08:57:07 +00:00
|
|
|
until done or SHIP:STATUS = "LANDED" {
|
2021-08-07 00:13:27 +00:00
|
|
|
set pid:SETPOINT to targetVel.
|
2021-08-06 08:57:07 +00:00
|
|
|
wait 0.001.
|
|
|
|
}
|
2021-08-07 00:13:27 +00:00
|
|
|
|
|
|
|
restoreControl().
|
2021-08-06 08:57:07 +00:00
|
|
|
set SHIP:CONTROL:PILOTMAINTHROTTLE to 0.0.
|
|
|
|
}
|
|
|
|
|
2021-08-08 20:23:20 +00:00
|
|
|
function ZeroVelocity {
|
|
|
|
assumeControl().
|
|
|
|
alignForHover().
|
|
|
|
local processVar is 0.0.
|
|
|
|
print "Aligning.".
|
|
|
|
if ReadSensor("PRES") = 0 {
|
|
|
|
lock processVar to SHIP:VELOCITY:SURFACE:MAG.
|
|
|
|
} else {
|
|
|
|
lock processVar to SHIP:VERTICALSPEED.
|
|
|
|
}
|
|
|
|
local bt is BurnTime(processVar).
|
|
|
|
print "Burning.".
|
|
|
|
lock THROTTLE to 1.0.
|
|
|
|
wait bt.
|
|
|
|
lock THROTTLE to 0.0.
|
|
|
|
restoreControl().
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
2021-08-07 00:13:27 +00:00
|
|
|
assumeControl().
|
2021-08-06 08:57:07 +00:00
|
|
|
alignForHover().
|
2021-08-07 00:13:27 +00:00
|
|
|
local throttlePID is PIDLoop(0.1, 0.1, 0.001, 0, 1).
|
|
|
|
set throttlePID:SETPOINT to vertSpeed.
|
2021-08-06 08:57:07 +00:00
|
|
|
lock THROTTLE to throttlePID:Update(TIME:SECONDS, SHIP:VERTICALSPEED).
|
|
|
|
wait until done.
|
2021-08-01 21:46:28 +00:00
|
|
|
|
2021-08-06 08:57:07 +00:00
|
|
|
// Unwind everything.
|
2021-08-07 00:13:27 +00:00
|
|
|
restoreControl().
|
2021-08-06 08:57:07 +00:00
|
|
|
}
|
2021-08-01 21:46:28 +00:00
|
|
|
|
2021-08-06 08:57:07 +00:00
|
|
|
function alignForHover {
|
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.
|
2021-08-06 08:57:07 +00:00
|
|
|
lock STEERING to SHIP:SRFRETROGRADE.
|
|
|
|
print "Aligning with retrograde.".
|
|
|
|
wait until done or VAng(SHIP:FACING:FOREVECTOR, SHIP:SRFRETROGRADE:FOREVECTOR) < 0.1.
|
|
|
|
if done {
|
|
|
|
restoreControls().
|
|
|
|
return.
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-08 01:33:51 +00:00
|
|
|
// ... otherwise just align vertically.
|
2021-08-06 08:57:07 +00:00
|
|
|
lock STEERING to SHIP:UP.
|
|
|
|
print "Aligning vertical.".
|
|
|
|
wait until done or VAng(SHIP:FACING:FOREVECTOR, SHIP:UP:FOREVECTOR) < 1.
|
|
|
|
if done {
|
|
|
|
restoreControls().
|
|
|
|
return.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 21:46:28 +00:00
|
|
|
|
2021-08-07 00:13:27 +00:00
|
|
|
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.
|
|
|
|
}
|