MainProgram.java
package prey;
 
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.LCD;
import lejos.nxt.MotorPort;
 
public class MainProgram implements ButtonListener {
 
    private static Arbitrator arbitrator;
    private static boolean running = true; // Setting running to false stops the entire program. 
 
    public MainProgram() {
        // Add escpe and enter buttons to the button listener. 
        Button.ESCAPE.addButtonListener(this);
        Button.ENTER.addButtonListener(this);
    }
 
    public static void main(String[] args) throws Exception {
        // Gives the user time to remove the hands before the robot starts driving and clears the display. 
        LCD.clearDisplay();
        LCD.drawString("   Starting       ", 0, 4);
        Thread.sleep(1000);
        LCD.clearDisplay();
 
        // Initialize the different components to run the constructors. 
        new HeadTurner();
        new InternalState();
        new Sensors();
        new MainProgram();
 
        // Initialize the behaviors. 
        AvoidBorder avoidEdge = new AvoidBorder("avoidEdge", 1);
        AvoidObstacle avoidObst = new AvoidObstacle("avoidObst", 2);
        BackBumper backBumper = new BackBumper("backBumper", 3);
        RunAway runAway = new RunAway("runAway", 4);
        EatFood eatFood = new EatFood("eatFood", 5);
        Wander wander = new Wander("wander", 6);
 
        Behavior[] behaviors = {avoidEdge, avoidObst, backBumper, runAway, eatFood, wander};
 
        // Initialize and start the arbitrator. 
        arbitrator = new Arbitrator(behaviors);
        arbitrator.start();
 
        // Once running becomes false the entire program will exit. 
        while (running) {        
 
            // Print debug information to the LCD.
            //LCD.drawString("C " + Sensors.getColourSensorValue() + "  C2 " + Sensors.getCSID() + "   ", 0, 7);
            //LCD.drawString("Hunger " + InternalState.getHunger() + " ", 0, 7);
            //LCD.drawString("IR " + Sensors.getAverageInfraredValues() + " ", 0, 7);
            //LCD.drawString("BB " + backBumper.isRunning() + "  BS " + Sensors.getTouchSensorValue(), 0, 7);
            //LCD.drawString("Hunger " + InternalState.getHunger(), 0, 7);
            Thread.sleep(10);
        }
 
        // Play four descending tones to indicate the prey dying.
        SoundPlayer.playDieSound();
 
        LCD.clearDisplay();
        LCD.drawString("    Terminating    ", 0, 4);
        Thread.sleep(2000);
    }
 
    @Override
    public void buttonPressed(Button b) {}
 
    @Override
    public void buttonReleased(Button b) {
        // When any button is pressed, the program stops. 
        stopRunning();
    }
 
    private void stopRunning() {
        // Stop the arbitrator, the main program and the motors. 
        running = false;
        arbitrator.stopRunning();
        resetMotors();
    }
 
    /**
     * Stop the motors and reset the head. It does this multiple times in one 
     * second, to make sure to overwrite all commands from delayed behaviors. 
     */
    private void resetMotors() {
        try {
            int step = 100;
            for (int i = 0; i < 1000; i += step) {
                MotorPort.C.controlMotor(0,3);
                MotorPort.B.controlMotor(0,3);
                HeadTurner.centerHead();
                Thread.sleep(step); 
            }
        } catch (InterruptedException e) {}
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License