-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
234 lines (187 loc) · 6.15 KB
/
main.py
File metadata and controls
234 lines (187 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# -*- coding: utf-8 -*-
'''
PID调参小工具 Python - tkinter
TODO 设定整体的背景颜色
TODO 美化页面布局
TODO 纵轴的取值范围问题
TODO Target值 中间画一条红线
TODO Y轴随着Target取值范围取
'''
import sys
import serial
import struct
import time
import math
import tkinter as tk
import matplotlib
import numpy as np
# import threading
# import time
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
# PID参数
kp = 0
ki = 0
kd = 0
real_value_set = [] # Y轴数据,实际值的集合
canvas_time_width = 100 # 窗口的宽度
target_value = 0 # 目标值
matplotlib.use('TkAgg')
'''
初始化串口通信相关
'''
# 串口号 默认为 /dev/ttyUSB0
ser_dev = '/dev/ttyUSB1'
# 创建一个串口实例
ser = serial.Serial(ser_dev, 115200, timeout=1, bytesize=8)
# tkinter组件
root = tk.Tk()
root.title('PID调参小工具-1Z实验室(1zlab.com)')
root.geometry('600x400')
pid_info_label = tk.Label(root, width=50, height=3, text='PID调参小工具-1Z实验室', font=('Arial', 15))
pid_info_label.pack(side = tk.TOP)
# 上位机上部
top_frame = tk.Frame(root)
top_frame.pack(side=tk.TOP)
# 上位机下部
bottom_frame = tk.Frame(root)
bottom_frame.pack(side=tk.BOTTOM)
# 创建左侧的Frame
left_frame = tk.Frame(top_frame)
left_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.NO)
# left_frame.geometry('500x250')
right_frame = tk.Frame(top_frame)
right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=tk.NO)
# right_frame.geometry('500x250')
target_set_frame = tk.Frame(left_frame)
# 目标值的输入框
target_input = tk.Entry(target_set_frame, width=10, font=('Arial', 20))
target_input.pack(side=tk.LEFT)
# 设定目标值按钮
def set_target():
# TODO 对input的值进行检测
global target_value
global target_input
try:
target_value = float(target_input.get())
except ValueError:
print('[ERROR] 非法的目标值设定, 只能是浮点数')
data_str = ','.join(['SET_TARGET', str(target_value)])
data_str += '\n' # 添加换行符号
data_byte = data_str.encode('utf-8')
ser.write(data_byte)
button =tk.Button(master=target_set_frame, text='设定目标值', command=set_target, width=10, height=2, font=('Arial', 12))
button.pack(side=tk.RIGHT)
target_set_frame.pack()
def update_pid_info():
# 更新PID信息
global kp
global ki
global kd
global pid_info_label
global ser
pid_info_label.config(text='KP={} , KI={} , KD = {}'.format(kp, ki, kd))
data_str = ','.join(['SET_PID', str(kp), str(ki) , str(kd)])
data_str += '\n' # 添加换行符号
data_byte = data_str.encode('utf-8')
ser.write(data_byte)
# 回调函数 更新PID参数
def update_kp(new_pd):
global kp
kp = -1 * float(new_pd)
update_pid_info()
def update_ki(new_pi):
global ki
ki = -1 * float(new_pi)
update_pid_info()
def update_kd(new_pd):
global kd
kd = -1 * float(new_pd)
update_pid_info()
# 显示PID信息的Label
pid_info_label = tk.Label(left_frame, bg='LightCyan', width=50, height=3, text='empty', font=('Arial', 15))
pid_info_label.pack()
# KP 滑动条
kp_scale = tk.Scale(right_frame, label='KP', from_=0, to=15, orient=tk.HORIZONTAL,
length=500, showvalue=0, tickinterval=3, resolution=0.01, command=update_kp)
kp_scale.pack()
# KI 滑动条
ki_scale = tk.Scale(right_frame, label='KI', from_=0, to=5, orient=tk.HORIZONTAL,
length=500, showvalue=0, tickinterval=1, resolution=0.01, command=update_ki)
ki_scale.pack()
# KD 滑动条
kd_scale = tk.Scale(right_frame, label='KD', from_=0, to=10, orient=tk.HORIZONTAL,
length=500, showvalue=0, tickinterval=2, resolution=0.01, command=update_kd)
kd_scale.pack()
def update_real_value(new_real_value):
global canvas
global bottom_frame
global figure
global real_value_set # Y轴数据,实际值的集合
global canvas_time_width # 窗口的宽度
new_real_value = float(new_real_value)
# 更新实际值,并更新画面
print('update real value {}'.format(new_real_value))
real_value_set.append(new_real_value)
while len(real_value_set) > canvas_time_width:
real_value_set.pop(0)
def cmd_process(data_str):
cmd_list = {
'REAL_VALUE': update_real_value
}
params = data_str.split(',')
cmd_name = params[0]
if cmd_name in cmd_list:
cmd_list[cmd_name](*params[1:])
else:
print("[ERROR] 非法指令 {}".format(cmd_name))
# 定时器回调
# 每隔0.05s执行一次
def timer_callback():
global ser
global canvas
global figure
global target_input
# 串口接收数据
while ser.in_waiting:
# 判断串口是不是有数据读进来
data_byte = ser.readline()
data_str = data_byte.decode('utf-8')
print('RECIEVE')
print(data_str)
cmd_process(data_str)
if len(target_input.get()) == 0:
target = 0
else:
target = float(target_input.get())
# 更新图像
figure.clf()
axes = figure.add_subplot(111)
axes.set_xlim(0, canvas_time_width)
axes.set_xticks([])
axes.set_ylim(-180 + target, target+180)
axes.set_yticks([-180 + target, target, target+180])
#绘制图形
axes.plot(np.arange(0,len(real_value_set)), np.array(real_value_set))
axes.plot([0, 100], [target, target], color='red')
canvas.draw()
# 100ms 更新一次
bottom_frame.after(1, timer_callback)
# 设置图形尺寸与质量
figure = Figure(figsize=(10,4), dpi=100)
axes = figure.add_subplot(111)
# 绘制图形
axes.plot(np.arange(len(real_value_set)), real_value_set)
# 把绘制的图形显示到tkinter窗口上
canvas = FigureCanvasTkAgg(figure, master=bottom_frame)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# 把matplotlib绘制图形的导航工具栏显示到tkinter窗口上
toolbar = NavigationToolbar2TkAgg(canvas, bottom_frame)
toolbar.update()
canvas._tkcanvas.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1)
# 执行定时器
bottom_frame.after(1, timer_callback)
# 程序主循环
root.mainloop()