Skip to content

motors API: speed_pct vs speed_dps vs speed_rps #370

Closed

Description

@dwalton76

While working on #353 #360 one thing we did not address was the API for specifying the motor speed in something other than speed_pct. The other two we want are degrees per second and rotations per second.

Some ideas that were floated:

Option 1
How about we make the default units % just like EV3-G, plus introduce scaling factor constants for those who want scientific units.

# run the motor at 50% of max speed
m.run(50)
# run the motor at 400 degrees/second
m.run(400 * m.DPS)
# run the motor at 60 RPM
m.run(60 * m.RPM)

Option 2
why not m.run(speed_pct=50), m.run(dps=400), and m.run(rpm=60)? If we define run as

def run(self, speed_pct=None, dps=None, rpm=None)

We have other parameters that need to be passed in though (speed) so basically everything becomes a positional arg. So the user would do one of

m.on_for_degrees(speed_pct=40, degrees=720)
m.on_for_degrees(speed_dps=90, degrees=720)
m.on_for_degrees(speed_rps=0.25, degrees=720)

Activity

  1. ddemidov commented on Sep 22, 2017

    @ddemidov
    Member

    Option 3
    (copied from #360 (comment))
    Introduce proxy properties for various units. For example, we could have

    @property
    def degrees_sp(self):
        return self.position_sp * self.POS2DEG_FACTOR
    @degrees_sp.setter
    def degrees_sp(self, value):
        self.position_sp = value * self.DEG2POS_FACTOR

    and then same thing for rotations_sp, power_sp (percentage of max_speed_sp), etc.

  2. dwalton76 commented on Sep 22, 2017

    @dwalton76
    CollaboratorAuthor

    @ddemidov can you give an example m.on_for_degrees(...) call with option 3?

  3. ddemidov commented on Sep 22, 2017

    @ddemidov
    Member

    I would just call our existing m.run_to_rel_position(degrees_sp=360).

  4. dwalton76 commented on Sep 22, 2017

    @dwalton76
    CollaboratorAuthor

    How would it be used for m.on_for_degrees(...) though?

  5. ddemidov commented on Sep 22, 2017

    @ddemidov
    Member

    I think m.run_to_rel_position(degrees_sp=360) is exactly m.on_for_degrees(360), no?

  6. dwalton76 commented on Sep 22, 2017

    @dwalton76
    CollaboratorAuthor

    Say I'm a user and I want to rotate a motor for 720 degrees at 360 degrees per second, I'm asking how does that happen with option 3 via a call to m.on_for_degrees(). Today if you call m.on_for_degrees(360) it is going to assert because 360 is speed_pct and it is greater than 100.

  7. ddemidov commented on Sep 22, 2017

    @ddemidov
    Member

    m.run_to_rel_position(degrees_sp=720, dps_sp=360) should do it, where dps_sp is another proxy property, but for speed_sp. We can rename (or create an alias) the method to on_for_degrees (I would prefer even simpler and generic on_for though: it would take kwargs dict and it would be useable with degrees_sp or seconds_sp or what not).

  8. dwalton76 commented on Sep 22, 2017

    @dwalton76
    CollaboratorAuthor

    Ignore run_to_rel_position, that is what is happening behind the scenes :) So you would have m.on_for_degrees(dps_sp=360, degrees=720)? If so that looks the same as option 2

  9. ddemidov commented on Sep 22, 2017

    @ddemidov
    Member

    Yes, it looks very much like option 2, but the implementation should is easier. Instead of

    def on_for_degrees(self, degrees=None, dps=None, speed=None, power=None):
      if dps is not None:
        self.speed_sp = dps * dps2speed
      elif power is not None:
        self.speed_sp = dps * power2speed
      self.position_sp = degrees * deg2pos
      self.run_to_rel_position()    

    we can do

    def on_for_degrees(self, degrees=None, **kwargs):
      for key,val in kwargs:
        setattr(self, key, val)
      self.position_sp = degrees * deg2pos
      self.run_to_rel_position()    

    or even

    def on_for(self, **kwargs):
      for key,val in kwargs:
        setattr(self, key, val)
      if degrees_sp in kwargs: # this is tricky, need to check for other positional units...
        self.run_to_rel_position() 
      elif time_sp in kwargs:
        self.run_timed()
      ...
  10. ddemidov commented on Sep 22, 2017

    @ddemidov
    Member

    We can come up with a name more generic than on_for_degrees that would mean 'turn for some rotations units'. rotate_for? Then we can do rotate_for(degrees_sp=720, dps_sp=360), or rotate_for(turns_sp=2, rpm_sp=1), and it would mean the same thing.

    EDIT1: And the implementation for it is as simple as

    def rotate_for(self, **kwargs):
      for key,val in kwargs:
        setattr(self, key, val)
      self.run_to_rel_position()    

    EDIT2: And that is exactly the implementation of our current run_to_rel_position.

  11. dwalton76 commented on Sep 22, 2017

    @dwalton76
    CollaboratorAuthor

    To me using kwargs makes the API much less readable/obvious if you are a new user looking at this for the first time...and that is really who we are trying to target here. Today you can look at the EV3-G motor block and look at def on_for_degrees(self, speed_pct, degrees, brake=True, block=True): and connect the dots pretty quickly on how they are equivalent. If we use kwargs we lose that.

  12. ddemidov commented on Sep 22, 2017

    @ddemidov
    Member

    We can put positional arguments into the documentation, but implement it with keyword args.

  13. dwalton76 commented on Sep 22, 2017

    @dwalton76
    CollaboratorAuthor

    That doesn't solve the problem of having readable/obvious code though :)

  14. ddemidov commented on Sep 22, 2017

    @ddemidov
    Member

    Well, for me the code with kwargs is more readable because it is more concise, and the resulting API is better because it gives you more freedom: you can use whatever meaningful combination of attributes you need, and you are not constrained by the set of positional arguments a developer thought would be enough.

  15. dwalton76 commented on Sep 22, 2017

    @dwalton76
    CollaboratorAuthor

    it gives you more freedom: you can use whatever meaningful combination of attributes you need, and you are not constrained by the set of positional arguments a developer thought would be enough

    Agreed but we already have that with the current run_XYZ API, that isn't the problem we are trying to solve. To me we don't want crazy amounts of freedom, we want a nice clean/obvious 1:1 mapping with the EV3-G motor blocks.

    Just my 2c though...lets see what the others think since you and I are just going back and forth on this.

  16. ddemidov commented on Sep 22, 2017

    @ddemidov
    Member

    we already have that with the current run_XYZ API

    That is a good point. We can add proxy attributes and keep the current interface. And I think that would also let us implement the new 'simpler' API easier.

  17. dlech commented on Sep 22, 2017

    @dlech
    Member

    I've been working on translating an EV3-G program for the last couple days and I am finding that having the API exactly the same as EV3-G is really the most useful thing to do and perhaps more importantly, it is just really fun.

    What I mean by exactly is having positional arguments in the same order as the EV3-G software that take the exact same value. I thought I would like to have fancy extra features or tweak things (like changing names) to make it "better", but I am finding that in practice, those kinds of changes are getting in the way (with the exception of correcting things that are just plain wrong in EV3-G, like using the name speed instead of power). So, I would say put this issue on the back burner and move on to something else. Once we have the basics, we can start using the library and find what we are really missing and add more if needed. By then, we should have a better idea of what feels right instead of just trying to guess like we are now.

    Also...

    import this
  18. WasabiFan commented on Sep 22, 2017

    @WasabiFan
    Member

    My two cents: in addition to what others have said, I think that we should fully commit to this new API surface if we're going to support it (and I'm pretty darn sure we are). My vote would be to remove the old methods given that the new ones are 1:1 replacements for all the normal use cases (or, if they aren't, add optional arguments to make them so).

  19. ddemidov commented on Sep 23, 2017

    @ddemidov
    Member

    My vote would be to remove the old methods

    Please lets not do this until we have the new API fully implemented. My feeling is the purpose of the new API is to make translation from EV3-G to our library simple, but not necessary 1:1. When a nicer API is possible, why not use a nicer API?

    I am not saying that what I am proposing is nicer, just lets not do this only to have 1:1 mapping with EV3-G. After all, people move to ev3dev because they are not satisfied with EV3-G.

  20. rhempel commented on Sep 23, 2017

    @rhempel
    Member

    Strongly suggest you implement an EV3G compatible API as a loadable helper module - leave the basic API as is because it's compatible across many scripting languages

  21. dwalton76 commented on Sep 23, 2017

    @dwalton76
    CollaboratorAuthor

    I don't think it hurts for us to keep the old API around...it is a little bit more code to maintain but it gives us backward compatibility

  22. dwalton76 commented on Sep 23, 2017

    @dwalton76
    CollaboratorAuthor

    Will close this one for now but we may re-open it in a few months when we've had time to finish and work with the EV3-G 1:1 API.

  23. WasabiFan commented on Sep 23, 2017

    @WasabiFan
    Member

    I don't think it hurts for us to keep the old API around...

    I do. A user has no way of knowing the difference between run_forever and on, or run_timed and on_for_seconds. With both APIs available, code will be fractured between the two and only be confusing. The two types of methods have entirely different calling patterns and behaviors which isn't clear to any user.

    If we are making choices, I'd prefer they are intentional and deliberate so we make sure we don't add to the confusion.

  24. ddemidov commented on Sep 23, 2017

    @ddemidov
    Member

    To think of it, how does the user know to look for on_for_seconds as opposed to run_timed? Aren't both of those just arbitrary names we came up for 'green motor block'? If on_for_seconds is somehow canonical, @dlech, are you willing to rename the corresponding action in kernel drivers?

    Creating a EV3G compatibility layer in a separate module sounds like a nice idea. Users can import * from ev3dev.ev3g and have just the ev3-g API available.

  25. rhempel commented on Sep 23, 2017

    @rhempel
    Member

    Thanks Denis - that's what I was trying to say. We have users that will be a bit annoyed if we change the API again

  26. dlech commented on Sep 23, 2017

    @dlech
    Member

    @dlech, are you willing to rename the corresponding action in kernel drivers?

    No. At this point, the only kernel changes I am planning on making are things that make it into the mainline kernel. I would really like to avoid these small breaking changes if at all possible because it breaks all existing ev3dev software.

    Creating a EV3G compatibility layer in a separate module sounds like a nice idea. Users can import * from ev3dev.ev3g and have just the ev3-g API available.

    This is exactly what I meant in #348. The current ev3dev-lang-python is the "low level" api because it looks and feels like the hardware drivers. You don't want to change it because people are using it. The new "high level" api should look and feel like the EV3-G software. And most importantly, there should be a clear separation between these two. Putting them in separate modules is very clear separation.

    In fact, this suggestion of an ev3dev.ev3g module fits well with what I have been thinking in regards to micropython. I'll post those thoughts in ev3dev/ev3dev#937 since it is off topic here.

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