Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import time
- class agent():
- def __init__(self, pins, frequency):
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- self.pins = pins
- self.pwms = []
- self.frequency = frequency
- for pin in self.pins:
- print(pin)
- GPIO.setup(pin, GPIO.OUT)
- self.pwms.append(GPIO.PWM(pin, self.frequency))
- for pwm in self.pwms:
- pwm.start(0)
- def stop_motors(self):
- for pwm in self.pwms:
- pwm.ChangeDutyCycle(0)
- def drive(self, left_motor, right_motor):
- right_motor *= 100
- left_motor *= 100
- print(left_motor, right_motor)
- print(self.pwms)
- if left_motor >= 0:
- print("left", )
- self.pwms[0].ChangeDutyCycle(left_motor)
- self.pwms[1].ChangeDutyCycle(0)
- else:
- print("-left")
- self.pwms[1].ChangeDutyCycle(left_motor * -1) # It is currently negative so making it positive
- self.pwms[0].ChangeDutyCycle(0)
- if right_motor >= 0:
- print("right")
- self.pwms[2].ChangeDutyCycle(right_motor)
- self.pwms[3].ChangeDutyCycle(0)
- else:
- print("-right")
- self.pwms[3].ChangeDutyCycle(right_motor * -1) # It is currently negative so making it positive
- self.pwms[2].ChangeDutyCycle(0)
- def cleanup(self):
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement