-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFollowLine.java
81 lines (68 loc) · 1.68 KB
/
FollowLine.java
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
package exercise3.part1;
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.LightSensor;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.util.Delay;
public class FollowLine {
private DifferentialPilot pilot;
private LightSensor lSensorLeft;
private LightSensor lSensorRight;
private boolean firstRun = true;
private int stuck_count = 0;
/** Constructor */
public FollowLine() {
pilot = new DifferentialPilot(5.6, 16.0, Motor.B, Motor.C);
lSensorLeft = new LightSensor(SensorPort.S2);
lSensorRight = new LightSensor(SensorPort.S3);
}
/**
* Exits the program when the 'ENTER' button is pressed on the robot.
*/
public void buttonExit() {
Button.ESCAPE.addButtonListener(new ButtonListener() {
public void buttonPressed(Button b) {
System.exit(0);
}
public void buttonReleased(Button b) {
System.exit(0);
}
});
}
/** Run method */
public void run() {
pilot.setTravelSpeed(20);
if(firstRun) {
pilot.forward();
}
//pilot.forward();
int leftValue = lSensorLeft.readNormalizedValue();
int rightValue = lSensorRight.readNormalizedValue();
if(leftValue < 470 && rightValue < 380) {
if(firstRun) {
firstRun = false;
return;
}
pilot.rotate(-90);
pilot.forward();
} else if(leftValue < 420) {
pilot.rotate(20);
pilot.forward();
} else if(rightValue < 330) {
pilot.rotate(-20);
pilot.forward();
}
}
/** Main method */
public static void main(String[] args) {
Button.waitForAnyPress();
FollowLine robot = new FollowLine();
Delay.msDelay(1000);
robot.buttonExit();
while(true) {
robot.run();
}
}
}