Generating toolpaths with G-code macros (unsupported by GRBL)

There have been a bunch of times when I’ve wanted to do something simple like face some stock or do a contour but I’ve been too lazy to open up CAM.

I could buy G-Wizard Editor and use its conversational CAM features but it costs $300.

However I spent a bunch of money on this fancy controller board that supports a G-code dialect with nice features like flow control, so I thought I’d try to use that and what do you know, it works pretty well! I thought it might be interesting to share and show people here a couple of macros so they can see what they look like. Even if you can’t use them, it’s still interesting.

First though, some basics:

  • Anything that comes after a ; is just a comment. The controller ignores it, it’s there to make the G-Code easier to read.
  • In my controller, things that look like #number, e.g. #33, are variables. These are placeholders that you can set to whatever you like and the controller will remember them for you. For example if I tell the controller #30 = 1234 and then tell the controller G1 X#30, the controller remembers that I told it #30 means 1234 and interprets G1 X#30 as G1 X1234
  • When there are things in between [], that just means logic like addition, subtraction or comparison is happening inside the [].
  • While <thing> <more stuff> Endwhile means "as long as <thing> checks out, do <more stuff>.
  • You can ignore anything with sub in it for now.

So here’s the code:

sub face
    ; X max: #30
    ; Y max: #31
    ; Z cut: #32
    ; Z retract: #33
    ; Z feed: #34
    ; Feed: #35
    ; Stepover #36
    ; Go to a safe Z height
    G0 Z#33
    ; Go to the bottom right corner of the stock being faced
    G0 X[#30] Y0
    ; Start the spindle
    M3 S10000
    ; As long as the position on the Y-axis is less than the maximum, do the stuff below
    While [#5002 < #31]
        ; Plunge into the stock at the plunge feed rate
        G1 Z#32 F#34
        ; Cut from the right side of the stock to the left side of the stock
        G1 X0 F#35
        ; Retract to safe Z height
        G0 Z#33
        ; Rapid to the right side of the stock, but up one step on Y
        G0 X[#30] Y[#5002 + #36]

gosub face

And the toolpath looks like this (red is rapid, blue is feed, pink is where rapid and feed overlaps):

And for a more complicated macro, here’s another one that faces a stock in a spiral pattern.

The way it works is by cutting a rectangle, then cutting a smaller rectangle inside the first rectangle and repeating.

; Stepover
#30 = 1.0

; These numbers keep track of the edges of the square (stock)
; Left of stock
#31 = 0.0
; Right of stock
#32 = 210
; Bottom of stock
#33 = 0.0
; Top of stock
#34 = 210

; Z-retract
#35 = 1.0
; Z cutting height
#36 = -0.05
; Feedrate
#38 = 300

; Retract to safe height
G0 Z#35
; Go to the bottom right corner
G0 X#32 Y#33

; Start the spindle
M3 S10000
; Set the feed rate
; Plunge into the stock
G1 Z#36

; Figure out whether there's still a rectangle to be cut. When the top meets the bottom and the left meets the right, there's nothing else to be done.
While [[[#32 - #31] > 0] or [[#34 - #33] > 0]]
        G1 X#31 ; Go left
        G1 Y#34 ; Go up
        G1 X#32 ; Go right
        G1 Y#33 ; Go down

        ; Shrink the square by bringing each of the edges <stepover> closer to the center.
        ; Bring the bottom edge <stepover> closer to the center
        #33 = [#33 + #30]
        ; Bring the left edge <stepover> closer to the center
        #31 = [#31 + #30]
        ; Bring the top edge <stepover> closer to the center
        #34 = [#34 - #30]
        ; Bring the right edge <stepover> closer to the center
        #32 = [#32 - #30]

And here’s what the toolpath looks like:

As for why all this matters, once you’ve set up the macro, you can add it to the controller’s startup file and then, using that sub thing, reference the macro by name.

For example if I want to use the first macro, since I wrapped all the G-Code in sub face, I can reference that macro using the name face. When I want to face some stock, all I need to do is set the variables (which I can do in my controller’s UI) and then invoke gosub face in MDI.


That’s exactly what I’d love to see from a grbl sender. Grbl can’t handle it, but maybe a UI could.

What controller?

Is this compatible with GSharp?

I’ve been trying to find a suitable way to program G-Code or something similar, but Tool Path Language didn’t work out, and I really need a nice 3D visualization such as OpenSCAD affords.

I believe either bCNC or GrblGru integrated GSharp.

one could imagine a tool that is gcode-to-grbl converter :wink:

Uh, that’s pretty much what Gsharp is — though something which was specifically G-code compatible would be great, I think G# is something of a variation.

I’m using EdingCNC but I think these features are supported by most serious controllers. Mach3, Mach4, UCCNC, and LinuxCNC all have the features used here in some form I think.

I’m not overly familiar with GSharp but it claims to be compatible with LinuxCNC G-Code, so my macros might work as-is, or with a little bit of finagling.

Might not be a bad idea to introduce it to Carbide Motion :wink:

(Or just use another sender)

If you program in G-Code, many controllers can give you a visualisation and I think G-Wizard Editor specialises in it.

1 Like

Yeah, I’ve been using ancient version of NC-Plot which was offered for free, but it doesn’t do helical arcs. I’ll definitely have to see if that works for G#.

Feature requests for CM would have to go to @robgrz

Usually I use CAMotics, but I find how it does partial previews of files clunky, and it doesn’t have a great editing interface.

This topic was automatically closed after 30 days. New replies are no longer allowed.