From 383b4a6a434a3431deae6cf2363d4dd4e7cb6532 Mon Sep 17 00:00:00 2001 From: Ryan Zotti Date: Sat, 9 Jul 2016 15:01:39 -0400 Subject: [PATCH] modularizing motor class --- Motor.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Motor.py diff --git a/Motor.py b/Motor.py new file mode 100644 index 00000000..cedce9e2 --- /dev/null +++ b/Motor.py @@ -0,0 +1,81 @@ +class Motor: + + def __init__(self, pinForward, pinBackward, pinControlStraight,pinLeft, pinRight, pinControlSteering): + """ Initialize the motor with its control pins and start pulse-width + modulation """ + + self.pinForward = pinForward + self.pinBackward = pinBackward + self.pinControlStraight = pinControlStraight + self.pinLeft = pinLeft + self.pinRight = pinRight + self.pinControlSteering = pinControlSteering + GPIO.setup(self.pinForward, GPIO.OUT) + GPIO.setup(self.pinBackward, GPIO.OUT) + GPIO.setup(self.pinControlStraight, GPIO.OUT) + + GPIO.setup(self.pinLeft, GPIO.OUT) + GPIO.setup(self.pinRight, GPIO.OUT) + GPIO.setup(self.pinControlSteering, GPIO.OUT) + + self.pwm_forward = GPIO.PWM(self.pinForward, 100) + self.pwm_backward = GPIO.PWM(self.pinBackward, 100) + self.pwm_forward.start(0) + self.pwm_backward.start(0) + + self.pwm_left = GPIO.PWM(self.pinLeft, 100) + self.pwm_right = GPIO.PWM(self.pinRight, 100) + self.pwm_left.start(0) + self.pwm_right.start(0) + + GPIO.output(self.pinControlStraight,GPIO.HIGH) + GPIO.output(self.pinControlSteering,GPIO.HIGH) + + def forward(self, speed): + """ pinForward is the forward Pin, so we change its duty + cycle according to speed. """ + self.pwm_backward.ChangeDutyCycle(0) + self.pwm_forward.ChangeDutyCycle(speed) + + def forward_left(self, speed): + """ pinForward is the forward Pin, so we change its duty + cycle according to speed. """ + self.pwm_backward.ChangeDutyCycle(0) + self.pwm_forward.ChangeDutyCycle(speed) + self.pwm_right.ChangeDutyCycle(0) + self.pwm_left.ChangeDutyCycle(100) + + def forward_right(self, speed): + """ pinForward is the forward Pin, so we change its duty + cycle according to speed. """ + self.pwm_backward.ChangeDutyCycle(0) + self.pwm_forward.ChangeDutyCycle(speed) + self.pwm_left.ChangeDutyCycle(0) + self.pwm_right.ChangeDutyCycle(100) + + def backward(self, speed): + """ pinBackward is the forward Pin, so we change its duty + cycle according to speed. """ + + self.pwm_forward.ChangeDutyCycle(0) + self.pwm_backward.ChangeDutyCycle(speed) + + def left(self, speed): + """ pinForward is the forward Pin, so we change its duty + cycle according to speed. """ + self.pwm_right.ChangeDutyCycle(0) + self.pwm_left.ChangeDutyCycle(speed) + + def right(self, speed): + """ pinForward is the forward Pin, so we change its duty + cycle according to speed. """ + self.pwm_left.ChangeDutyCycle(0) + self.pwm_right.ChangeDutyCycle(speed) + + def stop(self): + """ Set the duty cycle of both control pins to zero to stop the motor. """ + + self.pwm_forward.ChangeDutyCycle(0) + self.pwm_backward.ChangeDutyCycle(0) + self.pwm_left.ChangeDutyCycle(0) + self.pwm_right.ChangeDutyCycle(0) \ No newline at end of file