Behaviour Wander
take control:
action:
- Left motor Forwards
- Right motor Forwards
+ left motor forward
+ right motor forward
wait forever
Behaviour AvoidLowLeftObjects
- take control: Touched on Left
+ take control: Touched on left
action:
- Left motor Backwards
- Right motor Backwards
+ left motor backward
+ right motor backward
wait 250 ms
- Right motor Backwards
- Left motor Forwards
+ right motor backward
+ left motor forward
wait 250 ms
Behaviour AvoidLowRightObjects
- take control: Touched on Right
+ take control: Touched on right
action:
- Left motor Backwards
- Right motor Backwards
+ left motor backward
+ right motor backward
wait 250 ms
- Right motor Forwards
- Left motor Backwards
+ right motor forward
+ left motor backward
wait 250 ms
Behaviour AvoidHighObjects
take control: Distance < 50 cm
action:
- Right motor Forwards
+ right motor forward
wait 250 ms
Behaviour StayInLine
take control: (or Color is Black Color is DarkGray)
action:
- Right motor Forwards
- Left motor Backwards
+ right motor forward
+ left motor backward
wait 250 ms
Mission Assignment
using
import lejos.robotics.subsumption.Behavior;
public abstract class BasicBehaviour implements Behavior{
+ protected boolean suppressed;
protected EV3LargeRegulatedMotor leftMotor, rightMotor;
+ protected SensorCollector sensors;
+ protected ColorMemory colors;
+ protected long time;
- public BasicBehaviour(nl.ru.des.Sensors sensors, EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor){
+ public BasicBehaviour(SensorCollector sensors, EV3LargeRegulatedMotor leftMotor,
+ EV3LargeRegulatedMotor rightMotor, ColorMemory colors){
this.leftMotor = leftMotor;
this.rightMotor = rightMotor;
+ this.colors = colors;
+ this.sensors = sensors;
+ }
+
+ protected void reset(){
+ rightMotor.setSpeed(Constants.speed);
+ rightMotor.setAcceleration(Constants.acceleration);
+ leftMotor.setSpeed(Constants.speed);
+ leftMotor.setAcceleration(Constants.acceleration);
+ rightMotor.stop(true);
+ leftMotor.stop(true);
}
@Override
- public boolean takeControl() {
- return true;
+ public void action() {
+ suppressed = false;
}
@Override
- public void suppress() {}
+ public void suppress() {
+ suppressed = true;
+ }
+
+ @Override
+ public boolean takeControl() {
+ return true;
+ }
}
--- /dev/null
+package nl.ru.des;
+
+public class ColorMemory {
+
+}
import lejos.hardware.sensor.EV3TouchSensor;
import lejos.hardware.sensor.EV3UltrasonicSensor;
import lejos.robotics.SampleProvider;
-import nl.ru.des.dsl.Constants;
public class MarsRover {
public static final float SAMPLERATE = 100;
LCDPrinter.print("Loading motors...");
EV3LargeRegulatedMotor rightMotor = new EV3LargeRegulatedMotor(MotorPort.D);
EV3LargeRegulatedMotor leftMotor = new EV3LargeRegulatedMotor(MotorPort.A);
- leftMotor.setSpeed(Constants.defaultSpeed);
- rightMotor.setSpeed(Constants.defaultSpeed);
- rightMotor.setAcceleration(Constants.defaultAcceleration);
- leftMotor.setAcceleration(Constants.defaultAcceleration);
+ leftMotor.setSpeed(Constants.speed);
+ rightMotor.setSpeed(Constants.speed);
+ rightMotor.setAcceleration(Constants.acceleration);
+ leftMotor.setAcceleration(Constants.acceleration);
LCDPrinter.print("Loading touch sensors...");
SampleProvider leftTouch = new EV3TouchSensor(brick.getPort("S1")).getTouchMode();
SampleProvider ultraSonic = new EV3UltrasonicSensor(brick.getPort("S3")).getDistanceMode();
LCDPrinter.print("Initializing behaviours...");
- Sensors sensors = new Sensors(ultraSonic, color, leftTouch, rightTouch);
+ SensorCollector sensors = new SensorCollector(ultraSonic, color, leftTouch, rightTouch);
}
}
\ No newline at end of file
+++ /dev/null
-package nl.ru.des;
-
-import lejos.hardware.motor.EV3LargeRegulatedMotor;
-import nl.ru.des.Sensors;
-
-public abstract class ReactiveBehaviour extends BasicBehaviour {
- protected boolean suppressed;
- protected Sensors sensors;
-
- public ReactiveBehaviour(Sensors sensors, EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor) {
- super(sensors, leftMotor, rightMotor);
- this.sensors = sensors;
- }
-
- @Override
- public void action() {
- suppressed = false;
- }
-
- @Override
- public void suppress() {
- suppressed = true;
- }
-}
\ No newline at end of file
import lejos.robotics.SampleProvider;
-public class Sensors{
+public class SensorCollector{
public static final int DELAY = 50;
private SampleProvider ultrasone, color, leftTouch, rightTouch;
private float[] ultrasoneSamples, colorSamples, leftTouchSamples, rightTouchSamples;
private long ultrasoneTime, colorTime, leftTouchTime, rightTouchTime;
- public Sensors(SampleProvider ultrasone,
+ public SensorCollector(SampleProvider ultrasone,
SampleProvider color,
SampleProvider leftTouch,
SampleProvider rightTouch){
rightTouchTime = System.currentTimeMillis();
}
- public float getDistance(){
+ public float distance(){
if(System.currentTimeMillis()-ultrasoneTime>DELAY){
ultrasone.fetchSample(ultrasoneSamples, 0);
ultrasoneTime = System.currentTimeMillis();
return ultrasoneSamples[0];
}
- public int getColor(){
+ public int color(){
if(System.currentTimeMillis()-colorTime>DELAY){
color.fetchSample(colorSamples, 0);
colorTime = System.currentTimeMillis();
return (int)colorSamples[0];
}
- public boolean getLeftTouch(){
+ public boolean leftTouch(){
if(System.currentTimeMillis()-leftTouchTime>DELAY){
leftTouch.fetchSample(leftTouchSamples, 0);
leftTouchTime = System.currentTimeMillis();
return leftTouchSamples[0]==1;
}
- public boolean getRightTouch(){
+ public boolean rightTouch(){
if(System.currentTimeMillis()-rightTouchTime>DELAY){
rightTouch.fetchSample(rightTouchSamples, 0);
rightTouchTime = System.currentTimeMillis();
+++ /dev/null
-module robots.missions.GenerateTaskDSL
-
-import org.eclipse.emf.mwe.utils.*
-import org.eclipse.xtext.generator.*
-import org.eclipse.xtext.ui.generator.*
-
-var grammarURI = "classpath:/robots/missions/TaskDSL.xtext"
-var fileExtensions = "tdsl"
-var projectName = "desdsl"
-var runtimeProject = "../${projectName}"
-var generateXtendStub = true
-var encoding = "UTF-8"
-
-Workflow {
- bean = StandaloneSetup {
- scanClassPath = true
- platformUri = "${runtimeProject}/.."
- // The following two lines can be removed, if Xbase is not used.
- registerGeneratedEPackage = "org.eclipse.xtext.xbase.XbasePackage"
- registerGenModelFile = "platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel"
- }
-
- component = DirectoryCleaner {
- directory = "${runtimeProject}/src-gen"
- }
-
- component = DirectoryCleaner {
- directory = "${runtimeProject}/model/generated"
- }
-
- component = DirectoryCleaner {
- directory = "${runtimeProject}.ui/src-gen"
- }
-
- component = DirectoryCleaner {
- directory = "${runtimeProject}.tests/src-gen"
- }
-
- component = Generator {
- pathRtProject = runtimeProject
- pathUiProject = "${runtimeProject}.ui"
- pathTestProject = "${runtimeProject}.tests"
- projectNameRt = projectName
- projectNameUi = "${projectName}.ui"
- encoding = encoding
- language = auto-inject {
- uri = grammarURI
-
- // Java API to access grammar elements (required by several other fragments)
- fragment = grammarAccess.GrammarAccessFragment auto-inject {}
-
- // generates Java API for the generated EPackages
- fragment = ecore.EMFGeneratorFragment auto-inject {}
-
- // the old serialization component
- // fragment = parseTreeConstructor.ParseTreeConstructorFragment auto-inject {}
-
- // serializer 2.0
- fragment = serializer.SerializerFragment auto-inject {
- generateStub = false
- }
-
- // a custom ResourceFactory for use with EMF
- fragment = resourceFactory.ResourceFactoryFragment auto-inject {}
-
- // The antlr parser generator fragment.
- fragment = parser.antlr.XtextAntlrGeneratorFragment auto-inject {
- // options = {
- // backtrack = true
- // }
- }
-
- // Xtend-based API for validation
- fragment = validation.ValidatorFragment auto-inject {
- // composedCheck = "org.eclipse.xtext.validation.ImportUriValidator"
- // composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
- }
-
- // old scoping and exporting API
- // fragment = scoping.ImportURIScopingFragment auto-inject {}
- // fragment = exporting.SimpleNamesFragment auto-inject {}
-
- // scoping and exporting API
- fragment = scoping.ImportNamespacesScopingFragment auto-inject {}
- fragment = exporting.QualifiedNamesFragment auto-inject {}
- fragment = builder.BuilderIntegrationFragment auto-inject {}
-
- // generator API
- fragment = generator.GeneratorFragment auto-inject {}
-
- // formatter API
- fragment = formatting.FormatterFragment auto-inject {}
-
- // labeling API
- fragment = labeling.LabelProviderFragment auto-inject {}
-
- // outline API
- fragment = outline.OutlineTreeProviderFragment auto-inject {}
- fragment = outline.QuickOutlineFragment auto-inject {}
-
- // quickfix API
- fragment = quickfix.QuickfixProviderFragment auto-inject {}
-
- // content assist API
- fragment = contentAssist.ContentAssistFragment auto-inject {}
-
- // generates a more lightweight Antlr parser and lexer tailored for content assist
- fragment = parser.antlr.XtextAntlrUiGeneratorFragment auto-inject {}
-
- // generates junit test support classes into Generator#pathTestProject
- fragment = junit.Junit4Fragment auto-inject {}
-
- // rename refactoring
- fragment = refactoring.RefactorElementNameFragment auto-inject {}
-
- // provides the necessary bindings for java types integration
- fragment = types.TypesGeneratorFragment auto-inject {}
-
- // generates the required bindings only if the grammar inherits from Xbase
- fragment = xbase.XbaseGeneratorFragment auto-inject {}
-
- // generates the required bindings only if the grammar inherits from Xtype
- fragment = xbase.XtypeGeneratorFragment auto-inject {}
-
- // provides a preference page for template proposals
- fragment = templates.CodetemplatesGeneratorFragment auto-inject {}
-
- // provides a compare view
- fragment = compare.CompareFragment auto-inject {}
- }
- }
-}
-
+++ /dev/null
-grammar robots.missions.TaskDSL with org.eclipse.xtext.common.Terminals
-
-generate taskDSL "http://www.missions.robots/TaskDSL"
-
-Robot:
- 'Name' name=ID
- 'Acceleration' acc=INT
- 'Speed' spd=INT
- 'LeftMotor plugged in' leftMotorPort=MotorPort
- 'RightMotor plugged in' righMotorPort=MotorPort
- ('ColorSensor plugged in' colorSensorPort=SensorPort)?
- ('TouchSensor plugged in' leftTouchSensorPort=SensorPort 'and' rightTouchSensorPort=SensorPort)?
- ('UltrasoneSensor plugged in' ultrasoneSensorPort=SensorPort)?
- ('Mission:' mission+=Mission)+;
-
-Mission:
- behaviours+=Behaviour+ 'stops when' stoppingCondition=StoppingCondition
-;
-
-StoppingCondition:
- SensorLimitReached | CollectedColors;
-
-CollectedColors:
- 'Collected Colors:' colors+=ColorValue+
-;
-
-SensorLimitReached:
- TouchLimit | ColorLimit | DistanceLimit;
-
-ColorLimit:
- 'ColorLimit: ' lim=ColorValue
-;
-
-TouchLimit:
- 'TouchStatus: ' lim=Touch
-;
-
-Touch:
- 'Touch' | 'NoTouch'
-;
-
-DistanceLimit:
- 'Distance:' dist=STRING 'meter'
-;
-
-ColorValue:
- 'Black' | 'Blue' | 'Brown' | 'Cyan' | 'DarkGray' | 'Gray' | 'Green' | 'LightGray' | 'Magenta' | 'Orange' | 'Pink' | 'Red' | 'White' | 'Yellow'
-;
-
-Behaviour:
- 'Behaviour'
- 'Name:' name=ID
- 'TakeControl:' takeControl=SensorLimitReached?
- 'Action:' action=Action
-;
-
-Action:
- 'Avoid' | 'Wander' | 'Nothing'
-;
-
-SensorPort:
- 'S1' | 'S2' | 'S3' | 'S4';
-
-MotorPort:
- 'A' | 'B' | 'C' | 'D';
\ No newline at end of file
+++ /dev/null
-/*
- * generated by Xtext
- */
-package robots.missions;
-
-/**
- * Use this class to register components to be used at runtime / without the Equinox extension registry.
- */
-public class TaskDSLRuntimeModule extends robots.missions.AbstractTaskDSLRuntimeModule {
-
-}
+++ /dev/null
-/*
- * generated by Xtext
- */
-package robots.missions;
-
-/**
- * Initialization support for running Xtext languages
- * without equinox extension registry
- */
-public class TaskDSLStandaloneSetup extends TaskDSLStandaloneSetupGenerated{
-
- public static void doSetup() {
- new TaskDSLStandaloneSetup().createInjectorAndDoEMFRegistration();
- }
-}
-
+++ /dev/null
-/*
- * generated by Xtext
- */
-package robots.missions.formatting
-
-import org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter
-import org.eclipse.xtext.formatting.impl.FormattingConfig
-// import com.google.inject.Inject;
-// import robots.missions.services.TaskDSLGrammarAccess
-
-/**
- * This class contains custom formatting declarations.
- *
- * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#formatting
- * on how and when to use it.
- *
- * Also see {@link org.eclipse.xtext.xtext.XtextFormattingTokenSerializer} as an example
- */
-class TaskDSLFormatter extends AbstractDeclarativeFormatter {
-
-// @Inject extension TaskDSLGrammarAccess
-
- override protected void configureFormatting(FormattingConfig c) {
-// It's usually a good idea to activate the following three statements.
-// They will add and preserve newlines around comments
-// c.setLinewrap(0, 1, 2).before(SL_COMMENTRule)
-// c.setLinewrap(0, 1, 2).before(ML_COMMENTRule)
-// c.setLinewrap(0, 1, 1).after(ML_COMMENTRule)
- }
-}
+++ /dev/null
-/*
- * generated by Xtext
- */
-package robots.missions.generator
-
-import org.eclipse.emf.ecore.resource.Resource
-import org.eclipse.xtext.generator.IGenerator
-import org.eclipse.xtext.generator.IFileSystemAccess
-
-/**
- * Generates code from your model files on save.
- *
- * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
- */
-class TaskDSLGenerator implements IGenerator {
-
- override void doGenerate(Resource resource, IFileSystemAccess fsa) {
-// fsa.generateFile('greetings.txt', 'People to greet: ' +
-// resource.allContents
-// .filter(typeof(Greeting))
-// .map[name]
-// .join(', '))
- }
-}
+++ /dev/null
-/*
- * generated by Xtext
- */
-package robots.missions.scoping
-
-/**
- * This class contains custom scoping description.
- *
- * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#scoping
- * on how and when to use it.
- *
- */
-class TaskDSLScopeProvider extends org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider {
-
-}
+++ /dev/null
-/*
- * generated by Xtext
- */
-package robots.missions.validation
-
-//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 {
-
-// public static val INVALID_NAME = 'invalidName'
-//
-// @Check
-// def checkGreetingStartsWithCapital(Greeting greeting) {
-// if (!Character.isUpperCase(greeting.name.charAt(0))) {
-// warning('Name should start with a capital',
-// MyDslPackage.Literals.GREETING__NAME,
-// INVALID_NAME)
-// }
-// }
-}
Mission: 'Mission' name=ID 'using' behaviours+=[Behaviour]+ 'and stops when' stoppingExpression=StoppingExpression;
StoppingExpression:
- '(' op=Operator s+=StoppingExpression s+=StoppingExpression* ')' |
+ '(' op=Operator s+=StoppingExpression s+=StoppingExpression+ ')' |
scond=StoppingCondition
;
Time: time=INT 'ms' | {Time} 'forever';
-enum Direction: BACKWARDS = 'Backwards' | FORWARDS = 'Forwards';
-enum Operator: AND = 'and' | OR = 'or';
-enum Comparison: GE='>' | LE='<';
-enum LeftRight: LEFT='Left' | RIGHT='Right';
-enum Color:
+Direction: d=DirectionE;
+enum DirectionE: BACKWARDS = 'backward' | FORWARDS = 'forward';
+Operator: d=OperatorE;
+enum OperatorE: AND = 'and' | OR = 'or';
+Comparison: d=ComparisonE;
+enum ComparisonE: GE='>' | LE='<';
+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' |
*/
package robots.missions.generator
+import org.eclipse.emf.common.util.EList
import org.eclipse.emf.ecore.resource.Resource
-import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
+import org.eclipse.xtext.generator.IGenerator
+import robots.missions.taskDSL.Behaviour
+import robots.missions.taskDSL.OperatorE
+import robots.missions.taskDSL.Robot
+import robots.missions.taskDSL.StoppingExpression
/**
* Generates code from your model files on save.
class TaskDSLGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
-// fsa.generateFile('greetings.txt', 'People to greet: ' +
-// resource.allContents
-// .filter(typeof(Greeting))
-// .map[name]
-// .join(', '))
+ var root = resource.allContents.head as Robot;
+ if(root != null){
+ fsa.generateFile("nl/ru/des/Constants.java", makeConstants(root))
+ fsa.generateFile("nl/ru/des/Behaviours.java", makeBehaviours(root.behaviour))
+ }
+ }
+
+ def makeBehaviours(EList<Behaviour> list)'''
+package nl.ru.des;
+
+import lejos.hardware.motor.EV3LargeRegulatedMotor;
+
+public class Behaviours{
+ «FOR b : list»
+ public static class «b.name»Behaviour extends BasicBehaviour {
+ public «b.name»Behaviour(SensorCollector sensors, EV3LargeRegulatedMotor rightMotor,
+ EV3LargeRegulatedMotor leftMotor, ColorMemory colors){
+ super(sensors, rightMotor, leftMotor, colors);
+ }
+ «IF b.tc != null»
+ @Override public boolean takeControl(){
+ return «printExpression(b.tc)»;
+ }
+ «ENDIF»
+
+ @Override public void action(){
+ super.action();
+ «FOR a : b.actions»
+ «IF a.whichMotor != null»
+ «IF a.acc > 0»
+ «a.whichMotor.d.toString()»Motor.setAcceleration(«a.acc»);
+ «a.whichMotor.d.toString()»Motor.setSpeed(«a.spd»);
+ «ENDIF»
+ «a.whichMotor.d.toString()»Motor.«a.dir.d.toString()»();
+ «ELSE»
+ time = System.currentTimeMillis();
+ while(!suppressed«IF a.time.time > 0» && System.currentTimeMillis()-time>«a.time.time»«ENDIF»){
+ Thread.yield();
+ }
+ «ENDIF»
+ «ENDFOR»
+ reset();
+ }
}
+ «ENDFOR»
+}
+ '''
+
+ 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»})
+ «ELSEIF e.scond.touch != null»
+ sensors.«e.scond.touch.d.toString()»Touch()
+ «ELSEIF e.scond.op != null»
+ sensors.distance() «e.scond.op.d.toString()» «e.scond.dist»
+ «ELSEIF e.scond.color != null»
+ sensors.color() == «e.scond.color.d.ordinal»
+ «ENDIF»
+ «ELSE»
+ «IF e.op.d.equals(OperatorE.AND)»
+ «FOR ex : e.s BEFORE "(" SEPARATOR "&&" AFTER ")"»«printExpression(ex)»«ENDFOR»
+ «ELSE»
+ «FOR ex : e.s BEFORE "(" SEPARATOR "&&" AFTER ")"»«printExpression(ex)»«ENDFOR»
+ «ENDIF»
+ «ENDIF»
+ '''
+
+ def CharSequence makeConstants(Robot robot)'''
+package nl.ru.des;
+
+public class Constants{
+ public final static int speed = «robot.spd»;
+ public final static int acceleration = «robot.acc»;
+}'''
+
}