ki = (0, 5, 10) LS = func.linestyle_generator() fig1, ax1 = plt.subplots() for i in range(len(ki)): K = tf([kd, kp, ki[i]], [1, 0]) # proportional + defferential + integral control Gyr = feedback(P * K, 1) # closed loop system y, t = step(Gyr, np.arange(0, 2, 0.01)) # step response pltargs = {'ls': next(LS), 'label': '$k_I$=' + str(ki[i])} ax1.plot(t, y * ref, **pltargs) ax1.axhline(ref, color='k', linewidth=0.5) func.plot_set(ax1, 't', 'y', 'best') fig, ax2 = plt.subplots(2, 1) for i in range(len(ki)): K = tf([kd, kp, ki[i]], [1, 0]) # proportional control Gyr = feedback(P * K, 1) # closed loop system gain, phase, w = bode(Gyr, logspace(-1, 2), Plot=False) # bode diagram pltargs = {'ls': next(LS), 'label': '$k_I$=' + str(ki[i])} ax2[0].semilogx(w, 20 * np.log10(gain), **pltargs) ax2[1].semilogx(w, phase * 180 / np.pi, **pltargs) func.bodeplot_set(ax2, 'lower left') plt.show()
Rule[1] = 'No Overshoot' kp[1] = 0.2 * kp0 ki[1] = kp[1] / (0.5 * T0) kd[1] = kp[1] * (0.33 * T0) LS = func.linestyle_generator() fig, ax = plt.subplots() P = tf([0, 1], [J, mu, M * g * l]) ref = 30 # target angle[deg] num_delay, den_delay = pade(0.005, 1) # dead time system Pdelay = P * tf(num_delay, den_delay) for i in range(2): K = tf([kd[i], kp[i], ki[i]], [1, 0]) Gyr = feedback(Pdelay * K, 1) y, t = step(Gyr, np.arange(0, 2, 0.01)) ax.plot(t, y * ref, ls=next(LS), label=Rule[i]) print(Rule[i]) print('kp = ', kp[i]) print('ki = ', ki[i]) print('kd = ', kd[i]) print('--------------') ax.axhline(ref, color="k", linewidth=0.5) func.plot_set(ax, 't', 'y') plt.show()
from control.matlab import * from control import * import matplotlib.pyplot as plt import function_chapt5 as func import numpy as np import sympy as sp from scipy import linalg import slycot A = '0 1;-4 5' B = '0;1' C = '1 0; 0 1' D = '0;0' P = ss(A, B, C, D) Pole = [-1, -1] F = -acker(P.A, P.B, Pole) Ac1 = P.A + P.B * F Pfb = ss(Ac1, P.B, P.C, P.D) # system that is get stabled by state feedback Td = np.arange(0, 8, 0.01) Ud = 0.2 * (Td > 0) # step disterbance x, t, _ = lsim(Pfb, Ud, Td, [-0.3, 0.4]) fig, ax = plt.subplots() ax.plot(t, x[:, 0], label='$x_1$') ax.plot(t, x[:, 1], ls='-.', label='$x_2$') func.plot_set(ax, 't', 'x', 'best') plt.show()
# transfer function from (formed) target z(r) to u K1p = feedback(K1, P) fig, ax = plt.subplots(1, 3) # PID control(suppose z = r) y, _, _ = lsim(Gyz, r, Td, 0) # y, _, _ = lsim(K1p, r, Td, 0) ax[0].plot(t, r * ref, color='k') ax[1].plot(t, y * ref, ls='--', label='PID', color='k') # ax[2].plot(t, u * ref, ls='--', label='PID', color='k') # PI-D control y, _, _ = lsim(Gyz, z, Td, 0) ax[0].plot(t, z * ref, color='k') ax[1].plot(t, y * ref, label='PI-D', color='k') # ax[2].plot(t, u2 * ref, ls='--', label='PI-D', color='k') # I-PD control y, _, _ = lsim(Gyz, z2, Td, 0) ax[0].plot(t, z2 * ref, color='k') ax[1].plot(t, y * ref, ls=':', label='I-PD', color='k') # ax[2].plot(t, u3 * ref, ls='--', label='I-PD', color='k') ax[1].axhline(ref, color='k', linewidth=0.5) func.plot_set(ax[0], 't', 'r') func.plot_set(ax[1], 't', 'r', 'best') # func.plot_set(ax[2], 't', 'r', 'best') plt.show()