AForge.Robotics.Lego
GhostAPI wrapper class.
GhostAPI is a library provided by Lego, to communicate with its
RCX robotics kit.
Creates Ghost communication stack.
Port implementation required.
Protocol implementation required.
Session implementation required.
Stack handle created by this function.
GhostAPI error code.
Finds and selects the first available device.
Stack handle.
Buffer, which will be filled with the name of the selected port device.
Size of the specified buffer.
GhostAPI error code.
Opens the currently selected device.
Handle of the stack to be opened.
GhostAPI error code.
Closes the currently selected device.
Handle of the stack to be closed.
GhostAPI error code.
Sets the current command interleave between the execute and download queue.
Stack handle.
Number of immediate command blocks.
Number of download slices.
GhostAPI error code.
Set the current notification mode to WAIT.
Stack handle.
Must be IntPtr.Zero - no callback function.
GhostAPI error code.
Creates a command queue (containing one command to start with) and return handle.
Queue handle created by this function.
GhostAPI error code.
Releases a command queue.
Queue handle.
GhostAPI error code.
Appends a command to the given command queue.
Queue handle.
Command buffer (command + parameters).
Length of the command buffer.
Length of the expected reply to this command.
GhostAPI error code.
Submits a command queue on the EXECUTE queue.
Stack handle.
Queue handle.
GhostAPI error code.
Gets the first command in the queue.
Queue handle.
Retrieved command handle.
GhostAPI error code.
Gets command reply length.
Command handle.
Reply length.
GhostAPI error code.
Gets command reply.
Command handle.
Buffer for reply data.
Buffer size.
GhostAPI error code.
Mask of severity bits.
Success severity.
Informational severity.
Warning severity.
Error severity.
Checks if return value has success severity.
Error return value.
True if the return value has success severity.
Checks if return value has info severity.
Error return value.
True if the return value has info severity.
Checks if return value has warning severity.
Error return value.
True if the return value has warning severity.
Checks if return value has error severity.
Error return value.
True if the return value has error severity.
Checks for successful return code, which has success or info severity.
Error return value.
True if return value is successful.
Interface, which wraps communication functions with Lego Mindstorms NXT brick.
Get connection status.
Connect to NXT brick.
Returns true if connection was established successfully or false
otherwise.
Disconnect from NXT brick.
Send message to NXT brick over the communication interface.
Buffer containing the message to send.
Returns true if message was sent successfully or false
otherwise.
This method assumes that message starts from the start of the
specified buffer and occupies entire buffer.
Send message to NXT brick over the communication interface.
Buffer containing the message to send.
Length of the message to send.
Returns true if message was sent successfully or false
otherwise.
This method assumes that message starts from the start of the
specified buffer.
Send message to NXT brick over the communication interface.
Buffer containing the message to send.
Offset of the message in the buffer.
Length of the message to send.
Returns true if message was sent successfully or false
otherwise.
Read message from NXT brick over the communication interface.
Buffer to use for message reading.
On successful return the variable keeps message length.
Returns true if message was read successfully or false
otherwise.
Read message from NXT brick over the communication interface.
Buffer to use for message reading.
Offset in the buffer for message.
On successful return the variable keeps message length.
Returns true if message was read successfully or false
otherwise.
Enumeration of command types supported by Lego Mindstorms NXT brick.
Direct command, which requires reply.
System command, which requires reply.
Reply command received from NXT brick.
Direct command, which does not require reply.
System command, which does not require reply.
Enumeration of system commands supported by Lego Mindstorms NXT brick.
Get firmware version of NXT brick.
Set NXT brick name.
Get device information.
Enumeration of direct commands supported by Lego Mindstorms NXT brick.
Keep NXT brick alive.
Play tone of specified frequency.
Get battery level.
Set output state.
Get output state.
Reset motor position.
Set input mode.
Get input values.
Get status of the Low Speed bus.
Write to the Low Speed bus.
Read from the Low Speed bus.
Reset input scaled value.
Enumeration of commands supported by Lego Mindstorms RXT brick.
Check whether or not the RCX is alive.
Play one of defined sounds.
Play tone of specified frequency.
Get ROM and firmware versions.
Get battery power.
Set time displayed on RCX brick.
Turm off RCX brick.
Get value.
Set sensor type.
Set sensor mode.
Clear sensor value.
Set IR transmiter's range.
Turn on/off motor.
Set motor's power.
Set motor's direction
Implementation of serial communication interface with LEGO Mindstorm NXT brick.
Maximum message size, which can be sent over this communication interface to NXT
brick.
Serial port name used for communication.
Get connection status.
Initializes a new instance of the class.
Serial port name to use for communication.
This constructor initializes serial port with default write and read
timeout values, which are 1000 milliseconds.
Initializes a new instance of the class.
Serial port name to use for communication.
Timeout value used for write operations.
Timeout value used for read operations.
Connect to NXT brick.
Returns true if connection was established successfully or false
otherwise.
If communication interface was connected before the call, existing connection will be reused.
If it is required to force reconnection, then method should be called before.
Disconnect from NXT brick.
Send message to NXT brick over the communication interface.
Buffer containing the message to send.
Returns true if message was sent successfully or false
otherwise.
This method assumes that message starts from the start of the
specified buffer and occupies entire buffer.
Send message to NXT brick over the communication interface.
Buffer containing the message to send.
Length of the message to send.
Returns true if message was sent successfully or false
otherwise.
This method assumes that message starts from the start of the
specified buffer.
Send message to NXT brick over the communication interface.
Buffer containing the message to send.
Offset of the message in the buffer.
Length of the message to send.
Returns true if message was sent successfully or false
otherwise.
Read message from NXT brick over the communication interface.
Buffer to use for message reading.
On successful return the variable keeps message length.
Returns true if message was read successfully or false
otherwise.
Read message from NXT brick over the communication interface.
Buffer to use for message reading.
Offset in the buffer for message.
On successful return the variable keeps message length.
Returns true if message was read successfully or false
otherwise.
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 ) )
{
// ...
}
// ...
}
Enumeration of NXT brick sensor ports.
First sensor.
Second sensor.
Third sensor.
Fourth sensor.
Enumeration of NXT brick sensor types.
No sensor.
NXT or Legacy touch sensor.
Legacy temperature sensor.
Legacy light sensor.
Legacy rotation sensor.
NXT light sensor with floodlight enabled.
NXT light sensor with floodlight disabled.
NXT sound sensor (dB scaling).
NXT sound sensor (dBA scaling).
Unused
I2C digital sensor.
I2C digital sensor (9V power).
Unused.
NXT 2.0 color sensor in color detector mode.
NXT 2.0 color sensor in light sensor mode with red light on.
NXT 2.0 color sensor in light sensor mode with green light on.
NXT 2.0 color sensor in light sensor mode with blue light on.
NXT 2.0 color sensor in light sensor mode without light.
NXT 2.0 color sensor internal state (no functionality known yet).
Enumeration of NXT brick sensor modes.
Raw mode.
Boolean mode. Report scaled value as 1 (TRUE) or 0 (FALSE). The firmware uses
inverse Boolean logic to match the physical characteristics of NXT sensors. Readings
are FALSE if raw value exceeds 55% of total range; reading are TRUE if raw value
is less than 45% of total range.
Report scaled value as number of transition between TRUE and FALSE.
Report scaled value as number of transitions from FALSE to TRUE, then back to FALSE.
Report scaled value as percentage of full scale reading for configured sensor type.
Scale terperature reading to degrees Celsius.
Scale terperature reading to degrees Fahrenheit.
Report scaled value as count of ticks on RCX-style rotation sensor.
Class describing sensor's values received from NXT brick's sensor port.
Specifies if data value should be treated as valid data.
Specifies if calibration file was found and used for
field calculation.
Sensor type.
Sensor mode.
Raw A/D value (device dependent).
Normalized A/D value (sensor type dependent), [0, 1023].
Scaled value (sensor mode dependent).
Value scaled according to calibration.
According to Lego notes the value is currently unused.
Enumeration of NXT brick motor ports.
Motor A.
Motor B.
Motor C.
All motors (A, B and C).
Enumeration of supported motor modes.
Motor mode is a bit field, so several modes can be combined.
Mode is not set.
Turn on the motor.
Brake.
Turn on regulated mode.
Enumeration of motor regulation modes.
No regulation will be enabled.
Power control will be enabled on specified motor.
Synchronization will be enabled.
Synchronization need to be enabled on two motors.
Enumeration of motor run states.
Motor will be idle.
Motor will ramp-up.
Motor will be running.
Motor will ramp-down.
Class describing motor's state.
Power, [-100, 100].
Turn ratio, [-100, 100].
Mode (bit field).
Regulation mode.
Run state.
Tacho limit (0 - run forever).
The value determines motor's run limit.
Number of counts since last reset of motor counter.
The value is ignored when motor's state is set. The value is
provided when motor's state is retrieved.
Current position relative to last programmed movement.
The value is ignored when motor's state is set. The value is
provided when motor's state is retrieved.
Current position relative to last reset of motor's rotation sensor.
The value is ignored when motor's state is set. The value is
provided when motor's state is retrieved.
Initializes a new instance of the class.
Initializes a new instance of the class.
Power, [-100, 100].
Turn ratio, [-100, 100].
Mode (bit field).
Regulation mode.
Run state.
The value determines motor's run limit.
Check if connection to NXT brick is established.
Initializes a new instance of the class.
Destroys the instance of the class.
Connect to NXT brick.
Serial port name to use for communication, for example COM1.
Returns true on successful connection or false
otherwise.
If connection to NXT brick was established before the call, existing connection will be reused.
If it is required to force reconnection, then method should be called before.
Disconnect from Lego NXT brick.
Check if the NXT brick is alive and responds to messages.
Returns true if device is alive or false otherwise.
The command also keeps NXT brick alive preventing it from sleep.
Play tone of specified frequency.
Tone frequency in Hz.
Tone duration in milliseconds.
Returns true if device is alive or false otherwise.
Play tone of specified frequency.
Tone frequency in Hz.
Tone duration in milliseconds.
Wait reply from NXT (safer option) or not (faster option).
Returns true if device is alive or false otherwise.
Get firmware version of NXT brick.
Protocol version number.
Firmware version number.
Returns true if command was executed successfully or false otherwise.
Get information about NXT device.
Device name.
Bluetooth address.
Bluetooth signal strength.
Free user Flash.
Returns true if command was executed successfully or false otherwise.
Get battery power of NXT brick.
NXT brick's battery power in millivolts.
Returns true if command was executed successfully or false otherwise.
Set name of NXT device.
Device name to set for the brick.
Returns true if command was executed successfully or false otherwise.
Reset motor's position.
Motor to reset.
Specifies if relative (to last movement) or absolute motor's
position should reset.
Returns true if command was executed successfully or false otherwise.
Reset motor's position.
Motor to reset.
Specifies if relative (to last movement) or absolute motor's
position should reset.
Wait reply from NXT (safer option) or not (faster option).
Returns true if command was executed successfully or false otherwise.
Set motor state.
Motor to set state for.
Motor's state to set.
Returns true if command was executed successfully or false otherwise.
Set motor state.
Motor to set state for.
Motor's state to set.
Wait reply from NXT (safer option) or not (faster option).
Returns true if command was executed successfully or false otherwise.
Get motor state.
Motor to get state for.
Motor's state.
Returns true if command was executed successfully or false otherwise.
Set sensor's type and mode.
Sensor to set type of.
Sensor's type.
Sensor's mode.
Returns true if command was executed successfully or false otherwise.
Set sensor's type and mode.
Sensor to set type of.
Sensor's type.
Sensor's mode.
Wait reply from NXT (safer option) or not (faster option).
Returns true if command was executed successfully or false otherwise.
Get sensor's values.
Sensor to get values of.
etrieved sensor's values.
Returns true if command was executed successfully or false otherwise.
Clear sensor's scaled value.
Sensor to clear value of.
Returns true if command was executed successfully or false otherwise.
Clear sensor's scaled value.
Sensor to clear value of.
Wait reply from NXT (safer option) or not (faster option).
Returns true if command was executed successfully or false otherwise.
Get status of Low Speed bus.
Sensor to get the status from.
Number of bytes that are ready to be read from the bus.
Returns true if command was executed successfully or false otherwise.
Write to Low Speed bus.
Sensor to write to.
Data to send to the I2C device.
Number of bytes expected from device on reply, [0..16].
Can be set to zero if I2C command does not suppose any reply.
Returns true if command was executed successfully or false otherwise.
Data length must be in the [1..16] range.
Write to Low Speed bus.
Sensor to write to.
Data to send to the I2C device.
Number of bytes expected from device on reply, [0..16].
Can be set to zero if I2C command does not suppose any reply.
Wait reply from NXT (safer option) or not (faster option).
Returns true if command was executed successfully or false otherwise.
Data length must be in the [1..16] range.
Read data from Low Speed bus.
Sensor to read data from.
Array to read data to.
Bytes actually read from I2C device.
Returns true if command was executed successfully or false otherwise.
Read value of ultrasonic distance sensor.
Sensor to read value from.
Distance value obtained from ultrasonic sensor, [0..255] cm.
Returns true if command was executed successfully or false otherwise.
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
method. Then it waits until there is something available
to read using method. Finally it reads sensor's value
using device. See
this page
for details.
Before using this method it is required to use
method to set sensor's type to
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 event is raised every time a command is sent successfully.
The event is raised every time a command is read successfully.
Send command to Lego NXT brick and read reply.
Command to send.
Buffer to receive reply into.
Returns true if the command was sent successfully and reply was
received, otherwise false.
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.
Read data from HiTechnic color sensor (also color sensor v2).
Sensor to read from.
Found color number.
Found red value.
Found green value.
Found blue value.
Returns true if the command was sent successfully and reply was
received, otherwise false.
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
method. Then it waits until there is something available
to read using method. Finally it reads sensor's value
using device. See
this page
for details.
Before using this method it is required to use
method to set sensor's type to
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 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
Read data from HiTechnic compass sensor.
Sensor to read from.
The magnetic heading, [0, 359] degrees.
Returns true if the command was sent successfully and reply was
received, otherwise false.
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
method. Then it waits until there is something available
to read using method. Finally it reads sensor's value
using device. See
this page
for details.
Before using this method it is required to use
method to set sensor's type to
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 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.
Sensor to read from.
Acceleration in X direction, with a scaling of approximately 200 counts per g.
Acceleration in Y direction, with a scaling of approximately 200 counts per g.
Acceleration in Z direction, with a scaling of approximately 200 counts per g.
Returns true if the command was sent successfully and reply was
received, otherwise false.
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
method. Then it waits until there is something available
to read using method. Finally it reads sensor's value
using device. See
this page
for details.
Before using this method it is required to use
method to set sensor's type to
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 method.
Manipulation of Lego Mindstorms RCX device.
The class allows to manipulate with Lego Mindstorms RCX device,
setting its motors' state, getting information about sensors'
values and performing some other manipulations.
The class supports both types of IR towers - USB and serial (see
).
The class uses GhostAPI to communicate with Lego RCX device, so its
libraries (GhostAPI.dll, PbkComm32.dll and PbkUsbPort.dll) should be placed into applications folder.
The class is deprecated.
Sample usage:
// create an instance of RCX brick
RCXBrick rcx = new RCXBrick( );
// connect to the device
if ( rcx.Connect( RCXBrick.IRTowerType.USB ) )
{
// set forward direction of motor A
rcx.SetMotorDirection( RCXBrick.Motor.A, true );
// set power of motor
rcx.SetMotorPower( RCXBrick.Motor.A, 1 );
// turm motor on
rcx.SetMotorOn( RCXBrick.Motor.A, true );
// ...
// turn off motors A, B and C
rcx.SetMotorOn( RCXBrick.Motor.ABC, false );
// get first sensor's value
short value;
if ( rcx.GetSensorValue( RCXBrick.Sensor.First, out value ) )
{
// ...
}
// ...
}
Type of IR tower used for communication with RCX.
USB IR tower.
RS232 IR tower.
Enumeration of sound type playable by Lego RCX brick.
Blip sound.
Double beep spund.
Downward tones sound.
Upward tones sound.
Low buzz sound.
Fast upward tones sound.
Enumeration of RCX brick sensor ports.
First sensor.
Second sensor.
Third sensor.
Enumeration of RCX brick sensor types.
Use method to set RCX sensor's type.
Raw sensor.
Touch sensor (default mode is boolean).
Temperature sensor (default mode is temperature in 癈).
Light sensor (default mode is percentage).
Rotation sensor (default mode is angle).
Enumeration of RCX brick sensor modes.
Use method to set RCX sensor's mode.
Raw mode - value in [0, 1023].
Boolean - either 0 or 1.
Number of boolean transitions.
Number of boolean transitions divided by two.
Raw value scaled to [0, 100].
Temperature in 癈 - 1/10ths of a degree, [-19.8, 69.5].
Temperature in 癋 - 1/10ths of a degree, [-3.6, 157.1].
Angle - 1/16ths of a rotation, represented as a signed short.
Enumeration of RCX brick motor ports.
Motor A.
Motor B.
Motor C.
Motors A and B.
Motors A and C.
Motors B and C.
Motors A, B and C.
All motors (A, B and C).
Check if connection to RCX brick is established.
Initializes a new instance of the class.
Destroys the instance of the class.
Connect to Lego RCX brick.
Type of IR tower to use for communication with RCX brick.
Returns true on successful connection or false
otherwise.
If connection to RCX brick was established before the call, existing connection will be reused.
If it is required to force reconnection, then method should be called before.
Disconnnect from Lego RCX brick.
Check if the RCX brick is alive and responds to messages.
Returns true if device is alive or false otherwise.
Play one of supported sounds.
Sound type to play.
Returns true if command was executed successfully or false otherwise.
Play tone of specified frequency.
Tone frequency in Hz.
Tone duration in 1/100ths of a second.
Returns true if command was executed successfully or false otherwise.
Get version information of RCX brick.
ROM version number.
Firmware version number.
Returns true if command was executed successfully or false otherwise.
Get battery power of RCX brick.
RCX brick's battery power in millivolts.
Returns true if command was executed successfully or false otherwise.
Set current time for the RCX brick.
Hours, [0..23].
Minutes, [0..59].
Returns true if command was executed successfully or false otherwise.
Turn off the RCX brick.
Returns true if command was executed successfully or false otherwise.
Get sensor's value.
Sensor to get value of.
Retrieved sensor's value (units depend on current
sensor's type and mode).
Returns true if command was executed successfully or false otherwise.
Set sensor's type.
Sensor to set type of.
Sensor type to set.
Returns true if command was executed successfully or false otherwise.
Set sensor's mode.
Sensor to set mode of.
Sensor mode to set.
Returns true if command was executed successfully or false otherwise.
Clear the counter associated with the specified sensor by setting it to a value of zero.
Sensor to clear value of.
Returns true if command was executed successfully or false otherwise.
Turn on/off specified motors.
Motors to turn on/off.
True to turn motors on, otherwise false.
Returns true if command was executed successfully or false otherwise.
Set power of specified motors.
Motors to set power of.
Power level to set, [0..7].
Returns true if command was executed successfully or false otherwise.
Set direction of specified motors.
Motors to set direction of.
True to set forward direction, false to set backward.
Returns true if command was executed successfully or false otherwise.
Set IR transmitter's range.
True if long range should be set, otherwise false.
Returns true if command was executed successfully or false otherwise.
Send command to Lego RCX brick and read reply.
Command to send.
Buffer to receive reply into.
Expected reply length.
Returns true if the command was sent successfully and reply was
received, otherwise false.
Communication can not be performed, because connection with
RCX brick was not established yet.
Reply buffer size is smaller than the reply data size.
Reply does not correspond to command (first byte of reply
should be complement (bitwise NOT) to the first byte of command orred with 0x08).