Acrob043

Z SensorWiki
Prejsť na: navigácia, hľadanie

Software for Color Sensors

1. Basic demonstration


2. Color Matching Demo software

Following Arduino code works with an original Parallax (or PhiPi) ColorDemo.exe program available at the following address: https://www.parallax.com/sites/default/files/downloads/28380-ColorPAL-Color-Matching-Program-BS-Sample-Files.zip



ColorMatchDemoExe.png

4. Calculations

TCS230

Because even the completely black surface reflex some light under white LED illumination, the light response from a completely black subject will be non-zero. For this reason, the black response for each color component has to be determined experimentally, so that it, too, can be subtracted from the overall response. The three components thus measured (with an ambient reference subtracted), Kr, Kg, and Kb, are known as the “black reference”. A black reference is typically obtained only once before each measurement session.

The TCS230 light sensor colored photodiodes does not respond equally to the red, green, and blue wavelengths. So, in addition to the ambient reference and black reference, it is also necessary to take a “white reference”, wherein the TCS230 is presented with a completely white surface. Again, after subtracting the ambient lighting, the red, green, and blue components of this reference will be called, Wr, Wg, and Wb. Now we have a way of determining a subject’s actual color as a percentage of the difference between the white and black references. This percentage can be expressed as a value between 0 (0%) and 255 (100%), as follows for red, say:

Cr = 255 . (Ur – Kr) / (Wr – Kr), 

where Ur is the uncorrected (except for ambient) reading, and Cr is the corrected reading.


ColorPAL

The sensor outputs a voltage proportional to all the light that it sees, weighted by the above curve. Therefore, when a subject is illuminated with a red LED only, it will respond with a voltage proportional to the red component of the subject’s color, and similarly with blue and green. When there is ambient light mixed in with the LED’s illumination, its effect can be eliminated by sampling first without any LEDs turned on, then subtracting this reading, in turn, from each of the red, green, and blue components. This reference measurement should be taken before each color measurement to eliminate any effects from varying ambient conditions. In the paragraphs that follow, it will be assumed that an ambient reference is taken and subtracted from each measurement discussed.

Because the LED and sensor sit next to each other on the ColorPAL’s circuit board, and because the plastic snorkel itself reflects some light back (primarily from its threads), the light response from a completely black subject will be non-zero. For this reason, the black response for each color component has to be determined experimentally, so that it, too, can be subtracted from the overall response. The three components thus measured (with an ambient reference subtracted), Kr, Kg, and Kb, are known as the “black reference”. A black reference is typically obtained only once before each measurement session. The TSL13T light sensor does not respond equally to the red, green, and blue LEDs, and those LEDs don’t put out equal amounts of light at the red, green, and blue wavelengths. So, in addition to the ambient reference and black reference, it is also necessary to take a “white reference”, wherein the ColorPAL is presented with a completely white surface. Again, after subtracting the ambient lighting, the red, green, and blue components of this reference will be called, Wr, Wg, and Wb. Now we have a way of determining a subject’s actual color as a percentage of the difference between the white and black references. This percentage can be expressed as a value between 0 (0%) and 255 (100%), as follows for red, say:

Cr = 255 . (Ur – Kr) / (Wr – Kr), 

where Ur is the uncorrected (except for ambient) reading, and Cr is the corrected reading.


Color Matching

Color matching is done using a "minimax" approach in the RGB color space. In this method, the distance between two colors is the absolute value of the difference between the RGB component that differs most:

Di = max{|R - Ri|, |G - Gi|, |B - Bi|}

where R, G and B are the red, green, and blue coordinates of the color being scanned; and Ri, Gi and Bi are the coordinates of stored color. The color with the closest match is taken to be the one whose is the smallest, i.e. minimizing the maximum difference. Hence the term "minimax".

3. Processing software

ColorProcessingDemo.png
// Demonstration program for Color Sensors
// TCS 230 sensor requires TCS230_Demo03_SS_CIM.bs2
// ColorPAL sensor requires ColorPAL_Demo03_SS_CIM.ino with line 78 uncommented
//
// SS_CIM 2013
// Richard Balogh
 
import processing.serial.*;
 
int lf = 10;    // Line Feed in ASCII
int cr = 13;    // Carriage Returin in ASCII
 
int val = 0;    // To store data from serial port, used to color background
int r,g,b,c;    // Used to color background
Serial port;    // The serial port object
 
void setup() {
  size(200,200);             // Size of canvas
 
 println(Serial.list());     // List of available ports
 
                             // Use the port (CHANGE THIS according YOUR PC!)
 port = new Serial(this, Serial.list()[3], 9600); 
}
 
void draw() {
        // The serial data is used to color the background.   
 background(r,g,b); 
}
 
 
 
 
// Called whenever there is something available to read
 
void serialEvent(Serial port) {
 
  // Data from the Serial port is read in serialEvent() 
  // using the readStringUntil() function with LF as 
  // the end character.
 
  String input = port.readStringUntil(lf); 
 
  if (input != null) {
    // Print message received
    println( "Receiving:" + input);
 
   // The data is split into an array of Strings with 
   // a comma as a delimiter and converted into an 
   // array of integers.
   int[] vals = int(splitTokens(input, ",")); 
 
    // Fill r,g,b variables
    r = vals[0];
    g = vals[1];
    b = vals[2];
    c = vals[3];
 
    /* ************************************* */
    /*                                       */
    /*    Here is place for Your code!!!     */
    /*                                       */
    /* ************************************* */
 
     println( "R:" + r);
     println( "G:" + g);
     println( "B:" + b);
     println( "C:" + c);
 
  }
}