-
Notifications
You must be signed in to change notification settings - Fork 51
/
autonomous.py
61 lines (41 loc) · 956 Bytes
/
autonomous.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
import wheels
import sonar
import time
FORWARD=1
LEFT=2
RIGHT=3
BACKWARD=4
def autodrive(dur):
start_time = time.time()
end_time = time.time() + dur
mode = FORWARD
wheels.forward(-150)
while(time.time() < end_time):
time.sleep(0.1)
cdist = sonar.cdist()
ldist = sonar.ldist()
rdist= sonar.rdist()
print ("%d %d %d" % (ldist, cdist, rdist))
if (mode == FORWARD):
if (cdist < 35 or ldist <6 or rdist < 6):
print ("turning")
wheels.stop()
if (ldist < rdist):
mode=RIGHT
wheels.backward(-100)
time.sleep(1)
wheels.right(-250)
time.sleep(1)
else:
mode=LEFT
wheels.backward(-100)
time.sleep(1)
wheels.left(-250)
time.sleep(1)
if (mode==LEFT or mode==RIGHT):
if (cdist > 50):
mode=FORWARD
wheels.forward(-150)
wheels.stop()
if (__name__ == '__main__'):
autodrive(10)