如何将Python乌龟图形的命令发送到EV3乐高积木



编辑T KINTER:/strong>

IDE是Visual Studio代码

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 Graphics Library创建的UI程序与乐高EV3积木上的EV3 Python程序直接通信

到目前为止我所拥有的:

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

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

依赖关系似乎发生了冲突。就像乌龟和ev3不兼容。在我看来,FrontEnd.py文件试图加载到RobotInstruction.py文件中,结果却出现在砖块上,这不是我想要的。

重要提示:

独立地说,这些脚本运行良好。例如,RobotInstruction.py可以接收键盘输入以对电机进行操作。我只想从图形程序中获得"命令",而不是

我第一次尝试Janky Super Inefficient Workaround:

-现有代码异常附在末尾--

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

工作原理:

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

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

但是

它不是实时发生的。我对Python的文件读/写不是很熟悉。。。很有可能我在做一些尴尬的事情。。。

我的问题:

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

代码"异常">

前端.py

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

机器人指令.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通常使用乐高公司的基于块的编程语言进行编程。默认操作系统是用这种语言编程的。为了使用Python等基于文本的编程语言与机器人通信,您必须使用双启动SD卡安装一个名为ev3dev的新操作系统,该系统基于Linux。此处提供完整的设置说明。在运行以下脚本之前,此设置是必需的。

经过评论部分的讨论,我提出了一个可能对您有用的解决方案。这使用了问题中的Tkinter脚本(已测试工作(,并且Ev3_Motor脚本已被修改为包含Ev3_Motors类(这使得导入脚本和创建此类对象变得容易(。然而,这个脚本是未经测试的,可能会产生其他错误,因为我没有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()

最新更新