-
Notifications
You must be signed in to change notification settings - Fork 1
/
line_following.py
53 lines (39 loc) · 972 Bytes
/
line_following.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
from myro import *
DEFAULT_SPEED = -0.1
def goStraight():
motors(DEFAULT_SPEED, DEFAULT_SPEED)
def turnRightNotch(counter=1):
for i in range(0,counter):
turnLeft(0.1, 0.2)
def turnLeftNotch(counter=1):
for i in range(0, counter):
turnRight(0.1, 0.2)
def correctYourself(lastStep=0):
right, left = getLine()
print "left = %d, right = %d" % (left, right)
if left == 1 and right == 0:
# Left half is over line
turnLeftNotch()
return correctYourself(1)
elif right == 1 and left == 0:
# Right half is over line
turnRightNotch()
return correctYourself(-1)
elif right == 1 and left == 1:
goStraight()
return True
else:
if lastStep == -1: # Right
turnRightNotch()
return correctYourself(lastStep)
elif lastStep == 1:
turnLeftNotch()
return correctYourself(lastStep)
else:
goStraight()
return False
if __name__ == "__main__":
initialize("/dev/tty.IPRE6-193907-DevB")
goStraight()
while 1:
correctYourself()