Monday, August 1, 2011

Iridium: Simple file-based sequential NXT motor control

After a bit of typical Summer-induced inactivity for which I beg pardon, here is something possibly useful for the NXT fans.

First of all, a bit of introduction to the problem wouldn't harm. NXT smart brick is a great device, but its architecture and software imply some limits. For many implementations, those limits are completely irrelevant, but in some special cases they can become a nuisance. One of them is inconvenience and inefficiency of creating long sequences of independent motor moves within the NXT-G programming platform, and difficulty of creating them automatically. There are certain tricks that can help, such as flashing the firmware with custom code, Bluetooth-mailbox instruction systems, but these are usually cumbersome and require a lot of hassle for just a simple task.

Therefore, there is some space for a solution in between. Take, for example, a plotter: if manually aligned, in its simplest implementation it requires no sensors neither any logic structure; all that is required is to move motors in a specific, but rather long controlled sequence. Far too long to create (or even worse, debug) directly within NXT-G, but still too simple for many to go through the battle with firmwares, messaging, etc.

So I've opted to make a simple file-based solution. Create a file with lots of numbers and upload it on the device, then have an NXT program read it and move the motors accordingly. Don't let me be misunderstood: this is not a workaround or even something completely new in the NXT sphere - all these functions are normally supported by NXT (i.e. NXT-G supports reading raw, linebreak separated numbers from a text file). This is just an example of implementing them for the purpose in question.

The file structure is really easy to understand. It consists of successive number pairs, with the first one always indicating the type of instruction, and the second being the "value" of the said instruction. Specifically, instruction types 1, 2 and 3 refer to moving the three motors, for a value that follows them. In other words, a sequence "1, 45, 2, -360" will rotate the first motor 45° in positive direction, and then rotate the second motor one full circle in negative direction.

Obviously, this sequence can continue as long as the NXT brick's internal memory allows which, taking in account the storage requirements per instruction, can extend to 10000 or even more instructions, probably more than its batteries and owner's patience can last anyway.

Besides the motor movement instructions, there are some others that may prove useful: number 11 followed by a number between 1 and 100 will set the motor power of all the upcoming movements to that value (that is, all until the next number 11 instruction). Likewise, number pairs 12, 0 and 12, 1 will choose whether the system will proceed or, respectively, wait for the current instruction to finish before executing the next. Number 13 followed by a number allows to set amount of milliseconds to wait between each instruction.

Finally, when encountering instruction 0, the sequence will terminate. If it was followed by a 1, it will also play a sound to notify the user.

As you can see, despite offering no real logic or dependency on sensors, this is a rather simple way to automate many successive motor movements. Such files can be very easily edited on any computer with a simple text editor, or automatically created by some software or using a dedicated library (I'm working on one for my favourite language - Python). It can even be automated further, using e.g. an external software such as NeXTTool by John Hansen to upload the instruction file to the NXT and run it, allowing the user to control everything from the computer, at least if it is all right to have the NXT connected to the computer all the time.

Here you can download (File > Download original) the .RBT and .RXE file for NXT and a sample instruction file. It must always be Iridium.txt, unless you're ready to slightly modify the RBT file.

If you like it, and find some cool usage for it, I'd be glad if you would drop a link or a description in the comments. Have fun!

(One final note, if you are interested, the name of this project has nothing to do with LEGO. It is just a coincidence I have had a periodic table on my desk at the time I started making it, and one minifig was standing on the Iridium's box.)

No comments:

Post a Comment