-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcar.py
90 lines (81 loc) · 2.21 KB
/
car.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
82
83
84
85
86
87
88
89
90
import RPi.GPIO as GPIO
import time
import random
from sensor import distance
GPIO.setwarnings(False)
front_echo = 1
front_trigger = 7
rear_echo = 17
rear_trigger = 27
def init():
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.OUT) # right side motor
GPIO.setup(13, GPIO.OUT) # right side motor
GPIO.setup(19, GPIO.OUT) # left side motor
GPIO.setup(26, GPIO.OUT) # left side motor
# front sensor setup
GPIO.setup(front_echo, GPIO.IN)
GPIO.setup(front_trigger, GPIO.OUT)
# rear sensor setup
GPIO.setup(rear_echo, GPIO.IN)
GPIO.setup(rear_trigger, GPIO.OUT)
def forward(time_frame):
print('GOING FORWARD')
init()
GPIO.output(6, False) # right side wheels / input 1 & 2
GPIO.output(13, True) # right side wheels input 1 & 2
GPIO.output(19, False) # left side wheels / input 3 & 4
GPIO.output(26, True) # left side wheels / input 3 & 4
time.sleep(time_frame)
GPIO.cleanup()
return True
def pivot_left(time_frame):
print('PIVOTING LEFT')
init()
GPIO.output(6, False)
GPIO.output(13, True)
GPIO.output(19, True)
GPIO.output(26, False)
time.sleep(time_frame)
GPIO.cleanup()
return True
def pivot_right(time_frame):
print('PIVOTING RIGHT')
init()
GPIO.output(6, True)
GPIO.output(13, False)
GPIO.output(19, False)
GPIO.output(26, True)
time.sleep(time_frame)
GPIO.cleanup()
return True
def reverse(time_frame):
print('REVERSING')
init()
GPIO.output(6, True) # right side wheels / input 1 & 2
GPIO.output(13, False) # right side wheels input 1 & 2
GPIO.output(19, True) # left side wheels / input 3 & 4
GPIO.output(26, False) # left side wheels / input 3 & 4
time.sleep(time_frame)
GPIO.cleanup()
return True
def turn_right(time_frame):
print('turning right')
init()
GPIO.output(6, False)
GPIO.output(13, False)
GPIO.output(19, False)
GPIO.output(26, True)
time.sleep(time_frame)
GPIO.cleanup()
return True
def turn_left(time_frame):
print('turning left')
init()
GPIO.output(6, False)
GPIO.output(13, True)
GPIO.output(19, False)
GPIO.output(26, False)
time.sleep(time_frame)
GPIO.cleanup()
return True