001/* This source code is released under the new BSD license, a copy of the
002 * license is in the distribution directory. */
003
004package mazerob.nxt;
005
006import java.lang.RuntimeException;
007import lejos.nxt.Motor;
008import lejos.nxt.SensorPort;
009import lejos.nxt.UltrasonicSensor;
010import lejos.robotics.RotatingRangeScanner;
011import lejos.robotics.RangeReadings;
012import lejos.robotics.navigation.DifferentialPilot;
013import mazerob.conn.RemotelyControllable;
014
015/**
016 * NXT Explorer abstraction
017 *
018 * @author Pedro I. López
019 *
020 * @see <a
021 * href="http://www.nxtprograms.com/NXT2/explorer/steps.html">NXT Explorer</a>
022 *
023 */
024class Robot implements RemotelyControllable {
025
026    /** Abstraction of the Explorer's ultrasonic sensor */
027    RotatingRangeScanner scanner;
028
029    /** Instance of the Pilot mechanism to control the Explorer movements */
030    DifferentialPilot pilot;
031
032    /** Magnitude of translation in mm of methods {@link
033     * mazerob.nxt.Robot#translateForward} and {@link
034     * mazerob.nxt.Robot#translateBackward} specified by the
035     * {@code translationMagnitude} argument in {@link
036     * mazerob.nxt.Robot#Robot}
037     */
038    double translationMagnitude;
039
040    /** Magnitude of rotation in degrees of methods {@link
041     * mazerob.nxt.Robot#rotateRight} and {@link
042     * mazerob.nxt.Robot#rotateLeft} specified by the
043     * {@code rotationMagnitude} argument in {@link
044     * mazerob.nxt.Robot#Robot}
045     */
046    double rotationMagnitude;
047
048    /**
049     * @param wheelDiameter Diameter of the tires in mm
050     * @param trackWidth Distance between center of right tire and
051     * center of left tire in mm
052     * @param reverse If true, the NXT robot moves forward when the
053     * motors are running backward
054     * @param rotationSpeed Rotation speed of the vehicle, in degrees
055     * per second
056     * @param translationMagnitude Magnitude of translation in mm of methods
057     * {@link mazerob.nxt.Robot#translateForward} and {@link
058     * mazerob.nxt.Robot#translateBackward}
059     * @param rotationMagnitude Magnitude of rotation in degrees of methods
060     * {@link mazerob.nxt.Robot#rotateRight} and {@link
061     * mazerob.nxt.Robot#rotateLeft}
062     *
063     */
064    public Robot(double wheelDiameter,
065                      double trackWidth,
066                      boolean reverse,
067                      double rotationSpeed,
068                      double translationMagnitude,
069                      double rotationMagnitude) {
070        final String CONFIGURED_MSG = "Configuration complete";
071
072        this.translationMagnitude = translationMagnitude;
073        this.rotationMagnitude = rotationMagnitude;
074        pilot = new DifferentialPilot(wheelDiameter, trackWidth,
075            Motor.B, Motor.C, reverse);
076        pilot.setRotateSpeed(rotationSpeed);
077        scanner = new RotatingRangeScanner(Motor.A,
078            new UltrasonicSensor(SensorPort.S1));
079        scanner.setAngles( SCANNING_ANGLES );
080
081        System.out.println(CONFIGURED_MSG);
082    }
083
084    /**
085     * Wait until translation is done
086     *
087     * @see mazerob.conn.RemotelyControllable#translate
088     */
089    public void translate(double distance) {
090        pilot.travel(distance);
091    }
092
093    /**
094     * Translate forward {@link Robot#translationMagnitude} mm,  wait until
095     * translation is done
096     *
097     * @see mazerob.conn.RemotelyControllable#translateForward
098     */
099    public void translateForward() {
100        pilot.travel(translationMagnitude);
101    }
102
103    /**
104     * Translate backward {@link Robot#translationMagnitude} mm,  wait until
105     * translation is done
106     *
107     * @see mazerob.conn.RemotelyControllable#translateBackward
108     */
109    public void translateBackward() {
110        pilot.travel(-translationMagnitude);
111    }
112
113    /**
114     * Wait until rotation is done
115     *
116     * @see mazerob.conn.RemotelyControllable#rotate
117     */
118    public void rotate(double angle) {
119        pilot.rotate(angle);
120    }
121
122    /**
123     * Rotate to the right {@link Robot#rotationMagnitude} mm,  wait until
124     * rotation is done
125     *
126     * @see mazerob.conn.RemotelyControllable#rotateRight
127     */
128    public void rotateRight() {
129        pilot.rotate(rotationMagnitude);
130    }
131
132    /**
133     * Rotate to the left {@link Robot#rotationMagnitude} mm,  wait until
134     * rotation is done
135     *
136     * @see mazerob.conn.RemotelyControllable#rotateLeft
137     */
138    public void rotateLeft() {
139        pilot.rotate(-rotationMagnitude);
140    }
141
142    /**
143     * End the connection and exit the {@link mazerob.nxt.RobotApp}
144     * program
145     *
146     * @see mazerob.conn.RemotelyControllable#end
147     */
148    public void end() throws RuntimeException {
149        System.out.println(CLOSING_CONN_MSG);
150        throw new RuntimeException();
151    }
152
153    /**
154     * @see mazerob.conn.RemotelyControllable#scan
155     */
156    public RangeReadings scan() {
157        RangeReadings rangeValues = scanner.getRangeValues();
158        return rangeValues;
159    }
160
161}