-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid.py
45 lines (37 loc) · 1.18 KB
/
pid.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
import time
class PIDController:
def __init__(self, inval, target, kp, ki, kd):
self.lasttick = time.time()
self.delta = 1
self.target = target
self.kp = kp
self.ki = ki
self.kd = kd
# initialize variables used in PID controller
self.err1 = inval - self.target
self.integral = 0.0
self.diffntl = 0.0
self.output = 0.0
def getoutput(self, inval):
thistick = time.time()
self.delta = thistick - self.lasttick
self.lasttick = thistick
err2 = inval - self.target
self.integral += self.err1 * self.delta
if self.integral < 0.0:
self.integral = 0.0
elif self.integral > 100.0:
self.integral = 100.0
self.diffntl = (err2 - self.err1)/self.delta
# calculate output:
e = self.err1 * self.kp
i = self.integral * self.ki
d = self.diffntl * self.kd
self.output = e + i + d
# bound between 0 and 100:
if self.output > 100.0:
self.output = 100.0
elif self.output < 0.0:
self.output = 0.0
self.err1 = err2
return self.output