pbLua on Mac OS

April 2009

 

I’m always keen to learn new programming languages and keep myself “fresh” when it comes to programming concepts. I’ve been working for a while in RobotC and found it a powerful alternative to the standard LEGO NXT-G IDE. However while C as a programming environment has many benefits (raw speed and low-level bit manipulation are two immediate pluses) I really didn’t want to work around the constraints of the RobotC firmware support for arrays, structures and functions as arguments. And so I turned my attention to pbLua, a text-based language ported to the NXT by Ralph Hempel (of pbForth fame).


From a first reading Lua struck me as a strange blend of Perl, Scheme with a bit of C thrown in. It brought back some pleasant memories of perl programming (associative arrays known as “tables” in Lua) and also some of the incredible power and simplicity of scheme (functions as first-order variables with lexical scoping). When the base language you program in supports powerful concepts such as these you spend less time re-inventing data structures and more time solving problems.


Before I go through my experiences getting pbLua to work on a Mac I’d like to thank Ralph Hempel for his fast and extremely helpful replies to my emails. We have to respect that all too often the authors of these free development tools have busy “real” lives that take them away from their projects, and Ralph was very enthusiastic to help me get the problems I encountered solved. Good job Ralph!


Getting Started


My first step was to review Ralph’s tutorial on installation for pbLua on his website. I run Mac OS X Leopard (10.5.6) and also Microsoft Windows XP SP3 in a Parallels 3 desktop environment, so I could test pbLua on both Mac and Windows environments. I downloaded the latest version of pbLua, which as of this writing was beta 17f.


I re-flashed the firmware with pbLua beta 17f using the LEGO NXT-G software running on Windows in Parallels desktop 3. Once the NXT restarted the text “pbLua Beta 17f” was displayed on the NXT screen. Over the first hurdle, and on to the next...


pbLua installation in Parallels


Next, I disconnected and re-connected the USB cable from the NXT. As I was running Windows inside of Parallels desktop I had already connected the NXT brick as a USB device in Parallels. Windows popped up the “Found New Hardware” wizard. I copied the USB device driver files from the pbLua beta installation onto my Windows Desktop and told Windows to locate the driver there. No problems - pbLua Serial USB driver installed successfully.


Serial connection in Windows


Now to test if I could connect to the NXT. I chose to use the standard Windows XP HyperTerminal program to connect to the NXT. On my Windows installation running in Parallels desktop COM9 was assigned to the pbLua serial connection (you can find this by starting Device Manager in Windows and expanding the USB ports list to find what COM port is assigned to pbLuaUSBSerial). I started HyperTerminal and connected to the NXT on COM9 at 38400 baud. I was greeted with a “>” prompt indicating success! So far so good!


USB Serial problems on Mac OS


I could not make Eclipse connect to the NXT brick via USB, so I had to investigate if my NXT was connected via the USB and appearing as a serial device.


Debugging USB serial devices on a Mac turns out to be a common problem  - especially since the latest Mac OS 10.5.6 update has caused many problems as reported on the Apple support forums. I could see in System Profiler that the pbLuaUSBSerial device was appearing, which told me that the Mac could see the NXT connected via USB. The USB Profiler tool in Mac OS also showed the pbLuaUSBSerial device (of course, it is a special “virtual” USB port that actually speaks a serial protocol to the NXT, but let’s not worry about that).


Mac OS is based on BSD Unix. All devices are treated as if they were files that can be read and written to. In Unix a special directory named /dev holds these magic files that correspond to devices attached to the Mac. You can’t simply create a file in this directory and expect it to work. When a device is attached to the Mac a device entry can be created dynamically in /dev. Thus if I saw pbLuaUSBSerial as a USB device in the USB profiler, I would expect to see a /dev entry for this device. But why could I not find a device entry in /dev corresponding to the NXT?


The solution for pbLuaUSBSerial


After a lot of investigation I found a solution: I had to clear out the cache of kernel extensions loaded into the OS and then re-connect the NXT to cause it to re-load the pbLuaUSBSerial device and create a /dev entry. Success!


To re-cap, here is the procedure I followed to allow me to open a serial connection to the NXT running pbLua beta 17f from Mac OS 10.5.6:


  1. 1.Disconnect the NXT from the Mac

  2. 2.Open a Terminal application window

  3. 3.In Terminal type cd /System/Library/Extensions/Caches

  4. 4.Type sudo rm -f * (be very careful with this command - be sure you are in the correct directory!)

  5. 5.Reconnect NXT to the Mac

  6. 6.Turn on the NXT and select USB console on the menu

  7. 7.In Terminal type cd /dev

  8. 8.Then we need to find the name of the device for the pbLuaUSBSerial entry: type ls cu*

  9. 9.Locate the name of the usbmodem device returned. In my case it is usbmodem5d21 (this name may change each time you connect your NXT to the Mac, depending on which USB port you plug it into).

  10. 10.In Terminal type screen /dev/tty.usbmodem5d21 38400 (this starts a serial connection to the NXT at 38400 baud).

  11. 11.You will see the pbLua prompt - success!


Edit and download pbLua files to the NXT


I installed Eclipse 3.4.2 and the plugins as described on Ralph’s installation guide for an IDE. Ralph’s description refers to Eclipse 3.3 whereas I installed the latest version at 3.4. So some of the screens and menu options have changed, but the overall install procedure was the same.


However I could not get the Eclipse editor to load the Lua plugin - I kept getting an error about the “editor not in plugin.xml”. I found some helpful hints at http://n2.nabble.com/Eclipse-3.4.x-and-Lua--(on-OS-X)-td1570171.html but eventually gave up and downloaded Eclipse 3.3.2 for Mac OS.


In the end I settled on a far simpler editor-download solution: Smultron is a straightforward Mac OS text editor that supports syntax highlighting for many languages, Lua included. It does its job and gets out of the way, which is just what a text editor should be. I download the Lua source to the NXT using the Terminal, which is about as old-school as it gets!


In this example I assume that you store your Lua source code on the Desktop in a folder named “LEGO” and you have a source file named mysourcefile.lua to download. You can change the examples below if you store your Lua code elsewhere on your Mac.To download the Lua source to your NXT, open a terminal and enter:


  1. 1.cd /Users/<yourname>/Desktop/LEGO

  2. 2.ls /dev/cu* (as in the previous example you will see a device named usbmodemXXXX, which you should use in the next step)

  3. 3.Hit Cmd-N to open a new terminal window

  4. 4.In this new window open an interactive window onto your NXT: screen /dev/cu.usbmodemXXXX 38400

  5. 5.In the first terminal window type cat mysourcefile.lua > /dev/cu.usbmodemXXXX

  6. 6.You will see the Lua source downloaded onto the NXT in your interactive terminal window, and you can call Lua functions at the > prompt.

  7. 7.To quit your interactive session to the NXT type Ctrl-A followed by Ctrl-\

Last updated 10 April, 2009


All content © 2008, 2009 Mark Crosbie  mark@mastincrosbie.com


LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorize or endorse this site. This site, its owner and contents are in no way affiliated with or endorsed by the LEGO Group. For more please read the LEGO Fair Play policy.