finally working landing script

This commit is contained in:
2025-11-24 23:41:49 -05:00
parent b90b0157e4
commit 6fe0ecf988
5 changed files with 120 additions and 49 deletions

View File

@@ -1,4 +1,6 @@
wait until ship:unpacked. wait until ship:unpacked.
clearscreen. if ship:status = "LANDED" or ship:status = "PRELAUNCH" {
switch to 0. clearscreen.
run radenLaunch. switch to 0.
run radenLaunch.
}

1
defaults.ks Normal file
View File

@@ -0,0 +1 @@
set terminal:charheight to 20.

View File

@@ -4,6 +4,17 @@ declare function WaitForEngineStart{
wait until AnyEngineActive(). 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{ declare function AnyEngineActive{
list engines in allEngines. list engines in allEngines.
for eng in allEngines{ for eng in allEngines{
@@ -41,15 +52,7 @@ declare function GetMaxMassFlow{
return sum. 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 declare function CalculateSuicideBurnDuration
@@ -79,9 +82,48 @@ declare function CalculateBurnDuration
} }
declare function CalculateAverageAcceleration{ declare function CalculateSuicideBurnAltitude
{
parameter vertSpeed.
parameter localGravity. 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{ declare function GetLocalGravity{
@@ -97,11 +139,11 @@ declare function CalculateTimeToImpact{
parameter vertSpeed. parameter vertSpeed.
parameter curAltitude. parameter curAltitude.
parameter gravity. parameter gravity.
parameter tgtAltitude is 0. parameter tgtAltitude is 0.0.
if gravity <= 0 or curAltitude <= 0{ if gravity <= 0 or curAltitude <= 0{
print "[warn]: invalid altitude or gravity.". print "[warn]: invalid altitude or gravity.".
return 0. return 0.0.
} }
local vs is abs(vertSpeed). local vs is abs(vertSpeed).

View File

@@ -1,20 +1,28 @@
run "library/lib_vessel_utils". run "library/lib_vessel_utils".
run "library/lib_math".
run "library/lib_location_constants". run "library/lib_location_constants".
run tests. run "defaults".
CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal"). CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal").
function TestSetup{ function TestSetup{
parameter tgt is 5000.
if altitude > 100 {
return.
}
print "Setting up test". print "Setting up test".
lock throttle to 100. 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 steering to up.
lock throttle to 0. lock throttle to 0.
unlock throttle. unlock throttle.
unlock steering.
} }
function Landing{
function LandAtPad{
print "Starting Landing". print "Starting Landing".
gear on. gear on.
set pad to location_constants:kerbin:launchpad:position. set pad to location_constants:kerbin:launchpad:position.
@@ -23,14 +31,25 @@ function LandAtPad{
rcs on. rcs on.
set landingThrottle to 0. set landingThrottle to 0.
set steerPitch to 0.0.
set steerYaw to 0.0.
lock throttle to landingThrottle. lock throttle to landingThrottle.
set hoverPid to PIDLoop(5, 0.1, 30, 0, 1). set tgtAlt to 80.
set tgtAlt to 100.
set hoverPid:setpoint to tgtAlt.
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. set landed to false.
when abs(verticalSpeed) < 10 or verticalSpeed > 1 then{
when verticalSpeed > -10 then{
lock steering to up. lock steering to up.
preserve. preserve.
} }
@@ -40,54 +59,57 @@ function LandAtPad{
preserve. 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. wait 0.001.
} }
print "Landed".
lock throttle to 0.
} }
function Descent{ function WaitFalling{
parameter targetSpeed is 100.
print "Coasting to apoapsis.". print "Coasting to apoapsis.".
rcs on. rcs on.
lock steering to up. lock steering to up.
set warp to 1. set warp to 1.
wait until verticalSpeed < -100. wait until verticalSpeed < -targetSpeed.
set warp to 0. set warp to 0.
print "Falling...". print "Falling...".
lock steering to srfRetrograde. lock steering to srfRetrograde.
brakes on. brakes on.
}
function Descent {
local lock curAlt to alt:radar.
local lock vertSpeed to verticalSpeed. local lock vertSpeed to verticalSpeed.
local lock isp to GetIsp().
local lock localGravity to GetLocalGravity(altitude). local lock localGravity to GetLocalGravity(altitude).
local lock burnDuration to CalculateSuicideBurnDuration(isp). local lock drag to GetDragDir(localGravity, ship:up:vector).
local lock imactTime to CalculateTimeToImpact(abs(vertSpeed), alt:radar, localGravity). local lock burnDist to CalculateSuicideBurnAltitude(vertSpeed, localGravity, 80, drag).
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.
}
wait until timeToBurn < 0.5. wait until burnDist >= curAlt.
print "Starting Burn".
print burnDist.
gear on.
until abs(verticalSpeed) < 30{
until abs(verticalSpeed) < 50{
lock throttle to 1. lock throttle to 1.
} }
SET V0 TO GETVOICE(0).
V0:PLAY( NOTE(400, 0.5) ).
lock throttle to 0. lock throttle to 0.
print "Final".
print curAlt.
} }
print "Launch to start.". print "Launch to start.".
WaitForEngineStart(). WaitForEngineStart().
TestSetup(). TestSetup(5000).
WaitFalling(100).
Descent(). Descent().
LandAtPad(). Landing().

View File

@@ -58,14 +58,18 @@ print "Deploying Solar".
panels on. //deploy solar panels on. //deploy solar
print "Opening Docking Port". print "Opening Docking Port".
ag3 on. //open docking port ag3 on. //open docking port
lock steering to heading(90, 0, 0). lock steering to prograde.
set warp to 0. set warp to 0.
set warpmode to "rails". set warpmode to "rails".
set warp to 1. set warp to 1.
lock distToApo to apoapsis - altitude. lock distToApo to abs(apoapsis - altitude).
wait until distToApo < 500. wait until distToApo < 1000.
set warp to 0.
lock steering to heading(90, 0, 0).
wait until distToApo < 800.
print "Phase: Circularize". print "Phase: Circularize".
lock steering to heading(90, 0, 0). lock steering to heading(90, 0, 0).