-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstates.py
215 lines (162 loc) · 6.29 KB
/
states.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
import random
try:
import RPi.GPIO as GPIO
from pycreate2 import Create2
except:
from create2Dummy import Create2
import time
class _start():
def __init__(_state, scaredyBot):
_state.scaredyBot = scaredyBot
def getName(_state):
return "start"
def enter(_state):
print('entering', _state.scaredyBot.getState())
_state.scaredyBot.baseSpeed = 0
time.sleep(.5)
def execute(_state):
while _state.scaredyBot.ready == False:
pass
_state.scaredyBot.changeState(_searching(_state.scaredyBot))
def exit(_state):
print('exiting', _state.scaredyBot.getState())
class _searching():
botAngle=0
currAngle = 90
currDir = 'left'
goalAngle = currAngle
angleChange = 45
def __init__(_state, scaredyBot):
_state.scaredyBot = scaredyBot
def getName(_state):
return "searching"
def enter(_state):
print('entering', _state.scaredyBot.getState())
_state.scaredyBot.baseSpeed = 75
_state.scaredyBot.pir.light.green()
_state.currDir = _state.scaredyBot.randDir()
_state.botAngle = _state.scaredyBot.checkAngle()
_state.scaredyBot.stop()
def execute(_state):
sense = _state.scaredyBot.getSensors(True)
if(_state.scaredyBot.motion!=True):
#print(_state.currDir, sense['angle'])
pass
else:
_state.scaredyBot.changeState(_running(_state.scaredyBot))
def exit(_state):
print('exiting', _state.scaredyBot.getState())
class _running():
phases = {'rotating':'rotating','running':'running','waiting':'waiting', 'done':'done'}
phase = 'rotating'
firstRotate = True
newRotate = True
rotating = True
running = False
waiting = False
turnDir = 'left'
goalAngle = 120
currAngle = 0
#wall = False
#leftWall = False
#rightWall = False
startTime = 0
endTime = 18
def __init__(_state, scaredyBot):
_state.scaredyBot = scaredyBot
def getName(_state):
return "running"
def enter(_state):
_state.turnDir = _state.scaredyBot.randDir()
_state.goalAngle = random.randint(100,260)
print('entering', _state.scaredyBot.getState())
_state.scaredyBot.baseSpeed = 350
_state.scaredyBot.pir.light.red()
def execute(_state):
currTime = time.time()
#print('phase',_state.phase)
#print('wall', _state.scaredyBot.wall, _state.scaredyBot.wallLeft, _state.scaredyBot.wallRight)
#print('curr', currTime)
#print('end', _state.endTime)
if _state.phase == _state.phases['rotating']:
if _state.firstRotate == True:
_state.endTime = _state.endTime + currTime
if _state.newRotate:
bump = _state.scaredyBot.checkBump()
#print(bump)
if _state.scaredyBot.wallRight and _state.scaredyBot.wallLeft == False:
_state.turnDir = 'left'
_state.goalAngle = random.randint(100,170)
if _state.scaredyBot.wallRight == False and _state.scaredyBot.wallLeft:
_state.turnDir = 'right'
_state.goalAngle = random.randint(330, 280)
if _state.scaredyBot.wallRight and _state.scaredyBot.wallLeft:
_state.turnDir = _state.scaredyBot.randDir()
_state.goalAngle = random.randint(30, 80)
_state.currAngle = 0
_state.startTime = currTime
_state.endTime = _state.endTime + 3
_state.newRotate = False
if _state.firstRotate == False:
_state.scaredyBot.drive(dir = 'back')
time.sleep(.2)
_state.scaredyBot.stop()
_state.firstRotate = False
_state.scaredyBot.rotate(_state.turnDir)
if abs(_state.currAngle) >= _state.goalAngle:
_state.scaredyBot.stop()
_state.phase = _state.phases['running']
_state.startTime = currTime
#_state.endTime += currTime
_state.scaredyBot.drive()
elif abs(_state.currAngle) < _state.goalAngle:
_state.currAngle += _state.scaredyBot.checkAngle()
elif _state.phase == _state.phases['running']:
bump = _state.scaredyBot.checkBump()
if (currTime >= _state.endTime - 7):
_state.scaredyBot.stop()
_state.phase = _state.phases['waiting']
_state.scaredyBot.pir.light.blue()
if _state.scaredyBot.wall:
_state.newRotate = True
_state.phase = _state.phases['rotating']
elif _state.phase == _state.phases['waiting']:
#print(_state.scaredyBot.checkMotion())
bump = _state.scaredyBot.checkBump(False)
if _state.scaredyBot.wall:
_state.scaredyBot.drive(dir = 'back')
time.sleep(.2)
_state.newRotate = True
_state.phase = _state.phases['rotating']
time.sleep(.2)
if currTime >= _state.endTime:
_state.phase = _state.phases['done']
elif _state.phase == _state.phases['done']:
if _state.scaredyBot.looped<_state.scaredyBot.maxLoops:
_state.scaredyBot.wall = False
_state.scaredyBot.wallLeft = False
_state.scaredyBot.wallRight = False
_state.scaredyBot.looped += 1
_state.scaredyBot.changeState(_searching(_state.scaredyBot))
else:
_state.scaredyBot.changeState(_end(_state.scaredyBot))
#return
def exit(_state):
print('exiting', _state.scaredyBot.getState())
class _end():
def __init__(_state, scaredyBot):
_state.scaredyBot = scaredyBot
def getName(_state):
return "end"
def enter(_state):
print('entering', _state.scaredyBot.getState())
_state.scaredyBot.baseSpeed = 0
_state.scaredyBot.stop()
_state.exit()
def execute(_state):
return
def exit(_state):
print('exiting', _state.scaredyBot.getState())
time.sleep(.5)
_state.scaredyBot.destroy()