I'm trying to control H-bridge by raspberry and ROS with python. I tried to write a class to control motors by 2 pin in PWM mode and 4 pins as digital output.
That class it's called by ros to rightly use motors when a joypad callback sends driving controls.
I can properly set PWM dutycicles inside the class and its functions, naming them as:
self.pin = GPIO.PWM(pinID, Frequency)
But I receive error when I try to set digital outputs whit GPIO(pinID, 0). I think this happens because setting GPIO output is made without any reference to the class (self) but I can't find any code around that can help me, nor I've such a experience in python to understand how to do.
I found scripts witch works like mine, but coming from Java and with some implementations expectations, I'm trying to realize that using classes instead of a single (heavy) main.
The (basic) code to show what I meen:
import RPi.GPIO as GPIO
Frequency = 30
# Set up the GPIO pins
pwmLeft_pin = 10
pwmRight_pin = 7
leftF = 9
leftB = 11
rightF = 8
rightB = 25
class TankMotors():
def __init__(self):
GPIO.setmode(GPIO.BCM)
# GPIO.setwarnings(False)
GPIO.setup(leftF, GPIO.OUT)
GPIO.setup(leftB, GPIO.OUT)
GPIO.setup(rightF, GPIO.OUT)
GPIO.setup(rightB, GPIO.OUT)
GPIO.setup(pwmLeft_pin, GPIO.OUT)
GPIO.setup(pwmRight_pin, GPIO.OUT)
self.leftPwm = GPIO.PWM(pwmLeft_pin, Frequency)
self.rightPwm = GPIO.PWM(pwmRight_pin, Frequency)
self.brake()
def brake(self):
GPIO(leftB, 0)
GPIO(leftF, 0)
GPIO(rightB, 0)
GPIO(rightF, 0)
self.rightPwm.ChangeDutyCycle(0)
self.leftPwm.ChangeDutyCycle(0)
When I call self.brake() I receive error:
TypeError: 'module' object is not callable
This is due to fact that it can't understand GPIO(leftB, 0) as said..
But if I reduce brake() function only to PWM pins (only last 2 rows of function) I can use it without problems.
so..
There is a way to set GPIO.outputs in class functions in python?
(or) There is a way to name GPIO.outputs as class variables (as i did with PWM. es: self.leftPwm)?
Tank you a lot if you can help me.
GPIO
is definitely a module (as you called it). What do you want to do and why do you useas GPIO
part in the import? – 0andriy