Color recognition test.

This commit is contained in:
d3rped 2018-03-22 04:36:37 +01:00
parent 7bd9ce7582
commit df9c636b6e
2 changed files with 24 additions and 86 deletions

View file

@ -3,7 +3,6 @@
import ev3dev.ev3 as ev3 import ev3dev.ev3 as ev3
from wheel import Wheel from wheel import Wheel
from sensor import Sensor from sensor import Sensor
import time
class Move: class Move:
@ -11,7 +10,7 @@ class Move:
try: try:
self._wheel_l = Wheel('outB') self._wheel_l = Wheel('outB')
self._wheel_r = Wheel('outC') self._wheel_r = Wheel('outC')
self._sensor = Sensor() self._camera = Sensor()
self._bumper = ev3.TouchSensor() self._bumper = ev3.TouchSensor()
except: except:
print("ERROR: Cannot find Motor/Sensor") print("ERROR: Cannot find Motor/Sensor")
@ -29,52 +28,8 @@ class Move:
# Probably redundant because aligning can be done while scanning other paths # Probably redundant because aligning can be done while scanning other paths
def _aligntoedge(self): def _aligntoedge(self):
'''
isonedge = False
alternatingvalue = 2 # value used to switch turning directions using mod 2 hence theres only two possible values when you keep increasing the value
if(self._sensor.getbrightness() < 24):
while (not (self._sensor.getbrightness() == 24)):
self._wheel_l.speed_set(5)
self._wheel_r.speed_set(-5)
elif(self._sensor.getbrightness() > 24):
while (not isonedge):
if alternatingvalue % 2 == 0:
isonedge = self._findedgeonrightside(alternatingvalue)
self._wheel_l.stop()
self._wheel_r.stop()
if alternatingvalue % 2 == 1:
isonedge = self._findedgeonleftside(alternatingvalue)
self._wheel_l.stop()
self._wheel_r.stop()
alternatingvalue = alternatingvalue + 1
'''
pass pass
'''
def _findedgeonleftside(self, alternatingvalue):
i = 0
while i < alternatingvalue:
self._wheel_r.speed_set(5)
self._wheel_l.speed_set(-5)
if (self._sensor.getbrightness() == 24):
return True
i = i + 1
return False
def _findedgeonrightside(self, alternatingvalue):
i = 0
while i < alternatingvalue:
self._wheel_l.speed_set(5)
self._wheel_r.speed_set(-5)
if (self._sensor.getbrightness() == 24):
time.sleep(0.05)
while(not (self._sensor.getbrightness() == 24)):
self._wheel_l.speed_set(5)
self._wheel_r.speed_set(-5)
return True
i = i + 1
return False
'''
''' '''
uses odometry and the planet object to map edges that are connected to uses odometry and the planet object to map edges that are connected to
@ -92,15 +47,14 @@ class Move:
self._wheel_r.speed_set(24) self._wheel_r.speed_set(24)
self._wheel_l.run() self._wheel_l.run()
self._wheel_r.run() self._wheel_r.run()
while (not (self._bumper.value() or self._sensor.isRed() or self._sensor.isBlue())): while (self._bumper.value() == False and self._camera.lastcolor == None ):
self._wheel_l.speed_set(48 - self._sensor.getbrightness()) self._camera.setmode('COL-REFLECT')
self._wheel_r.speed_set(self._sensor.getbrightness()) self._wheel_l.speed_set(48 - self._camera.getbrightness())
self._wheel_r.speed_set(self._camera.getbrightness())
self._camera.setmode('RGB-RAW')
self._camera.getcolor()
if(isknownstation == False): if(isknownstation == False):
pass #run odometry stuff here pass # run odometry stuff here
if (not self._bumper.value()):
time.sleep(1)
# self._aligntoedge()
self._wheel_l.stop() self._wheel_l.stop()
self._wheel_r.stop() self._wheel_r.stop()

View file

@ -3,21 +3,6 @@
import ev3dev.ev3 as ev3 import ev3dev.ev3 as ev3
from enum import Enum, unique from enum import Enum, unique
@unique
class Color(Enum):
RED = (80, 95, 20, 40, 15, 30)
BLUE = (10, 25, 60, 80, 70, 90)
def __init__(self, rmin, rmax, gmin, gmax, bmin, bmax):
self.rmin = rmin
self.rmax = rmax
self.gmin = gmin
self.gmax = gmax
self.bmin = bmin
self.bmax = bmax
''' '''
add enum for color definitions (type should be tupel of int's). add enum for color definitions (type should be tupel of int's).
later onwards compare the idividual readouts like this to determine the color: later onwards compare the idividual readouts like this to determine the color:
@ -30,31 +15,30 @@ color = (100, 30, 20)
''' '''
class Color(tuple, Enum):
RED = (90, 40, 25)
BLUE = (25, 75, 90)
class Sensor: class Sensor:
def __init__(self): def __init__(self):
self._sensor = ev3.ColorSensor() self._sensor = ev3.ColorSensor()
self._sensor.mode = 'COL-REFLECT' self._sensor.mode = 'COL-REFLECT'
self.foundColor = False self.lastcolor = None
def getbrightness(self): def getbrightness(self):
self._sensor.mode = 'COL-REFLECT' if(self._sensor.mode == 'COL-REFLECT'):
return self._sensor.value() return self._sensor.value()
def isRed(self):
self._sensor.mode = 'RGB-RAW'
r, g, b = self._sensor.bin_data("hhh")
if(Color.RED.rmin <= r <= Color.RED.rmax and Color.RED.gmin <= g <= Color.RED.gmax and Color.RED.bmin <= b <= Color.RED.bmax):
return True
else: else:
return False print("ERROR: Wrong Sensor Mode.")
def isBlue(self): def getcolor(self):
self._sensor.mode = 'RGB-RAW' if(self._sensor.mode == 'RGB-RAW'):
r, g, b = self._sensor.bin_data("hhh") RGB = self._sensor.bin_data("hhh")
if(Color.BLUE.rmin <= r <= Color.BLUE.rmax and Color.BLUE.gmin <= g <= Color.BLUE.gmax and Color.BLUE.bmin <= b <= Color.BLUE.bmax): if(RGB[0] > Color.RED[0] and (RGB[1], RGB[2]) < (Color.RED[1], Color.RED[2])):
return True self.lastcolor = Color.RED
else: if(RGB[0] < Color.BLUE[0] and (RGB[1], RGB[2]) > (Color.BLUE[1], Color.BLUE[2])):
return False self.lastcolor = Color.BLUE
def setmode(self, newmode): def setmode(self, newmode):
self._sensor.mode = newmode self._sensor.mode = newmode