updated lcd printer and better low object avoidance behaviour
authorMart Lubbers <mart@martlubbers.net>
Fri, 16 Oct 2015 08:35:55 +0000 (10:35 +0200)
committerMart Lubbers <mart@martlubbers.net>
Fri, 16 Oct 2015 08:35:55 +0000 (10:35 +0200)
mart/ev3/ex1/nl/ru/des/AvoidBehaviour.java
mart/ev3/ex1/nl/ru/des/AvoidHighObjectBehaviour.java
mart/ev3/ex1/nl/ru/des/AvoidLowObjectBehaviour.java
mart/ev3/ex1/nl/ru/des/ButtonListener.java
mart/ev3/ex1/nl/ru/des/LCDPrinter.java
mart/ev3/ex1/nl/ru/des/Main.java
mart/ev3/ex1/nl/ru/des/StayInFieldBehaviour.java
mart/ev3/ex1/nl/ru/des/WavPlayer.java
mart/ev3/ex1/nl/ru/des/sounds/makeall.sh

index be21145..37747e7 100644 (file)
@@ -1,15 +1,13 @@
 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;
index 7a30ba1..b8870b4 100644 (file)
@@ -1,7 +1,5 @@
 package nl.ru.des;
 
-import java.io.File;
-
 import lejos.hardware.Button;
 import lejos.hardware.motor.EV3LargeRegulatedMotor;
 import lejos.hardware.sensor.EV3UltrasonicSensor;
@@ -14,16 +12,14 @@ public class AvoidHighObjectBehaviour extends AvoidBehaviour {
 
        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];
index 82309dc..d282226 100644 (file)
@@ -1,7 +1,5 @@
 package nl.ru.des;
 
-import java.io.File;
-
 import lejos.hardware.motor.EV3LargeRegulatedMotor;
 import lejos.hardware.sensor.EV3TouchSensor;
 import lejos.robotics.SampleProvider;
@@ -10,11 +8,10 @@ import lejos.utility.Delay;
 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()];
@@ -28,7 +25,6 @@ public class AvoidLowObjectBehaviour extends AvoidBehaviour {
                if(samples[0] == 1){
                        super.setAvoidDirection(false);
                        takeControl = true;
-                       lastPush = System.currentTimeMillis();
                }
                
                //Check if right sensor is pressed
@@ -36,9 +32,8 @@ public class AvoidLowObjectBehaviour extends AvoidBehaviour {
                if(samples[0] == 1){
                        super.setAvoidDirection(true);
                        takeControl = true;
-                       lastPush = System.currentTimeMillis();
                }
-               return takeControl || System.currentTimeMillis()-lastPush < 1000;
+               return takeControl;
        }
        
        @Override
index d4d4f8a..68ae3a3 100644 (file)
@@ -6,7 +6,6 @@ import lejos.hardware.KeyListener;
 class ButtonListener implements KeyListener {
        @Override
        public void keyPressed(Key k) {
-               LCDPrinter.shutdown("Bye...");
                System.exit(0);
        }
 
index d73e12b..125bb1c 100644 (file)
@@ -1,39 +1,56 @@
 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
index 00593bc..805ff02 100644 (file)
@@ -1,10 +1,9 @@
 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;
@@ -17,23 +16,21 @@ import lejos.utility.Delay;
 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);
@@ -41,19 +38,17 @@ public class Main {
                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),
@@ -61,12 +56,12 @@ public class Main {
                                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();
index 8f0823a..01ac30f 100644 (file)
@@ -1,7 +1,5 @@
 package nl.ru.des;
 
-import java.io.File;
-
 import lejos.hardware.Button;
 import lejos.hardware.motor.EV3LargeRegulatedMotor;
 import lejos.hardware.sensor.NXTLightSensor;
@@ -13,7 +11,7 @@ public class StayInFieldBehaviour extends AvoidBehaviour{
        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();
@@ -21,22 +19,18 @@ public class StayInFieldBehaviour extends AvoidBehaviour{
        }
        
        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);
        }
 
index 87a490e..1a3b55e 100644 (file)
@@ -6,10 +6,10 @@ import lejos.hardware.Audio;
 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;
        }
        
@@ -22,7 +22,10 @@ public class WavPlayer extends Thread{
        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);
index 58db755..79377df 100644 (file)
@@ -1,7 +1,7 @@
-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