BradenM / micropy-cli

Micropython Project Management Tool with VSCode support, Linting, Intellisense, Dependency Management, and more!
https://micropy-cli.readthedocs.io
MIT License
311 stars 25 forks source link

Add support for Lego EV3 MicroPython #43

Closed vbolshakov closed 4 years ago

vbolshakov commented 4 years ago

Add support for Lego EV3 MicroPython. https://education.lego.com/en-us/support/mindstorms-ev3/python-for-ev3

BradenM commented 4 years ago

Hi, thanks for the idea.

I will look further into it, but as I do not own an EV3 brick myself, I am not sure if I will be able to generate the stubs for it. Have you tried executing micropy stubs create <EV3_PORT>?

If the stubs generate successfully, you could either provide them to me or create a PR on micropy-stubs to add them for everyone.

Let me know if you're able to make any progress with this. Thanks.

vbolshakov commented 4 years ago

There is no micropython repl on uart, but there is ssh access to ev3. Just need to understand what scripr i need to run on ev3 to generate stubs.

vbolshakov commented 4 years ago

Can you add option to generate stubber as file?

BradenM commented 4 years ago

Interesting.

So micropy-cli uses micropython-stubber for its stub generation. Essentially, you need to do the following:

  1. Clone micropython-stubber and open createstubs.py in your editor.
  2. Update the self.modules list with the custom modules that are on the EV3
  3. Upload and execute createstubs.py
  4. Download the generated stubs directory (if it succeeds on the EV3)

If you run into any memory related errors, you can use my fork of the repo, make your module edits, then execute python process.py minify. This will created a smaller minified.py that uses less memory.

For finding the EV3 modules you can reference their documentation and try executing help('modules') in repl (on most forks, this will print out a list of modules).

If you run into any trouble, I am happy to help.

vbolshakov commented 4 years ago

There custom Lego build of micropython - pybricks-micropython (MicroPython v1.9.4 on 2019-03-26; linux version) with differences in 'os' module, so i change createstubs in several places with os and firmware version parsing. It is generating stubs for everything but custom modules. And i don't know why... If you interested, i can provide remote access to ev3.

BradenM commented 4 years ago

Sure, I'd love to take a look at it!

vbolshakov commented 4 years ago

What is the best way to give you access credential?

Josverl commented 4 years ago

Hi,

I think that what is happening is that there is no way for a .py script to find out which modules there are on the firmware.

the only way is to issue the command : help('modules') , however the output of that cannot be redirected , except on specific hardware ( pyboards) .

so the approach that I have uses in micro-stubber is :

so : you need to add the LEGO specific ones . to do that in createstubs.py add a (few) lines with the LEGO custom modules :

    #.... at the very end 
    stubber.add_modules(['pybricks']) # stubs for LEGO Pybricks
    stubber.create_all_stubs()

run createsubs.py , ie

brickrun createstubs.py 

retrieve (upload..) the stubs from the brick to your PC image

( and please share or send a PR to https://github.com/josverl/micropython-stubber including any license ( MIT?) that is relevant for LEGO )

Josverl commented 4 years ago

also if you have a list of LEGO specific MicroPython modules , i can just add them to the list of modules to try for micros-stubber.

vbolshakov commented 4 years ago

help('modules') show modules without any problems: robot@ev3dev:~$ pybricks-micropython MicroPython v1.9.4 on 2019-03-26; linux version Use Ctrl-D to exit, Ctrl-E for paste mode

help('modules') main parameters_c pybricks/uev3dev/messaging umachine _thread pybricks/init pybricks/uev3dev/sound uos array pybricks/display pybricks/uev3dev/util urandom boot pybricks/ev3brick sys ure btree pybricks/ev3devices termios uselect builtins pybricks/ev3devio tools usocket cmath pybricks/parameters ubinascii ussl ev3brick_c pybricks/robotics ucollections ustruct ev3devices_c pybricks/speaker ucryptolib utime ffi pybricks/tools uctypes utimeq framebuf pybricks/uev3dev/init uerrno uzlib gc pybricks/uev3dev/_alsa uhashlib websocket math pybricks/uev3dev/_wand uheapq micropython pybricks/uev3dev/display uio mmap pybricks/uev3dev/i2c ujson Plus any modules on the filesystem

But when i add stubber.add_modules(['pybricks/ev3brick'])

I got only this as result """ Module: 'pybricks.ev3brick' on 1 """

MCU: posix

Stubber: 1.2.0

display = None ev3brick = None parameters = None speaker = None tools = None uev3dev = None

Josverl commented 4 years ago

Okay, that is helpful

I suspect that has something to do with nested modules ( module/submodule and even module/sub/leaf ) ( that is somthing I have noticed ( with M5Stack) before , but have not untangled earlier

have you tried stubber.add_modules(['pybricks']) behaviour will depend on the implementation. It looks like LEGO has used frozen py-modules so we only can guess

Josverl commented 4 years ago

also likely relevant : a manual stubbing exercise : https://github.com/drewwhis/pybricks-stubs

vbolshakov commented 4 years ago

Already use it. But automation with micropy-cli will be better.

vbolshakov commented 4 years ago

Checked both: stubber.add_modules(['pybricks']) # stubs for LEGO Pybricks stubber.add_modules(['pybricks/ev3brick']) # stubs for LEGO Pybricks

BradenM commented 4 years ago

@Josverl, thanks for your help. I don't really know much about the EV3 products.

@vbolshakov, agreed. ~The automated stub creation is relatively primitive and could definitely be expanded on.~ (Edit: Misunderstood what you meant. For some reason I thought you were referring to micropy-cli 's ability to automate the uploading/retrieval process of createstubs.py)

When I get to a desktop later today I will look at this in more detail.

BradenM commented 4 years ago

@vbolshakov, I suppose I could add pybricks-stubs to micropy-stubs until Josverl/micropython-stubber#7 is solved. Sound good for now?

vbolshakov commented 4 years ago

Yes.

BradenM commented 4 years ago

Okay, went ahead and added what I could. Since it's only the extra modules from pybricks I would suggest adding another package along side it.

This seemed to work pretty well for me while I was testing it:

micropy stubs add ev3-pybricks-0.0.1
micropy stubs add esp32-micropython-1.9.4 # Someone mentioned PyBricks was based on 1.9.4?
{
    "name": "NewProject",
    "stubs": {
        "esp32-micropython-1.9.4": "1.2.0",
        "ev3-pybricks-0.0.1": "1.2.0"
    },
    "config": {
        "vscode": true,
        "pylint": true
    },
    "packages": {},
    "dev-packages": {}
}

I will leave this issue open until we can manage a full package from the EV3.

Josverl commented 4 years ago

@vbolshakov Please take a look at : https://github.com/Josverl/micropython-stubber/issues/7#issuecomment-526954206

BradenM commented 4 years ago

Closed via BradenM/micropy-stubs#3 and Josverl/micropython-stubber#7

Also, as I edited here, did not mean to bash on @Josverl 's work (my own, rather) haha. micropython-stubber is the reason we are even having this conversation.