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
from wheel import Wheel
from sensor import Sensor
import time
class Move:
@ -11,7 +10,7 @@ class Move:
try:
self._wheel_l = Wheel('outB')
self._wheel_r = Wheel('outC')
self._sensor = Sensor()
self._camera = Sensor()
self._bumper = ev3.TouchSensor()
except:
print("ERROR: Cannot find Motor/Sensor")
@ -29,52 +28,8 @@ class Move:
# Probably redundant because aligning can be done while scanning other paths
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
'''
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
@ -92,15 +47,14 @@ class Move:
self._wheel_r.speed_set(24)
self._wheel_l.run()
self._wheel_r.run()
while (not (self._bumper.value() or self._sensor.isRed() or self._sensor.isBlue())):
self._wheel_l.speed_set(48 - self._sensor.getbrightness())
self._wheel_r.speed_set(self._sensor.getbrightness())
while (self._bumper.value() == False and self._camera.lastcolor == None ):
self._camera.setmode('COL-REFLECT')
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):
pass #run odometry stuff here
if (not self._bumper.value()):
time.sleep(1)
# self._aligntoedge()
pass # run odometry stuff here
self._wheel_l.stop()
self._wheel_r.stop()

View file

@ -3,21 +3,6 @@
import ev3dev.ev3 as ev3
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).
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:
def __init__(self):
self._sensor = ev3.ColorSensor()
self._sensor.mode = 'COL-REFLECT'
self.foundColor = False
self.lastcolor = None
def getbrightness(self):
self._sensor.mode = 'COL-REFLECT'
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
if(self._sensor.mode == 'COL-REFLECT'):
return self._sensor.value()
else:
return False
print("ERROR: Wrong Sensor Mode.")
def isBlue(self):
self._sensor.mode = 'RGB-RAW'
r, g, b = 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):
return True
else:
return False
def getcolor(self):
if(self._sensor.mode == 'RGB-RAW'):
RGB = self._sensor.bin_data("hhh")
if(RGB[0] > Color.RED[0] and (RGB[1], RGB[2]) < (Color.RED[1], Color.RED[2])):
self.lastcolor = Color.RED
if(RGB[0] < Color.BLUE[0] and (RGB[1], RGB[2]) > (Color.BLUE[1], Color.BLUE[2])):
self.lastcolor = Color.BLUE
def setmode(self, newmode):
self._sensor.mode = newmode