-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriveToColor.py
61 lines (50 loc) · 1.95 KB
/
driveToColor.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
from Behavior import Behavior
from camera import Camera
from motor_rec import Motor_Rec
from imager2 import Imager
class driveToColor(Behavior):
def __init__(self, sensobs, static_pri):
super().__init__(sensobs, True, static_pri)
def _determine_angle(self, img): # Returns an angle. negative is left, right is positive
h = img.ymax
half = h//2
w = img.xmax
img = img.crop((0,half,w,half+1)) # Img is now a one-pixel tall image, same width
readings = []
big_first = 0
big_last = w-1
big_counter = 0
first, last, counter = 0, 0, 0
for cur in range(w):
rgb = img.get_pixel(cur,0)
if rgb[0] >= 120 and rgb[1] >= 120 and rgb[2] >= 80:
if cur - last <= 2: # If the next pixel is within 2 pixels of the last we visited
last = cur # Flytter last frem, hvis det stemmer
counter += 1
if counter > big_counter: # If the current streak is bigger
big_first = first
big_last = last
big_counter = counter
else:
first = cur
last = cur
counter = 0
center = w / 2
center_black = (big_first+big_last)//2 if big_counter >= 10 else center
if center_black > center:
return (round(((center_black-center)/center) * 26.75))
else: # center_blue < center
return (round(-((center-center_black)/center) * 26.75))
def _update_flag(self):
img = Imager(image=self.sensobs['camera'].get_value(), mode='RGB')
self.angle = self._determine_angle(img)
if abs(self.angle) > 3:
self.active_flag = True
else:
self.active_flag = False
def _sense_and_act(self):
if self.active_flag:
self.match_degree = 100
direction = 'l' if self.angle > 0 else 'r'
self.motor_recommendation.action = [direction, abs(self.angle)]
self.motor_recommendation.description = 'Drive to color'