Sunday, January 13, 2013

FINF is not FORTH!

... but it is pretty darned useful.

Today, I contributed to an open source project. I've never done that before. The project is a little command-line project for the Arduino, named FINF.

FINF is an open source partial implementation of the FORTH programming language, for Arduino. It gives you a command line on the serial port of your Arduino, and allows you to manipulate the hardware directly from the command line. For instance, you can read the GPIO lines, set the analog outs, set a PWM port, and so forth -- all without compiling or uploading any code. It's really great for rapid prototyping on the basic Arduino platform. FINF is a project started by Leandro Pereira, and he talks about the project on his blog, here.

A basic embedded FORTH on the Arduino is pretty darned cool -- however, I wanted a little bit more. For it to be really useful to me, I needed it to be able to control stepper motors and DC motors, as well as to be able to automatically run a program on startup (I store this in the Arduino's EEPROM), have variables and constants, be able to manipulate memory, and so forth. So I took Leandro's project and tweaked it. The results are up on GitHub, here: http://github.com/lpereira/finf

And Then I Bolted On Some Stuff

So, what we have now are the !, @, and ? FORTH operators, which allow memory access, the "variable" keyword, the PAD to manipulate strings, and bunch of other helpers, such as some to load programs from the EEPROM. More importantly, though, I have commands such as "forward", "back", "turn", and "motor" which allow me to control DC motors using an AdaFruit MShield motor controller, ("forward", "back", and "turn" assume a skid steer robot on one of the two banks, and "motor" directly turns the motor on at a specific speed). I also added a "step" command, which allows me to operate a stepper motor from the command line. Aside from that, I also built in some support for the SeeedStudio Relay Shield. Also, because of Leandro's original work, I have access to all the PWM ports, all the GPIO lines and analog inputs -- so I can do anything I want, such as controlling servos, reading potentiometers, monitoring pushbutton switches, motion detectors...

All this allows for some very rapid prototyping with the Arduino. Instead of writing Arduino code to try out an idea -- coding is always the longest and most involved part of these things -- I can, instead, just grab some parts, put them together, and open up a serial interface to the Arduino. From there I can just issue commands, and see if my idea works. I can't begin to tell you how freeing this is!

For instance, when I first got all this working, I snapped this together:

And then opened up a command line on the serial port of the arduino. The entire time I was doing this, I was also carrying on a conversation, explaining to my girlfriend what I was doing. In about ten minutes, I had assembled a small tank robot (I had already assembled the treads and motors part a while back), and watched it turn right, left, go forward, blink a few lights, and so forth -- as well as do a couple of patterns. No coding involved. Not one compile! 

Here's an example command line on the Arduino. I started by playing around with the platform I just built, and then I defined a command called "box". Executing "box" a couple times in a row makes the toy tank move in a square pattern.

From there, I grabbed a couple of steppers from my pile o' stuff, pulled the motor controller off the demo I had just made, plugged it into this:

About five to ten minutes and a Phillips head screwdriver later...

..and we have this. Two stepper motors hooked up to an Arduino Mega, easily controllable, no new code. From here it's a simple matter to hook up some servos, a couple of limit switches, some potentiometers, and build something.

Hey, wait a minute... is that a Whitebox Robotics PC-914 platform? With a Microsoft Kinect on it? and a Chumby Hacker Board? Why yes. Yes it is :-)

-- and controlled the stepper from the command line. All this in about 30 minutes. I don't want to even think about how much coding I would have had to do without FINF. So now, if I have an idea, I can just grab an Arduino, some parts, and go!

And Now, Some History

FORTH is a very cool language, and is very powerful. Stack based and very stack-centric, it's an early programming language which nevertheless allows for some modern paradigms such as reflection. FINF is an acronym, but FORTH is not -- even though ALL CAPS is the appropriate spelling. Originally written by Charles H. Moore, and early on used to control radio telescopes at NRAO, it has been used in many applications, such as embedded systems, CNCs, and so forth. I first used it when I was a kid, using a TRS-80 Color Computer (which still has an active community of enthusiasts), and used it to control my very first robotics project, which was a gutted Radio Shack remote control tank, wired up to two relays attached to one of the PIAs in the CoCo. It allowed me to define very English-like syntax to control the tank, where I could say things like "100 DEGREES TURN" and have it actually do it! 

If you would like to learn more about FORTH, I would highly suggest reading "Starting FORTH by Leo Brodie", which is available online. It's a classic on the language, and a lot of fun to read, as well.