Using the Navigator API
The leJOS Navigator API provides a convenient set of methods to control a robot. There are methods for moving to any location and controlling the direction of movement. A Navigator object automatically keeps track of the current angle and x, y coordinates after every movement. The great part about this class is that it can be used by any robot with differential steering, regardless of the construction of the robot. Differential steering has one requirement: The robot must be able to turn within its own footprint; that is, it must be able to change direction without changing its x and y coordinates. The robot can have wheels with a diameter of 8 cm or 3 cm, or no wheels at all (treads, legs), or it can be fast or slowit doesn't matter! These sorts of physical parameters are addressed by the constructor of the Navigator class. Once these parameters are set, the Navigator class works the same for all differential robots. Let's examine the actual interface:
josx.robotics.Navigator
public void forward()
Moves the RCX robot forward until stop() is called.
public void backward()
Moves the RCX robot backward until stop() is called.
public void travel(int distance)
Moves the RCX robot a specific distance. A positive value moves it forward and a negative value moves it backward. The method returns when movement is done.
Parameters: |
distance |
The distance to travel, in inches or centimeters. |
public void stop()
Halts the RCX robot and calculates new x, y coordinates.
public void rotate(float angle)
Rotates the RCX robot a specific number of degrees, in a positive or negative direction (+ or ). The angle can be any positive or negative integer. For example, rotate(720) will cause two complete rotations counterclockwise. This method returns only once the rotation is complete.
Parameters: |
angle |
The angle to rotate in degrees. A positive value is counterclockwise and a negative value is clockwise. |
public void gotoAngle(float angle)
Rotates the RCX robot to point in a specific direction. It will take the shortest path necessary to rotate to the desired angle. The method returns once rotation is complete.
Parameters: |
angle |
The angle to rotate to, in degrees. |
public void gotoPoint(float x, float y)
Moves to any point on the coordinate system. The method rotates the RCX robot toward the target point and travels the required distance. The stop() method can be called at any time to stop movement and recalculate the x, y coordinates.
Parameters: |
x |
The target x coordinate. |
|
y |
The target y coordinate. |
public float getX()
Returns the current x coordinate of the robot. If the robot is moving it calculates the present coordinate and returns the value.
public float getY()
Returns the current y coordinate of the robot. If the robot is moving it calculates the present coordinate and returns the value.
public float getAngle()
Returns the current angle the robot is facing. If the robot is rotating it does not calculate the present angle, so make sure the robot is stopped when calling this method.
The Navigator interface is used for two classes in the josx.robotics package: TimingNavigator and RotationNavigator. For this chapter we use TimingNavigator exclusively because it is the easiest to use and requires no additional LEGO parts. TimingNavigator relies on keeping track of coordinates by measuring movement in terms of the number of seconds it has traveled or rotated. Robots have different wheel radii, different gearing, different motor strengths, and different axle lengths, so the timing constants for a particular robot will differ. To accommodate this, the TimingNavigator class requires the time it takes for the robot to move any given distance and the time it takes to rotate any given angle. Both of these numbers could be given in the form of speeds, but to make things easier the constructor just asks for the time it takes to travel 100 units (inches or centimeters, whatever you prefer to use), and the time it takes to complete one full rotation. The constructor also requires two Motor objects, one for the left wheel and one for the right. This is to let it know which output ports are being used to drive the movement. The constructor is as follows:
josx.robotics.TimingNavigator
public TimingNavigator(Motor right, Motor left, float timeOneMeter, float timeRotate)
Allocates a Navigator object and initializes it with the left and right wheels. The x and y values will each equal zero on initialization, and the starting angle is 0 degrees.
Parameters: |
right |
The motor used to drive the right wheel (e.g., Motor.C). |
|
left |
The motor used to drive the left wheel (e.g., Motor.A). |
|
timeOneMeter |
The number of seconds it takes the robot to drive 100 units. |
|
timeRotate |
The number of seconds it takes the robot to rotate 360 degrees. |
public void setMomentumDelay(short delay)
Sets a variable that adds extra time (in milliseconds) to each rotation. This gives the robot more time to overcome momentum when starting a rotation. Proper use of this variable increases accuracy dramatically.