I'm using a raspberry pi 4b and it's running Raspbian 10 (Buster). I have a Geekworm Pi Motor HAT Full function Robot Expansion Board. I'm using the GitHub code: Raspi-MotorHat-master. I'm using Python version 3.7. This is the link to the GitHub library I'm using: https://github.com/Alictronix/Raspi-MotorHat

I'm trying to build a simple robot and drive 2 DC motors. I'm trying to get 4WDTEST.py to work. I keep getting this error in Raspi_MotorHAT.py:

File "/home/xxxxx/PythonCode/Raspi-MotorHat-master/Raspi_MotorHAT.py", line 213, in setSpeed
    self.MC._pwm.setPWM(self.PWMpin, 0, speed*16)
AttributeError: 'Raspi_DCMotor' object has no attribute 'PWMpin'

This is the function where the code is failing:

    def setSpeed(self, speed):
        if (speed < 0):
            speed = 0
        if (speed > 255):
            speed = 255
        self.MC._pwm.setPWM(self.PWMpin, 0, speed*16)

I assume this code was done for Python 2.7 or earlier. I can't figure out how to get past this error. I've looked at the Adafruit-Motor-HAT-Python-Library-master code and it is the same. I assume this code works. Any help would be greatly appreciated.

  • Please post links to the exact library you're using. – user96931 Feb 11 '20 at 20:30
  • The library code is fine. You have not explained what you are actually calling. What command are you entering at the command line which results in that error? – joan Feb 11 '20 at 21:05

I figured this out. If I used integer values instead of variables, the code worked. It turns out that the indentation for the class attribute definitions was wrong, so python did not recognize the attributes. I removed all the tabs in the code and replaced it with 4 spaces. Python is new to me and now I've learned to reformat any code you get, before doing anything with it.

  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year. – Ingo Feb 14 '20 at 18:29

I'm just helping someone else with this library over on the Raspberry Pi Educators Forum. I'm finding that the code is littered with errors, mostly bad indentation and missing print commands. FYI, for the same money, you can get Adafruit's Motor & Stepper baord (which it looks like this is based on), with decent code and support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.