lejos.nxt.addon
Class MMXRegulatedMotor

java.lang.Object
  extended by lejos.nxt.addon.MMXMotor
      extended by lejos.nxt.addon.MMXRegulatedMotor
All Implemented Interfaces:
BaseMotor, DCMotor, Encoder, EncoderMotor, RegulatedMotor, Tachometer

public class MMXRegulatedMotor
extends MMXMotor
implements RegulatedMotor

Abstraction to drive a regulated encoder motor with the NXTMMX motor multiplexer. The NXTMMX motor multiplexer device allows you to connect two additional motors to your robot using a sensor port. Multiple NXTMMXs can be chained together.

Create an instance of this class passing a NXTMMX instance and Motor ID (MMX_MOTOR_1 or MMX_MOTOR_2) in the constructor.

Author:
Michael D. Smith <mdsmitty@gmail.com>, Kirk P. Thompson
See Also:
NXTMMX

Field Summary
static int ROTSTOP_BRAKE
          Use to specify motor brake when a rotate method completes.
static int ROTSTOP_FLOAT
          Use to specify motor float when a rotate method completes.
static int ROTSTOP_LOCK
          Use to specify active hold when a rotate method completes.
 
Constructor Summary
MMXRegulatedMotor(NXTMMX mux, int motor)
          Create an instance of a MMXRegulatedMotor.
 
Method Summary
 void addListener(RegulatedMotorListener listener)
          Add a single motor listener.
 void backward()
          Causes motor to rotate backwards.
 void flt()
          Causes motor to float.
 void flt(boolean immediateReturn)
          Causes motor to float.
 void forward()
          Causes motor to rotate forward.
 int getLimitAngle()
          Return the absolute angle that this Motor is rotating to.
 float getMaxSpeed()
          Return the maximum speed of the motor.
 int getRotationSpeed()
          Returns the speed the motor is moving at.
 int getSpeed()
          Return the current target speed.
 int getTachoCount()
          Returns the tachometer count.
 boolean isMoving()
          This method returns true if the motor is rotating, whether under power or not.
 boolean isStalled()
          Return true if the motor has stalled after a motor action method was executed.
 void lock()
          Locks the motor in current position.
 RegulatedMotorListener removeListener()
          Remove the registered RegulatedMotorListener.
 void resetTachoCount()
          Resets the tachometer count to zero.
 void rotate(int angle)
          Rotate by the requested number of degrees.
 void rotate(int angle, boolean immediateReturn)
          Rotate by the requested number of degrees.
 void rotateTo(int limitAngle)
          Rotate to the target angle.
 void rotateTo(int limitAngle, boolean immediateReturn)
          Rotate to the target angle.
 void setAcceleration(int acceleration)
          Sets speed ramping is enabled/disabled for this motor.
 void setRotateStopMode(int mode)
          Sets the motor stopping mode used for the rotate methods after rotation completion.
 void setSpeed(int speed)
          Sets desired motor speed, in degrees per second.
 void setStallThreshold(int error, int time)
          Not implemented in the NXTMMX.
 void stop()
          Causes motor to stop pretty much instantaneously.
 void stop(boolean immediateReturn)
          Causes motor to stop pretty much instantaneously.
 void waitComplete()
          Wait until the current movement operation is complete.
 
Methods inherited from class lejos.nxt.addon.MMXMotor
getPower, setPower
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ROTSTOP_FLOAT

public static final int ROTSTOP_FLOAT
Use to specify motor float when a rotate method completes.

See Also:
setRotateStopMode(int), Constant Field Values

ROTSTOP_BRAKE

public static final int ROTSTOP_BRAKE
Use to specify motor brake when a rotate method completes.

See Also:
setRotateStopMode(int), Constant Field Values

ROTSTOP_LOCK

public static final int ROTSTOP_LOCK
Use to specify active hold when a rotate method completes. The NXTMMX will actively attempt to hold the motor angle.

See Also:
setRotateStopMode(int), Constant Field Values
Constructor Detail

MMXRegulatedMotor

public MMXRegulatedMotor(NXTMMX mux,
                         int motor)
Create an instance of a MMXRegulatedMotor.

Parameters:
mux - the motor multiplexor NXTMMX instance to bind this motor to.
motor - the index of the motor connected to the NXTMMX: NXTMMX.MMX_MOTOR_1 or NXTMMX.MMX_MOTOR_2
See Also:
NXTMMX, NXTMMX.MMX_MOTOR_1, NXTMMX.MMX_MOTOR_2
Method Detail

addListener

public void addListener(RegulatedMotorListener listener)
Add a single motor listener. Move operations will be reported to this object.

Specified by:
addListener in interface RegulatedMotor
Parameters:
listener - An instance of type RegulatedMotorListener
See Also:
RegulatedMotorListener

removeListener

public RegulatedMotorListener removeListener()
Remove the registered RegulatedMotorListener.

Specified by:
removeListener in interface RegulatedMotor
Returns:
The registered motor listener. null if none registered.

getMaxSpeed

public float getMaxSpeed()
Return the maximum speed of the motor. It is a general assumption that the maximum speed of a Motor is 100 degrees/second * Voltage.

Specified by:
getMaxSpeed in interface RegulatedMotor
Returns:
The maximum speed in degrees per second

getRotationSpeed

public int getRotationSpeed()
Returns the speed the motor is moving at. Note that this is estimated because the NXTMMX does not provide this natively.

Specified by:
getRotationSpeed in interface Tachometer
Returns:
speed in degrees per second. Negative value means motor is rotating backward.

waitComplete

public void waitComplete()
Wait until the current movement operation is complete. This can include the motor stalling only on a forward() or backward() call.

Specified by:
waitComplete in interface RegulatedMotor

rotate

public void rotate(int angle,
                   boolean immediateReturn)
Rotate by the requested number of degrees. Negative values rotate opposite positive ones.

Specified by:
rotate in interface RegulatedMotor
Parameters:
angle - number of degrees to rotate relative to the current position
immediateReturn - true will not block, false will wait until completion or stall.
See Also:
rotate(int), setRotateStopMode(int)

rotate

public void rotate(int angle)
Rotate by the requested number of degrees. Wait for the move to complete.

Specified by:
rotate in interface RegulatedMotor
Parameters:
angle - number of degrees to rotate relative to the current position
See Also:
rotate(int,boolean), setRotateStopMode(int)

rotateTo

public void rotateTo(int limitAngle,
                     boolean immediateReturn)
Rotate to the target angle. If immediateReturn is true, the 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 and the motor stops completely, the method isMoving() returns false.

Specified by:
rotateTo in interface RegulatedMotor
Parameters:
limitAngle - Angle to rotate to.
immediateReturn - iff true, method returns immediately, thus allowing monitoring of sensors in the calling thread.
See Also:
getTachoCount(), setRotateStopMode(int)

rotateTo

public void rotateTo(int limitAngle)
Rotate to the target angle. Do not return until the move is complete.

Specified by:
rotateTo in interface RegulatedMotor
Parameters:
limitAngle - Angle to rotate to.
See Also:
rotateTo(int, boolean), setRotateStopMode(int)

getLimitAngle

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

Specified by:
getLimitAngle in interface RegulatedMotor
Returns:
angle in degrees. 0 if no rotate method has been intiated.

getSpeed

public int getSpeed()
Return the current target speed.

Specified by:
getSpeed in interface RegulatedMotor
Returns:
Motor speed in degrees per second.
See Also:
setSpeed(int), MMXMotor.getPower()

setSpeed

public void setSpeed(int speed)
Sets desired motor speed, in degrees per second.

The NXTMMX does not provide speed control per se (just power) so we approximate the power value used based on the requested degress/sec (dps) passed in speed. This means if you request 400 dps, the actual dps value may not reflect that. Setting speed during a rotate method will have no effect on the running rotate but will on the next rotate method call.

experimental data gives: dps=8.1551*power+32.253 (unloaded @ 8.83V)

Note:The NXTMMX doesn't seem to want to drive the motor below ~40 dps.

Specified by:
setSpeed in interface RegulatedMotor
Parameters:
speed - Motor speed in degrees per second
See Also:
getSpeed(), MMXMotor.setPower(int)

forward

public final void forward()
Causes motor to rotate forward.

Specified by:
forward in interface BaseMotor
Overrides:
forward in class MMXMotor
See Also:
backward()

backward

public final void backward()
Causes motor to rotate backwards.

Specified by:
backward in interface BaseMotor
Overrides:
backward in class MMXMotor
See Also:
forward()

flt

public void flt(boolean immediateReturn)
Causes motor to float. This will stop the motor without braking and the position of the motor will not be maintained.

Specified by:
flt in interface RegulatedMotor
Parameters:
immediateReturn - If true, do not wait for the motor to actually stop
See Also:
flt()

flt

public final void flt()
Causes motor to float. This will stop the motor without braking and the position of the motor will not be maintained. This method will not wait for the motor to stop rotating before returning.

Specified by:
flt in interface BaseMotor
Overrides:
flt in class MMXMotor
See Also:
flt(boolean), lock(), stop()

stop

public final void stop(boolean immediateReturn)
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 RegulatedMotor
Parameters:
immediateReturn - if true, do not wait for the motor to actually stop
See Also:
stop()

stop

public final void stop()
Causes motor to stop pretty much instantaneously. In other words, the motor doesn't just stop; it will resist any further motion. The motor must stop rotating before stop() is complete.

Cancels any rotate() orders in progress.

Specified by:
stop in interface BaseMotor
Overrides:
stop in class MMXMotor
See Also:
stop(boolean), flt(), lock()

setRotateStopMode

public final void setRotateStopMode(int mode)
Sets the motor stopping mode used for the rotate methods after rotation completion.

Default on instantiation is ROTSTOP_BRAKE.

Parameters:
mode - ROTSTOP_FLOAT, ROTSTOP_BRAKE, or ROTSTOP_LOCK
See Also:
rotate(int), rotateTo(int)

lock

public final void lock()
Locks the motor in current position. Uses active feed back to hold it.

See Also:
stop(), flt()

getTachoCount

public final int getTachoCount()
Returns the tachometer count.

Specified by:
getTachoCount in interface Encoder
Overrides:
getTachoCount in class MMXMotor
Returns:
tachometer count in degrees
See Also:
resetTachoCount()

resetTachoCount

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

Specified by:
resetTachoCount in interface Encoder
Overrides:
resetTachoCount in class MMXMotor
See Also:
getTachoCount()

setAcceleration

public void setAcceleration(int acceleration)
Sets speed ramping is enabled/disabled for this motor. The RegulatedMotor interface specifies this in degrees/sec/sec but the NXTMMX does not allow the rate to be changed, just if the motor uses smooth acceleration or not so we use the acceleration parameter to specify ramping state.

Default at instantiation is ramping enabled.

Specified by:
setAcceleration in interface RegulatedMotor
Parameters:
acceleration - >0 means NXTMMX internal ramping is enabled otherwise disabled

isStalled

public boolean isStalled()
Return true if the motor has stalled after a motor action method was executed. The stalled status will persist until movement occurs or a new movement or stop/flt command is issued.

Specified by:
isStalled in interface RegulatedMotor
Returns:
true if the motor is stalled, else false.
See Also:
forward(), backward(), rotate(int)

isMoving

public final boolean isMoving()
This method returns true if the motor is rotating, whether under power or not. The return value corresponds to the actual motor movement so if something external is rotating the motor, isMoving() will return true. After flt() is called, this method will return true until the motor axle stops rotating by inertia, etc.

Specified by:
isMoving in interface BaseMotor
Overrides:
isMoving in class MMXMotor
Returns:
true if the motor is rotating, false otherwise.
See Also:
flt()

setStallThreshold

public void setStallThreshold(int error,
                              int time)
Not implemented in the NXTMMX.

Specified by:
setStallThreshold in interface RegulatedMotor
Parameters:
error - ignored
time - ignored