-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpid.py
205 lines (153 loc) · 5.37 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import time
class PID:
"""
Discrete PID control
"""
def __init__(self, **kwargs):
self.Kp = kwargs.get('P', 0)
self.Ki = kwargs.get('I', 0)
self.Kd = kwargs.get('D', 0)
self.Derivator = kwargs.get('Derivator', 0)
self.Integrator = kwargs.get('Integrator', 0)
self.Integrator_max = kwargs.get('Integrator_max', 25)
self.Integrator_min = kwargs.get('Integrator_min', 25)
self.set_point = 0.0
self.error = 0.0
self.minimum_thrust = kwargs.get('minimum_thrust', -50)
self.maximum_thrust = kwargs.get('maximum_thrust', 50)
self.current_time = time.time()
self.previous_time = time.time()
self.previous_error = 0
def update(self, current_value):
"""
Calculate PID output value for given reference input and feedback
"""
self.error = self.set_point - current_value
self.current_time = time.time()
dt = self.current_time - self.previous_time
if dt > 0:
self.Derivator = (self.error - self.previous_error) / dt
else:
self.Derivator = 0
self.P_value = self.Kp * self.error
self.D_value = self.Kd * self.Derivator
# self.D_value = self.Kd * (self.error - self.Derivator)
self.Integrator += self.error * dt
if self.Integrator > self.Integrator_max:
self.Integrator = self.Integrator_max
elif self.Integrator < self.Integrator_min:
self.Integrator = self.Integrator_min
# print self.Integrator
self.I_value = self.Integrator * self.Ki
PID = self.P_value + self.I_value + self.D_value
self.previous_error = self.error
self.previous_time = time.time()
return PID
def setPoint(self, set_point):
"""
Initilize the setpoint of PID
"""
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, P):
self.Kp = P
def setKi(self, I):
self.Ki = I
def setKd(self, D):
self.Kd = D
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
def constraint(self, value):
if value > self.maximum_thrust:
return self.maximum_thrust
elif value < self.minimum_thrust:
return self.minimum_thrust
else:
return int(round(value))
class PID_offline:
"""
Discrete PID control
"""
def __init__(self, **kwargs):
self.Kp = kwargs.get('P', 0)
self.Ki = kwargs.get('I', 0)
self.Kd = kwargs.get('D', 0)
self.Derivator = kwargs.get('Derivator', 0)
self.Integrator = kwargs.get('Integrator', 0)
self.Integrator_max = kwargs.get('Integrator_max', 25)
self.Integrator_min = kwargs.get('Integrator_min', 25)
self.set_point = 0.0
self.error = 0.0
self.minimum_thrust = kwargs.get('minimum_thrust', -50)
self.maximum_thrust = kwargs.get('maximum_thrust', 50)
self.current_time = time.time()
self.previous_time = time.time()
self.previous_error = 0
def update(self, current_value, dt):
"""
Calculate PID output value for given reference input and feedback
"""
self.error = self.set_point - current_value
self.current_time = time.time()
print self.error
#dt = self.current_time - self.previous_time
if dt > 0:
self.Derivator = (self.error - self.previous_error) / dt
else:
self.Derivator = 0
self.P_value = self.Kp * self.error
self.D_value = self.Kd * self.Derivator
# self.D_value = self.Kd * (self.error - self.Derivator)
self.Integrator += self.error * dt
if self.Integrator > self.Integrator_max:
self.Integrator = self.Integrator_max
elif self.Integrator < self.Integrator_min:
self.Integrator = self.Integrator_min
self.I_value = self.Integrator * self.Ki
PID = self.P_value + self.I_value + self.D_value
self.previous_error = self.error
#self.previous_time = time.time()
return PID
def setPoint(self, set_point):
"""
Initilize the setpoint of PID
"""
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, P):
self.Kp = P
def setKi(self, I):
self.Ki = I
def setKd(self, D):
self.Kd = D
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
def constraint(self, value):
if value > self.maximum_thrust:
return self.maximum_thrust
elif value < self.minimum_thrust:
return self.minimum_thrust
else:
return int(round(value))