From a9360771870ba9a36a8abae3d0f0574639de3536 Mon Sep 17 00:00:00 2001 From: kcjennin Date: Mon, 8 Jun 2026 23:28:21 -0700 Subject: [PATCH] Initial transfer file and some other improvements --- boot/testing.ks | 3 ++ scripts/ascent.ks | 4 ++- scripts/lander.ks | 4 ++- scripts/transfer.ks | 69 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 boot/testing.ks create mode 100644 scripts/transfer.ks diff --git a/boot/testing.ks b/boot/testing.ks new file mode 100644 index 0000000..02e3594 --- /dev/null +++ b/boot/testing.ks @@ -0,0 +1,3 @@ +wait until ship:unpacked. +core:part:getmodule("kOSProcessor"):doevent("Open Terminal"). +runPath("0:/scripts/transfer.ks"). \ No newline at end of file diff --git a/scripts/ascent.ks b/scripts/ascent.ks index 94afc04..6bbc075 100644 --- a/scripts/ascent.ks +++ b/scripts/ascent.ks @@ -92,6 +92,7 @@ function basicAscent { wait 0.1. + // this isn't always true, figure out a way around panels on. radiators on. for a in antennas { @@ -129,7 +130,8 @@ function basicAscent { // if we're using a booster to get to orbit drop it before finishing the cirularization when ship:periapsis > 30000 then { - if ship:stagenum <> 0 and ship:stagedeltav(ship:stagenum - 1):current > 0 { + // use RCS as a condition to not stage the booster + if ship:stagenum <> 0 and ship:stagedeltav(ship:stagenum - 1):current > 0 and not brakes { print "Dropping booster". // shutdown engines before dropping them diff --git a/scripts/lander.ks b/scripts/lander.ks index 4e7a740..b35191e 100644 --- a/scripts/lander.ks +++ b/scripts/lander.ks @@ -79,9 +79,11 @@ until h < 1 { wait 0. } +lock throttle to 0. +unlock throttle. clearscreen. print "Holding position". -lock steering to groundSlope(). +lock steering to lookDirUp(groundSlope(), ship:facing:upvector). wait until ship:angularvel:mag < 0.1. wait 5. print "Stable". diff --git a/scripts/transfer.ks b/scripts/transfer.ks new file mode 100644 index 0000000..6101c5f --- /dev/null +++ b/scripts/transfer.ks @@ -0,0 +1,69 @@ + +function hohmannDeltaV { + parameter r1 is 1, r2 is 1, mu is 1. + + local vCirc to sqrt(mu / r1). + local vHohmann to sqrt(mu * (2 / r1 - 2 / (r1 + r2))). + return vHohmann - vCirc. +} + +function angleBetween { + parameter posA to V(1, 1, 0), posB to V(1, 1, 0). + + local angle to arcTan2(posB:y, posB:x) - arcTan2(posA:y, posA:x). + if angle < 0 set angle to angle + 2 * constant:pi. + return angle. +} + + +function hohmannTransfer { + // parameter targetPeriapsis is 60000. + parameter tgtBody is Mun. + + // get the approximate required deltav for a transfer + local r1 to ship:orbit:semimajoraxis. + local r2 to tgtBody:orbit:semimajoraxis. + local vTransfer to hohmannDeltaV(r1, r2, kerbin:mu). + + // compute the transfer time + local transferSMA to (r1 + r2) / 2. + local transferPeriod to 2 * constant:pi * sqrt(transferSMA^3 / kerbin:mu). + local transitTime to transferPeriod / 2. + + // get lead angle (how far ahead the tgtBody must be) + local tgtAV to 2 * constant:pi / tgtBody:orbit:period. + local leadAngle to tgtAV * transitTime. + local transferAngle to constant:pi - leadAngle. + + // find the next time the phase angle condition is satisfied + local shipAV to 2 * constant:pi / ship:orbit:period. + local relativeAV to tgtAV - shipAV. + + // get the phase angle to the target + local bodyToShip to (ship:position - body:position):normalized. + local obtNorm to vCrs(bodyToShip, ship:velocity:orbit:normalized):normalized. + local bodyToTgt to vXcl(obtNorm, (tgtBody:position - body:position):normalized):normalized. + local signVec to vCrs(obtNorm, bodyToShip):normalized. + local phaseSign to vDot(bodyToTgt, signVec). + local currentPhaseAngle to vAng(ship:position - body:position, tgtBody:position - body:position) * (constant:pi / 180). + if phaseSign < 0 set currentPhaseAngle to -1 * currentPhaseAngle + 2 * constant:pi. + + // get the time to start the maneuver + local phaseError is transferAngle - currentPhaseAngle. + if phaseError < 0 set phaseError to phaseError + 2 * constant:pi. + local waitTime to phaseError / relativeAV. + if waitTime < 0 set waitTime to waitTime + ship:orbit:period. + + set nd to node(time:seconds + waitTime, 0, 0, vTransfer). + add nd. + + // print "Transfer DeltaV (m/s) : " + round(vTransfer, 2). + // print "Transit Time (s) : " + round(transitTime*57.3, 0). + // print "Lead Angle (deg) : " + round(transferAngle*57.3, 2). + // print "Relative AV (deg/s) : " + relativeAV*57.3. + // print "Phase Angle (deg) : " + round(currentPhaseAngle*57.3, 2). + // print "Phase Error (deg) : " + round(phaseError*57.3, 2). + // print "Wait Time (s) : " + round(waitTime, 0). +} + +hohmannTransfer(Mun). \ No newline at end of file