robolab/src/sensor.py

60 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import ev3dev.ev3 as ev3
from enum import Enum
2018-03-20 15:11:19 +01:00
'''
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))
'''
'''
Calculate brightness/to greyscale from RGB:
http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=5445596
https://en.wikipedia.org/wiki/YUV
Formula:
Y=(0.299 x R) + (0.587 x G) + (0.114 x B);
'''
2018-03-22 04:36:37 +01:00
class Color(tuple, Enum):
RED = (90, 40, 25)
BLUE = (25, 75, 90)
class Sensor:
def __init__(self):
self._bumper = ev3.TouchSensor()
self._camera = ev3.ColorSensor()
self._camera.mode = 'RGB-RAW'
self.RGB = self._camera.bin_data("hhh")
self._brightness = self._camera.value()
2018-03-22 04:36:37 +01:00
self.lastcolor = None
def bumperispressed(self):
self._bumper.value()
def refresh(self): # do we still want to check if the propper camera/sensor mode is set?
self.RGB = self._camera.bin_data("hhh")
self._brightness = self._camera.value()
2018-03-20 15:11:19 +01:00
def getbrightness(self):
return self._brightness
2018-03-22 04:36:37 +01:00
def getcolor(self):
if(self._camera.mode == 'RGB-RAW'):
if(self.RGB[0] > Color.RED[0] and (self.RGB[1], self.RGB[2]) < (Color.RED[1], Color.RED[2])):
2018-03-22 04:36:37 +01:00
self.lastcolor = Color.RED
if(self.RGB[0] < Color.BLUE[0] and (self.RGB[1], self.RGB[2]) > (Color.BLUE[1], Color.BLUE[2])):
2018-03-22 04:36:37 +01:00
self.lastcolor = Color.BLUE
def calibrateRGB(self):
2018-03-23 19:09:49 +01:00
# Enums are unchangeable right? Dunno how to calibrate like that
pass