-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
445 lines (434 loc) · 10.4 KB
/
main.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# Coordinate_wRandomSafeguard_V1
#
# Author: Alexa Baldon
# Collaboration: Steve Holguin
# Course: ENGR150 Fall 2018
# Last Modified: Version 5, Dec 5 2018
#
# Python application which applies a coordinate system to
# direct a micromouse towards the center of a maze of specified size
from mm_library import *
import time
#Mouse begins in NE corner facing north
#Typically begins in (0,0)
# __________ __________
# |0,y | x,y|
# | | | N(0)
# | Q1 | Q2 | |
# |_________|__________| (3)W--+--E(1)
# | | | |
# | Q0 | Q3 | S
# | | | (2)
# |0,0______|_______x,0|
#
#Known defects: Maze "size" seems to fluctuate depending on battery level
#because each unit of measurement is the distance traveled in 0.15s
#see "maintain" function under mm_library
#
#Currently the mouse will refuse to make less-than-ideal turns, instead
#opting to run into a wall.
#
#Location tracking works fairly well in Q0, or whichever quadrant
#is used as the start, but measurement error and potentially incorrect code
#distorts the states of the robot.
#
#Center-finding does not seem to work as well when outside of Q0.
#
#Occasionally the mouse will not be able to determined its quadrant at all
#leading to a complete stop.
#---------------------------------------------------------------------------#
#---------------------------Initialization----------------------------------#
x = 0
y = 0
direction = 0
size = 60
turn = 0 #used to determine if a turn was made through a function
#---------------------------Functions---------------------------------------#
# @param x integer value for the x coordinate of the mouse
# @param y integer value for the y coordinate of the mouse
# @param size integer value for the estimated maze size
# @returns an integer value 0-3 to represent a quadrant location
#
# Returns the quadrant in which the mouse is located. Displays the current
# coordinates, direction, and quadrant onto the screen.
def getQ(x,y,size):
mid = (size/2)
result = 4
if (x<=mid and y<mid):
result = 0
if (x<mid and y>=mid):
result = 1
if (x>=mid and y<=mid):
result = 3
if (x>mid and y>mid):
result = 2
print("coords = (" + str(x) + "," + str(y) + ") direction = " + str(direction) + ", Q = " + str(result))
return result
# A function that prompts the mouse to execute a series of tasks to turn 180 degrees
def turnAround():
moveLeft()
time.sleep(0.9)
moveBwd()
time.sleep(0.15)
moveFwd()
time.sleep(0.15)
getQ(x,y,size)
# @returns an integer value corresponding to the most recent turn
# or no-turn behavior (1 for turn, 0 for no-turn)
#
# Exhibits normal behavior for the mouse, the mouse will move forward
# in 0.15 second intervals, straightening itself as it goes. Turns 180
# if needed, returning an integer value 1 if it did, and 0 if it did not.
def normal():
turned = 0
maintain()
getQ(x,y,size)
if (sensorFwd()==0):
if (sensorL10()==0 and sensorR10()==0):
turned = 1
turnAround()
print("TURNED!")
return turned
#---------------------------------------------------------------------------#
#---------------------------------------------------------------------------#
while (getQ(x,y,size)==0 or getQ(x,y,size)==1 or getQ(x,y,size)==2 or getQ(x,y,size)==3):
#----------------------Quadrant Zero----------------------------------------#
while (getQ(x,y,size)==0):
if (direction==0): #Facing North
turn = normal()
if (turn == 1):
direction = 2
y = y + 1
if (sensorR10()==1):
moveFwd()
time.sleep(0.58)
y = y + 3
moveRight()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
x = x + 4
direction = 1 #Shift to east
getQ(x,y,size)
if (direction==1): #Facing east
turn = normal()
if (turn == 1):
direction = 3
x = x + 1
if (sensorL10()==1):
moveFwd()
time.sleep(0.58)
x = x + 3
moveLeft()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
y = y + 4
direction = 0 #Shift to north
getQ(x,y,size)
if (direction==2): #Facing south
turn = normal()
if (turn == 1):
direction = 0
y = y - 1
if (sensorL10()==1):
moveFwd()
y = y - 3
time.sleep(0.58)
moveLeft()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
x = x + 4
direction = 1 #Shift to east
getQ(x,y,size)
if (direction==3): #Facing west
turn = normal()
if (turn == 1):
direction = 1
x = x - 1
if (sensorR10()==1):
moveFwd()
time.sleep(0.58)
x = x - 3
moveRight()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
y = y + 4
direction = 0 #Shift to north
getQ(x,y,size)
#-----------------------Quadrant One----------------------------------------#
while (getQ(x,y,size)==1):
if (direction==0): #Facing North
turn = normal()
if (turn == 1):
direction = 2
print (direction)
y = y + 1
if (sensorR10()==1):
moveFwd()
y = y + 3
time.sleep(0.58)
moveRight()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
x = x + 4
direction = 1 #Shift to east
getQ(x,y,size)
if (direction==1): #Facing East
turn = normal()
if (turn == 1):
direction = 3
x = x + 1
if (sensorL10()==1):
moveFwd()
time.sleep(0.58)
x = x + 3
moveLeft()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
y = y + 4
direction = 0 #Shift to North
getQ(x,y,size)
if (direction==2): #Facing South
turn = normal()
if (turn == 1):
direction = 0
y = y - 1
if (sensorR10()==1):
moveFwd()
time.sleep(0.58)
y = y - 3
moveRight()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
x = x + 4
direction = 1 #Shift to east
getQ(x,y,size)
if (direction==3): #Facing West
turn = normal()
if (turn == 1):
direction = 1
x = x - 1
if (sensorR10()==1):
moveFwd()
time.sleep(0.58)
x = x - 3
moveRight()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
y = y + 4
direction = 0 #Shift to North
getQ(x,y,size)
#-----------------------Quadrant Two----------------------------------------#
while (getQ(x,y,size)==2):
if (direction==0): #Facing North
turn = normal()
if (turn == 1):
direction = 2
y = y + 1
if (sensorL10()==1):
moveFwd()
y = y + 3
time.sleep(0.58)
moveLeft()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
x = x + 4
direction = 1 #Shift to east
getQ(x,y,size)
if (direction==1): #Facing East
turn = normal()
if (turn == 1):
direction = 3
x = x + 1
if (sensorR10()==1):
moveFwd()
time.sleep(0.58)
x = x + 3
moveRight()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
y = y + 4
direction = 0 #Shift to North
getQ(x,y,size)
if (direction==2): #Facing South
turn = normal()
if (turn == 1):
direction = 0
y = y - 1
if (sensorR10()==1):
moveFwd()
time.sleep(0.58)
y = y - 3
moveRight()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
x = x + 4
direction = 1 #Shift to east
getQ(x,y,size)
if (direction==3): #Facing West
turn = normal()
if (turn == 1):
direction = 1
x = x - 1
if (sensorL10()==1):
moveFwd()
time.sleep(0.58)
x = x - 3
moveLeft()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
y = y + 4
direction = 0 #Shift to North
getQ(x,y,size)
#----------------------Quadrant Three----------------------------------------#
while (getQ(x,y,size)==3):
if (direction==0): #Facing North
turn = normal()
if (turn == 1):
direction = 2
y = y + 1
if (sensorL10()==1):
moveFwd()
y = y + 3
time.sleep(0.58)
moveLeft()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
x = x + 4
direction = 1 #Shift to east
getQ(x,y,size)
if (direction==1): #Facing East
turn = normal()
if (turn == 1):
direction = 3
x = x + 1
if (sensorL10()==1):
moveFwd()
time.sleep(0.58)
x = x + 3
moveLeft()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
y = y + 4
direction = 0 #Shift to North
getQ(x,y,size)
if (direction==2): #Facing South
turn = normal()
if (turn == 1):
direction = 0
y = y - 1
if (sensorR10()==1):
moveFwd()
time.sleep(0.58)
moveRight()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
x = x + 4
direction = 1 #Shift to east
getQ(x,y,size)
if (direction==3): #Facing West
turn = normal()
if (turn == 1):
direction = 1
x = x - 1
if (sensorR10()==1):
moveFwd()
time.sleep(0.58)
x = x - 3
moveRight()
time.sleep(0.47)
moveFwd()
time.sleep(0.65)
y = y + 4
direction = 0 #Shift to North
getQ(x,y,size)
# Safegaurd code to stop in case of error
while (1==1):
if (sensorFwd()==0): #checks if forward is blocked
stop()
time.sleep(0.2)
moveFwd()
time.sleep(0.35)
if (sensorL10()==1 and sensorR10()==0): #if left is open, move left
moveLeft()
time.sleep(0.45)
moveFwd()
time.sleep(0.2)
elif (sensorR10()==1 and sensorL10()==0): #if right is open, move right
moveRight()
time.sleep(0.45)
moveFwd()
time.sleep(0.2)
elif (sensorR10()==0 and sensorL10()==0): #if both are blocked, not digonal, spin 180
stop()
time.sleep(0.2)
moveRight()
time.sleep(0.9)
stop()
moveBwd()
time.sleep(0.5)
else: #if neither right or left is blocked, choose
c = random.randint(0,1)
if (c == 0):
moveLeft()
time.sleep(0.45)
moveFwd()
time.sleep(0.2)
if (c == 1):
moveLeft()
time.sleep(0.45)
moveFwd()
time.sleep(0.2)
if (sensorFwd()==1): #if forward is NOT blocked,
moveFwd()
if (sensorR10()==1):
c = random.randint(0,3) #1/3 chance of turning
print ("Random number for possible R turn (R is 1):" + str(c))
if (c == 1):
moveFwd()
time.sleep(0.35)
if (sensorR10()==1):
moveRight()
time.sleep(0.45)
moveFwd()
time.sleep(0.2)
if (sensorL10()==1):
c = random.randint(0,3) #1/3 chance of turning
print ("Random number for possible L turn (L is 1):" + str(c))
if (c == 1):
moveFwd()
time.sleep(0.35)
if (sensorR10()==1):
moveLeft()
time.sleep(0.45)
moveFwd()
time.sleep(0.2)
#straightening
elif (sensorL5()==0):
moveRight()
time.sleep(0.05)
stop()
moveFwd()
time.sleep(0.15)
stop()
elif (sensorR5()==0):
moveLeft()
time.sleep(0.05)
stop()
moveFwd()
time.sleep(0.2)
stop()