JavaRCX Code
//
import josx.platform.rcx.Motor;
import josx.platform.rcx.Sensor;
/**
* JavaRCX.java
* Contains basic methods and variables common to all robot objectives.
*
* @author CS371 2002
* @version 1.0
* @since 1.0
*/
public class JavaRCX{
public JavaRCX(){
}
/**
* Which state the sensors of the robot are in - possible values are constants below.
*/
public static int state;
/**
* A separate background thread that reads values from the light sensors and updates the state variable.
*/
public static LightWatcher watcher = new LightWatcher();
/**
* No sensors dark.
*/
public static final int NONE =0;
/**
* Left sensor dark.
*/
public static final int LEFT_ONLY =1;
/**
* Center sensor dark.
*/
public static final int CENTER_ONLY =2;
/**
* Right sensor dark.
*/
public static final int RIGHT_ONLY =3;
/**
* Left and center sensors dark.
*/
public static final int LEFT_CENTER =4;
/**
* Left and right sensors dark.
*/
public static final int LEFT_RIGHT =5;
/**
* Center and right sensors dark.
*/
public static final int CENTER_RIGHT =6;
/**
* All sensors dark.
*/
public static final int ALL = 7;
/**
* The level at which readings are considered black by the light sensor (<=DARK)
*/
public static final int DARK =45;
/**
* Power level of the motors.
* Try More experimentation with power levels and how much better light sensors are able
* to read at different speeds of the motor.
*/
public static final int POWER =3;
/**
* Counter variable used to estimate how far the robot should turn right when lost
*/
public static final int R_ROTATION =100;
/**
* Counter variable used to estimate how far the robot should turn left when lost
*/
public static final int L_ROTATION =70;
/**
* When the state NONE occurs this method will scan to the left for L_ROTATION
* then scan farther to the right R_ROTATION.
* This will ultimately cause the robot to do a 360 unless it hits a black line at some point.
*/
public static void lost(){
int i = 0;
while(state == NONE && i < L_ROTATION){
Motor.A.backward();
Motor.C.backward();
i++;
}
i = 0;
while(state == NONE && i < R_ROTATION){
Motor.A.forward();
Motor.C.forward();
i++;
}
}
/**
* * Precondition: State must start at CENTER_ONLY.
* Activates the sensors, sets up motor power,starts light sensor thread.
*/
public static void initialize(){
Sensor.S1.activate();
Sensor.S2.activate();
Sensor.S3.activate();
Motor.A.setPower(POWER);
Motor.C.setPower(POWER);
state = CENTER_ONLY;
watcher.start();
}
/**
* Exit program with 5 second delay.
*/
public static void powerOff(){
try{
Thread.sleep(5000);
}
catch(InterruptedException e){
}
System.exit(0);
}
}//class
//