Ruby 1.8.6 and Oniguruma on OSX
I'm working on a project that requires some complicated regexes and for the first time actually needed to do a look-behind. Unfortunately, ruby 1.8.6 doesn't support it. You can accomplish it using Oniguruma and Ruby 1.9 will come with it. There's a gem but then you have to specficially reference it and might require code changes when 1.9 comes out(?), not to mention the fact that it's a pain in the ass to install on osx. So I set out to figure out how to recompile ruby with it patched in. The only info I could find in english was for ruby 1.8.5. Finally figured it out, here's how (also works on linux, not just osx):
curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6.tar.gz
tar xzvf ruby-1.8.6.tar.gz
curl -O http://www.geocities.jp/kosako3/oniguruma/archive/onigd2_5_9.tar.gz
tar xzvf onigd2_5_9.tar.gz
cd oniguruma
./configure --with-rubydir=../ruby-1.8.6
make 186
cd ../ruby-1.8.6
./configure --prefix=/usr/local --enable-pthread --with-readline-dir=/usr/local --enable-shared
make
sudo make install
ruby -e "puts Regexp::ENGINE" # should return OnigurumaRuby on Rails OSX Console Aliases 2
I found that every day when I goto work in the morning I do the same things whenever I go to work on a rails project:
- open firefox
- open iTerm
- cd ~/Code/railsprojectx
- mate .
- script/server
- open another tab in iTerm
- svn update
- goto http://localhost:3000
Sure it only takes a few seconds, but it wasn't very DRY. :) I figured I could create a shell alias to basically reduce all that to a single command. I also thought I'd finally start giving mongrel a try. So this is what I came up with for an rdev command (along with a bunch of my other rails related aliases):
alias rdev='svn update;mate .;mongrel_rails start -d;sleep 2;open http://localhost:3000;tail -f log/development.log'
alias ss='script/server'
alias sc='script/console'
alias sg='script/generate'
alias sp='script/plugin'
alias sr='script/runner'
alias rt='rake test'
alias rtu='rake test:units'
alias rtr='rake test:recent'
alias mr='mongrel_rails start -d'
alias mrs='mongrel_rails stop'Just stick that in your ~/.profile file and either reopen your console or run:
. ~/.profileruby-nxt 0.8.1 - Finally available as a Gem 4
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):2For 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 OSX 3
Some people have been confused as to how to use ruby-nxt in OSX. Some of the confusion was that I had posted instructions which only allowed connection FROM the nxt to the mac where the mac is the slave. To use ruby-nxt you need to initiate a connection from the mac to the NXT where the mac is the master. Here's some "simplified" step by step instructions:
- Turn on the NXT and make sure bluetooth is turned on. (you should see a bluetooth icon at the top left corner)
- Click the bluetooth icon in the menubar, select "Setup bluetooth device".
- When it asks for Select Device Type, choose "Any device".
- Select the NXT from the list, click continue.
- The nxt will beep and ask for a passkey, choose 1234, press orange button.
- click continue in osx, enter 1234.
- The NXT will beep again, press orange button to use 1234 again.
- The mac will complain "There were no supported services found on your device". Don't worry about that and click continue and then click Quit.
- In OSX click the bluetooth icon, select "Open bluetooth preferences", you should see the NXT listed, select it, then click "Edit Serial Ports".
- It should show NXT-DevB-1, if not click add, use Port Name: NXT-DevB-1, Device Service: Dev B, Port type: RS-232. Click Apply.
- You're done! You should now have a /dev/tty.NXT-DevB-1
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! :)
Vacation and ruby-nxt
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-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!
Older posts: 1 2
Juju