Face_Grooving - rmu75/linuxcnc-wiki GitHub Wiki


date: '2015-06-30T21:52:42' title: G74_-_Peck_Drilling/Face Grooving wiki_parent: G74 - Peck Drilling

Notes on creating a new lathe g-code, G74 Peck Drill/Face Groove using Python remapping

(Page started 2015/06/27 Kirk Wallace)

.ini file entry


... [RS274NGC] ... # REMAPPING REMAP=g74 modalgroup=1 argspec=xzk py=g740 ... [PYTHON] TOPLEVEL = python/toplevel.py PATH_PREPEND = python ...

toplevel.py

#!/usr/bin/python

import remap

remap.py

#!/usr/bin/python

from interpreter import INTERP_OK import math

def g740(self, **words): # G74 is typically called like so: # G74 x ? z ? k ? (x = diameter, usually 0; z = hole endpoint; k = peck length)

<span style="color: #60a0b0; font-style: italic"># get position</span>
<span style="color: #007020; font-weight: bold">if</span> (<span style="color: #007020">self</span><span style="color: #666666">.</span>params[<span style="color: #4070a0">&#39;_lathe_diameter_mode&#39;</span>]):  <span style="color: #60a0b0; font-style: italic"># if is_lathe_mode</span>
    x_mode <span style="color: #666666">=</span> <span style="color: #40a070">2</span>
<span style="color: #007020; font-weight: bold">else</span>:
    x_mode <span style="color: #666666">=</span> <span style="color: #40a070">1</span>

x_start <span style="color: #666666">=</span> <span style="color: #007020">self</span><span style="color: #666666">.</span>params[<span style="color: #40a070">5420</span>] <span style="color: #666666">*</span> x_mode
z_start <span style="color: #666666">=</span> <span style="color: #007020">self</span><span style="color: #666666">.</span>params[<span style="color: #40a070">5422</span>]

<span style="color: #007020; font-weight: bold">if</span> <span style="color: #4070a0">&#39;x&#39;</span> <span style="color: #007020; font-weight: bold">in</span> words:
    x_end <span style="color: #666666">=</span> words[<span style="color: #4070a0">&#39;x&#39;</span>] <span style="color: #666666">*</span> x_mode
<span style="color: #007020; font-weight: bold">else</span>:
    x_end <span style="color: #666666">=</span> x_start <span style="color: #666666">*</span> x_mode

<span style="color: #007020; font-weight: bold">if</span> <span style="color: #4070a0">&#39;z&#39;</span> <span style="color: #007020; font-weight: bold">in</span> words:
    z_end <span style="color: #666666">=</span> words[<span style="color: #4070a0">&#39;z&#39;</span>]
    <span style="color: #007020; font-weight: bold">if</span> z_end <span style="color: #666666">&gt;</span> z_start:
        <span style="color: #007020; font-weight: bold">return</span> <span style="color: #4070a0">&quot;G74 error - Z cannot be larger than the starting Z position&quot;</span>
<span style="color: #007020; font-weight: bold">else</span>:
    z_end <span style="color: #666666">=</span> z_start

<span style="color: #007020; font-weight: bold">if</span> <span style="color: #4070a0">&#39;k&#39;</span> <span style="color: #007020; font-weight: bold">in</span> words:
    peck_length <span style="color: #666666">=</span> words[<span style="color: #4070a0">&#39;k&#39;</span>]
    <span style="color: #007020; font-weight: bold">if</span> peck_length <span style="color: #666666">&lt;</span> <span style="color: #40a070">0</span>:
        <span style="color: #007020; font-weight: bold">return</span> <span style="color: #4070a0">&quot;G74 error - K cannot be negative&quot;</span>
<span style="color: #007020; font-weight: bold">else</span>:
    peck_length <span style="color: #666666">=</span> <span style="color: #40a070">0</span>

<span style="color: #007020; font-weight: bold">if</span> (<span style="color: #007020">self</span><span style="color: #666666">.</span>params[<span style="color: #4070a0">&#39;_metric&#39;</span>]):  <span style="color: #60a0b0; font-style: italic"># if is_metric</span>
    backoff_length <span style="color: #666666">=</span> <span style="color: #40a070">0.50</span>  <span style="color: #60a0b0; font-style: italic"># mm</span>
    rounding_fudge <span style="color: #666666">=</span> <span style="color: #40a070">0.0001</span>
<span style="color: #007020; font-weight: bold">else</span>:
    backoff_length <span style="color: #666666">=</span> <span style="color: #40a070">0.020</span>  <span style="color: #60a0b0; font-style: italic"># inch</span>
    rounding_fudge <span style="color: #666666">=</span> <span style="color: #40a070">0.00001</span>

z_range <span style="color: #666666">=</span> math<span style="color: #666666">.</span>fabs(z_end <span style="color: #666666">-</span> z_start) <span style="color: #666666">-</span> rounding_fudge  <span style="color: #60a0b0; font-style: italic"># rounding_fudge prevents extra peck</span>
<span style="color: #007020; font-weight: bold">if</span> peck_length <span style="color: #666666">&gt;</span> <span style="color: #40a070">0</span>:
    num_pecks <span style="color: #666666">=</span> <span style="color: #007020">int</span>(z_range <span style="color: #666666">/</span> peck_length)
<span style="color: #007020; font-weight: bold">else</span>:
    num_pecks <span style="color: #666666">=</span> <span style="color: #40a070">0</span>

z_list <span style="color: #666666">=</span> []
<span style="color: #007020; font-weight: bold">for</span> i <span style="color: #007020; font-weight: bold">in</span> <span style="color: #007020">range</span>(num_pecks <span style="color: #666666">+</span> <span style="color: #40a070">1</span>):
    z_list<span style="color: #666666">.</span>append(z_start <span style="color: #666666">-</span> (i <span style="color: #666666">*</span> peck_length))
z_list<span style="color: #666666">.</span>append(z_end)

<span style="color: #60a0b0; font-style: italic">#print &quot;--kaw - z_list =&quot;, z_list</span>

<span style="color: #007020; font-weight: bold">if</span> math<span style="color: #666666">.</span>fabs(x_end <span style="color: #666666">-</span> x_start) <span style="color: #666666">&gt;</span> rounding_fudge:  <span style="color: #60a0b0; font-style: italic"># We&#39;re groove&#39;n</span>
    <span style="color: #007020; font-weight: bold">for</span> i <span style="color: #007020; font-weight: bold">in</span> <span style="color: #007020">range</span>(num_pecks <span style="color: #666666">+</span> <span style="color: #40a070">1</span>):
        <span style="color: #007020">self</span><span style="color: #666666">.</span>execute(<span style="color: #4070a0">&quot;G0 Z </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;</span> <span style="color: #666666">%</span> z_list[i])
        <span style="color: #007020">self</span><span style="color: #666666">.</span>execute(<span style="color: #4070a0">&quot;G1 Z </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;</span> <span style="color: #666666">%</span> z_list[i <span style="color: #666666">+</span> <span style="color: #40a070">1</span>])
        <span style="color: #007020">self</span><span style="color: #666666">.</span>execute(<span style="color: #4070a0">&quot;G1 X </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;</span> <span style="color: #666666">%</span> x_end)
        <span style="color: #007020">self</span><span style="color: #666666">.</span>execute(<span style="color: #4070a0">&quot;G1 Z </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;</span> <span style="color: #666666">%</span> (z_list[i] <span style="color: #666666">+</span> backoff_length))
        <span style="color: #007020">self</span><span style="color: #666666">.</span>execute(<span style="color: #4070a0">&quot;G0 X </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;</span> <span style="color: #666666">%</span> x_start)

<span style="color: #007020; font-weight: bold">else</span>:  <span style="color: #60a0b0; font-style: italic"># We&#39;re drilling</span>
    <span style="color: #007020; font-weight: bold">for</span> i <span style="color: #007020; font-weight: bold">in</span> <span style="color: #007020">range</span>(num_pecks <span style="color: #666666">+</span> <span style="color: #40a070">1</span>):
        <span style="color: #007020">self</span><span style="color: #666666">.</span>execute(<span style="color: #4070a0">&quot;G1 Z </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;</span> <span style="color: #666666">%</span> z_list[i <span style="color: #666666">+</span> <span style="color: #40a070">1</span>])
        <span style="color: #007020">self</span><span style="color: #666666">.</span>execute(<span style="color: #4070a0">&quot;G0 Z </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;</span> <span style="color: #666666">%</span> (z_list[i <span style="color: #666666">+</span> <span style="color: #40a070">1</span>] <span style="color: #666666">+</span> backoff_length))

<span style="color: #007020">self</span><span style="color: #666666">.</span>execute(<span style="color: #4070a0">&quot;G0 Z </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;</span> <span style="color: #666666">%</span> z_start)

<span style="color: #007020; font-weight: bold">return</span> INTERP_OK

The G74 routine above relies on an initial move to a starting position. This position is captured and used for x_start and z_start. The X and Z words in the G74 command are used for x_end and z_end. If x_start and x_end are equal, this invokes a plunge with chip break routine, otherwise a grooving routine is invoked. Since Python can round off floating point numbers, a "close enough" algorithm is use to test x_start and x_end equality. A check for peck_length > 0 is used to prevent a divide by zero error in the num_pecks calculation. Apparently, the x position parameter 5420 does not follow the lathe diameter mode, so we need to check for it and adjust x values. This needs testing, so beware. The z_range is adjusted a tiny bit so that an extra peck doesn't get added to the end. The peck algorithm counts the number of full pecks in z-range then adds a final z position of z_end. If the peck length fits evenly in z_range, the last full peck will happen to be at z_end, which we don't need because it's added later.

Peter Smid's book "CNC Programming Handbook, 3rd Edition" Page 222, covering G74 was used as a reference: https://books.google.com/books?id=w7-jBgAAQBAJ&pg=PA222

Parameters are:

X = Optional. Leaving X off or setting it equal to the start position invokes a face plunge with chip break routine, usually used for drilling or face plunge grooving. For grooving, this is the end X position. The X start position is were the tool was just before invoking G74. Grooving will need a tool that can cut on its side. Care is needed in limiting the area of the side cut by limiting the K peck depth value. The X value will need to be adjusted by the cutter width to get the desired groove width. In other words: groove width = (x_end - x_start) + cutter width. Z = Required. Z is the Z endpoint, or hole or face groove depth. The start position is were the tool was just before invoking G74. K = Required. The peck length. Pecks start from the Z start position. The last peck length is the distance between the z endpoint and the last full peck length.

(G74 Peck Drilling Example)
G7 (Dia. Mode)
G18 (XZ Plane)
G90 (Absolute Distance Mode)
G40 (Turn Cutter Compensation Off)
G21 (units in inches)
G54 (Work Offset)

G30 Z #5422 (Park Tool) T 0707

S 500 (RPM)

M8 (Coolant ON) M3 (Spindle ON, Forward)

G0 X 0.000 G0 Z 25.000 G74 Z 0.000 K 8 F 200

M9 (Coolant OFF) M5 (Spindle OFF) G30 Z #5422 (Park Tool)

M30 (End of Program)

  </td>
  <td><p>&nbsp;&nbsp;&nbsp;</p></td>
  <td><img src="http://wiki.linuxcnc.org/uploads/Screenshot_G74_Face_Groove-1.png"></td>
  <td>
(G74 Grooving Example)

G7 (Dia. Mode) G18 (XZ Plane) G90 (Absolute Distance Mode) G40 (Turn Cutter Compensation Off) G21 (units in inches) G54 (Work Offset)

G30 Z #5422 (Park Tool) T 0707

S 500 (RPM)

M8 (Coolant ON) M3 (Spindle ON, Forward)

G0 X 10.000 G0 Z 20.000 G74 X 25.000 Z 10.000 K 3 F 200

M9 (Coolant OFF) M5 (Spindle OFF) G30 Z #5422 (Park Tool)

M30 (End of Program)

  </td>
</tr>
<tr>
  <td colspan=3>Peck Drill Example</td>
  <td colspan=2>Grooving Example</td>
</tr>  </table>

Another Approach - Using .ngc

.ini


... [RS274NGC] ... # REMAPPING REMAP=g74 modalgroup=1 argspec=xzk ngc=g740 ...

g740.ngc

o<g740>sub

(Peck Drill)

(capture start position) #<x_start> = #5420 (Current X Location) #<z_start> = #5422 (Current Z Location)

#<z_end> = #<z> #<peck> = #<k> #<z_range> = [ABS[#<z_end> - #<z_start>]]

#<num_pecks> = [#<z_range> / #<peck>] o10 if [#<peck> GT 0] #<num_pecks> = [#<z_range> / #<peck>] o10 else #<num_pecks> = 0 o10 endif

o20 if [#<_metric>] #<back_off> = 0.500 (mm) o20 else #<back_off> = 0.0200 (inch) o20 endif

#<pass> = 1 o30 repeat [#<num_pecks>] #<z_target> = [#<z_start> - [#<pass> * #<peck>]] G1 Z #<z_target> G0 Z [#<z_target> + #<back_off>] #<pass> = [#<pass> + 1] o30 endrepeat

G1 Z #<z_end> G0 Z #<z_start>

o<g740> endsub M2


The above could use some input parameter checking. The loop does a needless back-off if the the peck length fits evenly in the Z range. Maybe a while z_target > z_end loop would be better.

Yet Another version

remap.py

#!/usr/bin/python

import sys import linuxcnc from interpreter import INTERP_OK import math import emccanon

def g740(self, **words): # G74 is typically called like so: # G74 x ? z ? k ? (x = diameter, usually 0; z = hole endpoint; k = peck length)

<span style="color: #60a0b0; font-style: italic"># get machine G30 position in current G20/21 units</span>
x_start <span style="color: #666666">=</span> <span style="color: #007020">self</span><span style="color: #666666">.</span>params[<span style="color: #40a070">5420</span>]
z_start <span style="color: #666666">=</span> <span style="color: #007020">self</span><span style="color: #666666">.</span>params[<span style="color: #40a070">5422</span>]

<span style="color: #007020; font-weight: bold">if</span> <span style="color: #4070a0">&#39;z&#39;</span> <span style="color: #007020; font-weight: bold">in</span> words:
    z_end <span style="color: #666666">=</span> words[<span style="color: #4070a0">&#39;z&#39;</span>]
<span style="color: #007020; font-weight: bold">else</span>:
    z_end <span style="color: #666666">=</span> z_start

<span style="color: #007020; font-weight: bold">if</span> <span style="color: #4070a0">&#39;k&#39;</span> <span style="color: #007020; font-weight: bold">in</span> words:
    peck_length <span style="color: #666666">=</span> words[<span style="color: #4070a0">&#39;k&#39;</span>]
<span style="color: #007020; font-weight: bold">else</span>:
    peck_length <span style="color: #666666">=</span> <span style="color: #40a070">0</span>

z_range <span style="color: #666666">=</span> z_end <span style="color: #666666">-</span> z_start
<span style="color: #007020; font-weight: bold">if</span> peck_length <span style="color: #666666">&gt;</span> <span style="color: #40a070">0</span>:
    num_pecks <span style="color: #666666">=</span> <span style="color: #007020">int</span>(math<span style="color: #666666">.</span>fabs(z_range <span style="color: #666666">/</span> peck_length))
<span style="color: #007020; font-weight: bold">else</span>:
    num_pecks <span style="color: #666666">=</span> <span style="color: #40a070">0</span>

<span style="color: #007020; font-weight: bold">if</span> (<span style="color: #007020">self</span><span style="color: #666666">.</span>params[<span style="color: #4070a0">&#39;_metric&#39;</span>]):  <span style="color: #60a0b0; font-style: italic"># if is_metric</span>
    backoff_length <span style="color: #666666">=</span> <span style="color: #40a070">0.50</span>  <span style="color: #60a0b0; font-style: italic"># mm</span>
<span style="color: #007020; font-weight: bold">else</span>:
    backoff_length <span style="color: #666666">=</span> <span style="color: #40a070">0.020</span>  <span style="color: #60a0b0; font-style: italic"># inch</span>

<span style="color: #007020; font-weight: bold">for</span> i <span style="color: #007020; font-weight: bold">in</span> <span style="color: #007020">range</span>(num_pecks):
    z_target <span style="color: #666666">=</span> z_start <span style="color: #666666">-</span> ((i <span style="color: #666666">+</span> <span style="color: #40a070">1</span>) <span style="color: #666666">*</span> peck_length)
    line <span style="color: #666666">=</span> i <span style="color: #666666">*</span> <span style="color: #40a070">2</span>
    emccanon<span style="color: #666666">.</span>STRAIGHT_FEED(line,x_start,<span style="color: #40a070">0</span>,z_target,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>)
    emccanon<span style="color: #666666">.</span>STRAIGHT_TRAVERSE(line <span style="color: #666666">+</span> <span style="color: #40a070">1</span>,x_start,<span style="color: #40a070">0</span>,(z_target <span style="color: #666666">+</span> backoff_length),<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>)

emccanon<span style="color: #666666">.</span>STRAIGHT_FEED(line <span style="color: #666666">+</span> <span style="color: #40a070">1</span>,x_start,<span style="color: #40a070">0</span>,z_end,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>)
emccanon<span style="color: #666666">.</span>STRAIGHT_TRAVERSE(line <span style="color: #666666">+</span> <span style="color: #40a070">2</span>,x_start,<span style="color: #40a070">0</span>,(z_start),<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>,<span style="color: #40a070">0</span>)

<span style="color: #007020; font-weight: bold">return</span> INTERP_OK

⚠️ **GitHub.com Fallback** ⚠️