C# Класс 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 ) ) { // ... } // ... }
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
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.

Описание методов

ClearSensor() публичный метод

Clear sensor's scaled value.
public ClearSensor ( Sensor sensor ) : bool
sensor Sensor Sensor to clear value of.
Результат bool

ClearSensor() публичный метод

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).
Результат bool

Connect() публичный метод

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.
Результат bool

Disconnect() публичный метод

Disconnect from Lego NXT brick.
public Disconnect ( ) : void
Результат void

GetBatteryPower() публичный метод

Get battery power of NXT brick.
public GetBatteryPower ( int &power ) : bool
power int NXT brick's battery power in millivolts.
Результат bool

GetDeviceInformation() публичный метод

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.
Результат bool

GetMotorState() публичный метод

Get motor state.
public GetMotorState ( Motor motor, MotorState &state ) : bool
motor Motor Motor to get state for.
state MotorState Motor's state.
Результат bool

GetSensorValue() публичный метод

Get sensor's values.
public GetSensorValue ( Sensor sensor, SensorValues &sensorValues ) : bool
sensor Sensor Sensor to get values of.
sensorValues SensorValues etrieved sensor's values.
Результат bool

GetUltrasonicSensorsValue() публичный метод

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.
Результат bool

GetVersion() публичный метод

Get firmware version of NXT brick.
public GetVersion ( string &protocolVersion, string &firmwareVersion ) : bool
protocolVersion string Protocol version number.
firmwareVersion string Firmware version number.
Результат bool

IsAlive() публичный метод

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
Результат bool

LsGetStatus() публичный метод

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.
Результат bool

LsRead() публичный метод

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.
Результат bool

LsWrite() публичный метод

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.
Результат bool

LsWrite() публичный метод

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).
Результат bool

NXTBrick() публичный метод

Initializes a new instance of the NXTBrick class.
public NXTBrick ( ) : System
Результат System

PlayTone() публичный метод

Play tone of specified frequency.
public PlayTone ( short frequency, short duration ) : bool
frequency short Tone frequency in Hz.
duration short Tone duration in milliseconds.
Результат bool

PlayTone() публичный метод

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).
Результат bool

ReadHiTechnicAccelerationTiltSensor() публичный метод

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.
Результат bool

ReadHiTechnicColorSensor() публичный метод

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.
Результат bool

ReadHiTechnicCompassSensor() публичный метод

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.
Результат bool

ResetMotorPosition() публичный метод

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.
Результат bool

ResetMotorPosition() публичный метод

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).
Результат bool

SendCommand() публичный метод

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.
Результат bool

SetBrickName() публичный метод

Set name of NXT device.
public SetBrickName ( string deviceName ) : bool
deviceName string Device name to set for the brick.
Результат bool

SetMotorState() публичный метод

Set motor state.
public SetMotorState ( Motor motor, MotorState state ) : bool
motor Motor Motor to set state for.
state MotorState Motor's state to set.
Результат bool

SetMotorState() публичный метод

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).
Результат bool

SetSensorMode() публичный метод

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.
Результат bool

SetSensorMode() публичный метод

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).
Результат bool