Implementing colors collection and mission exit
authorNatanael Adityasatria <nata.adit@gmail.com>
Fri, 20 Nov 2015 21:55:27 +0000 (22:55 +0100)
committerNatanael Adityasatria <nata.adit@gmail.com>
Fri, 20 Nov 2015 21:55:27 +0000 (22:55 +0100)
-Update the order of the Color Enum, so the value will be the same as
the integer value as stated in Lejos's Color documentation

dsl/runtime/specs/spec1.tdsl
dsl/runtime/src/nl/ru/des/ColorMemory.java
dsl/runtime/src/nl/ru/des/MarsRover.java
dsl/runtime/src/nl/ru/des/Mission.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 45e74a3..ccd0607 100644 (file)
@@ -7,6 +7,7 @@ Behaviour Wander
                left motor forward
                right motor forward
                wait forever
+       collect color
 Behaviour AvoidLowLeftObjects
        take control: Touched on left
        action:
@@ -44,4 +45,4 @@ using
        AvoidHighObjects
        StayInLine
 and stops when
-       Collected at least Blue Green Yellow
+       Collected at least Blue Green Yellow
\ No newline at end of file
index cec193e..61b0f2b 100644 (file)
@@ -1,14 +1,26 @@
 package nl.ru.des;
 
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
 import lejos.robotics.SampleProvider;
 
 public class ColorMemory {
 
+       private Set<Integer> colors;
+       
        public ColorMemory(SampleProvider color) {
+               colors = new HashSet<Integer>();
        }
 
-       public boolean containsAll(int[] is) {
-               return false;
+       public void addColor(int current) {
+               if(!colors.contains(current)){
+                       colors.add(current);
+               }
+       }
+       public boolean containsAll(List<Integer> is) {
+               return colors.containsAll(is);
        }
 
 }
index c4b3ddf..41a5f90 100644 (file)
@@ -1,5 +1,7 @@
 package nl.ru.des;
 
+import java.util.LinkedList;
+
 import lejos.hardware.ev3.EV3;
 import lejos.hardware.ev3.LocalEV3;
 import lejos.hardware.lcd.Font;
@@ -14,6 +16,7 @@ import lejos.robotics.subsumption.Arbitrator;
 
 public class MarsRover {
        public static final float SAMPLERATE = 100;
+       public static LinkedList<Mission> missions = new LinkedList<Mission>();
        
        @SuppressWarnings("resource")
        public static void main(String[] args) {
@@ -51,10 +54,19 @@ public class MarsRover {
                ColorMemory colorMemory = new ColorMemory(color);
                
                Arbitrator a;
-               for(Mission m : Missions.getMissions(sensors, rightMotor, leftMotor, colorMemory)){
+               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 c80678e..41b7c03 100644 (file)
@@ -1,13 +1,19 @@
 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 85aa07b..9b34b95 100644 (file)
@@ -4,12 +4,16 @@ import lejos.hardware.motor.EV3LargeRegulatedMotor;
 
 public class ShutdownBehaviour extends BasicBehaviour{
 
+       private String missionName;
+       
        public ShutdownBehaviour(SensorCollector sensors, EV3LargeRegulatedMotor leftMotor,
-                       EV3LargeRegulatedMotor rightMotor, ColorMemory colors) {
+                       EV3LargeRegulatedMotor rightMotor, ColorMemory colors, String missionName) {
                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);
        }
 }
index 9c14fa3..456ac4c 100644 (file)
@@ -13,8 +13,7 @@ Mission: 'Mission' name=ID 'using' behaviours+=[Behaviour]+ 'and stops when' se=
 
 StoppingExpression:
        '(' op=Operator s+=StoppingExpression s+=StoppingExpression+ ')' |
-       scond=StoppingCondition
-;
+       scond=StoppingCondition;
 
 StoppingCondition:
        'Collected at least' colors+=Color+ |
@@ -24,7 +23,7 @@ StoppingCondition:
 
 Behaviour: 'Behaviour' name=ID 
        'take control:' tc=StoppingExpression?
-       'action:' actions+=Action+;
+       'action:' actions+=Action+ (clcColor='collect color')?;
 
 Action:
        whichMotor=LeftRight 'motor' dir=Direction ('with speed' spd=INT 'acceleration' acc=INT)? |
@@ -42,7 +41,7 @@ LeftRight: d=LeftRightE;
 enum LeftRightE: LEFT='left' | RIGHT='right';
 Color: d=ColorE;
 enum ColorE:
-       BLACK='Black' | BLUE='Blue' | BROWN='Brown' | CYAN='Cyan' |
-       DARKGRAY='DarkGray' | GRAY='Gray' | GREEN='Green' | 
-       LIGHTGRAY='LightGray' | MAGENTA='Magenta' | ORANGE='Orange' | PINK='Pink' |
-       RED='Red' | WHITE='White' | YELLOW='Yellow';
\ No newline at end of file
+       RED='Red' | GREEN='Green' | BLUE='Blue' | YELLOW='Yellow' | 
+       MAGENTA='Magenta'| ORANGE='Orange'|     WHITE='White' | BLACK='Black' | 
+       PINK='Pink' | GRAY='Gray' | LIGHTGRAY='LightGray' |
+       DARKGRAY='DarkGray' | CYAN='Cyan' | BROWN='Brown';
\ No newline at end of file
index 9aecf7e..58e115f 100644 (file)
@@ -8,10 +8,10 @@ import org.eclipse.emf.ecore.resource.Resource
 import org.eclipse.xtext.generator.IFileSystemAccess
 import org.eclipse.xtext.generator.IGenerator
 import robots.missions.taskDSL.Behaviour
+import robots.missions.taskDSL.Mission
 import robots.missions.taskDSL.OperatorE
 import robots.missions.taskDSL.Robot
 import robots.missions.taskDSL.StoppingExpression
-import robots.missions.taskDSL.Mission
 
 /**
  * Generates code from your model files on save.
@@ -33,14 +33,13 @@ class TaskDSLGenerator implements IGenerator {
 package nl.ru.des;
 
 import java.util.LinkedList;
-import java.util.List;
 
 import lejos.hardware.motor.EV3LargeRegulatedMotor;
 import lejos.robotics.subsumption.Behavior;
 import nl.ru.des.Behaviours;
 
 public class Missions{
-       public static List<Mission> getMissions(SensorCollector sensors, EV3LargeRegulatedMotor rightMotor,
+       public static LinkedList<Mission> getMissions(SensorCollector sensors, EV3LargeRegulatedMotor rightMotor,
                        EV3LargeRegulatedMotor leftMotor, ColorMemory colors){
                LinkedList<Mission> missions = new LinkedList<Mission>();
                «FOR m : list»
@@ -48,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){
+                       new ShutdownBehaviour(sensors, rightMotor, leftMotor, colors, "«m.name»"){
                                @Override public boolean takeControl(){
                                        return «printExpression(m.se)»;
                                }
@@ -89,6 +88,12 @@ 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»
@@ -103,7 +108,7 @@ public class Behaviours{
        def CharSequence printExpression(StoppingExpression e)'''
        «IF e.scond != null»
                «IF !e.scond.colors.nullOrEmpty»
-               colors.containsAll(new int[]{«FOR c : e.scond.colors SEPARATOR ","»«c.d.ordinal»«ENDFOR»})
+               colors.containsAll(Constants.colorsToFind)
                «ELSEIF e.scond.touch != null»
                sensors.«e.scond.touch.d.toString()»Touch()
                «ELSEIF e.scond.op != null»
@@ -123,9 +128,17 @@ 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 67fdc6e..ae4ba7a 100644 (file)
@@ -3,11 +3,13 @@
  */
 package robots.missions.validation
 
-//import org.eclipse.xtext.validation.Check
+import org.eclipse.xtext.validation.Check
+import robots.missions.taskDSL.Mission
 
+//import org.eclipse.xtext.validation.Check
 /**
  * This class contains custom validation rules. 
- *
+ * 
  * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation
  */
 class TaskDSLValidator extends AbstractTaskDSLValidator {
@@ -22,4 +24,19 @@ class TaskDSLValidator extends AbstractTaskDSLValidator {
 //                                     INVALID_NAME)
 //             }
 //     }
+       @Check
+       def checkStoppingExpressionShouldHaveOneBehaviourToCollectColor(Mission mission) {
+               var isCollectColorExist = false;
+               if (!mission.se.scond.colors.nullOrEmpty) {
+                       var blist = mission.behaviours;
+                       for (var j = 0; j < blist.size; j++) {
+                               if (!blist.get(j).clcColor.nullOrEmpty) {
+                                       isCollectColorExist = true;
+                               }
+                       }
+               }
+               if (!isCollectColorExist) {
+                       error("Must specify one behaviour to collect color", null)
+               }
+       }
 }