By Brad Stewart – Welcome to the first installment of a series of Silicon Meets Software articles where I will discuss all matters robotic. That is, I will write about topics that cover robotics hardware, the StickOS for robot control, and a variety of other robotics-related topics. So let’s get started, shall we?
There’s a new programming language now available for robots and other embedded systems. Well, maybe not new, but certainly improved. It uses “Beginners All-purpose Symbolic Instruction Code”, also known as — ta-da! — BASIC. It’s the native programming language of a new embedded operating system known as StickOS.
Remember the Apple II? Or the Commodore 64, Atari 800, TRS-80? All of these early PCs had one thing in common — a ROM containing a BASIC programming language and an operating system. Flip the switch, and a prompt appeared almost immediately.
StickOS is all that and more.
StickOS BASIC is a comprehensive Integrated Development Environment (IDE) that runs inside flash memory of a 32-bit microcontroller unit (MCU), such as a Freescale ColdFire or Kinetis MCU. It doesn’t matter whether you run a Mac, PC, or Linux box. All it needs is a simple terminal program on your host PC, such as HyperTerm, TeraTerm, or MiniCom. That’s it. No gigabyte programs to download and install, no configuration hassles, no special hardware, no operating system incompatibilities. Just run your favorite terminal program and connect the PC to the target with a USB cable.
“Fine,” you say. “But BASIC? For crying out loud! It must be slow and devoid of instructions that let me do important robotic things.”
Well, this isn’t your Dad’s (or Granddad’s) BASIC (Figure 1).
Consider the following:
- It’s very easy to learn. I mean, really easy.
- Instant compilation to intermediate byte code. Syntax errors are flagged immediately.
- Built-in editor.
- Debug, trace, step, assert, and profile your code.
- Runs at approximately 95,000 instructions per second on an 80MHz ColdFire MCF52559.
- Structured programming using
do-until, while-do, for-next, break, continue,etc. - Interrupt handling on any peripheral that can generate an interrupt.
- Variables can be unsigned byte, unsigned 16-bit, and signed 32-bit words, arrays, strings, internal registers, memory locations, or even processor pins
- Bit shifts, and bit level Boolean operations such as OR, XOR, AND, NOT, etc.
- Random number generator
- Labels with long names
- Built in commands that manipulate all of the chip’s peripherals (GPIO, A/D, D/A, QSPI, I2C, USB, Serial, Ethernet, 802.15.4 RF, LCD display, timers, frequency generation, servo control, PWM, and so on.)
- Library support
- File manager
- Extensive built-in help system
- And lots more…
Want proof? Below is a program to read the X, Y, and Z values from an MMA7455 accelerometer on the bipedal Freescale Mechatronics Robotbased on the 32-bit ColdFire MCF52259 (Figure 2).
5 rem tilt_demo
10 dim x, y, z
20 gosub mma7455_init 0x1d //library function to initialize MMA7455
30 while 1 do //infinite loop
40 gosub mma7455_poll 0x1d, x, y, z //library function to read MMA7455
50 print hex "x = ", x, "y =", y, "z =", z
60 sleep 100 ms
70 endwhile
Pretty simple and straight-forward, don’t you agree? And easy to figure out what’s going on.
Things to note in the source code:
- “rem” and “//” denote comments
- Line 20 and 40 call functions in a library file stored in flash (more about that later).
- Line 50 prints out the values in hexadecimal format. Leaving out “hex” prints the values in decimal.
- Line 60 places the program in an idle task for 100 milliseconds.
Now suppose I wanted to print out a value every two seconds. I type “edit 60” and move the cursor to replace “100 ms” with “2 s” or “2000 ms”. Hit Enter. Done!
Instead of using the sleep function, let’s modify the same program to use a timer interrupt to print out the results every 100ms.
5 rem tilt_demo using interrupts
10 dim x, y, z
15 configure timer 0 for 100 ms //set up the timer
20 gosub mma7455_init 0x1d
25 on timer 0 do gosub print_results //define the isr handler
30 halt
35 sub print_results //this is the isr
40 gosub mma7455_poll 0x1d, x, y, z
50 print hex "x = ", x, "y =", y, "z =", z
70 endsub
Note that in line 30, we halt the program. But it continues to read and display accelerometer data every 100 ms. Try doing that in less than 2 minutes using C or Arduino! By the way, interrupts can be enabled, disabled, and masked.
Line numbers are there for editing purposes only (and they can be renumbered). Don’t like line numbers? Type “numbers off” and you get a nice listing like this:
dim x, y, z
configure timer 0 for 100 ms
gosub mma7455_init 0x1d
on timer 0 do gosub print_results
halt
sub print_results
gosub mma7455_poll 0x1d, x, y, z
print hex "x = ", x, "y =", y, "z =", z
endsub
end
In the examples above, we used the library feature of StickOS to access the I2C interface. This makes it easy to place common drivers into one file area in flash memory. It’s really just a separate program with the special name “library”. You can view and make changes to it as would any other StickOS program.
If you want to examine one of the library subroutines, list it by name. For example, type “list mma7445_init” to get:
sub mma7455_init addr
rem initialize the mma7455 at i2c addr
dim cmd as byte, data as byte
cmd = 0x16, data = 0x1 // Mode control
i2c start addr
i2c write cmd, data
i2c stop
endsub
Note the use of the special i2c commands. This makes it quick and easy to access sensors on the I2C bus. For example, you can see that the address of this subroutine is passed to it by the calling program (0x1d in this case). Also note that it writes a value of 0×1 to register 0×16, which is the mode control register that sets the g-Range to 8g. Simply change this value if you want a more sensitive setting, such as 0×05 for 2g.
There’s so much more to talk about. Stay tuned for future articles where, for example, I show how ridiculously easy it is to read a potentiometer and control a servo motor.
In the mean time, you can try StickOS yourself. Currently, it’s available on a number of ColdFire MCUs, some Microchip MCUs, and coming soon to Freescale’s ARM Cortex-M4 Kinetis MCUs (ooh…I can hardly wait!).
You can download StickOS and documentation here: www.cpustick.com. There is even an emulator available that runs in Windows.


11 Comments
Robotic programming is very difficult as one of my friend is in the same field so I know that.
Brad,
Interesting, I will have to take a look at it. Currently configuring a 3 year high school program which contains a large contingent of robotics. The origanal basic was “a dog” but I will give this a shot for the classroom.
Well ! coming back to good old days ! Eagerly waiting to see that it is also implemented for Freescale Arm Kinetis MCUs.
I have used one of the inexpensive controller boards from Atria Technologies. You can buy a board with the BASIC pre-installed on a board with a Freescale MCU for $23. Makes it pretty easy for robotic stuff but I used it for something else.
Very interesting stuff. One specific comment: I don’t share your (apparent) liking for ‘Go To’. Using ‘Go To’ is a bit of a cop out and really not a good thing for clean programming style. At any rate, I am definitely going to look into this as a ‘fun’ deal. Yeah, retired engineers can have fun too!
There are no goto statements in the above code.
In fact, Figure 1. captions states “There are no GOTOs in StickOS (darn!)”
Maybe the picture in Figure 1 is poorly chosen as some people will look at the picture and NOT read the text, or perhaps the article.
The goto statement was the standard back in the old BASIC and FORTRAN days. Seems it’s a bad thing. I don’t think so. It really depends on how it’s used. Take the time to look through a Linux source tree. You will be amazed on how often ‘goto’ is used!
StickOS is one of the best products I’ve had the opportunity to test. The possibilities are endless. Your imagination is the limit. An excellent job. Kudos to the author.
Boy that takes me back years and very refreshing to think, its back in vogue… loved BASIC since the 60′s.
I know from your blog that this BASIC is for robotics but have a complete bilingual (French/English) business management system that I authored in 1982-1986 and is written in IBM PC BASIC.
Do you have a compiler for X-86 machines so that I could try compiling it on my hardware?
If you go to http://www.cupstick.com and click on Downloads, you can get an entire CodeWarrior “skeleton” project that allows you to customize StickOS.
One Trackback
[...] my last blog article, I introduced the powerful programming environment known as StickOS. Among many of its features, [...]