-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid_controller.py
55 lines (45 loc) · 1.67 KB
/
pid_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from discrete_model import DiscreteModel
class PIDController(DiscreteModel):
"""
A discrete model that implements a PID controller.
"""
def __init__(
self,
world,
name: str,
dt: float = 0.1,
kp: float = 1.0,
ki: float = 1.0,
kd: float = 1.0,
setpoint: float = 1.0,
):
super().__init__(world, name, dt=dt)
# set the controller parameters
self.kp = kp
self.ki = ki
self.kd = kd
self.setpoint = setpoint
# initialize the error, its integral, and its derivative.
# We save these so our logger can access them (for plotting).
self.E = 0.0
self.ep = 0.0 # previous error
self.e = 0.0
self.de = 0.0
self.valid = False
def compute_inputs(self):
self.x = self.get_input('process')
def compute_outputs(self):
self.ep = self.e
self.e = self.setpoint - self.x # calculate the error
self.y = 0.0 # always zero until we're valid
if self.valid: # This prevents weird stuff from happening with the derivative term.
# calculate the integral (trapezoidal approximation)
self.E += self.e * self.dt
# TODO: Consider adding anti-windup protection here.
# calculate the derivative
self.de = (self.e - self.ep) / self.dt
# calculate the control signal and save the temporary state
self.y = self.kp * self.e + self.ki * self.E + self.kd * self.de
else:
# Once we've run this function once, we're valid.
self.valid = True