|
EV3 Color Sensor (Autonomous Mode)
Purpose: The EV3 color sensor has a built-in LED light source and detects the color of the reflected light by returning the three luminosity levels in range 0..1 for the red, green and blue components. In the ColorSensor class these RGB-values are then transformed into a Java Color reference, but it is important to know that the color value may vary considerably depending on the ambient illumination and the distance and direction to the reflection object. In this demonstration only 6 different colors of a color strip are discriminated, namely black, blue, green, yellow red and white (if we consider black and white as colors).
Download the color strip and print it on a white paper.
To distinguish the colors the class ColorSensor uses 6 "color cubes" in the three-dimensional RGB color space. The cuboids are represented by an integer array with the red, green and blue ranges [red_min, red_max, green_min, green_max, blue_min, blue_max] adapted to the color strip printed on a white paper with a standard color printer.
int[] blackCube = new int[] {0, 10, 0, 10, 0, 10};
int[] blueCube = new int[] {5, 15, 10, 25, 15, 45};
int[] greenCube = new int[] {8, 24, 25, 65, 3, 15};
int[] yellowCube = new int[] {50, 90, 35, 90, 3, 20};
int[] redCube = new int[] {40, 60, 5, 15, 3, 12};
int[] whiteCube = new int[] {40, 120, 40, 120, 40, 120};
|
|
|
Predefined color cuboids in the RGB color space |
There are different methods in class ColorSensor to return the detected color. c = getColor() returns a Java Color class reference and c.getRed(), c getGreen(), c.getBlue() provides the individual RGB components in the range 0..255. To check whether the color is inside a color cube, the static method inColorCube() may be used. getColorID() delivers a number 1..6 if the color lays in one of the predefined color cubes and 0 if the color is not part of any cube. getColorLabel() returns a ColorLabel enum with values UNDEFINED,
BLACK, BLUE,
GREEN,
YELLOW, RED,
WHITE.
The program uses a EV3 color sensor at port S1 and displays the current color and a beep of different frequency for each color.
import ch.aplu.ev3.*;
import lejos.hardware.lcd.LCD;
public class ColorSensorDemo
{
public ColorSensorDemo()
{
LegoRobot robot = new LegoRobot();
ColorSensor cs = new ColorSensor(SensorPort.S1);
robot.addPart(cs);
ColorSensor.ColorLabel oldColor =
ColorSensor.ColorLabel.UNDEFINED;
while (!Tools.isEscapeHit())
{
ColorSensor.ColorLabel color = cs.getColorLabel();
if (color != oldColor)
{
oldColor = color;
LCD.clear();
switch (color)
{
case BLACK:
robot.playTone(523, 100);
LCD.drawString("Black", 0, 2);
break;
case BLUE:
robot.playTone(784, 100);
LCD.drawString("Blue", 0, 2);
break;
case GREEN:
robot.playTone(1046, 100);
LCD.drawString("Green", 0, 2);
break;
case YELLOW:
robot.playTone(1568, 100);
LCD.drawString("Yellow", 0, 2);
break;
case RED:
robot.playTone(2093, 100);
LCD.drawString("Red", 0, 2);
break;
case WHITE:
robot.playTone(4186, 100);
LCD.drawString("White", 0, 2);
break;
case UNDEFINED:
LCD.drawString("Undefined", 0, 2);
break;
}
LCD.refresh();
}
}
robot.exit();
}
public static void main(String[] args)
{
new ColorSensorDemo();
}
}
Compilation/download using the OnlineEditor of PHBern (Bern University of Teacher Education)
It is interesting to port the same program to the direct mode to test the system's response time. In normal circumstances using a Bluetooth PAN link, the delay when the sensor is moved to a different color is in the order of 100 ms.This confirms that the direct mode is applicable for many robotics applications.
import ch.aplu.ev3.*;
import ch.aplu.util.*;
public class ColorSensorDemo extends Console
{
public ColorSensorDemo()
{
LegoRobot robot = new LegoRobot("10.0.1.1");
ColorSensor cs = new ColorSensor(SensorPort.S1);
robot.addPart(cs);
ColorSensor.ColorLabel oldColor
= ColorSensor.ColorLabel.UNDEFINED;
while (true)
{
ColorSensor.ColorLabel color = cs.getColorLabel();
if (color != oldColor)
{
oldColor = color;
switch (color)
{
case BLACK:
robot.playTone(523, 100);
System.out.println("Black");
break;
case BLUE:
robot.playTone(784, 100);
System.out.println("Blue");
break;
case GREEN:
robot.playTone(1046, 100);
System.out.println("Green");
break;
case YELLOW:
robot.playTone(1568, 100);
System.out.println("Yellow");
break;
case RED:
robot.playTone(2093, 100);
System.out.println("Red");
break;
case WHITE:
robot.playTone(4186, 100);
System.out.println("White");
break;
case UNDEFINED:
System.out.println("Undefined");
break;
}
}
}
}
public static void main(String[] args)
{
new ColorSensorDemo();
}
}
Execute the program locally using WebStart.
| |