Fix up hover script and improve bootloading.
This commit is contained in:
parent
75a7dfec22
commit
2099d0a284
|
@ -11,7 +11,8 @@ local compiled is list(
|
||||||
).
|
).
|
||||||
|
|
||||||
local copied is list(
|
local copied is list(
|
||||||
"/lib/ui"
|
"/lib/ui",
|
||||||
|
"/lib/sensors"
|
||||||
).
|
).
|
||||||
|
|
||||||
Bootstrap(
|
Bootstrap(
|
||||||
|
|
|
@ -6,7 +6,7 @@ local compiled is list(
|
||||||
"/lib/navigation",
|
"/lib/navigation",
|
||||||
"/lib/node",
|
"/lib/node",
|
||||||
"/lib/throttle",
|
"/lib/throttle",
|
||||||
"/prog/nodestats",
|
"/prog/nodestats"
|
||||||
).
|
).
|
||||||
|
|
||||||
local copied is list(
|
local copied is list(
|
||||||
|
|
14
lib/boot.ks
14
lib/boot.ks
|
@ -35,7 +35,14 @@ function compileLibs {
|
||||||
if lib:StartsWith("/prog") {
|
if lib:StartsWith("/prog") {
|
||||||
set tgt to lib:Remove(0, 5).
|
set tgt to lib:Remove(0, 5).
|
||||||
}
|
}
|
||||||
compile "0:" + lib to "1:" + tgt.
|
local res is true.
|
||||||
|
compile "0:" + lib.
|
||||||
|
set res to copypath("0:" + lib + ".ksm", "1:" + tgt).
|
||||||
|
deletepath("0:" + lib + ".ksm").
|
||||||
|
if not res {
|
||||||
|
print "Can't copy compiled file '" + lib + "', aborting.".
|
||||||
|
print ERR. // intentional error to trap execution
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +53,10 @@ function copyLibs {
|
||||||
if lib:StartsWith("/prog") {
|
if lib:StartsWith("/prog") {
|
||||||
set tgt to lib:Remove(0, 5).
|
set tgt to lib:Remove(0, 5).
|
||||||
}
|
}
|
||||||
copypath("0:" + lib, "1:" + tgt).
|
if not copypath("0:" + lib, "1:" + tgt) {
|
||||||
|
print "Can't copy file '" + lib + "', aborting.".
|
||||||
|
print ERR. // intentional error to trap execution
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
26
lib/sensors.ks
Normal file
26
lib/sensors.ks
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
|
||||||
|
// we rebuild the sensor list with every invocation, because things change
|
||||||
|
function buildSensorList {
|
||||||
|
local sList is Lexicon().
|
||||||
|
list SENSORS in SensorList.
|
||||||
|
for s in SensorList {
|
||||||
|
sList:add(s:type, s).
|
||||||
|
}
|
||||||
|
return sList.
|
||||||
|
}
|
||||||
|
|
||||||
|
function HasSensor {
|
||||||
|
parameter s.
|
||||||
|
local sList is buildSensorList().
|
||||||
|
return sList:HasKey(s).
|
||||||
|
}
|
||||||
|
|
||||||
|
function ReadSensor {
|
||||||
|
parameter s.
|
||||||
|
|
||||||
|
local sList is buildSensorList().
|
||||||
|
if not sList:HasKey(s) {
|
||||||
|
return -1. // TODO: is -1 an impossible result for all sensors? I suspect not...
|
||||||
|
}
|
||||||
|
return sList[s].
|
||||||
|
}
|
|
@ -5,6 +5,24 @@
|
||||||
//
|
//
|
||||||
// Do not include both this and stabilize_helicopter.ks in one program.
|
// Do not include both this and stabilize_helicopter.ks in one program.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
alignForHover().
|
||||||
|
local pid is hoverPID().
|
||||||
|
lock THROTTLE to pid:Update(TIME:SECONDS, SHIP:VERTICALSPEED).
|
||||||
|
until done or SHIP:STATUS = "LANDED" {
|
||||||
|
set pid:SETPOINT to (-1 * (SHIP:ALTITUDE - SHIP:GEOPOSITION:TERRAINHEIGHT) / 10) - 5.
|
||||||
|
wait 0.001.
|
||||||
|
}
|
||||||
|
|
||||||
|
set SHIP:CONTROL:PILOTMAINTHROTTLE to 0.0.
|
||||||
|
set done to false.
|
||||||
|
}
|
||||||
|
|
||||||
// Stabilizes a rocket relative to the surface, with optional vertical
|
// Stabilizes a rocket relative to the surface, with optional vertical
|
||||||
// velocity.
|
// velocity.
|
||||||
function Hover {
|
function Hover {
|
||||||
|
@ -12,33 +30,47 @@ function Hover {
|
||||||
|
|
||||||
set done to false.
|
set done to false.
|
||||||
SAS off.
|
SAS off.
|
||||||
|
|
||||||
lock THROTTLE to 0.0.
|
lock THROTTLE to 0.0.
|
||||||
|
|
||||||
if SHIP:SENSORS:PRES = 0 {
|
alignForHover().
|
||||||
lock STEERING to SHIP:SRFRETROGRADE.
|
local throttlePID is hoverPID(vertSpeed).
|
||||||
wait until VAng(SHIP:FACING:FOREVECTOR, SHIP:SRFRETROGRADE:FOREVECTOR) < 10.
|
lock THROTTLE to throttlePID:Update(TIME:SECONDS, SHIP:VERTICALSPEED).
|
||||||
} else {
|
wait until done.
|
||||||
// debug
|
|
||||||
print "Making vector visualization.".
|
|
||||||
local d1 is VecDraw(V(0,0,0), V(0,0,0), BLUE, "", 1.0, true).
|
|
||||||
set d1:VECUPDATER to {
|
|
||||||
return atmoSafeRetrograde() * 100.
|
|
||||||
}.
|
|
||||||
print "Vector visualization created.".
|
|
||||||
// end debug
|
|
||||||
|
|
||||||
lock STEERING to atmoSafeRetrograde().
|
|
||||||
wait until done or VAng(SHIP:FACING:FOREVECTOR, atmoSafeRetrograde()) < 10.
|
|
||||||
}
|
|
||||||
|
|
||||||
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:VELOCITY:SURFACE:MAG). // this is still wrong, we just burn hard and spin out of control.
|
|
||||||
|
|
||||||
|
|
||||||
// Unwind everything.
|
// Unwind everything.
|
||||||
|
restoreControls().
|
||||||
|
}
|
||||||
|
|
||||||
|
function hoverPID {
|
||||||
|
parameter initSetpoint is 0.
|
||||||
|
local pid is PIDLoop(0.01, 0.01, 0.001, 0, 1).
|
||||||
|
set pid:SETPOINT to initSetpoint.
|
||||||
|
return pid.
|
||||||
|
}
|
||||||
|
|
||||||
|
function alignForHover {
|
||||||
|
if ReadSensor("pres") = 0 {
|
||||||
|
// if we're in a vacuum, cancel all velocity first...
|
||||||
|
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 {
|
||||||
|
// ... otherwise just cancel vertical velocity
|
||||||
|
lock STEERING to SHIP:UP.
|
||||||
|
print "Aligning vertical.".
|
||||||
|
wait until done or VAng(SHIP:FACING:FOREVECTOR, SHIP:UP:FOREVECTOR) < 1.
|
||||||
|
if done {
|
||||||
|
restoreControls().
|
||||||
|
return.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function restoreControls {
|
||||||
set SHIP:CONTROL:PILOTMAINTHROTTLE to THROTTLE.
|
set SHIP:CONTROL:PILOTMAINTHROTTLE to THROTTLE.
|
||||||
unlock THROTTLE.
|
unlock THROTTLE.
|
||||||
unlock STEERING.
|
unlock STEERING.
|
||||||
|
@ -46,22 +78,3 @@ function Hover {
|
||||||
print "Stabilized operation ended. Returning control to pilot.".
|
print "Stabilized operation ended. Returning control to pilot.".
|
||||||
set done to false.
|
set done to false.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convenience function for landing operations. Hover over a point with a negative velocity, shutting down on landing.
|
|
||||||
function Land {
|
|
||||||
set done to false.
|
|
||||||
when SHIP:STATUS = "LANDED" then {
|
|
||||||
set done to true.
|
|
||||||
}
|
|
||||||
Hover(-4).
|
|
||||||
set SHIP:CONTROL:PILOTMAINTHROTTLE to 0.0.
|
|
||||||
set done to false.
|
|
||||||
}
|
|
||||||
|
|
||||||
function atmoSafeRetrograde {
|
|
||||||
if VAng(SHIP:SRFRETROGRADE:FOREVECTOR, SHIP:UP:FOREVECTOR) < 45 {
|
|
||||||
return SHIP:SRFRETROGRADE:FOREVECTOR.
|
|
||||||
}
|
|
||||||
local planeRetro is VXCL(SHIP:UP:FOREVECTOR, SHIP:SRFRETROGRADE:FOREVECTOR).
|
|
||||||
return AngleAxis(45, SHIP:FACING:STARVECTOR) * planeRetro.
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,15 +1,9 @@
|
||||||
// Check for various sensors and set appropriate global constants.
|
// This alternative approach to hovering is ultimately not worth the complexity to use, but it's really neat! It maintains *whatever your current vertical velocity is* using the fact that F = μ*m/(r^2).
|
||||||
// Currently only checks for grav sensor, as that's the only one used by this library.
|
// In practice, by the time the PID controller settles enough to make this worth
|
||||||
// global HAS_GRAV_SENSOR is false.
|
// using, the efficiency gains have mostly dried up anyway.
|
||||||
// function SensorCheck {
|
|
||||||
// local SensorList is 0.
|
// lock STEERING to SHIP:UP.
|
||||||
// list SENSORS in SensorList.
|
// wait until done or abs(SHIP:VERTICALSPEED - vertSpeed) < 0.05.
|
||||||
// for s in SensorList {
|
// print "Maintaining velocity.".
|
||||||
// if s:type = "grav" {
|
// lock targetForce to (SHIP:BODY:MU * SHIP:MASS) / ((SHIP:ALTITUDE + SHIP:BODY:RADIUS)^2).
|
||||||
// print "Gravometric sensor detected".
|
// lock THROTTLE to targetForce / SHIP:AVAILABLETHRUST.
|
||||||
// set HAS_GRAV_SENSOR to true.
|
|
||||||
// return.
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// set HAS_GRAV_SENSOR to false.
|
|
||||||
// }
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user