Python自动控制(三)

本文探讨了Python在实现二自由度控制中的应用,重点讲解了PI-D和I-PD控制器的设计与实现,为自动化控制提供了解决方案。
部署运行你感兴趣的模型镜像

二自由度之PI-D与I-PD控制

import control as ctrl
from control.matlab import *
import sympy as sp
import matplotlib.pyplot as plt
import numpy as np

#--------------------------------------------------------
def linestyle_generator():
    linestyle = ['-', '--', '-.', ':']
    lineID = 0
    while True:
        yield linestyle[lineID]
        lineID = (lineID + 1) % len(linestyle)

def plot_set(fig_ax, *argv):
    fig_ax.set_xlabel(argv[0])
    fig_ax.set_ylabel(argv[1])

    fig_ax.grid(ls=':')
    if len(argv) == 3:
        fig_ax.legend(loc=argv[2])

def bodeplot_set(fig_ax, *argv):
    fig_ax[0].grid(which='both', ls=':')
    fig_ax[0].set_ylabel('Gain [dB')

    fig_ax[1].grid(which='both', ls=':')
    fig_ax[1].set_xlabel('$\omega$ [rad/s]')
    fig_ax[1].set_ylabel('Phase [deg')

    if len(argv) > 0:
        fig_ax[1].legend(loc=argv[0])
    if len(argv) > 1:
        fig_ax[0].legend(loc=argv[1])
#--------------------------------------------------------

g = 9.81 
L = 0.2
M = 0.5
mu = 1.5e-2
J = 1.0e-2

P = ctrl.tf([0, 1], [J, mu, M*g*L])
ref = 30

#--------------------------------------------------------

kp = 2
ki = 10
kd = 0.1

K1 = ctrl.tf([kd, kp, ki], [1, 0])
K2 = ctrl.tf([kp, ki], [kd, kp, ki])

Gyz = ctrl.feedback(P * K1, 1)
Td = np.arange(0, 2, 0.01)
r = 1 * (Td > 0)

z, t, _ = lsim(K2, r, Td, 0)
z, t, _ = lsim(K3, r, Td, 0)
fig, ax = plt.subplots(1, 2)

y, _, _ = lsim(Gyz, r, Td, 0)
ax[0].plot(t, r*ref, color='k')
ax[1].plot(t, y*ref, ls='--', label='PID', color='k')

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[1].axhline(ref, color='k', linewidth=0.5)
plot_set(ax[0], 't', 'r')
plot_set(ax[1], 't', 'r', 'best')

 

import control as ctrl
from control.matlab import *
import sympy as sp
import matplotlib.pyplot as plt
import numpy as np

#--------------------------------------------------------
def linestyle_generator():
    linestyle = ['-', '--', '-.', ':']
    lineID = 0
    while True:
        yield linestyle[lineID]
        lineID = (lineID + 1) % len(linestyle)

def plot_set(fig_ax, *argv):
    fig_ax.set_xlabel(argv[0])
    fig_ax.set_ylabel(argv[1])

    fig_ax.grid(ls=':')
    if len(argv) == 3:
        fig_ax.legend(loc=argv[2])

def bodeplot_set(fig_ax, *argv):
    fig_ax[0].grid(which='both', ls=':')
    fig_ax[0].set_ylabel('Gain [dB')

    fig_ax[1].grid(which='both', ls=':')
    fig_ax[1].set_xlabel('$\omega$ [rad/s]')
    fig_ax[1].set_ylabel('Phase [deg')

    if len(argv) > 0:
        fig_ax[1].legend(loc=argv[0])
    if len(argv) > 1:
        fig_ax[0].legend(loc=argv[1])
#--------------------------------------------------------

g = 9.81 
L = 0.2
M = 0.5
mu = 1.5e-2
J = 1.0e-2

P = ctrl.tf([0, 1], [J, mu, M*g*L])
ref = 30

#--------------------------------------------------------

kp = 2
ki = 10
kd = 0.1

K1 = ctrl.tf([kd, kp, ki], [1, 0])
K3 = ctrl.tf([0, ki], [kd, kp, ki])

Gyz = ctrl.feedback(P * K1, 1)
Td = np.arange(0, 2, 0.01)
r = 1 * (Td > 0)

z, t, _ = lsim(K3, r, Td, 0)
fig, ax = plt.subplots(1, 2)

y, _, _ = lsim(Gyz, r, Td, 0)
ax[0].plot(t, r*ref, color='k')
ax[1].plot(t, y*ref, ls='--', label='PID', color='k')

y, _, _ = lsim(Gyz, z, Td, 0)
ax[0].plot(t, z*ref, color='k')
ax[1].plot(t, y*ref, label='I-PD', color='k')

ax[1].axhline(ref, color='k', linewidth=0.5)
plot_set(ax[0], 't', 'r')
plot_set(ax[1], 't', 'r', 'best')

 

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

issta

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值