From 9a4ae5e472d3191015732d890b9baf489605bc31 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Sat, 18 Apr 2026 23:19:23 -0400 Subject: [PATCH] ascend + basic auto landing + misc --- an.ks | 20 +++++++ autoLand.ks | 11 ++++ beegSana.ks | 40 ++++++++++++-- boot/anBoot.ks | 6 +++ boot/autoLandBoot.ks | 3 ++ boot/farisBoot.ks | 6 +++ boot/soraBoot.ks | 9 ++++ defaults.ks | 5 +- faris.ks | 20 +++++++ library/lib_ascent.ks | 87 ++++++++++++++++++++++++++++++ library/lib_capacitors.ks | 52 ++++++++++++++++++ library/lib_import.ks | 6 +++ library/lib_math.ks | 23 +++++++- library/lib_orbits.ks | 88 +++++++++++++++++++++++++++++++ library/lib_staging.ks | 32 +++++++++++ library/lib_vessel_utils.ks | 102 +++++++++++++++++++++++++++++++++++- poweredLanding.ks | 1 - 17 files changed, 504 insertions(+), 7 deletions(-) create mode 100644 an.ks create mode 100644 autoLand.ks create mode 100644 boot/anBoot.ks create mode 100644 boot/autoLandBoot.ks create mode 100644 boot/farisBoot.ks create mode 100644 boot/soraBoot.ks create mode 100644 faris.ks create mode 100644 library/lib_ascent.ks create mode 100644 library/lib_capacitors.ks create mode 100644 library/lib_import.ks create mode 100644 library/lib_orbits.ks create mode 100644 library/lib_staging.ks diff --git a/an.ks b/an.ks new file mode 100644 index 0000000..7dd0d5b --- /dev/null +++ b/an.ks @@ -0,0 +1,20 @@ +import("library/lib_vessel_utils"). +import("library/lib_ascent"). +import("library/lib_orbits"). + + + + +WaitForEngineStart(). + +CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal"). + +set profile to 1.2. +set turn_start to 1000. +set target_orbit to 100000. + +Ascent(target_orbit, turn_start, profile). + +print "Orbit Achieved, Releasing Controls". +CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Close Terminal"). +SHUTDOWN. \ No newline at end of file diff --git a/autoLand.ks b/autoLand.ks new file mode 100644 index 0000000..d107d94 --- /dev/null +++ b/autoLand.ks @@ -0,0 +1,11 @@ +run "defaults". +clearscreen. +run poweredLanding. + +wait until AG9. +WaitForEngineStart(). +CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal"). + +WaitFalling(100). +Descent(). +Landing(). diff --git a/beegSana.ks b/beegSana.ks index 575bd6c..435acc7 100644 --- a/beegSana.ks +++ b/beegSana.ks @@ -1,5 +1,6 @@ run "defaults". - +run "library/lib_orbits". +run "library/lib_vessel_utils". run "poweredLanding". declare function Reentry{ @@ -14,12 +15,45 @@ declare function Reentry{ rcs on. lock steering to srfRetrograde. } + +declare function DeorbitBurn{ + if periapsis < 0 + { + return. + } + set kscPad to location_constants:kerbin:launchpad. + + // wait until ag10. + sas off. + rcs on. + CreateDeorbit(kscPad, -300, 0, 0). + lock steering to nextNode:burnvector. + local lock align to vDot(ship:facing:vector, nextNode:burnvector). + wait until align > 0.95. + unlock steering. + sas on. + set sasMode to "MANEUVER". + wait 0.001. + warpTo(time:seconds + nextNode:eta - 15). + sas off. + local dur to CalculateBurnDuration(nextNode:burnvector:mag, GetIsp(), ship:mass, GetMaxMassFlow() * 1000). + print "duration " + dur. + lock steering to nextNode:burnvector. + wait until nextNode:eta < 1. + local end to time:seconds + dur. + lock throttle to 1. + wait until end <= time:seconds. + lock throttle to 0. + unlock throttle. + unlock steering. + remove nextNode. +} + +DeorbitBurn(). wait until (altitude > 70000 and periapsis > 70000) or (verticalSpeed < -100 and periapsis < 0). wait until (periapsis < 0 and throttle = 0). print "Prep for re-entry". print "Dont forget to Turn of MechJob Q Limit". -sas off. -rcs on. // set mj to addons:mj. // set asc to mj:ascent. // set asc:limitqaenabled to false. diff --git a/boot/anBoot.ks b/boot/anBoot.ks new file mode 100644 index 0000000..1c43677 --- /dev/null +++ b/boot/anBoot.ks @@ -0,0 +1,6 @@ +wait until ship:unpacked. +clearscreen. +copyPath("0:/defaults", ""). +run "defaults". + +import("an"). \ No newline at end of file diff --git a/boot/autoLandBoot.ks b/boot/autoLandBoot.ks new file mode 100644 index 0000000..896cec9 --- /dev/null +++ b/boot/autoLandBoot.ks @@ -0,0 +1,3 @@ +wait until ship:unpacked. +switch to 0. +run autoLand. diff --git a/boot/farisBoot.ks b/boot/farisBoot.ks new file mode 100644 index 0000000..72ad5fa --- /dev/null +++ b/boot/farisBoot.ks @@ -0,0 +1,6 @@ +wait until ship:unpacked. +clearscreen. +copyPath("0:/defaults", ""). +run "defaults". + +import("faris"). \ No newline at end of file diff --git a/boot/soraBoot.ks b/boot/soraBoot.ks new file mode 100644 index 0000000..017ae40 --- /dev/null +++ b/boot/soraBoot.ks @@ -0,0 +1,9 @@ +wait until ship:unpacked. +switch to 0. + +run "defaults". +run "library/lib_capacitors". + + +AutoManageCapacitors(). +until false. \ No newline at end of file diff --git a/defaults.ks b/defaults.ks index 6bc7bfc..642ffd3 100644 --- a/defaults.ks +++ b/defaults.ks @@ -1 +1,4 @@ -set terminal:charheight to 20. \ No newline at end of file +set terminal:charheight to 20. + +copyPath("0:/library/lib_import", "1:/library/lib_import"). +run "library/lib_import". \ No newline at end of file diff --git a/faris.ks b/faris.ks new file mode 100644 index 0000000..ac3e7bc --- /dev/null +++ b/faris.ks @@ -0,0 +1,20 @@ + + +import("library/lib_vessel_utils"). +import("library/lib_ascent"). +import("library/lib_orbits"). + +WaitForEngineStart(). + +CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal"). + +set profile to 1.2. +set turn_start to 1000. +set target_orbit to 100000. + +Ascent(target_orbit, turn_start, profile). + +print "Orbit Achieved, Releasing Controls". +print "Circ DV" + CalculateCircularizationDV(). +CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Close Terminal"). +SHUTDOWN. \ No newline at end of file diff --git a/library/lib_ascent.ks b/library/lib_ascent.ks new file mode 100644 index 0000000..ca5bde3 --- /dev/null +++ b/library/lib_ascent.ks @@ -0,0 +1,87 @@ +import("library/lib_staging"). +import("library/lib_math"). +import("library/lib_vessel_utils"). +import("library/lib_orbits"). + + +declare function Ascent{ + parameter targetOrbit is 100. + parameter vericalAscent is 1000. + parameter ascentProfile is 1.2. + + print "Vertical Climb to " + vericalAscent. + + set tgtPitch to 90. + + lock throttle to 1. + lock steering to heading(90, tgtPitch, 270). + + local lock asccent_prog to Map(apoapsis, 0, targetOrbit, 0.0, 1.0). + + wait until altitude > vericalAscent and asccent_prog > 0. + print "Start Gravity turn. Target Apoapsis: " + targetOrbit. + + local lock ease to EaseOutExp(asccent_prog, ascentProfile). + local lock tgtPitch to (1 - ease) * 90. + + when periapsis < targetOrbit then { + print "P: " + round(asccent_prog, 2) + " T:" + round(ease, 2) + " Pitch: " + round(tgtPitch, 2) + " " at(0, 2). + print "Ap: " + round(apoapsis, 2) + " Pe:" + round(periapsis, 2) + " Alt: " + round(altitude, 2) + " " at(0, 3). + wait 0.1. + preserve. + } + + //Auto Staging + when periapsis < targetOrbit then{ + AutoStage(). + wait 0.01. + preserve. + } + + wait until apoapsis >= targetOrbit. + + unlock tgt_pitch. + lock tgtPitch to 0. + unlock steering. + + + CircularizeBrute(targetOrbit). + + + unlock steering. + unlock throttle. +} + + +local function Circularize{ + parameter targetOrbit. + + lock throttle to 0. + lock steering to heading(90, 0, 270). + + local warpTime is eta:apoapsis. + + local burnDv is CalculateCircularizationDV(). + local burnDuration is CalculateMultiStageBurnDuration(burnDv). + print "Warping to Circularization. " + round(burnDv) + "m/s. T: " + round(burnDuration, 2) + "s" at(0, 4). + set warpTime to warpTime - burnDuration. + if warpTime > 0 { + wait until throttle = 0. + wait 0.01. //Wait for accelleration to stop + warpTo(time:seconds + warpTime). + } + wait until eta:apoapsis <= burnDuration. + + lock throttle to 1. + wait until periapsis >= targetOrbit. + lock throttle to 0. +} + + +local function CircularizeBrute{ + parameter targetOrbit. + lock throttle to 1. + lock steering to heading(90, 0, 270). + wait until periapsis >= targetOrbit. + lock throttle to 0. +} diff --git a/library/lib_capacitors.ks b/library/lib_capacitors.ks new file mode 100644 index 0000000..df6f77b --- /dev/null +++ b/library/lib_capacitors.ks @@ -0,0 +1,52 @@ +declare function AutoManageCapacitors{ + print "start". + local capacitors to ship:partsnamedpattern("capacitor"). + local lock electricity to GetTotalElectiricity(). + when electricity < 100 then { + for cap in capacitors { + local capR to cap:resources[0]. + + if capR:amount = capR:capacity{ + local discharge is cap:getmodule("DischargeCapacitor"). + discharge:doaction("Discharge Capacitor", true). + wait 1. + break. + } + } + preserve. + } + when throttle = 0 then { + for cap in capacitors { + local capR to cap:resources[0]. + local discharge is cap:getmodule("DischargeCapacitor"). + + if capR:amount = capR:capacity{ + discharge:doaction("Disable Recharge", true). + }else{ + discharge:doaction("Enable Recharge", true). + } + } + preserve. + } + when throttle > 0 then{ + for cap in capacitors { + local capR to cap:resources[0]. + local discharge is cap:getmodule("DischargeCapacitor"). + + discharge:doaction("Disable Recharge", true). + } + preserve. + } +} + + +declare function GetTotalElectiricity{ + LIST RESOURCES IN res. + local e is 0. + FOR resPart IN res { + if resPart:name = "ElectricCharge" { + set e to e + resPart:amount. + } + } + return e. +} \ No newline at end of file diff --git a/library/lib_import.ks b/library/lib_import.ks new file mode 100644 index 0000000..d8b9aaf --- /dev/null +++ b/library/lib_import.ks @@ -0,0 +1,6 @@ +declare function import { + parameter import_path. + copyPath("0:/" + import_path, "1:/" + import_path). + runOncePath(import_path). + +} \ No newline at end of file diff --git a/library/lib_math.ks b/library/lib_math.ks index 2efab72..c7dc0e7 100644 --- a/library/lib_math.ks +++ b/library/lib_math.ks @@ -27,4 +27,25 @@ declare function ApproximateExp { SET result TO result + (xPower / nFact). } RETURN result. -} \ No newline at end of file +} + +declare function EaseOutExpo { + parameter x. + parameter p is 10. + if x = 1 { + return 1. + } else{ + return 1 - (2 ^ (-p * x)). + } +} + +declare function EaseOutCirc{ + parameter x. + return sqrt(1 - (x - 1)^2). +} + +declare function EaseOutExp{ + parameter x. + parameter n is 2.0. + return x ^ (2.0 * n). +} diff --git a/library/lib_orbits.ks b/library/lib_orbits.ks new file mode 100644 index 0000000..826fc23 --- /dev/null +++ b/library/lib_orbits.ks @@ -0,0 +1,88 @@ + +declare function CreateDeorbit{ + parameter target. + parameter dv is 300. + parameter offsetX is 0.0. + parameter offsetZ is 0.0. + + local tol to 100. + + lock steering to retrograde. + local lock align to vDot(ship:facing:vector, retrograde:vector). + wait until align > 0.95. + print align. + + if dv > 0 { + set dv to dv * -1. + } + + local tr to ADDONS:TR. + if not tr:AVAILABLE { + print "Trajectoris is not available". + return. + } + + tr:settarget(target). + + + local deorbit to NODE(time:seconds + 200, 0, 0, dv). + + until not hasNode{ + remove nextNode. + wait 0.001. + } + + add deorbit. + wait until tr:hasimpact. + + local ipos to tr:impactpos. + local lock offset to (ipos:ALTITUDEPOSITION(0) - target:ALTITUDEPOSITION(0)). + local lock xDist to offset:x. + local lock zDist to ipos:lat - target:lat. + local dt is 60. + when xDist < 500000 then{ + set dt to 30. + } + when xDist < 100000 then{ + set dt to 5. + } + when xDist < 10000 then{ + set dt to 1. + } + + when xDist < 5000 then{ + set dt to 0.5. + } + until xDist < offsetX + tol { + remove nextNode. + wait 0.001. + set deorbit:eta to deorbit:eta + dt. + add deorbit. + wait until tr:hasimpact. + set ipos to tr:impactpos. + wait 0.001. + } + until abs(zDist) < offsetZ + tol{ + remove nextNode. + wait 0.001. + if zDist > 0 { + set deorbit:normal to deorbit:normal - 0.1. + } else{ + set deorbit:normal to deorbit:normal + 0.1. + } + add deorbit. + wait until tr:hasimpact. + set ipos to tr:impactpos. + } + + unlock steering. +} + +declare function CalculateCircularizationDV{ + local mu is body:mu. + + local apR is apoapsis + body:radius. + local circVel is sqrt(mu / apR). + local apVel is sqrt(mu * ((2 / apR) - (1 / orbit:semimajoraxis))). + return circVel - apVel. +} diff --git a/library/lib_staging.ks b/library/lib_staging.ks new file mode 100644 index 0000000..924d51b --- /dev/null +++ b/library/lib_staging.ks @@ -0,0 +1,32 @@ + + +declare function AutoStage{ + if not EnginesInStageActive(ship:stagenum) { + stage. + wait 0.1. + } +} + +local function EnginesInStageActive{ + parameter targetStage. + + list engines in allEngines. + for eng in allEngines{ + if eng:stage = targetStage { + if eng:IGNITION and eng:flameout { + return false. + } + } + } + return true. +} + +local function IsFairingStage{ + parameter s. + +} + +local function StateHasEngines{ + parameter s. + +} \ No newline at end of file diff --git a/library/lib_vessel_utils.ks b/library/lib_vessel_utils.ks index 3940bd4..8781ec3 100644 --- a/library/lib_vessel_utils.ks +++ b/library/lib_vessel_utils.ks @@ -1,4 +1,4 @@ -run "library/lib_math". +import("library/lib_math"). declare function WaitForEngineStart{ wait until AnyEngineActive(). @@ -41,6 +41,24 @@ declare function GetIsp{ return sum / weights. } +declare function GetIspForStage{ + parameter engineState. + parameter pressure is 0. + LIST ENGINES IN allEngines. + local totalThrust is GetThrustOfStage(engineState). + local sum is 0. + local weights is 0. + for eng in allEngines{ + if eng:stage = engineState { + local w is eng:POSSIBLETHRUST / totalThrust. + local ispW is eng:ispat(pressure) * w. + set sum to sum + ispW. + set weights to weights + w. + } + } + return sum / weights. +} + declare function GetMaxMassFlow{ LIST ENGINES IN allEngines. local sum is 0. @@ -129,4 +147,86 @@ declare function CalculateTimeToImpact{ local hRel is curAltitude - tgtAltitude. local disc is ((vs^2) + (2 * g * hRel)). return (vs + sqrt(disc)) / g. +} + +function CalculateMultiStageBurnDuration{ + parameter dv. + if ship:deltav:current >= dv { + print "one stage" at (0, 5). + return GetBurnTime(dv, ship:maxthrust, ship:mass). + }else{ + print "multi stage" at (0, 5). + local burnTime is 0. + local remDv is dv. + from {local s is ship:stagenum. } until s = 0 step { set s to s-1.} do { + if remDv <= 0 { + return burnTime. + } + local cdv is ship:stagedeltav(s):current. + local cmass is GetMassOfStage(s). + local cthrust is GetThrustOfStage(s). + local cisp is GetIspForStage(s). + if cdv >= remDv { + set burnTime to burnTime + GetBurnTime(remDv, cmass, cthrust, cisp). + }else{ + set burnTime to burnTime + GetBurnTime(cdv, cmass, cthrust, cisp). + } + set remDv to remDv - cdv. + } + return burnTime. + } +} + +// Burn time from rocket equation +function GetBurnTime { + parameter deltaV. + parameter stagethrust is 0. + parameter stagemass is 0. + parameter isp is 0. + + if deltaV:typename() = "Vector" { + set deltaV to deltaV:mag. + } + if isp = 0 { + set isp to GetIsp(). + } + if stagethrust = 0 { + set stagethrust to ship:thrust. + } + if stagemass = 0 { + set stagemass to ship:mass. + } + + local burnTime is -1. + if stagethrust <> 0 { + set burnTime to stagemass * (1 - CONSTANT:E ^ (-deltaV / isp)) / (stagethrust / isp). + } + return burnTime. +} + +function GetThrustOfStage { + parameter st. + + local totalThrust is 0. + LIST ENGINES IN allEngines. + for eng in allEngines{ + if eng:stage = st { + set totalThrust to totalThrust + eng:POSSIBLETHRUST. + } + } + return totalThrust. +} + +function GetMassOfStage { + parameter st. + + + local total is 0. + LIST PARTS IN allP. + for p in allP { + if p:stage <= st { + set total to total + p:mass. + } + } + return total. } \ No newline at end of file diff --git a/poweredLanding.ks b/poweredLanding.ks index 2a9f160..1e9ce3e 100644 --- a/poweredLanding.ks +++ b/poweredLanding.ks @@ -36,7 +36,6 @@ declare function Landing{ print "Starting Landing". gear on. - set pad to location_constants:kerbin:launchpad:position. lock dist to pad. lock steering to srfRetrograde. rcs on.