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

Add PID controller implementation #12643

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Empty file added control_algorithms/__init__.py
Empty file.
57 changes: 57 additions & 0 deletions control_algorithms/pid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""

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

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 1:1.
tokenizer error: no matching outer block for dedent

"""
^

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):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

"""
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:

Choose a reason for hiding this comment

The 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 control_algorithms/pid.py, please provide doctest for the function compute

Choose a reason for hiding this comment

The 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 control_algorithms/pid.py, please provide doctest for the function compute

Choose a reason for hiding this comment

The 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 control_algorithms/pid.py, please provide doctest for the function compute

Choose a reason for hiding this comment

The 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 control_algorithms/pid.py, please provide doctest for the function compute

"""
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):

Choose a reason for hiding this comment

The 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 control_algorithms/pid.py, please provide doctest for the function reset

Please provide return type hint for the function: reset. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The 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 control_algorithms/pid.py, please provide doctest for the function reset

Please provide return type hint for the function: reset. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The 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 control_algorithms/pid.py, please provide doctest for the function reset

Please provide return type hint for the function: reset. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The 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 control_algorithms/pid.py, please provide doctest for the function reset

Please provide return type hint for the function: reset. If the function does not return a value, please provide the type hint as: def function() -> None:

"""Reset the integral and previous error values."""
self.integral = 0.0
self.previous_error = 0.0


if __name__ == "__main__":
import doctest

doctest.testmod()