-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorMeasurement.java
More file actions
64 lines (55 loc) · 1.9 KB
/
ColorMeasurement.java
File metadata and controls
64 lines (55 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import lejos.hardware.Brick;
import lejos.hardware.BrickFinder;
import lejos.hardware.Button;
import lejos.hardware.lcd.GraphicsLCD;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.motor.EV3MediumRegulatedMotor;
import lejos.hardware.sensor.EV3ColorSensor;
import lejos.hardware.sensor.EV3UltrasonicSensor;
import lejos.robotics.SampleProvider;
import lejos.robotics.filter.MeanFilter;
import lejos.utility.Delay;
public class ColorMeasurement {
private EV3LargeRegulatedMotor motorB;
private EV3LargeRegulatedMotor motorC;
private EV3ColorSensor colorSensor;
private GraphicsLCD display;
private EV3MediumRegulatedMotor motorClaw;
public ColorMeasurement(Brick ev3) {
colorSensor = new EV3ColorSensor(ev3.getPort("S3"));
//initiate the display
display = ev3.getGraphicsLCD();
}
// measuring the color of the objects.
// 8 different colors, represented by 0,1,...,7
// in order: (red, green, ??, yellow)
public boolean ColorID(int colorID) {
SampleProvider ColorIDSensor = colorSensor.getColorIDMode();
SampleProvider ColorAvg = new MeanFilter(ColorIDSensor, 5);
int sampleSize = ColorAvg.sampleSize();
float[] sample = new float[sampleSize];
ColorAvg.fetchSample(sample, 0);
int ID = (int)sample[0];
/*
String sampleString = "value " + ": " + ID;
display.drawString(sampleString, 0, 0, GraphicsLCD.VCENTER | GraphicsLCD.LEFT);
display.drawString("Escape to exit!", 0, 40, GraphicsLCD.VCENTER | GraphicsLCD.LEFT);
Delay.msDelay(1000);
display.clear();
*/
return colorID == ID;
}
// all opened ports need to be closed!
public void ClosePorts() {
colorSensor.close();
}
/*
public static void main(String[] args) {
ColorMeasurement ds = new ColorMeasurement(BrickFinder.getDefault());
while(!Button.ESCAPE.isDown())
{
ds.ColorID(3);
}
ds.ClosePorts();
*/
}