Peck Drilling macros for Mach3
**********************************************

(c) Mick Grant 2009

CONTENTS
*********
This package contains a two macros to replace the G83 and G83.1 functions bundled with Mach
The G83.1 function does not work at all on my mill and just gives G83 on my lathe
G83 also does not rapid down to just above last peck.

Contained are macros
M830.m1s		Peck Drilling with full retraction to R value above workpiece after each peck
M831.m1s		Rapid peck drilling by Q peck value and retract same amount


Both should be placed in C:\Mach3\macros\_name_of_your_profile
(this may still be Mach3_Turn or Mach3_Mill or you may have renamed it)

IMPORTANT
**********

These macros assume standard set up procedures are followed and that X, Y and Z origin points on the workpiece are standard.
ie.
On a Lathe

Take a facing cut across the end of the billet and then without changing anything, set Workpiece Z to 0
All Z co-ordinates thereafter where they apply to the workpiece will be expressed as a negative value.

Take a light cut along the billet diameter, measure carefully and without moving anything set the Workpiece X to this value.
This is the only way to accurately set up, trying to use feeler gauges or rolling rods under tool tips has inherent inaccuracies 
which will be reflected in the work.

Ensure you use the master tool for setup and that your tool compensation tables etc are accurate, otherwise setup using the tool you will cut with.

All X values will be in terms of diameter not radius

All co-ordinates will be absolute, not incremental.

On a Mill

Set X and Y to one corner of the workpiece and Z to a safe height.
Ensure that the retraction height R is sufficient to clear any clamps when drilling repeat holes using L parameter
Likewise ensure Z is retracted sufficiently between calls to M83? when drilling series of holes in several planes


SYNTAX
*******
These macros cannot be run from the MDI line, they must be called from within a loaded .NC file

Both macros take the same parameters and require the same syntax in your .NC files.
The macros read the .NC file that is loaded into Mach and require that it keeps to a set of rules regards spacing etc.
Keeping to these rules is good disipline anyway and will ensure that your files are readable and well ordered.

1)	 Lines MUST have line numbers, the sequence can be broken but should ascend
2)	 Can be upper or lower case, will be converted to upper in macro
3)	 There MUST be a tab between the line number and first command
4)	 MUST be a space between each command on the same line
5)	 Each line of GCode MUST MUST MUST end with a carraige return
 	(This sounds obvious and simple, but some people insist upon using word processors to edit code files.
	This is one of the major causes of problems with Mach not being able to read .NC files, applies also to these macros
 	Word processors do not always insert what you would expect when you enter a tab for instance and if you allow word-wrap to do 
	carraige returns you may not get what you expected there either.)
 
6)	 Use comments sparingly and to make sure they don't interfere with the code, put them on the line above or below the code you 		are commenting
	 It is nearly always commented lines which cause problems, often because they are not terminated by a carraige return and when 		read at a binary level attach to another line of code
	
7)	 You can use semicolon comment symbol(recommended)  or parentheses but any line that starts with either will be
	 discounted by the macro (except the ;M83? parameter line )
	 The section of a line from any comment to the end will also be discarded, so don't put comments in the middle of code,
	 Put them on the next or preceding line.

 I have tried to error check most potential problems but there is a limit!

PARAMETERS
************
Each macro has a commented out call to the macro on the line immediately preceding the macro call
eg. 	......
	;M830 X0 Y10 Z-10 R10 Q2.5 W0.5 P0 D0 F10 I20 J0 L4
    	M830 Pxx
	......

This is because Mach3 can only pass 3 parameters from the Macro commandline, P Q and R
P and Q are normally used to identify loop blocks, we use P to identify the parameter line
in the .NC file, commented out so that they do not affect prior operations
eg.

 ;M830 X0 Y10 Z-10 R10 Q2.5 W0.5 P3 D2 F10 I20 J0 L4

 X0  The X initial position during the cycle.
     In a standard lathe, this will always be 0, however the facility exists to drill one or more holes along the X axis in a lathe
     Therefore you can use this macro on a machining centre with indexing spindle, or with stationary spindle and powered 
     drilling head etc.

 Y10  The Y initial position during the cycle.
      If G18 (XZ lathe) is set, this parameter will be ignored, but it must be set to 0

 Z-10 The minimum Z value, that is the depth of the hole you want to drill

 R10  The maximum Z value that the drill is retracted to and the height of the drill during moves to next hole co-ordinates
	Make sure it is high enough to clear clamps when milling

 Q2.5 The peck distance for each peck
      In M830, the drill cuts this distance, then retracts to R value and then re-enters to a fraction short of the last peck.
      In M831, The drill cuts Q distance, retract Q distance and then re-enters to a fraction short of the last peck.
      The retractions and re-entry moves are done as G0 rapid moves
      This fraction short of full retraction is set by W (see below)

 W0.5 The 'waver' distance which controls how close to a full re-entry to last peck distance the drill goes
      Should always be a positive number and it is subtracted from last peck depth.
      eg. last peck went to 0.5mm, W0.05 will re-enter to 0.45mm at G0 and then drop to feed speed

 P3  The pause interval at the top of the retraction.
     This can be very useful for ensuring that swarf is flung loose of the drill before re-entry.
     You must ensure that G4 delay times are set in Mach config, if relevant, to same as you use i.e. milliseconds or seconds
     If you enter P3000 and it is not set to milliseconds, you will have a long wait!!!

 D2  The dwell interval at the bottom of the hole.
     Sometimes necessary to ensure exact depth of cut, flat bottom to hole etc.
     Same caveat re G4 delay as above.

 F10 Feed rate for the drilling operation
     You can use either G94 or G95 for feed, the macro does not set feed to one type or the other.
     It looks like a profile will default to G94, unless G95 is set under Mach.
     Just don't get them mixed up!
          
 I20 The X offset for each subsequent hole in pattern
     Only makes sense where L is greater than 1, see below.
     If not used set to 0

 J0  The Y offset for each subsequent hole in pattern
     Only makes sense if a mill and where L is greater than 1, see below.
     If not used set to 0

 L4  The number of holes in the sequence
     The first hole is drilled at X (Y) specified for initial position
     The further 3 holes are drilled at X+20 Y+0 (in this example)
     i.e. This example parameter string will drill 4 holes 20mm apart along the X axis starting at absolute co-ordinates X0 Y10

See examples for more details of usage

RUNNING THE MACROS
*******************

You should always runs the code containing the macros in a simulation first, to ensure it does what you expect.

There can be issues with Mach, where either it ignores the macro completely, or it throws up errors which in reality do not exist.

I found these occuring fairly often whilst developing the macros, especially if I loaded , un-loaded and then re-loaded or edited the .NC files.
Some of it may be down to Mach reading from cache rather than the actual amended program.
The answer in all cases appears to be just shut down the Mach program and re-start it which clears the errors.
Mach 3.4xx lock down version appears a bit improved re this over 2.63, so worth upgrading?

NB.
Even these simple examples may not work if run on an unlicensed copy of Mach 3 
When loading the file, Mach will fail to render an image and lock up
The reason is that the macro can generate hundreds of lines of code depending upon the peck distance and the total distance to be drilled,
exceeding the 500 line limit on the unregistered version.
If you want to evaluate in simulation on an unregistered version, change the peck Q parameter to a 
higher number.


EXAMPLES
*********

If you study the below, very simple, .NC programs, they demonstrate usage of the macros.
Note the syntax, TAB between line number and first co-ordinate or command and a SPACE between each item on the line thereafter
Terminate with CARRAIGE RETURN and comments on next line if required
Much more easily read by humans, let alone the macros!

**********************************************************************
%
; standard lathe example
; drills a single hole at workpiece centre (X0)
; using full retraction peck drilling
; Because G18 is given Y is ignored
; and because L is 1, I and J are ignored, but still must be written
; P parameter to macro call tells it where to find other params
;  Feed of F100 is for simulation only!
;
; Feed is for simulation only
N10	G21 G18 G80 G90 G94 
N20	G0 X0 Z10
N30	M03 S600
N40	G90
N50	;M830 X0 Y0 Z-11 R2 Q2.5 W0.5 P5 D3 F100 I0 J0 L1
N60	M830 P50
N70	G80
N80	G90
N90	G0 X0 Z10
N100	M5
N120	M30
%

*************************************************************

%
; standard lathe example - imperial test M830
; drills a single hole at workpiece centre (X0)
; using full retraction peck drilling
; Because G18 is given Y is ignored
; and because L is 1, I and J are ignored, but still must be written
; P parameter tells macro where to look for other parameters
N10	G20 G18 G80 G90 G94
N20	G0 X0 Z0.5
N30	M03 S600
N40	G90
N50	;M830 X0 Y0 Z-.01 R.01 Q.002 W0.001 P1 D1 F1 I0 J0 L1	
N60	M830 P50
N70	G80
N80	G90
N90	G0 X0 Z0.5
N100	M5
N120	M30
%

****************************************************************

%
; standard lathe example - imperial test M831
; drills 4 holeS along X axis
; using rapid peck drilling
; Because G18 is given Y is ignored
; and because L is 4, I is used and J are ignored, but still must be written
; P parameter tells macro where to look for other parameters
;
N10	G20 G18 G80 G90 G94
N20	G0 X0 Z0.5
N30	M03 S600
N40	G90
N50	;M831 X0 Y0 Z-0.01 R0.01 Q0.002 W0.001 P1 D1 F1 I0.5 J0 L4	
N60	M831 P50
N70	G80
N80	G90
N90	G0 X0 Z0.5
N100	M5
N120	M30
%

********************************************************************

%
; Lathe example - indexing spindle and powered drill
; Drills a single line of 4 holes 20mm apart on X axis
; in full retraction peck drilling mode
; Y and J are set to 0 and later ignored by macro
; P parameter to macro call tells it where to find other params
;  Feed of F100 is for simulation only!
;
N10	G21 G18 G49 G40 G50 G80 G90 G94 
N20	G0 X0 Z20
N30	M03 S600
N40	G90
N50	;M830 X10 Y0 Z-9 R0.5 Q2.5 W0.5 P1 D0 F100 I20 J0 L4
N60	M830 P50
N70	G80
N80	G90
N90	G0 X0 Y0 Z20
N100	M5
N110	M30
%

************************************************************************

%
; Milling example
; Drills a single line of 4 holes 30mm apart on X axis
; in full retraction peck drilling mode
;  Feed of F100 is for simulation only!
;
N10	G21 G17 G49 G40 G50 G80 G90 G94 
N20	G0 X0 Y0 Z20
N30	M03 S600
N40	G90
N50	;M830 X10 Y10 Z-9 R10 Q2.5 W0.5 P3 D0 F100 I30 J0 L4
N60	M830 P50
N70	G80
N80	G90
N90	G0 X0 Y0 Z20
N100	M5
N110	M30
%

*********************************************************************

%
;  Milling example of multiple calls to M830, each drilling multiple holes
;  Drills line of 4 holes at 10mm offsets along x axis
;  repeated twice at 20mm offsets on Y axis to give 3x4 grid of holes
;  Feed of F100 is for simulation only!
;  Each call to M830 gives Nxx as the line where parameters are to be found
;
N10	G21 G17 G49 G40 G50 G80 G90 G94 
N20	G0 X0 Y0 Z20
N30	M03 S600
N40	G90
N50	;M830 X10 Y10 Z-9 R10 Q2.5 W0.5 P1 D1 F100 I10 J0 L4
N60	M830 P50
N70	G80
N80	G90
N85	;M830 X10 Y30 Z-9 R10 Q2.5 W0.5 P1 D1 F100 I10 J0 L4
N90	M830 P85
N100	G80
N110	G90
N115	;M830 X10 Y50 Z-9 R10 Q2.5 W0.5 P1 D1 F100 I10 J0 L4
N120	M830 P115
N130	G80
N140	G90
N150	G0 X0 Y0 Z20
N160	M5
N170	M30
%

*******************************************************************************

%
;  Milling example of multiple calls to M831, each drilling multiple holes
;  in rapid peck mode
;  Drills line of 4 holes at 10mm offsets along x axis
;  repeated twice at 20mm offsets on Y axis to give 3x4 grid of holes
;  Feed of F100 is for simulation only!
;  As each call to macro uses same parameters, each refers to N50
;
N10	G21 G17 G49 G40 G50 G80 G90 G94 
N20	G0 X0 Y0 Z20
N30	M03 S600
N40	G90
N50	;M831 X10 Y10 Z-9 R10 Q2.5 W0.5 P3 D1 F100 I10 J0 L4
N60	M831 P50
N70	G80
N80	G90
N85	;M831 X10 Y30 Z-9 R10 Q2.5 W0.5 P3 D1 F100 I10 J0 L4
N90	M831 P85
N100	G80
N110	G90
N115	;M831 X10 Y50 Z-9 R10 Q2.5 W0.5 P3 D1 F100 I10 J0 L4
N120	M831 P115
N130	G80
N140	G90
N150	G0 X0 Y0 Z20
N160	M5
N170	M30
%

***********************************************************************************

%
; Milling example
; Drills a single diagonal line of 4 holes starting at X10 Y10
; with each subsequent hole incremented in a plane X+20 Y+20
; in rapid-peck retraction drilling mode
;  Feed of F100 is for simulation only!
;
N10	G21 G17 G49 G40 G50 G80 G90 G94 
N20	G0 X0 Y0 Z20
N30	M03 S600
N40	G90
N50	;M831 X10 Y10 Z-9 R10 Q2.5 W0.5 P3 D0 F100 I20 J20 L4
N60	M831 P50
N70	G80
N80	G90
N90	G0 X0 Y0 Z20
N100	M5
N110	M30
%

All example files in ZIP
