forked from smarthomeNG/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid_controller.py
executable file
·217 lines (167 loc) · 6.13 KB
/
pid_controller.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
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python
#
# This file is part of IvPID.
# Copyright (C) 2015 Ivmech Mechatronics Ltd. <[email protected]>
#
# IvPID is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IvPID is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# title :PID.py
# description :python pid controller
# author :Caner Durmusoglu
# date :20151218
# version :0.1
# notes :
# python_version :2.7
# ==============================================================================
"""
Ivmech PID Controller is simple implementation of a Proportional-Integral-Derivative (PID) Controller
in the Python Programming Language.
More information about PID Controller: http://en.wikipedia.org/wiki/PID_controller
"""
import time
class PID:
"""PID Controller
"""
def __init__(self, Kp=0.2, Ki=0.0, Kd=0.0):
self._Kp = Kp
self._Ki = Ki
self._Kd = Kd
self.sample_time = 0.00
self.current_time = time.time()
self.last_time = self.current_time
self.clear()
def clear(self):
"""Clears PID computations and coefficients"""
self.SetPoint = 0.0
self.PTerm = 0.0
self.ITerm = 0.0
self.DTerm = 0.0
self.last_error = 0.0
# Windup Guard
self.int_error = 0.0
self.windup_guard = 20.0
self.output = 0.0
def update(self, feedback_value):
"""Calculates PID value for given reference feedback
.. math::
u(t) = K_p e(t) + K_i \int_{0}^{t} e(t)dt + K_d {de}/{dt}
.. figure:: images/pid_1.png
:align: center
Test PID with Kp=1.2, Ki=1, Kd=0.001 (test_pid.py)
"""
error = self.SetPoint - feedback_value
self.current_time = time.time()
delta_time = self.current_time - self.last_time
delta_error = error - self.last_error
if (delta_time >= self.sample_time):
self.PTerm = self._Kp * error
self.ITerm += error * delta_time
if (self.ITerm < -self.windup_guard):
self.ITerm = -self.windup_guard
elif (self.ITerm > self.windup_guard):
self.ITerm = self.windup_guard
self.DTerm = 0.0
if delta_time > 0:
self.DTerm = delta_error / delta_time
# Remember last time and last error for next calculation
self.last_time = self.current_time
self.last_error = error
self.output = self.PTerm + (self._Ki * self.ITerm) + (self._Kd * self.DTerm)
def setWindup(self, windup):
"""Integral windup, also known as integrator windup or reset windup,
refers to the situation in a PID feedback controller where
a large change in setpoint occurs (say a positive change)
and the integral terms accumulates a significant error
during the rise (windup), thus overshooting and continuing
to increase as this accumulated error is unwound
(offset by errors in the other direction).
The specific problem is the excess overshooting.
"""
self.windup_guard = windup
def setSampleTime(self, sample_time):
"""PID that should be updated at a regular interval.
Based on a pre-determined sampe time, the PID decides if it should compute or return immediately.
"""
self.sample_time = sample_time
# ===========================================================================================
@property
def Kp(self):
"""
Property: Kp - proportional gain
Determines how aggressively the PID reacts to the current error with setting Proportional Gain
:param value: set proportional gain
:type value: float
:return: actual proportional gain
:rtype: float
"""
return self._Kp
@Kp.setter
def Kp(self, value):
if isinstance(value, int):
value = float(value)
if isinstance(value, float):
self._Kp = value
return
else:
self._type_error('non-float')
return
@property
def Ki(self):
"""
Property: Ki - integral gain
Determines how aggressively the PID reacts to the current error with setting Integral Gain
:param value: set integral gain
:type value: float
:return: actual integral gain
:rtype: float
"""
return self._Ki
@Ki.setter
def Ki(self, value):
if isinstance(value, int):
value = float(value)
if isinstance(value, float):
self._Ki = value
return
else:
self._type_error('non-float')
return
@property
def Kd(self):
"""
Property: Kd - derivative gain
Determines how aggressively the PID reacts to the current error with setting Derivative Gain
:param value: set derivative gain
:type value: float
:return: actual derivative gain
:rtype: float
"""
return self._Kd
@Kd.setter
def Kd(self, value):
if isinstance(value, int):
value = float(value)
if isinstance(value, float):
self._Kd = value
return
else:
self._type_error('non-float')
return
def __repr__(self):
return "PID controller"
def _type_error(self, err):
import inspect
prop = inspect.stack()[1][3]
#self.logger.error("Cannot set property '{}' of item '{}' to a {} value".format(prop, self._item._path, err))
self.logger.error("Cannot set property '{}' to a {} value".format(prop, err))
return