1 import interfaces.Controller;
2 import interfaces.AbstractRobot;
3
4
18
19 public class GBN3 extends Controller {
20
21 AbstractRobot r;
22 boolean initcalled;
23 boolean running;
24 private boolean paused=false;
25
26 private int[] sensors={Controller.SENSOR_TYPE_LIGHT,Controller.SENSOR_TYPE_LIGHT,Controller.SENSOR_TYPE_LIGHT};
27
28
32 float sens0;
33 float sens1;
34 float sens2;
35
36 float input0;
37 float input1;
38 float input2;
39 float input3;
40
41
43 float output0;
44
45 final float weight00 = -0.75f;
46 final float weight10 = -0.75f;
47 final float weight20 = 0.1857f;
48 final float weight30 = 1.1088f;
49 final float weightb0 = -0.2127f;
50
51 public void run(){
52
53 running=true;
54
55 while (running) {
56
57
58
60 sens0=r.getSensor1()/100.0f;
61 sens1=r.getSensor2()/100.0f;
62 sens2=r.getSensor3()/100.0f;
63
64
66 if (sens0>sens1 && sens1>sens2) { input0=1.0f; } else {input0=0.0f;}
67 if (sens0>sens1 && sens1<sens2) { input1=1.0f; } else {input1=0.0f;}
68 if (sens0<sens1 && sens1>sens2) { input2=1.0f; } else {input2=0.0f;}
69 if (sens0<sens1 && sens1<sens2) { input3=1.0f; } else {input3=0.0f;}
70
71
74
76 output0 = 1.0f/(1.0f+(float)(Math.exp( weight00*input0+
77 weight10*input1+
78 weight20*input2+
79 weight30*input3+
80 weightb0)));
81
82
84 if (output0<0.4) {r.right();}
85 if (output0<0.6 && output0>=0.4) {r.forward();}
86 if (output0>=0.6) {r.left();}
87
88 try{Thread.sleep(500);}catch(Exception e){}
89 }
90 }
91
92
93 public void initController(AbstractRobot r) {
94
95 this.r=r;
96 initcalled=true;
97 }
98
99 public int[] getSensors(){
100
101 return sensors;
102 }
103
104 public void halt(){
105
106 running=false;
107 r.stopMoving();
108 }
109
110 public AbstractRobot getRobot(){
111
112 return r;
113 }
114
115 public GBN3() { }
116
117 }
118