ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
422 stars 146 forks source link

EV3-G API: color sensor #355

Closed dwalton76 closed 6 years ago

dwalton76 commented 6 years ago

color-sensor-01

dwalton76 commented 6 years ago

For the two calibrate methods per http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-stretch/sensor_data.html

[28] | This mode is not usable. When in COL-CAL mode, the color sensor does not respond to the keep-alive sent from the EV3 brick. As a result, the sensor will time out and reset.

so will skip those unless the quote above is no longer accurate.

I'm not sure what all we should add here...wait_for_color(...) might be handy. What methods would make line followers easier to code?

dwalton76 commented 6 years ago

Do you guys see the RGB values that come from the sensor as very very dark? I can point mine at the white side of a rubiks cube and it comes back with numbers like (34, 58, 46) on 0-255 scale. That is basically dark green: https://www.w3schools.com/colors/colors_converter.asp

I'm wondering if this thing is broken.

dwalton76 commented 6 years ago

Prior to this commit ColorSensor.rgb() took the raw RGB values and scaled them to 0-255 based on a 0-1020 scale (since that is the official range of the values returned by the sensor). The color sensor never returns anything close to 1020 though so the end result was that we got very dark RGB values out of rgb(). A typical MINDCUBER scan of a rubiks cube would look like this:

color-sensor-before

Now we have a calibrate_white() function the user can call (to be called when the color sensor is looking at something white) to figure out what are the maximum RGB values we can expect to see. ColorSensor.rgb() will then scale the results to 0-255 based on those max values. The result is that you get much brighter/accurate data:

color-sensor-after

Note that the two pics above are of different rubiks cubes but you get the idea, the colors in the second one are much more accurate than the first.

dlech commented 6 years ago

FYI, converting to HSV or some other colorspace would be even more accurate.

dwalton76 commented 6 years ago

RGB is very handy for printing in web pages for debugging color issues but sucks for computing color distances of one color vs. another. HSV I never had much luck with for color comparison. I found that Lab color space works best for comparing colors, you take the euclidean distance between the two colors and that works really well. This is what I use for all of the color resolving that has to happen in cranecuber.

I have a function that converts RGB to Lab without importing any additional modules. I'll add a lab() method to the ColorSensor.

dwalton76 commented 6 years ago

PS This is a good article on python and color sorting http://www.alanzucconi.com/2015/09/30/colour-sorting/