pbForth
This page documents how to use pbForth in programming Mindstorms projects. pbForth is only available for the Mindstorms RCX controller (RIS), not the newer NXT controller. I'm sure there are still plenty of RCX systems still out there.
The problem with the standard programming tool in the Robotics Invention System (RIS) is that it is very limited in its capabilities, can use minimal storage (8 variables?), isn't very intuitive, and doesn't translate into learning computer programming in any form (e.g. no subroutines or functions). Other systems such as RoboLab have a nice graphics interface, but graphical systems are not good for describing procedural or event driven systems. What is required is a REAL computer language. NQC and NXC are attempts to convert RCX programming into a text-based language, similar to C. However, because it is based on the original Mindstorms firmware of the RCX, it still has severe restrictions, including the same old number of variables. There are several languages which break out of these limitations by replacing the Mindstorms firmware, to allow better use of the processor and memory that exists in the RCX. There is a real C-based compiler and operating system (formerly known as LegOS, can't remember the new name). However, C is not particularly suited to robotics programming. One language exists that was specifically designed for limited speed, limited memory, robotic/machine control. This language is Forth. There are plenty of references on the Web for Forth. A version of Forth exists for the RCX, pbForth (programmable brick Forth) written by Ralph Hempel. It uses its own firmware (mostly written in Forth) to enable much larger programs and many more variables up to the limits of memory on the RCX. Although this Forth was basic in terms of how easily it can be managed and used for projects, Forth can easily be programmed to fix all of this, because it is extensible. In fact this is why Forth continues to be used for embedded and control purposes long after many new languages have been invented. Although Forth uses a completely different syntax to other languages, the programming concepts are all there.
Below, there is information and how to's to make the use of Forth much easier.
How To Use pbForth
Here are some guidelines on how to use (or at least, one way of using) pbForth for Mindstorms programming and learn a bit of the Forth language along the way. Most of the material is taken from Ralph Hempel's pbForth web-site (http://www.hempeldesigngroup.com/lego/pbForth/homePage.html).
What is Forth?
Forth is an interactive language. The user types commands into a "console" program and these commands are directly relayed to the RCX to be interpreted by Forth. These commands include definitions for new words to extend the language. As these language extensions (words) are built up, they eventually culminate in the final program that you wanted. In practice, it is common to store partially constructed programs in a source file (file extension .f, .4th etc) which can be edited and downloaded repeatedly. Downloading a program results simply in the commands being fed to the RCX interpreter as though you were typing them (very quickly) at the keyboard.
Forth is also a stack-based language, also called post-fix. For those who have used Hewlett Packard calculators, it is the same as the reverse-polish notation. This means that: 1 + 2 = becomes 1 2 + However, this notation is used for all functions/words, not just for calculation operators. This allows all the arguments for a function to be put on the stack before calling the "word" which will execute the function you have programmed for it. This stack-based methodology leads to extremely efficient function calls which allow you to break your code into very small (easy to debug) fragments without slowing your program down too much. It also allows an extremely elegant language syntax by making everything a "word" (including constants).
It is useful to get hold of a few key documents to learn generic Forth:
- Starting Forth or similar introductory book
- Standard Forth word list reference manual
Getting pbForth and Other Tools
pbForth is Forth specifically for the RCX or programmable brick (i.e. programmable brick Forth). It allows efficient programming of the RCX making lots of memory available for program and variables/arrays/matrices etc., far in excess of that allowed by the standard RCX firmware and programming tools.
Because the software doesn't seem to be maintained any more (and is therefore unlikely to be updated), I've taken the liberty of storing them here in one convenient place, so that you don't have to figure out which is the latest version or have to choose from the various tools available.
The tools you will need (download using the links) are:
the latest version of the pbForth firmware (pbForth.srec),
the current version of BricxCC (bricxcc_setup_33716.exe) programming environment (Includes: editor with colour syntax highlighting, firmware downloader, console, word browser, printing etc.),
a Button Operating System (BOS_20070409.f) ready to download,
a template for creating your own programs to be compatible with BOS (program_template.f)
- reference materials on pbForth
pbForth RCX word list rcxWordlist.html (web-page)
- a PC with a serial or USB port (to suit your RCX infra-red module) running some form of Windows operating system,
- a Mindstorms Robotics Invention System (RIS) which includes the RCX controller we want to program.
Installing and Setting-up the Tools
- installation directories
- finding the right port
- configuring the software
Bricx Command Centre (http://bricxcc.sourceforge.net/) - A more integrated environment than Hyperterminal! Includes: editor with colour syntax highlighting, firmware downloader, console, word browser, printing etc. Some recommended "Preferences" (found in Edit menu):
- 500ms interline delay (only required if you use long/multiline definitions - see tips)
- set "stop downloading on any error" so you don't miss an error
- downloading the firmware
- checking interactive communications
The first Interactive Programs
- Lets add 1 and 2. Note that the "." is a word that prints the value on the top of the stack (i.e. the result).
input: 1 2 + . output: 3 OK
- Defining a new word (i.e. a small program). This one calculates and prints the cube of the input ( x * x * x ). The : and ; words are the beginning and end of the definition. Our new word will be called "cube" and it expects "x" to be on the stack when you run the program. It duplicates what is on the stack twice (to create the 3 "x"s) ready to multiply with the two "*"s. Then there is a "." to print the result.
: cube DUP DUP * * . ;
and to test it ...input: 3 cube output: 9 OK
- The standard "hello world" program in Forth (." is a word that prints the following string ending with another ")
: hw ." Hello World" ;
testing ...input: hw output: Hello World OK
Button Operating System (BOS)
Downloading a Test Program
How to Write pbForth Programs
Here are some tips and tricks that might help prevent some of the usual teething problems with starting a new language or RCX project.
- Use lower-case for your own defined words, but remember that all the standard Forth words must be upper-case (yes, case-sensitive). This way it is easy to tell which are your own words (other than the colour syntax highlighting done by the BricxCC editor and console).
- Ensure you don't create an endless loop in your program, since there is no way to interrupt or terminate it, except to reset the RCX by pulling one cell of the battery out. This then requires you to reload the pbForth firmware before starting again with your programming. The Button Monitor system above is intended to help work around this to some extent by encouraging the use of a "yield" word in your loops which allows termination if necessary.
- Program lines must be less than or equal to 80 characters in length. Otherwise, the additional characters are truncated (not a problem if they are comments), which may chop off ";" words, stuffing up word definition completion.
- "Factor" or break your program up into single-line word definitions if possible; Its good programming practice anyway. This seems to prevent communication issues at the end of a complex word while pbForth compiles the word definition. Simply breaking the definition into multiple lines doesn't seem to be sufficient.
other stuff
Hyperterminal settings
The Hyperterminal terminal-emulation program found in all versions of Windows, can be used to interactively program Forth in the RCX. The following settings will give quite reliable operation of pbForth with the terminal emulator. About the only thing that will trip up the communications is a long dose of the keyboard auto-repeat holding down enter (CR). This can be restored by killing and restarting Hyperterminal. Use the Hyperterminal default settings with the following changes:
- correct COM port to suit computer
- port settings: 2400 bps, 8 data bits, Odd parity, 1 stop bit, Xon/Xoff flow control
- backspace = Ctrl+H
- ASCII setup: check "Append line feeds to incoming line ends"
- Line delay: 100 milliseconds