Al Williams (WD5GNR)'s newest electronic column about electronic design, embedded systems, ham radio, and other geeky things.
Monday, February 18, 2008
Is it Basic or is it C? Its SEABASS
Personally, I like to program in C. Luckily, there is pretty good C language support for most modern microprocessors. However, if you don't know C, its a pretty steep learning curve. Sometimes you just want to bang something out quick.
If your processor wants you to use C but you want to use Basic, you might try Seabass. Seabass is a BASIC compiler that outputs C code. So anywhere you can compile a C program (even on Linux or Windows) you can write BASIC programs. Even better, Seabass integrates with existing C libraries and you can even embed C code in your program.
The link shows an entire article about using Seabass to produce some Morse code on an Atmel AVR and a PIC. Here's the first part of the AVR program:
include "avrio.bh" ` get I/O routines cinclude "app4delay.h" ` get delay routines (from C) #link "app4delay.c" ` include C library
` Define a string type deftype string char *
` Speed of a dot in milliseconds Const speed=200
The first line includes a basic header file (by convention, these files have a .bh extension). This is a SeaBass file (included on the CDROM) that provides some common I/O routines for the APP-IV (the target board). This line allows us to use things like HIGH and LOW to affect the I/O pins.
The second line includes a C file, not a SeaBass file. This file is one that the APP-IV kit supplies and provides an easy way to write delays. Notice the line just under this tells the compiler where to find the associated C file. If you are manually compiling SeaBass' output, this isn't necessary, but if SeaBass controls the build process, this will allow it to automatically bring in the correct C code that your program needs.
Type names in SeaBass have to be a single word. However, many C types use multiple words (or even symbols). For example, if you want to deal with a string, in C you use a character pointer represented by "char *". The SeaBass DefType statement allows you to make a SeaBass type that represents a complex C type:
` Define a string type deftype string char *
The final part of the initial part of the program sets a constant using Const. In this case, the speed value is set to 200 (this will be the number of milliseconds to delay for a dot).
Now it is very simple to understand the main code:
` Main program function main() as int dim text as string dim i text="-.-. --.-" ` message to send ` Set LED to output OUTPUT(B,0)
The first two lines in the function define variables (notice one of them is a string).
Here's the main loop:
` Main code top: for i=0 to strlen(text)-1 ` walk through text ` do the right thing if text[i]='.' then dot() end if if text[i]='-' then dash() end if if text[i]=' ' then space() end if next
Notice that the program uses the C library call strlen to find the length of the string. Of course, you could write a version of strlen in SeaBass, but since the C compiler already has this function, why not use it?
I've been lucky enough to own two hot air stations. An Edsyn Atmoscope which I loved to use but had some limitations on larger parts and a Aoyue hot air station which is more flexible, but not the build quality of the Edsyn.
However, because I like to tinker I've also tried a few home-grown solutions. Most of these center on an aquarium pump for air and steel wool for a heat exchanger. I haven't had good luck with these -- maybe because I don't have the right components or maybe because I'm "spoiled" with real tools.
The truth is, the best "cheap" results I've had have been with an $20 embossing gun I got on sale for $10 at Hobby Lobby. If you are handy enough with metal working to get it to accept they cheap Aoyue tips, I think it would be good enough for just about everything and they are cheap enough to replace if you burn one up.
Here's a homebrew station that looks promising though. It uses part of an old hair dryer and a soda bottle for the air source. I haven't tried it, but surely it would produce more air flow than what I've done in the past.
Another idea is to hack up a "heat gun". Here's at least one (plenty of other homebrew SMD irons on the same site).
Take a look at this video from MIT. The brush itself seems almost trivial. It looks like a "web cam" with a brush around it. What isn't trivial is the big touch sensitive screen!
Very cool idea though. I wonder how you could adapt it to something pen sized on a standard LCD monitor. Come to think of it, I wonder if a regular light pen would work with an LCD since there really isn't any "raster" as we know it -- is there?
Lots of mechanical devices (like mice and shaft encoders) output quadrature signals. This is just a fancy way of saying that you have two signals and whichever one comes on "first" means something. So in the picture, you can see a mouse encoder wheel moving on a single axis counter clock wise. If it were moving clockwise channel 1 would come on before channel 2.
This is a natural byproduct of how the optical encoder wheels work. They are offset a little from each other, so the slots in the wheels are aligned about like the pulses in the oscilloscope trace shown.
The PAK-VII's primary purpose is to measure pulse widths, but with very little effort it can also measure quadrature inputs. Want to know more? Read this article.
If you have PDF documents you want to present on the Web, check out Issuu (pronounced Issue). For example, above is the GP3 manual PDF hosted on issuu. Click on it to open the viewer.
This little robot is made with two perf boards from Radio Shack (one of them cut into two for the sides). This board is easy to work with because it has a grid of holes. It is very easy to make nice straight cuts (or even just score and snap). It is also perfect for drilling exact holes. Basswood and polyurethane glue holds it all together (along with a few screws).
The drive consists of two Futuba RC servos modified for continuous rotation and a caster wheel from Home Depot in the rear. The tires are 2.25" RC aircraft tires.
A 4xAA holder under the top deck powers the motors. The 9V battery on the top is just for the electronics.
The electronics is the GP3 board which has been programmed by the GP3EZ software so programming the robot is simple point and click.
The sensor is a Panasonic IR sensor tuned for about 38kHz. The IR LED is pulsed through a 2N2222 with the GP3's PWM at 32kHz which is close enough that it works. A piece of antistatic foam pushed into a pin header blocks the sensor from seeing the LED directly. When something is in front of the bot, it sees the IR bounce off of it. It would be easy to add more LEDs (for example, two on the corners). The sensors could be paralleled or just connected to more I/O pins.
Here's the software (dumped out to HTML by GP3EZ; the real software is all constructed using GP3EZ's point and click interface):
Step #
Tag
Condition
Action
Next
Notes
1
Start
Always
LED Off PWM: 200 freq=32766
Start IR and reset LED (for when we finish turning)
2
MainLoop
Input: XXXXXXX0
LED On
object
Check IR sensor
3
Always
Pulse: pin 7 2000
Drive forward
4
Always
Pulse: pin 6 1000
Drive forward
5
After 20 ms
MainLoop
Servo delay (20ms)
6
object
Always
back (set bookmark)
We detected something, so back up a little.
7
turn
Always
Set Loop A to 20
Start turning (just under 1/2 second)
8
turn0
Always
Pulse: pin 7 2000
pulse motors the same way
9
Always
Pulse: pin 6 2000
Inserted step
10
After 20 ms
turn0 (Loop A)
Pause and loop for 2 seconds
11
Input: XXXXXXX1
Start
If sensor shows clear (high) then go back to forward motion
12
Always
turn
Sensor wasn't clear so do some more turns
13
back
Always
Set Loop A to 100
Back up for about 2 seconds
14
back0
Always
Pulse: pin 7 1000
Turn motors in reverse
15
Always
Pulse: pin 6 2000
16
After 20 ms
back0 (Loop A)
Delay and loop
17
Always
{last bookmark}
Go back to caller
The table is easy to understand. The step number should be obvious. The "Tag" is a label that names a particular step so you can refer to it later. Each step has 3 major parts:
Condition - This must be true for the step to execute. Many of the steps are marked "always" and some are marked "After xxx milliseconds". These will always execute, of course. Note the lines that work with the IR sensor, however. They only execute when a specific condition is true.
Action - When the condition occurs, this is what will happen. The GP3EZ can output digital values, PWM, pulses, and do a variety of other tasks as part of the action. If you are connected to a PC (the robot isn't) you can write data to a file or execute external commands.
Next - When a step executes, this column tells the program where to go next (which is usually the next step).
The note field is just a comment and is ignored by GP3EZ.
Note that the GP3EZ supports looping and subroutines. For example, at the "object" tag, there is a transfer (in the next field) to the tab "back". The notation says that a "bookmark" is set. If you find the back label, you'll see it does several steps and then goes to the "last bookmark." This is nothing more than a subroutine call and return. You can see examples of looping in the object and back routines which generate a specific number of motor pulses.