import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) class Motor(): def __init__(self,EnaA,In1A,In2A,EnaB,In1B,In2B): self.EnaA = EnaA self.In1A = In1A self.In2A = In2A self.EnaB = EnaB self.In1B = In1B self.In2B = In2B GPIO.setup(self.EnaA,GPIO.OUT) GPIO.setup(self.In1A,GPIO.OUT) GPIO.setup(self.In2A,GPIO.OUT) GPIO.setup(self.EnaB,GPIO.OUT) GPIO.setup(self.In1B,GPIO.OUT) GPIO.setup(self.In2B,GPIO.OUT) self.pwmA = GPIO.PWM(self.EnaA, 100); self.pwmA.start(0); self.pwmB = GPIO.PWM(self.EnaB, 100); self.pwmB.start(0); def move(self,speed=0.5,turn=0,t=0): speed *=100 turn *=100 leftSpeed = speed - turn print(leftSpeed) rightSpeed = speed + turn print(rightSpeed) if leftSpeed>100: leftSpeed=100 elif leftSpeed<-100: leftSpeed= -100 if rightSpeed>100: rightSpeed=100 elif rightSpeed<-100: rightSpeed= -100 self.pwmA.ChangeDutyCycle(abs(rightSpeed)) self.pwmB.ChangeDutyCycle(abs(leftSpeed)) if leftSpeed>0: GPIO.output(self.In1A,GPIO.HIGH) GPIO.output(self.In2A,GPIO.LOW) print('eksekusi left speed >0') print(leftSpeed) else: GPIO.output(self.In1A,GPIO.LOW) GPIO.output(self.In2A,GPIO.HIGH) print('eksekusi left speed <0') print(leftSpeed) if rightSpeed>0: GPIO.output(self.In1B,GPIO.HIGH) GPIO.output(self.In2B,GPIO.LOW) print('eksekusi right speed >0') print(rightSpeed) else: GPIO.output(self.In1B,GPIO.LOW) GPIO.output(self.In2B,GPIO.HIGH) print('eksekusi right speed <0') print(rightSpeed) sleep(t) def stop(self,t=0): self.pwmA.ChangeDutyCycle(0); self.pwmB.ChangeDutyCycle(0); sleep(t) def main(): motor.move(0.6,0,2) motor.stop(2) motor.move(-0.5,0.2,2) motor.stop(2) if __name__ == '__main__': motor= Motor(2,3,4,17,22,27) main()