Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pidloop improvements #2922

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/kOS.Safe/Encapsulation/PIDLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public static PIDLoop DeepCopy(PIDLoop source)
DTerm = source.DTerm,
ExtraUnwind = source.ExtraUnwind,
ChangeRate = source.ChangeRate,
lastIError = source.lastIError,
unWinding = source.unWinding
};
return newLoop;
Expand Down Expand Up @@ -119,6 +120,7 @@ public static PIDLoop DeepCopy(PIDLoop source)

public double ChangeRate { get; set; }

private double lastIError;
private bool unWinding;

public PIDLoop()
Expand Down Expand Up @@ -198,7 +200,11 @@ public ScalarValue Update(ScalarValue sampleTime, ScalarValue input)
double pTerm = error * Kp;
double iTerm = 0;
double dTerm = 0;
if (LastSampleTime < sampleTime)
if (LastSampleTime == double.MaxValue)
{
lastIError = error * Ki;
}
else if (LastSampleTime < sampleTime)
{
double dt = sampleTime - LastSampleTime;
if (Ki != 0)
Expand All @@ -219,7 +225,8 @@ public ScalarValue Update(ScalarValue sampleTime, ScalarValue input)
unWinding = false;
}
}
iTerm = ITerm + error * dt * Ki;
iTerm = ITerm + lastIError * dt;
lastIError = error * Ki;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why is it supposed to be better to time-shift the I term one iteration like this so it's using the previous error value instead of the current one from this iteration? The issue #2919 only has one sentence in the description which doesn't explain why it's desired to delay it like this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added some explanations to PR comments, but I shouldn't have changed the integration method, so I'll revert this.

}
ChangeRate = (input - Input) / dt;
if (Kd != 0)
Expand Down