added gracefull shutdown
[des2015.git] / dsl / xtend / src / robots / missions / generator / TaskDSLGenerator.xtend
index 84a1261..5af781f 100644 (file)
@@ -10,8 +10,8 @@ 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.Robot
 
 /**
  * Generates code from your model files on save.
@@ -27,34 +27,34 @@ class TaskDSLGenerator implements IGenerator {
                        for(Behaviour b : root.behaviour){
                                fsa.generateFile("nl/ru/des/" + b.name + "Behaviour.java", makeBehaviour(b));
                        }
-                       //fsa.generateFile("nl/ru/des/Missions.java", makeMissions(root.mission));
+                       fsa.generateFile("nl/ru/des/Missions.java", makeMissions(root.mission));
                }
        }
        
-       def CharSequence makeMissions(EList<Mission> list)'''
+       def CharSequence makeMissions(EList<Mission> mission)'''
 package nl.ru.des;
 
 import java.util.LinkedList;
 
-import lejos.hardware.motor.EV3LargeRegulatedMotor;
 import lejos.robotics.subsumption.Behavior;
-import nl.ru.des.Behaviours;
+import lejos.robotics.RegulatedMotor;
+
+import nl.ru.des.sensors.SensorCollector;
 
 public class Missions{
-       public static LinkedList<Mission> getMissions(SensorCollector sensors, EV3LargeRegulatedMotor rightMotor,
-                       EV3LargeRegulatedMotor leftMotor, ColorMemory colors){
+       public static LinkedList<Mission> getMissions(final SensorCollector sensors, RegulatedMotor rightMotor,
+                       RegulatedMotor leftMotor, RegulatedMotor measMotor){
                LinkedList<Mission> missions = new LinkedList<Mission>();
-               «FOR m : list»
+               «FOR m : mission SEPARATOR "));" AFTER "));"»
                missions.add(new Mission("«m.name»", new Behavior[]{
-                       «FOR b : m.behaviours SEPARATOR ","»
-                       new Behaviours.«b.name»Behaviour(sensors, rightMotor, leftMotor, colors)
-                       «ENDFOR»,
-                       new ShutdownBehaviour(sensors, rightMotor, leftMotor, colors){
+                       «FOR b : m.behaviours SEPARATOR "," AFTER ","»new «b.name»Behaviour(sensors, rightMotor, leftMotor, measMotor)
+                       «ENDFOR»
+                       new ShutdownBehaviour(){
                                @Override public boolean takeControl(){
-                                       return »;
+                                       return Â«printExpression(m.se)»;
                                }
                        }}
-               «ENDFOR»));
+               «ENDFOR»
                return missions;
        }
 }      
@@ -74,10 +74,11 @@ public class «b.name»Behaviour extends BasicBehaviour {
        «IF b.tc != null»
        @Override public boolean takeControl(){
                return «printExpression(b.tc)»;
-               }
+       }
        «ENDIF»
        
        @Override public void action(){
+               LCDPrinter.print("Start: «b.name»");
                super.action();
                «FOR a : b.actions»
                        «IF a.whichMotor != null»
@@ -86,16 +87,32 @@ public class «b.name»Behaviour extends BasicBehaviour {
                                «a.whichMotor.d.toString()»Motor.setSpeed(«a.spd»);
                                «ENDIF»
                                «a.whichMotor.d.toString()»Motor.«a.moveDir.d.toString()»();
-                       «ELSEIF a.measureWhat != null»
-                       «a.measureWhat.d.toString()»Measure();
-                       «ELSE»
+                       «ELSEIF a.time != null»
                        time = System.currentTimeMillis();
-                       while(!suppressed«IF a.time.time > 0» && System.currentTimeMillis()-time>«a.time.time»«ENDIF»){
+                       while(!suppressed«IF a.time.time > 0» && System.currentTimeMillis()-time<«a.time.time»«ENDIF»){
                                Thread.yield(); 
                        }
+                       «ELSEIF a.turnType != null»
+                               «IF a.acc > 0»
+                                       leftMotor.setAcceleration(«a.acc»);
+                                       leftMotor.setSpeed(«a.spd»);
+                                       rightMotor.setAcceleration(«a.acc»);
+                                       rightMotor.setSpeed(«a.spd»);
+                               «ENDIF»
+                               «IF a.turnType.turnDir != null»
+                                       «a.turnType.turnDir.d.toString()»Turn(«a.turnType.degrees»);
+                               «ELSE»
+                                       turnRandom(«a.turnType.start», «a.turnType.end»);
+                               «ENDIF»
+                       «ELSEIF a.rl != null»
+                       measure«a.rl.d.toString()»();
+                       «ELSE»
+                       if(!suppressed){
+                               sensors.saveVar("«a.varName.toString()»");
+                       }
                        «ENDIF»
                «ENDFOR»
-               reset();
+               LCDPrinter.print("Stop: «b.name»");
        }
 }
        '''
@@ -110,21 +127,26 @@ public class Constants{
        
        def CharSequence printExpression(StoppingExpression e)'''
        «IF e.scond != null»
-               «IF !e.scond.colors.nullOrEmpty»
-               sensors.collected(new int[]{«FOR c : e.scond.colors SEPARATOR ","»«c.d.ordinal»«ENDFOR»})
+               «IF e.scond.varName != null»
+               sensors.collected("«e.scond.varName.toString()»")
                «ELSEIF e.scond.touch != null»
                sensors.«e.scond.touch.d.toString()»Touch()
+               «ELSEIF e.scond.light != null»
+               sensors.«e.scond.light.d.toString()»Light()
                «ELSEIF e.scond.dist != null»
-               sensors.distance() «e.scond.dist.d.toString()» «e.scond.dist»
+               sensors.«e.scond.dist.d.toString()»Distance()
                «ELSEIF e.scond.color != null»
                sensors.color() == «e.scond.color.d.ordinal»
+               «ELSE»
+               false
                «ENDIF»
+       «ELSEIF e.negscond != null»
+               !(«printExpression(e.negscond)»)
        «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»
-       '''
+                       «FOR ex : e.s BEFORE "(" SEPARATOR "||" AFTER ")"»«printExpression(ex)»«ENDFOR»
+               «ENDIF»
+       «ENDIF»'''
 }
\ No newline at end of file