1   import interfaces.Controller;
2   import interfaces.AbstractRobot;
3   
4   import java.lang.*;
5   import java.util.*;
6   
7   /**
8   * This is the hand-coded light seeking controller
9   *
10  * @author Graham Ritchie
11  */
12  public class HandCodedLightSeeker extends Controller
13  {
14      private AbstractRobot robot;
15      private boolean running;
16  
17      private int[] sensors={Controller.SENSOR_TYPE_LIGHT,Controller.SENSOR_TYPE_LIGHT,Controller.SENSOR_TYPE_LIGHT};
18      
19      /************************************************
20                 Methods requird by Controller
21      *************************************************/
22      
23      public void initController(AbstractRobot r)
24      {
25          robot=r;
26      }
27      
28      public int[] getSensors()
29      {
30          return sensors;
31      }
32      
33      public void halt()
34      {
35          // set running to false, this will force run() to return, and therefore 
36          // stop the controller's thread
37          running=false;
38          
39          // stop the robot also
40          robot.stopMoving();
41      }
42      
43      public AbstractRobot getRobot()
44      {
45          return robot;
46      }
47      
48      public void run()
49      {
50          // set running to true
51          running=true;
52          
53          // loop continuously while running
54          while (running)
55          {
56              // Main mapping - takes the sensor values at each step and comands the
57              // robot to do the appropriate
58              
59              if(robot.getSensor1() > robot.getSensor2() && robot.getSensor2() > robot.getSensor3()){robot.left();}
60              if(robot.getSensor1() > robot.getSensor2() && robot.getSensor2() < robot.getSensor3()){robot.left();}
61              if(robot.getSensor1() < robot.getSensor2() && robot.getSensor2() > robot.getSensor3()){robot.forward();}
62              if(robot.getSensor1() < robot.getSensor2() && robot.getSensor2() < robot.getSensor3()){robot.right();}
63              
64              try{sleep(500);}catch(Exception e){}
65          }
66      }
67  }
68