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

ryv*_*ron 9 python robotics turtle-graphics python-3.x

编辑T KINTER:

IDE是Visual Studio Code

跟踪调用打印在脚本下方

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()
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

TRACEBACK错误:

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.
Run Code Online (Sandbox Code Playgroud)

**原始问题:我正在尝试做什么:**

我正在尝试创建一个程序设计,其中使用Python中的Turtle Graphics Library创建的UI程序直接与LEGO 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机器人发送命令吗?如果是这样,我将如何在两个单独的脚本之间形成连接

代码"EXCERPTS"

FrontEnd.py

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

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)
Run Code Online (Sandbox Code Playgroud)

ama*_*anb 3

重要信息

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

经过评论部分的讨论,我整理了一个可能适合您的解决方案。这利用了问题中的 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)
Run Code Online (Sandbox Code Playgroud)

Tkinter测试.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()
Run Code Online (Sandbox Code Playgroud)