From 745bd2bff57c1762cbbc1b53ae702ec587e4b8b3 Mon Sep 17 00:00:00 2001 From: TahmidSwad Date: Fri, 28 Mar 2025 09:07:28 +0600 Subject: [PATCH 1/7] Add PID controller implementation --- control_algorithms/pid.py | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 control_algorithms/pid.py diff --git a/control_algorithms/pid.py b/control_algorithms/pid.py new file mode 100644 index 000000000000..5ac2ab6a77a5 --- /dev/null +++ b/control_algorithms/pid.py @@ -0,0 +1,55 @@ +""" +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): + """ + 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 + self.previous_error = 0 + + def compute(self, measured_value: float, dt: float) -> float: + """ + 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 + derivative = (error - self.previous_error) / dt if dt > 0 else 0 + + output = (self.Kp * error) + (self.Ki * self.integral) + (self.Kd * derivative) + self.previous_error = error + return output + + def reset(self): + """Reset the integral and previous error values.""" + self.integral = 0 + self.previous_error = 0 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 588538eaddcee6fa4733a1775487123c0ed09a43 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Mar 2025 03:26:36 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- control_algorithms/pid.py | 1 - 1 file changed, 1 deletion(-) diff --git a/control_algorithms/pid.py b/control_algorithms/pid.py index 5ac2ab6a77a5..36974dbfc125 100644 --- a/control_algorithms/pid.py +++ b/control_algorithms/pid.py @@ -9,7 +9,6 @@ class pid: - def __init__(self, Kp: float, Ki: float, Kd: float, setpoint: float = 0): """ Initialize the PID controller. From 7708d3c08d8e797d5f99439245eb4ade8a26c6ee Mon Sep 17 00:00:00 2001 From: TahmidSwad Date: Fri, 28 Mar 2025 09:35:10 +0600 Subject: [PATCH 3/7] Add PID controller implementation --- control_algorithms/__init__.py | 0 control_algorithms/pid.py | 25 ++++++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 control_algorithms/__init__.py diff --git a/control_algorithms/__init__.py b/control_algorithms/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/control_algorithms/pid.py b/control_algorithms/pid.py index 5ac2ab6a77a5..d043c0c76686 100644 --- a/control_algorithms/pid.py +++ b/control_algorithms/pid.py @@ -1,16 +1,19 @@ """ -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. +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. +It applies proportional, integral, and derivative +corrections to minimize the error over time. Refer - https://en.wikipedia.org/wiki/PID_controller """ -class pid: +class PID: - def __init__(self, Kp: float, Ki: float, Kd: float, setpoint: float = 0): + def __init__(self, kp: float, ki: float, kd: float, setpoint: float = 0): """ Initialize the PID controller. @@ -19,9 +22,9 @@ def __init__(self, Kp: float, Ki: float, Kd: float, setpoint: float = 0): :param Kd: Derivative gain :param setpoint: Desired target value """ - self.Kp = Kp - self.Ki = Ki - self.Kd = Kd + self.kp = kp + self.ki = ki + self.kd = kd self.setpoint = setpoint self.integral = 0 @@ -39,14 +42,14 @@ def compute(self, measured_value: float, dt: float) -> float: self.integral += error * dt if error != 0 else 0 derivative = (error - self.previous_error) / dt if dt > 0 else 0 - output = (self.Kp * error) + (self.Ki * self.integral) + (self.Kd * derivative) + output = (self.kp * error) + (self.ki * self.integral) + (self.kd * derivative) self.previous_error = error return output def reset(self): """Reset the integral and previous error values.""" - self.integral = 0 - self.previous_error = 0 + self.integral = 0.0 + self.previous_error = 0.0 if __name__ == "__main__": From dfb637b3310f71716e008ced4489e24d9bdb5dd5 Mon Sep 17 00:00:00 2001 From: TahmidSwad Date: Fri, 28 Mar 2025 09:44:32 +0600 Subject: [PATCH 4/7] Add PID controller implementation --- control_algorithms/pid.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/control_algorithms/pid.py b/control_algorithms/pid.py index 7947ae9cf756..a90d9cf473c2 100644 --- a/control_algorithms/pid.py +++ b/control_algorithms/pid.py @@ -11,14 +11,8 @@ """ -<<<<<<< HEAD -class PID: - - def __init__(self, kp: float, ki: float, kd: float, setpoint: float = 0): -======= class pid: def __init__(self, Kp: float, Ki: float, Kd: float, setpoint: float = 0): ->>>>>>> 588538eaddcee6fa4733a1775487123c0ed09a43 """ Initialize the PID controller. From 8b6dc3d1e37d36fb89f0ede4316da739196ec65f Mon Sep 17 00:00:00 2001 From: TahmidSwad Date: Fri, 28 Mar 2025 09:48:45 +0600 Subject: [PATCH 5/7] Add PID controller implementation --- control_algorithms/pid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/control_algorithms/pid.py b/control_algorithms/pid.py index a90d9cf473c2..8cf9ffa2bf89 100644 --- a/control_algorithms/pid.py +++ b/control_algorithms/pid.py @@ -11,7 +11,7 @@ """ -class pid: +class PID: def __init__(self, Kp: float, Ki: float, Kd: float, setpoint: float = 0): """ Initialize the PID controller. From 1a90c8e420aaadccc86f16e109e42cf9121a40ff Mon Sep 17 00:00:00 2001 From: TahmidSwad Date: Fri, 28 Mar 2025 09:50:29 +0600 Subject: [PATCH 6/7] Add PID controller implementation --- control_algorithms/pid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/control_algorithms/pid.py b/control_algorithms/pid.py index 8cf9ffa2bf89..e7229b8baa18 100644 --- a/control_algorithms/pid.py +++ b/control_algorithms/pid.py @@ -12,7 +12,7 @@ class PID: - def __init__(self, Kp: float, Ki: float, Kd: float, setpoint: float = 0): + def __init__(self, kp: float, ki: float, kd: float, setpoint: float = 0): """ Initialize the PID controller. From 12d7359aace0b73c6ef0cd32cdc359dcd1b1ec3f Mon Sep 17 00:00:00 2001 From: TahmidSwad Date: Fri, 28 Mar 2025 09:55:22 +0600 Subject: [PATCH 7/7] Add PID controller implementation --- control_algorithms/pid.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/control_algorithms/pid.py b/control_algorithms/pid.py index e7229b8baa18..2ca26e90fe47 100644 --- a/control_algorithms/pid.py +++ b/control_algorithms/pid.py @@ -12,7 +12,7 @@ class PID: - def __init__(self, kp: float, ki: float, kd: float, setpoint: float = 0): + def __init__(self, kp: float, ki: float, kd: float, setpoint: float = 0.0): """ Initialize the PID controller. @@ -26,8 +26,8 @@ def __init__(self, kp: float, ki: float, kd: float, setpoint: float = 0): self.kd = kd self.setpoint = setpoint - self.integral = 0 - self.previous_error = 0 + self.integral = 0.0 + self.previous_error = 0.0 def compute(self, measured_value: float, dt: float) -> float: """ @@ -38,8 +38,8 @@ def compute(self, measured_value: float, dt: float) -> float: :return: Control output """ error = self.setpoint - measured_value - self.integral += error * dt if error != 0 else 0 - derivative = (error - self.previous_error) / dt if dt > 0 else 0 + 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