So i'm making a little robot using a raspberry pi B+ , everything is going fairly nicely, but I get this error message that I don't seem to be able to solve ..
Attribute error : RPi.GPIO.PWM has no attribute 'ChangeDutyCycle'
Here's two things you might need to know : :D - The code worked perfectly before I put it as a class, ChangeDutyCycle DOES exist - If I put my mouse in an 'unallowed' i.e. which gives ChangeDutyCycle a value less than 0 or more than 100, the error changes and becomes 'ChangeDutyCycle can't accept value less than 0 or more than 100' (So first you tell me there is no such function, and then tell me it can't have some values? :roll: )
So I'm going slightly crazy now. Note : I'm a complete beginner in python, and honestly, it seems to me like a bad version of java, but the RPi GPIO seems to be optimised for this language so I'm trying it out, so if you see any other mistake or bad things, do let me know :D
The code now :
My 'main', where I take input from a pygame window (position of mouse)
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pygame
import RPi.GPIO as GPIO
from pygame.locals import *
from control import control
print('Path :', pygame.__file__)
def main():
pygame.init()
screen = pygame.display.set_mode((200,200))
ctrl = control()
bg = pygame.Surface(screen.get_size())
bg = bg.convert()
bg.fill((250,250,250))
font = pygame.font.Font(None, 36)
text = font.render("Hello", 1, (10,10,10,))
textpos = text.get_rect()
textpos.centerx = bg.get_rect().centerx
bg.blit(text, textpos)
screen.blit(bg, (0,0))
pygame.display.flip()
try:
while 1:
for event in pygame.event.get():
bg.fill((250, 250, 250))
if event.type == QUIT:
return
pos = pygame.mouse.get_pos()
x = pos[0]-100
y = -(pos[1]-200)-100
text = font.render(str(x)+' '+str(y), 1, (10,10,10,))
ctrl.updateEngine(x,y)
bg.blit(text,textpos)
screen.blit(bg, (0,0))
pygame.display.flip()
except KeyboardInterrupt:
return
finally:
ctrl.cleanup()
if __name__ == '__main__': main()
And my class with functions to control motors , control.py :
import RPi.GPIO as GPIO
from time import sleep
import sys
import Tkinter as tk
class control:
def __init__(self):
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.OUT)# set GPIO 25 as output for white led
GPIO.setup(18, GPIO.OUT)# set GPIO 24 as output for red led
GPIO.setup(2, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
self.Rb = GPIO.PWM(2, 100)
self.Lf = GPIO.PWM(18, 100)
self.Lb = GPIO.PWM(17, 100)
self.Rf = GPIO.PWM(3, 100)
self.Rb.start(0)
self.Rf.start(0)
self.Lb.start(0)
self.Lf.start(0)
def cleanup(self):
self.Rb.stop()
self.Rf.stop()
self.Lb.stop()
self.Lf.stop()
def updateEngine(self, x, y):
self.clear()
if y>15 and (x>15 or x<-15) :
self.Rf.ChangeDutyCucle(y-15-x)
self.Lf.ChangeDutyCycle(y-15+x)
elif y<-15 and (x>15 or x<-15) :
self.Rb.ChangeDutyCycle(-15-y-x)
self.Lb.ChangeDutyCycle(-15-y+x)
def clear(self):
self.Rf.ChangeDutyCycle(0)
self.Rb.ChangeDutyCycle(0)
self.Lf.ChangeDutyCycle(0)
self.Lb.ChangeDutyCycle(0)
So the error happens in control.py, at the updateEngine method. Also you'll note I imported an amazing number of 3 times the same package (RPi.GPIO) cause I'm not sure where to import it ! :)
Any help would be graciously accepted :)