-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid_controll.py
67 lines (55 loc) · 1.68 KB
/
pid_controll.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
55
56
57
58
59
60
61
62
63
64
65
66
# coding: utf-8
class pid:
def __init__(self):
self.P_value=0.0
self.I_value=0.0
self.D_value=0.0
self.kp=0.05
self.ki=0.002
self.kd=0.002
self.Derivator=0
self.Integrator=0
self.Integrator_Max=500
self.Integrator_Min=-500
self.set_point=200.0
self.error=0.0
def update(self,current_value):
self.error=self.set_point - current_value
if self.error <0:
self.error = 0
try:
self.P_value=self.kp*self.error
except ZeroDivisionError:
self.P_value=0.0
self.D_value=self.kd*(self.error-self.Derivator)
self.Derivator=self.error
self.Integrator=self.Integrator+self.error
if self.Integrator > self.Integrator_Max:
self.Integrator_Max=self.Integrator_Max
elif self.Integrator < self.Integrator_Min:
self.Integrator_Min=self.Integrator_Min
self.I_value = self.Integrator*self.ki
PID = self.P_value + self.I_value + self.D_value
return PID
def setPoint(self,set_point):
self.set_point=set_point
self.Integrator=0
self.Derivator=0
def setIntegrator(self,Integrator):
self.Integrator=Integrator
def setDerivator(self,Derivator):
self.Derivator = Derivator
def setKp(self,Kp):
self.kp=Kp
def setki(self,Ki):
self.ki=Ki
def setkd(self,Kd):
self.kd=Kd
def getPoint(self):
return self.set_point
def getError(self):
return self.error
def getIntegrator(self):
return self.Integrator
def getDerivator(self):
return self.Derivator