Showing posts with label Machining. Show all posts
Showing posts with label Machining. Show all posts

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

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.

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