-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIDController.py
83 lines (73 loc) · 2.5 KB
/
PIDController.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import control
import matplotlib.pyplot as plt
import numpy as np
from GeneralController import GeneralController
class PIDController(GeneralController):
def __init__(self, t, P = 1.0, I = 100, D = 0):
super(PIDController, self).__init__(t)
self.P = P
self.I = I
self.D = D
self.characteristicEquation()
def setQTarget(self, q):
super(PIDController, self).setQTarget(q)
def characteristicEquation(self):
P_CE = control.tf([self.P], [1])
I_CE = control.tf([self.I], [1,0])
D_CE = control.tf([self.D, 0], [1])
self.CE = P_CE + I_CE + D_CE #parallel connection
return self.CE
def sysOpenCE(self, plantCE): # assumes controller is directly in front of plant
return super(PIDController, self).sysOpenCE(plantCE)
def sysClosedCE(self): # assumes that controller is before plant on feedforward step
return super(PIDController, self).sysOpenCE(plantCE)
def sysRecomp(self, P = None, I = None, D = None):
if P is not None:
self.P = P
if I is not None:
self.I = I
if D is not None:
self.D = D
self.characteristicEquation()
self.sysOpenCE(self.plantCE)
self.sysClosedCE()
print self.sysCCE
def plotImpulseResponse(self, CE, axx = None, Plot=True):
t,q = super(PIDController, self).plotImpulseResponse(CE, axx, Plot)
return t,q
def plotStepResponse(self, CE, axx = None, Plot=True):
t, q = control.step_response(CE)
if axx is not None:
axx.plot(t,q, marker = np.random.choice(self.marker), label = 'P:%s, I:%s, D:%s' %(self.P, self.I, self.D))
axx.legend()
if Plot:
plt.plot(t,q)
plt.show()
return t,q
def update(self, q_cur):
self.e = self.q_target[0] - q_cur[0]
if self.first_update:
self.q_last = q_cur
self.e_prev = self.e
self.first_update = False
self.e_total += self.e # don't need dt, can roll it into gains
self.delta_e = self.e - self.e_prev
self.u_P = self.P * self.e # proportional
self.u_I = self.I * self.e_total # integral
self.u_D = self.D * (self.e - self.e_prev) # derivative, assuming time change
self.u = self.u_P + self.u_D + self.u_I
self.q_last = q_cur
# pdb.set_trace()
return self.u, self.e
def plotRootLocus(self, rl_out, axx, linetype = '-'): #plots the output of a root_locus
poles = rl_out[0]
reals = np.real(poles)
imags = np.imag(poles)
real = [];
imag = []
for i in range(len(reals[0])):
imag.append(imags[:,i])
real.append(reals[:,i])
# pdb.set_trace()
plt.plot(np.array(real).flatten(), np.array(imag).flatten(), linetype, label = 'P:%s, I:%s, D:%s' %(self.P, self.I, self.D))
plt.legend()