Skip to content

Commit 28b97f4

Browse files
authored
ColorSensor: Move rgb() from helper.py to core.py (#371)
1 parent 390da18 commit 28b97f4

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

ev3dev/core.py

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,16 +2277,11 @@ class ColorSensor(Sensor):
22772277
LEGO EV3 color sensor.
22782278
"""
22792279

2280-
__slots__ = ['auto_mode']
2280+
__slots__ = ['auto_mode', 'red_max', 'green_max', 'blue_max']
22812281

22822282
SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME
22832283
SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION
22842284

2285-
def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, name_exact=False, **kwargs):
2286-
super(ColorSensor, self).__init__(address, name_pattern, name_exact, driver_name=['lego-ev3-color'], **kwargs)
2287-
self.auto_mode = True
2288-
2289-
22902285
#: Reflected light. Red LED on.
22912286
MODE_COL_REFLECT = 'COL-REFLECT'
22922287

@@ -2326,13 +2321,12 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
23262321
#: Brown color.
23272322
COLOR_BROWN = 7
23282323

2329-
23302324
MODES = (
2331-
'COL-REFLECT',
2332-
'COL-AMBIENT',
2333-
'COL-COLOR',
2334-
'REF-RAW',
2335-
'RGB-RAW',
2325+
MODE_COL_REFLECT,
2326+
MODE_COL_AMBIENT,
2327+
MODE_COL_COLOR,
2328+
MODE_REF_RAW,
2329+
MODE_RGB_RAW
23362330
)
23372331

23382332
COLORS = (
@@ -2346,6 +2340,14 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
23462340
'Brown',
23472341
)
23482342

2343+
def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, name_exact=False, **kwargs):
2344+
super(ColorSensor, self).__init__(address, name_pattern, name_exact, driver_name=['lego-ev3-color'], **kwargs)
2345+
self.auto_mode = True
2346+
2347+
# See calibrate_white() for more details
2348+
self.red_max = 300
2349+
self.green_max = 300
2350+
self.blue_max = 300
23492351

23502352
@property
23512353
def reflected_light_intensity(self):
@@ -2388,17 +2390,61 @@ def color(self):
23882390

23892391
return self.value(0)
23902392

2393+
@property
2394+
def color_name(self):
2395+
"""
2396+
Returns NoColor, Black, Blue, etc
2397+
"""
2398+
return self.COLORS[self.color]
2399+
23912400
@property
23922401
def raw(self):
23932402
"""
2394-
Red, green, and blue components of the detected color, in the range 0-1020.
2403+
Red, green, and blue components of the detected color, officially in the
2404+
range 0-1020 but the values returned will never be that high. We do not
2405+
yet know why the values returned are low, but pointing the color sensor
2406+
at a well lit sheet of white paper will return values in the 250-400 range.
2407+
2408+
If this is an issue, check out the rgb() and calibrate_white() methods.
23952409
"""
23962410

23972411
if self.auto_mode:
23982412
self.mode = self.MODE_RGB_RAW
23992413

24002414
return self.value(0), self.value(1), self.value(2)
24012415

2416+
def calibrate_white(self):
2417+
"""
2418+
The RGB raw values are on a scale of 0-1020 but you never see a value
2419+
anywhere close to 1020. This function is designed to be called when
2420+
the sensor is placed over a white object in order to figure out what
2421+
are the maximum RGB values the robot can expect to see. We will use
2422+
these maximum values to scale future raw values to a 0-255 range in
2423+
rgb().
2424+
2425+
If you never call this function red_max, green_max, and blue_max will
2426+
use a default value of 300. This default was selected by measuring
2427+
the RGB values of a white sheet of paper in a well lit room.
2428+
2429+
Note that there are several variables that influence the maximum RGB
2430+
values detected by the color sensor
2431+
- the distance of the color sensor to the white object
2432+
- the amount of light in the room
2433+
- shadows that the robot casts on the sensor
2434+
"""
2435+
(self.red_max, self.green_max, self.blue_max) = self.raw
2436+
2437+
@property
2438+
def rgb(self):
2439+
"""
2440+
Same as raw() but RGB values are scaled to 0-255
2441+
"""
2442+
(red, green, blue) = self.raw
2443+
2444+
return (min(int((red * 255) / self.red_max), 255),
2445+
min(int((green * 255) / self.green_max), 255),
2446+
min(int((blue * 255) / self.blue_max), 255))
2447+
24022448
@property
24032449
def red(self):
24042450
"""

ev3dev/helper.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,6 @@ class MediumMotor(ev3dev.auto.MediumMotor, MotorMixin):
142142
pass
143143

144144

145-
class ColorSensorMixin(object):
146-
147-
def rgb(self):
148-
"""
149-
Note that the mode for the ColorSensor must be set to MODE_RGB_RAW
150-
"""
151-
# These values are on a scale of 0-1020, convert them to a normal 0-255 scale
152-
red = int((self.value(0) * 255) / 1020)
153-
green = int((self.value(1) * 255) / 1020)
154-
blue = int((self.value(2) * 255) / 1020)
155-
156-
return (red, green, blue)
157-
158-
159-
class ColorSensor(ev3dev.auto.ColorSensor, ColorSensorMixin):
160-
pass
161-
162-
163145
# ============
164146
# Tank classes
165147
# ============

0 commit comments

Comments
 (0)