This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sensob.py
154 lines (125 loc) · 4.04 KB
/
Sensob.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
from abc import ABC, abstractmethod
import os
import imager2 as IMR
from camera import Camera
from reflectance_sensors import ReflectanceSensors
from ultrasonic import Ultrasonic
from zumo_button import ZumoButton
class Sensob(ABC):
@abstractmethod
def __init__(self):
pass
@abstractmethod
def update(self):
pass
@abstractmethod
def interpret(self):
pass
class FallingOut(Sensob):
def __init__(self):
super(FallingOut, self).__init__()
self.sensor = ReflectanceSensors(max_reading=3000, min_reading=1000)
self.value = []
def update(self):
self.sensor.update()
self.value = self.sensor.get_value()
def reset(self):
pass
def calibrate(self):
self.sensor = ReflectanceSensors(auto_calibrate=True)
def interpret(self):
danger = 0
if self.value[0] < 0.65:
danger = -1
elif self.value[5] < 0.65:
danger = 1
return danger
class ButtonFeeler(Sensob):
def __init__(self):
super(ButtonFeeler, self).__init__()
self.sensor = ZumoButton()
self.button_pressed = False
self.value = 0
def update(self):
if self.sensor.update():
self.value = 1
self.button_pressed = True
else:
self.value = 0
def reset(self):
self.button_pressed = False
def interpret(self):
return self.value == 1
class DuckFinder(Sensob):
def __init__(self):
super(DuckFinder, self).__init__()
self.width = 48
self.height = 36
self.sensor = Camera(img_width=self.width, img_height=self.height)
# os.system('rm -f image.png')
self.value = None
self.K = 12
self.imager = IMR.Imager(width=self.width, height=self.height)
self.interpretation = 2.0
def reset(self):
self.value = None
# os.system('rm -f image.png')
self.sensor.reset()
def update(self):
self.value = self.sensor.update() # Image object
self.interpret()
def get_interpretation(self): return self.interpretation
def interpret(self):
self.imager.set_image(self.value)
image = self.imager
rgb_table = [[image.get_pixel(x, y) for y in range(0, self.height)] for x in range(0, self.width)]
lines = [0 for x in range(0, 16)]
index = 0
for i in range(0, len(lines)):
for j in range(0, int(self.width/len(lines))):
for k in range(0, self.height):
if rgb_table[index][k][0] > 150 and \
rgb_table[index][k][1] > 130 and \
rgb_table[index][k][2] < 60:
lines[i] += 1
index += 1
if sum(lines) < self.K: # If there are not enough duck-pixels
return 2.0
maximum = (-1, 0)
position = 0.0 # Value between -1.0 and 1.0 in steps of .25
for i in range(0, len(lines)):
if lines[i] > maximum[1]:
maximum = (i, lines[i])
if maximum[0] < 8:
position = (8 - maximum[0]) * -0.125
else:
position = (maximum[0] - 7) * 0.125
self.interpretation = position
return position
class Peeper(Sensob):
def __init__(self):
super(Peeper, self).__init__()
self.sensor = Ultrasonic()
self.value = -1.0
self.interpretation = 1.0
def update(self):
self.sensor.update()
self.value = self.sensor.get_value()
self.interpret()
def reset(self):
pass
def interpret(self):
if self.value > 90:
self.interpretation = 1.0
else:
self.interpretation = self.value/90
return self.interpretation
def get_interpretation(self): return self.interpretation
if __name__ == '__main__':
mmmducks = DuckFinder()
button = ZumoButton()
while True:
mmmducks.update()
print(mmmducks.interpret())
button.wait_for_press()
mmmducks.reset()