From d27d63fb80908bb96b92cae1b060c6bd33a74881 Mon Sep 17 00:00:00 2001
From: annabunches <annabunches@gmail.com>
Date: Tue, 20 Jul 2021 04:18:16 -0400
Subject: [PATCH] Use orbital math to simplify circ node.

---
 execnode.ks       |  7 ++++---
 launch.ks         |  2 +-
 lib/navigation.ks | 33 +++++++++++----------------------
 ui/rocket.ks      |  2 +-
 4 files changed, 17 insertions(+), 27 deletions(-)

diff --git a/execnode.ks b/execnode.ks
index 3f06e44..dd5f0fa 100644
--- a/execnode.ks
+++ b/execnode.ks
@@ -2,18 +2,19 @@ runoncepath("lib/navigation").
 runoncepath("lib/throttle").
 
 SAS off.
-local t is BurnTime(NEXTNODE:DELTAV:MAG)/2.
+local t is BurnTime(NEXTNODE:DELTAV:MAG).
 
 print "Adjusting heading".
 lock STEERING to NEXTNODE:DELTAV.
 wait until VectorAngle(SHIP:FACING:FOREVECTOR, STEERINGMANAGER:TARGET:FOREVECTOR) <= 0.1.
 
 print "Warping to node.".
-KUNIVERSE:TIMEWARP:WarpTo(NEXTNODE:TIME - t - 5).
-wait until NEXTNODE:ETA <= t.
+KUNIVERSE:TIMEWARP:WarpTo(NEXTNODE:TIME - (t/2) - 5).
+wait until NEXTNODE:ETA <= (t/2).
 
 // todo: pid loop here or nah? overshoot would be tricky to deal with...
 print "Executing burn.".
+
 local throt is 1.0.
 lock THROTTLE to throt.
 local dvLast is NEXTNODE:DELTAV:MAG.
diff --git a/launch.ks b/launch.ks
index f99492f..6cc02e5 100644
--- a/launch.ks
+++ b/launch.ks
@@ -63,7 +63,7 @@ set SHIP:CONTROL:PILOTMAINTHROTTLE to 0.
 wait 0.001. // make sure these control updates get applied
 
 print "Target apoapsis acquired. Creating maneuver node.".
-AddCircularizationNode().
+add CreateCircularizationNode().
 runpath("/execnode").
 print "Orbit acquired. Releasing controls. Good luck, Kerman.".
 unlock THROTTLE.
diff --git a/lib/navigation.ks b/lib/navigation.ks
index 054af5a..c64a91e 100644
--- a/lib/navigation.ks
+++ b/lib/navigation.ks
@@ -18,26 +18,15 @@ function GetAscentVector {
   return newHeading.
 }
 
-function AddCircularizationNode {
-  parameter usePeriapsis is false.
-  local target is SHIP:ORBIT:APOAPSIS.
-  local t is TIME + SHIP:ORBIT:ETA:APOAPSIS.
-  if usePeriapsis {
-    set target to SHIP:ORBIT:PERIAPSIS.
-    set t to SHIP:ORBIT:ETA:PERIAPSIS.
-  }
-  local n is Node(t, 0, 0, 0).
-  add(n).
- 
-  local epsilon is 100.
-  lock diff to n:ORBIT:APOAPSIS - n:ORBIT:PERIAPSIS.
-  local pid is PIDLoop(0.05, 0.006, 0.006).
-  set pid:EPSILON to epsilon.
-  set pid:SETPOINT to n:ORBIT:APOAPSIS.
-  
-  until abs(diff) < epsilon {
-    set n:PROGRADE to pid:Update(TIME:SECONDS, n:ORBIT:PERIAPSIS).
-    wait 0.001.
-  }
-  print "Circularization node created."
+function CreateCircularizationNode {
+  parameter nodeAtPeriapsis is false.
+
+  local dt is choose SHIP:ORBIT:ETA:PERIAPSIS if nodeAtPeriapsis else SHIP:ORBIT:ETA:APOAPSIS.
+  local t is TIME + dt.
+
+  local Vc is sqrt(SHIP:BODY:MU/(SHIP:BODY:RADIUS + SHIP:ORBIT:APOAPSIS)).
+  local dV is Vc - VelocityAt(SHIP, t):ORBIT:MAG.
+  local n is Node(t, 0, 0, dV).
+
+  return n.
 }
diff --git a/ui/rocket.ks b/ui/rocket.ks
index c5fd0e6..0f6b588 100644
--- a/ui/rocket.ks
+++ b/ui/rocket.ks
@@ -61,7 +61,7 @@ set termButton:onClick to terminalButtonPressed@.
 // debug
 function circButtonPressed {
   runpath("/lib/navigation").
-  AddCircularizationNode().
+  add CreateCircularizationNode().
 }
 local cButton is interface:AddButton("Circularize").
 set cButton:onClick to circButtonPressed@.