-
-
Notifications
You must be signed in to change notification settings - Fork 46.5k
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
Add PID controller implementation #12643
base: master
Are you sure you want to change the base?
Changes from all commits
745bd2b
588538e
7708d3c
64aaaa3
dfb637b
8b6dc3d
1a90c8e
12d7359
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
A Proportional-Integral-Derivative (PID) controller | ||
is a control loop mechanism that calculates an error | ||
value as the difference between a desired setpoint | ||
and a measured process variable. | ||
|
||
It applies proportional, integral, and derivative | ||
corrections to minimize the error over time. | ||
|
||
Refer - https://en.wikipedia.org/wiki/PID_controller | ||
""" | ||
|
||
|
||
class PID: | ||
def __init__(self, kp: float, ki: float, kd: float, setpoint: float = 0.0): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
""" | ||
Initialize the PID controller. | ||
|
||
:param Kp: Proportional gain | ||
:param Ki: Integral gain | ||
:param Kd: Derivative gain | ||
:param setpoint: Desired target value | ||
""" | ||
self.kp = kp | ||
self.ki = ki | ||
self.kd = kd | ||
self.setpoint = setpoint | ||
|
||
self.integral = 0.0 | ||
self.previous_error = 0.0 | ||
|
||
def compute(self, measured_value: float, dt: float) -> float: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
""" | ||
Compute the control signal based on the error. | ||
|
||
:param measured_value: The current process variable | ||
:param dt: Time difference since the last update | ||
:return: Control output | ||
""" | ||
error = self.setpoint - measured_value | ||
self.integral += error * dt if error != 0 else 0.0 | ||
derivative = (error - self.previous_error) / dt if dt > 0 else 0.0 | ||
|
||
output = (self.kp * error) + (self.ki * self.integral) + (self.kd * derivative) | ||
self.previous_error = error | ||
return output | ||
|
||
def reset(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: |
||
"""Reset the integral and previous error values.""" | ||
self.integral = 0.0 | ||
self.previous_error = 0.0 | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error occurred while parsing the file:
control_algorithms/pid.py