Skip to content

Commit

Permalink
[wombat/utils] fix pid handling of negative numbers (CurtinFRC#122)
Browse files Browse the repository at this point in the history
* merged with silly stuff

* [wombat/utils] fix pid handling of negative numbers

* Revert "merged with silly stuff"

This reverts commit d0cc0b0.
  • Loading branch information
spacey-sooty authored and Superbro525Alt committed Feb 12, 2024
1 parent 18d76b0 commit 5e9957b
Showing 1 changed file with 8 additions and 0 deletions.
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

0 comments on commit 5e9957b

Please sign in to comment.