1   import interfaces.Controller;
2   import interfaces.AbstractRobot;
3   
4   
5   /**
6   * This sample program rocks the robot, meaning it alternates between moving the
7   * robot forwards and backwards at the execution of a timer. Also the light sensor
8   * reading of LEFT SENSOR or S1 is displayed on the LCD whenever a light change is
9   * detected.
10  *
11  * @author  Simon Zienkiewicz
12  */
13  public class ZincRocker extends Controller 
14  {
15      
16      private AbstractRobot zincBot;
17      private boolean running;
18      private int[] sensors={Controller.SENSOR_TYPE_LIGHT,Controller.SENSOR_TYPE_TOUCH,Controller.SENSOR_TYPE_LIGHT};
19      private boolean on = false;
20      int timer1;
21      boolean var = false;
22      
23      /** Creates a new instance of ZincRocker */
24      public ZincRocker()
25      {
26      }
27      
28      /** Initialises controller. It should be noted that this method will only
29      * ever be called once, whereas run() can be called many times, so any
30      * variables or data structures etc. that are meant to persist in between
31      * stops and starts of the controller's thread, or that should only be
32      * initialised once should be set up from within this method, not in run().
33      *
34      * @param r the AbstractRobot associated with this controller
35      *
36      */
37      public void initController(AbstractRobot r) 
38      {
39          zincBot = r;
40          
41          //creates a timer with a delay of 1000 milli seconds
42          timer1 = zincBot.createTimer(1000);
43      }
44      
45      /** Returns the AbstractRobot associated with this controller
46      *
47      * @return the AbstractRobot
48      *
49      */
50      public AbstractRobot getRobot() 
51      {
52          return zincBot;
53      }
54      
55      /** Returns an array of the sensors used by this controller, and the type
56      * of sensor required. This type must be one of the sensor type constants
57      * declared above. The index of the array is used to establish which sensor
58      * is being defined. e.g. if array[0] is SENSOR_TYPE_TOUCH then sensor 1
59      * will be set to a touch sensor. The sensors array *must* be initialised
60      * with the correct values from the outset, and must not be set in
61      * initController(), or any other method. (see example Controllers for
62      * working examples)
63      *
64      * @return the sensor array
65      *
66      */
67      public int[] getSensors() 
68      {
69          return sensors;
70      }
71      
72      /** Stops this controller's thread running, i.e. must allow run() to return
73      * as quickly as possible. However it is acceptable for this method to do
74      * some housekeeping before stopping the controller, e.g. save some internal
75      * data structure to a file.
76      *
77      */
78      public void halt() 
79      {
80          running = false;
81      }
82      
83      /** This method is called whenever a LIGHT SENSOR detects change from its previous
84      * value.
85      *
86      * @param sensorNumber, this is the LIGHT SENSOR number that called this method
87      */
88      public void lightSensorListener(int sensorNumber)
89      {
90          if(on){
91              
92              //
93              //when LIGHT SENSOR, S1 detects a change this is called
94              //
95              if(sensorNumber == AbstractRobot.S1)
96              {
97                  
98                  //display the colour reading of the sensor on the LCD
99                  zincBot.displayNumberLCD(zincBot.getSensor1());
100                 
101             }
102             
103             //
104             //when LIGHT SENSOR, S3 detects a change this is called
105             //
106             else if(sensorNumber == AbstractRobot.S3)
107             {
108             
109             }
110         
111         }
112     }
113     
114     /** This method is called whenever a TOUCH SENSOR detects change from its previous
115     * value.
116     *
117     * @param sensorNumber, this is the TOUCH SENSOR number that called this method
118     */
119     public void touchSensorListener(int sensorNumber)
120     {
121         if(on)
122         {
123             //
124             //when TOUCH SENSOR, S2 detects a change this is called
125             //
126         }
127       
128     }
129     
130     /**
131     * Starts this controller's thread running. The 'real' controller 
132     * functionality should be started from here.
133     */
134     public void run()
135     {
136         running = true;
137         
138         //activates all of the sensore S1, S2, S3
139         zincBot.activate();
140         
141         //prepares the sensor events for accepting events
142         on = true;
143         
144         go();
145     }
146     
147     /** The true body of the code. Your code will likely go in this block.
148     *
149     */
150     public void go()
151     {
152         
153         //startes the created timer
154         zincBot.startTimer(timer1);
155     
156     }
157    
158     
159     /** Whenever a timer elapses this method is called and the timer which 
160     * elapsed is passed as a parameter
161     *
162     * @param elapsedTimer, the timer which elapsed
163     */
164     public void setTimerExecution(int elapsedTimer) 
165     {
166        if(running)
167         {
168             if(elapsedTimer == timer1)
169             {
170                 //stop the timer
171                 zincBot.stopTimer(timer1);
172                 
173                 //begin desired execution of the rocking
174                 if(var)
175                 {
176                     //the robot moves forward
177                     zincBot.forward();
178                 }
179                 
180                 else
181                 {
182                     //the robot moves backwards
183                     zincBot.backward();
184                 }
185 
186                 var = !var;
187                 
188                 //start the timer once again
189                 zincBot.startTimer(timer1);
190             }
191         }
192         
193         else
194         {
195             
196             //stop all the active timers
197             zincBot.stopAllTimers();
198         }
199         
200     }
201     
202 }
203