-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPID.py
More file actions
35 lines (31 loc) · 993 Bytes
/
PID.py
File metadata and controls
35 lines (31 loc) · 993 Bytes
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
#coding utf-8
class PID(object):
"""docstring for PID."""
def __init__(self, Kp, Ti, Td, minOut, maxOut):
super(PID, self).__init__()
self.Kp = Kp
self.Ti = Ti
self.Td = Td
self.integral = 0.
self.last_error = 0.
self.minOut = minOut
self.maxOut = maxOut
def update(self, error):
proportional = error*self.Kp
derivate = (error - self.last_error)*self.Td
self.integral += error*self.Ti
self.last_error = error
if self.integral > self.maxOut:
self.integral = self.maxOut
elif self.integral < self.minOut:
self.integral = self.minOut
output = proportional + derivate + self.integral
if output > self.maxOut:
output = self.maxOut
elif output < self.minOut:
output = self.minOut
return output
def setPID(self, Kp, Ti, Td):
self.Kp = Kp
self.Ti = Ti
self.Td = Td