pt.citar.diablu.processing.nxt
Class LegoNXT

java.lang.Object
  extended by pt.citar.diablu.processing.nxt.LegoNXT

public class LegoNXT
extends Object

The LegoNXT class allows you to control the Mindstorms NXT robotic system. The NXT must have been paired using Bluetooth and you must know the name of the virtual COM port assigned (COM1, COM2, or something like that).

Author:
Jorge Cardoso http://jorgecardoso.org

Field Summary
static int ACTIVE
          The active light sensor type.
static int BLUE
          The blue color sensor type.
static int FULL
          The full color sensor type.
static int GREEN
          The green color sensor type.
static int INACTIVE
          The inactive light sensor type.
static int MOTOR_A
          The motor port A.
static int MOTOR_B
          The motor port B.
static int MOTOR_C
          The motor port C.
static int NONE
          The none color sensor type.
static int PORT_1
          The sensor port 1.
static int PORT_2
          The sensor port 2.
static int PORT_3
          The sensor port 3.
static int PORT_4
          The sensor port 4.
static int RED
          The red color sensor type.
 
Constructor Summary
LegoNXT(PApplet parent, String commPort)
          Creates a new instance of LegoNXT
 
Method Summary
 void dispose()
           
 int getBatteryLevel()
          Returns the battery level.
 boolean getButtonState(int portNumber)
          Returns the state of the pressure sensor at the specifiec port.
 int getColor(int portNumber)
          Returns the color level from the color sensor attached to the specified port.
 int getDB(int portNumber)
          Gets the DB level of the sound sensor on the specified port.
 int getDistance(int portNumber)
          Returns the proximity to an obstacle, measured by the ultrasonic sensor.
 int getLight(int portNumber)
          Returns the light level from the light sensor attached to the specified port.
 long getMotorBlockTachoCount(int motorNumber)
          Returns the current position realative to the last programmed movement.
 long getMotorRotationCount(int motorNumber)
          The current position relative to the last reset of the rotation sensor for this motor.
 long getMotorTachoCount(int motorNumber)
          Returns number of counts since last reset of the motor counter.
 long getMotorTachoLimit(int motorNumber)
          Returns the current limit on a movement in progress.
 boolean motorForward(int motorNumber, int power)
          Sets the power to the specified motor.
 boolean motorForwardLimit(int motorNumber, int power, int tachoLimit)
          Make the motor run for a specifiec amount of time.
 boolean motorHandBrake(int motorNumber)
          Stops the motor abruptely.
 boolean motorStop(int motorNumber)
          Stops the motor smoothely.
 boolean playTone(int frequency, int duration)
          Plays a tone on the NXT.
 String receiveMsg(int mailbox, boolean remove)
          Reads a BT message to the NXT.
 void resetMotorPosition(int motorNumber, boolean relative)
          Resets the motor position.
 boolean sendMsg(int mailbox, String message)
          Sends a BT message to the NXT.
 void setColorSensorType(int type, int portNumber)
          Sets the type of color sensor to be used on the specifiec port.
 void setLightSensorType(int type, int portNumber)
          Sets the type of light sensor to be used on the specifiec port.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MOTOR_A

public static final int MOTOR_A
The motor port A.

See Also:
Constant Field Values

MOTOR_B

public static final int MOTOR_B
The motor port B.

See Also:
Constant Field Values

MOTOR_C

public static final int MOTOR_C
The motor port C.

See Also:
Constant Field Values

PORT_1

public static final int PORT_1
The sensor port 1.

See Also:
Constant Field Values

PORT_2

public static final int PORT_2
The sensor port 2.

See Also:
Constant Field Values

PORT_3

public static final int PORT_3
The sensor port 3.

See Also:
Constant Field Values

PORT_4

public static final int PORT_4
The sensor port 4.

See Also:
Constant Field Values

ACTIVE

public static final int ACTIVE
The active light sensor type.

See Also:
Constant Field Values

INACTIVE

public static final int INACTIVE
The inactive light sensor type.

See Also:
Constant Field Values

FULL

public static final int FULL
The full color sensor type.

See Also:
Constant Field Values

RED

public static final int RED
The red color sensor type.

See Also:
Constant Field Values

GREEN

public static final int GREEN
The green color sensor type.

See Also:
Constant Field Values

BLUE

public static final int BLUE
The blue color sensor type.

See Also:
Constant Field Values

NONE

public static final int NONE
The none color sensor type.

See Also:
Constant Field Values
Constructor Detail

LegoNXT

public LegoNXT(PApplet parent,
               String commPort)
Creates a new instance of LegoNXT

Parameters:
parent - The PApplet. Use this.
commPort - The COM port where you connected the NXT. This is the name of the virtual COM port that was assigned to the NXT.
Method Detail

playTone

public boolean playTone(int frequency,
                        int duration)
Plays a tone on the NXT.

Parameters:
frequency - The frequency of the tone.
duration - The duration, in milliseconds, of the tone.
Returns:
True if all went ok. False otherwise.

sendMsg

public boolean sendMsg(int mailbox,
                       String message)
Sends a BT message to the NXT.

Parameters:
mailbox - number
message - The message string
Returns:
True if all went ok. False otherwise.

receiveMsg

public String receiveMsg(int mailbox,
                         boolean remove)
Reads a BT message to the NXT.

Parameters:
mailbox - Int mailbox number
remove - Boolean; true clears message from remote inbox
Returns:
String message

motorForward

public boolean motorForward(int motorNumber,
                            int power)
Sets the power to the specified motor.

Parameters:
motorNumber - The motor port (0, 1 or 2). The constants MOTOR_A, MOTOR_B and MOTOR_C should be used.
power - The power of the motor (-100, 100). Positive values make the motor fo forward, negative values make it go backwards.
Returns:
True if all ok. False on error.

motorForwardLimit

public boolean motorForwardLimit(int motorNumber,
                                 int power,
                                 int tachoLimit)
Make the motor run for a specifiec amount of time.

Parameters:
motorNumber - The motor port (0, 1 or 2). The constants MOTOR_A, MOTOR_B and MOTOR_C should be used.
power - The power of the motor (-100, 100). Positive values make the motor fo forward, negative values make it go backwards.
tachoLimit - The tacho limit on the motor. Haven't figured this out completely...
Returns:
True if all ok. False on error.

motorHandBrake

public boolean motorHandBrake(int motorNumber)
Stops the motor abruptely.

Parameters:
motorNumber - The motor port (0, 1 or 2). The constants MOTOR_A, MOTOR_B and MOTOR_C should be used.
Returns:
True if all ok. False on error.

motorStop

public boolean motorStop(int motorNumber)
Stops the motor smoothely.

Parameters:
motorNumber - The motor port (0, 1 or 2). The constants MOTOR_A, MOTOR_B and MOTOR_C should be used.
Returns:
True if all ok. False on error.

getMotorTachoLimit

public long getMotorTachoLimit(int motorNumber)
Returns the current limit on a movement in progress.

Parameters:
motorNumber - The motor port (0, 1 or 2). The constants MOTOR_A, MOTOR_B and MOTOR_C should be used.
Returns:
The current limit on a movement in progress, if any.

getMotorTachoCount

public long getMotorTachoCount(int motorNumber)
Returns number of counts since last reset of the motor counter.

Parameters:
motorNumber - The motor port (0, 1 or 2). The constants MOTOR_A, MOTOR_B and MOTOR_C should be used.
Returns:
The number of counts since last reset of the motor counter.

getMotorBlockTachoCount

public long getMotorBlockTachoCount(int motorNumber)
Returns the current position realative to the last programmed movement.

Parameters:
motorNumber - The motor port (0, 1 or 2). The constants MOTOR_A, MOTOR_B and MOTOR_C should be used.
Returns:
The current position realative to the last programmed movement.

getMotorRotationCount

public long getMotorRotationCount(int motorNumber)
The current position relative to the last reset of the rotation sensor for this motor.

Parameters:
motorNumber - The motor port (0, 1 or 2). The constants MOTOR_A, MOTOR_B and MOTOR_C should be used.
Returns:
The current position relative to the last reset of the rotation sensor for this motor.

resetMotorPosition

public void resetMotorPosition(int motorNumber,
                               boolean relative)
Resets the motor position.

Parameters:
motorNumber - The motor port (0, 1 or 2). The constants MOTOR_A, MOTOR_B and MOTOR_C should be used.
motorNumber -

getButtonState

public boolean getButtonState(int portNumber)
Returns the state of the pressure sensor at the specifiec port.

Parameters:
portNumber - The port number on which the pressure sensor is connected (the constants PORT_1, PORT_2, PORT_3 or PORT_4 should be used).
Returns:
True if sensor pressed. False if not pressed or on error.

getDB

public int getDB(int portNumber)
Gets the DB level of the sound sensor on the specified port.

Parameters:
portNumber - The port number on which the pressure sensor is connected (the constants PORT_1, PORT_2, PORT_3 or PORT_4 should be used).
Returns:
The DB level (0, 100). -1 on error.

getLight

public int getLight(int portNumber)
Returns the light level from the light sensor attached to the specified port.

Parameters:
portNumber - The port number on which the pressure sensor is connected (the constants PORT_1, PORT_2, PORT_3 or PORT_4 should be used).
Returns:
The light level (0..100). -1 on error

setLightSensorType

public void setLightSensorType(int type,
                               int portNumber)
Sets the type of light sensor to be used on the specifiec port.

Parameters:
type - The type of light sensor (ACTIVE or INACTIVE).
portNumber - The port number on which the pressure sensor is connected (the constants PORT_1, PORT_2, PORT_3 or PORT_4 should be used).

getColor

public int getColor(int portNumber)
Returns the color level from the color sensor attached to the specified port.

Parameters:
portNumber - The port number on which the pressure sensor is connected (the constants PORT_1, PORT_2, PORT_3 or PORT_4 should be used).
Returns:
The color level (0..100). -1 on error

setColorSensorType

public void setColorSensorType(int type,
                               int portNumber)
Sets the type of color sensor to be used on the specifiec port.

Parameters:
type - The type of color sensor (FULL or RED or GREEN or BLUE or NONE or RGB).
portNumber - The port number on which the pressure sensor is connected (the constants PORT_1, PORT_2, PORT_3 or PORT_4 should be used).

getDistance

public int getDistance(int portNumber)
Returns the proximity to an obstacle, measured by the ultrasonic sensor.

Parameters:
portNumber - The port number on which the pressure sensor is connected (the constants PORT_1, PORT_2, PORT_3 or PORT_4 should be used).
Returns:
The distance measured. -1 on error.

getBatteryLevel

public int getBatteryLevel()
Returns the battery level.

Returns:
The battery level in millivolts.

dispose

public void dispose()


Processing library NXTComm by Jorge C. S. Cardoso. (c) 2014