lejos.nxt.addon
Class MMXRegulatedMotor

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

public class MMXRegulatedMotor
extends java.lang.Object
implements RegulatedMotor, DCMotor

Supports Mindsensors NXTMMX motor multiplexer. This 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 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 getPower()
          Returns the current motor power setting (%).
 int getRotationSpeed()
          Returns the actual speed.
 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 is currently stalled after a motor action method is executed.
 void lock()
          Locks the motor in current position.
 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 setPower(int power)
          Set the power level 0-100% to be applied to the 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 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 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 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

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 actual speed.

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

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), 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(), setPower(int)

getPower

public int getPower()
Returns the current motor power setting (%).

Specified by:
getPower in interface DCMotor
Returns:
current power 0-100
See Also:
setPower(int), getSpeed()

setPower

public void setPower(int power)
Set the power level 0-100% to be applied to the motor. Setting power during a rotate method will have no effect on the running rotate but will on the next rotate method call.

Specified by:
setPower in interface DCMotor
Parameters:
power - new motor power 0-100%
See Also:
setSpeed(int), getPower()

forward

public void forward()
Causes motor to rotate forward.

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

backward

public void backward()
Causes motor to rotate backwards.

Specified by:
backward in interface BaseMotor
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 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
See Also:
flt(boolean), lock(), stop()

stop

public 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 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
See Also:
stop(boolean), flt(), lock()

setRotateStopMode

public 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 void lock()
Locks the motor in current position. Uses active feed back to hold it.

See Also:
stop(), flt()

waitComplete

public void waitComplete()
Wait until the current movement operation is complete. This can include the motor stalling.

Specified by:
waitComplete in interface RegulatedMotor

getTachoCount

public int getTachoCount()
Returns the tachometer count.

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

resetTachoCount

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

Specified by:
resetTachoCount in interface Encoder
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 is currently stalled after a motor action method is executed.

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

isMoving

public 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
Returns:
true if the motor is rotating, false otherwise.
See Also:
flt()