Skip to content

Commit

Permalink
Fix principia execution
Browse files Browse the repository at this point in the history
also back to not using center-of-thrust

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Oct 29, 2023
1 parent 437f551 commit 0d2025b
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions MechJeb2/MechJebModuleNodeExecutor.cs
Expand Up @@ -64,14 +64,14 @@ public string NextNodeCountdown()
public void ExecuteOneNode(object controller)
{
Users.Add(controller);
_mode = Mode.ONE_NODE;
_mode = Mode.ONE_NODE;
Init();
}

public void ExecuteAllNodes(object controller)
{
Users.Add(controller);
_mode = Mode.ALL_NODES;
_mode = Mode.ALL_NODES;
Init();
}

Expand All @@ -80,6 +80,7 @@ private void Init()
State = States.WARPALIGN;
_direction = Vector3d.zero;
_dvLeft = Vessel.patchedConicSolver.maneuverNodes[0].GetBurnVector(Orbit).magnitude;
Core.Thrust.ThrustOff();
}

public void Abort()
Expand Down Expand Up @@ -114,6 +115,7 @@ public enum States { WARPALIGN, LEAD, BURN, IDLE }

private double _dvLeft; // for Principia
private Vector3d _direction; // de-rotated world vector
private Vector3d _worldDirection => Planetarium.fetch.rotation * _direction;
private double _ignitionUT;
private static bool _isLoadedPrincipia => VesselState.isLoadedPrincipia;
private bool _hasNodes => Vessel.patchedConicSolver.maneuverNodes.Count > 0;
Expand Down Expand Up @@ -201,7 +203,7 @@ private void StateWarpAlign()

SetAttitude();

if (MuUtils.PhysicsRunning() ? AlignedAndSettled() : AngleFromDirection() < Deg2Rad(2))
if (MuUtils.PhysicsRunning() ? AlignedAndSettled() : AngleFromDirection() < Deg2Rad(10))
Core.Warp.WarpToUT(_ignitionUT - LeadTime);
else
Core.Warp.MinimumWarp();
Expand All @@ -211,10 +213,15 @@ private void StateLeadTime()
{
Core.Thrust.ThrustOff();

if (!MuUtils.PhysicsRunning()) Core.Warp.MinimumWarp();
if (!MuUtils.PhysicsRunning())
{
Core.Warp.MinimumWarp();
return;
}

SetAttitude();

// update _dvLeft here because we're out of warp and might be doing RCS
if (!_isLoadedPrincipia)
_dvLeft = Vessel.patchedConicSolver.maneuverNodes[0].GetBurnVector(Orbit).magnitude;
else
Expand All @@ -223,7 +230,11 @@ private void StateLeadTime()

private void StateBurn()
{
if (!MuUtils.PhysicsRunning()) Core.Warp.MinimumWarp();
if (!MuUtils.PhysicsRunning())
{
Core.Warp.MinimumWarp();
return;
}

SetAttitude();

Expand All @@ -244,8 +255,8 @@ private void StateBurn()

private void SetAttitude()
{
Core.Attitude.SetAxisControl(true,true, false);
Core.Attitude.attitudeTo(Planetarium.fetch.rotation * _direction, AttitudeReference.INERTIAL_COT, this);
Core.Attitude.SetAxisControl(true, true, false);
Core.Attitude.attitudeTo(_worldDirection, AttitudeReference.INERTIAL, this);
}

private bool ShouldTerminate()
Expand All @@ -263,14 +274,9 @@ private bool ShouldTerminate()
node.RemoveSelf();

if (_mode == Mode.ALL_NODES && Vessel.patchedConicSolver.maneuverNodes.Count > 0)
{
Core.Thrust.ThrustOff();
Init();
}
else
{
Abort();
}

return true;
}
Expand All @@ -282,15 +288,17 @@ private bool ShouldTerminate()

private double AngleFromNode()
{
Vector3d fwd = Quaternion.FromToRotation(VesselState.forward, VesselState.thrustForward) * VesselState.forward;
//Vector3d fwd = Quaternion.FromToRotation(VesselState.forward, VesselState.thrustForward) * VesselState.forward;
Vector3d fwd = VesselState.forward;
Vector3d dir = Vessel.patchedConicSolver.maneuverNodes[0].GetBurnVector(Orbit).normalized;
return SafeAcos(Vector3d.Dot(fwd, dir));
}

private double AngleFromDirection()
{
Vector3d fwd = Quaternion.FromToRotation(VesselState.forward, VesselState.thrustForward) * VesselState.forward;
Vector3d dir = (Planetarium.fetch.rotation * _direction).normalized;
//Vector3d fwd = Quaternion.FromToRotation(VesselState.forward, VesselState.thrustForward) * VesselState.forward;
Vector3d fwd = VesselState.forward;
Vector3d dir = _worldDirection.normalized;
return SafeAcos(Vector3d.Dot(fwd, dir));
}

Expand All @@ -306,7 +314,7 @@ private Vector3d NextDirection()
if (!_isLoadedPrincipia && _dvLeft < VesselState.minThrustAccel) return _direction;
}

return invRot * Vessel.patchedConicSolver.maneuverNodes[0].GetBurnVector(Orbit);
return invRot * Vessel.patchedConicSolver.maneuverNodes[0].GetBurnVector(Orbit).normalized;
}

private double CalculateIgnitionUT()
Expand All @@ -329,7 +337,7 @@ private void DecrementDvLeft()
// We also can't just use vesselState.currentThrustAccel because only engines are counted.
// NOTE: This *will* include acceleration from decouplers, which is pretty cool.
Vector3d dV = (Vessel.acceleration_immediate - Vessel.graviticAcceleration) * TimeWarp.fixedDeltaTime;
_dvLeft -= Vector3d.Dot(dV, _direction);
_dvLeft -= Vector3d.Dot(dV, _worldDirection);
}

// FIXME: this needs to work with RCSOnly
Expand Down

0 comments on commit 0d2025b

Please sign in to comment.