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.


Field Summary
 
Fields inherited from interface ch.aplu.nxt.SharedConstants
ABOUT, ACCELEROMETERPOLLDELAY, ANGLE, ANGLESTEPSMODE, AXELENGTH, BOOLEANMODE, BOOT, BRAKE, BRAKEDELAY, CELSIUSMODE, CLOSE, COLORPOLLDELAY, COMPASSPOLLDELAY, CRAWLERROTATIONFACTOR, CRAWLERROTATIONSPEED, CRAWLERSPEED, CRAWLERSTEPFACTOR, CUSTOM, DEBUG_LEVEL_HIGH, DEBUG_LEVEL_LOW, DEBUG_LEVEL_MEDIUM, DEBUG_LEVEL_OFF, DEBUGLEVEL, DELETE, DELETE_USER_FLASH, DIRECT_COMMAND_NOREPLY, DIRECT_COMMAND_REPLY, FAHRENHEITMODE, FIND_FIRST, FIND_NEXT, GEARSPEED, GET_BATTERY_LEVEL, GET_CURRENT_PROGRAM_NAME, GET_DEVICE_INFO, GET_FIRMWARE_VERSION, GET_INPUT_VALUES, GET_OUTPUT_STATE, GYROPOLLDELAY, HIGH_SPEED_BUFFER, INFRAREDSEEKERPOLLDELAY, KEEP_ALIVE, LIGHT_ACTIVE, LIGHT_INACTIVE, LIGHTPOLLDELAY, LOWSPEED, LOWSPEED_9V, LS_GET_STATUS, LS_READ, LS_WRITE, MESSAGE_READ, MESSAGE_WRITE, MODEMASK, MOTIONDETECTORPOLLDELAY, MOTOR_RUN_STATE_IDLE, MOTOR_RUN_STATE_RAMPDOWN, MOTOR_RUN_STATE_RAMPUP, MOTOR_RUN_STATE_RUNNING, MOTORON, MOTORSPEED, MOTORSPEEDFACTOR, MOTORSPEEDMULTIPLIER, NO_OF_SENSOR_TYPES, NO_SENSOR, 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, PROTOTYPEPOLLDELAY, RAWMODE, READ, REFLECTION, REGULATED, REGULATION_MODE_IDLE, REGULATION_MODE_MOTOR_SPEED, REGULATION_MODE_MOTOR_SYNC, REPLY_COMMAND, RESET_MOTOR_POSITION, RESET_SCALED_INPUT_VALUE, RFIDPOLLDELAY, SENSOREVENTDELAY, SET_BRICK_NAME, SET_INPUT_MODE, SET_OUTPUT_STATE, SLOPEMASK, SOUND_DB, SOUND_DBA, SOUNDPOLLDELAY, START_PROGRAM, STOP_PROGRAM, STOP_SOUND_PLAYBACK, SWITCH, SYSTEM_COMMAND_NOREPLY, SYSTEM_COMMAND_REPLY, TEMPERATURE, TITLE, TITLEMP, TOUCHPOLLDELAY, TRANSITIONCNTMODE, TURTLEROTATIONFACTOR, TURTLEROTATIONSPEED, TURTLESPEED, TURTLESTEPFACTOR, ULTRASONICPOLLDELAY, VERSION, WRITE
 
Constructor Summary
Motor()
           
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.
 Motor continueRelativeTo(int count)
          Same as rotateTo(int count), but the rotation counter is not set to zero.
 Motor continueRelativeTo(int count, boolean blocking)
          Same as rotateTo(int count, boolean blocking), but the rotation counter is not set to zero.
 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.
 lejos.nxt.NXTRegulatedMotor getLejosMotor()
          Returns the reference of the the underlying lejos.nxt.NXTRegulatedMotor.
 int getMotorCount()
          Returns current value of the rotation counter.
 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.
 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.
 void setAcceleration(int acceleration)
          Sets the acceleration rate of the motor in degrees/sec/sec The default value is 6000.
 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.
 Motor stop()
          Stops the motor.
 int velocityToSpeed(double velocity)
          Conversion from velocity to speed.
 
Methods inherited from class java.lang.Object
equals, 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)

Motor

public Motor()
Method Detail

getPortId

public int getPortId()
Returns the port number.

Returns:
the port number (0..2)

getPortLabel

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

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

forward

public Motor forward()
Starts the forward rotation with preset speed. Method returns, while the rotation continues. (If motor is already rotating with same speed, returns immediately.) The rotation counter continues to be increased from its start value.

Returns:
the object reference to allow method chaining

backward

public Motor backward()
Starts the backward rotation with preset speed. Method returns, while the rotation continues. The rotation counter continues to be decreased from its start value.

Returns:
the object reference to allow method chaining

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. The rotation counter is stopped but maintains its value. (If motor is already stopped, returns immediately.)

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)

getMotorCount

public int getMotorCount()
Returns current value of the rotation counter.

Returns:
the value of the rotation counter

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. The given count is the absolute value of the rotation counter to be reached. If the current count is higher than the count to reach, the motor turns backward.

Parameters:
count - the rotation counter value to be reached
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. The given count is the absolute value of the rotation counter to be reached. If the current count is higher than the count to reach, the motor turns backward.

Parameters:
count - the rotation counter value to be reached
See Also:
rotateTo(int count, boolean blocking)

continueRelativeTo

public Motor continueRelativeTo(int count)
Same as rotateTo(int count), but the rotation counter is not set to zero. The given count is the relative increasement/decreasement from the current value of the rotation counter. For count < 0 the motor turns backward.

Parameters:
count - the rotation counter value to be increased/decreased
See Also:
rotateTo(int count)

continueRelativeTo

public Motor continueRelativeTo(int count,
                                boolean blocking)
Same as rotateTo(int count, boolean blocking), but the rotation counter is not set to zero. The given count is the relative increasement/decreasement from the current value of the rotation counter. For count < 0 the motor turns backward.

Parameters:
count - the rotation counter value to be increased/decreased
blocking - if true, the method blocks until the count is reached, otherwise it returns immediately
See Also:
rotateTo(int count, boolean blocking)

isMoving

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

Returns:
true, if rotating, otherwise false

setAcceleration

public void setAcceleration(int acceleration)
Sets the acceleration rate of the motor in degrees/sec/sec The default value is 6000. Smaller values make speed changes smoother.

Parameters:
acceleration - the new accelaration

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)

getLejosMotor

public lejos.nxt.NXTRegulatedMotor getLejosMotor()
Returns the reference of the the underlying lejos.nxt.NXTRegulatedMotor.

Returns:
the reference of the Motor