XPM / XPiXMap and pbLua / NXT 3

Posted by Tom Fri, 06 Jun 2008 23:05:00 GMT

Well, while I was at it tonight, I found that the text-based interface of pbLua isn’t very user friendly in comparison to the default LEGO NXT interface with it’s nice icons. Not that we need icons of course, but they’re cool. ;)

So I wrote a Lua class which can open XPM images, which are stored on the FileSystem and show them anywhere on the NXT-screen.

XPM images are also textbased, very ideal for our purpose, albeit they don’t have a very small footprint (the below image is 160 bytes, so it’s also not TOO big):
! XPM2
16 7 2 1
* c #ffffff
. c #000000
**..*...........
*.*.*...........
**..*..**.**..**
*.*.*.*.*.*..*.*
**..*..**.*...**
...............*
.............**.

Luckily MacVim understands this format and allows you to see it:

Right now this image is on my NXT display – inverse though, next is making the parsing a bit more flexible.

A good thing is that PixelMator can convert to XPM, so no manual editing of XPM files ;)

Now to see whether we can get the below image to show on the NXT screen (nope no colors):

Getting a Lua program onto the NXT 1

Posted by Tom Fri, 06 Jun 2008 18:26:06 GMT

In my last post, only yesterday, we’ve seen how to install pbLua onto the LEGO NXT. Now we also want it to do something ofcourse.

Let’s first start with connecting to the pbLua-NXT again (using screen – or a utility if you like) and at the > prompt copy and paste the whole following code:

-- Replace HelloWorld on the next line, with the name of the program you want
fileName = "HelloWorld" 

-- Replace the text between [[ and ]] with your own program.
programText = [[
nxt.DisplayClear()
nxt.DisplayText( "Hello, World!" )
]]

h = nxt.FileCreate( fileName, string.len(programText) )
nxt.FileWrite( h, programText )
nxt.FileClose( h )

This now created a file called HelloWorld onto your NXT. To now also run the program give the following command: nxt.dofile('HelloWorld')

That’s all there is to it, isn’t it easy?

Remember that this procedure is not needed if you just want try the API, remember (if you checked yesterday’s article) that we did nxt.SoundTone() on the console.

Lua on LEGO Mindstorm NXT 2

Posted by Tom Thu, 05 Jun 2008 22:39:00 GMT

Wow! It’s been a long time since I wrote here, but in the mean time cool things have happened. Firstly there’s pbLua, secondly I bought myself a Lua book.

So, how now do you go about to install pbLua on your NXT? If you’re using Windows, there’s not really an issue, because that’s all explained on the homepage. I’m using a Mac, so here it is.

Setup

1) First download pbLua, I’ve got a ‘nxt-lua-beta-15b’ folder on my Desktop.

2) Start up LEGO’s MINDSTORMS NXT

3) Use the Tools menu and use the “Update NXT Firmware…” option, click “Browse” and navigate using the browser to the folder ‘nxt-lua-beta-15b’ on your Desktop. The window should show “nxt-lua” as “Available Firmware Files”.

4) Now connect your NXT, click “Download” and wait ….

5) If everything is correct, your NXT screen should show “pbLua Beta 15b”, the 15b depends on the version you downloaded.

Okay now what?

Now you’ve got a Lua-enabled NXT, but it’s not very clear how to continue further. Luckily it says somewhere on the homepage that there’s a USB Console as well as a BT (BlueTooth) Console. Let’s start with the USB Console.

1) Fire up Terminal.app

2) Do an ls /dev/tty.usb*, in my case this shows a /dev/tty.usbmodem1a21, but your results may vary.

3) Then do a screen /dev/tty.usbmodem1a21, this should refresh your screen and show you:
pbLua Beta 15b
> 

You’re now in a Lua interpreter, type print("Hello World!"), just for the kicks of it!

Basically you can now use the pbLua API, to do anything you want, try nxt.SoundTone() for example, woohoo isn’t that great?!

4) To quit the Console type Ctrl-a Ctrl-\.

Enabling Bluetooth

1) Basically you go about as you would connect any other Bluetooth device to your Mac, just select “Any Device”, find the NXT in the list of Devices and have the “Use a specific passkey” option selected for the Passkey Options.

2) During “Gathering information about your device” and while it’s connecting, Mac OSX will ask you for a passkey, think of something, then go to your USB console and use the Lua function nxt.BtSetPIN('something') (on your USB console) to confirm the bluetooth-passkey from your NXT end. At some point Mac OSX will say “Connected”. Remember the device name!

Bluetooth Console

1) Reboot your NXT (by holding the grey button for some time) and start it again by clicking the orange button. Then you should see something like:

USB Console
BT Console
. 
. 
. 
. 
. 
. 
00000 Button:0

Then use the dark grey button, to highlight BT Console, next use the arrow buttons to find the device name of your computer (which in my case is ‘galactica’).

USB Console
BT Console
. 
.
galactica
AD:000000000000
CL:000000
ST:00 Idx: 00 
00000 Button:0

Once you’ve got this selected, the screen should show:

Wait for connect
Active: 00
Update:  0 
.
.
.
.
.
.

2) Fire up Terminal.app

3) Do an ls /dev/cu.<bluetooth-device-name>*, in my case my NXT is called “r2d2” (yup!). So I did a ls /dev/cu.r2d2*, this shows a /dev/cu.r2d2-DevB-1, but your results may vary, depending on your device name.

3) Then do a screen /dev/cu.r2d2-DevB-1 (or whatever you found at step 2), this should refresh your screen and show you:
pbLua Beta 15b
> 

Woohoo, wireless pbLua!!

More to follow, later, keep you posted – check back!

Ruby & Mindstorms NXT 21955

Posted by Tom Sat, 14 Oct 2006 09:29:00 GMT

Using Ruby with your Mindstorms NXT is easy using RubyNXT. But it still requires a bluetooth connection or USB connection.

How cool would it be if it was possible to write a Ruby program, upload it (in sourcecode) to your NXT brick and have a Ruby interpreter firmware running on the NXT? It wouldn’t be needed to port the whole Ruby Standard Library, it wouldn’t even be needed to convert the whole Ruby Core Library.

So what would be needed to get this to work? Cross-compilation of Ruby to ARM, someone already ported Ruby to his Zaurus. A new firmare is needed.

Maybe better yet is the use of a Ruby VM like YARV and get that to run on the Mindstorms brick. Still, that needs to be cross compiled and a new firmware is needed.

Ruby is a fully object oriented programming language and is fairly easy to learn, even easier to use. More and more programmers learn Ruby.

NXT Linux 4346

Posted by Tom Thu, 12 Oct 2006 09:15:00 GMT

Just a short article on thoughts for a Linux for Mindstorms NXT. How hard would it be to do something similar as iPod Linux with the NXT brick, ie a LEGO Mindstorms NXT running μCLinux? The Mindstorms NXT is based on the Atmel AT91SAM7S256, it’s a microcontroller. Jurgen Stuber already hints at programming for the Mindstorms NXT using a cross-compiler.

As far as I understand the latest generation iPod is also based on ARM7 processors.

With the help of something like SDCC it should be possible to then build C programs (or the like) for the NXT brick.

I guess it’s just time which will tell.

NBC: How to read Mindstorms touch sensor 649

Posted by Tom Wed, 04 Oct 2006 19:23:00 GMT

Here a small program to read the touch sensor value. Be sure to connect the touch sensor to port 1.

#include "NXTDefs.h"

dseg segment 
  dtArgs TDrawText 
dseg ends 

thread main

    dseg segment
        sensorValue byte
        sensorPort byte
    dseg ends

    setin IN_TYPE_SWITCH, IN_1, Type
    setin IN_MODE_RAW, IN_1, InputMode

Endless:

    getin sensorValue, IN_1, RawValue

    set dtArgs.Location.X, 1
    set dtArgs.Location.Y, 1

    // Clear entire screen
    set dtArgs.Options, 1 

    // Convert from number to string
    numtostr dtArgs.Text, sensorValue

    syscall DrawText, dtArgs

    jmp Endless

endt

Getting started with NBC on Apple OSX 30498

Posted by Tom Sat, 30 Sep 2006 21:20:00 GMT

NBC means NeXT Byte Code and is an assembler like programming language for the LEGO Mindstorms NXT programmable brick. It uses the default LEGO firmware. What follows is a tutorial on how to start programming using your Mac and NBC.

Writing and compiling the code

First download NBC (I’ve used version 1.0.1 b8) from the NBC homepage. This will usually download onto your desktop. The editor I use is TextMate, it’s not expensive and is very usable. Later I will publish a TextMate Bundle for NBC, so that it’ll become easier to start programming. Now type or cut-and-paste the following code (which I stole from here) in TextMate and save the file as helloworld.nbc:
#include "NXTDefs.h"
// Always include the above

// Segments declare variables or types
dseg segment 
  // Variables are always global.
  // Define variable dtArgs of type TDrawText
  dtArgs TDrawText /* see Syscall DrawText docs */
dseg ends 

// Code is organized in threads 
thread main
  // Set is to store scalar constants
  // For instance, set X location to 1
  // X is a member of the TDrawText struct
  set dtArgs.Location.X, 1
  set dtArgs.Location.Y, 1
  set dtArgs.Options, 1 /* erase previous text */
  // Mov is to copy more complex data
  mov dtArgs.Text, 'Hello World'

  // Write to the screen 
  // by calling the Lego firmware
  syscall DrawText, dtArgs

  // Wait forever so the user sees the screen
Endless:
  jmp Endless
endt

Okay now you need to start Terminal.app and run nbc:

Make sure the extension of your sourcecode is .nbc, the extension of the output file is .rxe and the command you type is: ./nbc helloworld.nbc -O=helloworld.rxe. For more command-line options type ./nbc -help.

Putting it on the brick

Currently the Mac version of the NBC program is not capable of downloading your program to the NXT brick, like the Windows version does, so we’ll need to use the Mindstorms NXT software which came with your kit:

First start the software and start you’re brick (press the orange button). Now we need to download to you’re brick, the simplest way of achieving this I think is by pressing the New program GO button:

Now click the NXT Window button and open the Memory tab:

Click the Download button and find your helloworld.rxe application:

Click the Ok button and … that’s it! Now start helloworld from your brick! Quit the Mindstorms NXT application and don’t save the program (as there isn’t any programming done in the LEGO Mindstorms application). Do save your helloworld.nbc program of course!

Afterthoughts and future

Some afterthoughts and something on the future on NXT and Mac:
  • In the near future NBC on Mac OSX will very likely support auto-downloading to your brick, which simplifies things drastically.
  • TextMate supports Bundles, which are helpers to simplify programming for you. In the near future I will create such a TextMate Bundle and publish it on this site
  • This site is built using Typo and uses NBC code syntax highlighting using the default plugin of Typo and a NBC specific class to do the highlighting. I will publish this later.
For any hints, suggestions and or tips mail me at: tom at degrunt dot nl