+++ /dev/null
-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
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;
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);
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
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;
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
@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
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;
@Override
public boolean takeControl() {
- touch.fetchSample(samples, 0);
+ fetchSample();
if(samples[0]>0){
avoidDirection = false;
return true;
@Override
public void action() {
- suppressed = false;
int leftacc = leftMotor.getAcceleration();
int rightacc = rightMotor.getAcceleration();
leftMotor.setAcceleration(10000);
leftMotor.backward();
rightMotor.backward();
long time = System.currentTimeMillis();
- while (!suppressed && System.currentTimeMillis() - time > BACKWARDSTIME) {
+ while (!suppressed && System.currentTimeMillis() - time < BACKWARDSTIME) {
Thread.yield();
}
rightMotor.backward();
}
time = System.currentTimeMillis();
- while (!suppressed && System.currentTimeMillis() - time > TURNTIME) {
+ while (!suppressed && System.currentTimeMillis() - time < TURNTIME) {
Thread.yield();
}
leftMotor.stop(true);
leftMotor.setAcceleration(leftacc);
leftMotor.setAcceleration(rightacc);
}
-
- @Override
- public void suppress() {
- suppressed = true;
- }
}
\ No newline at end of file
@Override
public void action() {
- suppressed = true;
+ suppressed = false;
}
@Override
public void suppress() {
- suppressed = false;
+ suppressed = true;
}
}
package nl.ru.des.behaviours;
+import java.util.Random;
+
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.robotics.Color;
import lejos.robotics.SampleProvider;
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
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);
package nl.ru.des.behaviours;
+import lejos.hardware.Sound;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.robotics.SampleProvider;
import lejos.robotics.subsumption.Behavior;
while (!suppressed) {
super.fetchSample();
if (ColorMemory.COLORSTOFIND.contains((int)samples[0])) {
+ Sound.beep();
cm.addColor((int)samples[0]);
}
Thread.yield();
import java.util.List;
import java.util.Set;
+import lejos.hardware.Sound;
import lejos.robotics.Color;
import nl.ru.des.LCDPrinter;
@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() {
}
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));
+ }
}
}