From 6fe0ecf9886e148764607f6d2e4fe5a2b5857eed Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Mon, 24 Nov 2025 23:41:49 -0500 Subject: [PATCH] finally working landing script --- boot/radenBoot.ks | 8 ++-- defaults.ks | 1 + library/lib_vessel_utils.ks | 66 +++++++++++++++++++++++------ poweredLanding.ks | 84 +++++++++++++++++++++++-------------- radenLaunch.ks | 10 +++-- 5 files changed, 120 insertions(+), 49 deletions(-) create mode 100644 defaults.ks diff --git a/boot/radenBoot.ks b/boot/radenBoot.ks index fabd3d0..86ed23a 100644 --- a/boot/radenBoot.ks +++ b/boot/radenBoot.ks @@ -1,4 +1,6 @@ wait until ship:unpacked. -clearscreen. -switch to 0. -run radenLaunch. \ No newline at end of file +if ship:status = "LANDED" or ship:status = "PRELAUNCH" { + clearscreen. + switch to 0. + run radenLaunch. +} \ No newline at end of file diff --git a/defaults.ks b/defaults.ks new file mode 100644 index 0000000..6bc7bfc --- /dev/null +++ b/defaults.ks @@ -0,0 +1 @@ +set terminal:charheight to 20. \ No newline at end of file diff --git a/library/lib_vessel_utils.ks b/library/lib_vessel_utils.ks index ee0a608..eb55d52 100644 --- a/library/lib_vessel_utils.ks +++ b/library/lib_vessel_utils.ks @@ -4,6 +4,17 @@ declare function WaitForEngineStart{ wait until AnyEngineActive(). } +declare function GetDrag{ + parameter localGravity. + return ship:sensors:acc:mag + localGravity. +} + +declare function GetDragDir{ + parameter localGravity. + parameter dir. + return vDot(ship:sensors:acc, dir) + localGravity. +} + declare function AnyEngineActive{ list engines in allEngines. for eng in allEngines{ @@ -41,15 +52,7 @@ declare function GetMaxMassFlow{ return sum. } -declare function CalculateSuicideBurnAltitude -{ - parameter vertSpeed. - parameter localGravity. - parameter tgtAltitude is 0. - local burnAltitude is ((vertSpeed^2) / (2 * CalculateAverageAcceleration(localGravity))). - return burnAltitude + tgtAltitude. -} declare function CalculateSuicideBurnDuration @@ -79,9 +82,48 @@ declare function CalculateBurnDuration } -declare function CalculateAverageAcceleration{ +declare function CalculateSuicideBurnAltitude +{ + parameter vertSpeed. parameter localGravity. - return ((ship:maxthrust * 1000) / ship:mass) - localGravity. + parameter tgtAltitude is 0.0. + parameter drag is 0.0. + + local vertAcc is CalculateAverageDecceleration(localGravity). + local burnAltitude is ((vertSpeed^2) / (2 * (vertAcc))). + return burnAltitude + tgtAltitude. +} + +declare function CalculateSuicideBurnAltitudeV2 +{ + + parameter vertSpeed. + parameter localGravity. + parameter tgtAltitude is 0.0. + + + local vs is vertSpeed * -1. + local a is CalculateAverageDecceleration(localGravity). + local dt is 0.05. + local result is 0.0. + local h is alt:radar. + + until vs <= 0 { + set vs to vs + a * dt. + set result to result + vs * dt. + + if result > h { return result. } + } + + return result. +} + + +declare function CalculateAverageDecceleration{ + parameter localGravity. + local maxVertAcc is (ship:availablethrust / ship:mass) - localGravity. + + return maxVertAcc + localGravity. } declare function GetLocalGravity{ @@ -97,11 +139,11 @@ declare function CalculateTimeToImpact{ parameter vertSpeed. parameter curAltitude. parameter gravity. - parameter tgtAltitude is 0. + parameter tgtAltitude is 0.0. if gravity <= 0 or curAltitude <= 0{ print "[warn]: invalid altitude or gravity.". - return 0. + return 0.0. } local vs is abs(vertSpeed). diff --git a/poweredLanding.ks b/poweredLanding.ks index fe09946..5e61176 100644 --- a/poweredLanding.ks +++ b/poweredLanding.ks @@ -1,20 +1,28 @@ run "library/lib_vessel_utils". +run "library/lib_math". run "library/lib_location_constants". -run tests. +run "defaults". CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal"). + function TestSetup{ + parameter tgt is 5000. + if altitude > 100 { + return. + } print "Setting up test". lock throttle to 100. - wait until apoapsis > 5000. + lock steering to heading(90, 89). + gear off. + wait until apoapsis > tgt. lock steering to up. lock throttle to 0. unlock throttle. + unlock steering. } - -function LandAtPad{ +function Landing{ print "Starting Landing". gear on. set pad to location_constants:kerbin:launchpad:position. @@ -23,14 +31,25 @@ function LandAtPad{ rcs on. set landingThrottle to 0. + set steerPitch to 0.0. + set steerYaw to 0.0. lock throttle to landingThrottle. - set hoverPid to PIDLoop(5, 0.1, 30, 0, 1). - set tgtAlt to 100. - set hoverPid:setpoint to tgtAlt. + set tgtAlt to 80. + + + set pitchPid to PIDLoop(0.55, 0.06, 0.3, -1, 1). + set yawPid to PIDLoop(0.55, 0.06, 0.3, -1, 1). + set hoverPid to PIDLoop(0.1, 0.06, 0.01, 0, 1). + + set hoverPid:setpoint to -1. + set pitchPid:setpoint to 0. + set yawPid:setpoint to 0. set landed to false. - when abs(verticalSpeed) < 10 or verticalSpeed > 1 then{ + + + when verticalSpeed > -10 then{ lock steering to up. preserve. } @@ -40,54 +59,57 @@ function LandAtPad{ preserve. } - until landed { - set landingThrottle to hoverPid:update(time:seconds, alt:radar). - // print "offset " + dist. + until landed { + set landingThrottle to hoverPid:update(time:seconds, verticalSpeed). + set hoverPid:setpoint to Map(alt:radar, 0, 100, -0.5, -30). wait 0.001. } + print "Landed". + lock throttle to 0. } -function Descent{ +function WaitFalling{ + parameter targetSpeed is 100. print "Coasting to apoapsis.". rcs on. lock steering to up. set warp to 1. - wait until verticalSpeed < -100. + wait until verticalSpeed < -targetSpeed. set warp to 0. print "Falling...". lock steering to srfRetrograde. brakes on. +} + +function Descent { + + local lock curAlt to alt:radar. local lock vertSpeed to verticalSpeed. - local lock isp to GetIsp(). local lock localGravity to GetLocalGravity(altitude). - local lock burnDuration to CalculateSuicideBurnDuration(isp). - local lock imactTime to CalculateTimeToImpact(abs(vertSpeed), alt:radar, localGravity). - local lock burnAlt to CalculateSuicideBurnAltitude(vertSpeed, localGravity). - local lock timeToBurn to imactTime - burnDuration. - - until timeToBurn < 0.5 { - // print "time: " + round(timeToBurn) + " len: " + burnDuration + " impact: " + imactTime + " alt: " + burnAlt. - } + local lock drag to GetDragDir(localGravity, ship:up:vector). + local lock burnDist to CalculateSuicideBurnAltitude(vertSpeed, localGravity, 80, drag). - wait until timeToBurn < 0.5. + wait until burnDist >= curAlt. + print "Starting Burn". + print burnDist. + gear on. - - until abs(verticalSpeed) < 50{ + until abs(verticalSpeed) < 30{ lock throttle to 1. } - - - SET V0 TO GETVOICE(0). - V0:PLAY( NOTE(400, 0.5) ). lock throttle to 0. + print "Final". + print curAlt. + } print "Launch to start.". WaitForEngineStart(). -TestSetup(). +TestSetup(5000). +WaitFalling(100). Descent(). -LandAtPad(). +Landing(). diff --git a/radenLaunch.ks b/radenLaunch.ks index da10d48..2b1d264 100644 --- a/radenLaunch.ks +++ b/radenLaunch.ks @@ -58,14 +58,18 @@ print "Deploying Solar". panels on. //deploy solar print "Opening Docking Port". ag3 on. //open docking port -lock steering to heading(90, 0, 0). +lock steering to prograde. set warp to 0. set warpmode to "rails". set warp to 1. -lock distToApo to apoapsis - altitude. -wait until distToApo < 500. +lock distToApo to abs(apoapsis - altitude). +wait until distToApo < 1000. +set warp to 0. +lock steering to heading(90, 0, 0). +wait until distToApo < 800. + print "Phase: Circularize". lock steering to heading(90, 0, 0).