left motor forward
right motor forward
wait forever
+ collect color
Behaviour AvoidLowLeftObjects
take control: Touched on left
action:
AvoidHighObjects
StayInLine
and stops when
- Collected at least Blue Green Yellow
+ Collected at least Blue Green Yellow
\ No newline at end of file
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);
}
}
package nl.ru.des;
+import java.util.LinkedList;
+
import lejos.hardware.ev3.EV3;
import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.lcd.Font;
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) {
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
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;
+ }
}
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);
}
}
StoppingExpression:
'(' op=Operator s+=StoppingExpression s+=StoppingExpression+ ')' |
- scond=StoppingCondition
-;
+ scond=StoppingCondition;
StoppingCondition:
'Collected at least' colors+=Color+ |
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)? |
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
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.
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»
«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)»;
}
«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»
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»
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
*/
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 {
// 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)
+ }
+ }
}