Skip to content

Commit

Permalink
working terminal guidance and misc UI tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jun 27, 2017
1 parent 3628833 commit f61e4a0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
57 changes: 38 additions & 19 deletions MechJeb2/MechJebModuleAscentPEG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ public class MechJebModuleAscentPEG : MechJebModuleAscentBase
{
public MechJebModuleAscentPEG(MechJebCore core) : base(core) { }

/* default pitch program here works seemingly decent at SLT of about 1.4 */
/* default pitch program here works decently at SLT of about 1.4 */
[Persistent(pass = (int)(Pass.Type | Pass.Global))]
public EditableDoubleMult pitchStartTime = new EditableDoubleMult(10);
[Persistent(pass = (int)(Pass.Type | Pass.Global))]
public EditableDoubleMult pitchRate = new EditableDoubleMult(0.75);
[Persistent(pass = (int)(Pass.Type | Pass.Global))]
public EditableDoubleMult pitchEndTime = new EditableDoubleMult(55);
[Persistent(pass = (int)(Pass.Type | Pass.Global))]
public EditableDoubleMult pitchBias = new EditableDoubleMult(0);
public EditableDoubleMult desiredApoapsis = new EditableDoubleMult(100000, 1000);
[Persistent(pass = (int)(Pass.Type | Pass.Global))]
public EditableDoubleMult terminalGuidanceSecs = new EditableDoubleMult(10);

/* this deliberately does not persist, it is for emergencies only */
public EditableDoubleMult pitchBias = new EditableDoubleMult(0);

private MechJebModuleStageStats stats { get { return core.GetComputerModule<MechJebModuleStageStats>(); } }
private FuelFlowSimulation.Stats[] vacStats { get { return stats.vacStats; } }
Expand Down Expand Up @@ -148,10 +153,6 @@ private void peg_solve()

private void peg_estimate(double dt, bool debug = false)
{
double oldA = A;
double oldB = B;
double oldT = T;

/* update old guidance */
A = A + B * dt;
B = B;
Expand Down Expand Up @@ -209,6 +210,7 @@ private bool bad_guidance()
public bool guidanceEnabled = true;
public bool saneGuidance = false;
public int convergenceSteps;
public bool terminalGuidance = false;

private void converge(double dt, bool initialize = false)
{
Expand All @@ -224,23 +226,36 @@ private void converge(double dt, bool initialize = false)
double startingA = A;
double startingB = B;

bool converged = false;
for(convergenceSteps = 1; convergenceSteps <= 250; convergenceSteps++) {
double oldT = T;
if (convergenceSteps == 0)
peg_estimate(dt);
else
peg_estimate(0);
peg_solve();
if ( Math.Abs(T - oldT) < 0.01 ) {
converged = true;
break;
bool stable = false;

if (T < terminalGuidanceSecs)
{
peg_estimate(dt);
terminalGuidance = true;
stable = true; /* terminal guidance is always considered stable */
}
else
{
for(convergenceSteps = 1; convergenceSteps <= 250; convergenceSteps++) {
double oldT = T;
if (convergenceSteps == 0)
peg_estimate(dt);
else
peg_estimate(0);

peg_solve();

if ( Math.Abs(T - oldT) < 0.01 ) {
stable = true;
break;
}
}
terminalGuidance = false;
}

Debug.Log("pitch = " + Math.Asin(A+C) + " dV = " + dV + " cycles = " + convergenceSteps);

if (!converged || bad_guidance() || bad_dV() || bad_pitch())
if (!stable || bad_guidance() || bad_dV() || bad_pitch())
{
/* FIXME: probably shouldn't scribble over globals then restore them if they're bad --
should scribble in local vars and then set them if they're good. */
Expand Down Expand Up @@ -342,7 +357,11 @@ void DriveGravityTurn(FlightCtrlState s)
}

if (saneGuidance && guidanceEnabled) {
status = "Stable PEG Guidance";
if (terminalGuidance)
status = "Locked Terminal Guidance";
else
status = "Stable PEG Guidance";

attitudeTo(guidancePitch);
}
else
Expand Down
23 changes: 18 additions & 5 deletions MechJeb2/MechJebModuleAscentPEGMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,30 @@ protected override void WindowGUI(int windowID)

GUILayout.BeginVertical();

GuiUtils.SimpleTextBox("Target Periapsis:", autopilot.desiredOrbitAltitude, "km");
GuiUtils.SimpleTextBox("Target Apoapsis:", path.desiredApoapsis, "km");
if ( path.desiredApoapsis < autopilot.desiredOrbitAltitude )
{
GUIStyle s = new GUIStyle(GUI.skin.label);
s.normal.textColor = Color.yellow;
GUILayout.Label("Apoapsis < Periapsis: circularizing orbit at periapsis", s);
}

GuiUtils.SimpleTextBox("Booster Pitch start:", path.pitchStartTime, "s");
GuiUtils.SimpleTextBox("Booster Pitch rate:", path.pitchRate, "°/s");
GuiUtils.SimpleTextBox("Booster Pitch end:", path.pitchEndTime, "s");
GuiUtils.SimpleTextBox("Pitch adjustment:", path.pitchBias, "°");
GUILayout.Label(String.Format("ending pitch: {0:F1}°", (path.pitchEndTime - path.pitchStartTime)*path.pitchRate));
GuiUtils.SimpleTextBox("Terminal Guidance Period:", path.terminalGuidanceSecs, "s");


GUILayout.EndVertical();
GUILayout.BeginVertical();

GUILayout.Label("Burnout Stats");
GUILayout.Label(String.Format("delta-V: {0:F2}", path.dV));
GUILayout.Label(String.Format("time: {0:F2}", path.T));
GUILayout.Label(String.Format("pitch: {0:F2}", path.guidancePitch));
GUILayout.Label(String.Format("steps: {0:F2}", path.convergenceSteps));
GUILayout.Label(String.Format("delta-V: {0:F1}", path.dV));
GUILayout.Label(String.Format("time: {0:F1}", path.T));
GUILayout.Label(String.Format("pitch: {0:F1}", path.guidancePitch));
GUILayout.Label(String.Format("steps: {0:D}", path.convergenceSteps));

if (path.guidanceEnabled)
{
Expand All @@ -60,6 +71,8 @@ protected override void WindowGUI(int windowID)
path.guidanceEnabled = true;
}

GuiUtils.SimpleTextBox("Emergency pitch adj.:", path.pitchBias, "°");


if (autopilot.enabled)
{
Expand Down

0 comments on commit f61e4a0

Please sign in to comment.