ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
422 stars 146 forks source link

Run other modules with ev3dev #762

Closed GrecuAlexandru closed 3 years ago

GrecuAlexandru commented 3 years ago

I am trying to install other modules as PyAutoGUI on the ev3 machine and it doesnt work. I made a virtual environment following the steps at the bottom of this page https://github.com/ev3dev/vscode-hello-python. It worked well. I installed the module using "pip install pyautogui". The module successfully installed. Unfortunately, when i run the program using f5 (after i configured my new venv folder as my python interpreter) the script doesnt recognize the module and outputs the following error:

Traceback (most recent call last):
  File "/home/robot/EV3/start.py", line 9, in <module>
    import PyAutoGUI
ImportError: No module named 'PyAutoGUI'
>>> & c:/Users/Alex/Desktop/Programare/EV3/.venv/Scripts/Activate.ps1
  File "<stdin>", line 1
    & c:/Users/Alex/Desktop/Programare/EV3/.venv/Scripts/Activate.ps1
    ^
SyntaxError: invalid syntax

The code i am using:

#!/usr/bin/env python3

from time import sleep

from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, SpeedPercent, MoveTank
from ev3dev2.sensor import INPUT_1
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.led import Leds
import PyAutoGUI

while True:
    print(PyAutoGUI.position())
dlech commented 3 years ago

You installed PyAutoGUI on your computer, not on the EV3, so it gives this error.

What is the end goal you are trying to accomplish? It doesn't seem like installing PyAutoGUI on the EV3 would be useful since it is not a desktop computer.

dlech commented 3 years ago

To answer the title question though, we highly recommend installing Python packages on the EV3 using apt rather than pip3. For example: sudo apt update && sudo apt install python3-xyz. Many popular Python packages are available this way.

GrecuAlexandru commented 3 years ago

My end goal is to move my cursor around on desktop, the pyautogui module will track the movement and convert it into x and y coordinates, if the x is grater than 500, rotate the motor clockwise, if the x is lower, rotate the motor counterclockwise. I am expecting this process to work in real time or at least somewhat close to it. Can the ev3dev do this?

GrecuAlexandru commented 3 years ago

Also, what do you recommend to do in this case?

dlech commented 3 years ago

You should be able to install PyAutoGUI on your computer and write a Python program that runs on the computer that sends the information to the EV3. Then have a separate program on the EV3 that receives the information and controls the motors.

GrecuAlexandru commented 3 years ago

In short, how do you suggest sending and receiving the information. If you have a link to where i can read a little about this would be perfect.

dlech commented 3 years ago

Here is one way: https://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/stable/rpyc.html

GrecuAlexandru commented 3 years ago

Yesterday i took a look at that and run into a problem that i couldnt solve even after 2 hours of searching for a solution. I did the exact same steps as that link suggests but i run into the following error:

Traceback (most recent call last):
  File "c:\Users\Alex\Desktop\Programare\EV3\start.py", line 6, in <module>
    conn = rpyc.classic.connect('192.168.137.218')
  File "C:\Python38\lib\site-packages\rpyc\utils\classic.py", line 75, in connect
    return factory.connect(host, port, SlaveService, ipv6=ipv6, keepalive=keepalive)
  File "C:\Python38\lib\site-packages\rpyc\utils\factory.py", line 99, in connect
    return connect_stream(s, service, config)
  File "C:\Python38\lib\site-packages\rpyc\utils\factory.py", line 56, in connect_stream
    return connect_channel(Channel(stream), service=service, config=config)
  File "C:\Python38\lib\site-packages\rpyc\utils\factory.py", line 44, in connect_channel
    return service._connect(channel, config)
  File "C:\Python38\lib\site-packages\rpyc\core\service.py", line 105, in _connect
    self.on_connect(conn)
  File "C:\Python38\lib\site-packages\rpyc\core\service.py", line 213, in on_connect
    self._install(conn, conn.root)
  File "C:\Python38\lib\site-packages\rpyc\core\protocol.py", line 502, in root
    self._remote_root = self.sync_request(consts.HANDLE_GETROOT)
  File "C:\Python38\lib\site-packages\rpyc\core\protocol.py", line 469, in sync_request
    return self.async_request(handler, *args, timeout=timeout).value
  File "C:\Python38\lib\site-packages\rpyc\core\async_.py", line 100, in value
    self.wait()
  File "C:\Python38\lib\site-packages\rpyc\core\async_.py", line 47, in wait
    self._conn.serve(self._ttl)
  File "C:\Python38\lib\site-packages\rpyc\core\protocol.py", line 392, in serve
    self._dispatch(data)
  File "C:\Python38\lib\site-packages\rpyc\core\protocol.py", line 360, in _dispatch
    obj = self._unbox(args)
  File "C:\Python38\lib\site-packages\rpyc\core\protocol.py", line 294, in _unbox
    proxy = self._netref_factory(id_pack)
  File "C:\Python38\lib\site-packages\rpyc\core\protocol.py", line 308, in _netref_factory
    cls_methods = self.sync_request(consts.HANDLE_INSPECT, id_pack)
  File "C:\Python38\lib\site-packages\rpyc\core\protocol.py", line 469, in sync_request
    return self.async_request(handler, *args, timeout=timeout).value
  File "C:\Python38\lib\site-packages\rpyc\core\async_.py", line 102, in value
    raise self._obj
KeyError: ('3047605264', 'SlaveService', 'rpyc.core.service')

I found out something that the versions of rpyc must be the same both on the client and the server, but i dont know how to check the version of the ev3. If you could help me it would be very much appreciated. In any case thank you very much for your help so far.

dlech commented 3 years ago
$ dpkg -s python3-rpyc
Package: python3-rpyc
Status: install ok installed
Priority: optional
Section: python
Installed-Size: 269
Maintainer: Denis Demidov <dennis.demidov@gmail.com>
Architecture: all
Source: rpyc
Version: 3.3-1
Depends: python3, python3-plumbum, python3:any (>= 3.3.2-2~)
Description: RPyC (Remote Python Call).
 A transparent and symmetric RPC library for python.
Homepage: http://rpyc.readthedocs.io

Looks like v3.3.

GrecuAlexandru commented 3 years ago

The same version (3.3) is installed on my ev3. I uninstalled the rpyc module from my desktop computer and installed version 3.3 with pip install rpyc==3.3. Still, the same error appears.

GrecuAlexandru commented 3 years ago

Nevermind, I made it work using the following link: https://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/ev3dev-jessie/rpyc.html Thanks again for your help!