-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindTheBlob.py
More file actions
executable file
·112 lines (87 loc) · 2.78 KB
/
findTheBlob.py
File metadata and controls
executable file
·112 lines (87 loc) · 2.78 KB
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
from Myro import *
from Graphics import *
from random import *
width = 500
height = 500
sim = Simulation("Maze World", width, height, Color("gray"))
#outside walls
sim.addWall((10, 10), (490, 20), Color("black"))
sim.addWall((10, 10), (20, 490), Color("black"))
sim.addWall((480, 10), (490, 490), Color("black"))
sim.addWall((10, 480), (490, 490), Color("black"))
#blue spot
poly = Circle((50, 50), 45)
poly.bodyType = "static"
poly.color = Color("blue")
poly.outline = Color("black")
sim.addShape(poly)
#red spot
poly = Circle((450, 50), 45)
poly.bodyType = "static"
poly.color = Color("red")
poly.outline = Color("black")
sim.addShape(poly)
#green spot
poly = Circle((50, 450), 45)
poly.bodyType = "static"
poly.color = Color("green")
poly.outline = Color("black")
sim.addShape(poly)
#yellow spot
poly = Circle((450, 450), 45)
poly.bodyType = "static"
poly.color = Color("yellow")
poly.outline = Color("black")
sim.addShape(poly)
#begin simulation and sets robot's position
makeRobot("SimScribbler", sim)
sim.setPose(0, width/2, height/2, 0)
sim.setup()
# 1-RED
# 2-GREEN
# 3-BLUE
# 4-YELLOW
#The following is a helper function
#Inputs: A picture and a color represented by the list above
#Returns the average x location of the color in the picture or -1 if the robot has found the color spot
def findColorSpot(picture, color):
xPixelSum = 0
totalPixelNum = 0
averageXPixel = 0
show(picture)
for pixel in getPixels(picture):
if(color == 1 and getRed(pixel) > 200 and getGreen(pixel) == 0 and getBlue(pixel) == 0):
xPixelSum += getX(pixel)
totalPixelNum += 1
elif(color == 2 and getRed(pixel)== 0 and getGreen(pixel) > 90 and getBlue(pixel) == 0):
xPixelSum += getX(pixel)
totalPixelNum += 1
elif(color == 3 and getRed(pixel) == 0 and getGreen(pixel) == 0 and getBlue(pixel) > 200):
xPixelSum += getX(pixel)
totalPixelNum += 1
elif(color == 4 and getRed(pixel) > 180 and getGreen(pixel) > 180 and getBlue(pixel) == 0):
xPixelSum += getX(pixel)
totalPixelNum += 1
if(totalPixelNum != 0):
averageXPixel = xPixelSum/totalPixelNum
#Handles the case where robot has found the spot if it is near it
#If necessary adjust the value
if(totalPixelNum/(getWidth(picture)*getHeight(picture)) > 0.21):
averageXPixel = -1
return averageXPixel
# Use the following integers for colors:
# 1-RED
# 2-GREEN
# 3-BLUE
# 4-YELLOW
######################Code Starts Here##################################
#Surendra Persaud
greenValue = 0
while greenValue != -1:
num = randrange(-180, 180)
turnBy(num, "deg")
pic = takePicture()
greenValue = findColorSpot(pic, 2)
if greenValue > 1:
forward(1, 3)
stop()