LEGO::NXT - LEGO NXT Direct Commands API.
use LEGO::NXT; # Create a new Bluetooth/NXT object by connecting to # a specific bluetooth address and channel. my $nxt = LEGO::NXT->new( 'xx:xx:xx:xx:xx:xx', 1 ); $nxt->play_sound_file($NXT_NORET, 0,'! Attention.rso'); $res = $nxt->get_battery_level($NXT_RET); # Turn on Motor 1 to full power. $res = $nxt->set_output_state( $NXT_RET, $NXT_SENSOR1, 100, $NXT_MOTORON|$NXT_REGULATED, $NXT_REGULATION_MODE_MOTOR_SPEED, 0, $NXT_MOTOR_RUN_STATE_RUNNING, 0, );
This module provides low-level control of a LEGO NXT brick over bluetooth using the Direct Commands API. This API will not enable you to run programs on the NXT, rather, it will connect to the NXT and issue real-time commands that turn on/off motors, retrieve sensor values, play sound, and more.
Users will leverage this API to control the NXT directly from an external box.
This is known to work on Linux. Other platforms are currently untested, though it should work on any system that has the Net::Bluetooth module.
There is a manual for this module with an introduction, tutorials, plugins, FAQ, etc. See LEGO::NXT::Manual.
If you would like to get some help join the #lego-nxt IRC chat room on the MagNET IRC network (the official perl IRC network). More information at:
LEGO::NXT supports the ability to load plugins.
use LEGO::NXT qw( Scorpion );
Plugins provide higher level and more sophisticated means of handling your NXT. Likely you will want to use a plugin if you want to control your NXT as the methods in LEGO::NXT itself are very low level and tedious to use by themselves.
Please see LEGO::NXT::Manual::Plugins for more details about how to use plugins (and write your own!) as well as what plugins are available to you.
$nxt = LEGO::NXT->new( new LEGO::NXT:BlueComm('xx:xx:xx:xx',0) ); $nxt = LEGO::NXT->new( new LEGO::NXT:USBComm() );
Creates a new NXT object, however a connection is not established until the first direct command is issued. Argument 1 should be the bluetooth address of your NXT (from "hcitool scan" for instance). Argument 2 is the channel you wish to connect on -- 1 or 2 seems to work.
Sets the port of your choosing to use the ultrasound digital sensor.
Returns the units of measurement the US sensor is using (cm? in?)
Returns the distance reading from the NXT from register $byte. $byte should be a value 0-7 indicating the measurement register in the ultrasound sensor. In continuous measurement mode, measurements are stored in register 0 only, however in one-shot mode, each time one-shot is called a value will be stored in a new register.
Returns the time period between ultrasound measurements.
Returns whether the sensor is in one-off mode or continuous measurement mode (the default).
Returns the calibrated zero-distance value for the sensor
Returns the scale factor used to compute distances
Returns the scale divisor used to compute distances
Turns the ultrasound sensor off
Puts the sensor in single shot mode - it will only store a value in a register once each time this function is called
Puts the sensor in continuous measurement mode.
In this mode the US sensor will detect only other ultrasound sensors in the vicinity.
I won't lie - I don't know what a "warm reset" is, but it sounds like a nice new beginning to me. =)
Sets the sampling interval for the range sensor.
TODO: Document valid values...
Sets the calibrated zero value for the sensor.
Sets the scale factor used in computing range.
Sets the scale divisor used in computing range.
Start a program on the NXT called $filename
Stop the currently executing program on the NXT
Play a Tone in $pitch HZ for $duration miliseconds
Play a NXT sound file called $file. Specify $repeat=1 for infinite repeat, 0 to play only once.
Set the output state for one of the motor ports.
$port One of the motor port constants. $power -100 to 100 power level. $mode An bitwise or of output mode constants. $regulation One of the motor regulation mode constants. $runstate One of the motor runstate constants. $tacholimit Number of rotation ticks the motor should turn before it stops.
Configure the input mode of a sensor port.
$port A sensor port constant. $sensor_type A sensor type constant. $sensor_mode A sensor mode constant.
$ret = $nxt->get_output_state($NXT_RET,$port)
Retrieve the current ouput state of $port.
$ret A hashref containing the port attributes.
$ret = $nxt->get_input_values($NXT_RET,$port)
Retrieve the current sensor input values of $port.
$ret A hashref containing the sensor value attributes.
If your sensor port is using scaled values, reset them.
Write a $message to local mailbox# $mailbox.
$ret = $nxt->get_battery_level($NXT_RET) $ret A hash containing battery attributes - voltage in MV
Stops the currently playing sound file
Prevents the NXT from entering sleep mode
Determine whether there is data ready to read from an I2C digital sensor. NOTE: The Ultrasonic Range sensor is such a sensor and must be interfaced via the ls* commands
Send an I2C command to a digital I2C sensor.
$port The sensor port of the I2C sensor $txlen The length of $txdata $rxlen The length of the expected response (sensor/command specific) $txdata The I2C command you wish to send in packed byte format. NOTE: The NXT will suffix the command with a status byte R+0x03, but you dont need to worry about this. Do not send it as part of $txdata though - it will result in a bus error.
NOTE: The Ultrasonic Range sensor is such a sensor and must be interfaced via the ls* commands
Read a pending I2C message from a digital I2C device.
Higher level I2C request-response routine. Loops to ensure data is ready to read from the sensor and returns the result.
$ret = $nxt->get_current_program_name($NXT_RET)
$ret is a hash containing info on the current;y running program.
$ret = $nxt->message_read($NXT_RET,$remotebox,$localbox,$remove)
Read a message.
Caution. Use these only if you know what you're doing.
If you know what you're doing, these methods can be very useful. If you don't know what you're doing, you will probably end up with a dead robot. Beware. Seriously.
NOTE: Every system command requires a return value, so there's no need to pass NXT_RET.
$ret = $nxt->sys_open_read($filename);
Opens a system file for reading, returns a file descriptor.
$ret = $nxt->sys_open_write($filename,$size);
Opens (creates?) a system file for writing, returns a file descriptor. You must specify the size of the file you wish to write to.
$ret = $nxt->sys_read($fd,$nbytes);
Reads $nbytes from open file descriptor $fd. I believe $nbytes should be < 60 when using USB.
$ret = $nxt->sys_write($fd,$data);
Writes $data to open file descriptor $fd.
$ret = $nxt->sys_close($fd);
Closes the file descriptor $fd.
$ret = $nxt->sys_delete($filename);
Deletes a file on the NXT with name $filename.
$ret = $nxt->sys_find_first($search);
Finds a file on the system with filename $search. Searches may use wildcards: *.*, *.txt, etc. This command will return statstr=>"File not found" on failure. The filehandle MUST be closed when finished with the query.
$ret = $nxt->sys_find_next($fd);
Finds the next matching file on the system from sys_find_first. $fd is the descriptor returned from sys_find_first
$ret = $nxt->sys_get_firmware_version();
Does what it says.
$ret = $nxt->sys_open_write_linear($filename,$size);
Opens a system file for writing (raw data mode? update me... ), returns a file descriptor. You must specify the size of the file you wish to write to.
$ret = $nxt->sys_open_read_linear($filename);
Opens a system file for reading, returns the memory address of the file (NOT FD).
$ret = $nxt->sys_open_write_data($filename,$size);
Opens a system file for writing (data mode? update me... ), returns a file descriptor. You must specify the size of the file you wish to write to.
$ret = $nxt->sys_open_append_data($filename);
Opens a system file for appendinf (data mode?), returns the fd and available size.
$ret = $nxt->sys_boot()
Boot the NXT. DO NOT USE UNLESS YOU **REALLY** KNOW WHAT YOU'RE DOING!!! YOU CAN BREAK YOUR NXT!!! YOU HAVE BEEN WARNED.
NOTE: USB Interface ONLY!
$ret = $nxt->sys_set_brick_name($filename);
Does what it says. I believe this defaults to NXT and is used in Bluetooth identification.
$ret = $nxt->sys_get_device_info();
Returns various informations about the NXT including Bluetooth info and available flash.
$ret = $nxt->sys_poll_command_length($buffer_number);
Polls for the number of bytes ready in the command buffer. $buffer_number = 0x00 for poll buffer $buffer_number = 0xA1 for the high speed buffer * I have no idea what this does as of this writing.
$ret = $nxt->sys_poll_command($buffer_number,$command_length);
Polls the command buffer. $buffer_number = 0x00 for poll buffer $buffer_number = 0xA1 for the high speed buffer * I have no idea what this does as of this writing.
$ret = $nxt->sys_bluetooth_factory_reset($filename);
Resets the on-board bluetooth chip to factory defaults.
This is a custom import method for supporting plugins. See LEGO::NXT::Manual::Plugins.
Michael Collins <email@example.com>
Aran Deltac <firstname.lastname@example.org>
You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
The LEGO::NXT module is Copyright (c) 2006 Michael Collins. USA. All rights reserved.
See Additional Resources at http://nxt.ivorycity.com
LEGO::NXT is free open source software. IT COMES WITHOUT WARRANTY OF ANY KIND.