1 package lejos.navigation;
5 * The CompassNavigatort class, like its superclass, can keep track of the robot position and the direction angle it faces. It uses a CompassPilot object to control NXT robot movements.<br>
6 * The position and direction angle values are updated automatically when the movement command returns after the movement is complete and and after stop() command is issued.
7 * However, some commands optionally return immediately, to permit sensor monitoring in the main thread. It is then the programmers responsibility to
8 * call updateXY() when the robot motion is completed. All angles are in degrees, distances in the units used to specify robot dimensions.
9 * As with pilot, the robot must be have two independently controlled drive wheels.
10 * Uses the Compass Sensor to determine the robot heading.
13 public class CompassNavigator extends TachoNavigator
16 private CompassPilot compassPilot; //
19 * Allocates a CompassNavigator objects and its CompassPilot object and initializes it with the proper motors and dimensions.
20 * This is a subclass of TachoNavigator (see that API for other methods).
21 * The x and y values and the direction angle are all initialized to 0, so if the first move is forward() the robot will run along
23 * @param compassPort the sensor port connected to the compass sensor e.g SensorPort.S1
24 * @param wheelDiameter The diameter of the wheel, usually printed right on the
25 * wheel, in centimeters (e.g. 49.6 mm = 4.96 cm = 1.95 in)
26 * @param trackWidth The distance from the center of the left tire to the center
27 * of the right tire, in units of your choice
28 * @param rightMotor The motor used to drive the right wheel e.g. Motor.C.
29 * @param leftMotor The motor used to drive the left wheel e.g. Motor.A.
32 public CompassNavigator(SensorPort compassPort, float wheelDiameter, float trackWidth, Motor leftMotor, Motor rightMotor)
34 this(compassPort, wheelDiameter,trackWidth,leftMotor, rightMotor, false);
37 public CompassNavigator(SensorPort compassPort, float wheelDiameter, float trackWidth, Motor leftMotor, Motor rightMotor, boolean reverse)
39 this(new CompassSensor(compassPort), wheelDiameter,trackWidth,leftMotor, rightMotor,reverse);
42 public CompassNavigator(CompassSensor compass, float wheelDiameter, float trackWidth, Motor leftMotor, Motor rightMotor, boolean reverse)
44 super(new CompassPilot(compass, wheelDiameter,trackWidth,leftMotor, rightMotor,reverse));
45 this.compassPilot = (CompassPilot) _pilot;
46 _heading = getAngle();
50 * To use this constructor, you must first create a compass pilot.
53 public CompassNavigator(CompassPilot compassPilot)
56 compassPilot = (CompassPilot) _pilot;
57 _heading = getAngle();
61 * returns the pilot of this navigator
62 * @return compass pilot
64 CompassPilot getCompassPilot(){return compassPilot;}
67 * Robot rotates 360 degrees while calibrating the compass sensor
69 public void calibrateCompass() {compassPilot.calibrate();}
72 * Rotates the NXT robot to point in a specific direction. It will take the shortest
73 * path necessary to point to the desired angle.
74 * If immediateReturnis true, method returns immidiately and your code MUST call updatePostion()
75 * when the robot has stopped. Otherwise, the robot position is lost.
76 * @param angle The angle to rotate to, in degrees.
77 * @param immediateReturn iff true, method returns immediately and the programmer is responsible for calling
78 * updatePosition() before the robot moves again.
80 public void rotateTo(float angle, boolean immediateReturn)
82 compassPilot.rotateTo((int)angle,false); //???
83 if(immediateReturn)return;
84 while(compassPilot.isRotating())Thread.yield();
89 * Rotates the NXT robot by a specified angle.
90 * If immediateReturnis true, method returns immidiately and your code MUST call updatePostion()
91 * when the robot has stopped. Otherwise, the robot position is lost.
92 * @param angle The angle to rotate to, in degrees.
93 * @param immediateReturn iff true, method returns immediately and the programmer is responsible for calling
94 * updatePosition() before the robot moves again.
96 public void rotate(float angle, boolean immediateReturn)
98 compassPilot.rotate((int)angle,immediateReturn);
103 * Moves the NXT robot a specific distance. A positive value moves it forwards and
104 * a negative value moves it backwards.
105 * If immediateReturnis true, method returns immidiately and your code MUST call updatePostion()
106 * when the robot has stopped. Otherwise, the robot position is lost.
107 * @param distance The positive or negative distance to move the robot, same units as _wheelDiameter
108 * @param immediateReturn iff true, the method returns immediately, in which case the programmer <br>
109 * is responsible for calling updatePosition() before the robot moves again.
111 public void travel(float distance,boolean immediateReturn)
113 compassPilot.resetTachoCount();
114 compassPilot.travel(distance,immediateReturn);
115 if(immediateReturn)return;
116 while(compassPilot.isTraveling())Thread.yield();
121 * Halts the NXT robot and calculates new x, y coordinates.
130 * Direction of robot facing is set equal to the current compass reading
132 public void updateHeading()
134 _heading = (int)compassPilot.compass.getDegreesCartesian();
138 * Updates x,y coordinates; assumes last compass angle was constant during travel
140 public void updateXY()
143 float angle = (float)Math.toRadians(_heading);
144 float dx = compassPilot.getTravelDistance() *(float) Math.cos(angle);
145 float dy = compassPilot.getTravelDistance() * (float)Math.sin(angle);
146 setPosition(dx + getX(),dy+getY(),_heading);