-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineFollow.java
More file actions
77 lines (60 loc) · 1.57 KB
/
LineFollow.java
File metadata and controls
77 lines (60 loc) · 1.57 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
package localisation;
import lejos.nxt.LCD;
import lejos.nxt.LightSensor;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.robotics.subsumption.Behavior;
import lejos.util.Delay;
/**
* The default behaviour for following a line
*/
public class LineFollow implements Behavior {
private LightSensor left;
private LightSensor right;
private int leftValue;
private int rightValue;
private int threshold;
private boolean suppress=false;
private DifferentialPilot pilot;
public LineFollow(DifferentialPilot pilot, LightSensor left,LightSensor right, double speed, int threshold){
this.pilot = pilot;
this.left=left;
this.right=right;
this.threshold = threshold;
pilot.setTravelSpeed(speed);
}
@Override
public boolean takeControl() {
if(Localisation.locs.size()<=1){
pilot.stop();
}
LCD.drawString("" + Localisation.locs.size(), 0, 3);
return Localisation.locs.size()>1;
}
@Override
public void action() {
pilot.forward();
generateLightValues();
while((leftValue > threshold || rightValue > threshold) && !suppress){
float diff = leftValue - rightValue;
pilot.steer(-diff);
generateLightValues();
}
pilot.stop();
suppress = false;
Delay.msDelay(100);
//LCD.drawString("I found a junction", 0, 6);
}
@Override
public void suppress() {
pilot.stop();
suppress = true;
}
/**
* Generates calibrated light values
*/
private void generateLightValues(){
leftValue = left.getLightValue();
rightValue = right.getLightValue();
//System.out.println("L: " + leftValue + " R: " + rightValue);
}
}