forked from RyanZotti/Self-Driving-Car
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMotor.py
81 lines (66 loc) · 2.99 KB
/
Motor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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)