working robot
[des2015.git] / mart / ev3 / ex1 / nl / ru / des / AvoidLowObjectBehaviour.java
1 package nl.ru.des;
2
3 import java.io.File;
4
5 import lejos.hardware.motor.EV3LargeRegulatedMotor;
6 import lejos.hardware.sensor.EV3TouchSensor;
7 import lejos.robotics.SampleProvider;
8 import lejos.utility.Delay;
9
10 public class AvoidLowObjectBehaviour extends AvoidBehaviour {
11 private SampleProvider rightSample, leftSample;
12 private float[] samples;
13 private long lastPush = 0;
14
15 public AvoidLowObjectBehaviour(EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor,
16 EV3TouchSensor leftTouch, EV3TouchSensor rightTouch) {
17 super(leftMotor, rightMotor, new File("bump.wav"));
18 rightSample = rightTouch.getTouchMode();
19 leftSample = leftTouch.getTouchMode();
20 samples = new float[rightTouch.sampleSize()];
21 }
22
23 @Override
24 public boolean takeControl() {
25 //Check if left sensor is pressed
26 leftSample.fetchSample(samples, 0);
27 boolean takeControl = false;
28 if(samples[0] == 1){
29 super.setAvoidDirection(false);
30 takeControl = true;
31 lastPush = System.currentTimeMillis();
32 }
33
34 //Check if right sensor is pressed
35 rightSample.fetchSample(samples, 0);
36 if(samples[0] == 1){
37 super.setAvoidDirection(true);
38 takeControl = true;
39 lastPush = System.currentTimeMillis();
40 }
41 return takeControl || System.currentTimeMillis()-lastPush < 1000;
42 }
43
44 @Override
45 public void action() {
46 rightMotor.backward();
47 leftMotor.backward();
48 Delay.msDelay(200);
49 leftMotor.forward();
50 rightMotor.forward();
51 super.action();
52 }
53 }