Class NXTComm
In: lib/nxt_comm.rb
Parent: Object

Description

Low-level interface for communicating directly with the NXT via a Bluetooth serial port. Implements direct commands outlined in Appendix 2-LEGO MINDSTORMS NXT Direct Commands.pdf

Not all functionality is implemented yet!

For instructions on creating a bluetooth serial port connection:

Examples

First create a new NXTComm object and pass the device.

  @nxt = NXTComm.new("/dev/tty.NXT-DevB-1")

Rotate the motor connected to port B forwards indefinitely at 100% power:

  @nxt.set_output_state(
    NXTComm::MOTOR_B,
    100,
    NXTComm::MOTORON,
    NXTComm::REGULATION_MODE_MOTOR_SPEED,
    100,
    NXTComm::MOTOR_RUN_STATE_RUNNING,
    0
  )

Play a tone at 1000 Hz for 500 ms:

  @nxt.play_tone(1000,500)

Print out the current battery level:

  puts "Battery Level: #{@nxt.get_battery_level/1000.0} V"

Methods

Constants

SENSOR_1 = 0x00   sensors
SENSOR_2 = 0x01
SENSOR_3 = 0x02
SENSOR_4 = 0x03
MOTOR_A = 0x00   motors
MOTOR_B = 0x01
MOTOR_C = 0x02
MOTOR_ALL = 0xFF
COAST = 0x00   output mode
MOTORON = 0x01
BRAKE = 0x02
REGULATED = 0x04
REGULATION_MODE_IDLE = 0x00   output regulation mode
REGULATION_MODE_MOTOR_SPEED = 0x01
REGULATION_MODE_MOTOR_SYNC = 0x02
MOTOR_RUN_STATE_IDLE = 0x00   output run state
MOTOR_RUN_STATE_RAMPUP = 0x10
MOTOR_RUN_STATE_RUNNING = 0x20
MOTOR_RUN_STATE_RAMPDOWN = 0x40
NO_SENSOR = 0x00   sensor type
SWITCH = 0x01
TEMPERATURE = 0x02
REFLECTION = 0x03
ANGLE = 0x04
LIGHT_ACTIVE = 0x05
LIGHT_INACTIVE = 0x06
SOUND_DB = 0x07
SOUND_DBA = 0x08
CUSTOM = 0x09
LOWSPEED = 0x0A
LOWSPEED_9V = 0x0B
NO_OF_SENSOR_TYPES = 0x0C
RAWMODE = 0x00   sensor mode
BOOLEANMODE = 0x20
TRANSITIONCNTMODE = 0x40
PERIODCOUNTERMODE = 0x60
PCTFULLSCALEMODE = 0x80
CELSIUSMODE = 0xA0
FAHRENHEITMODE = 0xC0
ANGLESTEPSMODE = 0xE0
SLOPEMASK = 0x1F
MODEMASK = 0xE0

Public Class methods

Create a new instance of NXTComm. Be careful not to create more than one NXTComm object per serial port dev. If two NXTComms try to talk to the same dev, there will be trouble.

Public Instance methods

Close the connection

Returns true if the connection to the NXT is open; false otherwise

Returns the battery voltage in millivolts.

Returns the name of the program currently running on the NXT. Returns an error If no program is running.

Get the current values from an input sensor port.

  • port - input port (SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4)

Returns a hash with the following info (enumerated values see: set_input_mode):

  {
    :port             => see: input ports,
    :valid            => boolean, true if new data value should be seen as valid data,
    :calibrated       => boolean, true if calibration file found and used for 'Calibrated Value' field below,
    :type             => see: sensor types,
    :mode             => see: sensor modes,
    :value_raw        => raw A/D value (device dependent),
    :value_normal     => normalized A/D value (0 - 1023),
    :value_scaled     => scaled value (mode dependent),
    :value_calibrated => calibrated value, scaled to calibration (CURRENTLY UNUSED)
  }

Get the state of the output motor port.

  • port - output port (MOTOR_A, MOTOR_B, MOTOR_C)

Returns a hash with the following info (enumerated values see: set_output_state):

  {
    :port               => see: output ports,
    :power              => -100 - 100,
    :mode               => see: output modes,
    :reg_mode           => see: regulation modes,
    :turn_ratio         => -100 - 100 negative shifts power to left motor, positive to right, 50 = one stops, other moves, 100 = each motor moves in opposite directions,
    :run_state          => see: run states,
    :tacho_limit        => current limit on a movement in progress, if any,
    :tacho_count        => internal count, number of counts since last reset of the motor counter,
    :block_tacho_count  => current position relative to last programmed movement,
    :rotation_count     => current position relative to last reset of the rotation sensor for this motor
  }

Keep the connection alive and prevents NXT from going to sleep until sleep time. Also, returns the current sleep time limit in ms

Get the status of an LS port (like ultrasonic sensor). Returns the count of available bytes to read.

  • port - input port (SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4)

Read data from from lowspeed I2C port (for receiving data from the ultrasonic sensor)

  • port - input port (SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4)

Returns a hash containing:

  {
    :bytes_read => number of bytes read
    :data       => Rx data (padded)
  }

  For LS communication on the NXT, data lengths are limited to 16 bytes per command.
  Furthermore, this protocol does not support variable-length return packages, so the response
  will always contain 16 data bytes, with invalid data bytes padded with zeroes.

Write data to lowspeed I2C port (for talking to the ultrasonic sensor)

  • port - input port (SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4)
  • i2c_msg - the I2C message to send to the lowspeed controller; the first byte specifies the transmitted data length, the second byte specifies the expected respone data length, and the remaining 16 bytes are the transmitted data. See UltrasonicComm for an example of an I2C sensor protocol implementation.

    For LS communication on the NXT, data lengths are limited to 16 bytes per command. Rx data length MUST be specified in the write command since reading from the device is done on a master-slave basis

Read a message from a specific inbox on the NXT.

  • inbox_remote - remote inbox number (1 - 10)
  • inbox_local - local inbox number (1 - 10) (not sure why you need this?)
  • remove - boolean, true - clears message from remote inbox

Write a message to a specific inbox on the NXT. This is used to send a message to a currently running program.

  • inbox - inbox number (1 - 10)
  • message - message data

Play a sound file stored on the NXT.

  • name - file name of the sound file to play
  • repeat - Loop? (true or false)

Play a tone.

  • freq - frequency for the tone in Hz
  • dur - duration for the tone in ms

Process the reply

Reset the scaled value on an input sensor port.

  • port - input port (SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4)

Reset the position of an output motor port.

  • port - output port (MOTOR_A, MOTOR_B, MOTOR_C)
  • relative - boolean, true - position relative to last movement, false - absolute position

Send message and return response

Send direct command bytes

Set various parameters for an input sensor port.

  • port - input port (SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4)
  • type - sensor type (NO_SENSOR, SWITCH, TEMPERATURE, REFLECTION, ANGLE, LIGHT_ACTIVE, LIGHT_INACTIVE, SOUND_DB, SOUND_DBA, CUSTOM, LOWSPEED, LOWSPEED_9V, NO_OF_SENSOR_TYPES)
  • mode - sensor mode (RAWMODE, BOOLEANMODE, TRANSITIONCNTMODE, PERIODCOUNTERMODE, PCTFULLSCALEMODE, CELSIUSMODE, FAHRENHEITMODE, ANGLESTEPMODE, SLOPEMASK, MODEMASK)

Set various parameters for the output motor port(s).

  • port - output port (MOTOR_A, MOTOR_B, MOTOR_C, or MOTOR_ALL)
  • power - power set point (-100 - 100)
  • mode - output mode (MOTORON, BRAKE, REGULATED)
  • reg_mode - regulation mode (REGULATION_MODE_IDLE, REGULATION_MODE_MOTOR_SPEED, REGULATION_MODE_MOTOR_SYNC)
  • turn_ratio - turn ratio (-100 - 100) negative shifts power to left motor, positive to right, 50 = one stops, other moves, 100 = each motor moves in opposite directions
  • run_state - run state (MOTOR_RUN_STATE_IDLE, MOTOR_RUN_STATE_RAMPUP, MOTOR_RUN_STATE_RUNNING, MOTOR_RUN_STATE_RAMPDOWN)
  • tacho_limit - tacho limit (number, 0 - run forever)

Start a program stored on the NXT.

  • name - file name of the program

Stop any programs currently running on the NXT.

Stop any currently playing sounds.

[Validate]