Class NXT
In: lib/nxt.rb
Parent: Object

High-level interface for controlling motors and sensors connected to the NXT. Currently only motors and some other misc functionality is implemented.


  nxt ='/dev/tty.NXT-DevB-1')

  nxt.motor_a do |m|
    m.forward(:degrees => 180, :power => 15)

  nxt.motors_bc do |m|
    m.backward(:time => 5, :power => 20)

  nxt.motors_abc do |m|
    m.forward(:time => 3, :power => 10)
    puts "Motor #{} moved #{m.read_state[:degree_count]} degrees."


Be sure to call NXT#disconnect when done sending commands, otherwise there may be trouble if you try to connect or send commands again afterwards.


disconnect   method_missing   motor   motors   new   sensor  

Public Class methods

Initialize the NXT. This creates three Motor instances and one kind of each sensor. It is assumed that the sensors are connected to the standard ports as follows:

  • Port 1: Touch
  • Port 2: Sound
  • Port 3: Light
  • Port 4: Ultrasonic

You can specify the path to the serialport device (e.g. ’/dev/tty.NXT-DevB-1’) or omit the argument to use the serialport device specified in the global $DEV variable.

Public Instance methods

Waits for all running jobs to finish and cleanly closes all connections to the NXT device. You should always call this when done sending commands to the NXT.

Runs the given proc for the given motor. You should use the motor_x dynamic method instead of calling this directly. For example…

  nxt.motor_a {|m| m.forward(:degrees => 180)}

…would rotate motor A by 180 degrees, while…

  nxt.motor_b {|m| m.forward(:degrees => 180)}

…would do the same for motor B.

Runs the given proc for multiple motors. You should use the motors_xxx dynamic method instead of calling this directly. For example…

  nxt.motors_abc {|m| m.forward(:degrees => 180)}

…would run the given block simultanously on all three motors, while…

  nxt.motors_bc {|m| m.forward(:degrees => 180)}

…would only run it on motors B and C.