You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Constructor for initializing a link to the `Light Color Sensor`_.
67
-
68
73
:param str port = "I2C": The port to which the distance sensor is connected to. Can also be connected to ports ``"AD1"`` or ``"AD2"`` of the `GoPiGo3`_. If you're passing an **invalid port**, then the sensor resorts to an ``"I2C"`` connection. Check the :ref:`hardware specs <hardware-interface-section>` for more information about the ports.
69
74
:param bool led_state = False: The LED state. If it's set to ``True``, then the LED will turn on, otherwise the LED will stay off. By default, the LED is turned off.
70
75
:param bool use_mutex = False: When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
71
76
:raises ~exceptions.OSError: When the `Light Color Sensor`_ is not reachable.
72
77
:raises ~exceptions.RuntimeError: When the chip ID is incorrect. This happens when we have a device pointing to the same address, but it's not a `Light Color Sensor`_.
# LJM: This is not required if we import <colorsys> module and use <colorsys.rgb_to_hsv> function
107
+
# for backwards compatibility with existing code, we could just make this function a wrapper for <colorsys.rgb_to_hsv>
102
108
deftranslate_to_hsv(self, in_color):
103
109
"""
104
110
Standard algorithm to switch from one color system (**RGB**) to another (**HSV**).
105
-
106
111
:param tuple(float,float,float) in_color: The RGB tuple list that gets translated to HSV system. The values of each element of the tuple is between **0** and **1**.
107
112
:return: The translated HSV tuple list. Returned values are *H(0-360)*, *S(0-100)*, *V(0-100)*.
108
113
:rtype: tuple(int, int, int)
109
-
110
114
.. important::
111
-
112
115
For finding out the differences between **RGB** *(Red, Green, Blue)* color scheme and **HSV** *(Hue, Saturation, Value)*
113
116
please check out `this link <https://www.kirupa.com/design/little_about_color_hsv_rgb.htm>`__.
Returns the color as read by the `Light Color Sensor`_.
150
-
151
151
The colors detected vary depending on the lighting conditions of the nearby environment.
152
-
153
152
:returns: The RGBA values from the sensor. RGBA = Red, Green, Blue, Alpha (or Clear). Range of each element is between **0** and **1**. **-1** means an error occured.
154
153
:rtype: tuple(float,float,float,float)
155
-
156
154
"""
157
155
ifMutexAcquire(self.use_mutex)
158
156
try:
@@ -168,7 +166,6 @@ def safe_raw_colors(self):
168
166
defsafe_rgb(self):
169
167
"""
170
168
Detect the RGB color off of the `Light Color Sensor`_.
171
-
172
169
:returns: The RGB color in 8-bit format.
173
170
:rtype: tuple(int,int,int)
174
171
"""
@@ -178,58 +175,56 @@ def safe_rgb(self):
178
175
else:
179
176
r,g,b,c=colors
180
177
returnr,g,b
181
-
178
+
179
+
# LJM: a new function that calibrates for a single color
180
+
# (suggestion: for feedback, take a subsequent reading, identify color and illuminate eyes to prove it has worked)
181
+
# Rather than devise rules for black and white as exceptions, which may not be applicable to all environments
182
+
# it might be better instead to adjust the robot's view of a typical white or black for the current environment
183
+
# You could also choose to run a calibration for all colors that you are interested in, using task & environment specific examples
184
+
defcalibrate(self, color):
185
+
"""
186
+
Replace the HSV centroid for a given color with the sensor reading obtained from an example of that color in the current lighting environment
187
+
<color> can be one of black | white | red | green | blue | yellow | cyan | fuschia
188
+
"""
189
+
190
+
ifcolorinself.known_hsv:
191
+
r, g, b, c=self.safe_raw_colors()
192
+
h, s, v=colorsys.rgb_to_hsv(r/c, g/c, b/c)
193
+
self.known_hsv[color] = [360*h, 100*s, 100*v]
194
+
else:
195
+
print (f"Invalid color name: [{color}].")
196
+
colorlist=', '.join(self.known_hsv.keys())
197
+
print (f"color can only be one of {colorlist}.")
198
+
182
199
defguess_color_hsv(self, in_color):
183
200
"""
184
201
Determines which color `in_color` parameter is closest to in the :py:attr:`~di_sensors.easy_light_color_sensor.EasyLightColorSensor.known_colors` list.
185
-
186
202
This method uses the euclidean algorithm for detecting the nearest center to it out of :py:attr:`~di_sensors.easy_light_color_sensor.EasyLightColorSensor.known_colors` list.
187
203
It does work exactly the same as KNN (K-Nearest-Neighbors) algorithm, where `K = 1`.
188
-
189
204
:param tuple(float,float,float,float) in_color: A 4-element tuple list for the *Red*, *Green*, *Blue* and *Alpha* channels. The elements are all valued between **0** and **1**.
190
205
:returns: The detected color in string format and then a 3-element tuple describing the color in RGB format. The values of the RGB tuple are between **0** and **1**.
191
206
:rtype: tuple(str,(float,float,float))
192
-
193
207
.. important::
194
-
195
208
For finding out the differences between **RGB** *(Red, Green, Blue)* color scheme and **HSV** *(Hue, Saturation, Value)*
196
209
please check out `this link <https://www.kirupa.com/design/little_about_color_hsv_rgb.htm>`__.
197
-
198
210
"""
199
211
200
212
r,g,b,c=in_color
201
213
# print("incoming: {} {} {} {}".format(r,g,b,c))
202
214
203
-
# handle black
204
-
# luminosity is too low, or all color readings are too low
205
-
ifc<0.04or (r/c<0.10andg/c<0.10andb/c<0.10):
206
-
return ("black",(0,0,0))
207
-
208
-
# handle white
209
-
# luminosity is high, or all color readings are high
210
-
ifc>0.95or (r/c>0.9andg/c>0.9andb/c>0.9):
211
-
return ("white",(255,255,255))
212
-
213
215
# divide by luminosity(clarity) to minimize variations
214
-
h,s,v=self.translate_to_hsv((r/c, g/c, b/c))
215
-
216
-
# another black is possible
217
-
# black has a value of 0 and a saturation of 100
218
-
# values of 15 and 95 chosen randomly. They may need to be tweaked
219
-
ifv<15ands>95:
220
-
return ("Black",(0,0,0))
221
-
222
-
# so is another white
223
-
# white has a value of 100 and a saturation of 0
224
-
# values of 95 and 10 chosen randomly. They may need to be tweaked
225
-
ifv>95ands<10:
226
-
return ("White",(255,255,255))
227
-
216
+
# LJM: I've replaced code for HSV conversion but perhaps not necessary if we convert existing function to colorsys.rgb_to_hsv wrapper
0 commit comments