-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwander.py
76 lines (71 loc) · 2.78 KB
/
wander.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
import time
from lclkbd import *
from Ultrasonic import *
class wander:
def __init__(self, kb):
self.kb = kb
self.kb.reset_head()
self.kb.action.control.speed = 8
self.ultrasonic = Ultrasonic()
def go(self):
try:
#print("Try")
f = self.ultrasonic.getDistance()
print("f",f)
l = 0
r = 0
#edgeDanger = 40
aheadClear = 50
obstDanger = 35
#criticalEdge = 15
#make some decisions
#way ahead blocked
if (f < obstDanger):
print("Blocked")
# backup
self.kb.action.control.backWard()
# look left
self.kb.action.control.turnLeft()
self.kb.action.control.turnLeft()
l = self.ultrasonic.getDistance()
print("l",l)
if (l > aheadClear):
self.kb.action.control.forWard()
else:
#look right
self.kb.action.control.turnRight()
self.kb.action.control.turnRight()
self.kb.action.control.turnRight()
self.kb.action.control.turnRight()
r = self.ultrasonic.getDistance()
print("r",r)
if (r > aheadClear):
self.kb.action.control.forWard()
else:
#go back to faccing forward
self.kb.action.control.turnLeft()
self.kb.action.control.turnLeft()
#back up a bit more
self.kb.action.control.backWard()
if (r > l):
self.kb.action.control.turnRight()
self.kb.action.control.turnRight()
r = self.ultrasonic.getDistance()
print("r2",r)
if (r > aheadClear):
self.kb.action.control.forWard()
else:
# look left
self.kb.action.control.turnLeft()
self.kb.action.control.turnLeft()
l = self.ultrasonic.getDistance()
print("l2",l)
if (l > aheadClear):
self.kb.action.control.forWard()
else:
print("forward")
self.kb.action.control.forWard()
except:
e = sys.exc_info()[0]
print("Except: %s" % e)
pass