5 import lejos
.hardware
.Button
;
6 import lejos
.hardware
.motor
.EV3LargeRegulatedMotor
;
7 import lejos
.hardware
.sensor
.NXTLightSensor
;
8 import lejos
.robotics
.SampleProvider
;
10 public class StayInFieldBehaviour
extends AvoidBehaviour
{
11 private SampleProvider light
;
12 private float[] samples
;
13 private float black
, white
, dist
;
15 public StayInFieldBehaviour(NXTLightSensor lightSensor
, EV3LargeRegulatedMotor leftMotor
, EV3LargeRegulatedMotor rightMotor
) {
16 super(rightMotor
, leftMotor
, new File("bound.wav"));
17 light
= lightSensor
.getRedMode();
18 samples
= new float[light
.sampleSize()];
20 dist
= Math
.abs(black
-white
);
23 private void calibrate(){
24 LCDPrinter
.print("Calibrate Light...");
26 LCDPrinter
.print("Place on black");
27 Button
.waitForAnyPress();
28 light
.fetchSample(samples
, 0);
30 LCDPrinter
.print("Black: " + black
);
33 LCDPrinter
.print("Place on white");
34 Button
.waitForAnyPress();
35 light
.fetchSample(samples
, 0);
37 LCDPrinter
.print("White: " + white
);
40 dist
= Math
.abs(black
-white
);
44 public boolean takeControl() {
45 this.light
.fetchSample(samples
, 0);
46 return samples
[0] < Math
.abs(white
- dist
/2);