def testLists(self): """Make sure that lists of various lengths work for operations""" sys1 = ctrl.tf([1, 1], [1, 2]) sys2 = ctrl.tf([1, 3], [1, 4]) sys3 = ctrl.tf([1, 5], [1, 6]) sys4 = ctrl.tf([1, 7], [1, 8]) sys5 = ctrl.tf([1, 9], [1, 0]) # Series sys1_2 = ctrl.series(sys1, sys2) np.testing.assert_array_almost_equal(sort(pole(sys1_2)), [-4., -2.]) np.testing.assert_array_almost_equal(sort(zero(sys1_2)), [-3., -1.]) sys1_3 = ctrl.series(sys1, sys2, sys3); np.testing.assert_array_almost_equal(sort(pole(sys1_3)), [-6., -4., -2.]) np.testing.assert_array_almost_equal(sort(zero(sys1_3)), [-5., -3., -1.]) sys1_4 = ctrl.series(sys1, sys2, sys3, sys4); np.testing.assert_array_almost_equal(sort(pole(sys1_4)), [-8., -6., -4., -2.]) np.testing.assert_array_almost_equal(sort(zero(sys1_4)), [-7., -5., -3., -1.]) sys1_5 = ctrl.series(sys1, sys2, sys3, sys4, sys5); np.testing.assert_array_almost_equal(sort(pole(sys1_5)), [-8., -6., -4., -2., -0.]) np.testing.assert_array_almost_equal(sort(zero(sys1_5)), [-9., -7., -5., -3., -1.]) # Parallel sys1_2 = ctrl.parallel(sys1, sys2) np.testing.assert_array_almost_equal(sort(pole(sys1_2)), [-4., -2.]) np.testing.assert_array_almost_equal(sort(zero(sys1_2)), sort(zero(sys1 + sys2))) sys1_3 = ctrl.parallel(sys1, sys2, sys3); np.testing.assert_array_almost_equal(sort(pole(sys1_3)), [-6., -4., -2.]) np.testing.assert_array_almost_equal(sort(zero(sys1_3)), sort(zero(sys1 + sys2 + sys3))) sys1_4 = ctrl.parallel(sys1, sys2, sys3, sys4); np.testing.assert_array_almost_equal(sort(pole(sys1_4)), [-8., -6., -4., -2.]) np.testing.assert_array_almost_equal(sort(zero(sys1_4)), sort(zero(sys1 + sys2 + sys3 + sys4))) sys1_5 = ctrl.parallel(sys1, sys2, sys3, sys4, sys5); np.testing.assert_array_almost_equal(sort(pole(sys1_5)), [-8., -6., -4., -2., -0.]) np.testing.assert_array_almost_equal(sort(zero(sys1_5)), sort(zero(sys1 + sys2 + sys3 + sys4 + sys5)))
def Controlador(X): #Atribuição dos paramentros Kp, Ki e Kd Kp = Kpi + X[0] / 100 Ki = Kii + X[1] / 100 Kd = Kdi + X[2] / 100 #Função Transferência dos termos do controlador P = Kp I = tf(Ki, [1, 0]) D = tf([Kd, 0], [0.1 * Kd, 1]) #União dos blocos PID C = parallel(P, I, D) # Função Transferência com o Controlador F = series(C, G) # Penalidade para o valor do sinal de Entrada na planta # ou seja penalidade para o sinal de Controle alto tc = feedback(C, G) _, yc = step_response(tc, time) if max(yc) > maxcontrolador: #SE1 = np.square(np.subtract(1,yc)) ISE = tdiv + max(yc) else: # Realizando a Integral do erro quadrado t1 = feedback(F, 1) _, y1 = step_response(t1, time) SE = np.square(np.subtract(1, y1)) ISE = np.sum(SE) return (ISE)
def classic(): # PI controller Kp = 1.5 Ki = 30. P = tf([Kp], [1]) I = tf([Ki], [1, 0]) Gc = parallel(P, I) # Power convertor Gpc = synthesize_2pole_filter(50, 0.707) # Plant Gp = tf([50], [1, 0]) # Sensor # если выбросить из петли становится лучше # "remove sensor lag" Gs = synthesize_1pole_filter(20) # Openloop Gol = series(series(Gc, Gpc), Gp) # Closed loop Gcl = feedback(Gol) ts, xs = step_response( Gcl ) grid() plot(ts, xs)
def instantiate(self, specs=None): # Sobrepor a anterior: if specs is not None: self.specs = specs self.Kc = insert(1, self.specs, 'Kc') self.Ti = insert(np.inf, self.specs, 'Ti') self.Td = insert(0, self.specs, 'Td') self.b = insert(1.0, self.specs, 'b') self.c = insert(1.0, self.specs, 'c') self.N = insert(9.0, self.specs, 'N') num_p = self.Kc * self.b den_p = 1 Cp = ctrl.tf(num_p, den_p) if (self.Ti != np.inf): num_i = self.Kc den_i = [self.Ti, 0] Ci = ctrl.tf(num_i, den_i) if (self.Td != 0): num_d = [self.Kc * self.Td * self.c, 0] den_d = [(self.Td / self.N), 0] Cd = ctrl.tf(num_d, den_d) self.tf = ctrl.parallel(Cp, Ci if 'Ci' in locals() else 0, Cd if 'Cd' in locals() else 0)
def tr2(numG, denG, numH, denH): G = control.tf(numG, denG) H = control.tf(numH, denH) k = control.parallel(G, H) s1 = "(" s2 = "(" s = str(k) s = s.split('\n') s[1] = s[1].strip() s[3] = s[3].strip() s1 = s1 + s[1] + ')' s2 = s2 + s[3] + ')' t1, y1 = control.step_response(k) plt.plot(t1, y1, 'b', linewidth=1, label='G(s)') plt.grid(True) plt.title('Time response of Parallel Operation\n' + (s1) + '/' + (s2)) plt.xlabel('Time(sec)') plt.ylabel('Amplitude') if not os.path.isdir('static'): os.mkdir('static') else: # Remove old plot files for filename in glob.glob( os.path.join('static', 'SeriesParallel', 'sp2.png')): os.remove(filename) sp2 = os.path.join('static', 'SeriesParallel', 'sp2.png') plt.savefig(sp2) plt.clf() plt.cla() plt.close() return sp2
def test_bdalg_functions(self): """Test block diagram functions algebra on I/O systems""" # Set up parameters for simulation T = self.T U = [np.sin(T), np.cos(T)] X0 = 0 # Set up systems to be composed linsys1 = self.mimo_linsys1 linio1 = ios.LinearIOSystem(linsys1) linsys2 = self.mimo_linsys2 linio2 = ios.LinearIOSystem(linsys2) # Series interconnection linsys_series = ct.series(linsys1, linsys2) iosys_series = ct.series(linio1, linio2) lin_t, lin_y, lin_x = ct.forced_response(linsys_series, T, U, X0) ios_t, ios_y = ios.input_output_response(iosys_series, T, U, X0) np.testing.assert_array_almost_equal(ios_y, lin_y, decimal=3) # Make sure that systems don't commute linsys_series = ct.series(linsys2, linsys1) lin_t, lin_y, lin_x = ct.forced_response(linsys_series, T, U, X0) self.assertFalse((np.abs(lin_y - ios_y) < 1e-3).all()) # Parallel interconnection linsys_parallel = ct.parallel(linsys1, linsys2) iosys_parallel = ct.parallel(linio1, linio2) lin_t, lin_y, lin_x = ct.forced_response(linsys_parallel, T, U, X0) ios_t, ios_y = ios.input_output_response(iosys_parallel, T, U, X0) np.testing.assert_array_almost_equal(ios_y, lin_y, decimal=3) # Negation linsys_negate = ct.negate(linsys1) iosys_negate = ct.negate(linio1) lin_t, lin_y, lin_x = ct.forced_response(linsys_negate, T, U, X0) ios_t, ios_y = ios.input_output_response(iosys_negate, T, U, X0) np.testing.assert_array_almost_equal(ios_y, lin_y, decimal=3) # Feedback interconnection linsys_feedback = ct.feedback(linsys1, linsys2) iosys_feedback = ct.feedback(linio1, linio2) lin_t, lin_y, lin_x = ct.forced_response(linsys_feedback, T, U, X0) ios_t, ios_y = ios.input_output_response(iosys_feedback, T, U, X0) np.testing.assert_array_almost_equal(ios_y, lin_y, decimal=3)
def pid_controller(K_kr, T_kr): num = [10] den = [10, 25, 11, 2] G = ctl.tf(num, den) K_P = 0.6 * K_kr K_I = 1.2 * K_kr / T_kr K_D = 0.072 * K_kr * T_kr K_PID = ctl.parallel(ctl.tf([K_I], [1, 0]), ctl.tf([K_D, 0], [1]), K_P) G_0 = ctl.series(K_PID, G) return ctl.feedback(G_0)
def get_TrFunc_pid_regulator(self): """ definition for calculating transmission function of complex PID regulator type :return: transmission function """ w1 = tf([0, self.k], [0, 1]) w2 = tf([self.Td, 0], [0, 1]) w3 = tf([0, 1], [self.Tu, 0]) w = parallel(w1, w2, w3) return w
def f(X): sis = tf([2], [4, 1]) #tf do sistema 2/(4s + 1) sisp = tf([X[0]], [1]) # tf do controlador proporcional Kp sisi = tf([X[0] * X[1]], [1, 0]) # tf do controlador integral Kp*Ki/s sisd = tf([X[0] * X[2], 0], [1]) # tf do controlador diferencial Kd*s sis2 = crt.parallel(sisp, sisi, sisd) # tf do PID sis3 = crt.series(sis, sis2) # tf do sistema em série com o PID mf = feedback(sis3, 1) # malha fechada do sistema time = np.linspace(0, 20, 100) # tempo de simulação _, y1 = step_response(mf, time) # resposta do sistema em malha fechada com PID erro = sum(abs(np.array((y1 - 1) * time))) # integral do erro return erro
def parallelize(): """ Generates a system of transfer functions for a parallel block combination """ # Create arrays for G(s) numerator_g = np.array([1]) denominator_g = np.array([500, 0, 0]) # Create arrays for G(c) numerator_h = np.array([1, 1]) denominator_h = np.array([1, 2]) # Get transfer functions sys_g = ct.tf(numerator_g, denominator_g) sys_h = ct.tf(numerator_h, denominator_h) sys_tranfer_func = ct.parallel(sys_g, sys_h) td.ouput('Parallel TF', keys=['G(s)', 'G(c)', 'sys tf'], values=[sys_g, sys_h, sys_tranfer_func])
import control as co s = co.tf('s') g1 = 1 / ((s + 1) * (s - 1)) g2 = 1 / (s + 2) g3 = (0.5 * s) / (3 * s**2 + 5 * s + 1) g12 = co.parallel(g1, g2) g123 = co.feedback(g12, g3, -1) g = co.series(g123, g3) gtot = co.series(co.feedback(co.parallel(g1, g2), g3, -1), g3) print(g, gtot)
P = ctrl.tf([1],[1,2,0]) # Modelo del primer Controlador #Kp = 2; Kd = 3; Ki = 0.000000001; pd = 1 #integ = ctrl.tf ([Ki],[1,0]) #deriv = ctrl.tf ([Kd, Kp], [1,pd]) #C = ctrl.parallel (integ, deriv) # Modelo del Controlador Kp = 4; Kd = 2; Ki = .1 integ = ctrl.tf ([Ki],[1,0]) deriv = ctrl.tf ([Kd, 0], [0.1,1]) prop = Kp # Interconección del controlador C = ctrl.parallel (deriv + prop, integ) #derivPuro = ctrl.tf ([Kd, Kp], [0,1]) # Serie del Controlador y la Planta l = ctrl.series (C,P) # Realimentación Unitaria sys = ctrl.feedback(l,1) # Respuesta al escalón unitario y,t = ctrl.matlab.step(sys, T = np.arange(0, Ttotal, DT) ) # Gráfica del escalón unitario #mpl.plot(t,y) #mpl.show()
# Import the required packages import numpy as np import matplotlib.pyplot as plt import control from collections import namedtuple # Define the parameters r = 2 k = 1 # Generate the transfer functions for spring and damper sys_spring = control.TransferFunction([k], [1]) sys_damper = control.TransferFunction([r, 0], [1]) # Combine these appropriately voigt = 1 / (control.parallel(sys_spring, sys_damper)) serial = 1 / (control.series(sys_spring, sys_damper)) voigt_serial = control.series(voigt, voigt) # Display the transfer functions print(voigt) print(serial) print(voigt_serial) # Give the output simple names, and specify the time axis Response = namedtuple('Response', ['t', 'x']) t_out = np.arange(0, 10, 0.1) # Simulate a step response of these three systems r_voigt = Response(*control.step_response(voigt, t_out)) r_serial = Response(*control.step_response(serial, t_out))
actuator = tf2ss(tf([20.2], [1, 20.2])) k_alpha = -0.5 k_q = -0.25 k_i = 1.5 k_p = 0.5 lp_filter = tf2ss(tf([[[k_alpha * 10], [0]]], [[[1, 10], [1]]])) ol_airplane = series(actuator, aircraft) sys = feedback(ol_airplane, lp_filter) q_sys = tf2ss(tf([[[0], [k_q]]], [[[1], [1]]])) sas = feedback(sys, q_sys) root_locus_alt(sas) prop = tf2ss(tf([k_p / k_i], [1])) integral = tf2ss(tf([1], [1, 0])) ff = series(parallel(prop, integral), tf2ss(tf([k_i], [1]))) ol = series(ff, sas) cas = feedback(ol, tf([[[1], [0]]], [[[1], [1]]])) t_final = 6 dt = 0.01 t = array([0, 0.99, 1, 20]) u = array([0, 0, 1, 1]) t_out = linspace(0, t_final, int(t_final / dt + 1)) u_t = interp(t_out, t, u) t, y, x = forced_response(sas, t_out, u_t) plt.plot(t, y[0, :]) plt.show()
variable_type='int', variable_boundaries=varbound, algorithm_parameters=algorithm_param) model.run() convergence = model.report solution = model.output_dict bKp = Kpi + solution['variable'][0] / 100 bKi = Kii + solution['variable'][1] / 100 bKd = Kdi + solution['variable'][2] / 100 bP = bKp bI = tf(bKi, [1, 0]) bD = tf([bKd, 0], [0.1 * bKd, 1]) #União dos blocos PID bC = parallel(bP, bI, bD) # Função Transferência com o Controlador bF = series(bC, G) bt = feedback(bF, 1) _, by = step_response(bt, time) btc = feedback(bC, G) _, bc = step_response(btc, time) ts = feedback(G, 1) _, ys = step_response(ts, time) plt.figure(1) plt.axhline(y=1, color='r', linestyle='--', label='Entrada') plt.plot(time, by, label='Sistema Controlado')
ct.bode_plot(Li) # Compute out the gain and phase margins gm, pm, wcg, wcp = ct.margin(Li) # Compute the sensitivity and complementary sensitivity functions Si = ct.feedback(1, Li) Ti = Li * Si # Check to make sure that the specification is met plt.figure(3) ct.gangof4(Pi, Ci) # Compute out the actual transfer function from u1 to v1 (see L8.2 notes) # Hi = Ci*(1-m*g*Pi)/(1+Ci*Pi) Hi = ct.parallel(ct.feedback(Ci, Pi), -m * g * ct.feedback(Ci * Pi, 1)) plt.figure(4) plt.clf() plt.subplot(221) ct.bode_plot(Hi) # Now design the lateral control system a, b, K = 0.02, 5, 2 Co = -K * ct.tf([1, 0.3], [1, 10]) # another lead compensator Lo = -m * g * Po * Co plt.figure(5) ct.bode_plot(Lo) # margin(Lo) # Finally compute the real outer-loop loop gain + responses
# A simple integrator transfer function with a discrete time step of 1.0 could be implemented as: from scipy import signal import control tf = ([ 1.0, ], [1.0, -1.0], 1.0) t_in = [0.0, 1.0, 2.0, 3.0] u = np.asarray([0.0, 0.0, 1.0, 1.0]) t_out, y = signal.dlsim(tf, u, t=t_in) print "tf: " print tf #num = [[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]]] #den = [[[9., 8., 7.], [6., 5., 4.]], [[3., 2., 1.], [-1., -2., -3.]]] num = [1] den = [1, 1] sys1 = control.tf(num, den) print "tf in series with tf: " print control.series(sys1, sys1) print "tf in parallel with tf: " print control.parallel(sys1, sys1) print "tf in feedback with tf: " print control.feedback(sys1, sys1)
X = model.best_variable erro = model.best_function print(f'Kp = {X[0]}') print(f'Ki = {X[1]}') print(f'Kd = {X[2]}') print(f'erro = {erro}') sis = tf([2], [4, 1]) #tf do sistema 2/(4s + 1) sisp = tf([X[0]], [1]) # tf do controlador proporcional Kp sisi = tf([X[0] * X[1]], [1, 0]) # tf do controlador integral Kp*Ki/s sisd = tf([X[0] * X[2], 0], [1]) # tf do controlador diferencial Kd*s sis2 = crt.parallel(sisp, sisi, sisd) # tf do PID sis3 = crt.series(sis, sis2) # tf do sistema em série com o PID mf = feedback(sis3, 1) # malha fechada do sistema mf_sem_PID = feedback(sis, 1) # malha fechada do sistema sem PID time = np.linspace(0, 20, 100) # tempo de simulação _, y1 = step_response(mf, time) # resposta do sistema em malha fechada com PID _, y2 = step_response(mf_sem_PID, time) # resposta do sistema em malha fechada sem PID _, y3 = step_response(sis, time) # resposta do sistema em malha aberta plt.figure(1) plt.plot(time, y1, label='$y_1(t)$') plt.ylim([-0.1, 3]) plt.xlim([0, 20])
import control as co s = co.tf('s') g1 = 1 / ((s + 1) * (s - 1)) g2 = 1 / (s + 2) g3 = (0.5 * s) / (3 * s ** 2 + 5 * s + 1) g12 = co.parallel(g1, g2) g123 = co.feedback(g12, g3, 1) gtot = co.series(g123, g3) print(gtot)
import control as co import matplotlib.pyplot as plt import numpy as np s = co.tf('s') a = 1 / (s - 3) b = (s + 2) / (s - 2) c = (s + 3) / (s + 4) e = 1 / (s * ((s**2) + 1)) acp = co.parallel(c, a) bap = co.parallel(b, a) acbas = co.series(acp, bap) acbaef = co.feedback(acbas, e, +1) print(acbaef) g_poles = co.pole(acbaef) print("G poles") for elem in g_poles: print(f'{elem: .2f}') g_zeros = co.zero(acbaef) print("G zeros") for elem in g_zeros: print(f'{elem: .2f}') g1_map = co.pzmap(acbaef, plot=True) t = np.linspace(0, 25, 100)
import control as co import matplotlib.pyplot as plt s = co.tf('s') g1 = (s + 2) / (5 * s**3 + 7 * s - 3) g2 = 1 / ((s**2 - 3 * s + 4) * (s + 8)) g12s = co.series(g1, g2) g12p = co.parallel(g1, g2) g12f = co.feedback(g1, g2, -1) print(g12s, g12p, g12f) # G1(s) g1_poles = co.pole(g1) print("g1 poles") for elem in g1_poles: print(f'{elem: .2f}') g1_zeros = co.zero(g1) print("g1 zero") for elem in g1_zeros: print(f'{elem: .2f}') # G2(s) g2_poles = co.pole(g2) print("g2 poles") for elem in g2_poles: print(f'{elem: .2f}') g2_zeros = co.zero(g2) print("g2 zero")