added gracefull shutdown
authorMart Lubbers <mart@martlubbers.net>
Mon, 11 Jan 2016 10:39:11 +0000 (11:39 +0100)
committerMart Lubbers <mart@martlubbers.net>
Mon, 11 Jan 2016 10:39:11 +0000 (11:39 +0100)
dsl/runtime/specs/wander.tdsl
dsl/runtime/src/nl/ru/des/BasicBehaviour.java
dsl/runtime/src/nl/ru/des/Marster.java
dsl/runtime/src/nl/ru/des/sensors/BTController.java
dsl/runtime/src/nl/ru/des/sensors/SensorCollector.java
dsl/runtime/src/nl/ru/des/sound/rick.wav [deleted file]
dsl/xtend/src/robots/missions/generator/TaskDSLGenerator.xtend

index 0f764d3..30081c8 100644 (file)
@@ -7,6 +7,7 @@ Behaviour Drive
                left motor forward
                right motor forward
                wait forever
+               
 Behaviour Wander
        take control:
        action:
@@ -37,106 +38,70 @@ Behaviour StayInFieldB
                right motor forward
                wait 1000 ms
                
-Behaviour MeasureGreenLake
+Behaviour GreenLake
        take control: (&& Color is Green not flag set GreenMeasured)
        action:
                measure Lake
-               set flag GreenMeasured
-Behaviour MeasureBlueLake
+               set flag GreenLake
+               
+Behaviour BlueLake
        take control: (&& Color is Blue not flag set BlueMeasured)
        action:
                measure Lake
-               set flag BlueMeasured
-Behaviour MeasureRedLake
+               set flag BlueLake
+               
+Behaviour RedLake
        take control: (&& Color is Red not flag set RedMeasured)
        action:
                measure Lake
-               set flag RedMeasured
+               set flag RedLake
                
-Behaviour LocateL
+Behaviour MeasureRock
+       take control: (&& Distance dangerous at front not flag set Measured)
+       action:
+               measure Rock
+               set flag Measured
+                               
+Behaviour PushL
        take control: Touched on left
        action:
                right motor forward
                left motor backward
-               wait 500 ms
+               /*wait 500 ms
                right motor forward
                left motor forward
-               wait 1000 ms
+               wait 500ms*/
                
-Behaviour LocateR
+Behaviour PushR
        take control: Touched on right
        action:
                left motor forward
                right motor backward
-               wait 500 ms
+               /*wait 500 ms
                right motor forward
                left motor forward
-               wait 1000 ms
+               wait 1000 ms*/
 
-Behaviour Push
+Behaviour PushB
        take control: (&& Touched on left Touched on right)
        action:
+               set flag Pushing
                left motor forward
                right motor forward
-               wait 1000 ms
-               
-Behaviour BumpL
-       take control: Touched on left
-       action:
-               left motor backward with speed 50 acceleration 1000
-               right motor backward
-               wait 2000 ms
-               
-Behaviour BumpR
-       take control: Touched on right
-       action:
-               right motor backward with speed 50 acceleration 1000
-               left motor backward
-               wait 2000 ms
-
-Behaviour AvoidHigh
-       take control: Distance dangerous at front
-       action: 
-               turn randomly 10 to 11 degrees
+               wait 10000 ms
 
-Behaviour MeasureRock
-       take control: (&& Distance dangerous at front not flag set RockMeasured)
+Behaviour StopPushing
+       take control: (&& not Touched on left not Touched on right flag set Pushing)
        action:
-               measure Rock
-               set flag RockMeasured
-
-Behaviour FindParkingL
-       take control: Light on left
-       action:
-               turn left exactly 10 degrees with speed 20 acceleration 1000
-
-Behaviour FindParkingR
-       take control: Light on right
-       action:
-               turn right exactly 10 degrees with speed 20 acceleration 1000
-
-Behaviour FindParkingSpace
-       take control: (&& Light on right Light on left)
-       action:
-               right motor backward with speed 80 acceleration 1000
-               left motor backward with speed 80 acceleration 1000
-               wait 1000 ms
-               turn right exactly 180 degrees
-               set flag Parked
+               set flag Pushed
+               
+/*Mission FindAndMeasureLakes
+       using Wander StayInFieldR StayInFieldL StayInFieldB GreenLake RedLake BlueLake 
+       and stops when (&& flag set GreenLake flag set RedLake flag set BlueLake)
 
-//Mission pushRock
-//     using Drive LocateR LocateL Push StayInFieldB StayInFieldL StayInFieldR and stops when Never
-//Mission avoidHighRocks
-//     using Drive AvoidHigh BumpR BumpL StayInFieldB StayInFieldL StayInFieldR and stops when Never
-Mission MeasureRocks
-       using Drive StayInFieldB StayInFieldL StayInFieldR MeasureRock and stops when flag set RockMeasured
-Mission FindParkingSpaceInTheCorner
-       using Wander StayInFieldB FindParkingL FindParkingR FindParkingSpace and stops when flag set Parked
-//Mission measureLakes
-//     using Drive MeasureRedLake MeasureGreenLake MeasureBlueLake and stops when (&& flag set GreenMeasured flag set BlueMeasured flag set RedMeasured)
-//Mission findBlueLakeWhileAvoidingRocks
-//     using Wander BumpR BumpL StayInFieldB StayInFieldL StayInFieldR and stops when Color is Blue
-//Mission findAllLakesAndMeasureThem
-//     using Wander MeasureLake StayInFieldB StayInFieldL StayInFieldR and stops when Collected at least Green Blue Red
-//Mission justWander 
-//     using Wander Wander StayInFieldB StayInFieldL StayInFieldR and stops when Color is Cyan
\ No newline at end of file
+Mission FindAndMeasureRocks
+       using Wander StayInFieldR StayInFieldL StayInFieldB MeasureRock
+       and stops when flag set Measured */
+Mission PushRocks
+       using Wander PushL PushR PushB StopPushing StayInFieldR StayInFieldL StayInFieldB
+       and stops when flag set Pushed
\ No newline at end of file
index 44fcbe2..455cbe2 100644 (file)
@@ -29,13 +29,14 @@ public abstract class BasicBehaviour implements Behavior{
                leftMotor.setAcceleration(Constants.acceleration);
                rightMotor.stop(true);
                leftMotor.stop(true);
+               measMotor.stop(true);
        }
 
        protected void measureLake(){
                long time = System.currentTimeMillis();
                rightMotor.backward();
                leftMotor.backward();
-               while(System.currentTimeMillis()-time<500){
+               while(System.currentTimeMillis()-time < 250){
                        Thread.yield();
                }
                
index 83bc184..3684c0a 100644 (file)
@@ -1,12 +1,9 @@
 package nl.ru.des;
 
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
 import java.util.LinkedList;
 import java.util.Random;
 
+import lejos.hardware.Button;
 import lejos.hardware.Sound;
 import lejos.hardware.ev3.EV3;
 import lejos.hardware.ev3.LocalEV3;
@@ -56,20 +53,12 @@ public class Marster {
                        RemoteSensors rs = new RemoteSensors(leftTouch, rightTouch, frontUltra, color);
                        
                        BTController.startSlave();
-                       rs.start(BTController.buf);
+                       try {
+                               rs.start(BTController.buf);
+                       } catch (Exception e){
+                               System.exit(0);
+                       }
                } else {
-//                     try {
-//                             InputStream inp = Marster.class.getResourceAsStream("nl/ru/des/sound/rick.wav");
-//                             FileOutputStream out = new FileOutputStream("rick.wav");
-//                             byte buffer[] = new byte[2048];
-//                             while(inp.read(buffer)>0){
-//                                     out.write(buffer);
-//                             }
-//                             inp.close();
-//                             out.close();
-//                     } catch (IOException e) {
-//                             e.printStackTrace();
-//                     }
                        String slave = brick.getName().equalsIgnoreCase("Rover5") ? "Rover6" : "Rover8";
                        LCDPrinter.print("Starting as as master...");
                        LCDPrinter.print("My name is " + brick.getName());
@@ -97,16 +86,20 @@ public class Marster {
 
                        BTController.startMaster(slave, sc);
                        LCDPrinter.print("Finished loading");
+                       sc.calibrate();
                        LinkedList<Mission> missions = Missions.getMissions(sc, rightMotor, leftMotor, measMotor);
                        random = new Random();
+                       LCDPrinter.print("Press any button to start");
+                       Button.waitForAnyEvent();
                        for(Mission m : missions){
                                LCDPrinter.print("Start " + m.name + " mission...");
                                arb = new Arbitrator(m.behaviours);
                                sc.reset();
+                               Sound.buzz();
                                arb.start();
                                LCDPrinter.print(m.name + " finished!!1one!");
                        }
-//                     Sound.playSample(new File("rick.wav"));
+                       System.exit(0);
                }
        }
 }
\ No newline at end of file
index 062fa61..37214a5 100644 (file)
@@ -67,8 +67,9 @@ public class BTController{
                                                try {
                                                        dataOut.write(buf.poll().getBytes());
                                                        dataOut.flush();
-                                               } catch (IOException e) {
+                                               } catch (Exception e) {
                                                        e.printStackTrace();
+                                                       System.exit(0);
                                                }
                                        }
                                        Thread.yield();
index db10870..af8e87a 100644 (file)
@@ -3,15 +3,18 @@ package nl.ru.des.sensors;
 import java.util.HashSet;
 import java.util.Set;
 
+import lejos.hardware.Button;
 import lejos.hardware.sensor.EV3GyroSensor;
 import lejos.robotics.SampleProvider;
+import lejos.utility.Delay;
+import nl.ru.des.LCDPrinter;
 
 public class SensorCollector implements MessageHandler{
        public static final int DELAY = 200;
 
-       private static final float DANGER_DISTANCE_FRONT = 0.175f;
-       private static final float DANGER_DISTANCE_BACK = 0.035f;
-       private static final float DANGER_LIGHT = 0.40f;
+       private float DANGER_DISTANCE_FRONT = 0.175f;
+       private float DANGER_DISTANCE_BACK = 0.035f;
+       private float DANGER_LIGHT = 0.40f;
        
        private Set<String> variables;
        
@@ -39,6 +42,7 @@ public class SensorCollector implements MessageHandler{
                gyroSamples = new float[gyro.sampleSize()];
                leftLightSamples = new float[leftLight.sampleSize()];
                rightLightSamples = new float[rightLight.sampleSize()];
+
                ultraTime = System.currentTimeMillis();
                leftLightTime = System.currentTimeMillis();
                rightLightTime = System.currentTimeMillis();
@@ -51,6 +55,36 @@ public class SensorCollector implements MessageHandler{
                variables = new HashSet<String>();
        }
        
+
+       public void calibrate() {
+               LCDPrinter.print("Put left light on Blue");
+               Button.waitForAnyEvent();
+               leftLight();
+               DANGER_LIGHT = leftLightSamples[0];
+               LCDPrinter.print("Light limit: " + Float.toString(DANGER_LIGHT));
+
+               Delay.msDelay(350);
+               LCDPrinter.print("Put left light on Black");
+               Button.waitForAnyEvent();
+               leftLight();
+               DANGER_LIGHT = (leftLightSamples[0]+DANGER_LIGHT)/2.0f;
+               LCDPrinter.print("Light limit: " + Float.toString(DANGER_LIGHT));
+               
+               Delay.msDelay(350);             
+               LCDPrinter.print("Place back ultra safe");
+               Button.waitForAnyEvent();
+               backDistance();
+               DANGER_DISTANCE_BACK = ultraSamples[0] + 0.05f;
+               LCDPrinter.print("Back ultra limit: " + Float.toString(DANGER_DISTANCE_BACK));
+
+               Delay.msDelay(350);
+               LCDPrinter.print("Place front ultra in danger");
+               Button.waitForAnyEvent();
+               DANGER_DISTANCE_FRONT = frontUltra;
+               LCDPrinter.print("Calibration done");
+               LCDPrinter.print("Front ultra limit: " + Float.toString(DANGER_DISTANCE_FRONT));
+       }
+       
        //Local sensors
        public boolean backDistance(){
                if(System.currentTimeMillis()-ultraTime>DELAY){
diff --git a/dsl/runtime/src/nl/ru/des/sound/rick.wav b/dsl/runtime/src/nl/ru/des/sound/rick.wav
deleted file mode 100644 (file)
index 9e46ce1..0000000
Binary files a/dsl/runtime/src/nl/ru/des/sound/rick.wav and /dev/null differ
index 3baaa06..5af781f 100644 (file)
@@ -107,7 +107,9 @@ public class «b.name»Behaviour extends BasicBehaviour {
                        «ELSEIF a.rl != null»
                        measure«a.rl.d.toString()»();
                        «ELSE»
-                       sensors.saveVar("«a.varName.toString()»");
+                       if(!suppressed){
+                               sensors.saveVar("«a.varName.toString()»");
+                       }
                        «ENDIF»
                «ENDFOR»
                LCDPrinter.print("Stop: «b.name»");