Skip to content

Script pygcode norm

Peter Boin edited this page Jul 30, 2017 · 2 revisions

Normalization Script

The pygcode-norm command can be used to normalize gcode from different CAM software. It's intended to be used as a pre-process before feeding gcode into your CNC machine.

usage: pygcode-norm [-h] [--singles] [--full] [--machine_mode MACHINE_MODE]
                    [--arc_linearize] [--arc_lin_method {i,o,m}[,{i,o,m}]]
                    [--arc_precision ARC_PRECISION] [--canned_expand]
                    [--canned_codes CANNED_CODES] [--rm_comments]
                    [--rm_blanks] [--rm_whitespace] [--rm_gcodes RM_GCODES]
                    infile

Normalize gcode for machine consistency when using different CAM software

positional arguments:
  infile                gcode file to normalize

optional arguments:
  -h, --help            show this help message and exit
  --singles, -s         only output one command per gcode line
  --full, -f            output full commands, any modal parameters will be
                        acompanied with the fully qualified gcode command
  --machine_mode MACHINE_MODE, -mm MACHINE_MODE
                        Machine's startup mode as gcode (default: 'G0 G54 G17
                        G21 G90 G94 M5 M9 T0 F0 S0')

Arc Linearizing:
  Converting arcs (G2/G3 codes) into linear interpolations (G1 codes) to
  aproximate the original arc. Indistinguishable from an original arc when
  --arc_precision is set low enough.

  --arc_linearize, -al  convert G2,G3 commands to a series of linear
                        interpolations (G1 codes)
  --arc_lin_method {i,o,m}[,{i,o,m}], -alm {i,o,m}[,{i,o,m}]
                        Method of linearizing arcs, i=inner, o=outer, m=mid.
                        List 2 for <cw>,<ccw>, eg 'i,o'. 'i' is equivalent to
                        'i,i'. (default: 'm')
  --arc_precision ARC_PRECISION, -alp ARC_PRECISION
                        maximum positional error when creating linear
                        interpolation codes (default: 0.005)

Canned Cycle Simplification:
  Convert canned cycles into basic linear or scalar codes, such as linear
  interpolation (G1), and pauses (or 'dwells', G4)

  --canned_expand, -ce  Expand canned cycles into basic linear movements, and
                        pauses
  --canned_codes CANNED_CODES, -cc CANNED_CODES
                        List of canned gcodes to expand, (default is
                        'G73,G76,G81,G82,G83,G85,G89')

Removing Content:
  options for the removal of content

  --rm_comments, -rc    remove all comments (non-functional)
  --rm_blanks, -rb      remove all empty lines (non-functional)
  --rm_whitespace, -rws
                        remove all whitespace from gcode blocks (non-
                        functional)
  --rm_gcodes RM_GCODES, -rmg RM_GCODES
                        remove gcode (and it's parameters) with words in the
                        given list (eg: M6,G43) (note: only works for modal
                        params with --full)

personally I'm getting good results for my machine using:

pygcode-norm -s -f -rc -rb -rmg m6,g43 -al -ce myfile.gcode

File-wide Options

--singles

only output one command per gcode line

$ cat part.gcode
T5 M06 G43
G17 G90 G21
G00 Z5 S7000 M03
X9.602 Y48.74

$ pygcode-norm --singles part.gcode
T5
M06
G43
G17
G21
G90
S7000
M03
G00 Z5
X9.602 Y48.74

--full

output full commands, any modal parameters will be accompanied with the fully qualified gcode command

$ cat part.gcode
T4 M06 G43
G17 G90 G21
G00 Z5
X9 Y-10.5
G83 Q1 G98 X9 Y-10.5 Z-12.3 R2 F50
X9 Y-45.5

$ pygcode-norm --full part.gcode
T4 M06 G43
G17 G21 G90
G00 Z5
G00 X9 Y-10.5
F50 G98 G83 Q1 R2 X9 Y-10.5 Z-12.3
G83 Q1 R2 X9 Y-45.5 Z-12.3

Arc Linearizing

Converting arcs (G2/G3 codes) into linear interpolations (G1 codes) to aproximate the original arc. Indistinguishable from an original arc when --arc_precision is set low enough.

note: the example below sets arc_precision to a very coarse 1mm just for illustration purposes

$ cat part.gcode
G0 X10 Y10
G1 X15 Y10
G2 X20 Y5 I0 J-5
G1 X20 Y0
G3 X25 Y-5 I5 J0
G1 X30 Y-5

$ pygcode-norm --arc_linearize --arc_precision 1 part.gcode
G00 X10 Y10
G01 X15 Y10
(linearized arc: <GCodeArcMoveCW: G02{I0, J-5, X20, Y5}>)
G01 X16.989 Y9.802 Z0
    X19.802 Y6.989 Z0
    X20 Y5 Z0
G01 X20 Y0
(linearized arc: <GCodeArcMoveCCW: G03{I5, J0, X25, Y-5}>)
G01 X20.198 Y-1.989 Z0
    X23.011 Y-4.802 Z0
    X25 Y-5 Z0
G01 X30 Y-5

Arc Linearizing Methods

The following images indicate how different arc linearizing methods generate linear segments. Each image shows the original (perfectly circular) arcs overlaid with the linear segments.

Inside -alm i

Inside arc linearizing method

Outside -alm o

Outside arc linearizing method

Mid -alm m

This method sets the maximum error-band around the arc at +-precicion/2, making it twice as accurate as the other methods, but the error is on both sides of the arc (inside, and outside).

All things considered, if the arc_precision is set to 1/2 that of your machine, I doubt you'll notice much difference between any of these methods.

Mid arc linearizing method

Mixing Methods -alm i,o

You may also mix methods by separating them with a comma: <cw>,<ccw>, so the example shown i,o uses the inside method for clockwise arcs, and the outside method for couter-clockwise arcs. This is a good way to carefully control the error for pocket, or profile operations.

Mixed arc linearizing method (inside, outside)

Canned Cycle Simplification

Converts canned cycles into basic linear or scalar codes, such as linear interpolation (G1), and pauses (or 'dwells', G4)

Not all hobby CNC machines support canned drilling cycles, this is one way to overcome that.

$ cat part.gcode
T4 M06 G43
G17 G90 G21
G00 X9 Y-10.5 S7000 M03
Z5
G83 Q5 G98 X9 Y-10.5 Z-12.3 R2 F50
X9 Y-45.5

$ pygcode-norm --canned_expand part.gcode
T4 M06 G43
G17 G90 G21
G00 X9 Y-10.5 S7000 M03
Z5
F50 G98
(expanded: <GCodeDrillingCyclePeck: G83{Q5, R2, X9, Y-10.5, Z-12.3}>)
G00 X9 Y-10.5
    Z2
G01 Z-3
G00 Z2
    Z-2.9
G01 Z-8
G00 Z2
    Z-7.9
G01 Z-12.3
G00 Z5
(expanded: <GCodeDrillingCyclePeck: G83{Q5, R2, X9, Y-45.5, Z-12.3}>)
G00 X9 Y-45.5
    Z2
G01 Z-3
G00 Z2
    Z-2.9
G01 Z-8
G00 Z2
    Z-7.9
G01 Z-12.3
G00 Z5