gpt4 book ai didi

python - 如何将命令从 Python turtle 图形发送到 EV3 乐高积木?

转载 作者:太空狗 更新时间:2023-10-29 20:28:50 25 4
gpt4 key购买 nike

为 T KINTER 编辑:

IDE 是 Visual Studio Code

Traceback 调用打印在脚本下方

TkinterTest.py

#!/usr/bin/env python3

from tkinter import *
from tkinter import ttk
import Ev3_Motor

ev3 = Ev3_Motor.Ev3_Motor()

def calculate(*args):
ev3.TestFunction("SUCCESSS YAHOOOOOO")
print("command to robot >>>>")

root = Tk()
root.title("TEST TKINTER")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

ttk.Button(mainframe, text="TEST BUTTON", command=calculate).grid(column=3, row=3, sticky=W)

#ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

root.bind('<Return>', calculate)
root.mainloop()

Ev3_Motor.py

#!/usr/bin/env python3
from ev3dev.ev3 import *
import os
import sys
from time import sleep
import shutil
import fileinput

os.system('setfont Lat15-TerminusBold14')

## FUNCTIONS ##

def __init(self):
debug_print("Constructor Ev3")

def TestFunction(randomString):
debug_print("Connection established: " + randomString)

回溯错误:

Starting: brickrun --directory="/home/robot/vscode-hello-python-master/Ev3" "/home/robot/vscode-hello-python-master/Ev3/TkinterTest.py"
Started.
----------
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 36, in <module>
import _tkinter
ImportError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/robot/vscode-hello-python-master/Ev3/TkinterTest.py", line 3, in <module>
from tkinter import *
File "/usr/lib/python3.5/tkinter/__init__.py", line 38, in <module>
raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package
----------
Exited with error code 1.

** 原始问题:我正在尝试做的事情:**

我正在尝试创建一个程序设计,其中使用 Python 中的 Turtle 图形库创建的 UI 程序直接与乐高 EV3 积木上存在的 EV3 Python 程序进行通信。

我目前拥有的:

  1. FrontEnd.py - 一个面向对象的程序,它根据鼠标指针 x 和 y 以及用于绘制类似对象的按钮的形状感应独特的按钮点击
  2. RobotInstruction.py - 一个基于函数调用转动电机的 EV3 程序

当我试图让它工作时发生了什么:

似乎依赖关系存在冲突。像 turtle 和 ev3 不兼容。在我看来,FrontEnd.py 文件正试图加载到 RobotInstruction.py 文件中,但最终失败了,这不是我想要的。

重要提示:

独立地,这些脚本工作正常。例如,RobotInstruction.py 可以接收键盘输入以作用于电机。我只想让那个“命令”从图形程序中触发

我第一次尝试 Janky super 低效解决方法:

-- 现有代码摘录附在最后 --

使用 FrontEnd.py 将字符串命令写入文件并让 RobotInstruction.py 不断读取该文件以获取命令,然后根据命令调用相关函数来转动电机。

有效的方法:

可以使用来自 FrontEnd.py 的命令成功写入文件

可以从同一个文件成功读取命令

但是

它不会实时发生。我不太熟悉用 Python 读取/写入文件...所以很可能我可能会做一些尴尬的事情...

我的问题:

我想做的事情可行吗?你能点击 turtle 图形创建按钮向 ev3 机器人发送命令吗?如果是这样,我将如何在两个独立的脚本之间形成连接

代码“摘录”

FrontEnd.py

def TurnTier(ButtonName):
if ButtonName == "TurnT1":
fileName = open("file1.txt", "w+")
fileName.write("TurnT1")
fileName.close()

RobotInstruction.py

while (not blnTierFound):
# file1.txt is written to from FrontEnd.py through a button click
# We are writing "TurnT1" into file1.txt
# Here we are opening the same file for reading to check for changes

fileName = open("file1.txt", "r+")
ButtonName = fileName.read()
fileName.close()

ButtonName = str(ButtonName)
if (ButtonName == "TurnT1"):
blnTierFound = True
strMotor = 'A'

# In the main part of the code
motorLeft = fncStartMotor(strMotor)

最佳答案

重要信息:

通常,EV3 是使用乐高的基于 block 的编程语言进行编程的。默认操作系统是用这种语言编程的。为了使用基于文本的编程语言(如 Python)与机器人进行通信,您必须使用双启动 SD 安装一个名为 ev3dev 的新操作系统,该操作系统是 基于 Linux 的卡片。完整的设置说明可用here .在运行以下脚本之前必须进行此设置。

在评论部分进行讨论后,我整理了一个可能适合您的解决方案。这在问题中使用了 Tkinter 脚本(已经过测试可以工作)并且 Ev3_Motor 脚本已被修改为包含一个 Ev3_Motor 类(这使得导入脚本和创建此类的对象变得容易)。但是,此脚本未经测试,可能会产生其他错误,因为我没有 Ev3 机器人。稍后可以调试这些错误。确保 Ev3_Motor.py 与 TkinterTest.py 位于同一目录中。

Ev3_Motor.py

#!/usr/bin/env python3
from ev3dev.ev3 import *
import os
import sys
from time import sleep
import shutil
import fileinput
import debug_print

os.system('setfont Lat15-TerminusBold14')

## Main Ev3 Motor Class ##
class Ev3_Motor:

def __init__(self):
debug_print("Constructor Ev3")

def TestFunction(randomString):
debug_print("Connection established: " + randomString)

TkinterTest.py

#!/usr/bin/env python3
from tkinter import *
from tkinter import ttk
import Ev3_Motor

ev3 = Ev3_Motor.Ev3_Motor()

def calculate(*args):
ev3.TestFunction("SUCCESSS YAHOOOOOO")
print("command to robot >>>>")

root = Tk()
root.title("TEST TKINTER")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

ttk.Button(mainframe, text="TEST BUTTON", command=calculate).grid(column=3, row=3, sticky=W)

#ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

root.bind('<Return>', calculate)
root.mainloop()

关于python - 如何将命令从 Python turtle 图形发送到 EV3 乐高积木?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53797319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com