diff --git a/boosterDeorbit.ks b/boosterDeorbit.ks new file mode 100644 index 0000000..0128929 --- /dev/null +++ b/boosterDeorbit.ks @@ -0,0 +1,38 @@ +//-126 +function DeOrbit{ + parameter burnLng, deltaV. + + lock pos to SHIP:geoposition. + + lock curLng to pos:lng. + + DECLARE LOCAL tgtLng IS burnLng - 5. + if curLng > burnLng or curLng < tgtLng { + SET WARPMODE TO "RAILS". + SET WARP TO 3. + print "warping to " + tgtLng. + WAIT UNTIL curLng > tgtLng and curLng < burnLng. + SET WARP TO 0. + print "arived at dst " + tgtLng. + print "burn at " + burnLng. + } + print "currently at" + curLng. + + BRAKES ON. + RCS ON. + LOCK steering to retrograde. + wait until curLng >= burnLng. + +} + +function CalculateBurnDuration{ + parameter dv. + + // declare local isp is GetIsp(). + // declare local finalMass is mass * exp(-dv / (isp * constant:g0)). + // declare local twr is ship:maxThrust / (((mass + finalMass)/2) * constant:g0). + + + // local t is dv / (twr * constant.g0). + // return t. +} \ No newline at end of file diff --git a/boot/radenBoot.ks b/boot/radenBoot.ks new file mode 100644 index 0000000..fabd3d0 --- /dev/null +++ b/boot/radenBoot.ks @@ -0,0 +1,4 @@ +wait until ship:unpacked. +clearscreen. +switch to 0. +run radenLaunch. \ No newline at end of file diff --git a/boot/sanaBoot.ks b/boot/sanaBoot.ks new file mode 100644 index 0000000..9da68ff --- /dev/null +++ b/boot/sanaBoot.ks @@ -0,0 +1,4 @@ +wait until ship:unpacked. +clearscreen. +switch to 0. +run poweredLanding. \ No newline at end of file diff --git a/library/lib_math.ks b/library/lib_math.ks index b6befd5..2efab72 100644 --- a/library/lib_math.ks +++ b/library/lib_math.ks @@ -1,15 +1,30 @@ -declare function lerp{ +declare function Lerp{ parameter a. parameter b. parameter t. return a + (b - a) * t. } -declare function map{ - parameter a1. - parameter b1. - parameter a2. - parameter b2. - parameter v. - return a2 + (v - a1) * (b2 - a2) / (b1 - a1). +declare function Map{ + parameter value. + parameter low1. + parameter high1. + parameter low2. + parameter high2. + return low2 + (high2 - low2) * ((value - low1) / (high1 - low1)). +} + +declare function ApproximateExp { + parameter x. + local result is 1.0. + local xPower is 1.0. + local nFact is 1.0. + + // Loop 4 times (for terms 1 through 4, total 5 terms) + from { local n is 1.} until n = 5 step {set n to n + 1.} do { + SET xPower TO xPower * x. + SET nFact TO nFact * n. + SET result TO result + (xPower / nFact). + } + RETURN result. } \ No newline at end of file diff --git a/library/lib_vessel_utils.ks b/library/lib_vessel_utils.ks new file mode 100644 index 0000000..ee0a608 --- /dev/null +++ b/library/lib_vessel_utils.ks @@ -0,0 +1,112 @@ +run "library/lib_math". + +declare function WaitForEngineStart{ + wait until AnyEngineActive(). +} + +declare function AnyEngineActive{ + list engines in allEngines. + for eng in allEngines{ + if eng:ignition { + return true. + } + } + return false. +} + +declare function GetIsp{ + LIST ENGINES IN allEngines. + declare local totalThrust is ship:maxThrust. + local sum is 0. + local weights is 0. + for eng in allEngines{ + if eng:IGNITION and not eng:flameout{ + local w is eng:AVAILABLETHRUST / totalThrust. + local ispW is eng:isp * 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. + for eng in allEngines{ + if eng:IGNITION and not eng:flameout{ + set sum to sum + eng:maxmassflow. + } + } + 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 +{ + parameter isp. + return CalculateBurnDuration(verticalSpeed, isp, ship:mass, GetMaxMassFlow() * 1000). +} + +declare function CalculateBurnDuration +{ + parameter dv. + parameter burnIsp. + parameter initialMass. + parameter massFlow. + + set dv to abs(dv). + local exp is -dv / (burnIsp * constant:g0). + local massRatio is ApproximateExp(-exp). + local finalMass is initialMass / massRatio. + local fuelUsed is initialMass - finalMass. + + if massFlow <= 0{ + return 0. + } + + return fuelUsed / massFlow. +} + + +declare function CalculateAverageAcceleration{ + parameter localGravity. + return ((ship:maxthrust * 1000) / ship:mass) - localGravity. +} + +declare function GetLocalGravity{ + parameter curAltitude is -1. + + if curAltitude = -1 { + set curAltitude to altitude. + } + return body:mu / (curAltitude + body:radius)^2. +} + +declare function CalculateTimeToImpact{ + parameter vertSpeed. + parameter curAltitude. + parameter gravity. + parameter tgtAltitude is 0. + + if gravity <= 0 or curAltitude <= 0{ + print "[warn]: invalid altitude or gravity.". + return 0. + } + + local vs is abs(vertSpeed). + local g is gravity. + local hRel is curAltitude - tgtAltitude. + local disc is ((vs^2) + (2 * g * hRel)). + return (vs + sqrt(disc)) / g. +} \ No newline at end of file diff --git a/poweredLanding.ks b/poweredLanding.ks index 4708bb7..fe09946 100644 --- a/poweredLanding.ks +++ b/poweredLanding.ks @@ -1,58 +1,93 @@ +run "library/lib_vessel_utils". +run "library/lib_location_constants". +run tests. -//-126 -function DeOrbit{ - parameter burnLng, deltaV. +CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal"). - lock pos to SHIP:geoposition. +function TestSetup{ + print "Setting up test". + lock throttle to 100. + wait until apoapsis > 5000. + lock steering to up. + lock throttle to 0. + unlock throttle. +} - lock curLng to pos:lng. - DECLARE LOCAL tgtLng IS burnLng - 5. - if curLng > burnLng or curLng < tgtLng { - SET WARPMODE TO "RAILS". - SET WARP TO 3. - print "warping to " + tgtLng. - WAIT UNTIL curLng > tgtLng and curLng < burnLng. - SET WARP TO 0. - print "arived at dst " + tgtLng. - print "burn at " + burnLng. +function LandAtPad{ + print "Starting Landing". + gear on. + set pad to location_constants:kerbin:launchpad:position. + lock dist to pad. + lock steering to srfRetrograde. + rcs on. + + set landingThrottle to 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 landed to false. + + when abs(verticalSpeed) < 10 or verticalSpeed > 1 then{ + lock steering to up. + preserve. } - print "currently at" + curLng. - BRAKES ON. - RCS ON. - LOCK steering to retrograde. - wait until curLng >= burnLng. - -} - -function CalculateBurnDuration{ - parameter dv. - - declare local isp is GetIsp(). - declare local finalMass is mass * exp(-dv / (isp * constant:g0)). - declare local twr is ship:maxThrust / (((mass + finalMass)/2) * constant:g0). - - - local t is dv / (twr * constant.g0). - return t. -} - -function GetIsp{ - LIST ENGINES IN allEngines. - declare local totalThrust is ship:maxThrust. - local sum is 0. - local weights is 0. - for eng in allEngines{ - if eng:IGNITION and not eng:flameout{ - local w is eng:AVAILABLETHRUST / totalThrust. - local ispW is eng:isp * w. - set sum to sum + ispW. - set weights to weights + w. - } + when verticalSpeed < -10 then{ + lock steering to srfRetrograde. + preserve. + } + + until landed { + set landingThrottle to hoverPid:update(time:seconds, alt:radar). + + // print "offset " + dist. + wait 0.001. } - return sum / weights. } -// DeOrbit(-126, 250). -print CalculateBurnDuration(250). \ No newline at end of file +function Descent{ + print "Coasting to apoapsis.". + rcs on. + lock steering to up. + set warp to 1. + wait until verticalSpeed < -100. + set warp to 0. + print "Falling...". + lock steering to srfRetrograde. + brakes on. + + 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. + } + + wait until timeToBurn < 0.5. + + + until abs(verticalSpeed) < 50{ + lock throttle to 1. + } + + + SET V0 TO GETVOICE(0). + V0:PLAY( NOTE(400, 0.5) ). + lock throttle to 0. +} + + +print "Launch to start.". +WaitForEngineStart(). +TestSetup(). +Descent(). +LandAtPad(). + diff --git a/radenLaunch.ks b/radenLaunch.ks index 3c9a673..da10d48 100644 --- a/radenLaunch.ks +++ b/radenLaunch.ks @@ -1,28 +1,47 @@ run "library/lib_math". +run "library/lib_vessel_utils". + +local athmoAccelTarget is 600. + +CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal"). clearScreen. BRAKES on. +print "Waiting for engine ignition". +WaitForEngineStart(). + print "Preparing to launch". from { local c is 5.} until c = 0 step {set c to c - 1.} do { print c. wait 1. } +clearScreen. print "Launching!". -lock throttle to 100. -lock steering to heading(90, 0, 0). +// stage. +lock throttle to 1. +lock steering to heading(90, 1). +brakes off. +sas off. print "Phase: Takeoff". -wait until surfaceSpeed > 140. + +wait until groundspeed > 120. print "Phase: Rotate". -lock steering to heading(90, 3, 0). -wait 1. +lock steering to heading(90, 4). +wait until altitude > 80. print "Gear up". gear off. +print "Phase: Athmospheric Acceleration". +lock steering to heading(90, 3, 0). +wait until groundspeed > athmoAccelTarget. + print "Phase: Athmospheric Climb". -lock steering to heading(90, 15, 0). -wait until altitude > 20000. +lock tgtPitch to Map(groundSpeed, athmoAccelTarget, 1600, 3, 15). +lock steering to heading(90, tgtPitch, 0). + +wait until altitude > 25000. print "Phase: Mode Switch". ag1 on. //Switch engine mode @@ -31,20 +50,28 @@ wait until apoapsis > 80000. print "Phase: Sub-Orbital Coast". lock throttle to 0. -lock steering to prograde. +set warpmode to "physics". +set warp to 1. + +wait until altitude > 70000. +print "Deploying Solar". +panels on. //deploy solar +print "Opening Docking Port". +ag3 on. //open docking port +lock steering to heading(90, 0, 0). +set warp to 0. +set warpmode to "rails". +set warp to 1. -when altitude > 70000 then { - print "Deploying Solar". - ag2 on. //deploy solar - print "Opening Docking Port". - ag3 on. //open docking port -} lock distToApo to apoapsis - altitude. -wait until distToApo < 100. +wait until distToApo < 500. + print "Phase: Circularize". -lock throttle to 100. -wait until periapsis > 79000. +lock steering to heading(90, 0, 0). +rcs on. +lock throttle to 1. +wait until periapsis > 75000. unlock throttle. print "In orbit. Releasing controls". diff --git a/tests.ks b/tests.ks new file mode 100644 index 0000000..ea30fa6 --- /dev/null +++ b/tests.ks @@ -0,0 +1,6 @@ +run "library/lib_vessel_utils". + +CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal"). + +print "impact " + CalculateTimeToImpact(300, 500, 9.8). +print "Burn Duration" + CalculateBurnDuration(300, 350, 5000, 3000). \ No newline at end of file