lejos.nxt
Class Motor

java.lang.Object
  extended by lejos.nxt.BasicMotor
      extended by lejos.nxt.Motor
All Implemented Interfaces:
DCMotor, Encoder, Tachometer, TachoMotor

public class Motor
extends BasicMotor
implements TachoMotor

Abstraction for a NXT motor. Three instances of Motor are available: Motor.A, Motor.B and Motor.C. To control each motor use methods forward, backward, reverseDirection, stop and flt. To set each motor's speed, use setSpeed. Speed is in degrees per second. .\ Methods that use the tachometer: regulateSpeed, rotate, rotateTo. These rotate methods may not stop smoothly at the target angle if called when the motor is already moving
Motor has 2 modes : speedRegulation and smoothAcceleration which only works if speed regulation is used. These are initially enabled. The speed is regulated by comparing the tacho count with speed times elapsed time and adjusting motor power to keep these closely matched. Smooth acceleration corrects the speed regulation to account for the acceleration time. They can be switched off/on by the methods regulateSpeed() and smoothAcceleration(). The actual maximum speed of the motor depends on battery voltage and load. With no load, the maximum is about 100 times the voltage. Speed regulation fails if the target speed exceeds the capability of the motor. If you need the motor to hold its position and you find that still moves after stop() is called , you can use the lock() method.

Example:

   Motor.A.setSpeed(720);// 2 RPM
   Motor.C.setSpeed(720);
   Motor.A.forward();
   Motor.C.forward();
   Thread.sleep (1000);
   Motor.A.stop();
   Motor.C.stop();
   Motor.A.regulateSpeed(true);
   Motor.A.rotateTo( 360);
   Motor.A.rotate(-720,true);
   while(Motor.A.isRotating();
   int angle = Motor.A.getTachoCount(); // should be -360
 

Author:
Roger Glassey revised 9 Feb 2008 - added lock() method.

Nested Class Summary
 class Motor.Regulator
          inner class to regulate speed; also stop motor at desired rotation angle
 
Field Summary
 TachoMotorPort _port
           
 boolean _rampUp
          set by smoothAcceleration, forward(),backward(), setSpeed().
static Motor A
          Motor A.
static Motor B
          Motor B.
static Motor C
          Motor C.
 Motor.Regulator regulator
           
 
Constructor Summary
Motor(TachoMotorPort port)
          Use this constructor to assign a variable of type motor connected to a particular port.
 
Method Summary
 void backward()
          Causes motor to rotate backwards.
 void flt()
          Causes motor to float.
 void forward()
          Causes motor to rotate forward.
 float getBasePower()
          for debugging
 float getError()
          for debugging
 int getLimitAngle()
          Return the angle that a Motor is rotating to.
 int getMode()
          Returns the mode.
 int getPower()
          Returns the current power setting.
 int getRotationSpeed()
          Returns the actual speed.
 int getSpeed()
          Returns the current motor speed in degrees per second
 int getStopAngle()
           
 int getTachoCount()
          Returns the tachometer count.
 boolean isMoving()
          Returns true iff the motor is in motion.
 boolean isRegulating()
           
 void lock(int power)
          Applies power to hold motor in current position.
 void regulateSpeed(boolean yes)
          turns speed regulation on/off;
Cumulative speed error is within about 1 degree after initial acceleration.
 void resetTachoCount()
          Resets the tachometer count to zero.
 void reverseDirection()
          Reverses direction of the motor.
 void rotate(int angle)
          Causes motor to rotate by a specified angle.
 void rotate(int angle, boolean immediateReturn)
          causes motor to rotate through angle;
iff immediateReturn is true, method returns immediately and the motor stops by itself
If any motor method is called before the limit is reached, the rotation is canceled.
 void rotateTo(int limitAngle)
          Causes motor to rotate to limitAngle;
Then getTachoCount should be within +- 2 degrees of the limit angle when the method returns
 void rotateTo(int limitAngle, boolean immediateReturn)
          causes motor to rotate to limitAngle;
if immediateReturn is true, method returns immediately and the motor stops by itself
and getTachoCount should be within +- 2 degrees if the limit angle If any motor method is called before the limit is reached, the rotation is canceled.
 void setBrakePower(int pwr)
           
 void setPower(int power)
          sets motor power.
 void setSpeed(int speed)
          Sets motor speed , in degrees per second; Up to 900 is possible with 8 volts.
 void shutdown()
          causes run() to exit
 void smoothAcceleration(boolean yes)
          enables smoother acceleration.
 void stop()
          Causes motor to stop, pretty much instantaneously.
 
Methods inherited from class lejos.nxt.BasicMotor
isBackward, isFloating, isForward, isStopped
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_port

public TachoMotorPort _port

regulator

public Motor.Regulator regulator

_rampUp

public boolean _rampUp
set by smoothAcceleration, forward(),backward(), setSpeed(). Only has effect if _regulate is true


A

public static final Motor A
Motor A.


B

public static final Motor B
Motor B.


C

public static final Motor C
Motor C.

Constructor Detail

Motor

public Motor(TachoMotorPort port)
Use this constructor to assign a variable of type motor connected to a particular port.

Parameters:
port - to which this motor is connected
Method Detail

getStopAngle

public int getStopAngle()

forward

public void forward()
Description copied from class: BasicMotor
Causes motor to rotate forward.

Specified by:
forward in interface DCMotor
Overrides:
forward in class BasicMotor
See Also:
BasicMotor.forward()

backward

public void backward()
Description copied from class: BasicMotor
Causes motor to rotate backwards.

Specified by:
backward in interface DCMotor
Overrides:
backward in class BasicMotor
See Also:
BasicMotor.backward()

reverseDirection

public void reverseDirection()
Reverses direction of the motor. It only has effect if the motor is moving.

Overrides:
reverseDirection in class BasicMotor

flt

public void flt()
Description copied from class: BasicMotor
Causes motor to float. The motor will lose all power, but this is not the same as stopping. Use this method if you don't want your robot to trip in abrupt turns.

Specified by:
flt in interface DCMotor
Overrides:
flt in class BasicMotor

stop

public void stop()
Causes motor to stop, pretty much instantaneously. In other words, the motor doesn't just stop; it will resist any further motion. Cancels any rotate() orders in progress

Specified by:
stop in interface DCMotor
Overrides:
stop in class BasicMotor

lock

public void lock(int power)
Applies power to hold motor in current position. Use if stop() is not good enough
to hold the motor in positi0n against a load.

Parameters:
power - - a value between 1 and 100;

isMoving

public boolean isMoving()
Description copied from class: BasicMotor
Returns true iff the motor is in motion.

Specified by:
isMoving in interface DCMotor
Overrides:
isMoving in class BasicMotor
Returns:
true iff the motor is currently in motion.

rotate

public void rotate(int angle)
Description copied from interface: TachoMotor
Causes motor to rotate by a specified angle. The resulting tachometer count should be within +- 2 degrees on the NXT. This method does not return until the rotation is completed.

Specified by:
rotate in interface TachoMotor
Parameters:
angle - by which the motor will rotate.

rotate

public void rotate(int angle,
                   boolean immediateReturn)
Description copied from interface: TachoMotor
causes motor to rotate through angle;
iff immediateReturn is true, method returns immediately and the motor stops by itself
If any motor method is called before the limit is reached, the rotation is canceled. When the angle is reached, the method isRotating() returns false;

Specified by:
rotate in interface TachoMotor
Parameters:
angle - through which the motor will rotate
immediateReturn - iff true, method returns immediately, thus allowing monitoring of sensors in the calling thread.
See Also:
TachoMotor.rotate(int, boolean)

rotateTo

public void rotateTo(int limitAngle)
Description copied from interface: TachoMotor
Causes motor to rotate to limitAngle;
Then getTachoCount should be within +- 2 degrees of the limit angle when the method returns

Specified by:
rotateTo in interface TachoMotor
Parameters:
limitAngle - to which the motor will rotate, and then stop (in degrees). Includes any positive or negative int, even values > 360.

rotateTo

public void rotateTo(int limitAngle,
                     boolean immediateReturn)
Description copied from interface: TachoMotor
causes motor to rotate to limitAngle;
if immediateReturn is true, method returns immediately and the motor stops by itself
and getTachoCount should be within +- 2 degrees if the limit angle If any motor method is called before the limit is reached, the rotation is canceled. When the angle is reached, the method isRotating() returns false;

Specified by:
rotateTo in interface TachoMotor
Parameters:
limitAngle - to which the motor will rotate, and then stop (in degrees). Includes any positive or negative int, even values > 360.
immediateReturn - iff true, method returns immediately, thus allowing monitoring of sensors in the calling thread.

shutdown

public void shutdown()
causes run() to exit


regulateSpeed

public void regulateSpeed(boolean yes)
turns speed regulation on/off;
Cumulative speed error is within about 1 degree after initial acceleration.

Specified by:
regulateSpeed in interface TachoMotor
Parameters:
yes - is true for speed regulation on

smoothAcceleration

public void smoothAcceleration(boolean yes)
enables smoother acceleration. Motor speed increases gently, and does not <> overshoot when regulate Speed is used.

Specified by:
smoothAcceleration in interface TachoMotor
Parameters:
yes - is true for smooth acceleration.

setSpeed

public void setSpeed(int speed)
Sets motor speed , in degrees per second; Up to 900 is possible with 8 volts.

Specified by:
setSpeed in interface TachoMotor
Parameters:
speed - value in degrees/sec

setPower

public void setPower(int power)
sets motor power. This method is used by the Regulator thread to control motor speed. Warning: negative power will cause the motor to run in reverse but without updating the _direction field which is used by the Regulator thread. If the speed regulation is enabled, the results are unpredictable.

Overrides:
setPower in class BasicMotor
Parameters:
power - power setting: 0 - 100

getSpeed

public int getSpeed()
Returns the current motor speed in degrees per second

Specified by:
getSpeed in interface TachoMotor
Returns:
motor speed in degrees per second

getMode

public int getMode()
Description copied from class: BasicMotor
Returns the mode.

Overrides:
getMode in class BasicMotor
Returns:
: 1 = forward, 2= backward, 3 = stop, 4 = float

getPower

public int getPower()
Description copied from class: BasicMotor
Returns the current power setting.

Overrides:
getPower in class BasicMotor
Returns:
power value 0-100

getLimitAngle

public int getLimitAngle()
Return the angle that a Motor is rotating to.

Returns:
angle in degrees

isRegulating

public boolean isRegulating()

getRotationSpeed

public int getRotationSpeed()
Description copied from interface: Tachometer
Returns the actual speed. This value is calculated every 100 ms on the NXT. TODO: getRotationSpeed() is an alternate method name, but then we are again competing with Motor.getSpeed()

Specified by:
getRotationSpeed in interface Tachometer
Returns:
speed in degrees per second, negative value means motor is rotating backward
See Also:
Tachometer.getRotationSpeed()

getTachoCount

public int getTachoCount()
Description copied from interface: Encoder
Returns the tachometer count.

Specified by:
getTachoCount in interface Encoder
Returns:
tachometer count in degrees
See Also:
Encoder.getTachoCount()

resetTachoCount

public void resetTachoCount()
Resets the tachometer count to zero.

Specified by:
resetTachoCount in interface Encoder

getError

public float getError()
for debugging

Returns:
regulator error

getBasePower

public float getBasePower()
for debugging

Returns:
base power of regulator

setBrakePower

public void setBrakePower(int pwr)