ch.aplu.nxt
Class Motor

java.lang.Object
  extended by ch.aplu.nxt.Part
      extended by ch.aplu.nxt.Motor
All Implemented Interfaces:
SharedConstants

public class Motor
extends Part

Class that represents one of the NXT motors. Most methods will call connect, if not yet connected.


Field Summary
 
Fields inherited from class ch.aplu.nxt.Part
robot
 
Fields inherited from interface ch.aplu.nxt.SharedConstants
ABOUT, ANGLE, ANGLESTEPSMODE, BOOLEANMODE, BOOT, BRAKE, CELSIUSMODE, CLOSE, CUSTOM, DEBUG_LEVEL_HIGH, DEBUG_LEVEL_LOW, DEBUG_LEVEL_MEDIUM, DEBUG_LEVEL_OFF, DELETE, DELETE_USER_FLASH, DIRECT_COMMAND_NOREPLY, DIRECT_COMMAND_REPLY, FAHRENHEITMODE, FIND_FIRST, FIND_NEXT, GET_BATTERY_LEVEL, GET_CURRENT_PROGRAM_NAME, GET_DEVICE_INFO, GET_FIRMWARE_VERSION, GET_INPUT_VALUES, GET_OUTPUT_STATE, HIGH_SPEED_BUFFER, KEEP_ALIVE, LIGHT_ACTIVE, LIGHT_INACTIVE, LOWSPEED, LOWSPEED_9V, LS_GET_STATUS, LS_READ, LS_WRITE, MESSAGE_READ, MESSAGE_WRITE, MODEMASK, MOTOR_RUN_STATE_IDLE, MOTOR_RUN_STATE_RAMPDOWN, MOTOR_RUN_STATE_RAMPUP, MOTOR_RUN_STATE_RUNNING, MOTORON, NO_OF_SENSOR_TYPES, NO_SENSOR, NXJ_FIND_FIRST, NXJ_FIND_NEXT, OPEN_APPEND_DATA, OPEN_READ, OPEN_READ_LINEAR, OPEN_WRITE, OPEN_WRITE_DATA, OPEN_WRITE_LINEAR, PCTFULLSCALEMODE, PERIODCOUNTERMODE, PLAY_SOUND_FILE, PLAY_TONE, POLL, POLL_BUFFER, POLL_LENGTH, RAWMODE, READ, REFLECTION, REGULATED, REGULATION_MODE_IDLE, REGULATION_MODE_MOTOR_SPEED, REGULATION_MODE_MOTOR_SYNC, REPLY_COMMAND, RESET_MOTOR_POSITION, RESET_SCALED_INPUT_VALUE, SET_BRICK_NAME, SET_INPUT_MODE, SET_OUTPUT_STATE, SLOPEMASK, SOUND_DB, SOUND_DBA, START_PROGRAM, STOP_PROGRAM, STOP_SOUND_PLAYBACK, SWITCH, SYSTEM_COMMAND_NOREPLY, SYSTEM_COMMAND_REPLY, TEMPERATURE, TITLE, TITLEMP, TRANSITIONCNTMODE, VERSION, WRITE
 
Constructor Summary
Motor(MotorPort port)
          Creates a motor instance that is plugged into given port.
 
Method Summary
 void addMotionListener(MotionListener motionListener)
          Registers the given motion listener.
 Motor backward()
          Starts the backward rotation with preset speed.
protected  Motor backward(boolean rampup)
           
protected  void cleanup()
           
 Motor continueTo(int count)
          Same as rotateTo(int count), but the rotation counter is not set to zero.
 Motor continueTo(int count, boolean blocking)
          Same as rotateTo(int count, boolean blocking), but the rotation counter is not set to zero.
 Motor forward()
          Starts the forward rotation with preset speed.
protected  Motor forward(boolean rampup)
           
 int getPortId()
          Returns the port number.
 java.lang.String getPortLabel()
          Returns the port label.
 int getSpeed()
          Returns the current speed (arbitrary units).
 double getVelocity()
          Returns the current velocity.
protected  void init()
           
 boolean isMoving()
          Checks if the motor is rotating.
 void resetMotorCount()
          Resets the rotation counter to zero.
 Motor rotateTo(int count)
          Sets the rotation counter to zero and rotates the motor until the given count is reached.
 Motor rotateTo(int count, boolean blocking)
          Sets the rotation counter to zero and rotates the motor until the given count is reached.
 Motor setSpeed(int speed)
          Sets the speed to the given value (arbitrary units).
 void setSpeedFactor(double value)
          Sets the motor speed factor to given value.
 Motor setVelocity(double velocity)
          Sets the velocity to the given value.
 double speedToVelocity(int speed)
          Conversion from speed to velocity.
protected  void startMotionDetector()
           
 Motor stop()
          Stops the motor.
protected  void stopMotionDetector()
           
 int velocityToSpeed(double velocity)
          Conversion from velocity to speed.
 
Methods inherited from class ch.aplu.nxt.Part
setRobot
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Motor

public Motor(MotorPort port)
Creates a motor instance that is plugged into given port. Default speed set in nxtlib.properties.

Parameters:
port - the port where the motor is plugged-in (MotorPort.A, MotorPort.B, MotorPort.C)
Method Detail

getPortId

public int getPortId()
Returns the port number.

Returns:
the port number (0..3)

getPortLabel

public java.lang.String getPortLabel()
Returns the port label.

Returns:
the port label (A, B, C)

init

protected void init()
Specified by:
init in class Part

cleanup

protected void cleanup()
Specified by:
cleanup in class Part

addMotionListener

public void addMotionListener(MotionListener motionListener)
Registers the given motion listener. When calling rotateTo(), a motion detector thread is started that checks the motion of the motor. When the motion stops because the rotation count is reached or stop() is called, the MotionListener's callback motionStopped() is invoked and the motion detector thread terminates.

Parameters:
motionListener - a reference to a MotionListener
See Also:
MotionListener, rotateTo(int count, boolean blocking)

startMotionDetector

protected void startMotionDetector()

stopMotionDetector

protected void stopMotionDetector()

forward

public Motor forward()
Starts the forward rotation with preset speed. Method returns immediately, while the rotation continues.

Returns:
the object reference to allow method chaining

forward

protected Motor forward(boolean rampup)

backward

public Motor backward()
Starts the backward rotation with preset speed. Method returns immediately, while the rotation continues.

Returns:
the object reference to allow method chaining

backward

protected Motor backward(boolean rampup)

setSpeed

public Motor setSpeed(int speed)
Sets the speed to the given value (arbitrary units). The speed will be changed to the new value at the next movement call only.

Parameters:
speed - the speed 0..100
Returns:
the object reference to allow method chaining

getSpeed

public int getSpeed()
Returns the current speed (arbitrary units).

Returns:
the speed 0..100

setVelocity

public Motor setVelocity(double velocity)
Sets the velocity to the given value. The velocity will be changed to the new value at the next movement call only.
velocity = MotorSpeedFactor * speed (default MotorSpeedFactor set in nxtlib.properties).

Parameters:
velocity - the velocity in m/s
Returns:
the object reference to allow method chaining

getVelocity

public double getVelocity()
Returns the current velocity. The velocity in m/s with some inaccuracy.
velocity = MotorSpeedFactor * speed (default MotorSpeedFactor set in nxtlib.properties).

Returns:
the velocity in m/s

stop

public Motor stop()
Stops the motor.

Returns:
the object reference to allow method chaining

setSpeedFactor

public void setSpeedFactor(double value)
Sets the motor speed factor to given value. Formula: velocity = speedFactor * speed where velocity is in m/s and speed is value used in setSpeed() Default: Set in nxtlib.properties

Parameters:
value - the velocity per speed unit

speedToVelocity

public double speedToVelocity(int speed)
Conversion from speed to velocity.

Parameters:
speed - the speed as set in setSpeed()
Returns:
the velocity in m/s
See Also:
setSpeedFactor(double value)

velocityToSpeed

public int velocityToSpeed(double velocity)
Conversion from velocity to speed.

Parameters:
velocity - the velocity in m/s
Returns:
the speed as set in setSpeed()
See Also:
setSpeedFactor(double value)

resetMotorCount

public void resetMotorCount()
Resets the rotation counter to zero.

See Also:
rotateTo(int count)

rotateTo

public Motor rotateTo(int count)
Sets the rotation counter to zero and rotates the motor until the given count is reached. If count is negative, the motor turns backwards. This method returns when the count is reached and the motor stops.

Returns:
the object reference to allow method chaining
See Also:
addMotionListener(MotionListener motionListener), rotateTo(int count, boolean blocking)

rotateTo

public Motor rotateTo(int count,
                      boolean blocking)
Sets the rotation counter to zero and rotates the motor until the given count is reached. If count is negative, the motor turns backwards. If blocking = false, the method returns immediately. You may register a motion listener with addMotionListener() to get a callback when the count is reached and the motor stops. If blocking = true, the method returns when the count is reached and the motor stops.

Returns:
the object reference to allow method chaining
See Also:
addMotionListener(MotionListener motionListener), rotateTo(int count)

continueTo

public Motor continueTo(int count)
Same as rotateTo(int count), but the rotation counter is not set to zero.

See Also:
rotateTo(int count)

continueTo

public Motor continueTo(int count,
                        boolean blocking)
Same as rotateTo(int count, boolean blocking), but the rotation counter is not set to zero.

See Also:
rotateTo(int count, boolean blocking)

isMoving

public boolean isMoving()
Checks if the motor is rotating.

Returns:
true, if rotating, otherwise false