Python Tkinter画布绘制串行数据流 (数学问题)

问题描述 投票:1回答:1

我正试图可视化来自一个声纳传感器的数据,该传感器来回转动180度。

读取传感器数据后,用一个regex变量 "dist "包含一个代表距离的float,单位是cm.变量 "angl "包含一个0到180之间的整数,代表旋转状态。

到目前为止,一切都很好,但我不能完全理解如何用Tkinter在画布上表示这些。我尝试了各种方法,但为了这条线,我稍微清理了一下代码。

我们的目标是以画布上的点(1000, 1000)为中心,将绿色圆圈移动到(x, y)坐标上,并相应地进行缩放。

下面是一个终端的读出示例

ANGLE:174 DISTANCE:208.11 X:-72.99856014995218 Y:-194.88710146142 ANGLE:175 DISTANCE:161.67 X:96.75694368800949 Y:-129. 51943000243384 ANGLE:176 距离:100.88 X:100.62718668260311 Y:7.13748557578522 ANGLE:177 距离:43.61 X:20.907170903220738 Y:38.27169064496002

import serial
import re
import math
import tkinter
import time

w_width = 2000
w_height = 1000


def create_animation_window():
    window = tkinter.Tk()
    window.title("WALL-E SONAR")
    window.geometry(f'{w_width}x{w_height}')
    return window

def create_animation_canvas(window):
    canvas = tkinter.Canvas(window)
    canvas.configure(bg="black")
    canvas.pack(fill="both", expand=True)
    return canvas


ser = serial.Serial('/dev/ttyACM0', 9600)
strPort = '/dev/ttyACM0'
def animate_sonar(window, canvas):
    intercept = canvas.create_oval(0,0,20,20, fill="green")

    while True:
        rawData = ser.readline() # rawData input example: b'D:140.98A:57\r\n
        decodedData = rawData.decode("utf-8")
        line = re.search(r'D:(.*)A:(.*)\r\n', decodedData)

        if line:
            dist = float(line.group(1))
            angl = int(line.group(2))


            print(f"ANGLE:  {angl}")
            print(f"DISTANCE: {dist}")
            x = dist * math.cos(angl)
            y = dist * math.sin(angl)
            print(f"X:  {x}")
            print(f"Y:  {y}")

            canvas.moveto(intercept, x,y)

            window.update()



animation_window = create_animation_window()
animation_canvas = create_animation_canvas(animation_window)
animate_sonar(animation_window, animation_canvas)
python canvas tkinter coordinates trigonometry
1个回答
0
投票

我看到的东西不多。

  • 你在sin和cos中使用的角度应该是弧度。
  • 你必须使用相对于画布的位置

这是我要做的。

import math
import tkinter
import time

window = tkinter.Tk()
window.title("WALL-E SONAR")
window.geometry(f'2000x1000')

canvas = tkinter.Canvas(window)
canvas.configure(bg="black")
canvas.pack(fill="both", expand=True)


def animate_sonar(window, canvas):
    dist = 200
    for angl in range(180, 360):
        rad = angl * math.pi / 180
        x = dist * math.cos(rad) + 300
        y = dist * math.sin(rad) + 300

        canvas.delete("all")
        canvas.create_oval(x-10, y-10, x+10, y+10, fill="green")
        canvas.create_line(300, 300, x, y, fill="red")
        window.update()
        time.sleep(0.05)


animate_sonar(window, canvas)
window.mainloop()

如果你运行它,你应该看到这样的东西:

我的代码中的300是用来放一些东西的 但你必须利用你的窗口和画布的尺寸 把它正确地放在你想要的地方。

        rad = angl * math.pi / 180
        x = dist * math.cos(rad) + 300
        y = dist * math.sin(rad) + 300

从真正简单的东西开始,模拟数据,这样会让别人更容易重现你的代码,然后结合你的传感器。当我第一次读到这个问题时,我几乎关闭了页面,我的第一个想法是没有那个传感器,我不可能重现这个......。

我也用了 canvas.delete("all") 你的 canvas.moveto 给我的错误,但如果它为你完美的工作,以类似于我的例子的方式使用它。

希望这能让你走上正确的道路......

© www.soinside.com 2019 - 2024. All rights reserved.