Skip to content

brickpi3 and medium motors #543

Closed
Closed

Description

@dwalton76

The firmware in BrickPi3 (and earlier BrickPi models) doesn't know about different kinds of motors, so we just treat everything like an NXT motor.
-- dlech in ev3dev/ev3dev#1059

The work-around is to use the LargeMotor class for the medium motor. One potential problem is if the user's program relies on max_speed since that value is different for medium vs large motors.

If the user does try to use the MediumMotor class we will fail to set the driver (see the intro in ev3dev/ev3dev#1059). One option for presenting the user with a more informed error message is to raise an exception if the MediumMotor class is used:

        if platform in ('brickpi', 'brickpi3'):
            raise Exception("%s is unaware of different motor types, use LargeMotor instead" % platform)

Activity

  1. self-assigned this
    on Oct 31, 2018
  2. dwalton76 commented on Oct 31, 2018

    @dwalton76
    CollaboratorAuthor

    Running

    Image file:         ev3dev-stretch-rpi2-generic-2017-12-13
    Kernel version:     4.14.71-ev3dev-2.3.0-rpi2
    Brickman:           0.10.0
    BogoMIPS:           38.40
    Board:              board0
    BOARD_INFO_HW_REV=A22082
    BOARD_INFO_MODEL=Raspberry Pi 3 Model B Rev 1.2
    BOARD_INFO_SERIAL_NUM=000000002f94f0a9
    BOARD_INFO_TYPE=main
    Board:              board1
    BOARD_INFO_FW_VER=1.4.3
    BOARD_INFO_HW_REV=3.2.1
    BOARD_INFO_MODEL=Dexter Industries BrickPi3
    BOARD_INFO_SERIAL_NUM=E1DA1CC95150324336202020FF19312F
    BOARD_INFO_TYPE=aux
    

    Testing with:

    #!/usr/bin/env python3
    
    from ev3dev2.motor import OUTPUT_A, LargeMotor, MediumMotor, SpeedRPM
    
    mmotor = MediumMotor(OUTPUT_A)
    #mmotor = LargeMotor(OUTPUT_A)
    mmotor.on_for_seconds(SpeedRPM(40), 3)
    

    If you try to use the MediumMotor class the brickpi thinks that it is not connected

    robot@pi3[ev3dev-lang-python]# ./utils/foo.py
    Traceback (most recent call last):
      File "./utils/foo.py", line 5, in <module>
        mmotor = MediumMotor(OUTPUT_A)
      File "/usr/local/lib/python3.5/dist-packages/python_ev3dev2-2.0.0b2.post5-py3.5.egg/ev3dev2/motor.py", line 1058, in __init__
        super(MediumMotor, self).__init__(address, name_pattern, name_exact, driver_name=['lego-ev3-m-motor'], **kwargs)
      File "/usr/local/lib/python3.5/dist-packages/python_ev3dev2-2.0.0b2.post5-py3.5.egg/ev3dev2/motor.py", line 316, in __init__
        super(Motor, self).__init__(self.SYSTEM_CLASS_NAME, name_pattern, name_exact, **kwargs)
      File "/usr/local/lib/python3.5/dist-packages/python_ev3dev2-2.0.0b2.post5-py3.5.egg/ev3dev2/__init__.py", line 201, in __init__
        chain_exception(DeviceNotFound("%s is not connected." % self), None)
      File "/usr/local/lib/python3.5/dist-packages/python_ev3dev2-2.0.0b2.post5-py3.5.egg/ev3dev2/__init__.py", line 38, in chain_exception
        raise exception from cause
    ev3dev2.DeviceNotFound: MediumMotor(spi0.1:MA) is not connected.
    robot@pi3[ev3dev-lang-python]#
    

    With this patch

    
    robot@pi3[ev3dev-lang-python]# git diff
    diff --git a/ev3dev2/motor.py b/ev3dev2/motor.py
    index da7afbd..3295c46 100644
    --- a/ev3dev2/motor.py
    +++ b/ev3dev2/motor.py
    @@ -1055,6 +1055,9 @@ class MediumMotor(Motor):
    
         def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, name_exact=False, **kwargs):
    
    +        if platform in ('brickpi', 'brickpi3'):
    +            raise Exception("%s is unaware of different motor types, use LargeMotor instead" % platform)
    +
             super(MediumMotor, self).__init__(address, name_pattern, name_exact, driver_name=['lego-ev3-m-motor'], **kwargs)
    
    
    robot@pi3[ev3dev-lang-python]#
    

    We give a better description of what the issue is

    Traceback (most recent call last):
      File "./utils/foo.py", line 5, in <module>
        mmotor = MediumMotor(OUTPUT_A)
      File "/home/robot/ev3dev-lang-python/ev3dev2/motor.py", line 1059, in __init__
        raise Exception("%s is unaware of different motor types, use LargeMotor instead" % platform)
    Exception: brickpi3 is unaware of different motor types, use LargeMotor instead
    robot@pi3[ev3dev-lang-python]#
    
  3. dwalton76 commented on Oct 31, 2018

    @dwalton76
    CollaboratorAuthor

    In the review for #536 @WasabiFan had the idea to have MediumMotor use the LargeMotor driver if running on brickpi(3). I tried it and it works...that seems much better than raising an exception here.

    One downside though is max_speed, it is always going to be reported as the max_speed of a LargeMotor (1020). We are going to need to make max_speed writeable, today we raise an exception

    Traceback (most recent call last):
      File "./utils/foo.py", line 7, in <module>
        mmotor.max_speed = 1450
    AttributeError: can't set attribute
    
  4. dwalton76 commented on Oct 31, 2018

    @dwalton76
    CollaboratorAuthor

    Looking at the max_speed issue some more...there are other attributes that could differ based on motor type and be reported incorrectly if using a brickpi

    • commands
    • count_per_rot
    • count_per_m
    • PID constants?
    • max_speed
    • state
    • stop_actions

    I can't think of a good clean way to handle this...my gut says there aren't enough ev3dev brickpi users out there to justify really hacking up the code to make this work perfectly. We could just declare that we only support large motors 100% correctly when using brickpi, all others should work but with some quirks.

  5. dwalton76 commented on Oct 31, 2018

    @dwalton76
    CollaboratorAuthor

    @dlech any ideas on a better way to handle this?

  6. dlech commented on Oct 31, 2018

    @dlech
    Member

    Can you use set_device on the output port to tell it there is a medium motor attached?

  7. WasabiFan commented on Oct 31, 2018

    @WasabiFan
    Member

    Yeah, what I want to know is whether you can set the driver manually to be the normal medium motor driver.

  8. dwalton76 commented on Oct 31, 2018

    @dwalton76
    CollaboratorAuthor

    Trying this:

    #!/usr/bin/env python3
    
    from ev3dev2.motor import OUTPUT_A, OUTPUT_B, OUTPUT_C
    from ev3dev2.motor import LargeMotor, MediumMotor, SpeedRPM
    from ev3dev2.port import LegoPort
    import sys
    
    port_C = LegoPort(OUTPUT_C)
    port_C.mode = "tacho-motor"
    port_C.set_device = "lego-ev3-m-motor"
    
    mmotor = MediumMotor(OUTPUT_C)
    mmotor.on_for_seconds(SpeedRPM(40), 3)
    

    It barfs on set_device

    Traceback (most recent call last):
      File "./utils/foo.py", line 10, in <module>
        port_C.set_device = "lego-ev3-m-motor"
      File "/home/robot/ev3dev-lang-python/ev3dev2/port.py", line 140, in set_device
        self._set_device = self.set_attr_string(self._set_device, 'set_device', value)
      File "/home/robot/ev3dev-lang-python/ev3dev2/__init__.py", line 297, in set_attr_string
        return self._set_attribute(attribute, name, value)
      File "/home/robot/ev3dev-lang-python/ev3dev2/__init__.py", line 258, in _set_attribute
        self._raise_friendly_access_error(ex, name)
      File "/home/robot/ev3dev-lang-python/ev3dev2/__init__.py", line 281, in _raise_friendly_access_error
        raise driver_error
      File "/home/robot/ev3dev-lang-python/ev3dev2/__init__.py", line 255, in _set_attribute
        attribute.write(value)
    OSError: [Errno 95] Operation not supported
    

    Not 100% clear on if I did exactly what you were asking for.

  9. dlech commented on Oct 31, 2018

    @dlech
    Member

    Yeah, that's it. FWIW, If it actually worked, you would need a delay after set_device but it looks like it isn't implemented in the kernel driver. I kind of though that would be the case.

  10. WasabiFan commented on Oct 31, 2018

    @WasabiFan
    Member

    It looks like it isn't possible to use the medium motor driver on BrickPi I'd say it's fine to throw an error in our constructor.

  11. dwalton76 commented on Oct 31, 2018

    @dwalton76
    CollaboratorAuthor

    If anyone wants to weigh in over at dexter industries on getting them to support this in the future:
    https://forum.dexterindustries.com/t/brickpi3-firmware-an-medium-motors/5463/2

  12. dlech commented on Oct 31, 2018

    @dlech
    Member

    I don't see any reason why this couldn't be implemented in the ev3dev driver using the existing BrickPi3 firmware (other than lack of time to do the work needed).

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