lejos.nxt
Class Motor

java.lang.Object
  extended by lejos.nxt.BasicMotor
      extended by lejos.nxt.Motor

public class Motor
extends BasicMotor

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.
 int getActualSpeed()
          returns actualSpeed degrees per second, calculated every 100 ms; negative value means motor is rotating backward
 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 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()
           
 boolean isRotating()
          returns true when motor rotation task is not yet complete a specified angle
 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 through 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()
Causes motor to rotate forward.

Overrides:
forward in class BasicMotor

backward

public void backward()
Causes motor to rotate backwards.

Overrides:
backward in class BasicMotor

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()
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.

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

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.

Overrides:
isMoving in class BasicMotor
Returns:
true iff the motor is currently in motion.

rotate

public void rotate(int angle)
causes motor to rotate through angle.

Parameters:
angle - through which the motor will rotate

rotate

public 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. When the angle is reached, the method isRotating() returns false;

Parameters:
angle - through which the motor will rotate
immediateReturn - iff true, method returns immediately, thus allowing monitoring of sensors in the calling thread.

rotateTo

public 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

Parameters:
limitAngle - to which the motor will rotate

rotateTo

public 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. When the angle is reached, the method isRotating() returns false;

Parameters:
limitAngle - to which the motor will rotate, and then stop.
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.

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.


setSpeed

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

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


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

isRotating

public boolean isRotating()
returns true when motor rotation task is not yet complete a specified angle


isRegulating

public boolean isRegulating()

getActualSpeed

public int getActualSpeed()
returns actualSpeed degrees per second, calculated every 100 ms; negative value means motor is rotating backward


getTachoCount

public int getTachoCount()
Returns the tachometer count.

Returns:
tachometer count in degrees

resetTachoCount

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


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)