robolab/src/sensor.py

61 lines
1.6 KiB
Python

#!/usr/bin/env python3
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:
RED = (>90, <40, <25)
BLUE = (<25, >75, >90)
Example:
color = (100, 30, 20)
(color[0] > 90 and (color[1], color[2]) < (40, 25))
'''
class Sensor:
def __init__(self):
self._sensor = ev3.ColorSensor()
self._sensor.mode = 'COL-REFLECT'
self.foundColor = False
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
else:
return False
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 setmode(self, newmode):
self._sensor.mode = newmode