Kate's Comment

Thoughts on British ICT, energy & environment, cloud computing and security from Memset's MD

Programming B.O.B (Lego Mindstorms NXT) in not-exactly C on MacOS / OSX

My true first beginnings as a hacker were with Lego technics. I was very lucky that my Dad got me one of the very first systems for controlling Lego from a computer back in the late 80’s. At the time it was only officially available for schools, but he pulled some strings.

Lego Mindstorms NXT

My childhood love of Lego never really died and last year I was given Lego Mindstorms set which comes with a programmable “NXT” block.

My latest creation, codenamed BOB 2.0, is pictured below. The name is a homage to to the old sci-fi film Black Hole where “BOB” is short for “BiOsanitation Battallion” (I am a megalomaniac evil genius intent or world domination, remember, and therefore need a robot army – baby steps and all that), and an little joke referring to my former life. 😉

BOB NXT robot

He is my own design with the intended purpose of being able to navigate around a room initially. To that end he has a simple catapillar drive arrangement though quite a lot wider than the design the set comes with. To accomodate the width I had to put in quite a bit of additional structural support beams at the back.

The extra width is so that he can have the third motor mounted sideways in the middle. That one turns his sensor “head” in a range of 180o. This was surprisingly fiddley since I wanted to get the swivel point exactly in the middle and the axis of the motors is off-centre. Thus I needed to use a little gearing to get it in the right place. I also had to ensure a low-friction mounting.

The sensor head is the utrasonic range finder with the light sensor stuck on top. Another of the challenges was getting the wires to curl neatly as the head turns, hence some of the apparantly non-structural and superfluous bits around the “neck”. I have been reading Extreme NXT and for the mark 3 I will dispense with the kit-supplied cables which are not very flexible and make my own.

Finally, the two “bumpers” are mounted onto pressure sensors. They are for two reasons:

1) He often does not detect low-down objects or tall thin things like table legs with the ultrasonic sensor.
2) Part of my plan is to have him using the ultrasound sensor to maintain a specific distance from a wall while travelling, thus his “head” will be turned to the side and he won’t be able to use it to sense obstacles.

Programming in Python and Not-Quite C (NXC)

The Lego Mindstorms graphical software has a number of issues. Under MacOS (OSX) there are bugs with the scrolling which makes large programs difficult. Further, to any actual programmer the graphical language is immensely frustrating to use. Therefore I have been looking for alternatives.

Being on Mac limits the options a little. Python is my preferred language so I was very excited to find NXT Python (also hosted with Google code) which is available for OSX. However, after much fiddling around I just could not get it to work. Bluetooth is often a problem and I just could not get the software talking to my NXT brick.

So I turned to the NeXT Tools (written by John Hansen for OSX as an alternative to the Windows BrixCC Windows IDE) which allows you to programm the NXT in Not-eXactly C (NXC).

The NeXT Tools / nxtcc apps are really great. You can use them to remote control the NXT which is good when in the build phase and that bit works fine via bluetooth.

Bluetooth woes

Unfortunately you don’t appear to be able to upload software via the bluetooth interface. The final post on this blog suggests that this is a known issue. In theory one should be able to use the NeXTTool command line program (OSX download here) but I’ve not managed to get that work.

Instead I have just been plugging in a USB cable which is not ideal since the concept is of a free-ranging bot, but will do while in the development phase. If someone has found a way to upload software via bluetooth to the NXT from OSX please let me know how in a comment!

Actual coding

So, I can’t use my preferred language (Python) but I used to write a lot of C and NXC is pretty easy to get your head around so that has not really held me back. The NeXT Tools code editor is pretty useless to be honest so I am just editing the code in Aquamacs (Emacs for Mac OS X – a must-have for any self-respecing coder!) and using the NeXT Tools for compiling and uploading.

Thus far BOB is still pretty basic but now that I’ve cracked the issue of being able to write real code for him I have high hopes! People have telling me I should get another pet since C4 (the cat) did a runner and I think BOB will count. 😉

Update 2012/4/3

Thus far NXC is proving usable but quite frustrating. For example, I wanted to make a function that makes BOB’s head scan, look at the immediate surroundings, then return a data structure with the distances to various obstacles at each scan angle. I spent a long time trying to figure out how to return a struct (or even an array) from a function (sub). Since pointers are not supported this was non-trivial but even when I got the code to compile (needing to declare the structs in the root scope was part of the issue) it throws an error at runtime.

As things stand I have given up on returning anything more complex than strings, floats and integers from functions.

Such quirks of the language, the lack of much in terms of debugging reports at compile time (the compiler often give you a totally bum steer) and essentially zero debugging at runtime on the brick makes it rather challenging!

Further, it sometimes just freezes. The block completely stiffs and I can’t kill the program.

On actually reading the information I’ve realised I need to update the NXT’s firmware to be able to use some array structures. *facepalm*

Additions

I’ve just discovered a range of add-on sensors from HiTechnic. I’m espeically excited by their sensor multiplexers (four sensors just is not enough) and the infra-red seeker. I plan to use the latter to make BOB follow me around (I will attach an IR beacon to my ankle).

2 comments