Skip to content

Separation of driver interface from high level library #348

Closed

Description

@dlech

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.

selection_014

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

  1. dwalton76 commented on Sep 5, 2017

    @dwalton76
    Collaborator

    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

    • helper.py
    • GyroBalancer.py
    • fonts?
    • webserver.py
    • demo...or put these in a separate repo? It seems like having them in the api repo would be a good place for them but I'm not sure if that muddies the waters for including the demo folder as part of the base ev3dev image.

    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.

  2. WasabiFan commented on Sep 5, 2017

    @WasabiFan
    Member

    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_mode and set_mode? Or just get_distance and then it sets the mode automatically?

  3. dwalton76 commented on Sep 7, 2017

    @dwalton76
    Collaborator

    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

  4. dlech commented on Sep 18, 2017

    @dlech
    MemberAuthor

    Here are the medium motor blocks. They are exactly the same as the Large motor but operate on the Medium motor.

    image

    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.

    image

  5. dlech commented on Sep 18, 2017

    @dlech
    MemberAuthor

    Move steering has all of the same methods as the single motor blocks with additional parameters an extra port and for steering.

    image

  6. dlech commented on Sep 18, 2017

    @dlech
    MemberAuthor

    The move tank blocks are similar to move steering, but have independent motor power parameters instead of a steering parameter.

    image

  7. dlech commented on Sep 18, 2017

    @dlech
    MemberAuthor

    The display block:

    image

    RE: micropython - The micropython framebuffer module is missing drawing circles, images from file and different text sizes, but otherwise has the same functions.

  8. dlech commented on Sep 18, 2017

    @dlech
    MemberAuthor

    The sound block. The play_type parameter can be "Wait for completion", "play once" or "repeat".

    image

  9. dlech commented on Sep 18, 2017

    @dlech
    MemberAuthor

    LED block:

    image

  10. dlech commented on Sep 18, 2017

    @dlech
    MemberAuthor

    Buttons:

    image

  11. dlech commented on Sep 18, 2017

    @dlech
    MemberAuthor

    Color sensor. Note: calibration is implemented in software, it is not a feature of the hardware driver.

    image

  12. dlech commented on Sep 18, 2017

    @dlech
    MemberAuthor

    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, while orfor statements. 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)

  13. dlech commented on Sep 18, 2017

    @dlech
    MemberAuthor

    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.

    image

    image

  14. ensonic commented on Sep 19, 2017

    @ensonic
    Contributor

    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.

  15. dwalton76 commented on Sep 19, 2017

    @dwalton76
    Collaborator

    I'll file bugs for each for each of these so we can break this down into some smaller chunks

  16. dwalton76 commented on Sep 20, 2017

    @dwalton76
    Collaborator

    Closing this one since we have issues filed for individual components

  17. dlech commented on Sep 20, 2017

    @dlech
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions