-
Notifications
You must be signed in to change notification settings - Fork 0
/
Versuch3_7_3_1.py
223 lines (172 loc) · 5.6 KB
/
Versuch3_7_3_1.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
import numpy as np
import scipy.integrate as sci
import matplotlib.pyplot as plt
import Function_Blocks as FB
import time
# zu plottende Aufgabe, Arbeitspunkt und Strecke auswählen
A1 = False
normaleStrecke = True
class Parameters(object):
pass
# Simulationsparameter
sim_para = Parameters() # instance of class Parameters
sim_para.t0 = 0 # start time
sim_para.tf = 20 # final time
sim_para.h = 0.001
sim_para.w0 = 0.0
sim_para.t_step = 1.0
sim_para.w1 = 1.0
sim_para.w2 = 0.0
sim_para.z11 = 0.0
sim_para.z21 = 0.0
sim_para.deltaT = 0.001
tt = np.arange(sim_para.t0, sim_para.tf + sim_para.h, sim_para.h)
#Erzeugen der Eingänge
w1 =FB.Inputfunction(sim_para.t_step, sim_para.w0, sim_para.w1, sim_para.deltaT)
w2 =FB.Inputfunction(sim_para.t_step, sim_para.w0, sim_para.w2, sim_para.deltaT)
z11 = FB.Inputfunction(sim_para.t_step, sim_para.w0, sim_para.z11, sim_para.deltaT)
z21 = FB.Inputfunction(sim_para.t_step, sim_para.w0, sim_para.z21, sim_para.deltaT)
# Listen in denen Ergebnisse für das spätere Plotten gespeichert werden
m1_traj= []
m2_traj = []
x1_traj = []
x2_traj = []
w1_traj = []
w2_traj = []
z11_traj = []
z21_traj = []
# Parameterfestlegung
if A1:
Ki_p11 = 0.4 #k1
Ki_p12 = -0.8 #k3
Kp_p21 = -0.2 #k4
T_p21 = 1 #T
Ki_p22 = 1.2 #k2
else: #A2
Ki_p11 = 0.4
Ki_p12 = -1.28
Kp_p21 = -0.32
T_p21 = 1
Ki_p22 = 1.2
if normaleStrecke:
Kp_r11 = 1.699
Ti_r11 = 1.291
Kp_r22 = 0.566
Ti_r22 = 1.291
else: #aus resultierende Strecke
Kp_r11 = 2.972
Ti_r11 = 9.950
Kp_r22 = 0.991
Ti_r22 = 9.950
calcus = FB.Simple_Calc()
test = []
timer = []
h = sim_para.h
# starte Timer zur Simulationszeitberechnung
time1 = time.perf_counter()
# Simulation
zp = []
m1_traj = []
m2_traj = []
w1_traj = []
w2_traj = []
# weitere Aufgaben unter diese if Bedingungen
def ode(t, x):
x1, x2, x3, x4, e1_dt, e2_dt, x7 = x
r1 = x1 + x2
e1 = w1.jump(t) - r1
x_dot5 = e1 #Hilfsgröße für m1 -> Berechung |e2*dt
r2 = x3 + x4
e2 = w2.zero() - r2
x_dot6 = e2
m1 = Kp_r11*e1 + Kp_r11/Ti_r11*e1_dt #Lösung PI-Regler im Zeitbereich
m2 = Kp_r22*e2 + Kp_r22/Ti_r22*e2_dt
# Eingang DT1-Glied ohne Abhängigkeit von sich selbst
#a1 = 1/(1-(Ki_p12 * Kp_p21)/(Ki_p11 * Ki_p22*T_p21)) * (m1-Ki_p12/Ki_p11 * m2 - (Ki_p12 * Kp_p21)/(Ki_p11 * Ki_p22 * T_p21) * x7)
M = np.array([[1, Ki_p12/Ki_p11], [Kp_p21/(T_p21*Ki_p22), 1]])
C = np.array([m1, m2+x7*Kp_p21/(T_p21*Ki_p22)])
a1, a2 = np.linalg.solve(M, C)
x_dot7 = 1/T_p21*(a1-x7) #DGL PT1-Glied
#y = x_dot7*(-Kp_p21/Ki_p22) #Lösung DT1-Glied
#a2 = m2 + y
x_dot1 = Ki_p11*(a1+z11.zero()) #DGL I-Glied
x_dot2 = Ki_p12*(a2) #DGL I-Glied
x_dot3 = 1/T_p21*(-x3+Kp_p21*(a1+z21.zero())) #DGL PT1-Glied
x_dot4 = Ki_p22*(a2) #DGL I-Glied
x_dot = np.array([x_dot1, x_dot2, x_dot3, x_dot4, x_dot5, x_dot6, x_dot7])
return x_dot
x0 = np.array([0,0,0,0,0,0,0])
solv = sci.solve_ivp(lambda t, x: ode(t, x), (sim_para.t0, sim_para.tf), x0, t_eval=tt)
x1_traj = solv.y.T[:,0] + solv.y.T[:,1] #2 param wahl x
x2_traj = solv.y.T[:,2] + solv.y.T[:,3]
for tx in tt:
w1_traj.append(w1.linear(tx))
w2_traj.append(w2.zero())
z11_traj.append(z11.zero())
z21_traj.append(z21.zero())
for tx in range(len(tt)):
m1 = Kp_r11*(w1_traj[tx] - solv.y.T[tx,0] - solv.y.T[tx,1]) + Kp_r11/Ti_r11*solv.y.T[tx,4] #Lösung PI-Regler im Zeitbereich
m1_traj.append(m1)
m2 = Kp_r22*(w1_traj[tx] - solv.y.T[tx,2] - solv.y.T[tx,3]) + Kp_r22/Ti_r22*solv.y.T[tx,5]
m2_traj.append(m2)
print("Simulationszeit: " + str(time.perf_counter()-time1))
fig1, (ax1_1, ax1_2, axz11, axz21) = plt.subplots(4)
plt.subplots_adjust(hspace=0.5)
ax1_1.plot(tt, w1_traj) # '-o', color='blue', MarkerSize=2
ax1_2.plot(tt, w2_traj)
# axis label
ax1_1.set(xlabel="Zeit in s", ylabel="Spannung in V", title="Eingangssignal w1")
ax1_2.set(xlabel="Zeit in s", ylabel="Spannung in V", title="Eingangssignal w2")
# format x axis
# adding grid
ax1_1.xaxis.grid(True)
ax1_1.yaxis.grid(True)
ax1_2.xaxis.grid(True)
ax1_2.yaxis.grid(True)
axz11.plot(tt, z11_traj)
axz21.plot(tt, z21_traj)
# axis label
axz11.set(xlabel="Zeit in s", ylabel="Spannung in V", title="Störsignal z11")
axz21.set(xlabel="Zeit in s", ylabel="Spannung in V", title="Störsignal z21")
#plt.tight_layout()
# format x axis
# adding grid
axz11.xaxis.grid(True)
axz11.yaxis.grid(True)
axz21.xaxis.grid(True)
axz21.yaxis.grid(True)
filename ="Grafiken/V7_3_1_Eingaenge.svg"
plt.savefig(filename, format="svg")
fig2, (ax2_1, ax2_2) = plt.subplots(2)
plt.tight_layout()
ax2_1.plot(tt, m1_traj)
ax2_2.plot(tt, m2_traj)
# axis label
ax2_1.set(xlabel="Zeit in s", ylabel="Spannung in V", title="Signal m1")
ax2_2.set(xlabel="Zeit in s", ylabel="Spannung in V", title="Signal m2")
plt.tight_layout()
# format x axis
# adding grid
ax2_1.xaxis.grid(True)
ax2_1.yaxis.grid(True)
ax2_2.xaxis.grid(True)
ax2_2.yaxis.grid(True)
filename ="Grafiken/V7_3_1_m.svg"
plt.savefig(filename, format="svg")
fig3, (ax3_1, ax3_2) = plt.subplots(2)
plt.tight_layout()
ax3_1.plot(tt, x1_traj)
ax3_2.plot(tt, x2_traj)
# axis label
ax3_1.set(xlabel="Zeit in s", ylabel="Spannung in V", title="Ausgangssignal x1")
ax3_2.set(xlabel="Zeit in s", ylabel="Spannung in V", title="Ausgangssignal x2")
plt.tight_layout()
# format x axis
# adding grid
ax3_1.xaxis.grid(True)
ax3_1.yaxis.grid(True)
ax3_2.xaxis.grid(True)
ax3_2.yaxis.grid(True)
filename ="Grafiken/V7_3_1_Ausgaenge.svg"
plt.savefig(filename, format="svg")
plt.show()