Skip to content

Commit

Permalink
Modify update structure
Browse files Browse the repository at this point in the history
SteeringManager.cs
* Use UpdateHandler.CurrentFixedTime instead of planetarium time
(smaller value, less chance for floating point errors)
* Update the state vectors and display vectors if SAS is enabled
* Hide angular velocity, rcs, and engine thrust display vectors when SAS
is enabled.
* Don't throw an exception when getting target direction, throwing an
exception won't trigger the kOS logger.  Instead, log the exception
directly.
PIDLoop.cs
* Update integral/derivative logic to use the previous dTerm and iTerm
if dt is not greater than 0.
  • Loading branch information
hvacengi committed Apr 17, 2016
1 parent d8cb660 commit aafb646
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
42 changes: 22 additions & 20 deletions src/kOS.Safe/Encapsulation/PIDLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,35 +132,37 @@ public ScalarValue Update(ScalarValue sampleTime, ScalarValue input)
if (LastSampleTime < sampleTime)
{
double dt = sampleTime - LastSampleTime;
if (dt < 1)
if (Ki != 0)
{
if (Ki != 0)
if (ExtraUnwind)
{
if (ExtraUnwind)
if (Math.Sign(error) != Math.Sign(ErrorSum))
{
if (Math.Sign(error) != Math.Sign(ErrorSum))
if (!unWinding)
{
if (!unWinding)
{
Ki *= 2;
unWinding = true;
}
}
else if (unWinding)
{
Ki /= 2;
unWinding = false;
Ki *= 2;
unWinding = true;
}
}
iTerm = ITerm + error * dt * Ki;
}
ChangeRate = (input - Input) / dt;
if (Kd != 0)
{
dTerm = -ChangeRate * Kd;
else if (unWinding)
{
Ki /= 2;
unWinding = false;
}
}
iTerm = ITerm + error * dt * Ki;
}
ChangeRate = (input - Input) / dt;
if (Kd != 0)
{
dTerm = -ChangeRate * Kd;
}
}
else
{
dTerm = DTerm;
iTerm = ITerm;
}
Output = pTerm + iTerm + dTerm;
if (Output > MaxOutput)
{
Expand Down
16 changes: 10 additions & 6 deletions src/kOS/Binding/SteeringManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,16 @@ private void Update(FlightCtrlState c)
sw.Reset();
sw.Start();
lastSessionTime = sessionTime;
sessionTime = Math.Round(Planetarium.GetUniversalTime(), 3);
sessionTime = shared.UpdateHandler.CurrentFixedTime;
if (sessionTime - lastSessionTime > 1) ResetIs();
//if (sessionTime > lastSessionTime)
//{
//}
if (shared.Vessel.ActionGroups[KSPActionGroup.SAS])
{
UpdateStateVectors();
UpdateControl(c);
UpdateVectorRenders();
}
else
{
Expand Down Expand Up @@ -491,8 +494,9 @@ private Direction GetDirectionFromValue()
}
}
DisableControl();
throw new Safe.Exceptions.KOSWrongControlValueTypeException(
"STEERING", Value.GetType().Name, "Direction, Vector, Maneuver Node, or special string \"KILL\"");
SafeHouse.Logger.LogException(new Safe.Exceptions.KOSWrongControlValueTypeException(
"STEERING", Value.GetType().Name, "Direction, Vector, Maneuver Node, or special string \"KILL\""));
return new Direction(vesselRotation);
}

public void UpdateStateVectors()
Expand Down Expand Up @@ -1130,7 +1134,7 @@ public void UpdateVectorRenders()
}
}

if (ShowAngularVectors && enabled)
if (ShowAngularVectors && enabled && !Vessel.ActionGroups[KSPActionGroup.SAS])
{
if (vOmegaX == null)
{
Expand Down Expand Up @@ -1224,7 +1228,7 @@ public void UpdateVectorRenders()
}
}

if (ShowThrustVectors && enabled)
if (ShowThrustVectors && enabled && !Vessel.ActionGroups[KSPActionGroup.SAS])
{
foreach (var fv in engineNeutVectors)
{
Expand Down Expand Up @@ -1289,7 +1293,7 @@ public void UpdateVectorRenders()
vEngines.Clear();
}

if (ShowRCSVectors && enabled && Vessel.ActionGroups[KSPActionGroup.RCS])
if (ShowRCSVectors && enabled && Vessel.ActionGroups[KSPActionGroup.RCS] && !Vessel.ActionGroups[KSPActionGroup.SAS])
{
foreach (var force in rcsVectors)
{
Expand Down

0 comments on commit aafb646

Please sign in to comment.