tags:

views:

42

answers:

0

We are trying to implement two behaviors on a rcx robot, the lowest priority given to a touch sensor, and the hightest given to a light sensor for a sumobot competition. However, the behaviors cannot run simultaneously. For example, the action for the touch sensor will not occur twice in a row. Instead each action of the behaviors must run back and forth, instead of our highest priority light sensor always running.

import josx.robotics.*;
import josx.platform.rcx.*;

public class SumoBot2 {

    public static void main(String[] args) {

        Behavior el = new EdgeLight();
        Behavior bts = new BackTouchSensor();

        Behavior [] bArray = {bts, el};
        Arbitrator arby = new Arbitrator(bArray); //passes behavior array into arbitrator class to monitor priority
        arby.start();

    }
}



import josx.robotics.*;
import josx.platform.rcx.*;

public class EdgeLight implements Behavior {

    public EdgeLight () {
        Sensor.S3.activate(); // Constructor To activate Light Sensor
    }

    public boolean takeControl () {    // Method takeControl Looks for darker border edge
        if (Sensor.S3.readValue() < 50 ) {
            return true;
        }
        return false;
    }

    public void action () {  //Method to drive robot backward to avoid driving off the arena
        Motor.A.setPower(7);
        Motor.C.setPower(7);
        Motor.A.backward();
        Motor.C.backward();

        try{Thread.sleep(2000); } catch (Exception e) {}
        Motor.A.stop();
        Motor.C.stop();

    }

    public void suppress() {  //Supress method to stop motors from running after backing up from edge
        Motor.A.stop();
        Motor.C.stop();


        }

}

import josx.robotics.*;
import josx.platform.rcx.*;

public class BackTouchSensor implements Behavior {

    public BackTouchSensor () {
        Sensor.S2.activate(); // Constructor to activate Touch Sensor
    }

    public boolean takeControl () {    // Method takeControl Looks for back touch sensor to be pressed
        return Sensor.S2.readBooleanValue();

    }

    public void action () {  //Method to turn robot around and avoid opponent from behind
        Motor.A.setPower(4);
        Motor.C.setPower(4);
        Motor.A.backward();
        Motor.C.forward();

            try{Thread.sleep(350); } catch (Exception e) {}

        Motor.A.setPower(7);
        Motor.C.setPower(7);
        Motor.A.forward();
        Motor.C.forward();

            try{Thread.sleep(2000); } catch (Exception e) {}

        Motor.A.stop();
        Motor.C.stop();
    }   

        public void suppress () {  //Supress method to stop motors

            Motor.A.stop();
            Motor.C.stop();
        }

}