7a30ba1b8a5dc5f05f39ea800bb32866c1d18aca
[des2015.git] / mart / ev3 / ex1 / nl / ru / des / AvoidHighObjectBehaviour.java
1 package nl.ru.des;
2
3 import java.io.File;
4
5 import lejos.hardware.Button;
6 import lejos.hardware.motor.EV3LargeRegulatedMotor;
7 import lejos.hardware.sensor.EV3UltrasonicSensor;
8 import lejos.robotics.SampleProvider;
9
10 public class AvoidHighObjectBehaviour extends AvoidBehaviour {
11 private SampleProvider ultrasoneSample;
12 private float[] samples;
13 private float limit;
14
15 public AvoidHighObjectBehaviour(EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor,
16 EV3UltrasonicSensor ultraSone) {
17 super(leftMotor, rightMotor, new File("detect.wav"));
18 ultrasoneSample = ultraSone.getDistanceMode();
19 samples = new float[ultrasoneSample.sampleSize()];
20 calibrate();
21 }
22
23 private void calibrate(){
24 LCDPrinter.print("Calibrate ultrasone...");
25 LCDPrinter.print("Place object in");
26 LCDPrinter.print(" turn radius");
27 Button.waitForAnyPress();
28 ultrasoneSample.fetchSample(samples, 0);
29 limit = samples[0];
30 LCDPrinter.print("Limit: " + limit);
31 }
32
33 @Override
34 public boolean takeControl() {
35 ultrasoneSample.fetchSample(samples, 0);
36 return samples[0] < limit;
37 }
38 }