1   import interfaces.Controller;
2   import interfaces.AbstractRobot;
3   
4   
5   /**
6   * This controller is based on real world measured input values
7   * It is a 3-1 net plus bias.
8   *
9   * @author Graeme Bell
10  */
11  
12  public class GBN2b extends Controller {
13  
14  AbstractRobot r;
15  boolean initcalled;
16  boolean running;
17  private boolean paused=false;
18  
19  
20  private int[] sensors={Controller.SENSOR_TYPE_LIGHT,Controller.SENSOR_TYPE_LIGHT,Controller.SENSOR_TYPE_LIGHT};
21  
22  // this looks rubbish - but it will compile to run extremely fast 
23  // this is because the weights will be inlined and the inputs/outputs
24  // won't need dereferencing or array overhead.
25  
26  float input0;
27  float input1;
28  float input2;
29  
30  // bias input is assumed to be +1
31  
32  float output0;
33  
34  
35  final float weight00=-11.208f;
36  final float weight10=1.80533f;
37  final float weight20=11.604f;
38  final float weightb0=-2.01753f;
39  
40      public void run(){
41  
42          running=true;
43              
44          while (running) {
45  
46                  // get input
47                                                          
48                  input0=r.getSensor1()/100.0f;
49                  input1=r.getSensor2()/100.0f;
50                  input2=r.getSensor3()/100.0f;
51  
52                  //Sigmoidal activation function 1.0/1.0+exp(excitation)
53  
54                  output0 = 1.0f/(1.0f+(float)(Math.exp(  weight00*input0+
55                              weight10*input1+
56                              weight20*input2+
57                              weightb0)));
58  
59                  //decide what to do.
60  
61                  if (output0<0.4) {r.right();}
62                  if (output0<0.6 && output0>=0.4) {r.forward();}
63                  if (output0>=0.6) {r.left();}
64                  
65                  try{Thread.sleep(500);}catch(Exception e){}
66          }
67  
68      }
69  
70      
71      public void initController(AbstractRobot r) {
72  
73          this.r=r;
74          initcalled=true;
75      }
76      
77      public int[] getSensors(){
78      
79          return sensors;
80      }
81      
82      public void halt(){
83      
84          running=false;
85          r.stopMoving();
86      }
87      
88      public AbstractRobot getRobot(){
89      
90          return r;
91      }
92      
93      public GBN2b() {    }
94  }   
95