def test_ss_input_with_int_element(self): a = np.array([[0, 1], [-1, -2]], dtype=int) b = np.array([[0], [1]], dtype=int) c = np.array([[0, 1]], dtype=int) d = np.array([[1]], dtype=int) sys = ss(a, b, c, d) sys2 = tf(sys) np.testing.assert_almost_equal(dcgain(sys), dcgain(sys2))
def test_ss_input_with_int_element(self): ident = np.matrix(np.identity(2), dtype=int) a = np.matrix([[0, 1], [-1, -2]], dtype=int) * ident b = np.matrix([[0], [1]], dtype=int) c = np.matrix([[0, 1]], dtype=int) d = 0 sys = ctl.ss(a, b, c, d) sys2 = ctl.ss2tf(sys) self.assertAlmostEqual(ctl.dcgain(sys), ctl.dcgain(sys2))
def test_tf_input_with_int_element_works(self): num = 1 den = np.convolve([1.0, 2, 1], [1, 1, 1]) sys = ctl.tf(num, den) self.assertAlmostEqual(1.0, ctl.dcgain(sys))
def test_tf_num_with_numpy_int_element(self): num = np.convolve([1], [1, 1]) den = np.convolve([1, 2, 1], [1, 1, 1]) sys = ctl.tf(num, den) self.assertAlmostEqual(1.0, ctl.dcgain(sys))
def test_ss_input_with_0int_dcgain(self): a = np.array([[0, 1], [-1, -2]], dtype=int) b = np.array([[0], [1]], dtype=int) c = np.array([[0, 1]], dtype=int) d = 0 sys = ss(a, b, c, d) np.testing.assert_allclose(dcgain(sys), 0, atol=np.finfo(float).epsneg)
def test_tf_num_with_numpy_int_element(self): num = np.convolve([1], [1, 1]) den = np.convolve([1, 2, 1], [1, 1, 1]) sys = tf(num, den) np.testing.assert_array_max_ulp(1., dcgain(sys))
def test_tf_input_with_int_element(self): num = 1 den = np.convolve([1.0, 2, 1], [1, 1, 1]) sys = tf(num, den) np.testing.assert_almost_equal(1., dcgain(sys))
def CaseA(sysg,wctar): R.ur = ((Spec.A + Spec.B) / Spec.e - 1) * 1.3 / G.DCgain R.sysR1 = ctrl.tf([R.ur], [1]) tauAlow = ctrl.dcgain(G.sysG*R.sysR1) / (wctar*1.5) tauAhigh = sqrt(prod(-ctrl.pole(G.sysG*R.sysR1) ** -1) / tauAlow) R.R2NUM=G.DEN R.R2DEN=[tauAhigh * tauAhigh * tauAlow, 2 * tauAhigh * tauAlow, 2 * tauAhigh + tauAlow, 1] R.sysR2 = ctrl.tf(R.R2NUM,R.R2DEN ) R.sysR=R.sysR1*R.sysR2 return R.sysR
def TF_properties(self, window, key, entry1, entry2): transfer_function = self.get_TF(entry1, entry2) if key == "dcgain": dc_gain = co.dcgain(transfer_function) self.print_TF_props(window, dc_gain, "DC Gain:") elif key == "poles": poles = co.pole(transfer_function) self.print_TF_props(window, poles, "Poles:") elif key == "zeros": zeros = co.zero(transfer_function) self.print_TF_props(window, str(zeros), "Zeros:")
def time(tf): print "Poles: ", ctrl.pole(tf) print "Zeros: ", ctrl.zero(tf) dc = ctrl.dcgain(tf) print "DC gain: ", dc t, y = ctrl.step_response(tf) ys = filter(lambda l: l >= 0.98 * dc, y) i = np.ndarray.tolist(y).index(min(ys)) print "Ts: ", t[i] print "Overshoot: ", (max(y) / dc) - 1 i = np.ndarray.tolist(y).index(max(y)) print "Tr: ", t[i]
def time (tf, method = 'step', plot = False): print "=================================================================="; print "Poles: ", ctrl.pole (tf); print "Zeros: ", ctrl.zero (tf); dc = ctrl.dcgain (tf); print "DC gain: ", dc; if (method == 'step'): t, y = ctrl.step_response (tf); if (method == 'impulse'): t, y = ctrl.impulse_response (tf); ys = filter (lambda l: l >= 0.98 * dc, y); i = np.ndarray.tolist(y).index (min (ys)); print "Ts: ", t[i]; print "Overshoot: ", (max (y) / dc) - 1; i = np.ndarray.tolist(y).index (max (y)); print "Tr: ", t[i]; if (plot == True): p = Plotter ({'grid' : True}); p.plot ([(t, y)]); return t, y
import control from cvxpy import Variable, Parameter, Minimize, Problem, OSQP, quad_form if __name__ == "__main__": len_sim = 120 # simulation length (s) # Discrete time model of a frictionless mass (pure integrator) Ts = 1.0 r_den = 0.9 # magnitude of poles wo_den = 0.2 # phase of poles (approx 2.26 kHz) # Build a second-order discrete-time dynamics with dcgain=1 (inner loop model) H_sys = control.TransferFunction( [1], [1, -2 * r_den * np.cos(wo_den), r_den**2], Ts) H_sys = H_sys / control.dcgain(H_sys) H_ss = control.ss(H_sys) # SISO ABCD Ad = np.array(H_ss.A) Bd = np.array(H_ss.B) Cd = np.array(H_ss.C) Dd = np.array(H_ss.D) # MIMO ABCD Ad = scipy.linalg.block_diag(Ad, Ad) Bd = scipy.linalg.block_diag(Bd, Bd) Cd = scipy.linalg.block_diag(Cd, 1.5 * Cd) Dd = scipy.linalg.block_diag(Dd, Dd) [nx, ng] = Bd.shape # number of states and number or inputs
import control as co import numpy as np import matplotlib.pyplot as plt G1 = co.tf([2,5],[1,2,3]) # 2s + 5 / s^2 + 2s + 3 --- Provided only the coeficients G2 = 5*co.tf(np.poly([-2,-5]), np.poly([-4,-5,-9])) # Provided the 'zeros' and the 'poles' of the function # Algebrism is supported G3 = G1+G2 G4 = G1*G2 G5 = G1/G2 G6 = G1-G2 G7 = co.feedback(G1,G2) # Minimum Realization (Pole-Zero Cancellation) co.minreal(G2) # TF Properties print(G1) print('DC Gain: '+ str(co.dcgain(G1))) # DC Gain print('Pole: ' + str(co.pole(G1))) # Pole for G6 print('Zero: ' + str(co.zero(G1))) # Zero for G6
E = LA.eigvals(A) print(E)# One of the eigenvalues is >0 so the energy of the system will blow up to infty P = np.array([-2, -1]) # Place poles at -2, -1 # Note: You can change the aggressiveness by changing the poles K = c.place(A, B, P) # Place new poles Acl = np.array(A - B * K) # Create a closed loop A matrix Ecl = LA.eigvals(Acl) # Calculate new eigenvalues (this evaluates to the poles that I placed!) syscl = c.ss(Acl, B, C, D) # Closed loop state space model step = c.step_response(syscl) Kdc = c.dcgain(syscl) # Calculating a dc gain in order to reach intended output goal Kr = 1 / Kdc sysclNew = c.ss(Acl, B * Kr, C, D) sysclNewDiscrete = matlab.c2d(sysclNew, 0.01) stepNew = c.step_response(sysclNew) stepDiscrete = c.step_response(sysclNewDiscrete) plt.figure(1) yout, T = stepDiscrete plt.plot(yout.T, T.T) plt.xlabel("Time (Seconds)") plt.ylabel("Amplitude")
import control as co import numpy as np import matplotlib.pyplot as plt G1 = co.tf( [2, 5], [1, 2, 3]) # 2s + 5 / s^2 + 2s + 3 --- Provided only the coeficients G2 = 5 * co.tf(np.poly([-2, -5]), np.poly( [-4, -5, -9])) # Provided the 'zeros' and the 'poles' of the function # Algebrism is supported G3 = G1 + G2 G4 = G1 * G2 G5 = G1 / G2 G6 = G1 - G2 G7 = co.feedback(G1, G2) # Minimum Realization (Pole-Zero Cancellation) co.minreal(G2) # TF Properties print(co.dcgain(G6)) # DC Gain print(co.pole(G6)) # Pole for G6 print(co.zero(G6)) # Zero for G6
import numpy as np import control # pylint: disable=import-error dt = 0.01 A = np.array([[0., 1.], [-0.78823806, 1.78060701]]) B = np.array([[-2.23399437e-05], [7.58330763e-08]]) C = np.array([[1., 0.]]) # Kalman tuning Q = np.diag([1, 1]) R = np.atleast_2d(1e5) (_, _, L) = control.dare(A.T, C.T, Q, R) L = L.T # LQR tuning Q = np.diag([2e5, 1e-5]) R = np.atleast_2d(1) (_, _, K) = control.dare(A, B, Q, R) A_cl = (A - B.dot(K)) sys = control.ss(A_cl, B, C, 0, dt) dc_gain = control.dcgain(sys) print(("self.A = np." + A.__repr__()).replace('\n', '')) print(("self.B = np." + B.__repr__()).replace('\n', '')) print(("self.C = np." + C.__repr__()).replace('\n', '')) print(("self.K = np." + K.__repr__()).replace('\n', '')) print(("self.L = np." + L.__repr__()).replace('\n', '')) print("self.dc_gain = " + str(dc_gain))
clsysSat = ctl.feedback(ctl.series(conpid, ss_mod), ufb, sign=-1) # negative fb closed loop system ############################################## Simulations # # Run Linear simulations and analysis # #kr = np.arange(0,0.1,0.0002) #ctl.root_locus(olsys,krange=kr,xlim=(-0.02,0.0),ylim=(-0.01,0.1),grid=True) ctl.root_locus(olsys, xlim=(-0.1, 0.01), ylim=(-.1, 0.1), grid=True) #ctl.root_locus(olsys,grid=True) if False: # compute closed loop response to ref input Rexp t1, y1, x1 = ctl.forced_response(clsys, Texp, [Rexp]) print('DC Gain: {}'.format(ctl.dcgain(clsys))) # compute the control effort (Watts) t1, yce, xce = ctl.forced_response(ctleff, Texp, [Rexp]) for i, y5 in enumerate(y1): y1[i] += tamb # add back ambient temp #ax,fig = plt.subplots() #plt.plot(t1,y1) # x-values #a = plt.gca() #a.set_ylim([50,200]) #plt.title('Closed loop response') ########## Linear Systems Plotting t2 = np.zeros(np.shape(t))
import numpy as np import scipy import control # In[System dynamics] Ts = 1.0 r_den_1 = 0.9 # magnitude of poles wo_den_1 = 0.2 # phase of poles (approx 2.26 kHz) # Build a second-order discrete-time dynamics with dcgain=1 (inner loop model) G_1 = control.TransferFunction([1], [1, -2 * r_den_1 * np.cos(wo_den_1), r_den_1 ** 2], Ts) G_1 = G_1 / control.dcgain(G_1) G_1_ss = control.ss(G_1) # SISO state-space matrices subsystem 11 A_1 = np.array(G_1_ss.A) B_1 = np.array(G_1_ss.B) C_1 = np.array(G_1_ss.C) D_1 = np.array(G_1_ss.D) r_den_2 = 0.9 # magnitude of poles wo_den_2 = 0.4 # phase of poles (approx 2.26 kHz) # Build a second-order discrete-time dynamics with dcgain=1 (inner loop model) G_2 = control.TransferFunction([1], [1, -2 * r_den_2 * np.cos(wo_den_2), r_den_2 ** 2], Ts) G_2 = G_2 / control.dcgain(G_2) G_2_ss = control.ss(G_2) # SISO state-space matrices subsystem 22
import matplotlib.pyplot as plt import control import numpy as np ts = 1.0 r_den = 0.9 # magnitude of poles wo_den = 0.2 # phase of poles (approx 2.26 kHz) H_noise = control.TransferFunction([1], [1, -2 * r_den * np.cos(wo_den), r_den**2], ts) H_noise = H_noise / control.dcgain(H_noise) H_ss = control.ss(H_noise) t, val = control.step_response(H_ss, np.arange(100)) plt.plot(val[0, :])
import numpy as np import matplotlib.pyplot as plt import sympy as sp num = [25.95] den = [1,7.25,9.77] numMpComp = [1,5.46] denMpComp = [1,2.21] numErrComp = [1,1] denErrComp = [1,.00945] sys = co.tf(num,den) mpComp = 0.289*co.tf(numMpComp,denMpComp) errComp = 1.1591*co.tf(numErrComp,denErrComp) mpSys = mpComp*sys errMpSys = errComp*mpSys print('\n\nErro sem compesador:',1/(1+co.dcgain(mpSys))) print('Erro com compensador:',1/(1+co.dcgain(errMpSys))) co.rlocus(sys) plt.title('LGR do sistema original') plt.grid() plt.figure(2) co.rlocus(mpSys) plt.title('LGR do sistema com overshoot compensado') plt.grid() plt.figure(3) co.rlocus(errMpSys) plt.title('LGR do sistema com overshoot e erro compensado') plt.grid() plt.show()