Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 6970

Troubleshooting • Motors not running backwards in gpiozero

$
0
0
I'm building a car that has 2 motors on each side connected to one H-bridge, which is connected to my Raspberry Pi 5. I'm using gpiozero to control the car, but the motors are not moving backwards when I set the speed to -1. Here's my code:

Code:

from gpiozero import PWMOutputDevice, DigitalOutputDevicefrom time import sleepclass Motor:    def __init__(self, ena, in1a, in2a, enb, in1b, in2b):        self.pwmA = PWMOutputDevice(ena)        self.in1A = DigitalOutputDevice(in1a)        self.in2A = DigitalOutputDevice(in2a)        self.pwmB = PWMOutputDevice(enb)        self.in1B = DigitalOutputDevice(in1b)        self.in2B = DigitalOutputDevice(in2b)    def move(self, speed=0.5, turn=0, t=0):        left_speed = max(min(speed - turn, 1), -1)        right_speed = max(min(speed + turn, 1), -1)        # Set speed for all motors        self.pwmA.value = abs(left_speed)        self.pwmB.value = abs(right_speed)        # Control left side motors        if left_speed > 0:            self.in1A.on()            self.in2A.off()            print("Left side motors forward: IN1A ON, IN2A OFF")        else:            self.in1A.off()            self.in2A.on()            print("Left side motors backward: IN1A OFF, IN2A ON")        # Control right side motors        if right_speed > 0:            self.in1B.on()            self.in2B.off()            print("Right side motors forward: IN1B ON, IN2B OFF")        else:            self.in1B.off()            self.in2B.on()            print("Right side motors backward: IN1B OFF, IN2B ON")        sleep(t)        self.stop()    def stop(self, t=0):        print("Stopping motors")        self.pwmA.value = 0        self.pwmB.value = 0        self.in1A.off()        self.in2A.off()        self.in1B.off()        self.in2B.off()        sleep(t)def main():    Motor.move(1, 0, 3)    sleep(1)    Motor.move(1, 0.3, 3)    sleep(1)    Motor.stop()if __name__ == '__main__':    motor = Motor(27, 17, 22, 5, 23, 24)    main()
I also double checked the wiring and everything is correctly. I would appreciate any help I can get with this. Thanks.

Statistics: Posted by suryasure — Tue Jul 23, 2024 1:37 am — Replies 2 — Views 37



Viewing all articles
Browse latest Browse all 6970

Trending Articles