package nl.ru.des;
-import java.io.File;
-
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.robotics.subsumption.Behavior;
public abstract class AvoidBehaviour implements Behavior {
protected EV3LargeRegulatedMotor rightMotor, leftMotor, avoidMotor;
- private File audioFile;
+ private String audioFile;
- public AvoidBehaviour(EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor, File audioFile){
+ public AvoidBehaviour(EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor, String audioFile){
this.rightMotor = rightMotor;
this.leftMotor = leftMotor;
this.avoidMotor = leftMotor;
package nl.ru.des;
-import java.io.File;
-
import lejos.hardware.Button;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.sensor.EV3UltrasonicSensor;
public AvoidHighObjectBehaviour(EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor,
EV3UltrasonicSensor ultraSone) {
- super(leftMotor, rightMotor, new File("detect.wav"));
+ super(leftMotor, rightMotor, "detect.wav");
ultrasoneSample = ultraSone.getDistanceMode();
samples = new float[ultrasoneSample.sampleSize()];
calibrate();
}
private void calibrate(){
- LCDPrinter.print("Calibrate ultrasone...");
- LCDPrinter.print("Place object in");
- LCDPrinter.print(" turn radius");
+ LCDPrinter.print("Calibrate ultrasone, place object in the turn radius...");
Button.waitForAnyPress();
ultrasoneSample.fetchSample(samples, 0);
limit = samples[0];
package nl.ru.des;
-import java.io.File;
-
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.sensor.EV3TouchSensor;
import lejos.robotics.SampleProvider;
public class AvoidLowObjectBehaviour extends AvoidBehaviour {
private SampleProvider rightSample, leftSample;
private float[] samples;
- private long lastPush = 0;
public AvoidLowObjectBehaviour(EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor,
EV3TouchSensor leftTouch, EV3TouchSensor rightTouch) {
- super(leftMotor, rightMotor, new File("bump.wav"));
+ super(leftMotor, rightMotor, "bump.wav");
rightSample = rightTouch.getTouchMode();
leftSample = leftTouch.getTouchMode();
samples = new float[rightTouch.sampleSize()];
if(samples[0] == 1){
super.setAvoidDirection(false);
takeControl = true;
- lastPush = System.currentTimeMillis();
}
//Check if right sensor is pressed
if(samples[0] == 1){
super.setAvoidDirection(true);
takeControl = true;
- lastPush = System.currentTimeMillis();
}
- return takeControl || System.currentTimeMillis()-lastPush < 1000;
+ return takeControl;
}
@Override
class ButtonListener implements KeyListener {
@Override
public void keyPressed(Key k) {
- LCDPrinter.shutdown("Bye...");
System.exit(0);
}
package nl.ru.des;
+import java.util.Deque;
+import java.util.Iterator;
import java.util.LinkedList;
-import java.util.Queue;
-import lejos.hardware.lcd.LCD;
+import lejos.hardware.lcd.Font;
+import lejos.hardware.lcd.GraphicsLCD;
import lejos.utility.Delay;
-public class LCDPrinter extends Thread{
- public static final int PRINTDELAY = 250;
-
- private static Queue<String> buffer = new LinkedList<String>();
- private static boolean shutdown = false;
+public class LCDPrinter extends Thread {
+ public static final int PRINTDELAY = 50;
+
+ private static Deque<String> buffer = new LinkedList<String>();
+
+ private GraphicsLCD glcd;
+ private Font font;
+ private Deque<String> lcdbuffer;
+ private int charwidth;
+
+ public LCDPrinter(GraphicsLCD glcd, Font font) {
+ this.glcd = glcd;
+ this.font = font;
+ glcd.setFont(font);
+ lcdbuffer = new LinkedList<String>();
+ charwidth = glcd.getWidth()/font.width;
+ for(int i = 0; i<glcd.getHeight()/font.height; i++){
+ lcdbuffer.add("");
+ }
+ }
public static void print(String s){
- buffer.add(s);
+ buffer.addLast(s);
}
-
- public void run(){
- int y = 0;
- while(!shutdown || (shutdown && !buffer.isEmpty())){
- if(!buffer.isEmpty()){
- LCD.clear(y);
- LCD.clear(Math.max(0, y+1));
- LCD.drawString(buffer.remove(), 0, y);
- y = y < LCD.DISPLAY_CHAR_DEPTH-1 ? y + 1 : 0;
+
+ public void run() {
+ Iterator<String> it;
+ while (true) {
+ if (!buffer.isEmpty()) {
+ String c = buffer.remove();
+ lcdbuffer.removeLast();
+ if(c.length() > charwidth){
+ buffer.addFirst(c.substring(charwidth, c.length()));
+ c = c.substring(0, charwidth);
+ }
+ lcdbuffer.addFirst(c);
+ glcd.clear();
+ it = lcdbuffer.descendingIterator();
+ for(int i = 0; it.hasNext(); i++){
+ glcd.drawString(it.next(), 0, i*font.height, font.getBaselinePosition());
+ }
}
Delay.msDelay(PRINTDELAY);
}
}
-
- public static void shutdown(String s) {
- buffer.add(s);
- shutdown = true;
- while(!buffer.isEmpty()){
- Delay.msDelay(200);
- }
- }
}
\ No newline at end of file
package nl.ru.des;
-import java.io.File;
-
import lejos.hardware.Audio;
import lejos.hardware.ev3.EV3;
import lejos.hardware.ev3.LocalEV3;
+import lejos.hardware.lcd.Font;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.hardware.sensor.EV3TouchSensor;
public class Main {
public static void main(String[] args) {
- LCDPrinter lcdprinter = new LCDPrinter();
- LCDPrinter.print("Starting up");
- lcdprinter.start();
- LCDPrinter.print("Brick...");
EV3 brick = LocalEV3.get();
+ LCDPrinter lcdprinter = new LCDPrinter(brick.getGraphicsLCD(), Font.getSmallFont());
+ LCDPrinter.print("Starting up systems");
+ lcdprinter.start();
- LCDPrinter.print("Audio...");
+ LCDPrinter.print("Loading audio...");
Audio audio = brick.getAudio();
WavPlayer wavplayer = new WavPlayer(audio);
wavplayer.start();
- WavPlayer.playWav(new File("bootaudio.wav"));
+ WavPlayer.playWav("boot.wav");
- LCDPrinter.print("Keylistener...");
+ LCDPrinter.print("Loading keylistener...");
brick.getKey("Escape").addKeyListener(new ButtonListener());
- //Get motors
- LCDPrinter.print("Motors...");
+ LCDPrinter.print("Loading motors...");
EV3LargeRegulatedMotor rightMotor = new EV3LargeRegulatedMotor(MotorPort.D);
EV3LargeRegulatedMotor leftMotor = new EV3LargeRegulatedMotor(MotorPort.A);
rightMotor.setSpeed(300);
rightMotor.setAcceleration(1000);
leftMotor.setAcceleration(1000);
- //Get sensors
- LCDPrinter.print("Touch...");
+ LCDPrinter.print("Loading touch sensors...");
EV3TouchSensor leftTouch = new EV3TouchSensor(brick.getPort("S1"));
EV3TouchSensor rightTouch = new EV3TouchSensor(brick.getPort("S4"));
- LCDPrinter.print("Light...");
+ LCDPrinter.print("Loading light sensor...");
NXTLightSensor lightSensor = new NXTLightSensor(brick.getPort("S2"));
- LCDPrinter.print("Ultrasone...");
+ LCDPrinter.print("Loading ultrasone sensor...");
EV3UltrasonicSensor ultraSensor = new EV3UltrasonicSensor(brick.getPort("S3"));
- WavPlayer.playWav(new File("bootsystem.wav"));
+ WavPlayer.playWav("bootsystem.wav");
- //Initialize behaviours
- LCDPrinter.print("Behaviours...");
+ LCDPrinter.print("Initializing behaviours...");
Behavior[] behaviorList = new Behavior[] {
new WandererBehaviour(leftMotor, rightMotor),
new AvoidHighObjectBehaviour(leftMotor, rightMotor, ultraSensor),
new StayInFieldBehaviour(lightSensor, leftMotor, rightMotor),
};
- WavPlayer.playWav(new File("calibrate.wav"));
+ WavPlayer.playWav("calibrate.wav");
- LCDPrinter.print("Arbitrator...");
+ LCDPrinter.print("Initializing arbitrator...");
Arbitrator arb = new Arbitrator(behaviorList);
- WavPlayer.playWav(new File("takeoff.wav"));
+ WavPlayer.playWav("takeoff.wav");
Delay.msDelay(1850);
LCDPrinter.print("Takeoff!");
arb.start();
package nl.ru.des;
-import java.io.File;
-
import lejos.hardware.Button;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.sensor.NXTLightSensor;
private float black, white, dist;
public StayInFieldBehaviour(NXTLightSensor lightSensor, EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor) {
- super(rightMotor, leftMotor, new File("bound.wav"));
+ super(rightMotor, leftMotor, "bound.wav");
light = lightSensor.getRedMode();
samples = new float[light.sampleSize()];
calibrate();
}
private void calibrate(){
- LCDPrinter.print("Calibrate Light...");
- //Get the black value
- LCDPrinter.print("Place on black");
+ LCDPrinter.print("Calibrate Light, place the light sensor on black...");
Button.waitForAnyPress();
light.fetchSample(samples, 0);
black = samples[0];
LCDPrinter.print("Black: " + black);
- //Get the white value
- LCDPrinter.print("Place on white");
+ LCDPrinter.print("Place the light sensor on white");
Button.waitForAnyPress();
light.fetchSample(samples, 0);
white = samples[0];
LCDPrinter.print("White: " + white);
- //Calculate the delta
dist = Math.abs(black-white);
}
import lejos.utility.Delay;
public class WavPlayer extends Thread{
- private static File current;
+ private static String current;
private Audio audio = null;
- public static void playWav(File c){
+ public static void playWav(String c){
current = c;
}
public void run(){
while(true){
if(current != null){
- audio.playSample(current);
+ File c = new File(current);
+ if(c.canRead()){
+ audio.playSample(new File(current));
+ }
current = null;
}
Delay.msDelay(200);
-echo "Audio system loaded" | espeak --stdout | sox - -r 8000 -b 8 bootaudio.wav
-echo "Ready for calibration" | espeak --stdout | sox - -r 8000 -b 8 bootsystem.wav
-echo "Succesfull calibration" | espeak --stdout | sox - -r 8000 -b 8 calibrate.wav
-echo "Takeoff in 3 2 1 go" | espeak --stdout | sox - -r 8000 -b 8 takeoff.wav
-echo "Almost out of bounds" | espeak --stdout | sox - -r 8000 -b 8 bounds.wav
-echo "Bumped against low object" | espeak --stdout | sox - -r 8000 -b 8 bump.wav
-echo "Detected big object" | espeak --stdout | sox - -r 8000 -b 8 detect.wav
+echo "Audio system loaded" | espeak --stdout | sox - -r 8000 -b 8 bootaudio.wav
+echo "Ready for calibration" | espeak --stdout | sox - -r 8000 -b 8 bootsystem.wav
+echo "Succesfull calibration" | espeak --stdout | sox - -r 8000 -b 8 calibrate.wav
+echo "Takeoff in 3 2 1 go" | espeak --stdout | sox - -r 8000 -b 8 takeoff.wav
+echo "Almost out of bounds" | espeak --stdout | sox - -r 8000 -b 8 bounds.wav
+echo "Bumped against low object" | espeak --stdout | sox - -r 8000 -b 8 bump.wav
+echo "Detected big object" | espeak --stdout | sox - -r 8000 -b 8 detect.wav