import josx.platform.rcx.Motor; import josx.platform.rcx.Sensor; /** * BasicLEGO contains several static methods for controlling a single LEGO RCX * robot with two motors plugged into ports A and C, two touch sensors plugged into * ports 1 and 3, and a single light sensor plugged into Port 2. * * @author Michael Salley * @author Aaron Folstead * @version 1.0 11 NOV 2008 */ public class BasicLEGO { private static Sensor s1; private static Sensor s2; private static Sensor s3; private static Motor a; private static Motor c; /** * Place any statements you wish your robot to execute inside the main() method. * Feel free to write your own methods to supplement those provided in the class. */ public static void main(String args[]) { setUpSensorsAndMotors(); } /** * Configures local variables s1, s2, s3, a, c as three sensors, and two motors. * Sets the default operational modes of the sensors. * Should be called as the first "setting-up chore" before a robot tries * to execute any maneuvers or sensor checks. */ public static void setUpSensorsAndMotors() { s1 = Sensor.S1; s2 = Sensor.S2; s3 = Sensor.S3; s1.setTypeAndMode(1, 0x20); s2.setTypeAndMode(3, 0x80); s3.setTypeAndMode(1, 0x20); s1.activate(); s2.activate(); s3.activate(); a = Motor.A; c = Motor.C; } /** * Deactivates all motors and "passivates" all sensors. Should be the last * executed statement of all robot programs. */ public static void turnEverythingOff() { a.stop(); c.stop(); s1.passivate(); s2.passivate(); s3.passivate(); } /** * sets all motors to the power percentage 0-100 specified by the parameter. * @param percentPower An integer value representing 0-100% of a motor's available power. */ public static void setAllMotors(int percentPower) { a.setPower(percentPower); c.setPower(percentPower); } /** * Sets motor A to the percentage specified by the first parameter, * and sets motor C to the percentage specified by the second. * @param percentA percent power to send to Motor A * @param percentC percent power to send to Motor C */ public static void setMotors(int percentA, int percentC) { a.setPower(percentA); c.setPower(percentC); } /** * Sets both motors to full power. */ public static void fullPower() { setAllMotors(100); } /** * Sets both motor directions to forward, assuming correct polarity on the wires. */ public static void allForward() { a.forward(); c.forward(); } /** * Sets both motors to backward, assuming correct wire polarity. */ public static void allBackward() { a.backward(); c.backward(); } /** * returns true if either of the touch sensors is touched. * @return boolean value of s1 OR s3 */ public static boolean eitherTouchSensorPressed() { return s1.readBooleanValue() || s3.readBooleanValue(); } /** * returns an integer representing the percentage from 0-100 of brightness * measured by the light sensor at the time of the request. A good general * guideline is to consider values < 45-50 "dark". Ambient light, battery voltage, * and numerous factors can affect this guesstimate. * @return int value of brightness read by light sensor. */ public static int lightSensorValue() { return s2.readValue(); } /** * Returns true if the sensor is reading "Dark", where dark is defined * as a brightness value less than that percentage specified in the parameter. * @param darkPercent the integer value of the "darkness" percent threshold. * @return true if "dark", false if "bright" */ public static boolean dark(int darkPercent) { return s2.readValue() < darkPercent; } /** * Causes the robot to remain in its current state of motion and logic * for the number of milliseconds specified by the parameter. One second * is equal to 1000 milliseconds. */ public static void rest(int milliseconds) { try { Thread.sleep(milliseconds); } catch(Exception e) { } } /** * Executes a relatively close U-turn in a counterclockwise direction. * Modify the value in the call to rest() to represent the amount of actual * turn-around time the robot requires based on its current wheel configuration, * power level, "road" surface, and other variables. */ public static void uTurnLeft() { c.forward(); a.backward(); rest(2100); } /** * Executes a relatively close U-turn in a clockwise direction. * Modify the value in the call to rest() to represent the amount of actual * turn-around time the robot requires based on its current wheel configuration, * power level, "road" surface, and other variables. */ public static void uTurnRight() { a.forward(); c.backward(); rest(2100); } }