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.
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.
WhyAt 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 VideoBackgroundCNC 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.
ArchitectureFinally, 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