C# Class AForge.Robotics.Lego.NXTBrick

Manipulation of Lego Mindstorms NXT device.

The class allows to manipulate with Lego Mindstorms NXT device, setting/getting its motors' state, getting information about sensors' values and retrieving generic information about the NXT brick.

Only communication through Bluetooth (virtual serial port) is supported at this point.

Sample usage:

// create an instance of NXT brick NXTBrick nxt = new NXTBrick( ); // connect to the device if ( nxt.Connect( "COM8" ) ) { // run motor A NXTBrick.MotorState motorState = new NXTBrick.MotorState( ); motorState.Power = 70; motorState.TurnRatio = 50; motorState.Mode = NXTBrick.MotorMode.On; motorState.Regulation = NXTBrick.MotorRegulationMode.Idle; motorState.RunState = NXTBrick.MotorRunState.Running; motorState.TachoLimit = 1000; nxt.SetMotorState( NXTBrick.Motor.A, motorState ); // get input value from the first sensor NXTBrick.SensorValues sensorValues; if ( nxt.GetSensorValue( NXTBrick.Sensor.First, out sensorValues ) ) { // ... } // ... }
Mostra file Open project: holisticware-admin/MonoVersal.AForgeNET Class Usage Examples

Public Methods

Method Description
ClearSensor ( Sensor sensor ) : bool

Clear sensor's scaled value.

ClearSensor ( Sensor sensor, bool waitReply ) : bool

Clear sensor's scaled value.

Connect ( string portName ) : bool

Connect to NXT brick.

If connection to NXT brick was established before the call, existing connection will be reused. If it is required to force reconnection, then Disconnect method should be called before.

Disconnect ( ) : void

Disconnect from Lego NXT brick.

GetBatteryPower ( int &power ) : bool

Get battery power of NXT brick.

GetDeviceInformation ( string &deviceName, byte &btAddress, int &btSignalStrength, int &freeUserFlash ) : bool

Get information about NXT device.

GetMotorState ( Motor motor, MotorState &state ) : bool

Get motor state.

GetSensorValue ( Sensor sensor, SensorValues &sensorValues ) : bool

Get sensor's values.

GetUltrasonicSensorsValue ( Sensor sensor, int &value ) : bool

Read value of ultrasonic distance sensor.

The method retrieves value of ultrasonic distance sensor by communicating with I2C device (writing to and reading from low speed bus). The method first sends { 0x02, 0x42 } command to the specified device using LsWrite( Sensor, byte[], int ) method. Then it waits until there is something available to read using LsGetStatus method. Finally it reads sensor's value using LsRead device. See this page for details.

Before using this method it is required to use SetSensorMode( Sensor, SensorType, SensorMode, bool ) method to set sensor's type to SensorType.Lowspeed9V mode. It should be done once after NXT brick is powered on. If sensor's type is not set properly, the method will generate an exception. Also after setting sensor's type application may need to wait a bit to give device some time to initialize.

GetVersion ( string &protocolVersion, string &firmwareVersion ) : bool

Get firmware version of NXT brick.

IsAlive ( ) : bool

Check if the NXT brick is alive and responds to messages.

The command also keeps NXT brick alive preventing it from sleep.

LsGetStatus ( Sensor sensor, int &readyBytes ) : bool

Get status of Low Speed bus.

LsRead ( Sensor sensor, byte readValues, int &bytesRead ) : bool

Read data from Low Speed bus.

LsWrite ( Sensor sensor, byte data, int expectedBytes ) : bool

Write to Low Speed bus.

LsWrite ( Sensor sensor, byte data, int expectedBytes, bool waitReply ) : bool

Write to Low Speed bus.

NXTBrick ( ) : System

Initializes a new instance of the NXTBrick class.

PlayTone ( short frequency, short duration ) : bool

Play tone of specified frequency.

PlayTone ( short frequency, short duration, bool waitReply ) : bool

Play tone of specified frequency.

ReadHiTechnicAccelerationTiltSensor ( NXTBrick sensor, int &xAceeleration, int &yAceeleration, int &zAceeleration ) : bool

Read data from HiTechnic acceleration/tilt sensor. The HiTechnic accelerometer/tilt sensor measures acceleration in three axes. It measures also tilt along each axis. Using the sensor, you can measure the acceleration of your robot in the range of -2g to 2g.

The method retrieves the acceleration in three directions of a HiTechnic acceleration/tilt sensor by communicating with I2C device (writing to and reading from low speed bus). The method first sends { 0x02, 0x42 } command to the specified device using LsWrite( Sensor, byte[], int ) method. Then it waits until there is something available to read using LsGetStatus method. Finally it reads sensor's value using LsRead device. See this page for details.

Before using this method it is required to use SetSensorMode( Sensor, SensorType, SensorMode, bool ) method to set sensor's type to SensorType.Lowspeed mode. It should be done once after NXT brick is powered onq If sensor's type is not set properly, the method will generate an exception. Also after setting sensor's type application may need to wait a bit to give device some time to initialize.

The acceleration sensor can also be used to measure tilt in three axes This is possible because gravity is perceived as acceleration. When the sensor is stationary and in the normal horizontal position, the x and y axis will be near zero, because they are horizontal, while the z axis will be near 200, which represents g. If you tilt the sensor then gravity will also be detected on the other axis and the value for the z axis will go down. Since gravity is distributed among the three component vectors, the tilt of the sensor can be determined.

NXT Firmware version 1.05 or later must be loaded in the NXT for the acceleration/tilt sensor and other digital I2C sensors to operate correctly. You can check the firmware version using the GetVersion method.

ReadHiTechnicColorSensor ( NXTBrick sensor, int &colorNumber, int &redValue, int &greenValue, int &blueValue ) : bool

Read data from HiTechnic color sensor (also color sensor v2).

The method retrieves the color valuse of a HiTechnic color sensor by communicating with I2C device (writing to and reading from low speed bus). The method first sends { 0x02, 0x42 } command to the specified device using LsWrite( Sensor, byte[], int ) method. Then it waits until there is something available to read using LsGetStatus method. Finally it reads sensor's value using LsRead device. See this page for details.

Before using this method it is required to use SetSensorMode( Sensor, SensorType, SensorMode, bool ) method to set sensor's type to SensorType.Lowspeed mode. It should be done once after NXT brick is powered on. If sensor's type is not set properly, the method will generate an exception. Also after setting sensor's type application may need to wait a bit to give device some time to initialize.

NXT Firmware version 1.24 must be loaded in the NXT for the HiTechnic color sensor to operate correctly. You can check the firmware version using the GetVersion method.

The color sensor V2 must be configured to match the mains electricity frequency for your country. Details on how to configure the Color Sensor V2 can be found at

ReadHiTechnicCompassSensor ( NXTBrick sensor, int &angle ) : bool

Read data from HiTechnic compass sensor.

The method retrieves the angle of a HiTechnic compass sensor by communicating with I2C device (writing to and reading from low speed bus). The method first sends { 0x02, 0x42 } command to the specified device using LsWrite( Sensor, byte[], int ) method. Then it waits until there is something available to read using LsGetStatus method. Finally it reads sensor's value using LsRead device. See this page for details.

Before using this method it is required to use SetSensorMode( Sensor, SensorType, SensorMode, bool ) method to set sensor's type to SensorType.Lowspeed mode. It should be done once after NXT brick is powered on. If sensor's type is not set properly, the method will generate an exception. Also after setting sensor's type application may need to wait a bit to give device some time to initialize.

The HiTechnic compass sensor will only operate correctly in a horizontal plane so you must keep the compass level for it to read correctly. This is very important so remember this when you build it into your robot. It is highly desirable to mount the compass at least 6 inches (15cm) away from the motors and 4 inches (10cm) away from the NXT brick itself. Try to make sure it is firmly mounted, if it bounces around, the readings may bounce around too.

NXT Firmware version 1.03 must be loaded in the NXT for the compass to operate correctly. You can check the firmware version using the GetVersion method.

ResetMotorPosition ( Motor motor, bool relative ) : bool

Reset motor's position.

ResetMotorPosition ( Motor motor, bool relative, bool waitReply ) : bool

Reset motor's position.

SendCommand ( byte command, byte reply ) : bool

Send command to Lego NXT brick and read reply.

SetBrickName ( string deviceName ) : bool

Set name of NXT device.

SetMotorState ( Motor motor, MotorState state ) : bool

Set motor state.

SetMotorState ( Motor motor, MotorState state, bool waitReply ) : bool

Set motor state.

SetSensorMode ( Sensor sensor, SensorType type, SensorMode mode ) : bool

Set sensor's type and mode.

SetSensorMode ( Sensor sensor, SensorType type, SensorMode mode, bool waitReply ) : bool

Set sensor's type and mode.

Method Details

ClearSensor() public method

Clear sensor's scaled value.
public ClearSensor ( Sensor sensor ) : bool
sensor Sensor Sensor to clear value of.
return bool

ClearSensor() public method

Clear sensor's scaled value.
public ClearSensor ( Sensor sensor, bool waitReply ) : bool
sensor Sensor Sensor to clear value of.
waitReply bool Wait reply from NXT (safer option) or not (faster option).
return bool

Connect() public method

Connect to NXT brick.
If connection to NXT brick was established before the call, existing connection will be reused. If it is required to force reconnection, then Disconnect method should be called before.
public Connect ( string portName ) : bool
portName string Serial port name to use for communication, for example COM1.
return bool

Disconnect() public method

Disconnect from Lego NXT brick.
public Disconnect ( ) : void
return void

GetBatteryPower() public method

Get battery power of NXT brick.
public GetBatteryPower ( int &power ) : bool
power int NXT brick's battery power in millivolts.
return bool

GetDeviceInformation() public method

Get information about NXT device.
public GetDeviceInformation ( string &deviceName, byte &btAddress, int &btSignalStrength, int &freeUserFlash ) : bool
deviceName string Device name.
btAddress byte Bluetooth address.
btSignalStrength int Bluetooth signal strength.
freeUserFlash int Free user Flash.
return bool

GetMotorState() public method

Get motor state.
public GetMotorState ( Motor motor, MotorState &state ) : bool
motor Motor Motor to get state for.
state MotorState Motor's state.
return bool

GetSensorValue() public method

Get sensor's values.
public GetSensorValue ( Sensor sensor, SensorValues &sensorValues ) : bool
sensor Sensor Sensor to get values of.
sensorValues SensorValues etrieved sensor's values.
return bool

GetUltrasonicSensorsValue() public method

Read value of ultrasonic distance sensor.

The method retrieves value of ultrasonic distance sensor by communicating with I2C device (writing to and reading from low speed bus). The method first sends { 0x02, 0x42 } command to the specified device using LsWrite( Sensor, byte[], int ) method. Then it waits until there is something available to read using LsGetStatus method. Finally it reads sensor's value using LsRead device. See this page for details.

Before using this method it is required to use SetSensorMode( Sensor, SensorType, SensorMode, bool ) method to set sensor's type to SensorType.Lowspeed9V mode. It should be done once after NXT brick is powered on. If sensor's type is not set properly, the method will generate an exception. Also after setting sensor's type application may need to wait a bit to give device some time to initialize.

public GetUltrasonicSensorsValue ( Sensor sensor, int &value ) : bool
sensor Sensor Sensor to read value from.
value int Distance value obtained from ultrasonic sensor, [0..255] cm.
return bool

GetVersion() public method

Get firmware version of NXT brick.
public GetVersion ( string &protocolVersion, string &firmwareVersion ) : bool
protocolVersion string Protocol version number.
firmwareVersion string Firmware version number.
return bool

IsAlive() public method

Check if the NXT brick is alive and responds to messages.
The command also keeps NXT brick alive preventing it from sleep.
public IsAlive ( ) : bool
return bool

LsGetStatus() public method

Get status of Low Speed bus.
public LsGetStatus ( Sensor sensor, int &readyBytes ) : bool
sensor Sensor Sensor to get the status from.
readyBytes int Number of bytes that are ready to be read from the bus.
return bool

LsRead() public method

Read data from Low Speed bus.
public LsRead ( Sensor sensor, byte readValues, int &bytesRead ) : bool
sensor Sensor Sensor to read data from.
readValues byte Array to read data to.
bytesRead int Bytes actually read from I2C device.
return bool

LsWrite() public method

Write to Low Speed bus.
Data length must be in the [1..16] range.
public LsWrite ( Sensor sensor, byte data, int expectedBytes ) : bool
sensor Sensor Sensor to write to.
data byte Data to send to the I2C device.
expectedBytes int Number of bytes expected from device on reply, [0..16]. /// Can be set to zero if I2C command does not suppose any reply.
return bool

LsWrite() public method

Write to Low Speed bus.
Data length must be in the [1..16] range.
public LsWrite ( Sensor sensor, byte data, int expectedBytes, bool waitReply ) : bool
sensor Sensor Sensor to write to.
data byte Data to send to the I2C device.
expectedBytes int Number of bytes expected from device on reply, [0..16]. /// Can be set to zero if I2C command does not suppose any reply.
waitReply bool Wait reply from NXT (safer option) or not (faster option).
return bool

NXTBrick() public method

Initializes a new instance of the NXTBrick class.
public NXTBrick ( ) : System
return System

PlayTone() public method

Play tone of specified frequency.
public PlayTone ( short frequency, short duration ) : bool
frequency short Tone frequency in Hz.
duration short Tone duration in milliseconds.
return bool

PlayTone() public method

Play tone of specified frequency.
public PlayTone ( short frequency, short duration, bool waitReply ) : bool
frequency short Tone frequency in Hz.
duration short Tone duration in milliseconds.
waitReply bool Wait reply from NXT (safer option) or not (faster option).
return bool

ReadHiTechnicAccelerationTiltSensor() public method

Read data from HiTechnic acceleration/tilt sensor. The HiTechnic accelerometer/tilt sensor measures acceleration in three axes. It measures also tilt along each axis. Using the sensor, you can measure the acceleration of your robot in the range of -2g to 2g.

The method retrieves the acceleration in three directions of a HiTechnic acceleration/tilt sensor by communicating with I2C device (writing to and reading from low speed bus). The method first sends { 0x02, 0x42 } command to the specified device using LsWrite( Sensor, byte[], int ) method. Then it waits until there is something available to read using LsGetStatus method. Finally it reads sensor's value using LsRead device. See this page for details.

Before using this method it is required to use SetSensorMode( Sensor, SensorType, SensorMode, bool ) method to set sensor's type to SensorType.Lowspeed mode. It should be done once after NXT brick is powered onq If sensor's type is not set properly, the method will generate an exception. Also after setting sensor's type application may need to wait a bit to give device some time to initialize.

The acceleration sensor can also be used to measure tilt in three axes This is possible because gravity is perceived as acceleration. When the sensor is stationary and in the normal horizontal position, the x and y axis will be near zero, because they are horizontal, while the z axis will be near 200, which represents g. If you tilt the sensor then gravity will also be detected on the other axis and the value for the z axis will go down. Since gravity is distributed among the three component vectors, the tilt of the sensor can be determined.

NXT Firmware version 1.05 or later must be loaded in the NXT for the acceleration/tilt sensor and other digital I2C sensors to operate correctly. You can check the firmware version using the GetVersion method.

public ReadHiTechnicAccelerationTiltSensor ( NXTBrick sensor, int &xAceeleration, int &yAceeleration, int &zAceeleration ) : bool
sensor NXTBrick Sensor to read from.
xAceeleration int Acceleration in X direction, with a scaling of approximately 200 counts per g.
yAceeleration int Acceleration in Y direction, with a scaling of approximately 200 counts per g.
zAceeleration int Acceleration in Z direction, with a scaling of approximately 200 counts per g.
return bool

ReadHiTechnicColorSensor() public method

Read data from HiTechnic color sensor (also color sensor v2).

The method retrieves the color valuse of a HiTechnic color sensor by communicating with I2C device (writing to and reading from low speed bus). The method first sends { 0x02, 0x42 } command to the specified device using LsWrite( Sensor, byte[], int ) method. Then it waits until there is something available to read using LsGetStatus method. Finally it reads sensor's value using LsRead device. See this page for details.

Before using this method it is required to use SetSensorMode( Sensor, SensorType, SensorMode, bool ) method to set sensor's type to SensorType.Lowspeed mode. It should be done once after NXT brick is powered on. If sensor's type is not set properly, the method will generate an exception. Also after setting sensor's type application may need to wait a bit to give device some time to initialize.

NXT Firmware version 1.24 must be loaded in the NXT for the HiTechnic color sensor to operate correctly. You can check the firmware version using the GetVersion method.

The color sensor V2 must be configured to match the mains electricity frequency for your country. Details on how to configure the Color Sensor V2 can be found at

public ReadHiTechnicColorSensor ( NXTBrick sensor, int &colorNumber, int &redValue, int &greenValue, int &blueValue ) : bool
sensor NXTBrick Sensor to read from.
colorNumber int Found color number.
redValue int Found red value.
greenValue int Found green value.
blueValue int Found blue value.
return bool

ReadHiTechnicCompassSensor() public method

Read data from HiTechnic compass sensor.

The method retrieves the angle of a HiTechnic compass sensor by communicating with I2C device (writing to and reading from low speed bus). The method first sends { 0x02, 0x42 } command to the specified device using LsWrite( Sensor, byte[], int ) method. Then it waits until there is something available to read using LsGetStatus method. Finally it reads sensor's value using LsRead device. See this page for details.

Before using this method it is required to use SetSensorMode( Sensor, SensorType, SensorMode, bool ) method to set sensor's type to SensorType.Lowspeed mode. It should be done once after NXT brick is powered on. If sensor's type is not set properly, the method will generate an exception. Also after setting sensor's type application may need to wait a bit to give device some time to initialize.

The HiTechnic compass sensor will only operate correctly in a horizontal plane so you must keep the compass level for it to read correctly. This is very important so remember this when you build it into your robot. It is highly desirable to mount the compass at least 6 inches (15cm) away from the motors and 4 inches (10cm) away from the NXT brick itself. Try to make sure it is firmly mounted, if it bounces around, the readings may bounce around too.

NXT Firmware version 1.03 must be loaded in the NXT for the compass to operate correctly. You can check the firmware version using the GetVersion method.

public ReadHiTechnicCompassSensor ( NXTBrick sensor, int &angle ) : bool
sensor NXTBrick Sensor to read from.
angle int The magnetic heading, [0, 359] degrees.
return bool

ResetMotorPosition() public method

Reset motor's position.
public ResetMotorPosition ( Motor motor, bool relative ) : bool
motor Motor Motor to reset.
relative bool Specifies if relative (to last movement) or absolute motor's /// position should reset.
return bool

ResetMotorPosition() public method

Reset motor's position.
public ResetMotorPosition ( Motor motor, bool relative, bool waitReply ) : bool
motor Motor Motor to reset.
relative bool Specifies if relative (to last movement) or absolute motor's /// position should reset.
waitReply bool Wait reply from NXT (safer option) or not (faster option).
return bool

SendCommand() public method

Send command to Lego NXT brick and read reply.
Communication can not be performed, because connection with /// NXT brick was not established yet. Reply buffer size is smaller than the reply data size. Reply does not correspond to command (second byte of reply should /// be equal to second byte of command). Error occurred on NXT brick side.
public SendCommand ( byte command, byte reply ) : bool
command byte Command to send.
reply byte Buffer to receive reply into.
return bool

SetBrickName() public method

Set name of NXT device.
public SetBrickName ( string deviceName ) : bool
deviceName string Device name to set for the brick.
return bool

SetMotorState() public method

Set motor state.
public SetMotorState ( Motor motor, MotorState state ) : bool
motor Motor Motor to set state for.
state MotorState Motor's state to set.
return bool

SetMotorState() public method

Set motor state.
public SetMotorState ( Motor motor, MotorState state, bool waitReply ) : bool
motor Motor Motor to set state for.
state MotorState Motor's state to set.
waitReply bool Wait reply from NXT (safer option) or not (faster option).
return bool

SetSensorMode() public method

Set sensor's type and mode.
public SetSensorMode ( Sensor sensor, SensorType type, SensorMode mode ) : bool
sensor Sensor Sensor to set type of.
type SensorType Sensor's type.
mode SensorMode Sensor's mode.
return bool

SetSensorMode() public method

Set sensor's type and mode.
public SetSensorMode ( Sensor sensor, SensorType type, SensorMode mode, bool waitReply ) : bool
sensor Sensor Sensor to set type of.
type SensorType Sensor's type.
mode SensorMode Sensor's mode.
waitReply bool Wait reply from NXT (safer option) or not (faster option).
return bool