提交 a1f5c66e 编写于 作者: geekincode's avatar geekincode 💬

更新Draw_PID.py, PID_control.py

上级
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import style
fig, axes = plt.subplots()
a = np.linspace(0,240,240)
b = np.linspace(160,161,240)
a1 = np.linspace(0,240,240)
b1 = np.linspace(200,201,240)
x = np.linspace(0,240,240)
y1 = [0]
target = 160
t_error = 0
d_error = 0
y = 0
Kp = 0.07
Ki = 0.003 #设置PID参数
Kd = 0.5
def draw_PID():
global fig,axes, a, b, a1, b1, x, y1, target, d_error, y, Kp, Ki, Kd, t_error
for i in range(len(x)-1):
error = target - y
y = y + error*Kp
t_error = t_error + error
y = y + t_error*Ki
if i != 0:
y = y + (error-d_error)*Kd
#y = y - 3
y1.append(y)
d_error = error
axes.plot(a1, b1, color='gray', label='top')
axes.plot(a, b, color='skyblue', label='Target')
axes.plot(x, y1, color='lightcoral', label='Output')
axes.set(ylabel='Y Ax', xlabel='X Ax', title='PID_control (P I D)',
ylim=[-2,320], xlim=[-2,240])
axes.legend()
plt.show()
fig, axes = plt.subplots()
y1 = [0]
y = 0 #初始化
if __name__ =="__main__": #__main__为主程序脚本文件名,__name__为该程序文件名
draw_PID() #当主程序为当前程序文件时,执行该段代码
#from Draw_PID import draw_PID
import Draw_PID
import tkinter as tk
def function():
#print(e1.get(),e2.get())
Draw_PID.Kp = eval(e1.get())
Draw_PID.Ki = eval(e2.get())
Draw_PID.Kd = eval(e3.get())
Draw_PID.draw_PID()
def e1press1(self):
e1.config(bg='skyblue')
def e1press2(self):
e1.config(bg='white')
def e2press1(self):
e2.config(bg='skyblue')
def e2press2(self):
e2.config(bg='white')
def e3press1(self):
e3.config(bg='skyblue')
def e3press2(self):
e3.config(bg='white')
window = tk.Tk()
window.title('PID_control')
window.geometry('500x300') # 这里的乘是小x
# 第4步,在图形界面上设定标签
var = tk.StringVar() # 将label标签的内容设置为字符类型,用var来接收hit_me函数的传出内容用以显示在标签上
var.set('white')
bgcolor = 'white'
l1 = tk.Label(window, text='Kp:',
font=('Arial Black',12),
width=3, height=1)
l1.place(relx=0.25, rely=0.03)
l2 = tk.Label(window, text='Ki:',
font=('Arial Black',12),
width=3, height=1)
l2.place(relx=0.25, rely=0.13)
l3 = tk.Label(window, text='Kd:',
font=('Arial Black',12),
width=3, height=1)
l3.place(relx=0.25, rely=0.23)
e1 = tk.Entry(window, show = None, bg='white',font=('Arial',10))#显示成明文形式
e1.place(relx=0.35,rely=0.05)
e1.bind('<Enter>',e1press1)
e1.bind('<Leave>',e1press2)
e2 = tk.Entry(window, show = None, bg='white', font=('Arial',10))#显示成明文形式
e2.place(relx=0.35, rely=0.15)
e2.bind('<Enter>',e2press1)
e2.bind('<Leave>',e2press2)
e3 = tk.Entry(window, show = None, bg='white', font=('Arial',10))#显示成明文形式
e3.place(relx=0.35, rely=0.25)
e3.bind('<Enter>',e3press1)
e3.bind('<Leave>',e3press2)
# 第5步,在窗口界面设置放置Button按键Draw_PID.Kp
b = tk.Button(window,
text='Show PID',
font=('Arial', 12),
width=10, height=1,
bg='lightcoral',
command=function)
b.place(relx=0.4, rely=0.75)
# 第6步,主窗口循环显示
window.mainloop()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册