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

[wombat/utils] fix pid handling of negative numbers #122

Merged
merged 5 commits into from
Feb 4, 2024
Merged
Changes from all commits
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
8 changes: 8 additions & 0 deletions wombat/src/main/include/utils/PID.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ class PIDController {
void Reset() { _integralSum = sum_t{0}; }

out_t Calculate(in_t pv, units::second_t dt, out_t feedforward = out_t{0}) {
bool is_negative;
if (pv.value() < 0) {
is_negative = true;
pv = units::math::fabs(pv);
}
auto error = do_wrap(_setpoint - pv);
_integralSum += error * dt;
if (config.izone.value() > 0 && (error > config.izone || error < -config.izone))
Expand Down Expand Up @@ -130,6 +135,9 @@ class PIDController {
_last_pv = pv;
_last_error = error;
_iterations++;
if (is_negative) {
return out * -1;
}
return out;
}

Expand Down