Begin work on Capture the Flag. Some rules:
Near the top, with your other variables, please paste in the following:
/** A "kill switch" for the robot that can be activated by pressing * one of the touch sensors. When true, the robot should not respond * to commands. Additionally, the robot should release any grip or * activated features when this is true. */ private boolean killed = false; /** Amount of time robot should be disabled (in milliseconds) */ private static final int KILL_DELAY_MS = 10000; /** Amount of time robot should be invincible after being killed (in milliseconds) */ private static final int RECOVERY_DELAY_MS = 3000;
In your RobotServer constructor, you should remove the Escape-key button press listener code and replace it with the code below. This code will both listen for the escape key, and also for the touch sensors. You will need to change the "S1" and "S2" to match the ports where you have connected your touch sensors.
// create a thread that listens for touch sensor input and // disables the robot for a period of time. It also listens // for the escape key being pressed and quits when that happens. new Thread() { public void run() { // Get sensor modes for both touch sensors SensorMode[] ts = new SensorMode[] { new EV3TouchSensor(brick.getPort("S1")).getTouchMode(), new EV3TouchSensor(brick.getPort("S2")).getTouchMode() }; float[] s = new float[1]; Key esc = brick.getKey("Escape"); // keep checking sensors until the user presses Escape while (esc.isUp()) { for (SensorMode sm : ts) { sm.fetchSample(s, 0); if (s[0] > 0.5) { println("Touch detected"); killed = true; } } if (killed) { request("KILLED"); Button.LEDPattern(2); // red Delay.msDelay(KILL_DELAY_MS-1000); Button.LEDPattern(3); // amber Delay.msDelay(1000); println("Touch delay expired"); killed = false; Button.LEDPattern(1); // green Delay.msDelay(RECOVERY_DELAY_MS); Button.LEDPattern(0); // off println("Touch recovery expired"); } else { Delay.msDelay(200); } } // escape was pressed, so exit the program println("Exiting"); System.exit(0); } }.start();
Finally, you'll need to add your own code to your request() method. This will vary for each person, so what is below is only a starting point:
if (killed) { return "Robot is disabled"; }
If you put that at the top of your method, it will prevent any other code from running while the robot is "killed". However, you may need to add extra statements to stop all the motors, "release" any grips or other features you've enabled, or other steps to make the robot stop after it's been killed.