|
11 | 11 | import winsound
|
12 | 12 | from simple_pid import PID
|
13 | 13 |
|
| 14 | +import matplotlib |
| 15 | +import matplotlib.pyplot as plt |
| 16 | + |
14 | 17 | ads = 'ads'
|
15 | 18 | pidc = 'pidc'
|
16 | 19 | size = 'size'
|
|
20 | 23 | head = 'head'
|
21 | 24 | left = 'left'
|
22 | 25 | title = 'title'
|
| 26 | +debug = 'debug' |
23 | 27 | region = 'region'
|
24 | 28 | center = 'center'
|
25 | 29 | radius = 'radius'
|
|
42 | 46 | show: False, # 显示, Down
|
43 | 47 | head: False, # 瞄头, Up
|
44 | 48 | pidc: False, # 是否启用 PID Controller, 还未完善, Left
|
45 |
| - left: False, # 左键锁, PgDn, 按左键时锁 |
| 49 | + left: False, # 左键锁, Right, 按鼠标左键时锁 |
| 50 | + debug: False, # Debug 模式, 用来调试 PID 值 |
46 | 51 | }
|
47 | 52 |
|
48 | 53 |
|
@@ -90,10 +95,11 @@ def release(key):
|
90 | 95 | data[pidc] = not data[pidc]
|
91 | 96 | winsound.Beep(800 if data[pidc] else 400, 200)
|
92 | 97 | elif key == Key.right:
|
93 |
| - pass |
94 |
| - elif key == Key.page_down: |
95 | 98 | data[left] = not data[left]
|
96 | 99 | winsound.Beep(800 if data[left] else 400, 200)
|
| 100 | + elif key == Key.page_down: |
| 101 | + data[debug] = not data[debug] |
| 102 | + winsound.Beep(800 if data[debug] else 400, 200) |
97 | 103 |
|
98 | 104 | with Listener(on_release=release, on_press=press) as k:
|
99 | 105 | k.join()
|
@@ -169,8 +175,10 @@ def follow(aims):
|
169 | 175 | return targets[index]
|
170 | 176 |
|
171 | 177 | text = 'Realtime Screen Capture Detect'
|
172 |
| - pidx = PID(2, 0, 0, setpoint=0) |
173 |
| - pidy = PID(2, 0, 0, setpoint=0) |
| 178 | + pidx = PID(2, 0, 0.02, setpoint=0) |
| 179 | + pidx.output_limits = [-50, 50] |
| 180 | + pidy = PID(2, 0, 0.02, setpoint=0) |
| 181 | + times, targets, distances = [], [], [] # 用于绘图 |
174 | 182 |
|
175 | 183 | # 主循环
|
176 | 184 | while True:
|
@@ -210,14 +218,33 @@ def follow(aims):
|
210 | 218 | x = sx - cx
|
211 | 219 | y = sy - cy
|
212 | 220 | if data[pidc]:
|
| 221 | + if data[debug]: # 用于绘图 |
| 222 | + times.append(time.time()) |
| 223 | + targets.append(0) |
| 224 | + distances.append(x) |
213 | 225 | px = int(pidx(x))
|
214 | 226 | py = int(pidy(y))
|
215 | 227 | move(-px, -py)
|
216 | 228 | else:
|
217 | 229 | ax = int(x * data[ads])
|
218 | 230 | ay = int(y * data[ads])
|
219 | 231 | move(ax, ay)
|
220 |
| - |
| 232 | + else: # 用于绘图 |
| 233 | + if data[debug] and len(times) != 0: |
| 234 | + try: |
| 235 | + plt.plot(times, targets, label='target') |
| 236 | + plt.plot(times, distances, label='distance') |
| 237 | + plt.legend() # 图例 |
| 238 | + plt.xlabel('time') |
| 239 | + plt.ylabel('distance') |
| 240 | + times.clear() |
| 241 | + targets.clear() |
| 242 | + distances.clear() |
| 243 | + matplotlib.use('TkAgg') # TkAgg, module://backend_interagg |
| 244 | + winsound.Beep(600, 200) |
| 245 | + plt.show() |
| 246 | + except: |
| 247 | + pass |
221 | 248 | # 显示检测
|
222 | 249 | if data[show] and img is not None:
|
223 | 250 | cv2.namedWindow(text, cv2.WINDOW_AUTOSIZE)
|
|
0 commit comments