forked from RyanZotti/Self-Driving-Car
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |