/******************** * JimsRobot.java * * Playing around with a simple lego robot. * Motors A and B run wheels on the two sides of the robot. * * The main() method that calls these is in RunJimsRobot.java. * * @author Jim Mahoney * @version 1.0, September 30, 2002 * @see RunJimsRobot * **/ import josx.platform.rcx.*; public class JimsRobot { // about 1 sec for 20 cm (Varies depending on friction and battery.) final double milliSecPerCm = 1000.0/20.0; // about 1 sec for 100 degrees (Varies) final double milliSecPerDeg = 1000.0/100.0; // This constructor is here as a placeholder; // it doesn't do anything right now. JimsRobot(){ } public void makeNoise() throws Exception { Sound.beepSequence(); Thread.sleep(1000); // This beep takes about a second. } public void goForward(double distanceInCm) throws Exception { Motor.A.stop(); Motor.B.stop(); Motor.A.setPower(7); Motor.B.setPower(7); Motor.A.backward(); Motor.B.forward(); Thread.sleep((int)(milliSecPerCm*distanceInCm)); Motor.A.stop(); Motor.B.stop(); Thread.sleep(10); // Brief pause to make sure we've really stopped. } public void goBackward(double distanceInCm) throws Exception { Motor.A.stop(); Motor.B.stop(); Motor.A.setPower(7); Motor.B.setPower(7); Motor.A.forward(); Motor.B.backward(); Thread.sleep((int)(milliSecPerCm*distanceInCm)); Motor.A.stop(); Motor.B.stop(); Thread.sleep(10); // Brief pause to make sure we've really stopped. } public void turnRight(double angleInDeg) throws Exception { Motor.A.stop(); Motor.B.stop(); Motor.A.setPower(7); Motor.B.setPower(7); Motor.A.forward(); Motor.B.forward(); Thread.sleep((int)(milliSecPerDeg*angleInDeg)); Motor.A.stop(); Motor.B.stop(); Thread.sleep(10); // Brief pause to make sure we've really stopped. } public void turnLeft(double angleInDeg) throws Exception { Motor.A.stop(); Motor.B.stop(); Motor.A.setPower(7); Motor.B.setPower(7); Motor.A.backward(); Motor.B.backward(); Thread.sleep((int)(milliSecPerDeg*angleInDeg)); Motor.A.stop(); Motor.B.stop(); Thread.sleep(10); // Brief pause to make sure we've really stopped. } public void waggleDance() throws Exception { turnLeft(10.0); turnRight(15.0); turnLeft(15.0); turnRight(10.0); } }