import lejos.robotics.subsumption.Behavior; /* * Bounce behavior checks for a black line. If it encounters it, it * causes the robot to "bounce back", i.e. to drive backwards and to * rotate. That behavior should trap the robot inside a black circle. * Should have high or highest priority. */ public class Bounce implements Behavior { // boolean variable to allow arbitrator thread to stop the action method boolean suspended = false; @Override // Is called by arbitrator thread to check if behavior wants to kick in public boolean takeControl() { return (MyMain.getLight() < MyMain.zero); } @Override // Is called by arbitrator thread when this behavior takes over. Note that // this behavior must quit immediately as soon as 'suspended' is true! public void action() { suspended = false; MyMain.leftMotor.setSpeed(MyMain.TURN_SPEED); MyMain.rightMotor.setSpeed(MyMain.TURN_SPEED); MyMain.leftMotor.rotate(-300, true); MyMain.rightMotor.rotate(-300, true); while (MyMain.leftMotor.isMoving() && (!suspended)) { Thread.yield(); } if (!suspended) { MyMain.leftMotor.rotate(-300, true); MyMain.rightMotor.rotate(300, true); while (MyMain.leftMotor.isMoving() && (!suspended)) { Thread.yield(); } } MyMain.leftMotor.stop(true); MyMain.rightMotor.stop(); } @Override // Is called by arbitrator thread to stop this behavior. public void suppress() { suspended = true; } }