Wednesday, February 15, 2012

Samples of NXT (Lego Mindstorms) programming in Haskell

I've just created a new repository on GitHub called nxt-samples. It's only got one program now, a Bumper program that has the robot going straight till it hits something, then reversing and turning a bit, and going again (inspired by this Bumper Car, even though mine has tracks and no wheel). I've put this up because I didn't find it easy to get started programming a NXT robot, even with the excellent NXT library. Samples on the web were pretty scarce.

A few things bit me:
- not waiting for the final orders to be sent to the robot before exiting the program. This got me wondering a long time why the robot wasn't performing in a consistent manner. Now I'm resetting the motors at the start and end of the program.
- I found it's more reliable to check how much the motors have moved by repeatedly and wait till you reach the amount you wanted in the first place
- I added a simple command: when you press space the robot halts and the program stops. However, this doesn't work on Windows: you need to press space AND enter. Look here to see the woeful story of a Windows only, 4 year old, GHC bug that was fixed, but then reverted as it caused regressions in Cygwin and such. Every few months, that poor bug gets pushed to next release...  I know I'm so uncool to use Windows, but really... Can Haskell be really successful without working well on the OS that a lot of corporate developers still use?

Hopefully I'll add to these samples as I go on playing with NXT... Any feedback appreciated!

6 comments:

Yutong Wang said...

Hi there! Using Lego Robotics to learn Haskell sounds totally wicked! I am really inspired by your post.

Do you have any recommendation on how to start working with Haskell NXT? Like what would be a Lego Mindstorms set to purchase? How well suited is Haskell for programming robots?

Thanks!

JP Moresmau said...

I have the standard NXT 2.0 box, you can do a few robots out of the box, and the internet has quite a few other models you can do with the bricks in that box.
I don't know if you could say that Haskell is particularly suited, but the NXT library does take care of the low level stuff for you, so you only need to worry about high level operations on your robot. You could probably link a FRP library like reactive-banana to have cool haskell code, but I don't really know enough about FRP yet to relaly be confident.

sekol said...

Hello, thank you for the samples! I would like to know how to run compiled Bumper.hs program on Windows (7) in more detail. More specifically: What path to the device i am supposed to give it as a parameter. Thank you very much!

sekol said...
This comment has been removed by the author.
sekol said...

I have discovered that it is "COM4" in my case. For anyone also wandering here is how i managed to run it: Establish a connection with the standard Lego software, then close the software. After that right click the Bluetooth icon in tray and hit 'open setting' (or something similar, i don't have english version) and second tab with ports will tell you witch COM it is.
My lego runs on haskell, yipeee! :)

JP Moresmau said...

Yes, it's COM4 for me too on Windows. Glad you could figure it out!