The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME NXT

A Perl module for the LEGO NXT Direct Commands interface.

SYNOPSIS

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);

DESCRIPTION

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.

IMPORTANT CONSTANTS

RET & NORET

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.

IO PORT CONSTANTS

$NXT::SENSOR1 $NXT::SENSOR2 $NXT::SENSOR3 $NXT::SENSOR4

$NXT::MOTOR_A $NXT::MOTOR_B $NXT::MOTOR_C $NXT::MOTOR_ALL

MOTOR CONTROL CONSTANTS

  $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
  

SENSOR TYPE CONSTANTS

  $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

SENSOR MODE CONSTANTS

  $NXT::RAWMODE   
  $NXT::BOOLEANMODE
  $NXT::TRANSITIONCNTMODE
  
  $NXT::PERIODCOUNTERMODE
  $NXT::PCTFULLSCALEMODE
  $NXT::CELSIUSMODE 
  
  $NXT::FAHRENHEITMODE
  $NXT::ANGLESTEPSMODE
  $NXT::SLOPEMASK 

  $NXT::MODEMASK    
  

METHODS

new

$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.

set_ultrasound_event_capture_mode

$conn->set_ultrasound_event_capture_mode($NXT::SENSOR_4);

In this mode the US sensor will detect only other ultrasound sensors in the vicinity.

play_tone

$conn->play_tone($NXT::NORET,$pitch,$duration)

Play a Tone in $pitch HZ for $duration miliseconds

play_sound_file

$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.

set_output_state

$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.

set_input_mode

$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.

get_output_state

$ret = $conn->get_output_state($NXT::RET,$port)

Retrieve the current ouput state of $port.

$ret is a hashref containing the port attributes.

get_input_values

$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.

reset_input_scaled_value

$conn->reset_input_scaled_value($NXT::NORET,$port)

If your sensor port is using scaled values, reset them.

message_write

$conn->message_write($NXT::NORET,$mailbox,$message)

Write a $message to local mailbox# $mailbox.

reset_motor_position

$conn->reset_motor_position($NXT::NORET,$port,$relative)

TODO: Specifics

get_battery_level

$ret = $conn->get_battery_level($NXT::RET)

$ret is a hash containing battery attributes - voltage in MV

set_stop_sound_playback

$conn->set_stop_sound_playback($NXT::NORET)

Stops the currently playing sound file

keep_alive

$conn->keep_alive($NXT::NORET)

Prevents the NXT from entering sleep mode

ls_get_status

$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

ls_write

$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

ls_read

$conn->ls_read($NXT::RET,$port)

Read a pending I2C message from a digital I2C device.

ls_request_response

$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.

get_current_program_name

$ret = $conn->get_current_program_name($NXT::RET)

$ret is a hash containing info on the current;y running program.

message_read

$ret = $conn->message_read($NXT::RET,$remotebox,$localbox,$remove)

Read a message.

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 631:

'=item' outside of any '=over'

Around line 653:

You forgot a '=back' before '=head2'