1
1
"""
2
- A Proportional-Integral-Derivative (PID) controller is a control loop mechanism that calculates an error
3
- value as the difference between a desired setpoint and a measured process variable.
2
+ A Proportional-Integral-Derivative (PID) controller
3
+ is a control loop mechanism that calculates an error
4
+ value as the difference between a desired setpoint
5
+ and a measured process variable.
4
6
5
- It applies proportional, integral, and derivative corrections to minimize the error over time.
7
+ It applies proportional, integral, and derivative
8
+ corrections to minimize the error over time.
6
9
7
10
Refer - https://en.wikipedia.org/wiki/PID_controller
8
11
"""
9
12
10
13
11
- class pid :
14
+ class PID :
12
15
13
- def __init__ (self , Kp : float , Ki : float , Kd : float , setpoint : float = 0 ):
16
+ def __init__ (self , kp : float , ki : float , kd : float , setpoint : float = 0 ):
14
17
"""
15
18
Initialize the PID controller.
16
19
@@ -19,9 +22,9 @@ def __init__(self, Kp: float, Ki: float, Kd: float, setpoint: float = 0):
19
22
:param Kd: Derivative gain
20
23
:param setpoint: Desired target value
21
24
"""
22
- self .Kp = Kp
23
- self .Ki = Ki
24
- self .Kd = Kd
25
+ self .kp = kp
26
+ self .ki = ki
27
+ self .kd = kd
25
28
self .setpoint = setpoint
26
29
27
30
self .integral = 0
@@ -39,14 +42,14 @@ def compute(self, measured_value: float, dt: float) -> float:
39
42
self .integral += error * dt if error != 0 else 0
40
43
derivative = (error - self .previous_error ) / dt if dt > 0 else 0
41
44
42
- output = (self .Kp * error ) + (self .Ki * self .integral ) + (self .Kd * derivative )
45
+ output = (self .kp * error ) + (self .ki * self .integral ) + (self .kd * derivative )
43
46
self .previous_error = error
44
47
return output
45
48
46
49
def reset (self ):
47
50
"""Reset the integral and previous error values."""
48
- self .integral = 0
49
- self .previous_error = 0
51
+ self .integral = 0.0
52
+ self .previous_error = 0.0
50
53
51
54
52
55
if __name__ == "__main__" :
0 commit comments