ch.aplu.nxt
Class Gear

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

public class Gear
extends Part

Combines two motors on an axis to perform a car-like movement.


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
Gear()
          Creates a gear instance with left motor plugged into port A, right motor plugged into port B.
Gear(MotorPort port1, MotorPort port2)
          Creates a gear instance with left motor plugged into port1, right motor plugged into port2.
 
Method Summary
 void addMotionListener(MotionListener motionListener)
          Register the given motion listener.
 Gear backward()
          Same as forward(), but move in reverse direction.
 Gear backward(int duration)
          Same as forward(int duration), but move in reverse direction.
 Gear forward()
          Starts the forward movement with preset speed.
 Gear forward(int duration)
          Starts the forward movement for the given duration (in ms) with preset speed.
 Motor getMotLeft()
          Returns left motor of the gear.
 Motor getMotRight()
          Returns right motor of the gear.
 int getSpeed()
          Returns the current speed (arbitrary units).
 double getVelocity()
          Returns the current velocity.
 boolean isMoving()
          Checks if one or both motors are rotating.
 Gear left()
          Starts turning left with left motor rotating forward and right motor rotating backward at preset speed.
 Gear left(int duration)
          Starts turning left for the given duration (in ms) with preset speed.
 Gear leftArc(double radius)
          Starts turning to the left on an arc with given radius (in m).
 Gear leftArc(double radius, int duration)
          Starts turning to the left on an arc with given radius (in m) for the given duration (in ms) with preset speed.
 Gear moveTo(int count)
          Same as moveTo(int count, boolean blocking) with blocking = true.
 Gear moveTo(int count, boolean blocking)
          Sets the rotation counter to zero and rotate both motors until the given count is reached.
 Gear right()
          Same as left(), but turns in the opposite direction.
 Gear right(int duration)
          Same as left(int duration), but turns in the opposite direction.
 Gear rightArc(double radius)
          Same as leftArc(double radius), but turns to the right.
 Gear rightArc(double radius, int duration)
          Same as leftArc(double radius, int duration), but turns to the right.
 void setAcceleration(int acceleration)
          Sets the acceleration rate of both motors in degrees/sec/sec The default value is 2000.
 Gear setSpeed(int speed)
          Sets the speed to the given value (arbitrary units).
 Gear setVelocity(double velocity)
          Sets the velocity to the given value.
 Gear stop()
          Stops the gear.
 Gear turnTo(int count)
          Same as turnTo(int count, boolean blocking) with blocking = true.
 Gear turnTo(int count, boolean blocking)
          Sets the rotation counter to zero and turn with the motors running in opposite direction.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Gear

public Gear(MotorPort port1,
            MotorPort port2)
Creates a gear instance with left motor plugged into port1, right motor plugged into port2.

Parameters:
port1 - MotorPort.A or MotorPort.B or MotorPort.C (not both the same)
port2 - MotorPort.A or MotorPort.B or MotorPort.C (not both the same)

Gear

public Gear()
Creates a gear instance with left motor plugged into port A, right motor plugged into port B.

Method Detail

getSpeed

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

Returns:
speed 0..100

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:
velocity in m/s

setSpeed

public Gear 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 - 0..100
Returns:
the object reference to allow method chaining

setVelocity

public Gear 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 - in m/s
Returns:
the object reference to allow method chaining.

stop

public Gear stop()
Stops the gear. (If motor is already stopped, returns immediately.)

Returns:
the object reference to allow method chaining.

moveTo

public Gear moveTo(int count,
                   boolean blocking)
Sets the rotation counter to zero and rotate both motors until the given count is reached. If count is negative, the motors turn 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 motors stop. If blocking = true, the method returns when the count is reached and the motors stop.

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

moveTo

public Gear moveTo(int count)
Same as moveTo(int count, boolean blocking) with blocking = true.

See Also:
moveTo(int count, boolean blocking)

turnTo

public Gear turnTo(int count)
Same as turnTo(int count, boolean blocking) with blocking = true.

See Also:
turnTo(int count, boolean blocking)

turnTo

public Gear turnTo(int count,
                   boolean blocking)
Sets the rotation counter to zero and turn with the motors running in opposite direction. If count is positive, turns clockwise, otherwise anti-clockwise (MotorPort.A is right, MotorPort.B is left motor). If blocking = false, the method returns immediately. If blocking = true, the method returns when the count is reached and the motors stop.

Returns:
the object reference to allow method chaining.
See Also:
turnTo(int count)

addMotionListener

public void addMotionListener(MotionListener motionListener)
Register the given motion listener. When calling moveTo() a motion detector thread is started that checks the motion of the motors. 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 - the MotionListener to be registered

setAcceleration

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

Parameters:
acceleration - the new accelaration

forward

public Gear forward()
Starts the forward movement with preset speed. Method returns, while the movement continues. (If motor is already rotating with same speed, returns immediately.)

Returns:
the object reference to allow method chaining.

forward

public Gear forward(int duration)
Starts the forward movement for the given duration (in ms) with preset speed. Method returns at the end of the given duration, but the movement continues for 200 ms. Then it stops unless another movement method call (forward, backward, left, right, leftArc, rightArc) is invoked within that time. Calling several movement methods one after the other will result a seamless movement without intermediate stops.
To use it without problems in a callback method, it returns quickly when NxtRobot.disconnect is called.

Parameters:
duration - the duration (in ms)
Returns:
the object reference to allow method chaining

backward

public Gear backward()
Same as forward(), but move in reverse direction.

See Also:
forward()

backward

public Gear backward(int duration)
Same as forward(int duration), but move in reverse direction.

See Also:
forward(int duration)

left

public Gear left()
Starts turning left with left motor rotating forward and right motor rotating backward at preset speed. Method returns, while the movement continues. (If gear is already turning left, returns immediately.)

Returns:
the object reference to allow method chaining

left

public Gear left(int duration)
Starts turning left for the given duration (in ms) with preset speed. Method returns at the end of the given duration but the movement continues for 200 ms. Then it stops unless another movement method call (forward, backward, left, right, leftArc, rightArc) is invoked within that time. Calling several movement methods one after the other will result a seamless movement without intermediate stops.

Parameters:
duration - the duration (in ms)
Returns:
the object reference to allow method chaining

right

public Gear right()
Same as left(), but turns in the opposite direction.

See Also:
left()

right

public Gear right(int duration)
Same as left(int duration), but turns in the opposite direction.

See Also:
left(int duration)

leftArc

public Gear leftArc(double radius)
Starts turning to the left on an arc with given radius (in m). If the radius is negative, turns left backwards. The accuracy is limited and depends on the distance between the two wheels (default: AxeLength set in NxtLib.properties). Method returns, while the movement continues. (If gear is already moving on an arc with given radius, returns immediately.)

Parameters:
radius - the radius of the arc (in m)
Returns:
the object reference to allow method chaining

leftArc

public Gear leftArc(double radius,
                    int duration)
Starts turning to the left on an arc with given radius (in m) for the given duration (in ms) with preset speed. If the radius is negative, turns left backwards. The accuracy is limited and depends on the distance between the two wheels (default: AxeLength set in NxtLib.properties). Method returns at the end of the given duration but the movement continues for 200 ms. Then it stops unless another movement method call (forward, backward, left, right, leftArc, rightArc) is invoked within that time. Calling several movement methods one after the other will result a seamless movement without intermediate stops.

Parameters:
radius - the radius of the arc (in m)
duration - the duration (in ms)
Returns:
the object reference to allow method chaining

rightArc

public Gear rightArc(double radius)
Same as leftArc(double radius), but turns to the right.

See Also:
leftArc(double radius)

rightArc

public Gear rightArc(double radius,
                     int duration)
Same as leftArc(double radius, int duration), but turns to the right.

See Also:
leftArc(double radius, int duration)

getMotRight

public Motor getMotRight()
Returns right motor of the gear.

Returns:
the reference of the right motor

getMotLeft

public Motor getMotLeft()
Returns left motor of the gear.

Returns:
the reference of the left motor

isMoving

public boolean isMoving()
Checks if one or both motors are rotating.

Returns:
true, if gear is moving, otherwise false