Skip to content

Commit

Permalink
Multiple PVG fixes
Browse files Browse the repository at this point in the history
1. don't do TERMINAL_RCS in anything other than the last stage
2. end the TERMINAL state if there's no thrust
3. skip the current stage in the stage dV iterations in the glueball
   if it is coasting and is coast after (not coast before).

plus a bit of mass costate removal

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Feb 29, 2024
1 parent 0a5f2b8 commit 8b1fb75
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
13 changes: 12 additions & 1 deletion MechJeb2/MechJebModuleGuidanceController.cs
Expand Up @@ -115,7 +115,7 @@ private bool WillDoRCSButNotYet()
bool hasRCS = Vessel.hasEnabledRCSModules() &&
VesselState.rcsThrustAvailable.Up > 0.1 * VesselState.rcsThrustAvailable.MaxMagnitude();

return hasRCS && Status != PVGStatus.TERMINAL_RCS;
return hasRCS && Status != PVGStatus.TERMINAL_RCS && Vessel.currentStage == _ascentSettings.LastStage;
}

private void HandleTerminal()
Expand Down Expand Up @@ -200,7 +200,18 @@ private void HandleTerminal()
Vector3d v1 = VesselState.orbitalVelocity + a0 * dt;
Vector3d x1 = VesselState.orbitalPosition + VesselState.orbitalVelocity * dt + 0.5 * a0 * dt * dt;

bool shouldEndTerminal = false;

if (Status == PVGStatus.TERMINAL && VesselState.thrustCurrent == 0)
{
Debug.Log("[MechJebModuleGuidanceController] no thrust in TERMINAL state.");
shouldEndTerminal = true;
}

if (Solution.TerminalGuidanceSatisfied(x1.WorldToV3Rotated(), v1.WorldToV3Rotated(), solutionIndex))
shouldEndTerminal = true;

if (shouldEndTerminal)
{
if (WillDoRCSButNotYet())
{
Expand Down
12 changes: 12 additions & 0 deletions MechJeb2/MechJebModulePVGGlueBall.cs
Expand Up @@ -140,6 +140,10 @@ public void SetTarget(double peR, double apR, double attR, double inclination, d
double dv = Core.StageStats.VacStats[mjPhase].DeltaV;
int kspStage = Core.StageStats.VacStats[mjPhase].KSPStage;

// skip the current stage if we are doing a coast after it
if (IsCurrentCoastAfterStage(kspStage))
continue;

// skip the zero length stages
if (dv == 0)
continue;
Expand Down Expand Up @@ -242,5 +246,13 @@ public void SetTarget(double peR, double apR, double attR, double inclination, d

_blockOptimizerUntilTime = VesselState.time + 1;
}

private bool IsCurrentCoastAfterStage(int kspStage)
{
if (kspStage == Vessel.currentStage && Core.Guidance.IsCoasting() && !_ascentSettings.CoastBeforeFlag)
return true;

return false;
}
}
}
9 changes: 3 additions & 6 deletions MechJebLib/PVG/ResidualLayout.cs
Expand Up @@ -10,15 +10,14 @@ namespace MechJebLib.PVG
{
public class ResidualLayout
{
public const int RESIDUAL_LAYOUT_LEN = 15;
public const int RESIDUAL_LAYOUT_LEN = 14;

public V3 R;
public V3 V;
private V3 _terminal0;
private V3 _terminal1;
public double M;
public double Bt;
public double Pm;

public (double a, double b, double c, double d, double e, double f) Terminal
{
Expand All @@ -44,8 +43,7 @@ public void CopyTo(IList<double> other)
other[10] = _terminal1[0];
other[11] = _terminal1[1];
other[12] = _terminal1[2];
other[13] = Pm;
other[14] = Bt;
other[13] = Bt;
}

public void CopyFrom(IList<double> other)
Expand All @@ -55,8 +53,7 @@ public void CopyFrom(IList<double> other)
M = other[6];
_terminal0 = new V3(other[7], other[8], other[9]);
_terminal1 = new V3(other[10], other[11], other[12]);
Pm = other[13];
Bt = other[14];
Bt = other[13];
}

public static ResidualLayout CreateFrom(IList<double> other)
Expand Down

0 comments on commit 8b1fb75

Please sign in to comment.