Wednesday, 19 December 2018

How to send commands from Python turtle graphics to an EV3 Lego brick?

What I'm trying to do:

I'm trying to create a program design where a UI program created with the Turtle Graphics Library in Python communicates directly with an EV3 Python Program that exists on the LEGO EV3 brick.

What I have so far:

  1. FrontEnd.py - An object oriented program that senses unique button clicks based on mouse pointer x and y and the shapes used to draw out button like objects
  2. RobotInstruction.py - An EV3 program that turns motors based on function calls

What happened when I tried to get it to work:

It seemed like there's a clash in dependencies. Like turtle and ev3 are not compatible. What it seemed to me was that the FrontEnd.py file was trying to load into the RobotInstruction.py file and end up on the brick, which is not what I wanted.

Important Note:

Independently, these scripts work fine. For example, RobotInstruction.py can receive keyboard input to act on the motors. I just want to get that "command" firing from the graphical program instead

My first attempt at a Janky Super Inefficient Workaround:

-- THE EXISTING CODE EXCERPTS ARE ATTACHED AT THE END --

Use FrontEnd.py to write a string command to a file and have RobotInstruction.py constantly reading that file for a command and then based on the command, call the relevant function to turn motors.

What Worked:

Can successfully write to file with a command from FrontEnd.py

Can successfully read the command from the same file

BUT

It doesn't happen in real time. I'm not super familiar with file reading / writing with Python so much...so very possible that I may be doing something awkward...

My Question:

Is what I'm trying to do, possible? Can you click a turtle graphics created button to send a command to an ev3 robot? If so, how would I go about forming the CONNECTION between the 2 separate scripts?

CODE "EXCERPTS"

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)



from How to send commands from Python turtle graphics to an EV3 Lego brick?

No comments:

Post a Comment