A Perl module for the LEGO NXT Direct Commands interface.
use LEGO::NXT; use Data::Dumper;
# Create a new Bluetooth/NXT object using btaddr=<xx:xx:xx:xx:xx:xx> and channel=1 my $conn = new NXT('xx:xx:xx:xx:xx:xx',1);
#issue the command to play '! Attention.rso'. #0 means do not repeat. #$NXT::NORET means do not require the NXT to issue a return value $conn->play_sound_file($NXT::NORET, 0,'! Attention.rso');
#issue the command to retrieve the battery level. $res = $conn->get_battery_level($NXT::Ret); print Dumper($res);
#Turn on Motor 1 to full power $res = $conn->set_output_state($NXT::RET, 0x01, 100, $NXT::MOTORON|$NXT::Regulated, $NXT::REGULATION_MODE_MOTOR_SPEED, 0, $NXT::MOTOR_RUN_STATE_RUNNING, 0 ); print Dumper($res);
NXT.pm enables users to control the 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.
For each request of the NXT, you must specify whether you want the NXT to send a return value.
The constants...
$NXT::RET or $NXT::NORET
...must be passed as the first argument each Direct Commands call.
You may want to avoid requesting a response on every direct command as this can slow things down considerably.
$NXT::SENSOR1 $NXT::SENSOR2 $NXT::SENSOR3 $NXT::SENSOR4
$NXT::MOTOR_A $NXT::MOTOR_B $NXT::MOTOR_C $NXT::MOTOR_ALL
$NXT::MOTORON $NXT::BRAKE $NXT::REGULATED Output regulation modes: $NXT::REGULATION_MODE_IDLE $NXT::REGULATION_MODE_MOTOR_SPEED $NXT::REGULATION_MODE_MOTOR_SYNC Output run states: $NXT::MOTOR_RUN_STATE_IDLE $NXT::MOTOR_RUN_STATE_RAMPUP $NXT::MOTOR_RUN_STATE_RUNNING $NXT::MOTOR_RUN_STATE_RAMPDOWN
$NXT::NO_SENSOR $NXT::SWITCH $NXT::TEMPERATURE $NXT::REFLECTION $NXT::ANGLE $NXT::LIGHT_ACTIVE $NXT::LIGHT_INACTIVE $NXT::SOUND_DB $NXT::SOUND_DBA $NXT::CUSTOM $NXT::LOWSPEED $NXT::LOWSPEED_9V $NXT::NO_OF_SENSOR_TYPES
$NXT::RAWMODE $NXT::BOOLEANMODE $NXT::TRANSITIONCNTMODE $NXT::PERIODCOUNTERMODE $NXT::PCTFULLSCALEMODE $NXT::CELSIUSMODE $NXT::FAHRENHEITMODE $NXT::ANGLESTEPSMODE $NXT::SLOPEMASK $NXT::MODEMASK
$conn = new NXT('xx:xx:xx:xx:xx:xx',1);
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.
$conn->set_ultrasound_event_capture_mode($NXT::SENSOR_4);
In this mode the US sensor will detect only other ultrasound sensors in the vicinity.
$conn->play_tone($NXT::NORET,$pitch,$duration)
Play a Tone in $pitch HZ for $duration miliseconds
$conn->play_sound_file($NXT::NORET,$repeat,$file)
Play a NXT sound file called $file. Specify $repeat=1 for infinite repeat, 0 to play only once.
$conn->set_output_state($NXT::NORET,$port,$power,$mode,$regulation,$turnratio,$runstate,$tacholimit)
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.
$conn->set_input_mode($NXT::NORET,$port,$sensor_type,$sensor_mode)
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 = $conn->get_output_state($NXT::RET,$port)
Retrieve the current ouput state of $port.
$ret is a hashref containing the port attributes.
$ret = $conn->get_input_values($NXT::RET,$port)
Retrieve the current sensor input values of $port.
$ret is a hashref containing the sensor value attributes.
$conn->reset_input_scaled_value($NXT::NORET,$port)
If your sensor port is using scaled values, reset them.
$conn->message_write($NXT::NORET,$mailbox,$message)
Write a $message to local mailbox# $mailbox.
$conn->reset_motor_position($NXT::NORET,$port,$relative)
TODO: Specifics
$ret = $conn->get_battery_level($NXT::RET)
$ret is a hash containing battery attributes - voltage in MV
$conn->set_stop_sound_playback($NXT::NORET)
Stops the currently playing sound file
$conn->keep_alive($NXT::NORET)
Prevents the NXT from entering sleep mode
$conn->ls_get_status($NXT::RET,$port)
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
$conn->ls_write($NXT::RET,$port,$txlen,$rxlen,$txdata)
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
$conn->ls_read($NXT::RET,$port)
Read a pending I2C message from a digital I2C device.
$conn->ls_request_response($port,$txlen,$rxlen,$txdata)
Higher level I2C request-response routine. Loops to ensure data is ready to read from the sensor and returns the result.
$ret = $conn->get_current_program_name($NXT::RET)
$ret is a hash containing info on the current;y running program.
$ret = $conn->message_read($NXT::RET,$remotebox,$localbox,$remove)
Read a message.
2 POD Errors
The following errors were encountered while parsing the POD:
'=item' outside of any '=over'
You forgot a '=back' before '=head2'
To install NXT, copy and paste the appropriate command in to your terminal.
cpanm
cpanm NXT
CPAN shell
perl -MCPAN -e shell install NXT
For more information on module installation, please visit the detailed CPAN module installation guide.