From 55df69a5d4941d45bd3d860e6cea9b1357e09f98 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Sat, 21 Nov 2015 10:52:39 +0100 Subject: [PATCH] update with colors etc, we now need to test it on physical robots --- dsl/runtime/specs/spec1.tdsl | 1 - dsl/runtime/src/nl/ru/des/MarsRover.java | 12 +----- dsl/runtime/src/nl/ru/des/Mission.java | 6 --- .../src/nl/ru/des/SensorCollector.java | 37 ++++++++++++++----- .../src/nl/ru/des/ShutdownBehaviour.java | 10 ++--- dsl/xtend/src/robots/missions/TaskDSL.xtext | 2 +- .../missions/generator/TaskDSLGenerator.xtend | 18 +-------- .../validation/TaskDSLValidator.xtend | 7 +--- 8 files changed, 37 insertions(+), 56 deletions(-) diff --git a/dsl/runtime/specs/spec1.tdsl b/dsl/runtime/specs/spec1.tdsl index ccd0607..b1d6f6a 100644 --- a/dsl/runtime/specs/spec1.tdsl +++ b/dsl/runtime/specs/spec1.tdsl @@ -7,7 +7,6 @@ Behaviour Wander left motor forward right motor forward wait forever - collect color Behaviour AvoidLowLeftObjects take control: Touched on left action: diff --git a/dsl/runtime/src/nl/ru/des/MarsRover.java b/dsl/runtime/src/nl/ru/des/MarsRover.java index 41a5f90..8561c0a 100644 --- a/dsl/runtime/src/nl/ru/des/MarsRover.java +++ b/dsl/runtime/src/nl/ru/des/MarsRover.java @@ -16,7 +16,7 @@ import lejos.robotics.subsumption.Arbitrator; public class MarsRover { public static final float SAMPLERATE = 100; - public static LinkedList missions = new LinkedList(); + 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 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 diff --git a/dsl/runtime/src/nl/ru/des/Mission.java b/dsl/runtime/src/nl/ru/des/Mission.java index 41b7c03..c80678e 100644 --- a/dsl/runtime/src/nl/ru/des/Mission.java +++ b/dsl/runtime/src/nl/ru/des/Mission.java @@ -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; - } } diff --git a/dsl/runtime/src/nl/ru/des/SensorCollector.java b/dsl/runtime/src/nl/ru/des/SensorCollector.java index 7695ee0..5210bd1 100644 --- a/dsl/runtime/src/nl/ru/des/SensorCollector.java +++ b/dsl/runtime/src/nl/ru/des/SensorCollector.java @@ -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 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(); + 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 diff --git a/dsl/runtime/src/nl/ru/des/ShutdownBehaviour.java b/dsl/runtime/src/nl/ru/des/ShutdownBehaviour.java index 9b34b95..430b5df 100644 --- a/dsl/runtime/src/nl/ru/des/ShutdownBehaviour.java +++ b/dsl/runtime/src/nl/ru/des/ShutdownBehaviour.java @@ -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(); } } diff --git a/dsl/xtend/src/robots/missions/TaskDSL.xtext b/dsl/xtend/src/robots/missions/TaskDSL.xtext index 456ac4c..7a2de03 100644 --- a/dsl/xtend/src/robots/missions/TaskDSL.xtext +++ b/dsl/xtend/src/robots/missions/TaskDSL.xtext @@ -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)? | diff --git a/dsl/xtend/src/robots/missions/generator/TaskDSLGenerator.xtend b/dsl/xtend/src/robots/missions/generator/TaskDSLGenerator.xtend index 58e115f..d7ee2ed 100644 --- a/dsl/xtend/src/robots/missions/generator/TaskDSLGenerator.xtend +++ b/dsl/xtend/src/robots/missions/generator/TaskDSLGenerator.xtend @@ -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 colorsToFind = Arrays.asList(new Integer[] {«FOR c : m.se.scond.colors SEPARATOR ","»«c.d.ordinal»«ENDFOR»}); - «ENDIF» - «ENDFOR» }''' } \ No newline at end of file diff --git a/dsl/xtend/src/robots/missions/validation/TaskDSLValidator.xtend b/dsl/xtend/src/robots/missions/validation/TaskDSLValidator.xtend index ae4ba7a..96fe5ac 100644 --- a/dsl/xtend/src/robots/missions/validation/TaskDSLValidator.xtend +++ b/dsl/xtend/src/robots/missions/validation/TaskDSLValidator.xtend @@ -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) } - } + }*/ } -- 2.20.1