Now, when I run the program, it will stop after a short while and just show the leJOS J icon on the screen. I'm not sure what is causing this problem. Any help anyone can give me would be appreciated.
Below is the code. Note that I actually commented out the code to do the left turn, hoping that might help, but it didn't.
- Code: Select all
import lejos.navigation.Pilot;
import lejos.nxt.Button;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.Sound;
import lejos.nxt.UltrasonicSensor;
/**
* @author Paul Reiners
*
*/
public class WallFollower {
private static final int LEFT_TURN = -2;
private static final float WHEEL_DIAM = 5.6F;
private static final float TRACK_W = 13F;
private static final int WALL_HUGGING_CORRECTION_ANGLE = 2;
private static final int WALL_HUGGING_MIN = 9;
private static final int WALL_HUGGING_MAX = 11;
private UltrasonicSensor us = new UltrasonicSensor(SensorPort.S1);
private Pilot sc = new Pilot(WHEEL_DIAM, TRACK_W, Motor.C, Motor.B);
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
WallFollower wallFollower = new WallFollower();
wallFollower.runMaze();
}
public void runMaze() {
sc.setSpeed(700); // Movement speed
sc.forward();
while (!Button.ESCAPE.isPressed()) {
int distance = us.getDistance();
if (distance < WALL_HUGGING_MIN) {
int adjustAngle = -WALL_HUGGING_CORRECTION_ANGLE;
adjustDirection(adjustAngle);
} else if (distance > 2 * WALL_HUGGING_MAX) {
// Passage to left. Turn left.
// adjustDirection(LEFT_TURN);
} else if (distance > WALL_HUGGING_MAX) {
int adjustAngle = WALL_HUGGING_CORRECTION_ANGLE;
adjustDirection(adjustAngle);
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// Keep going.
}
}
}
private void adjustDirection(int adjustAngle) {
sc.stop();
if (adjustAngle > 0) {
Sound.buzz();
} else if (adjustAngle == LEFT_TURN) {
Sound.twoBeeps();
} else {
Sound.beep();
}
sc.rotate(adjustAngle);
sc.forward();
}
}
