update with colors etc, we now need to test it on physical robots
authorMart Lubbers <mart@martlubbers.net>
Sat, 21 Nov 2015 09:52:39 +0000 (10:52 +0100)
committerMart Lubbers <mart@martlubbers.net>
Sat, 21 Nov 2015 09:52:39 +0000 (10:52 +0100)
dsl/runtime/specs/spec1.tdsl
dsl/runtime/src/nl/ru/des/MarsRover.java
dsl/runtime/src/nl/ru/des/Mission.java
dsl/runtime/src/nl/ru/des/SensorCollector.java
dsl/runtime/src/nl/ru/des/ShutdownBehaviour.java
dsl/xtend/src/robots/missions/TaskDSL.xtext
dsl/xtend/src/robots/missions/generator/TaskDSLGenerator.xtend
dsl/xtend/src/robots/missions/validation/TaskDSLValidator.xtend

index ccd0607..b1d6f6a 100644 (file)
@@ -7,7 +7,6 @@ Behaviour Wander
                left motor forward
                right motor forward
                wait forever
-       collect color
 Behaviour AvoidLowLeftObjects
        take control: Touched on left
        action:
index 41a5f90..8561c0a 100644 (file)
@@ -16,7 +16,7 @@ import lejos.robotics.subsumption.Arbitrator;
 
 public class MarsRover {
        public static final float SAMPLERATE = 100;
-       public static LinkedList<Mission> missions = new LinkedList<Mission>();
+       public static Arbitrator arb;
        
        @SuppressWarnings("resource")
        public static void main(String[] args) {
@@ -54,19 +54,11 @@ public class MarsRover {
                ColorMemory colorMemory = new ColorMemory(color);
                
                Arbitrator a;
-               missions = Missions.getMissions(sensors, rightMotor, leftMotor, colorMemory);
+               LinkedList<Mission> missions = Missions.getMissions(sensors, rightMotor, leftMotor, colorMemory);
                for(Mission m : missions){
                        LCDPrinter.print("Start " + m.name + " mission...");
                        a = new Arbitrator(m.behaviours);
-                       m.SetArbitrator(a);
                        a.start();
                }
        }
-       
-       public static void FinishMission(String missionName){
-               Mission m = missions.stream().filter(o -> o.name.equalsIgnoreCase(missionName)).findFirst().get();
-               if(m != null){
-                       m.arbitrator.stop();
-               }
-       }
 }
\ No newline at end of file
index 41b7c03..c80678e 100644 (file)
@@ -1,19 +1,13 @@
 package nl.ru.des;
 
-import lejos.robotics.subsumption.Arbitrator;
 import lejos.robotics.subsumption.Behavior;
 
 public class Mission {
        public String name;
        public Behavior[] behaviours;
-       public Arbitrator arbitrator;
        
        public Mission(String name, Behavior[] behaviours){
                this.name = name;
                this.behaviours = behaviours;
        }
-       
-       public void SetArbitrator(Arbitrator arbitrator){
-               this.arbitrator = arbitrator;
-       }
 }
index 7695ee0..5210bd1 100644 (file)
@@ -1,20 +1,24 @@
 package nl.ru.des;
 
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
 import lejos.robotics.SampleProvider;
 
 public class SensorCollector{
-       public static final int DELAY = 50;
+       public static final int DELAY = 100;
        
-       private SampleProvider ultrasone, color, leftTouch, rightTouch;
+       private SampleProvider ultrasone, leftTouch, rightTouch;
        private float[] ultrasoneSamples, colorSamples, leftTouchSamples, rightTouchSamples;
-       private long ultrasoneTime, colorTime, leftTouchTime, rightTouchTime;
+       private long ultrasoneTime, leftTouchTime, rightTouchTime;
+       private Set<Integer> colorsCollected;
 
        public SensorCollector(SampleProvider ultrasone,
-                       SampleProvider color,
+                       final SampleProvider color,
                        SampleProvider leftTouch,
                        SampleProvider rightTouch){
                this.ultrasone = ultrasone;
-               this.color = color;
                this.leftTouch = leftTouch;
                this.rightTouch = rightTouch;
                ultrasoneSamples = new float[ultrasone.sampleSize()];
@@ -22,9 +26,22 @@ public class SensorCollector{
                leftTouchSamples = new float[leftTouch.sampleSize()];
                rightTouchSamples = new float[rightTouch.sampleSize()];
                ultrasoneTime = System.currentTimeMillis();
-               colorTime = System.currentTimeMillis();
                leftTouchTime = System.currentTimeMillis();
                rightTouchTime = System.currentTimeMillis();
+               colorsCollected = new HashSet<Integer>();
+               new Thread(){
+                       @Override public void run(){
+                               long time = System.currentTimeMillis();
+                               while(true){
+                                       if(System.currentTimeMillis() - time > DELAY){
+                                               color.fetchSample(colorSamples, 0);
+                                               colorsCollected.add(color());
+                                               time = System.currentTimeMillis();
+                                       }
+                                       Thread.yield();
+                               }
+                       }
+               }.start();
        }
        
        public float distance(){
@@ -36,10 +53,6 @@ public class SensorCollector{
        }
        
        public int color(){
-               if(System.currentTimeMillis()-colorTime>DELAY){
-                       color.fetchSample(colorSamples, 0);
-                       colorTime = System.currentTimeMillis();
-               }
                return (int)colorSamples[0];
        }
        
@@ -58,4 +71,8 @@ public class SensorCollector{
                }
                return rightTouchSamples[0]==1;
        }
+
+       public boolean collected(int[] is) {
+               return colorsCollected.containsAll(Arrays.asList(is));
+       }
 }
\ No newline at end of file
index 9b34b95..430b5df 100644 (file)
@@ -3,17 +3,13 @@ package nl.ru.des;
 import lejos.hardware.motor.EV3LargeRegulatedMotor;
 
 public class ShutdownBehaviour extends BasicBehaviour{
-
-       private String missionName;
-       
        public ShutdownBehaviour(SensorCollector sensors, EV3LargeRegulatedMotor leftMotor,
-                       EV3LargeRegulatedMotor rightMotor, ColorMemory colors, String missionName) {
+                       EV3LargeRegulatedMotor rightMotor, ColorMemory colors) {
                super(sensors, leftMotor, rightMotor, colors);
-               this.missionName = missionName;
        }
        
        @Override public void action(){
-               //Here it should stop the current arbitrator, not sure how yet...
-               MarsRover.FinishMission(missionName);
+               LCDPrinter.print("Terminate mission");
+               MarsRover.arb.stop();
        }
 }
index 456ac4c..7a2de03 100644 (file)
@@ -23,7 +23,7 @@ StoppingCondition:
 
 Behaviour: 'Behaviour' name=ID 
        'take control:' tc=StoppingExpression?
-       'action:' actions+=Action+ (clcColor='collect color')?;
+       'action:' actions+=Action+;
 
 Action:
        whichMotor=LeftRight 'motor' dir=Direction ('with speed' spd=INT 'acceleration' acc=INT)? |
index 58e115f..d7ee2ed 100644 (file)
@@ -47,7 +47,7 @@ public class Missions{
                        «FOR b : m.behaviours SEPARATOR ","»
                        new Behaviours.«b.name»Behaviour(sensors, rightMotor, leftMotor, colors)
                        «ENDFOR»,
-                       new ShutdownBehaviour(sensors, rightMotor, leftMotor, colors, "«m.name»"){
+                       new ShutdownBehaviour(sensors, rightMotor, leftMotor, colors){
                                @Override public boolean takeControl(){
                                        return «printExpression(m.se)»;
                                }
@@ -88,12 +88,6 @@ public class Behaviours{
                                «ELSE»
                                time = System.currentTimeMillis();
                                while(!suppressed«IF a.time.time > 0» && System.currentTimeMillis()-time>«a.time.time»«ENDIF»){
-                                       «IF !b.clcColor.nullOrEmpty»
-                                       int currentColor = sensors.color();
-                                       if (Constants.colorsToFind.contains(currentColor)) {
-                                               colors.addColor(currentColor);
-                                       }
-                                       «ENDIF»
                                        Thread.yield(); 
                                }
                                «ENDIF»
@@ -108,7 +102,7 @@ public class Behaviours{
        def CharSequence printExpression(StoppingExpression e)'''
        «IF e.scond != null»
                «IF !e.scond.colors.nullOrEmpty»
-               colors.containsAll(Constants.colorsToFind)
+               sensors.collected(new int[]{«FOR c : e.scond.colors SEPARATOR ","»«c.d.ordinal»«ENDFOR»})
                «ELSEIF e.scond.touch != null»
                sensors.«e.scond.touch.d.toString()»Touch()
                «ELSEIF e.scond.op != null»
@@ -128,17 +122,9 @@ public class Behaviours{
        def CharSequence makeConstants(Robot robot)'''
 package nl.ru.des;
 
-import java.util.Arrays;
-import java.util.List;
-
 public class Constants{
        public final static int speed = «robot.spd»;
        public final static int acceleration = «robot.acc»;
-       «FOR m : robot.mission»
-               «IF !m.se.scond.colors.nullOrEmpty»
-                       public final static List<Integer> colorsToFind = Arrays.asList(new Integer[] {«FOR c : m.se.scond.colors SEPARATOR ","»«c.d.ordinal»«ENDFOR»});
-               «ENDIF»
-       «ENDFOR»
 }'''
        
 }
\ No newline at end of file
index ae4ba7a..96fe5ac 100644 (file)
@@ -3,9 +3,6 @@
  */
 package robots.missions.validation
 
-import org.eclipse.xtext.validation.Check
-import robots.missions.taskDSL.Mission
-
 //import org.eclipse.xtext.validation.Check
 /**
  * This class contains custom validation rules. 
@@ -24,7 +21,7 @@ class TaskDSLValidator extends AbstractTaskDSLValidator {
 //                                     INVALID_NAME)
 //             }
 //     }
-       @Check
+/*     @Check
        def checkStoppingExpressionShouldHaveOneBehaviourToCollectColor(Mission mission) {
                var isCollectColorExist = false;
                if (!mission.se.scond.colors.nullOrEmpty) {
@@ -38,5 +35,5 @@ class TaskDSLValidator extends AbstractTaskDSLValidator {
                if (!isCollectColorExist) {
                        error("Must specify one behaviour to collect color", null)
                }
-       }
+       }*/
 }