| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
from __future__ import print_function |
| 4 |
import time |
| 5 |
from Model import Model |
| 6 |
from PlantOPC import PlantOPC |
| 7 |
import OpenOPC |
| 8 |
import numpy |
| 9 |
import matplotlib.pyplot as plt |
| 10 |
|
| 11 |
opc = OpenOPC.client() # Cria o cliente OPC; o servidor é o RSLinx |
| 12 |
opc.connect('RSLinx OPC Server') # Essa string não muda; conecta ao RSLinx |
| 13 |
|
| 14 |
pC = numpy.array([0.5, 0.6, 0.7, 0.5 + 0.4j, 0.5 - 0.4j]) |
| 15 |
Ki = 0.183111320328469 |
| 16 |
Kp = numpy.array([0.007993734748865, 0.009705988539721, -0.004630469582507, -0.000426479250745]) |
| 17 |
|
| 18 |
t = numpy.array(range(0, n_t)) * Ts |
| 19 |
# time = linspace(0,10,n_t) |
| 20 |
# instantiate the plant that will be used, it should be a subclass of Plant |
| 21 |
|
| 22 |
plant = PlantOPC(opc, '[CLP_AB]position', '[CLP_AB]speed', init_pos) |
| 23 |
model = Model(n, A, B, C, D, Ak, Bk, Ck, Q, R, Kp, Ki, epsilon, Ts, plant) |
| 24 |
|
| 25 |
start = time.clock() |
| 26 |
t_old = start |
| 27 |
times_p = [] |
| 28 |
for i in range(0, n_t): |
| 29 |
y_out[i] = model.closed_loop(y_topo[i],y_fundo[i]) |
| 30 |
#time.sleep(0.1) |
| 31 |
plant.kill() |
| 32 |
print("Total simulation time: {}s".format(time.clock() - start)) |
| 33 |
|
| 34 |
y_out_phased = y_out[5:n_t] |
| 35 |
t_out_phased = t[0:n_t-5] |
| 36 |
##plt.plot(t, y_out[0:n_t], label='out') |
| 37 |
plt.plot(t_out_phased,y_out_phased, label='out_n') |
| 38 |
plt.plot(t, y_fundo[0:n_t], label='ref fundo') |
| 39 |
plt.plot(t, y_topo[0:n_t], label='ref topo (in)') |
| 40 |
plt.legend(loc=4) |
| 41 |
plt.xlabel('time (s)') |
| 42 |
plt.ylabel('position (m)') |
| 43 |
plt.title('Position of cart - closed loop') |
| 44 |
plt.grid(True) |
| 45 |
# plt.show() |
| 46 |
plt.savefig("resultados/closed_loop.png", format='png', dpi=200) |
| 47 |
File = open('resultados/trajetoria.npz', 'wb') |
| 48 |
numpy.savez(File, t=t, y_topo=y_topo, y_fundo=y_fundo, y_out=y_out, pC=pC, Ki=Ki, Kp=Kp) |
| 49 |
File.close() |
| 50 |
|
| 51 |
opc.close() # Encerra a sessão |