- Code: Select all
//Author:▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒skyminer
//Version: 1.0.1
import java.io.*;
import lejos.nxt.*;
import lejos.nxt.comm.*;
class PathScanner {
public static int xPos = 0; //The X position of the robot (0 is the start)
public static int yPos = 0; //The Y position of the robot (0 is the start)
public static int dir = 1; //The direction of the robot (1 is the start)(0 = north, 1 = east, 2 = south, 3 = west)
public static boolean n, e, s, w; //True if there is a path in the direction indicated from the current cell
public static boolean isScanComplete = false; //Initializes a boolean variable that shuts down the scan once true
/// public static BTConnection comm; //Initializes the connection
/// public static DataOutputStream out; //Initializes the connection's output stream
/// public static DataInputStream in; //Initializes the connection's input stream
public static final LightSensor R = new LightSensor(SensorPort.S1, true); //Initializes the right light sensor
public static final LightSensor C = new LightSensor(SensorPort.S2, true); //Initializes the center light sensor
public static final LightSensor L = new LightSensor(SensorPort.S3, true); //Initializes the left light sensor
public static void main(String[] args) {
LCD.drawString("PathScanner", 0, 7);
/// comm = Bluetooth.waitForConnection(); //Connects the NXT to the PC
/// out = comm.openDataOutputStream(); //Opens the connection's output stream
/// in = comm.openDataInputStream(); //Open's the connection's input stream
while(!isScanComplete) { //Starts the scanning loop
Motor.C.rotateTo(280, true);
Motor.B.rotateTo(280, true); //Starts the robot moving forward
while(Motor.C.isMoving() && Motor.B.isMoving()) {
if(!(C.getLightValue() < 50) || ((R.getLightValue() < 50) && (L.getLightValue() < 50))) {
Motor.C.stop();
Motor.B.stop();
}
if(R.getLightValue() < 50) {
Motor.C.stop();
while(!(R.getLightValue() < 50)) {}
Motor.C.rotateTo(280, true);
}
if(L.getLightValue() < 50) {
Motor.B.stop();
while(!(L.getLightValue() < 50)) {}
Motor.B.rotateTo(280, true);
}
}
Motor.C.resetTachoCount();
Motor.B.resetTachoCount();
Motor.C.rotate(60, true);
Motor.B.rotate(60, false);
//Scans the cell data
boolean r_on = R.getLightValue() < 50;
boolean c_on = C.getLightValue() < 50;
boolean l_on = L.getLightValue() < 50;
//Configures the cell data
/// if(dir == 0) {
/// n = c_on;
/// e = r_on;
/// s = true;
/// w = l_on;
/// }
/// if(dir == 1) {
/// n = l_on;
/// e = true;
/// s = r_on;
/// w = c_on;
/// }
/// if(dir == 2) {
/// n = c_on;
/// e = l_on;
/// s = true;
/// w = r_on;
/// }
/// if(dir == 3) {
/// n = r_on;
/// e = c_on;
/// s = l_on;
/// w = true;
/// }
//Gets boolean isScanComplete and performs the necessary operations
/// try {
/// isScanComplete = in.readBoolean();
/// } catch(IOException error) {
/// System.out.println(" write error " + error);
/// }
/// if(isScanComplete) {
/// break;
/// }
//Sends the cell data
/// try {
/// out.writeInt(xPos);
/// out.writeInt(yPos);
/// out.writeBoolean(n);
/// out.writeBoolean(e);
/// out.writeBoolean(s);
/// out.writeBoolean(w);
/// } catch(IOException error) {
/// System.out.println(" write error " + error);
/// }
//Prepares for the next move
if(l_on) { //Turn left
Motor.C.rotateTo(-150);
Motor.B.rotateTo(150);
if(!(dir == 0)) {
dir--;
} else {
dir = 3;
}
}
if(r_on) { //Turn right
Motor.C.rotateTo(150);
Motor.B.rotateTo(-150);
if(!(dir == 3)) {
dir++;
} else {
dir = 0;
}
}
if(!l_on && !c_on && !r_on) { //Turn around
Motor.C.rotateTo(-300);
Motor.B.rotateTo(300);
if(dir == 0) {
dir = 2;
} else if(dir == 1) {
dir = 3;
} else if(dir == 2) {
dir = 0;
} else {
dir = 1;
}
}
Motor.C.resetTachoCount();
Motor.B.resetTachoCount();
}
}
}
This version is in fact a total rework of a previous version, which had the same bug, going in circles. Due to my "amazing" organization skills, I seem to have lost it. However, all that was different is that there were absolutely no Motor.stops() methods, and coded likewise.
