-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigDist.java
64 lines (49 loc) · 1.27 KB
/
ConfigDist.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
package exercise3.part1;
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.addon.OpticalDistanceSensor;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.util.Delay;
public class ConfigDist {
private OpticalDistanceSensor sensor;
private DifferentialPilot pilot;
private final float MAX_RANGE = 800;
public ConfigDist() {
pilot = new DifferentialPilot(5.6, 16.0, Motor.B, Motor.C);
sensor = new OpticalDistanceSensor(SensorPort.S1);
}
public void run() {
float range = sensor.getRange();
float normalisedRange = range / MAX_RANGE;
float speed = (normalisedRange * 300f);
pilot.setTravelSpeed((int)speed);
pilot.forward();
Thread.yield();
if(range <= 10) {
pilot.stop();
pilot.rotate(90);
}
}
/**
* Exits the program when the 'ENTER' button is pressed on the robot.
*/
public void buttonExit() {
Button.ENTER.addButtonListener(new ButtonListener() {
public void buttonPressed(Button b) {
System.exit(0);
}
public void buttonReleased(Button b) {
System.exit(0);
}
});
}
public static void main(String[] args) {
ConfigDist robot = new ConfigDist();
Button.waitForAnyPress();
while(true) {
robot.run();
}
}
}