Community PCB Mill Project

Basic Stamp G-Code Interpreter

by D. Daniel McGlothin
22 July 2005, version 1.3 [ 5968 views ]

Copyright © 2005 by D. Daniel McGlothin, All rights reserved.


Abstract
As a proof of concept, the Basic Stamp 2 implementation of a 3-axis, 1-spindle, limited G-Code interpreter is described.


Inspiration
Lashup
G00 Rapid Motion
G01 Straight Interpolated Motion
How Much Can Be Done in a Stamp?
Next Steps
Selected Links


Inspiration

As part of the on-going discussion about how to implement a PCB milling machine with an SX embedded controller, I put forth the suggestion that the first five (5) G-Codes defined the essential motion primitives. As such, I thought that they could be the basis of a command protocol between a PC-hosted POST-processor software application and the SX motion engine driving a 3-axis milling machine.

Somewhere along the line, the Thomas Dvorak's Printed Circuit Board Drill, an entry in the Circuit Cellar Flash Innovation 2003 Design Contest, was brought up as an example of what was being attempted. On seeing the brevity of the code to read the PC's data stream and manage the three stepper motors, I was made curious about whether an SX could just directly interpret G-Code motion commands and drive three axes of steppers.


Lashup

So, I started a series of prototypes of a simple stepper controller based on the Basic Stamp 2 (in this case, a HomeWork Board). Here is the lashup, actual connection and components are detailed within the source code.

2.jpg
 
3.jpg
 

The first activity was to just run the basic stepper motor application from the Parallax appnote. This demostrated that I could, in fact, wiggle a stepper axis in a predictable fashion.


G00 Rapid Motion

Next came a simple G00 Rapid Motion interpreter. To simplify things, I pared the G00 down quite a bit: all motions in machine coordinates, all motions incremental in nature, axis demand words have 1-bit of direction and 7-bits of magnatude data, and all implemented G-Codes were thrown into the same modal grouping. Although the DEBUG window was used for diagnostic output, the part program stored in the Stamp's EEPROM. The application's operational verification was done by imagining the stepper motor spindles being attached to the knobs of an EtchaSketch. Of course, the effective tooltip path was not hard to imagine for the simple part program crafted for this exercise.
G04 P3
G00 X48 Y96
G04
G00 X48 Y0
X-48 Y-96
X-48 Y0
M30
G00 Intepreter Source Code


G01 Straight Interpolated Motion

With that experiment worked out, the next step is to do the G01 Interpolated Motion. At this point, I must admit to the standing on the shoulders of giants. For the implementation of the interpolated motion, I borrowed heavily from the freeware STEPSTER G-Code Interpreter and Stepper Motor driver [see source code and operational description, a more complete reference is in Selected Links]. STEPSTER's interpolation engine in an implementation of the Bresenham straight-line algorithm; it also recalled to mind Don Lancaster's description of a Flutterwhumper implementation of the Bresenham algorithm. Anyway, STEPSTER is freely available, and unencumbered; it is also rather complete, given its rather brief, and somewhat convoluted implementation in QuickBasic.

The results of the first pass of adapting the STEPSTER straight-line interpolated motion engine to the Basic Stamp 2, even with the limitations associated with the rapid motion engine, exhausted the available RAM by a handfull of bytes. So I pared the STEPSTER engine a bit, by removing backlash compensation, and by having only one coordinate system. Once I got over the hurdle of moving the motion engine from floating point to fixed point integer distances, there was a few bytes of RAM left, and about one-fourth of the EEPROM available.

G01 Intepreter Source Code


How Much Can Be Done in a Stamp?

I got to wondering if I could stuff a 3-Axis controller into a Basic Stamp 2. Sure enough, with a code refactoring, and with eliminating quite a bit if the DEBUG diagnostics, I managed to get a 3-Axis, 1-Spindle, straight line (G00 & G01) G-Code interpreting and execution controller, with axis limit-switch and feed-hold monitoring, with a part program length of less than 11 bytes.

Unfortunately, I ran out of 9-volt batteries while starting the testing, but I can assure you that the program does compile without error.

Anyway, here is a listing of the features implemented in this little gizmo:

  • Part programs are stored in the Stamp's EEPROM.
  • Three axes are defined: X-Axis, Y-Axis, Z-Axis.
  • One spindle has simple OFF/ON, CW/CCW control.
  • This subset of the standard G-Codes are implemented:
    • G00 - Rapid Move - for X-Axis, Y-Axis, and/or Z-Axis
    • G01 - Interpolated Move - for X-Axis, Y-Axis, and/or Z-Axis
      • F-Word defines the feedrate
      • motion can be either positive or negative
      • demand is in machine units, range -127..127, equivalent to one stepper motor detent/step
      • incremental mode (G91) is assumed throughout,
    • G04 - Dwell - the P-Word contains the dwell time in seconds
  • Different from RS274. the G-commands are not treated in seperate groups; instead they are treated as modal commands in the same group. This means that after a Dwell, the Rapid/Interpolated prepartory command must be reissued. Another deviation is that the incremental moves always use all three axis words—to not have an axis move, it must be sepcified as having a zero motion.
  • This subset of the standard M-Codes are implemented:
    • M02 signals the end of the part program
    • M03 run spindle clockwise (CW)
    • M04 run spindle counter-clockwise (CCW)
    • M05 turn spindle off
    • M30 signals the end of the part program
3-Axis Stamp Source Code


Next Steps

At this point, I'm reasonably certain that, by using a multi-banked Stamp with scratchpad RAM, there likely be enough RAM space to implement 16 or 24 bit axis commands, incremental/absolute modes (G90 / G91), and serial connection to drip-feed a part program one-line-at-a-time or to download the whole part program to a I2C EEPROM. I'm inclined to believe that there would be enough program space to include 3-axis circular interpolation (G02 / G03) as well.

[edit on 22-Jul2005] But I do not have such a Stamp. O frabjous day! Callooh! Callay! / He chortled in his joy. I just remembered that I have a BS2p40 laying around. Let's see, now...32 I/O points, 8 banks of 2K bytes, 128 bytes scratchpad RAM, still just 26 bytes general purpose RAM (but I can implement a form of virtual memory)...why I'm starting to think I'll need to see if I can't do a G02 / G03 on a Stamp. Of course, I'll also need to add coolant control, maybe 16-bit axis commands, perhaps even full blown Gerber/Excellon style implied decimal and suppressed zeros, and that will lead to requiring absolute and incremental modes, as well as inch and metric positon entry. [end of edit]

Poor little Stamp, I might even feel sorry for it 'til I'm done.

[Then] I suppose my next step will be to implement this 3-Axis gizmo on an SX using SX/B, and then see if I could add circular interpolation to it. Other wishlist items then would include half-stepping (or maybe microstepping) the motors, adding a small screen & keypad (or maybe just a touchscreen) as an operator interface, and whatever else that would be useful that could be squeezed in.

I was a bit surprised how much capability I was able to torment the Basic Stamp 2 with, I'll have to see now what the SX might be able to endure.


Selected Links

These are links to the various resources mentioned above,
plus a few others that seem to fit in.

Parallax
 
Basic Stamp 2
 
SX
 
HomeWork Board
 
Stepper Motor
 
UNL2803A
 
Interactive Thread
 
Printable Thread
 
Links to Parallax, and to their SX microcontroller and the SX/B compiler. This is the central core of the motion processor of this PCB Mill project. Also links to the thread in the Parallax The Sandbox support forum that documents the development of the idea of the SX Community PCB Mill.
Introduction to G-Codes
 
A tutorial that introduces the the CAD - CAM - CNC toolchain, with an emphasis on the G-Codes, especially as used for PCB fabrication. I wrote this tutorial as a contribution to the Community PCB Mill project mentioned above. If you are unfamiliar with G-Codes as discussed on this page and in the Basic Stamp programs, the tutorial and the section of annotated external reference links may be of some assistance.
Printed Circuit Board Drill
 
Thomas Dvorak won a Distinctive Excellence award with this entry in the Circuit Cellar Flash Innovation 2003 Design Contest. He describes a PCB drill using an X-Y table made from old printers. This device has axis encoders of a sort, uses stepper motors to move the axes, and has a Motorola 68HC908QY4 as its motion engine.
CNC979_source.zip
 
CNC979inside.txt
 
Charlie Gallo
 
MetalWorking.com
 
Luberth
 
Mendonet
 
CrankOrgan
 
The first two links are STEPSTER, or at least the version to which I made reference. Kevin Carroll authored STEPSTER, and Charlie Gallo is STEPSTER's caretaker. The additional links are the references (that I am aware of) that discuss, or link to, STEPSTER.
Flutterwhumper
 
Vector Plotting
 
Rees Video
 
Don Lancaster describes his Flutterwhumper concept, how to implement Bresenham's algorithm, and even points you to a John Rees video that tells you how to rewind car alternators as a powerful three-phase stepper motors for a large-bed wood router.
ULN2803A Datasheets
 
The ULN2803A datasheet is available from a number of locations.
Jones' Definative Stepper Reference
 
An Embedded Systems Stepper Article
 
PicList Stepper Reference
 
Stepper Wire Wizard
 
While there may be much Stepper Motor information out there, these are my favorites.