lejos.nxt
Class Motor

java.lang.Object
  extended by lejos.nxt.Motor
All Implemented Interfaces:
TimerListener

public class Motor
extends Object
implements TimerListener

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
Motor has 2 modes : speedRegulation and smoothAcceleration. These are initially enabled. <> 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.. Speed regulation fails if the target speed exceeds the capability of the motor.

Example:

   Motor.A.setSpeed(720);// 2 RPM
   Motor.C.setSpeed(7200);
   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 22 March 2007

Field Summary
static Motor A
          Motor A.
static Motor B
          Motor B.
static Motor C
          Motor C.
 lejos.nxt.Motor.Regulator regulator
           
 
Constructor Summary
Motor(MotorPort 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
 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 isBackward()
          Return true if motor is backward.
 boolean isFloating()
          Returns true iff the motor is in float mode.
 boolean isForward()
          Return true if motor is forward.
 boolean isMoving()
          Returns true iff the motor is in motion.
 boolean isRotating()
          returns true when motor is rotating towarad a specified angle
 boolean isStopped()
          Return true if motor is stopped.
 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
When the angle is reached, the method isRotating() returns false;
 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
Then getTachoCount should be within +- 2 degrees if the limit angle When the angle is reached, the method isRotating() returns false;
 void setPower(int power)
          sets motor power.
 void setSpeed(int speed)
          Sets motor speed , in degrees per second; Up to 900 is posssible 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.
 void timedOut()
          requred by TimerListener interface
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait
 

Field Detail

regulator

public lejos.nxt.Motor.Regulator regulator

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(MotorPort port)
Method Detail

getStopAngle

public int getStopAngle()

isMoving

public final boolean isMoving()
Returns true iff the motor is in motion.

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
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
Then getTachoCount should be within +- 2 degrees if the limit angle 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 final void setSpeed(int speed)
Sets motor speed , in degrees per second; Up to 900 is posssible 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 rusults are unpredictable.

Parameters:
power - power setting: 0 - 100

getSpeed

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


getMode

public int getMode()
Returns the mode.

Returns:
mode 1=forward, 2=backward, 3=stopped, 4=floating

getPower

public int getPower()
Returns the current power setting.

Returns:
power value 0-100

getLimitAngle

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

Returns:
angle in degrees

isRotating

public final boolean isRotating()
returns true when motor is rotating towarad a specified angle


timedOut

public void timedOut()
requred by TimerListener interface

Specified by:
timedOut in interface TimerListener

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.


forward

public void forward()
Causes motor to rotate forward.


isForward

public boolean isForward()
Return true if motor is forward.


backward

public void backward()
Causes motor to rotate backwards.


isBackward

public boolean isBackward()
Return true if motor is backward.


reverseDirection

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


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.


isFloating

public boolean isFloating()
Returns true iff the motor is in float mode.

Returns:
true iff the motor is currently in float mode.

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


isStopped

public boolean isStopped()
Return true if motor is stopped.