-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJunctionDetection.java
More file actions
245 lines (208 loc) · 5.98 KB
/
JunctionDetection.java
File metadata and controls
245 lines (208 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package localisation;
import Objects.Direction;
import lejos.nxt.LCD;
import lejos.nxt.LightSensor;
import lejos.nxt.Sound;
import lejos.nxt.addon.OpticalDistanceSensor;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.robotics.subsumption.Behavior;
import lejos.util.Delay;
/**
* Behaviour for detecting junctions
*/
public class JunctionDetection implements Behavior {
private LightSensor left;
private LightSensor right;
private int leftValue;
private int rightValue;
private DifferentialPilot pilot;
//Light value threshold
private int threshold;
//Moves
private int NORTH = 0;
private int WEST = 1;
private int SOUTH = 2;
private int EAST = 3;
private int robotDirection = NORTH;
private float NORTH_DISTANCE;
private float SOUTH_DISTANCE;
private float WEST_DISTANCE;
private float EAST_DISTANCE;
private float DISTANCE;
private boolean firstTime=false;
private float heading;
//private ArrayList<PosProb> Localisation.locsArray;
private float cellSize = Localisation.locs.getCellSize();
private OpticalDistanceSensor ranger;
public JunctionDetection(DifferentialPilot pilot, LightSensor left, LightSensor right, double speed, OpticalDistanceSensor distanceSensor, int threshold){
this.left=left;
this.right=right;
this.pilot=pilot;
this.ranger=distanceSensor;
this.threshold = threshold;
//this.Localisation.locsArray = Localisation.locsArray;
//Localisation.locs.setArray(this.Localisation.locsArray);
pilot.setTravelSpeed(speed);
}
/**
* Generates calibrated values
* On a black line = 45
* Not on a black line = 35
*/
@Override
public boolean takeControl(){
generateLightValues();
if((leftValue < threshold) && (rightValue < threshold) && Localisation.locs.size()>1){
return true;
}
return false;
}
public void action() {
pilot.travel(0.1f);
//LCD.drawString(ranger.getRange() + " " + Localisation.locs.getCellSize() , 0, 6);
if(!firstTime || ranger.getRange()<Localisation.locs.getCellSize()){
for(int i=0;i<4;i++){
//LCD.clear();
//LCD.drawString(+ Localisation.locs.size() + " " + ranger.getRange(), 0, 7);
if(checkEnd(Localisation.locs.size()))
return;
executeMove(RotationDirections.LEFT);
Delay.msDelay(1000);
// if(Localisation.locs.size() < 7){
//
}for(int i1 = 0; i1 < Localisation.locs.size(); i1++){
// //LCD.drawString(Localisation.locs.getPoints(i1).getxCoord() + " " + Localisation.locs.getPoints(i1).getyCoord(), 0, i1+1);
// }
// }
// Delay.msDelay(3000);
}
//pilot.stop();
firstTime = true;
}
if(checkEnd(Localisation.locs.size()))
return;
if(ranger.getRange()<Localisation.locs.getCellSize()){
//LCD.drawString("I am in with: " + ranger.getRange() , 0, 0);
if(heading == 90){
turnDecision(WEST_DISTANCE, EAST_DISTANCE);
}else if(heading == 180){
turnDecision(NORTH_DISTANCE, SOUTH_DISTANCE);
}else if(heading == -90){
turnDecision(EAST_DISTANCE, WEST_DISTANCE);
}else if(heading == 0){
turnDecision(SOUTH_DISTANCE, NORTH_DISTANCE);
}
}else{
executeMove(RotationDirections.FORWARD);
Localisation.locs.updateLocations(heading);
}
if(checkEnd(Localisation.locs.size()))
return;
LCD.clear();
LCD.drawString("" + Localisation.locs.size(), 0, 7);
Delay.msDelay(1000);
}
@Override
public void suppress() {
}
//Turns left
private void moveLeft(){
pilot.rotate(126);
}
//Turns right
private void moveRight(){
pilot.rotate(-126);
}
//Continues going forward
private void moveForward(){
pilot.forward();
}
private void generateLightValues(){
leftValue = left.getLightValue();
rightValue = right.getLightValue();
}
private void executeMove(int move){
setDirections();
//LCD.drawString(DISTANCE + " ", 0, testCount);
Localisation.locs.setLocations(heading, DISTANCE);
if(move==RotationDirections.LEFT){
moveLeft();
if(robotDirection==EAST)
robotDirection=NORTH;
else
robotDirection+=1;
}
else if(move==RotationDirections.RIGHT){
moveRight();
if(robotDirection == NORTH)
robotDirection = EAST;
else
robotDirection-=1;
}
else
moveForward();
setDirections();
}
private void turnDecision(float leftPosition, float rightPosition){
if(leftPosition > cellSize && rightPosition > cellSize){
if(leftPosition < rightPosition){
executeMove(RotationDirections.LEFT);
Localisation.locs.updateLocations(heading);
}else{
executeMove(RotationDirections.RIGHT);
Localisation.locs.updateLocations(heading);
}
}else if(leftPosition > cellSize){
executeMove(RotationDirections.LEFT);
Localisation.locs.updateLocations(heading);
}else if(rightPosition > cellSize){
executeMove(RotationDirections.RIGHT);
Localisation.locs.updateLocations(heading);
}else{
executeMove(RotationDirections.LEFT);
executeMove(RotationDirections.LEFT);
Localisation.locs.updateLocations(heading);
}
//LCD.clear();
//LCD.drawString("h: " + heading, 0, 0);
}
private void setDirections(){
if(robotDirection==NORTH){
heading = 90;
NORTH_DISTANCE=ranger.getRange();
DISTANCE=ranger.getRange();
}
else if(robotDirection==EAST){
heading = 0;
EAST_DISTANCE=ranger.getRange();
DISTANCE=ranger.getRange();
}
else if(robotDirection==SOUTH){
heading = -90;
SOUTH_DISTANCE=ranger.getRange();
DISTANCE=ranger.getRange();
}
else if(robotDirection==WEST){
heading = 180;
WEST_DISTANCE=ranger.getRange();
DISTANCE=ranger.getRange();
}
}
private boolean checkEnd(int size){
//LCD.drawString(Localisation.locs.getPoints(0).getxCoord() + " " + Localisation.locs.getPoints(0).getyCoord(), 0, 4);
return size <= 1;
}
public ProbableLocations getlocs(){
return Localisation.locs;
}
public Direction getDir(){
if(robotDirection==NORTH)
return Direction.NORTH;
else if(robotDirection==EAST)
return Direction.EAST;
else if(robotDirection==SOUTH)
return Direction.SOUTH;
else
return Direction.WEST;
}
}