ruby-nxt in MacTech Magazine

Posted by Tony Buser Fri, 13 Jul 2007 20:11:00 GMT

Check this out, MacTech did an article on using the LEGO MINDSTORMS NXT on a mac and talked about ruby-nxt. Even linked to my website. Unfortunately, they spelled my name wrong! :)

ruby-nxt 0.8.1 - Finally available as a Gem 4

Posted by Tony Buser Sat, 04 Nov 2006 04:05:00 GMT

I've been pretty busy lately so haven't had much time to work on ruby-nxt. However, tonight I had some time to finally get it packaged into a gem! The main reason it took so long is because of some kind of weird bug with requiring ruby-serialport and rubygems resulting in the following error:

NameError: (eval):1:in `private_class_method': undefined method `create' for class `Class'
        from (eval):1
        from (eval):1
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
        from (irb):2

For some reason, it doesn't like rubygem's custom require code. So I got around it by doing a Kernel::require "serialport".

Unfortunately, there's really no way to include ruby-serialport in the ruby gem, so you'll still have to download and install that seperately. Once serialport is installed, all you should have to do is sudo gem install ruby-nxt. Then in your code require "rubygems" then require "nxt_comm" or require "nxt" depending on how you want to use it.

The 0.8.1 release doesn't have anything new in terms of features, however you might want to take a look at examples/drb_server.rb ;)

Bluetooth Serial Port To NXT in Linux

Posted by Tony Buser Sun, 22 Oct 2006 17:23:00 GMT

I finally got around to playing with the NXT in linux with ruby-nxt. Since ruby-nxt currently only supports a serial port connection (hopefully a native bluetooth module will be ready soon), you have to use rfcomm to setup a serial device. Here's how you do it (using ubuntu 6.06 and a linksys usb adapter)...

First you have to find the mac address of your NXT with the following command:

abuser@wraith:/etc/bluetooth$ hcitool scan
Scanning ...
        00:16:53:04:B3:46       NXT

Then sudo vim /etc/bluetooth/rfcomm.conf and add an entry like this:

rfcomm0 {
        bind yes;
        # Bluetooth address of the device
        device 00:16:53:04:B3:46;
        # RFCOMM channel for the connection
        channel 1;
        # Description of the connection
        comment "NXT";
}

Then restart bluetooth with a sudo /etc/init.d/bluez-utils restart.

Then to verify it's setup, run rfcomm and you should see output like this:

abuser@wraith:/etc/bluetooth$ rfcomm
rfcomm0: 00:16:53:04:B3:46 channel 1 clean

You should now have a /dev/rfcomm0 that you can use with ruby-nxt. The first time you run a ruby-nxt program, it might pop up a message for the PIN, just enter 1234.

If you want to setup a connection FROM the NXT to your computer, (where your computer is a slave to the NXT where you can send BT messages from a program running on the NXT to your computer) follow these instructions.

Similar instructions for windows here and osx here.

Vacation and ruby-nxt

Posted by Tony Buser Sat, 09 Sep 2006 23:54:00 GMT

I haven't had much time to work on ruby-nxt because I was away on vacation (cruise to alaska). While I was away, Matt Zukowski has expanded it adding some more higher level functions and setup a rubyforge project. I will be moving development there instead of my local subversion repository, so make sure you switch to it if you want the latest version. Now that I'm back, I plan on spending a lot more time on it.

ruby-nxt 6

Posted by Tony Buser Mon, 07 Aug 2006 03:11:00 GMT

I made a lot of progress this weekend on ruby-nxt. I got most of the Direct Commands completed. Update: ruby-nxt can now be found at rubyforge.

It makes programming the NXT as simple as:

require 'nxt.rb'

NXT.exec("/dev/tty.NXT-B") do |cmd|

  puts "Battery Level: #{cmd.GetBatteryLevel[0]/1000.0} V"

  cmd.PlaySoundFile(true,"Good Job.rso")
  sleep(3)
  cmd.StopSoundPlayback

end

Once I get it a little more polished, I plan on also creating a Ruby On Rails plugin. Forget Microsoft Robotics Studio. ;)

ruby-nxt Progress

Posted by Tony Buser Fri, 04 Aug 2006 04:57:00 GMT

Following up on my ruby nxt datalogger from yesterday, I've got two way communication working now. Doesn't do much yet, just sends a play tone command to the nxt, sends a bluetooth message, or read a mailbox. It's a start.

Unlike the datalogger, this requires creating a serial port for the NXT device using Dev B service. (in OSX that's under Bluetooth Preferences -> Devices -> NXT -> Edit Serial Ports. Device Service: Dev B, Port type: RS-232, require pairing for security)

Also, if you want to send messages to a program running on the NXT, make sure you run this script first before starting your program on the NXT.

One problem I can't figure out is I created an NXT-G program and made it send a bluetooth message on connection 0 but I get no output on the serial port... however, if I go into the NXT and tell it to connect to my computer using my datalogger I get output there. The NXT shows my computer as being connected to both connection 0 and 1, which I thought wasn't possible. The documentation says the NXT can only be a Master or a Slave, not both. So who knows.

require "serialport"

@tty = SerialPort.new("/dev/tty.NXT-B", 57600, 8, 1, SerialPort::NONE)
@tty.flow_control = SerialPort::HARD

puts "bluetooth SPP connected"

if fork

  puts "input thread started"

  while (res = @tty.getc)
    puts "Response: %02x\t%s" % [res,res]
  end

else

  puts "output thread started"

  # play tone
  tone_cmd = [0x05,0x00,0x00,0x03,0xff,0x00,0x10,0x00,0x00]

  # write True to mailbox 0
  write_cmd = [0x06,0x00,0x00,0x09,0x00,0x02,0x01,0x00]

  # read mailbox 0 from slave?
  read_cmd = [0x05,0x00,0x00,0x13,0x0A,0x00,0x00]

  while true
    while (key = STDIN.gets.chomp) do
      if key == "w"
        puts "MessageWrite"
        write_cmd.each do |b|
          @tty.putc b
        end
      end
      if key == "r"
        puts "MessageRead"
        read_cmd.each do |b|
          @tty.putc b
        end
      end
      if key == "t"
        puts "PlayTone"
        tone_cmd.each do |b|
          @tty.putc b
        end
      end
    end
  end

end

Ruby NXT Bluetooth Data Logger

Posted by Tony Buser Thu, 03 Aug 2006 05:08:00 GMT

I just finished writing a simple ruby script that listens for messages from a LEGO Mindstorms NXT robot on a bluetooth serial port and print out the messages in a comma delimited format with a datestamp, mailbox the message was sent to, and the message itself.

I've only tested it on OSX so far, but it should work on linux and windows so long as you have ruby and the ruby-serialport module. It's a little rough at the moment and probably buggy. I couldn't find info on the incoming message bytecodes in the NXT Dev Kit. Found the info in "Appendix 2 - LEGO MINDSTORMS NXT Direct commands.pdf" pages 5 and 9. There doesn't seem to be anything in the message header to indicate what type of message it is (Text, Number, or Logic), so I had to kind of fudge it. If the info is in the docs, please let me know. I put some comments in the code describing the different bytecodes I was able to figure out.

You can always download the latest version of nxtlogger.rb using this link.

Update 2006-08-05: Made the code much cleaner. I've never worked with low level binary data before. :)

Instructions for connecting NXT to OSX via bluetooth.

One step closer to chunky robotic bacon!

Mindstorms NXT Bluetooth on OSX 7

Posted by Tony Buser Wed, 02 Aug 2006 00:07:00 GMT

nxt_bluetooth_osx_helloworld

Lego released the Mindstorms NXT Developer Kit today! I was bummed when I found out that the software that comes with it for OSX is PPC. Apps running under Rosetta are unable to access bluetooth so I couldn't play with in on my Macbook Pro. Now that the dev kit is available I plan on writing a ruby module to communicate with the NXT via bluetooth. First I had to figure out how to access it via a bluetooth serial port.

When I first got my NXT and found bluetooth no worky in NXT-G, I tried creating a serial port and connecting to it. It would connect, and show up as being on connection 0 on the NXT and no matter what I did, I couldn't get a message to show up in OSX. Connection 0 means the NXT thought it was a slave. It should still be able to send messages to it's master, yet I get nothing. Connections initiated on the NXT to the computer always came up Line is Busy.

I finally made a breakthrough today. I was doing some research and found that you can create an incoming bluetooth serial port and then connect to my computer from the NXT. Here's the steps:

  1. Pair the NXT with OSX.

  2. Go into the Bluetooth Preferences, Sharing tab and Add Serial Port Service. Give it a name (I made it NXT-Master). Set Type to RS-232. This will create a /dev/tty.NXT-Master device.

  3. Open the device in OSX (this is important, otherwise you'll get a Line Is Busy message on the NXT) Just doing a cat /dev/tty.NXT-Master in a terminal will do it.

  4. On the NXT, goto Bluetooth, My Contacts, Select your computer, Connect to whatever connection number you want.

  5. Run a program on the NXT that sends a message to whatever connection number you chose.

  6. Woot! The message should show up in terminal.