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.

Update

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 CGCC.py

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

Nutshell
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.

Why

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

Background

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.

Architecture

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














9 comments:

  1. hi my name is arsalan and i want to make a software which able to convert text in to g code...

    can u guide me from where i take start i lit bit confuse .

    contact me at arzoo105@hotmail.com

    ReplyDelete
  2. The company I work with is planning to work with Plastic Engineering for our Plastic CNC Machining needs. I believe they use proprietary software. Would it be a waste if I learn CGCC?

    ReplyDelete
  3. C GCode Compiler is not that boring that you skip it..What I feel is its quite interesting and different approach..those who worked with C will find it even more interesting..Its coding is easy to understand and implementation worth consideration.So I suggest give it a look.
    software testing services

    ReplyDelete
  4. Nice work! I was just about to start writing something similar myself.

    I'm only just digging into this, but it appears that the #includes do not like filenames with dashes (-) in them.

    ReplyDelete
  5. hye. can u teach or give simple example on how to convert the gcode to c programming? i required to convert cnc machine code (g code) to c program

    ReplyDelete
  6. Why you just don't use Python (with its power OOP and nifty easy syntax) system for Gcode autogeneration ?

    ReplyDelete