////// Robot.java ////// import josx.platform.rcx.Motor; import josx.platform.rcx.Sensor; import josx.platform.rcx.Sound; import josx.platform.rcx.SensorConstants; import josx.platform.rcx.SensorListener; import java.lang.Thread; import josx.util.Timer; /** * Public Class Robot Version 1.0 * @author Karl Gustav Sterneberg, Romain Barrallon, Victor Contreras */ public class Robot { /** * Constructor **/ public Robot() { Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_BOOL); leftSensorId = Sensor.S1.getId(); leftSensorListener = new MySensorListener(this); Sensor.S1.addSensorListener(leftSensorListener); Sensor.S3.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_BOOL); rightSensorId = Sensor.S3.getId(); rightSensorListener = new MySensorListener(this); Sensor.S3.addSensorListener(rightSensorListener); myTimerListener = new MyTimerListener(); timer = new Timer(timerInterval,myTimerListener); } /** * Sets the speed of the robot, that is the power with which the motors will be feed. * @param speed the speed of the robot (integer between 1 and 7). */ public void setSpeed(int speed) { Motor.A.setPower(speed); Motor.C.setPower(speed); } /** * Sets the forward speed of the robot. If parameter value less than 1, speed is set to 1. * If parameter value is larger thann 7, speed is set to 7. * @param speed the forward speed of the robot (integer between 1 and 7). */ public void setForwardSpeed(int speed) { if (minSpeed <= speed && speed <= maxSpeed) forwardSpeed = speed; else if (speed < minSpeed) forwardSpeed = minSpeed; else forwardSpeed = maxSpeed; } /** * Sets the backward speed of the robot. If parameter value less than 1, speed is set to 1. * If parameter value is larger thann 7, speed is set to 7. * @param speed the backward speed of the robot (integer between 1 and 7). */ public void setBackwardSpeed(int speed) { if (minSpeed <= speed && speed <= maxSpeed) backwardSpeed = speed; else if (speed < minSpeed) backwardSpeed = minSpeed; else backwardSpeed = maxSpeed; } /** * Sets the time that the robot will go backward before turning when a senor is pressed. * @param time the time that the robot will go backward before turning when a senor is pressed. */ public void setTimeGoBack(int time) { timeGoBack = time; } /** * Makes the robot go backward. */ public void goBackward() { setSpeed(backwardSpeed); Motor.A.forward(); Motor.C.forward(); } /** * Makes the robot go forward. */ public void goForward() { setSpeed(forwardSpeed); Motor.A.backward(); Motor.C.backward(); } /** * Makes the robot turn to the left. * @param angle number of degrees the robot will turn. */ public void turnLeft(float angle) { try { goBackward(); timer.start(); Thread.sleep(timeGoBack); timer.stop(); setSpeed(turnSpeed); Motor.A.backward(); Motor.C.forward(); Sound.systemSound(true, 3); Thread.sleep(makeTime(angle)); goForward(); } catch (Exception e) { /*do nothing*/ } } /** * Makes the robot turn to the right. * @param angle number of degrees the robot will turn. */ public void turnRight(float angle) { try { goBackward(); timer.start(); Thread.sleep(timeGoBack); timer.stop(); setSpeed(turnSpeed); Motor.A.forward(); Motor.C.backward(); Sound.systemSound(true, 2); Thread.sleep(makeTime(angle)); goForward(); } catch (Exception e) { /*do nothing*/ } } /** * Makes the robot stop. */ public void stop() { Motor.A.stop(); Motor.C.stop(); } /** * Returns a aleatorious angle larger or equal to min and less or equal to max. * @param min the minimun return value. * @param max the maximun return value. */ private float makeAngle(float min, float max) { return (float)(min + (max - min) * Math.random()); } /** * Converts unit degrees to unit miliseconds. * @param angle the angle in degrees to be converted in to angle in miliseconds. */ private int makeTime(float angle) { return (int)(angle * kAngleTime); } /** * Performs the desired action when a sensor is pressed. * @param source the sensor that has been pressed. */ public synchronized void sensorTouched(Sensor source) { Sound.twoBeeps(); if (source.getId() == rightSensorId) turnRight(makeAngle(45F, 90F)); else if (source.getId() == leftSensorId) turnLeft(makeAngle(45F, 90F)); else goForward(); } /** * The ID of the left sensor on the robot. */ private int leftSensorId; /** * The MySensorListetener to be added to the left sensor. */ private MySensorListener leftSensorListener; /** * The ID of the right sensor on the robot. */ private int rightSensorId; /** * The MySensorListener to be added to the right sensor. */ private MySensorListener rightSensorListener; /** * The time the robot goes backward before turning when a sensor is pressed. */ private int timeGoBack = 2000; /** * Coefficient determining the realtionship between unit degrees and unit miliseconds. */ private final float kAngleTime = 4000 / 225; /** * The backward speed of the robot. */ private int backwardSpeed = 1; /** * The forward speed of the robot. */ private int forwardSpeed = 7; /** * The turnspeed of the robot. */ private final int turnSpeed = 4; /** * The minimum speed of the robot. */ private final int minSpeed = 1; /** * The maximum speed of the robot. */ private final int maxSpeed = 7; /** * The timer interval. */ private final int timerInterval = 500; /** * The timer listener. */ private MyTimerListener myTimerListener; /** * The timer. */ private Timer timer; }