Skip to content

motors API: passing something other than speed percentage #406

Closed

Description

@dwalton76

Today in on_for_rotations(), etc the user passes the speed_pct because this is how the EV3-G block works. I started updating the MINDCUBER demo to use the new API but I end up doing the following all over the place (two lines, one to cald the speed_pct and a second to call the API)

        speed_pct = self.turntable.dps_to_speed(MindCuber.rotate_speed)
        self.turntable.on_to_position(speed_pct, temp_pos)

which works just fine but doesn't look super clean (side note, on_to_position() is new). What if the on_to_XYZ() methods accepted either speed_pct or (speed_type, speed_value) tuple such as the following

        self.turntable.on_to_position(('dps', MindCuber.rotate_speed), temp_pos)

Valid types there could be 'dps', 'dpm', 'rps', or 'rpm'.

Activity

  1. self-assigned this
    on Oct 10, 2017
  2. WasabiFan commented on Oct 10, 2017

    @WasabiFan
    Member

    Maybe this is just my non-Python background, but doesn't this style feel a bit unnatural? To an experienced programmer, it seems like there would be a more direct and obvious way of specifying this value; to an inexperienced one, it would be quite easy to misinterpret the grouping and be left completely confused. Why not add other named parameters for speed_dps, speed_rpm, etc.?

  3. dwalton76 commented on Oct 10, 2017

    @dwalton76
    CollaboratorAuthor

    we could do

       def on_for_rotations(self, speed_pct, rotations, brake=True, block=True, speed_rps=None, speed_rpm=None, speed_dps=None, speed_dpm=None)
    

    where the user would then call via

        motor.on_for_rotations(None, 3, speed_rps=400)
    

    but I was worried that might be confusing when comparing our API vs the EV3-G block since the speed parameter is out of order (in the block it is specified first).

  4. WasabiFan commented on Oct 10, 2017

    @WasabiFan
    Member

    That seems cleaner to me, but I'd be interested to hear what others think.

  5. dwalton76 commented on Oct 10, 2017

    @dwalton76
    CollaboratorAuthor

    Another option would be something like

    class SpeedRPS(int):
        pass
    
    class SpeedRPM(int):
        pass
    
    class SpeedDPS(int):
        pass
    
    class SpeedDPM(int):
        pass
    

    and then the user would call via

        motor.on_for_rotations(SpeedRPS(400), 3)
    

    That does look a little less hackish doesn't it

  6. dwalton76 commented on Oct 10, 2017

    @dwalton76
    CollaboratorAuthor

    For the SpeedRPS approach on_for_rotations() would need to call a function that uses isinstance to correctly convert the SpeedRPS value to speed percentage.

  7. WasabiFan commented on Oct 10, 2017

    @WasabiFan
    Member

    That seems like a pretty nice option as well.

    For the SpeedRPS approach on_for_rotations() would need to call a function that uses isinstance to correctly convert the SpeedRPS value to speed percentage.

    You could also put a get_speed_sp(Motor) method on each of the "unit" classes and call that from our side. That would make it super easy to extend the behavior with custom units (e.g., rotations through gearing or feet of travel) and remove the need for a chain of ifs.

  8. dwalton76 commented on Oct 10, 2017

    @dwalton76
    CollaboratorAuthor

    I like that...let me try this out and will update the PRs

  9. WasabiFan commented on Oct 10, 2017

    @WasabiFan
    Member

    The one downside I see to this approach is that if we intend to recommend the from ev3dev.motor import Motor, OUTPUT_A, OUTPUT_B, ... approach you'll have to manually import each of the unit types, which is a pain.

  10. dwalton76 commented on Oct 10, 2017

    @dwalton76
    CollaboratorAuthor

    yeah but I bet most programs only use one speed unit so hopefully won't be bad....new code posted

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

Metadata

Metadata

Assignees

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