-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pid.py
401 lines (326 loc) · 10.5 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#Pinout range of ESP32 board mapped by firmata-esp32
#define IS_PIN_DIGITAL(p) (((p) >= 2 && (p) <= 5) || ((p) >= 13 && (p) <= 27))
#define IS_PIN_ANALOG(p) (((p) >= 32 && (p) <= 32 + TOTAL_ANALOG_PINS))
#define IS_PIN_PWM(p) digitalPinHasPWM(p) // all gpios in digital
#OBS: Para este teste, o produto da resolução PWM no arquivo FirmataExt.cpp foi alterado de 1000 para 10.
# Esse alteração foi feita para que a frequência no pino do ESP32 seja a mesma do arduino uno: 490Hz.
from pyfirmata import Arduino, util
import numpy as np
import time
import matplotlib.pyplot as plt
from scipy.integrate import odeint
board = Arduino('/dev/ttyUSB0')
it = util.Iterator(board)
it.start()
print('✔ board ready!')
temperature = 0
def handle_temperature(*data):
rawV = 0
for i in data:
rawV = rawV + i
global temperature
temperature = (rawV * (5000 / 1023)) / 10
# print(temperature)
board.add_cmd_handler(0x02, handle_temperature)
def pid(sp, pv, pv_last, ierr, dt):
Kc = 10.0 # K/%Heater
tauI = 50.0 # sec
tauD = 1.0 # sec
# Parameters in terms of PID coefficients
KP = Kc
KI = Kc/tauI
KD = Kc*tauD
# ubias for controller (initial heater)
op0 = 0
# upper and lower bounds on heater level
ophi = 100
oplo = 0
# calculate the error
error = sp-pv
# calculate the integral error
ierr = ierr + KI * error * dt
# calculate the measurement derivative
dpv = (pv - pv_last) / dt
# calculate the PID output
P = KP * error
I = ierr
D = -KD * dpv
op = op0 + P + I + D
# implement anti-reset windup
if op < oplo or op > ophi:
I = I - KI * error * dt
# clip output
op = max(oplo, min(ophi, op))
# return the controller output and PID terms
return [op, P, I, D]
# save txt file with data and set point
# t = time
# u1,u2 = heaters
# y1,y2 = tempeatures
# sp1,sp2 = setpoints
def save_txt(t, u1, u2, y1, y2, sp1, sp2):
data = np.vstack((t, u1, u2, y1, y2, sp1, sp2)) # vertical stack
data = data.T # transpose data
top = ('Time (sec), Heater 1 (%), Heater 2 (%), '
'Temperature 1 (degC), Temperature 2 (degC), '
'Set Point 1 (degC), Set Point 2 (degC)')
np.savetxt('data.txt', data, delimiter=',', header=top, comments='')
def save_data_pid(y, u, erro):
data = np.vstack((y, u, erro))
data = data.T
top = ('Temperature (degC), Heater (%), error')
np.savetxt('data_pid.csv', data, delimiter=',', header=top, comments='')
######################################################
# FOPDT model #
######################################################
#Get in pidtuner.com
Kp = 0.34953822182155675
tauP = 113.55168586918873
thetaP = 31.945949604357498
Tss = 30
Qss = 0
#Get in regression experiment
# Kp = 0.6747566878044584
# tauP = 172.3375915827159
# thetaP = 29.130396322742243
# Tss = 30
# Qss = 0
#Original
# Kp = 0.5 # degC/%
# tauP = 120.0 # seconds
# thetaP = 10 # seconds (integer)
# Tss = 23 # degC (ambient temperature)
# Qss = 0 # % heater
######################################################
# Energy balance model #
######################################################
def heat(x, t, Q):
# Parameters
Ta = 23 + 273.15 # K
U = 10.0 # W/m^2-K
m = 4.0/1000.0 # kg
Cp = 0.5 * 1000.0 # J/kg-K
A = 12.0 / 100.0**2 # Area in m^2
alpha = 0.01 # W / % heater
eps = 0.9 # Emissivity
sigma = 5.67e-8 # Stefan-Boltzman
# Temperature State
T = x[0]
# Nonlinear Energy Balance
dTdt = (1.0/(m*Cp))*(U*A*(Ta-T)
+ eps * sigma * A * (Ta**4 - T**4)
+ alpha*Q)
return dTdt
######################################################
# Do not adjust anything below this point #
######################################################
# Turn LED on
# print('LED On')
# a.LED(100)
# Run time in minutes
run_time = 15
# Number of cycles
loops = int(60.0*run_time)
tm = np.zeros(loops)
# Temperature
# set point (degC)
Tsp1 = np.ones(loops) * 35
Tsp1[60:] = 50.0
Tsp1[360:] = 30.0
Tsp1[660:] = 40.0
board.send_sysex(0x02, [32])
time.sleep(0.1) #Esse sleep é necessário para dar tempo do valor da variável temperatura ser atualizado
T1 = np.ones(loops) * temperature # measured T (degC)
error_sp = np.zeros(loops)
Tsp2 = np.ones(loops) * 23.0 # set point (degC)
# T2 = np.ones(loops) * a.T2 # measured T (degC)
# Predictions
board.send_sysex(0x02, [32])
time.sleep(0.1) #Esse sleep é necessário para dar tempo do valor da variável temperatura ser atualizado
Tp = np.ones(loops) * temperature
error_eb = np.zeros(loops)
Tpl = np.ones(loops) * temperature
error_fopdt = np.zeros(loops)
# impulse tests (0 - 100%)
Q1 = np.ones(loops) * 0.0
Q2 = np.ones(loops) * 0.0
error = np.zeros(loops)
print('Running Main Loop. Ctrl-C to end.')
print(' Time SP PV Q1 = P + I + D')
print(('{:6.1f} {:6.2f} {:6.2f} ' +
'{:6.2f} {:6.2f} {:6.2f} {:6.2f}').format(
tm[0], Tsp1[0], T1[0],
Q1[0], 0.0, 0.0, 0.0))
# Create plot
plt.figure() # figsize=(10,7)
plt.ion()
plt.show()
# Main Loop
start_time = time.time()
prev_time = start_time
dt_error = 0.0
# Integral error
ierr = 0.0
try:
for i in range(1, loops):
# Sleep time
sleep_max = 1.0
sleep = sleep_max - (time.time() - prev_time) - dt_error
if sleep >= 1e-4:
time.sleep(sleep-1e-4)
else:
print('exceeded max cycle time by ' + str(abs(sleep)) + ' sec')
time.sleep(1e-4)
# Record time and change in time
t = time.time()
dt = t - prev_time
if (sleep >= 1e-4):
dt_error = dt-1.0+0.009
else:
dt_error = 0.0
prev_time = t
tm[i] = t - start_time
# Read temperatures in Kelvin
board.send_sysex(0x02, [32])
T1[i] = temperature
# T2[i] = a.T2
error[i] = Tsp1[i] - T1[i]
# Simulate one time step with Energy Balance
Tnext = odeint(heat, Tp[i-1]+273.15, [0, dt], args=(Q1[i-1],))
Tp[i] = Tnext[1]-273.15
# Simulate one time step with linear FOPDT model
z = np.exp(-dt/tauP)
Tpl[i] = (Tpl[i-1]-Tss) * z \
+ (Q1[max(0, i-int(thetaP)-1)]-Qss)*(1-z)*Kp \
+ Tss
# Calculate PID output
[Q1[i], P, ierr, D] = pid(Tsp1[i], T1[i], T1[i-1], ierr, dt)
# Start setpoint error accumulation after 1 minute (60 seconds)
if i >= 60:
error_eb[i] = error_eb[i-1] + abs(Tp[i]-T1[i])
error_fopdt[i] = error_fopdt[i-1] + abs(Tpl[i]-T1[i])
error_sp[i] = error_sp[i-1] + abs(Tsp1[i]-T1[i])
# Write output (0-100)
datasToWrite = []
datasToWrite.append(12)
datasToWrite.append(0)
datasToWrite.append(49)
datasToWrite.append(8)
valueQ1 = max(0, min(Q1[i], 100))
datasToWrite.append(int((valueQ1 * 255) / 100))
board.send_sysex(0x04, datasToWrite)
# a.Q1(Q1[i])
# a.Q2(0.0)
# Print line of data
print(('{:6.1f} {:6.2f} {:6.2f} ' +
'{:6.2f} {:6.2f} {:6.2f} {:6.2f}').format(
tm[i], Tsp1[i], T1[i],
Q1[i], P, ierr, D))
# plt.pause(0.05)
# Plot
# Turn off heaters
datasToWrite = []
datasToWrite.append(12)
datasToWrite.append(0)
datasToWrite.append(49)
datasToWrite.append(8)
datasToWrite.append(0)
board.send_sysex(0x04, datasToWrite)
plt.clf()
ax = plt.subplot(3, 1, 1)
ax.grid()
plt.plot(tm, T1, 'r.', label=r'$T_1$ measured')
plt.plot(tm, Tsp1, 'k--', label=r'$T_1$ set point')
plt.ylim(25, 110)
plt.ylabel('Temperature (degC)')
plt.legend(loc=2)
ax = plt.subplot(3, 1, 2)
ax.grid()
plt.plot(tm, Q1, 'b-', label=r'$Q_1$')
plt.ylabel('Heater')
plt.legend(loc='best')
ax = plt.subplot(3, 1, 3)
ax.grid()
plt.plot(tm, T1, 'r.', label=r'$T_1$ measured')
plt.plot(tm, Tpl, 'g-', label=r'$T_1$ linear model')
plt.ylabel('Temperature (degC)')
plt.legend(loc=2)
plt.xlabel('Time (sec)')
plt.draw()
plt.savefig('test_PID.png')
#Salvar o y (saída), sinal de controle, erro em um arquivo .txt
save_data_pid(T1, Q1, error)
board.exit()
# Save text file
# save_txt(tm[0:i], Q1[0:i], Q2[0:i], T1[0:i], T2[0:i], Tsp1[0:i], Tsp2[0:i])
# Allow user to end loop with Ctrl-C
except KeyboardInterrupt:
# Disconnect from ESP32
datasToWrite = []
datasToWrite.append(12)
datasToWrite.append(0)
datasToWrite.append(49)
datasToWrite.append(8)
datasToWrite.append(0)
board.send_sysex(0x04, datasToWrite)
plt.clf()
ax = plt.subplot(3, 1, 1)
ax.grid()
plt.plot(tm, T1, 'r.', label=r'$T_1$ measured')
plt.plot(tm, Tsp1, 'k--', label=r'$T_1$ set point')
plt.ylim(25, 110)
plt.ylabel('Temperature (degC)')
plt.legend(loc=2)
ax = plt.subplot(3, 1, 2)
ax.grid()
plt.plot(tm, Q1, 'b-', label=r'$Q_1$')
plt.ylabel('Heater')
plt.legend(loc='best')
ax = plt.subplot(3, 1, 3)
ax.grid()
plt.plot(tm, T1, 'r.', label=r'$T_1$ measured')
plt.plot(tm, Tpl, 'g-', label=r'$T_1$ linear model')
plt.ylabel('Temperature (degC)')
plt.legend(loc=2)
plt.xlabel('Time (sec)')
plt.draw()
plt.savefig('test_PID.png')
save_data_pid(T1, Q1, error)
board.exit()
print('Shutting down')
# Make sure serial connection still closes when there's an error
except:
# Disconnect from ESP32
datasToWrite = []
datasToWrite.append(12)
datasToWrite.append(0)
datasToWrite.append(49)
datasToWrite.append(8)
datasToWrite.append(0)
board.send_sysex(0x04, datasToWrite)
plt.clf()
ax = plt.subplot(3, 1, 1)
ax.grid()
plt.plot(tm, T1, 'r.', label=r'$T_1$ measured')
plt.plot(tm, Tsp1, 'k--', label=r'$T_1$ set point')
plt.ylim(25, 110)
plt.ylabel('Temperature (degC)')
plt.legend(loc=2)
ax = plt.subplot(3, 1, 2)
ax.grid()
plt.plot(tm, Q1, 'b-', label=r'$Q_1$')
plt.ylabel('Heater')
plt.legend(loc='best')
ax = plt.subplot(3, 1, 3)
ax.grid()
plt.plot(tm, T1, 'r.', label=r'$T_1$ measured')
plt.plot(tm, Tpl, 'g-', label=r'$T_1$ linear model')
plt.ylabel('Temperature (degC)')
plt.legend(loc=2)
plt.xlabel('Time (sec)')
plt.draw()
plt.savefig('test_PID.png')
save_data_pid(T1, Q1, error)
board.exit()
print('Shutting down')
raise