Thursday, April 8, 2010

CGCC now at version 1.01

CGCC v1.01 is now available

small bug fixes
  • Fixed a problem with postfix decrementor i.e. "x--" not working
  • Added instructions in README to create symbolic link for
Link: CGCC v1.01

Wednesday, April 7, 2010

Some CGCC Features and Examples

Some features of CGCC
If it works in C or C99 give it a try in CGCC

See the original posting for more info on what CGCC is
Link: Original CGCC Post

This page should give some examples of the capabilities that CGCC already supports.

C Style comments
Multiline block comments can be used to comment out large sections of GCode
// Single Line Comment
Multiline Comment

Declare functions with return values
float AddFive(float i) {
return i + 5

Evaluate expressions inside GCode
float x = 1;
float r = 2;
G00 X[x + r / 4]

#Include is supported
Useful for making libraries of functions in header files and including them in your GCode programs.
#include "lib.h"

Assignment from native GCode numbered variables
This is useful in probing operations
x = #1

Directly Emit GCode with the __gcode__ keyword
Useful if you run into a situation where CGCC doesn't handle exactly what you need. You can just emit the correct GCode manually.
__gcode__("(DEBUG, THIS IS A TEST)");

The const keyword to declare constants
const float Radius = 5;

Comparison Operators
== != < <= >= >

Arithmatic Operators
+ - * /

Compiler detects the use of uninitialized variables

Compiler detects use of undeclared variables

Postfix Increment and Decrement

If else statements
if (x == y) {
} else {

While statements
while (x != y) {

Tuesday, April 6, 2010

CGCC -- GCode with C constructs

CGCC (C GCode Compiler)

Not interested in CNC machining? Please skip this post.

Got a question or found a bug? Post it in the comments.

Link: Download the latest CGCC
Includes instructions on how to integrate it with the EMC2 AXIS GUI as a filter.


CGCC has now been updated to v1.01
  • Fixed a problem with postfix decrementor i.e. "x--" not working
  • Added instructions in README to create symbolic link for

More information on how to use CGCC and what it can do are now available in a separate posting

CGCC Screenshot

The CGCC compiler allows the use of C and C99 style variables, Functions, Looping Constructs, If Then statements, etc. in GCode with a compiler that converts GCode with C constructs into pure GCode. Written in Python. Use your favorite code editor and get C style syntax highlighting for free in your programs ( I use Eclipse on Ubuntu when creating my GCode programs)

As an example if you wanted to drill a grid of a 100 holes in a 10 inch by 10 inch grid you would repeat the following chunk of code in your GCode file 100 times with different X and Y coordinates. Add in skipping a line of holes down the diagonal where the x equals the y and you have a big headache on your hands.

G00 Z1
G00 X0 Y0
G01 Z0 F1
G00 Z1
(Repeat 99 more times with different X and Y coordinates)

The CGCC way (excuse the bad indents, blogger doesn't seem to like the way I write code)
// Constants
const float X_Holes = 10;
const float Y_Holes = 10;
// Loop
for (float y = 0; y < Y_Holes; y++) {    
for (float x = 0; x < X_Holes; x++) {
        if (x != y) {
            G00 Z1
            G00 X[x] Y[y]
            G01 Z0 F1
            G00 Z1
Nifty, no?

You could do the same using the o-word extensions in EMC2 GCode and I tried but almost ended up in the looney bin trying to keep everything straight with the global naming conventions of the o-words.

Link: More Information about LinuxCNC.


At the time I was building a SMT pick and place machine robot and needed to mill lots and lots of custom pieces for the machine. I was hand writing a lot of GCode and after a week I thought that there had to be a better way to do this. If I only had to do a few parts then it wouldn't have been worth the time to write a compiler, it would have been faster to just tough it out with standard GCode. But I had around 30 different parts that needed to be machined so spending a week writing a compiler seemed like it would be the faster solution.

Here is a video of the finished machine assembling boards
Link: Youtube Video


CNC milling machines and CNC lathes are programmed to cut parts using a language called GCode. Invented around the 1960s and first run off of punched paper tapes. The langauge hasn't evolved much although EMC2 has added variables, functions, and looping constructs through the use of o-words. Functionally the EMC2 enhanced variant of GCode can do everything a C Language can do, but syntactically it is not easy to write good programs. So I decided to build a compiler which can take C syntax mixed in with GCode and emit pure EMC2 GCode.


Finally, something I learned in college comes in useful. C is an old language so why not use old tools that were best suited for it. Bust out the old dragon compiler book and go to town.

An open source python version of lex and yacc are used to do the lexical analysis and some of the final code generation along with custom glue code to make this work with GCode. It basically works just like a standard C compiler except that instead of targeting the ISA of a particular CPU it targets the EMC2 variant of GCode.

I wrote this quite a while ago but just got around to publishing it now. It may need a little bit of clean up but it should work. I don't know how much time I will have to improve on this but other people are welcome to make it better.

Link: PLY an open source Python Lex-Yacc Implementation

CNC Milling Machine

Here is a picture of my CNC mill.

CNC Mill

In the Beginning...

It started off life as a small manual Sieg X1 Micro Mill bought from Harbor Freight (discontinued now)

Then it got CNCed...

CNC is accomplished by NEMA23 Stepper motors from Keling and Stepper Drivers and 2 parallel port breakout boards of my own design along with a custom 42V switching power supply.  One parallel port didn't provide enough inputs and outputs to control the 3 stepper motors, home / limit switches, spindle encoder, touch probes, and flood cooling.

The motor mounting brackets and spacers were all made on the mill itself when it was still a manual mill.  The solid couplers were made on a manual mini lathe.  Not fun and I'm glad I will never have to make anything manually on this mill again.

The mill runs a closed loop spindle so the spindle speed can be set from GCode programs.
The mill uses dynamic tool sensing so everytime a tool is changed the length of the tool is measured on a touch off plate automatically.

Software for controlling the CNC mill is EMC2 available at LinuxCNC

When everything was first put together and nicely maintained, the X and the Y would do 60 IPM rapids all day long with no problems.  Sadly after a few years of just enough maintenance to keep it running for one more job and not having the time to really take the mill apart and clean and realign everything performance has dropped down to 30 IPM rapids.

Then it got upgraded...

Along the way it got upgraded with these parts from LittleMachineShop

And then I went nuts with flood cooling...

It has a plastic enclosure surrounding the mill with doors.  The mill has flood cooling which pours a continuous stream of coolant at the cutting tool.  Before flood cooling the mill really struggled to make good clean cuts at any decent speed.  Heavy cuts would make horrible noises and it felt like the machine would shake itself apart.  With flood cooling, cuts with end mills come out very nice and machining speed has drastically improved with very little noise.  The belt drive and cutting at 6000RPM helps immensely also.  I do most of my milling in aluminum and the coolant helps carry away hot chips and keeps them from welding to the end mill especially at high RPMs.

Protip:  Never ever ever do high speed machining and flood cooling without an enclosure.  Coolant ends up sprayed all over the room.  Don't ask how I know this...

One day I was at Harbor Freight and there was a clearance sale...

I was at harbor freight to pick up some tools and noticed they were clearing out all of their MT2 drill chucks for something like $10 each.  I bought all they had left.  You can see them neatly arrayed in the tool holding rack on the right side of the picture along with a bunch of end mill holders.