Skip to content

Commit

Permalink
add some caching state to be used later
Browse files Browse the repository at this point in the history
also converts num_stages back to a plain-old-int

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jul 7, 2017
1 parent 8952531 commit 1d933fa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
36 changes: 34 additions & 2 deletions MechJeb2/MechJebModuleAscentPEG.cs
Expand Up @@ -26,7 +26,8 @@ public class MechJebModuleAscentPEG : MechJebModuleAscentBase
[Persistent(pass = (int)(Pass.Type | Pass.Global))]
public EditableDoubleMult stageLowDVLimit = new EditableDoubleMult(20);
[Persistent(pass = (int)(Pass.Type | Pass.Global))]
public EditableInt num_stages = new EditableInt(2);
public EditableInt edit_num_stages = new EditableInt(2);
private int num_stages;

/* this deliberately does not persist, it is for emergencies only */
public EditableDoubleMult pitchBias = new EditableDoubleMult(0);
Expand All @@ -37,6 +38,7 @@ public class MechJebModuleAscentPEG : MechJebModuleAscentBase

public override void OnModuleEnabled()
{
SetNumStages(edit_num_stages);
mode = AscentMode.VERTICAL_ASCENT;
InitStageStats();
}
Expand Down Expand Up @@ -88,6 +90,29 @@ private enum AscentMode { VERTICAL_ASCENT, INITIATE_TURN, GRAVITY_TURN, EXIT };

public List<StageInfo> stages = new List<StageInfo>();

private void SetNumStages(int n)
{
edit_num_stages = n;
if (num_stages != n)
{
num_stages = n;
InitConstantCache();
}
}

private void InitConstantCache()
{
for(int i = 0; i < num_stages; i++) {
stages[i].b = new double[num_stages+1];
stages[i].c = new double[num_stages+1];
stages[i].b_dirty = new bool[num_stages+1];
stages[i].c_dirty = new bool[num_stages+1];
for(int j = 0; i <= num_stages; j++) {
stages[i].b_dirty[j] = stages[i].c_dirty[j] = true;
}
}
}

public class StageInfo
{
/* updated directly from vehicle stats */
Expand All @@ -100,6 +125,12 @@ public class StageInfo
public double dV; /* lower stages are == avail_dV, upper stage needs estimation */
public double T; /* lower stages are == avail_T, upper stage needs estimation */

/* constant cache */
public double[] b;
public double[] c;
public bool[] b_dirty;
public bool[] c_dirty;

/* steering constants */
public double A;
public double B;
Expand Down Expand Up @@ -264,7 +295,7 @@ void UpdateStageStats()
/* if someone runs a booster program though the full boster we don't consume a PEG stage */
/* also if PEG is disabled manually we don't consume stages */
if ( mode == AscentMode.GRAVITY_TURN && guidanceEnabled )
num_stages = Math.Max(1, num_stages - 1);
SetNumStages( Math.Max(1, num_stages - 1) );
stages.RemoveAt(i);
}
}
Expand Down Expand Up @@ -617,6 +648,7 @@ public override bool DriveAscent(FlightCtrlState s)
{
stats.RequestUpdate(this);
stats.liveSLT = true; /* yes, this disables the button, yes, it is important we do this */
SetNumStages(edit_num_stages);
UpdateRocketStats();

if (last_time != 0.0D)
Expand Down
2 changes: 1 addition & 1 deletion MechJeb2/MechJebModuleAscentPEGMenu.cs
Expand Up @@ -49,7 +49,7 @@ protected override void WindowGUI(int windowID)
GuiUtils.SimpleTextBox("Booster Pitch end:", path.pitchEndTime, "s");
GUILayout.Label(String.Format("ending pitch: {0:F1}°", 90.0 - (path.pitchEndTime - path.pitchStartTime)*path.pitchRate));
GuiUtils.SimpleTextBox("Terminal Guidance Period:", path.terminalGuidanceSecs, "s");
GuiUtils.SimpleTextBox("Num Stages:", path.num_stages);
GuiUtils.SimpleTextBox("Num Stages:", path.edit_num_stages);
GUILayout.Label("Stage Stats");
if (GUILayout.Button("Reinitialize Stage Analysis"))
path.InitStageStats();
Expand Down

0 comments on commit 1d933fa

Please sign in to comment.