lejos.navigation
Class TachoPilot

java.lang.Object
  extended by lejos.navigation.TachoPilot
All Implemented Interfaces:
Pilot
Direct Known Subclasses:
CompassPilot

public class TachoPilot
extends Object
implements Pilot

The TachoPilot class is a software abstraction of the Pilot mechanism of a NXT robot. It contains methods to control robot movements: travel forward or backward in a straight line or a circular path or rotate to a new direction.
Note: this class will only work with two independently controlled motors to steer differentially, so it can rotate within its own footprint (i.e. turn on one spot).
It can be used with robots that have reversed motor design: the robot moves in the direction opposite to the the direction of motor rotation. Uses the Motor class, which regulates motor speed using the NXT motor's built in tachometer.
Some methods optionally return immediately so the thread that called the method can monitor sensors and call stop() if necessary.
Uses the smoothAcceleration property of Motors to improve motor synchronization when starting a movement. Example:

 Pilot pilot = new TachoPilot(2.1f, 4.4f, Motor.A, Motor.C, true);  // parameters in inches
 pilot.setRobotSpeed(10);                                           // inches per second
 pilot.travel(12);                                                  // inches
 pilot.rotate(-90);                                                 // degree clockwise
 pilot.travel(-12,true);
 while(pilot.isMoving())Thread.yield();
 pilot.rotate(-90);
 pilot.rotateTo(270);
 pilot.steer(-50,180,true);
 while(pilot.isMoving())Thread.yield();
 pilot.steer(100);
 try{Thread.sleep(1000);}
 catch(InterruptedException e){}
 pilot.stop();
 

Note: if you are sure you do not want to use any other part of navigation you can as well use "TachoPilot pilot = new TachoPilot(...)" instead of "Pilot pilot = new TachoPilot(...)"


Field Summary
protected  Motor _left
          Left motor.
protected  float _leftDegPerDistance
          Left motor degrees per unit of travel.
protected  float _leftTurnRatio
          Left motor revolutions for 360 degree rotation of robot (motors running in opposite directions).
protected  float _leftWheelDiameter
          Diameter of left wheel.
protected  int _motorSpeed
          Motor speed degrees per second.
protected  Motor _right
          Right motor.
protected  float _rightDegPerDistance
          Right motor degrees per unit of travel.
protected  float _rightTurnRatio
          Right motor revolutions for 360 degree rotation of robot (motors running in opposite directions).
protected  float _rightWheelDiameter
          Diameter of right wheel.
protected  float _robotMoveSpeed
          Speed of robot for moving in wheel diameter units per seconds.
protected  float _robotTurnSpeed
          Speed of robot for turning in degree per seconds.
protected  float _trackWidth
          Distance between wheels.
 
Constructor Summary
TachoPilot(float leftWheelDiameter, float rightWheelDiameter, float trackWidth, Motor leftMotor, Motor rightMotor, boolean reverse)
          Allocates a TachoPilot object, and sets the physical parameters of the NXT robot.
TachoPilot(float wheelDiameter, float trackWidth, Motor leftMotor, Motor rightMotor)
          Allocates a TachoPilot object, and sets the physical parameters of the NXT robot.
Assumes Motor.forward() causes the robot to move forward.
TachoPilot(float wheelDiameter, float trackWidth, Motor leftMotor, Motor rightMotor, boolean reverse)
          Allocates a TachoPilot object, and sets the physical parameters of the NXT robot.
 
Method Summary
 void backward()
          Moves the NXT robot backward until stop() is called.
 void forward()
          Moves the NXT robot forward until stop() is called.
 float getAngle()
           
 Motor getLeft()
           
 int getLeftActualSpeed()
           
 int getLeftCount()
           
 float getMoveMaxSpeed()
           
 float getMoveSpeed()
           
 Motor getRight()
           
 int getRightActualSpeed()
           
 int getRightCount()
           
 float getTravelDistance()
           
 float getTurnMaxSpeed()
           
 float getTurnRatio()
           
 float getTurnSpeed()
           
 boolean isMoving()
           
 void regulateSpeed(boolean yes)
          Sets motor speed regulation (default is true).
Allows steer() method to be called by (for example) a line tracker or compass navigator so direction control is from sensor inputs.
 void reset()
          Resets tacho count for both motors.
 void rotate(float angle)
          Rotates the NXT robot through a specific angle.
 void rotate(float angle, boolean immediateReturn)
          Rotates the NXT robot through a specific angle.
 void setMoveSpeed(float speed)
          also sets _motorSpeed
 void setSpeed(int speed)
          Sets speed of both motors, as well as moveSpeed and turnSpeed.
 void setTurnSpeed(float speed)
          Sets the turning speed of the robot.
 boolean stalled()
           
 void steer(int turnRate)
          Moves the NXT robot in a circular path at a specific turn rate.
 void steer(int turnRate, int angle)
          Moves the NXT robot in a circular path at a specific turn rate.
 void steer(int turnRate, int angle, boolean immediateReturn)
          Moves the NXT robot in a circular path at a specific turn rate.
 void stop()
          Stops the NXT robot.
 void travel(float distance)
          Moves the NXT robot a specific distance in an (hopefully) straight line.
A positive distance causes forward motion, a negative distance moves backward.
 void travel(float distance, boolean immediateReturn)
          Moves the NXT robot a specific distance in an (hopefully) straight line.
A positive distance causes forward motion, a negative distance moves backward.
 void turn(float radius)
          Moves the NXT robot in a circular path with a specified radius.
 void turn(float radius, int angle)
          Moves the NXT robot in a circular arc through the specificd angle;
The center of the turning circle is on the right side of the robot iff parameter radius is negative.
 void turn(float radius, int angle, boolean immediateReturn)
          Move in a circular arc with specified radius; the center of the turning circle
is on the right side of the robot if the radius is negative.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_left

protected final Motor _left
Left motor.


_right

protected final Motor _right
Right motor.


_leftDegPerDistance

protected final float _leftDegPerDistance
Left motor degrees per unit of travel.


_rightDegPerDistance

protected final float _rightDegPerDistance
Right motor degrees per unit of travel.


_leftTurnRatio

protected final float _leftTurnRatio
Left motor revolutions for 360 degree rotation of robot (motors running in opposite directions). Calculated from wheel diameter and track width. Used by rotate() and steer() methods.


_rightTurnRatio

protected final float _rightTurnRatio
Right motor revolutions for 360 degree rotation of robot (motors running in opposite directions). Calculated from wheel diameter and track width. Used by rotate() and steer() methods.


_robotMoveSpeed

protected float _robotMoveSpeed
Speed of robot for moving in wheel diameter units per seconds. Set by setSpeed(), setMoveSpeed()


_robotTurnSpeed

protected float _robotTurnSpeed
Speed of robot for turning in degree per seconds.


_motorSpeed

protected int _motorSpeed
Motor speed degrees per second. Used by forward(),backward() and steer().


_trackWidth

protected final float _trackWidth
Distance between wheels. Used in steer() and rotate().


_leftWheelDiameter

protected final float _leftWheelDiameter
Diameter of left wheel.


_rightWheelDiameter

protected final float _rightWheelDiameter
Diameter of right wheel.

Constructor Detail

TachoPilot

public TachoPilot(float wheelDiameter,
                  float trackWidth,
                  Motor leftMotor,
                  Motor rightMotor)
Allocates a TachoPilot object, and sets the physical parameters of the NXT robot.
Assumes Motor.forward() causes the robot to move forward.

Parameters:
wheelDiameter - Diameter of the tire, in any convenient units (diameter in mm is usually printed on the tire).
trackWidth - Distance between center of right tire and center of left tire, in same units as wheelDiameter.
leftMotor - The left Motor (e.g., Motor.C).
rightMotor - The right Motor (e.g., Motor.A).

TachoPilot

public TachoPilot(float wheelDiameter,
                  float trackWidth,
                  Motor leftMotor,
                  Motor rightMotor,
                  boolean reverse)
Allocates a TachoPilot object, and sets the physical parameters of the NXT robot.

Parameters:
wheelDiameter - Diameter of the tire, in any convenient units (diameter in mm is usually printed on the tire).
trackWidth - Distance between center of right tire and center of left tire, in same units as wheelDiameter.
leftMotor - The left Motor (e.g., Motor.C).
rightMotor - The right Motor (e.g., Motor.A).
reverse - If true, the NXT robot moves forward when the motors are running backward.

TachoPilot

public TachoPilot(float leftWheelDiameter,
                  float rightWheelDiameter,
                  float trackWidth,
                  Motor leftMotor,
                  Motor rightMotor,
                  boolean reverse)
Allocates a TachoPilot object, and sets the physical parameters of the NXT robot.

Parameters:
leftWheelDiameter - Diameter of the left wheel, in any convenient units (diameter in mm is usually printed on the tire).
rightWheelDiameter - Diameter of the right wheel. You can actually fit intentionally wheels with different size to your robot. If you fitted wheels with the same size, but your robot is not going straight, try swapping the wheels and see if it deviates into the other direction. That would indicate a small difference in wheel size. Adjust wheel size accordingly. The minimum change in wheel size which will actually have an effect is given by minChange = A*wheelDiameter*wheelDiameter/(1-(A*wheelDiameter) where A = PI/(moveSpeed*360). Thus for a moveSpeed of 25 cm/second and a wheelDiameter of 5,5 cm the minChange is about 0,01058 cm... The reason for this is, that different while sizes will result in different motor speed. And that is given as an integer in degree per second.
trackWidth - Distance between center of right tire and center of left tire, in same units as wheelDiameter.
leftMotor - The left Motor (e.g., Motor.C).
rightMotor - The right Motor (e.g., Motor.A).
reverse - If true, the NXT robot moves forward when the motors are running backward.
Method Detail

getLeft

public Motor getLeft()
Returns:
left motor.

getRight

public Motor getRight()
Returns:
right motor.

getLeftCount

public int getLeftCount()
Returns:
tachoCount of left motor. Positive value means motor has moved the robot forward.

getRightCount

public int getRightCount()
Returns:
tachoCount of the right motor. Positive value means motor has moved the robot forward.

getLeftActualSpeed

public int getLeftActualSpeed()
Returns:
actual speed of left motor in degrees per second. A negative value if motor is rotating backwards. Updated every 100 ms.

getRightActualSpeed

public int getRightActualSpeed()
Returns:
actual speed of right motor in degrees per second. A negative value if motor is rotating backwards. Updated every 100 ms.

getTurnRatio

public float getTurnRatio()
Returns:
ratio of motor revolutions per 360 degree rotation of the robot. If your robot has wheels with different size, it is the average.

setSpeed

public void setSpeed(int speed)
Sets speed of both motors, as well as moveSpeed and turnSpeed. Only use if your wheels have the same size.

Specified by:
setSpeed in interface Pilot
Parameters:
speed - The wanted speed in degrees per second.

setMoveSpeed

public void setMoveSpeed(float speed)
also sets _motorSpeed

Specified by:
setMoveSpeed in interface Pilot
Parameters:
speed - The speed in wheel diameter units per second.
See Also:
Pilot.setMoveSpeed(float)

getMoveSpeed

public float getMoveSpeed()
Specified by:
getMoveSpeed in interface Pilot
Returns:
the movement speed of the robot in wheel diameter units per second.
See Also:
Pilot.getMoveSpeed()

getMoveMaxSpeed

public float getMoveMaxSpeed()
Specified by:
getMoveMaxSpeed in interface Pilot
Returns:
the maximal movement speed of the robot in wheel diameter units per second which can be maintained accurately. Will change with time, as it is normally dependent on the battery voltage.
See Also:
Pilot.getMoveMaxSpeed()

setTurnSpeed

public void setTurnSpeed(float speed)
Description copied from interface: Pilot
Sets the turning speed of the robot.

Specified by:
setTurnSpeed in interface Pilot
Parameters:
speed - The speed in degree per second.
See Also:
Pilot.setTurnSpeed(float)

getTurnSpeed

public float getTurnSpeed()
Specified by:
getTurnSpeed in interface Pilot
Returns:
the turning speed of the robot in degree per second.
See Also:
Pilot.getTurnSpeed()

getTurnMaxSpeed

public float getTurnMaxSpeed()
Specified by:
getTurnMaxSpeed in interface Pilot
Returns:
the maximal turning speed of the robot in degree per second which can be maintained accurately. Will change with time, as it is normally dependent on the battery voltage.
See Also:
Pilot.getTurnMaxSpeed()

forward

public void forward()
Moves the NXT robot forward until stop() is called.

Specified by:
forward in interface Pilot
See Also:
Navigator.stop()

backward

public void backward()
Moves the NXT robot backward until stop() is called.

Specified by:
backward in interface Pilot
See Also:
Navigator.stop()

rotate

public void rotate(float angle)
Rotates the NXT robot through a specific angle. Returns when angle is reached. Wheels turn in opposite directions producing a zero radius turn.
Note: Requires correct values for wheel diameter and track width.

Specified by:
rotate in interface Pilot
Parameters:
angle - The wanted angle of rotation in degrees. Positive angle rotate left (clockwise), negative right.

rotate

public void rotate(float angle,
                   boolean immediateReturn)
Rotates the NXT robot through a specific angle. Returns when angle is reached. Wheels turn in opposite directions producing a zero radius turn.
Note: Requires correct values for wheel diameter and track width.

Specified by:
rotate in interface Pilot
Parameters:
angle - The wanted angle of rotation in degrees. Positive angle rotate left (clockwise), negative right.
immediateReturn - If true this method returns immediately.

getAngle

public float getAngle()
Specified by:
getAngle in interface Pilot
Returns:
the angle of rotation of the robot since last call to reset of tacho count;

stop

public void stop()
Stops the NXT robot.

Specified by:
stop in interface Pilot
See Also:
Navigator.forward()

isMoving

public boolean isMoving()
Specified by:
isMoving in interface Pilot
Returns:
true if the NXT robot is moving.

reset

public void reset()
Resets tacho count for both motors.

Specified by:
reset in interface Pilot

getTravelDistance

public float getTravelDistance()
Specified by:
getTravelDistance in interface Pilot
Returns:
distance traveled since last reset of tacho count.

travel

public void travel(float distance)
Moves the NXT robot a specific distance in an (hopefully) straight line.
A positive distance causes forward motion, a negative distance moves backward. If a drift correction has been specified in the constructor it will be applied to the left motor.

Specified by:
travel in interface Pilot
Parameters:
distance - The distance to move. Unit of measure for distance must be same as wheelDiameter and trackWidth.

travel

public void travel(float distance,
                   boolean immediateReturn)
Moves the NXT robot a specific distance in an (hopefully) straight line.
A positive distance causes forward motion, a negative distance moves backward. If a drift correction has been specified in the constructor it will be applied to the left motor.

Specified by:
travel in interface Pilot
Parameters:
distance - The distance to move. Unit of measure for distance must be same as wheelDiameter and trackWidth.
immediateReturn - If true this method returns immediately.

steer

public void steer(int turnRate)
Moves the NXT robot in a circular path at a specific turn rate. The center of the turning circle is on the right side of the robot if parameter turnRate is negative. Values for turnRate are between -200 and +200. The turnRate determines the ratio of inner wheel speed to outer wheel speed (as a percent).
Formula: ratio = 100 - abs(turnRate). When the ratio is negative, the outer and inner wheels rotate in opposite directions.
Examples: Note: Not supported for a robot with wheels of different size.

Specified by:
steer in interface Pilot
Parameters:
turnRate - If positive, the left wheel is on the inside of the turn. If negative, the left wheel is on the outside.

steer

public void steer(int turnRate,
                  int angle)
Moves the NXT robot in a circular path at a specific turn rate. The center of the turning circle is on the right side of the robot if parameter turnRate is negative. Values for turnRate are between -200 and +200. The turnRate determines the ratio of inner wheel speed to outer wheel speed (as a percent).
Formula: ratio = 100 - abs(turnRate). When the ratio is negative, the outer and inner wheels rotate in opposite directions.
Examples: Note: Not supported for a robot with wheels of different size.

Specified by:
steer in interface Pilot
Parameters:
turnRate - If positive, the left wheel is on the inside of the turn. If negative, the left wheel is on the outside.
angle - The angle through which the robot will rotate. If negative, robot traces the turning circle backwards.

steer

public void steer(int turnRate,
                  int angle,
                  boolean immediateReturn)
Moves the NXT robot in a circular path at a specific turn rate. The center of the turning circle is on the right side of the robot if parameter turnRate is negative. Values for turnRate are between -200 and +200. The turnRate determines the ratio of inner wheel speed to outer wheel speed (as a percent).
Formula: ratio = 100 - abs(turnRate). When the ratio is negative, the outer and inner wheels rotate in opposite directions.
Examples: Note: Not supported for a robot with wheels of different size.

Specified by:
steer in interface Pilot
Parameters:
turnRate - If positive, the left wheel is on the inside of the turn. If negative, the left wheel is on the outside.
angle - The angle through which the robot will rotate. If negative, robot traces the turning circle backwards.
immediateReturn - If true this method returns immediately.

stalled

public boolean stalled()
Returns:
true if either motor actual speed is zero.

regulateSpeed

public void regulateSpeed(boolean yes)
Sets motor speed regulation (default is true).
Allows steer() method to be called by (for example) a line tracker or compass navigator so direction control is from sensor inputs.

Parameters:
yes - Set motor speed regulation on = true or off = false.

turn

public void turn(float radius)
Moves the NXT robot in a circular path with a specified radius.
The center of the turning circle is on the right side of the robot iff parameter radius is negative;
Postcondition: Motor speeds are unpredictable. Note: Not supported for a robot with wheels of different size.

Specified by:
turn in interface Pilot
Parameters:
radius - of the circular path. If positive, the left wheel is on the inside of the turn. If negative, the left wheel is on the outside.

turn

public void turn(float radius,
                 int angle)
Moves the NXT robot in a circular arc through the specificd angle;
The center of the turning circle is on the right side of the robot iff parameter radius is negative. Robot will stop when total rotation equals angle. If angle is negative, robot will move travel backwards. Note: Not supported for a robot with wheels of different size.

Specified by:
turn in interface Pilot
Parameters:
radius - radius of the turning circle
angle - The sign of the angle determines the direction of robot motion

turn

public void turn(float radius,
                 int angle,
                 boolean immediateReturn)
Move in a circular arc with specified radius; the center of the turning circle
is on the right side of the robot if the radius is negative. Note: Not supported for a robot with wheels of different size.

Specified by:
turn in interface Pilot
Parameters:
radius - radius of the turning circle
angle - The sign of the angle determines the direction of robot motion
immediateReturn - If true this method returns immediately.