diff --git a/boot/rocket.ks b/boot/rocket.ks index 243a7fa..dc67b70 100644 --- a/boot/rocket.ks +++ b/boot/rocket.ks @@ -7,6 +7,7 @@ deletepath("/boot/rocket"). // Install software. compile "0:/lib/navigation" to "1:/lib/navigation". compile "0:/lib/throttle" to "1:/lib/throttle". +compile "0:/lib/stabilize_rocket" to "1:/lib/stabilize_rocket". compile "0:/rocket/init" to "1:/init". compile "0:/rocket/launch" to "1:/launch". copypath("0:/rocket/execnode", "1:/execnode"). diff --git a/boot/rocket_debug.ks b/boot/rocket_debug.ks new file mode 100644 index 0000000..76dfafa --- /dev/null +++ b/boot/rocket_debug.ks @@ -0,0 +1,9 @@ +// Install software. +copypath("0:/lib/navigation", "1:/lib/navigation"). +copypath("0:/lib/throttle", "1:/lib/throttle"). +copypath("0:/lib/stabilize_rocket", "1:/lib/stabilize_rocket"). +copypath("0:/rocket/init", "1:/init"). +copypath("0:/rocket/launch", "1:/launch"). +copypath("0:/rocket/execnode", "1:/execnode"). + +run "init". diff --git a/compileall.ks b/compileall.ks index 9197130..59dd378 100644 --- a/compileall.ks +++ b/compileall.ks @@ -1,10 +1,29 @@ // Intended to be run from volume 0 for checking compiled sizes. -for file in FILES { - if file:ISFILE and file:EXTENSION = "ks" { - compile file:NAME. +switch to 0. +compileHere(). + +function compileDir { + parameter dir. + + cd("dir"). + compileHere(). + cd(".."). +} + +function compileHere { + local fileList is list(). + list files in fileList. + + for f in fileList { + if f:ISFILE { + compile f. + } else { + compileDir(f). + } } +} + // TODO: might be neat to actually do a filesize comparison and print a report or something, // specifically iterating files NOT to compile. // TODO: We could hypothetically even integrate this with the bootstrapper. -} diff --git a/debug/vecrender.ks b/debug/vecrender.ks index 122ec87..f3e31a0 100644 --- a/debug/vecrender.ks +++ b/debug/vecrender.ks @@ -73,8 +73,12 @@ function RenderDirAxes { } if vec <> V(0,0,0) { - // RenderVectorShipRelative(vec). - RenderDirAxes(vec). + if vec:istype("vector") { + RenderVectorWithComponents(vec). + } + if vec:istype("direction") { + RenderDirAxes(vec). + } } function ClearRenders { diff --git a/helicopter/hover.ks b/helicopter/hover.ks index 7009d7f..93946f1 100644 --- a/helicopter/hover.ks +++ b/helicopter/hover.ks @@ -1,4 +1,4 @@ // This script assumes you have bound collective (aka deploy angle) to main throttle. runoncepath("lib/control"). -Stabilize(). +PointStabilizeH(). diff --git a/helicopter/init.ks b/helicopter/init.ks index 1f4e948..6723694 100644 --- a/helicopter/init.ks +++ b/helicopter/init.ks @@ -17,7 +17,7 @@ local y is interface:AddVLayout(). local x is y:AddHLayout(). set x:AddButton("HOVER"):onClick to { - Stabilize(). + PointStabilizeH(). }. set x:AddButton("LAND"):onClick to { @@ -29,7 +29,7 @@ set x:AddButton("LAND"):onClick to { toggle AG10. BRAKES on. } - Stabilize(-5). + PointStabilizeH(-5). }. diff --git a/lib/control.ks b/lib/control.ks index 79ee629..e6a31a7 100644 --- a/lib/control.ks +++ b/lib/control.ks @@ -4,10 +4,11 @@ // set externally when they should return. +// Helicopter-style point stability control. // Hover, ascend, or descend at a fixed rate. // Adjusts the throttle to control ascent or descent. // Adjusts pitch and roll to maintain zero lateral velocity. -function Stabilize { +function PointStabilizeH { parameter vertSpeed is 0.0. set done to false. diff --git a/lib/stabilize_rocket.ks b/lib/stabilize_rocket.ks new file mode 100644 index 0000000..13833c0 --- /dev/null +++ b/lib/stabilize_rocket.ks @@ -0,0 +1,98 @@ +// Provides stabilization functions, including Hover. + + +// Stabilizes a rocket relative to the surface, with optional vertical +// velocity. Do not include both this and helicopter_stabilize.ks in one +// program. +function Hover { + parameter vertSpeed is 0.0. + + set done to false. + SAS off. + + lock THROTTLE to 0.0. + + if SHIP:SENSORS:PRES = 0 { + // In a vacuum, perform a more efficient vacuum maneuver. + // This will call hoverAtmo after initial stabilization, to make sure + // we don't flip upside down. + hoverVac(vertSpeed). + } else { + hoverAtmo(vertSpeed). + } + + set SHIP:CONTROL:PILOTMAINTHROTTLE to THROTTLE. + unlock THROTTLE. + unlock STEERING. + SAS on. + print "Stabilized operation ended. Returning control to pilot.". + set done to false. +} + +function hoverAtmo { + parameter vertSpeed. + + local headingV is V(0,0,0). + lock headingV to VectorExcl(SHIP:UP:FOREVECTOR, SHIP:SRFRETROGRADE). + + // set up PID controllers + local pitchPID is PIDLoop(5, 0.1, 0.01, 0, 90). + set pitchPID:SETPOINT to 0. + + local throttlePID is PIDLoop(0.1, 0.1, 0.001, 0, 1). + set throttlePID:SETPOINT to vertSpeed. + + // we use the inverse of GROUNDSPEED because pitch and acceleration are + // inversely proportional here. + local newPitch is 0.0. + lock newPitch to pitchPID:Update(TIME:SECONDS, -SHIP:GROUNDSPEED). + lock STEERING to AngleAxis(-newPitch, SHIP:FACING:STARVECTOR) * headingV. + lock THROTTLE to throttlePID:Update(TIME:SECONDS, SHIP:VERTICALSPEED). + wait until done. +} + +function hoverVac { + parameter vertSpeed. + // TODO: implement me! + hoverAtmo(vertSpeed). +} + +// 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(-0.5). + set SHIP:CONTROL:PILOTMAINTHROTTLE to 0.0. + set done to false. +} + +// Rocket-style point stability control. +// Hover, ascend, or descend at a fixed rate. +// Adjusts the throttle to control target vertical speed. +// Uses retrograde lock to cancel horizontal velocity. +// TODO: how does this work +// function PointStabilizeR { +// parameter vertSpeed is 0.0. + +// set done to false. +// SAS off. + +// lock STEERING to SHIP:RETROGRADE. +// wait until VectorAngle( +// SHIP:FACING:FOREVECTOR, +// SHIP:SRFRETROGRADE:FOREVECTOR) < 0.05. + +// local throttlePID is PIDLoop(0.1, 0.001, 0.001, 0, 1). +// set throttlePID:SETPOINT to vertSpeed. +// lock THROTTLE to throttlePID:Update(TIME:SECONDS, SHIP:VERTICALSPEED). +// wait until done. + +// set SHIP:CONTROL:PILOTMAINTHROTTLE to THROTTLE. +// unlock THROTTLE. +// unlock STEERING. +// SAS on. +// set done to false. +// print "Stabilized operation ended. Returning control to pilot.". +// } diff --git a/lib/throttle.ks b/lib/throttle.ks index b9e63a4..4ad9c26 100644 --- a/lib/throttle.ks +++ b/lib/throttle.ks @@ -1,4 +1,5 @@ -// Functions for calculating thrust values. +// Functions for calculating values related to throttle and thrust. + @lazyglobal off. // point gravity for TWR calculations. diff --git a/reinstall.ks b/reinstall.ks index 46d7ebf..6ffa89e 100644 --- a/reinstall.ks +++ b/reinstall.ks @@ -1,6 +1,6 @@ parameter BOOT_FILE is "rocket". clearguis(). -copypath("0:/boot/"+BOOT_FILE+".ks", "1:/boot"). +copypath("0:/boot/"+BOOT_FILE+".ks", "1:/boot/"+BOOT_FILE+".ks"). switch to 1. -runpath("/boot/"+BOOT_FILE). +runpath("/boot/"+BOOT_FILE+".ks"). diff --git a/rocket/init.ks b/rocket/init.ks index 4b906c5..ea25d6c 100644 --- a/rocket/init.ks +++ b/rocket/init.ks @@ -1,14 +1,14 @@ runoncepath("/lib/navigation"). runoncepath("/lib/throttle"). +runoncepath("/lib/stabilize_rocket"). + +global done is false. +on AG9 { + set done to true. + return true. +} function launchButtonPressed { - // adjust torque settings here... - local oldEpsilon is STEERINGMANAGER:TORQUEEPSILONMAX. - set STEERINGMANAGER:TORQUEEPSILONMAX to maxEpsilon:TEXT:ToNumber(). - - local oldStopTime is STEERINGMANAGER:MAXSTOPPINGTIME. - set STEERINGMANAGER:MAXSTOPPINGTIME to maxStopTime:TEXT:ToNumber(). - run "/launch"( targetApo:TEXT:ToNumber(), gravTurnStart:TEXT:ToNumber(), @@ -16,9 +16,6 @@ function launchButtonPressed { initialPitch:TEXT:ToNumber(), minimumPitch:TEXT:ToNumber() ). - - set STEERINGMANAGER:TORQUEEPSILONMAX to oldEpsilon. - set STEERINGMANAGER:MAXSTOPPINGTIME to oldStopTime.. } // Main UI. @@ -48,17 +45,28 @@ set x:AddButton("CONF"):onClick to { set x to y:AddHLayout(). +set x:AddButton("HOVER"):onClick to { + Hover(). +}. + +set x:AddButton("LAND"):onClick to { + Land(). +}. + +set x to y:AddHLayout(). + set x:AddButton("LNCH"):onClick to { stk:ShowOnly(launchmenu). }. set x:AddButton("NODE"):onClick to { - run "/execnode". + set nodeDV:TEXT to NEXTNODE:DELTAV:MAG:ToString. + set nodeBT:TEXT to BurnTime(NEXTNODE:DELTAV:MAG):ToString. + stk:ShowOnly(nodeMenu). }. set x:AddButton("TWR"):onClick to { stk:ShowOnly(twrMenu). }. - local stk is interface:AddStack(). @@ -130,13 +138,33 @@ set twrMenu:AddButton("Lock TWR"):onClick to { set done to true. } + print "Locking throttle to target TWR.". local tgt is twrLock:TEXT:ToNumber(). lock THROTTLE to ThrottleToTWR(tgt). wait until done. + print "Throttle unlocked.". }. // end twr menu +// node menu +local nodeMenu is stk:AddVLayout(). +local nodeBox is nodeMenu:AddScrollBox(). + +set sbox to nodeBox:AddHLayout(). +sbox:AddLabel("Node dV"). +local nodeDV is sbox:AddLabel(). + +set sbox to nodeBox:AddHLayout(). +sbox:AddLabel("Node Burn Time"). +local nodeBT is sbox:AddLabel(). + +set nodeMenu:AddButton("Execute"):onClick to { + run "/execnode". +}. + +// end node menu + interface:show(). wait until false.