left motor forward
right motor forward
wait forever
- collect color
Behaviour AvoidLowLeftObjects
take control: Touched on left
action:
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) {
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
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;
- }
}
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()];
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(){
}
public int color(){
- if(System.currentTimeMillis()-colorTime>DELAY){
- color.fetchSample(colorSamples, 0);
- colorTime = System.currentTimeMillis();
- }
return (int)colorSamples[0];
}
}
return rightTouchSamples[0]==1;
}
+
+ public boolean collected(int[] is) {
+ return colorsCollected.containsAll(Arrays.asList(is));
+ }
}
\ No newline at end of file
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();
}
}
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)? |
«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)»;
}
«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(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»
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 robots.missions.taskDSL.Mission
-
//import org.eclipse.xtext.validation.Check
/**
* This class contains custom validation rules.
// INVALID_NAME)
// }
// }
- @Check
+/* @Check
def checkStoppingExpressionShouldHaveOneBehaviourToCollectColor(Mission mission) {
var isCollectColorExist = false;
if (!mission.se.scond.colors.nullOrEmpty) {
if (!isCollectColorExist) {
error("Must specify one behaviour to collect color", null)
}
- }
+ }*/
}