Repository navigation
Description
Looking forward to ev3dev-lang-python 2.0...
I don't know if this has come up in GitHub before, but in some of my out-of-band discussions with @rhempel in the early days of ev3dev-lang-python, the idea was that it would be a low-level wrapper around the drivers and that a (separate) higher level library/API would be built on top of it.
One of the most frequent support requests I get for ev3dev is how do I make a motor move in python. Explaining this is not easy. The reason being that the library exposes all of the driver implementation details and requires the user to understand them.
I think this library would be much more fun and easy to use if we could make all of the low-level driver wrappers internal or separate somehow where most people don't have to worry about it or ever see it. This would also fix the problem of breaking driver changes resulting in breaking all user programs. If we hide the driver details from the user, then we only have to fix ev3dev-lang-python and all user programs keep working.
I've had the idea in the back of my mind for a long time to make a python API based on the EV3-G blocks to make is super-easy to transition from official EV3 to text based programming. I'm remembering back 8 years ago when I got my first MINDSTORMS (NXT) and I was not an expert programmer like I am now. I found it really helpful that NXC basically had the same API and features as the NXT-G software. It made learning the new language/library easier because for me because I knew what to look for. I knew how motors and sensors worked in NXT-G, so I knew there must be functions in NXC that did more-or-less the same thing and would have more-or-less the same name.
Here is an example of something I did a while back that shows how you could make a 1:1 correspondence between EV3-G and python.
In practice, I don't think I would make it 1:1 exactly, but the point is to make something a bit more familiar to EV3 users and hide all of the driver details so that we can continue to improve the drivers without breaking everyone's programs.
I see some pull requests adding high level APIs, which is nice, but I think it would be tremendously helpful to have a clear separation between these APIs and the driver wrappers.

Activity
dwalton76 commentedon Sep 5, 2017
So maybe have ev3dev-lang-python be primarily the code generated via the templates (that would allow this repo to be comparable to other ev3dev-lang-XYZ repos) and then create a ev3dev-lang-python-api that would contain
I like the idea of having an API for many of the EV3-G blocks. That would make it much easier for someone who is following an EV3-G program in a book to port it to python.
WasabiFan commentedon Sep 5, 2017
I've generally been attempting to move in the direction of adding higher-level APIs, but we certainly haven't hit complete interface parity with EV3-G.
Personally, I think we should avoid having two entirely different libraries (it causes confusion when some people use one and other people use a different one) but I could certainly see an architecture where we pull out the low-level interfaces into their own module and don't advertise them for the purposes of external use. If someone is particularly interested in execution speed they could use the inner module, but we wouldn't recommend it for most users. Does that seem reasonable?
The question I have is: exactly what level of API are we interested in supporting? We should begin by defining, at least in a general sense, the API surface that we want to expose, and then draw a line between the lower-level interfaces and the new ones. For example, for sensors, would we expose
get_modeandset_mode? Or justget_distanceand then it sets the mode automatically?dwalton76 commentedon Sep 7, 2017
Starting on page 21 here they cover a few of the motor blocks
http://stemrobotics.cs.pdx.edu/sites/default/files/Lego%20Robot%20-%202013EV3Programming.pdf
To be sure there are full docs out there somewhere on all of the different blocks...hopefully
dlech commentedon Sep 18, 2017
Here are the medium motor blocks. They are exactly the same as the Large motor but operate on the Medium motor.
There are also "sensor" blocks for motors that would make sense as being part of the same motor object in python. Note: this "reset" method only resets the position to zero, it is not the reset command of the ev3dev drivers.
dlech commentedon Sep 18, 2017
Move steering has all of the same methods as the single motor blocks with additional parameters an extra port and for steering.
dlech commentedon Sep 18, 2017
The move tank blocks are similar to move steering, but have independent motor power parameters instead of a steering parameter.
dlech commentedon Sep 18, 2017
The display block:
RE: micropython - The micropython framebuffer module is missing drawing circles, images from file and different text sizes, but otherwise has the same functions.
dlech commentedon Sep 18, 2017
The sound block. The
play_typeparameter can be "Wait for completion", "play once" or "repeat".dlech commentedon Sep 18, 2017
LED block:
dlech commentedon Sep 18, 2017
Buttons:
dlech commentedon Sep 18, 2017
Color sensor. Note: calibration is implemented in software, it is not a feature of the hardware driver.
dlech commentedon Sep 18, 2017
The blocks above should give a pretty good idea of the core features of the EV3-G software, but is by no means exhaustive. All of the green action blocks are shown, but I've only included one sensor in the yellow sensor blocks. Also, all sensors can be used in the orange flow control blocks, but those can be implemented using python
if,whileorforstatements. The red data operation blocks can be implemented using built-in python operators for the most part (variable assignment, comparisons, math operations, lists/tuples) and some standard modules (random)dlech commentedon Sep 18, 2017
And for comparison, here is another simple "api" from OpenRoberta. Again, just doing motors and sensors because everything else can be done with basic python stuff.
ensonic commentedon Sep 19, 2017
Here is the implementation of the OpenRoberta blocks:
https://github.com/OpenRoberta/robertalab-ev3dev/blob/develop/roberta/ev3.py
It shows that we quite far away for having a 1-2 lines of py for each block. Suggestions to reduce code size (or even PRs) welcome. The new MotorSet will certainly help.
dwalton76 commentedon Sep 19, 2017
I'll file bugs for each for each of these so we can break this down into some smaller chunks
dwalton76 commentedon Sep 19, 2017
See
dwalton76 commentedon Sep 20, 2017
Also see
dwalton76 commentedon Sep 20, 2017
Closing this one since we have issues filed for individual components
dlech commentedon Sep 20, 2017
FYI, in all of the screenshots, the python examples are overly literal translations so that you can see exactly what in EV3-G corresponds to what in Python. The actually implementation should be tweaked to be more pythonic and concise.