Beispiel #1
0
LS2 = func.linestyle_generator()
fig, ax = plt.subplots(2, 1)
fig, ax2 = plt.subplots()

for i in range(len(kd)):
    K = tf([kd[i], kp, ki], [1, 0])  # proportional + integral control
    H = P * K  # open loop system
    gain, phase, w = bode(H, logspace(-1, 2), Plot=False)

    # closed loop system
    Gyr = feedback(P * K, 1)
    y, t = step(Gyr, np.arange(0, 2, 0.01))
    pltargs2 = {'ls': next(LS2), 'label': '$k_D$=' + str(kd[i])}
    ax2.plot(t, y * ref, **pltargs2)

    # bode diagram
    pltargs = {'ls': next(LS), 'label': '$k_D$=' + str(kd[i])}
    ax[0].semilogx(w, 20 * np.log10(gain), **pltargs)
    ax[1].semilogx(w, phase * 180 / np.pi, **pltargs)

    # gain margin, phase margen, phase cross frequency, gain cross frequency
    print('kP = ', kp, 'kI = ', ki, 'kD = ', kd[i])
    print('(GM,PM,wpc,wgc)')
    print(margin(H))
    print('--------------------')

ax2.axhline(ref, color="k", linewidth=0.5)
func.bodeplot_set(ax, 3)
func.plot_set(ax2, 't', 'y', 'best')
plt.show()
H0 = P  # base open loop system
H1 = P * K1  # open loop system with phase lag complement

gain0, phase0, w0 = bode(H0, logspace(-1, 2), Plot=False)
gain1, phase1, w1 = bode(H1, logspace(-1, 2), Plot=False)

fig, ax0 = plt.subplots(2, 1)
fig, ax1 = plt.subplots(2, 1)

ax0[0].semilogx(w0, 20 * np.log10(gain0), color='k')
ax0[1].semilogx(w0, phase0 * 180 / np.pi, color='k')

ax1[0].semilogx(w1, 20 * np.log10(gain1), color='b')
ax1[1].semilogx(w1, phase1 * 180 / np.pi, color='b')
func.bodeplot_set(ax0)
func.bodeplot_set(ax1)

# check gain and phase at 40 rad/sec
[[[mag]]], [[[phase]]], omega = freqresp(H1, [40])
magH1at40 = mag
phaseH1at40 = phase * (180 / np.pi)
print('------------------')
print('gain at 40 rad/s = ', 20 * np.log10(magH1at40))
print('phase at 40 rad/s = ', phaseH1at40)

# compensation of phase lead
# get value to be compensated
phim = (60 - (180 + phaseH1at40)) * np.pi / 180
beta = (1 - np.sin(phim)) / (1 + np.sin(phim))
Beispiel #3
0
# the case that closed loop system will be unstable
P = tf([0, 1], [1, 1, 1.5, 1])
x, y, _ = nyquist(P, logspace(-3, 5, 1000), Plot=False)
ax[0].plot(x, y, color='k')
ax[0].plot(x, -y, ls='--', color='k')
ax[0].scatter(-1, 0, color='k')
func.plot_set(ax[0], 'Re', 'Im')

gain, phase, w = bode(P, logspace(-2, 2), Plot=False)
pltargs = {'ls': next(LS), 'label': 'P=' + str(P)}
ax2[0].semilogx(w, 20 * np.log10(gain), **pltargs)
ax2[1].semilogx(w, phase * 180 / np.pi, **pltargs)

# the case that closed loop system will be stable
P = tf([0, 1], [1, 2, 2, 1])
x, y, _ = nyquist(P, logspace(-3, 5, 1000), Plot=False)
ax[1].plot(x, y, color='k')
ax[1].plot(x, -y, ls='--', color='k')
ax[1].scatter(-1, 0, color='k')
func.plot_set(ax[1], 'Re', 'Im')

gain, phase, w = bode(P, logspace(-2, 2), Plot=False)
pltargs = {'ls': next(LS), 'label': 'P=' + str(P)}
ax2[0].semilogx(w, 20 * np.log10(gain), **pltargs)
ax2[1].semilogx(w, phase * 180 / np.pi, **pltargs)

fig.tight_layout()
func.bodeplot_set(ax2, 3, 3)
plt.show()
from control.matlab import *
import matplotlib.pyplot as plt
import numpy as np
import sympy as sp
import function_chapt6 as func

alpha = 10
T1 = 0.1
K1 = tf([alpha * T1, alpha], [alpha * T1, 1])
gain, phase, w = bode(K1, logspace(-2, 3), Plot=False)
fig, ax = plt.subplots(2, 1)
ax[0].semilogx(w, 20 * np.log10(gain), color='k')
ax[1].semilogx(w, phase * 180 / np.pi, color='k')
func.bodeplot_set(ax)
plt.show()
[[[mag2]]], [[[phase2]]], omega2 = freqresp(H2, [40])
magH2at40 = mag2
phaseH2at40 = phase2 * (180 / np.pi)
# compensation of gain
k = 1 / magH2at40  # gain compensation
H3 = P * k * K1 * K2  # open loop system with all types of compensation

# closed loop system after tuning
Gyr_H = feedback(H3, 1)
y, t = step(Gyr_H, np.arange(0, 2, 0.01))
ax.plot(t, y * ref, label='After', color='k')

gain, phase, w = bode(Gyr_H, logspace(-1, 2), Plot=False)
ax1[0].semilogx(w, 20 * np.log10(gain), label='After')
ax1[1].semilogx(w, phase * 180 / np.pi, label='After')

# step response of closed loop system before tuning
Gyr_P = feedback(H0, 1)
y, t = step(Gyr_P, np.arange(0, 2, 0.01))
pltargs = {'ls': '--', 'label': 'Before'}
ax.plot(t, y * ref, **pltargs)

gain, phase, w = bode(Gyr_P, logspace(-1, 2), Plot=False)
ax1[0].semilogx(w, 20 * np.log10(gain), **pltargs)
ax1[1].semilogx(w, phase * 180 / np.pi, **pltargs)

ax.axhline(ref, color="k", linewidth=0.5)
func.plot_set(ax, 't', 'y', 'best')
func.bodeplot_set(ax1)
plt.show()