From: Mart Lubbers Date: Thu, 5 Nov 2015 15:47:05 +0000 (+0100) Subject: final X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=1a5c525a6a1f33e6de5d43a09dcf0ccacd8674c3;p=des2015.git final --- diff --git a/mart/ev3/ex1/nl/ru/des/WavPlayer.java b/mart/ev3/ex1/nl/ru/des/WavPlayer.java deleted file mode 100644 index 2e0760f..0000000 --- a/mart/ev3/ex1/nl/ru/des/WavPlayer.java +++ /dev/null @@ -1,67 +0,0 @@ -package nl.ru.des; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; - -import lejos.hardware.Audio; -import lejos.utility.Delay; - -public class WavPlayer extends Thread{ - private static String current; - private Audio audio = null; - - public static void playWav(String c){ - current = c; - } - - public WavPlayer(Audio audio){ - this.audio = audio; - audio.setVolume(Audio.VOL_MAX); - } - - public void preLoad(String[] files){ - LCDPrinter.print("Preloading audiofiles..."); - for(String sound : files){ - try { - LCDPrinter.print("Preloading: " + sound); - InputStream inp = new BufferedInputStream(Class.class.getResourceAsStream("/nl/ru/des/sounds/" + sound)); - File f = new File(sound); - if(f.exists()){ - inp.close(); - LCDPrinter.print(sound + " already exists, skipping..."); - continue; - } - FileOutputStream outp = new FileOutputStream(sound); - byte[] buffer = new byte[1024]; - int length; - while ((length = inp.read(buffer)) > 0){ - outp.write(buffer, 0, length); - } - outp.close(); - LCDPrinter.print("Done..."); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - @Override - public void run(){ - File c; - while(true){ - if(current != null){ - c = new File(current); - if(c.canRead()){ - audio.playSample(new File(current)); - } else { - LCDPrinter.print("can't load: " + current); - } - current = null; - } - Delay.msDelay(00); - } - } -} \ No newline at end of file diff --git a/mart/ev3/ex2/nl/ru/des/MarsRover.java b/mart/ev3/ex2/nl/ru/des/MarsRover.java index 16b97c6..872ab93 100644 --- a/mart/ev3/ex2/nl/ru/des/MarsRover.java +++ b/mart/ev3/ex2/nl/ru/des/MarsRover.java @@ -12,7 +12,6 @@ import lejos.hardware.sensor.EV3TouchSensor; import lejos.hardware.sensor.EV3UltrasonicSensor; import lejos.robotics.SampleProvider; import lejos.robotics.filter.ConcatenationFilter; -import lejos.robotics.filter.MeanFilter; import lejos.robotics.subsumption.Arbitrator; import lejos.robotics.subsumption.Behavior; import nl.ru.des.behaviours.AvoidHighObjectBehaviour; @@ -33,19 +32,20 @@ public class MarsRover { LCDPrinter.startLCDPrinter(tlcd); System.setOut(LCDPrinter.getPrefixedPrintstream("out: ", tlcd)); System.setErr(LCDPrinter.getPrefixedPrintstream("err: ", tlcd)); - - ColorMemory mh = new ColorMemory(); - LCDPrinter.print("Loading keylistener..."); - brick.getKey("Escape").addKeyListener(new ButtonListener()); + final ColorMemory mh = new ColorMemory(); + + LCDPrinter.print("Place the robots in front of each other and press any key"); + Button.waitForAnyPress(); - BTController btController; LCDPrinter.print("Trying to pair"); - //btController = BTController.getBTMaster("Rover1", mh); // Master - btController = BTController.getBTSlave(mh); // Slave + BTController btController; + btController = BTController.getBTMaster("Rover1", mh); // Master + //btController = BTController.getBTSlave(mh); // Slave btController.start(); - LCDPrinter.print("Robots are connected"); + LCDPrinter.print("Loading keylistener..."); + brick.getKey("Escape").addKeyListener(new ButtonListener()); LCDPrinter.print("Loading motors..."); EV3LargeRegulatedMotor rightMotor = new EV3LargeRegulatedMotor(MotorPort.D); @@ -63,22 +63,18 @@ public class MarsRover { SampleProvider color = new EV3ColorSensor(brick.getPort("S2")).getColorIDMode(); LCDPrinter.print("Loading ultrasone sensor..."); - SampleProvider ultraSonic = new MeanFilter(new EV3UltrasonicSensor(brick.getPort("S3")).getDistanceMode(), 3); - + SampleProvider ultraSonic = new EV3UltrasonicSensor(brick.getPort("S3")).getDistanceMode(); + LCDPrinter.print("Initializing behaviours..."); Behavior[] behaviorList = new Behavior[] { new WandererBehaviour(color, leftMotor, rightMotor, mh), - new StayInFieldBehaviour(color, leftMotor, rightMotor), new AvoidLowObjectBehaviour(leftMotor, rightMotor, touch), new AvoidHighObjectBehaviour(leftMotor, rightMotor, ultraSonic), + new StayInFieldBehaviour(color, leftMotor, rightMotor), new ShutdownBehaviour(mh) }; - LCDPrinter.print("Initializing arbitrator..."); Arbitrator arbitrator = new Arbitrator(behaviorList); - LCDPrinter.print("Press any button for takeoff!"); - Button.waitForAnyPress(); arbitrator.start(); - LCDPrinter.print("Finish"); } } \ No newline at end of file diff --git a/mart/ev3/ex2/nl/ru/des/behaviours/AvoidHighObjectBehaviour.java b/mart/ev3/ex2/nl/ru/des/behaviours/AvoidHighObjectBehaviour.java index e9395f8..181ecb3 100644 --- a/mart/ev3/ex2/nl/ru/des/behaviours/AvoidHighObjectBehaviour.java +++ b/mart/ev3/ex2/nl/ru/des/behaviours/AvoidHighObjectBehaviour.java @@ -1,10 +1,11 @@ package nl.ru.des.behaviours; -import lejos.hardware.Button; +import java.util.Random; + import lejos.hardware.motor.EV3LargeRegulatedMotor; import lejos.robotics.SampleProvider; import lejos.robotics.subsumption.Behavior; -import nl.ru.des.LCDPrinter; +import lejos.utility.Delay; public class AvoidHighObjectBehaviour extends ReactiveBehaviour implements Behavior { private static final long TURNTIME = 250; @@ -12,16 +13,17 @@ public class AvoidHighObjectBehaviour extends ReactiveBehaviour implements Behav private EV3LargeRegulatedMotor leftMotor; private EV3LargeRegulatedMotor rightMotor; private float limit; - + private Random random; + public AvoidHighObjectBehaviour(EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor, SampleProvider ultraSone) { super(ultraSone); this.leftMotor = leftMotor; this.rightMotor = rightMotor; - LCDPrinter.print("Place object in turn radius and press a key"); - Button.waitForAnyPress(); + Delay.msDelay(500); fetchSample(); limit = samples[0]; + random = new Random(); } @Override @@ -33,13 +35,24 @@ public class AvoidHighObjectBehaviour extends ReactiveBehaviour implements Behav @Override public void action() { super.action(); - rightMotor.backward(); - leftMotor.forward(); + int leftacc = leftMotor.getAcceleration(); + int rightacc = rightMotor.getAcceleration(); + leftMotor.setAcceleration(10000); + rightMotor.setAcceleration(10000); + if(random.nextBoolean()){ + leftMotor.backward(); + rightMotor.forward(); + } else { + rightMotor.backward(); + leftMotor.forward(); + } long time = System.currentTimeMillis(); - while (!suppressed && System.currentTimeMillis() - time > TURNTIME) { + while (!suppressed && System.currentTimeMillis() - time < TURNTIME) { Thread.yield(); } leftMotor.stop(true); rightMotor.stop(true); + leftMotor.setAcceleration(leftacc); + rightMotor.setAcceleration(rightacc); } } \ No newline at end of file diff --git a/mart/ev3/ex2/nl/ru/des/behaviours/AvoidLowObjectBehaviour.java b/mart/ev3/ex2/nl/ru/des/behaviours/AvoidLowObjectBehaviour.java index 1c47f4f..fd090bf 100644 --- a/mart/ev3/ex2/nl/ru/des/behaviours/AvoidLowObjectBehaviour.java +++ b/mart/ev3/ex2/nl/ru/des/behaviours/AvoidLowObjectBehaviour.java @@ -4,18 +4,15 @@ import lejos.hardware.motor.EV3LargeRegulatedMotor; import lejos.robotics.SampleProvider; import lejos.robotics.subsumption.Behavior; -public class AvoidLowObjectBehaviour implements Behavior { +public class AvoidLowObjectBehaviour extends ReactiveBehaviour implements Behavior { private static final long BACKWARDSTIME = 250; - private static final long TURNTIME = 250; - private SampleProvider touch; - private float[] samples; + private static final long TURNTIME = 500; private boolean avoidDirection; - private boolean suppressed; private EV3LargeRegulatedMotor leftMotor, rightMotor; public AvoidLowObjectBehaviour(EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor, SampleProvider touch) { - this.touch = touch; + super(touch); this.samples = new float[touch.sampleSize()]; this.leftMotor = leftMotor; this.rightMotor = rightMotor; @@ -23,7 +20,7 @@ public class AvoidLowObjectBehaviour implements Behavior { @Override public boolean takeControl() { - touch.fetchSample(samples, 0); + fetchSample(); if(samples[0]>0){ avoidDirection = false; return true; @@ -37,7 +34,6 @@ public class AvoidLowObjectBehaviour implements Behavior { @Override public void action() { - suppressed = false; int leftacc = leftMotor.getAcceleration(); int rightacc = rightMotor.getAcceleration(); leftMotor.setAcceleration(10000); @@ -45,7 +41,7 @@ public class AvoidLowObjectBehaviour implements Behavior { leftMotor.backward(); rightMotor.backward(); long time = System.currentTimeMillis(); - while (!suppressed && System.currentTimeMillis() - time > BACKWARDSTIME) { + while (!suppressed && System.currentTimeMillis() - time < BACKWARDSTIME) { Thread.yield(); } @@ -57,7 +53,7 @@ public class AvoidLowObjectBehaviour implements Behavior { rightMotor.backward(); } time = System.currentTimeMillis(); - while (!suppressed && System.currentTimeMillis() - time > TURNTIME) { + while (!suppressed && System.currentTimeMillis() - time < TURNTIME) { Thread.yield(); } leftMotor.stop(true); @@ -65,9 +61,4 @@ public class AvoidLowObjectBehaviour implements Behavior { leftMotor.setAcceleration(leftacc); leftMotor.setAcceleration(rightacc); } - - @Override - public void suppress() { - suppressed = true; - } } \ No newline at end of file diff --git a/mart/ev3/ex2/nl/ru/des/behaviours/ReactiveBehaviour.java b/mart/ev3/ex2/nl/ru/des/behaviours/ReactiveBehaviour.java index 2f33eb6..db17ebb 100644 --- a/mart/ev3/ex2/nl/ru/des/behaviours/ReactiveBehaviour.java +++ b/mart/ev3/ex2/nl/ru/des/behaviours/ReactiveBehaviour.java @@ -23,11 +23,11 @@ public abstract class ReactiveBehaviour implements Behavior { @Override public void action() { - suppressed = true; + suppressed = false; } @Override public void suppress() { - suppressed = false; + suppressed = true; } } diff --git a/mart/ev3/ex2/nl/ru/des/behaviours/StayInFieldBehaviour.java b/mart/ev3/ex2/nl/ru/des/behaviours/StayInFieldBehaviour.java index 75e6d2a..682c659 100644 --- a/mart/ev3/ex2/nl/ru/des/behaviours/StayInFieldBehaviour.java +++ b/mart/ev3/ex2/nl/ru/des/behaviours/StayInFieldBehaviour.java @@ -1,5 +1,7 @@ package nl.ru.des.behaviours; +import java.util.Random; + import lejos.hardware.motor.EV3LargeRegulatedMotor; import lejos.robotics.Color; import lejos.robotics.SampleProvider; @@ -7,15 +9,17 @@ import lejos.robotics.subsumption.Behavior; public class StayInFieldBehaviour extends ReactiveBehaviour implements Behavior { public static final long BACKWARDSTIME = 250; - public static final long TURNTIME = 250; + public static final long TURNTIME = 500; private EV3LargeRegulatedMotor leftMotor, rightMotor; + private Random random; public StayInFieldBehaviour(SampleProvider color, EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor) { super(color); this.leftMotor = leftMotor; this.rightMotor = rightMotor; + random = new Random(); } @Override @@ -34,14 +38,19 @@ public class StayInFieldBehaviour extends ReactiveBehaviour implements Behavior leftMotor.backward(); rightMotor.backward(); long time = System.currentTimeMillis(); - while (!suppressed && System.currentTimeMillis() - time > BACKWARDSTIME) { + while (!suppressed && System.currentTimeMillis() - time < BACKWARDSTIME) { Thread.yield(); } - leftMotor.forward(); - rightMotor.backward(); + if(random.nextBoolean()){ + leftMotor.backward(); + rightMotor.forward(); + } else { + rightMotor.backward(); + leftMotor.forward(); + } time = System.currentTimeMillis(); - while (!suppressed && System.currentTimeMillis() - time > TURNTIME) { + while (!suppressed && System.currentTimeMillis() - time < TURNTIME) { Thread.yield(); } leftMotor.stop(true); diff --git a/mart/ev3/ex2/nl/ru/des/behaviours/WandererBehaviour.java b/mart/ev3/ex2/nl/ru/des/behaviours/WandererBehaviour.java index f66bb62..ebf5e64 100644 --- a/mart/ev3/ex2/nl/ru/des/behaviours/WandererBehaviour.java +++ b/mart/ev3/ex2/nl/ru/des/behaviours/WandererBehaviour.java @@ -1,5 +1,6 @@ package nl.ru.des.behaviours; +import lejos.hardware.Sound; import lejos.hardware.motor.EV3LargeRegulatedMotor; import lejos.robotics.SampleProvider; import lejos.robotics.subsumption.Behavior; @@ -30,6 +31,7 @@ public class WandererBehaviour extends ReactiveBehaviour implements Behavior { while (!suppressed) { super.fetchSample(); if (ColorMemory.COLORSTOFIND.contains((int)samples[0])) { + Sound.beep(); cm.addColor((int)samples[0]); } Thread.yield(); diff --git a/mart/ev3/ex2/nl/ru/des/bluetooth/ColorMemory.java b/mart/ev3/ex2/nl/ru/des/bluetooth/ColorMemory.java index ec9c575..5448e7b 100644 --- a/mart/ev3/ex2/nl/ru/des/bluetooth/ColorMemory.java +++ b/mart/ev3/ex2/nl/ru/des/bluetooth/ColorMemory.java @@ -5,6 +5,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import lejos.hardware.Sound; import lejos.robotics.Color; import nl.ru.des.LCDPrinter; @@ -18,9 +19,12 @@ public class ColorMemory implements MessageHandler { @Override public void handleMessage(String message) { - colors.add(Integer.valueOf(message)); - LCDPrinter.print(message + " received"); - LCDPrinter.print(colors.toString()); + int color = Integer.valueOf(message); + if(!colors.contains(color)){ + colors.add(Integer.valueOf(message)); + Sound.beep(); + LCDPrinter.print(colors.toString()); + } } public boolean finished() { @@ -28,9 +32,11 @@ public class ColorMemory implements MessageHandler { } public void addColor(int current) { - BTController.sendMessage(Integer.toString(current)); - colors.add(current); - LCDPrinter.print(current + " found"); - LCDPrinter.print(colors.toString()); + if(!colors.contains(current)){ + colors.add(current); + Sound.buzz(); + LCDPrinter.print(colors.toString()); + BTController.sendMessage(Integer.toString(current)); + } } }