Skip to content

Commit

Permalink
modularizing motor class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Zotti authored and Ryan Zotti committed Jul 9, 2016
1 parent 76747d6 commit 383b4a6
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Motor.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 383b4a6

Please sign in to comment.