-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathtest_pid.py
298 lines (208 loc) · 7.08 KB
/
test_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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import sys
import time
import pytest
from simple_pid import PID
def test_zero():
pid = PID(1, 1, 1, setpoint=0)
assert pid(0) == 0
def test_P():
pid = PID(1, 0, 0, setpoint=10, sample_time=None)
assert pid(0) == 10
assert pid(5) == 5
assert pid(-5) == 15
def test_P_negative_setpoint():
pid = PID(1, 0, 0, setpoint=-10, sample_time=None)
assert pid(0) == -10
assert pid(5) == -15
assert pid(-5) == -5
assert pid(-15) == 5
def test_I():
pid = PID(0, 10, 0, setpoint=10, sample_time=0.1)
time.sleep(0.1)
assert round(pid(0)) == 10.0 # Make sure we are close to expected value
time.sleep(0.1)
assert round(pid(0)) == 20.0
def test_I_negative_setpoint():
pid = PID(0, 10, 0, setpoint=-10, sample_time=0.1)
time.sleep(0.1)
assert round(pid(0)) == -10.0
time.sleep(0.1)
assert round(pid(0)) == -20.0
def test_D():
pid = PID(0, 0, 0.1, setpoint=10, sample_time=0.1)
# Should not compute derivative when there is no previous input (don't assume 0 as first input)
assert pid(0) == 0
time.sleep(0.1)
# Derivative is 0 when input is the same
assert pid(0) == 0
assert pid(0) == 0
time.sleep(0.1)
assert round(pid(5)) == -5
time.sleep(0.1)
assert round(pid(15)) == -10
def test_D_negative_setpoint():
pid = PID(0, 0, 0.1, setpoint=-10, sample_time=0.1)
time.sleep(0.1)
# Should not compute derivative when there is no previous input (don't assume 0 as first input)
assert pid(0) == 0
time.sleep(0.1)
# Derivative is 0 when input is the same
assert pid(0) == 0
assert pid(0) == 0
time.sleep(0.1)
assert round(pid(5)) == -5
time.sleep(0.1)
assert round(pid(-5)) == 10
time.sleep(0.1)
assert round(pid(-15)) == 10
def test_desired_state():
pid = PID(10, 5, 2, setpoint=10, sample_time=None)
# Should not make any adjustment when setpoint is achieved
assert pid(10) == 0
def test_output_limits():
pid = PID(100, 20, 40, setpoint=10, output_limits=(0, 100), sample_time=None)
time.sleep(0.1)
assert 0 <= pid(0) <= 100
time.sleep(0.1)
assert 0 <= pid(-100) <= 100
def test_sample_time():
pid = PID(setpoint=10, sample_time=10)
control = pid(0)
# Last value should be returned again
assert pid(100) == control
def test_time_fn():
pid = PID()
# Default time function should be time.monotonic, or time.time in older versions of Python
if sys.version_info < (3, 3):
assert pid.time_fn == time.time
else:
assert pid.time_fn == time.monotonic
i = 0
def time_function():
nonlocal i
i += 1
return i
pid.time_fn = time_function
for j in range(1, 5):
# Call pid a few times and verify that the time function above was used
pid(0)
assert pid._last_time == j
def test_time_fn_notime():
# Deliberately prevent the time module from being imported
import sys
sys.modules['time'] = None
with pytest.raises(ModuleNotFoundError):
# Must specify a time_fn if time is not available
_ = PID()
# We can still create a PID if we specify our own time_fn
_ = PID(time_fn=lambda: 0)
# Restore time module so the following tests can use it
sys.modules['time'] = time
def test_starting_output():
# If the PID is started with a system already at the setpoint, we can give it our best guess
# for which output it should start at
pid = PID(1, 0, 0, setpoint=10, starting_output=25)
assert pid(10) == 25
def test_auto_mode():
pid = PID(1, 0, 0, setpoint=10, sample_time=None)
# Ensure updates happen by default
assert pid(0) == 10
assert pid(5) == 5
# Ensure no new updates happen when auto mode is off
pid.auto_mode = False
assert pid(1) == 5
assert pid(7) == 5
# Should reset when reactivating
pid.auto_mode = True
assert pid._last_input is None
assert pid._integral == 0
assert pid(8) == 2
# Last update time should be reset to avoid huge dt
pid.auto_mode = False
time.sleep(1)
pid.auto_mode = True
assert pid.time_fn() - pid._last_time < 0.01
# Check that setting last_output works
pid.auto_mode = False
pid.set_auto_mode(True, last_output=10)
assert pid._integral == 10
def test_separate_components():
pid = PID(1, 0, 1, setpoint=10, sample_time=0.1)
assert pid(0) == 10
assert pid.components == (10, 0, 0)
time.sleep(0.1)
assert round(pid(5)) == -45
assert tuple(round(term) for term in pid.components) == (5, 0, -50)
def test_clamp():
from simple_pid.pid import _clamp
assert _clamp(None, (None, None)) is None
assert _clamp(None, (-10, 10)) is None
# No limits
assert _clamp(0, (None, None)) == 0
assert _clamp(100, (None, None)) == 100
assert _clamp(-100, (None, None)) == -100
# Only lower limit
assert _clamp(0, (0, None)) == 0
assert _clamp(100, (0, None)) == 100
assert _clamp(-100, (0, None)) == 0
# Only upper limit
assert _clamp(0, (None, 0)) == 0
assert _clamp(100, (None, 0)) == 0
assert _clamp(-100, (None, 0)) == -100
# Both limits
assert _clamp(0, (-10, 10)) == 0
assert _clamp(-10, (-10, 10)) == -10
assert _clamp(10, (-10, 10)) == 10
assert _clamp(-100, (-10, 10)) == -10
assert _clamp(100, (-10, 10)) == 10
def test_repr():
pid = PID(1, 2, 3, setpoint=10)
new_pid = eval(repr(pid))
assert new_pid.Kp == 1
assert new_pid.Ki == 2
assert new_pid.Kd == 3
assert new_pid.setpoint == 10
def test_converge_system():
pid = PID(1, 0.8, 0.04, setpoint=5, output_limits=(-5, 5))
pv = 0 # Process variable
def update_system(c, dt):
# Calculate a simple system model
return pv + c * dt - 1 * dt
start_time = time.time()
last_time = start_time
while time.time() - start_time < 120:
c = pid(pv)
pv = update_system(c, time.time() - last_time)
last_time = time.time()
# Check if system has converged
assert abs(pv - 5) < 0.1
def test_converge_diff_on_error():
pid = PID(1, 0.8, 0.04, setpoint=5, output_limits=(-5, 5), differential_on_measurement=False)
pv = 0 # Process variable
def update_system(c, dt):
# Calculate a simple system model
return pv + c * dt - 1 * dt
start_time = time.time()
last_time = start_time
while time.time() - start_time < 12:
c = pid(pv)
pv = update_system(c, time.time() - last_time)
last_time = time.time()
# Check if system has converged
assert abs(pv - 5) < 0.1
def test_error_map():
import math
def pi_clip(angle):
"""Transform the angle value to a [-pi, pi) range."""
if angle > 0:
if angle > math.pi:
return angle - 2 * math.pi
else:
if angle < -math.pi:
return angle + 2 * math.pi
return angle
sp = 0.0 # Setpoint
pv = 5.0 # Process variable
pid = PID(1, 0, 0, setpoint=0.0, sample_time=0.1, error_map=pi_clip)
# Check if error value is mapped by the function
assert pid(pv) == pi_clip(sp - pv)