Skip to content

Commit

Permalink
Landing autopilot style cleanup
Browse files Browse the repository at this point in the history
This may be breaking due to changing methods to private and changing
the name of public methods.

If anything has been made private that is actually getting used just
drop in a bug report to revert it and it can be made public with a
[UsedImplicitly] added to make IDEs happy.  It isn't intended to
force people to beat on it with reflection calls to private APIs.

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jun 18, 2023
1 parent 681d58f commit 21844b9
Show file tree
Hide file tree
Showing 12 changed files with 709 additions and 736 deletions.
62 changes: 29 additions & 33 deletions MechJeb2/AutopilotModule.cs
Expand Up @@ -5,77 +5,73 @@ namespace MuMech
{
public class AutopilotModule : ComputerModule
{
public AutopilotModule(MechJebCore core) : base(core)
protected AutopilotModule(MechJebCore core) : base(core)
{
}

public override void Drive(FlightCtrlState s)
{
if (CurrentStep != null)
if (CurrentStep == null) return;

try
{
CurrentStep = CurrentStep.Drive(s);
}
catch (Exception ex)
{
try
{
CurrentStep = CurrentStep.Drive(s);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
Debug.LogException(ex);
}
}

public override void OnFixedUpdate()
{
if (CurrentStep != null)
if (CurrentStep == null) return;

try
{
CurrentStep = CurrentStep.OnFixedUpdate();
}
catch (Exception ex)
{
try
{
CurrentStep = CurrentStep.OnFixedUpdate();
}
catch (Exception ex)
{
Debug.LogException(ex);
}
Debug.LogException(ex);
}
}

public void setStep(AutopilotStep step)
protected void SetStep(AutopilotStep step)
{
CurrentStep = step;
}

public string status
public string Status
{
get
{
if (CurrentStep == null)
return "Off";
return CurrentStep.status;
return CurrentStep == null ? "Off" : CurrentStep.Status;
}
}

public bool active => CurrentStep != null;
protected bool Active => CurrentStep != null;

public AutopilotStep CurrentStep { get; private set; }
}

public class AutopilotStep
{
public MechJebCore core;
protected readonly MechJebCore Core;

//conveniences:
public VesselState vesselState => core.vesselState;
public Vessel vessel => core.part.vessel;
public CelestialBody mainBody => core.part.vessel.mainBody;
public Orbit orbit => core.part.vessel.orbit;
protected VesselState VesselState => Core.vesselState;
protected Vessel Vessel => Core.part.vessel;
protected CelestialBody MainBody => Core.part.vessel.mainBody;
protected Orbit Orbit => Core.part.vessel.orbit;

public AutopilotStep(MechJebCore core)
protected AutopilotStep(MechJebCore core)
{
this.core = core;
this.Core = core;
}

public virtual AutopilotStep Drive(FlightCtrlState s) { return this; }
public virtual AutopilotStep OnFixedUpdate() { return this; }
public string status { get; protected set; }
public string Status { get; protected set; }
}
}
105 changes: 52 additions & 53 deletions MechJeb2/LandingAutopilot/CoastToDeceleration.cs
Expand Up @@ -15,116 +15,115 @@ public CoastToDeceleration(MechJebCore core) : base(core)

public override AutopilotStep Drive(FlightCtrlState s)
{
if (!core.landing.PredictionReady)
if (!Core.landing.PredictionReady)
return this;

Vector3d deltaV = core.landing.ComputeCourseCorrection(true);
Vector3d deltaV = Core.landing.ComputeCourseCorrection(true);

if (core.landing.rcsAdjustment)
{
if (deltaV.magnitude > 3)
core.rcs.enabled = true;
else if (deltaV.magnitude < 0.01)
core.rcs.enabled = false;
if (!Core.landing.RCSAdjustment) return this;

if (core.rcs.enabled)
core.rcs.SetWorldVelocityError(deltaV);
}
if (deltaV.magnitude > 3)
Core.rcs.enabled = true;
else if (deltaV.magnitude < 0.01)
Core.rcs.enabled = false;

if (Core.rcs.enabled)
Core.rcs.SetWorldVelocityError(deltaV);

return this;
}

private bool warpReady;
private bool _warpReady;

public override AutopilotStep OnFixedUpdate()
{
core.thrust.targetThrottle = 0;
Core.thrust.targetThrottle = 0;

// If the atmospheric drag is has started to act on the vessel then we are in a position to start considering when to deploy the parachutes.
if (core.landing.deployChutes)
if (Core.landing.DeployChutes)
{
if (core.landing.ParachutesDeployable())
if (Core.landing.ParachutesDeployable())
{
core.landing.ControlParachutes();
Core.landing.ControlParachutes();
}
}

double maxAllowedSpeed = core.landing.MaxAllowedSpeed();
if (vesselState.speedSurface > 0.9 * maxAllowedSpeed)
double maxAllowedSpeed = Core.landing.MaxAllowedSpeed();
if (VesselState.speedSurface > 0.9 * maxAllowedSpeed)
{
core.warp.MinimumWarp();
if (core.landing.rcsAdjustment)
core.rcs.enabled = false;
return new DecelerationBurn(core);
Core.warp.MinimumWarp();
if (Core.landing.RCSAdjustment)
Core.rcs.enabled = false;
return new DecelerationBurn(Core);
}

status = Localizer.Format("#MechJeb_LandingGuidance_Status1"); //"Coasting toward deceleration burn"
Status = Localizer.Format("#MechJeb_LandingGuidance_Status1"); //"Coasting toward deceleration burn"

if (core.landing.landAtTarget)
if (Core.landing.LandAtTarget)
{
double currentError = Vector3d.Distance(core.target.GetPositionTargetPosition(), core.landing.LandingSite);
double currentError = Vector3d.Distance(Core.target.GetPositionTargetPosition(), Core.landing.LandingSite);
if (currentError > 1000)
{
if (!vesselState.parachuteDeployed &&
vesselState.drag <=
if (!VesselState.parachuteDeployed &&
VesselState.drag <=
0.1) // However if there is already a parachute deployed or drag is high, then do not bother trying to correct the course as we will not have any attitude control anyway.
{
core.warp.MinimumWarp();
if (core.landing.rcsAdjustment)
core.rcs.enabled = false;
return new CourseCorrection(core);
Core.warp.MinimumWarp();
if (Core.landing.RCSAdjustment)
Core.rcs.enabled = false;
return new CourseCorrection(Core);
}
}
else
{
Vector3d deltaV = core.landing.ComputeCourseCorrection(true);
status += "\n" + Localizer.Format("#MechJeb_LandingGuidance_Status2",
Vector3d deltaV = Core.landing.ComputeCourseCorrection(true);
Status += "\n" + Localizer.Format("#MechJeb_LandingGuidance_Status2",
deltaV.magnitude.ToString("F3")); //"Course correction DV: " + + " m/s"
}
}

// If we're already low, skip directly to the Deceleration burn
if (vesselState.altitudeASL < core.landing.DecelerationEndAltitude() + 5)
if (VesselState.altitudeASL < Core.landing.DecelerationEndAltitude() + 5)
{
core.warp.MinimumWarp();
if (core.landing.rcsAdjustment)
core.rcs.enabled = false;
return new DecelerationBurn(core);
Core.warp.MinimumWarp();
if (Core.landing.RCSAdjustment)
Core.rcs.enabled = false;
return new DecelerationBurn(Core);
}

if (core.attitude.attitudeAngleFromTarget() < 1) { warpReady = true; } // less warp start warp stop jumping
if (Core.attitude.attitudeAngleFromTarget() < 1) { _warpReady = true; } // less warp start warp stop jumping

if (core.attitude.attitudeAngleFromTarget() > 5) { warpReady = false; } // hopefully
if (Core.attitude.attitudeAngleFromTarget() > 5) { _warpReady = false; } // hopefully

if (core.landing.PredictionReady)
if (Core.landing.PredictionReady)
{
if (vesselState.drag < 0.01)
if (VesselState.drag < 0.01)
{
double decelerationStartTime = core.landing.Prediction.Trajectory.Any()
? core.landing.Prediction.Trajectory.First().UT
: vesselState.time;
Vector3d decelerationStartAttitude = -orbit.WorldOrbitalVelocityAtUT(decelerationStartTime);
decelerationStartAttitude += mainBody.getRFrmVel(orbit.WorldPositionAtUT(decelerationStartTime));
double decelerationStartTime = Core.landing.Prediction.Trajectory.Any()
? Core.landing.Prediction.Trajectory.First().UT
: VesselState.time;
Vector3d decelerationStartAttitude = -Orbit.WorldOrbitalVelocityAtUT(decelerationStartTime);
decelerationStartAttitude += MainBody.getRFrmVel(Orbit.WorldPositionAtUT(decelerationStartTime));
decelerationStartAttitude = decelerationStartAttitude.normalized;
core.attitude.attitudeTo(decelerationStartAttitude, AttitudeReference.INERTIAL, core.landing);
Core.attitude.attitudeTo(decelerationStartAttitude, AttitudeReference.INERTIAL, Core.landing);
}
else
{
core.attitude.attitudeTo(Vector3.back, AttitudeReference.SURFACE_VELOCITY, core.landing);
Core.attitude.attitudeTo(Vector3.back, AttitudeReference.SURFACE_VELOCITY, Core.landing);
}
}

//Warp at a rate no higher than the rate that would have us impacting the ground 10 seconds from now:
if (warpReady && core.node.autowarp)
if (_warpReady && Core.node.autowarp)
{
// Make sure if we're hovering that we don't go straight into too fast of a warp
// (g * 5 is average velocity falling for 10 seconds from a hover)
double velocityGuess = Math.Max(Math.Abs(vesselState.speedVertical), vesselState.localg * 5);
core.warp.WarpRegularAtRate((float)(vesselState.altitudeASL / (10 * velocityGuess)));
double velocityGuess = Math.Max(Math.Abs(VesselState.speedVertical), VesselState.localg * 5);
Core.warp.WarpRegularAtRate((float)(VesselState.altitudeASL / (10 * velocityGuess)));
}
else
{
core.warp.MinimumWarp();
Core.warp.MinimumWarp();
}

return this;
Expand Down
56 changes: 28 additions & 28 deletions MechJeb2/LandingAutopilot/CourseCorrection.cs
Expand Up @@ -6,76 +6,76 @@ namespace Landing
{
public class CourseCorrection : AutopilotStep
{
private bool courseCorrectionBurning;
private bool _courseCorrectionBurning;

public CourseCorrection(MechJebCore core) : base(core)
{
}

public override AutopilotStep Drive(FlightCtrlState s)
{
if (!core.landing.PredictionReady)
if (!Core.landing.PredictionReady)
return this;

// If the atomospheric drag is at least 100mm/s2 then start trying to target the overshoot using the parachutes
if (core.landing.deployChutes)
if (Core.landing.DeployChutes)
{
if (core.landing.ParachutesDeployable())
if (Core.landing.ParachutesDeployable())
{
core.landing.ControlParachutes();
Core.landing.ControlParachutes();
}
}

double currentError = Vector3d.Distance(core.target.GetPositionTargetPosition(), core.landing.LandingSite);
double currentError = Vector3d.Distance(Core.target.GetPositionTargetPosition(), Core.landing.LandingSite);

if (currentError < 150)
{
core.thrust.targetThrottle = 0;
if (core.landing.rcsAdjustment)
core.rcs.enabled = true;
return new CoastToDeceleration(core);
Core.thrust.targetThrottle = 0;
if (Core.landing.RCSAdjustment)
Core.rcs.enabled = true;
return new CoastToDeceleration(Core);
}

// If we're off course, but already too low, skip the course correction
if (vesselState.altitudeASL < core.landing.DecelerationEndAltitude() + 5)
if (VesselState.altitudeASL < Core.landing.DecelerationEndAltitude() + 5)
{
return new DecelerationBurn(core);
return new DecelerationBurn(Core);
}


// If a parachute has already been deployed then we will not be able to control attitude anyway, so move back to the coast to deceleration step.
if (vesselState.parachuteDeployed)
if (VesselState.parachuteDeployed)
{
core.thrust.targetThrottle = 0;
return new CoastToDeceleration(core);
Core.thrust.targetThrottle = 0;
return new CoastToDeceleration(Core);
}

// We are not in .90 anymore. Turning while under drag is a bad idea
if (vesselState.drag > 0.1)
if (VesselState.drag > 0.1)
{
return new CoastToDeceleration(core);
return new CoastToDeceleration(Core);
}

Vector3d deltaV = core.landing.ComputeCourseCorrection(true);
Vector3d deltaV = Core.landing.ComputeCourseCorrection(true);

status = Localizer.Format("#MechJeb_LandingGuidance_Status3",
Status = Localizer.Format("#MechJeb_LandingGuidance_Status3",
deltaV.magnitude.ToString("F1")); //"Performing course correction of about " + + " m/s"

core.attitude.attitudeTo(deltaV.normalized, AttitudeReference.INERTIAL, core.landing);
Core.attitude.attitudeTo(deltaV.normalized, AttitudeReference.INERTIAL, Core.landing);

if (core.attitude.attitudeAngleFromTarget() < 2)
courseCorrectionBurning = true;
else if (core.attitude.attitudeAngleFromTarget() > 30)
courseCorrectionBurning = false;
if (Core.attitude.attitudeAngleFromTarget() < 2)
_courseCorrectionBurning = true;
else if (Core.attitude.attitudeAngleFromTarget() > 30)
_courseCorrectionBurning = false;

if (courseCorrectionBurning)
if (_courseCorrectionBurning)
{
const double timeConstant = 2.0;
core.thrust.ThrustForDV(deltaV.magnitude, timeConstant);
const double TIME_CONSTANT = 2.0;
Core.thrust.ThrustForDV(deltaV.magnitude, TIME_CONSTANT);
}
else
{
core.thrust.targetThrottle = 0;
Core.thrust.targetThrottle = 0;
}

return this;
Expand Down

0 comments on commit 21844b9

Please sign in to comment.