ruby-nxt 0.8.0 4
We've made quite a lot of progress on ruby-nxt. The new version is a pretty complete implementation of the NXT direct command set. Almost everything is pretty well documented now, too. One of the more interesting things I've been working on is a high level api based on the "blocks" in NXT-G. So if you're familiar with the way NXT-G works, you should be able to pick it up pretty easily with code such as:
t = Commands::TouchSensor.new(@nxt)
t.port = 1
t.action = :pressed
while t.logic == false
puts "Hold down the button..."
sleep(0.5)
endNow that it's pretty complete and usable, I think I'll finally get around to making a Ruby on Rails plugin, which was the original reason I started all this! :)
ruby-serialport/nxt on Windows
My MacBookPro has been acting up (randomly deciding not to boot) So I sent it back to Apple for repairs. In the meantime, I've been stuck in Windows which has kept me from working on ruby-nxt because the ruby-serialport module requires compilation. This is not an easy thing to accomplish in Windows. I tried in vain to get it to compile using the free MSVC++ Express version. Apparently MSVC++ version 6 is required. However, I did manage to get it working using Cygwin.
Cygwin is a Linux-like environment for Windows. It's a little confusing to install and just plain feels weird, but it gets the job done. Basically when you install Cygwin, navigate down the tree of packages and install the following under Devel: gcc, make, and ruby. Add c:\cygwin\bin to your path. The ruby that comes with cygwin is slightly borked. It sets RUBYOPT=-rubygems for some reason. So make sure you "SET RUBYOPT=" to clear that out. Then download ruby-serialport and install it normally. (ruby extconf.rb, make, make install)
Now, when you pair your NXT via bluetooth, it should have created a COM4 Outgoing port on NXT 'Dev B'. Cygwin emulates the windows com ports to virtual /dev devices. COM1 = /dev/ttyS0. So in my case, the NXT is on COM4 therefore in ruby-nxt you want to connect to /dev/ttyS3.
During this whole fiasco I came this close to wiping this computer and installing Linux on it. Man I miss my MacBook...
ruby-nxt 6
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
endOnce I get it a little more polished, I plan on also creating a Ruby On Rails plugin. Forget Microsoft Robotics Studio. ;)
ruby-nxt Progress
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
endRuby NXT Bluetooth Data Logger
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
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:
Pair the NXT with OSX.
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.
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.
On the NXT, goto Bluetooth, My Contacts, Select your computer, Connect to whatever connection number you want.
Run a program on the NXT that sends a message to whatever connection number you chose.
Woot! The message should show up in terminal.
Juju