Lathe Roughing and Finishing Macros for Mach3
**********************************************

(c) Mick Grant 2008 - 2009

CONTENTS
*********
This package contains a set of macros to carry out roughing and finish profiling on a CNC Lathe
It attempts to address the deficiencies of Mach3 regards the lack of G71, G72 and G73 GCode functions and
to provide an internal boring and profiling function.

Contained are macros
M71.m1s		External profile roughing and finishing (decreasing X value)
M72.m1s		External rough facing,profiling and finishing  (decreasing Z value)
M77.m1s		Internal rough boring/ profiling and facing   (increasing X value)

All 3 should be placed in C:\Mach3\macros\_name_of_your_turning_profile
(this may still be Mach3_Turn or you may have renamed it)

IMPORTANT
**********

These macros assume standard set up procedures are followed and that X and Z origin points on the workpiece are standard.
ie.
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.


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

All 3 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)	 All 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 ;M7? 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. 	......
	;M71 I0.1 K0.1 D0.25 F140 B55 L11 V56
    	M71 P60 Q90
	......

This is because Mach3 can only pass 3 parameters from the Macro commandline, P Q and S
S is unusable until version 3.42xx and beyond, because it just causes Mach to change spindle speed

So we embed the other parameters we require in the .NC file, commented out so that they do not affect prior operations
eg.
 I0.1 (X finish allowance) 
 K0.1 (Z finish allowance)  
 D0.25 (depth of each cut) 
 F140 (roughing feed )
 B55 (billet diameter or start diameter for M77) 
 L11 (number of lines in the code loop between P and Q) This is a sanity check to prevent horrible errors
 V56 (stop line - 
      In M71 a X value above which tool will not retract, thus shortening fresh air cutting in initial stages.
      In M72 it is the limit of Z retraction from face of workpiece       
      In M77 this is an X value which governs the minimum X value used for retraction, to prevent boring bar fouling other side of bore
      If it is set to 0 , then no stop line is in use - see examples)

USAGE
******
All the macros work by taking a code loop defined in parameters P and Q.
This sets out a tool path which will produce the finished profile required.

The macro then takes the billet diameter (or start diameter in the case of M77), depth of cut and finishing allowances and works out how many loops of the code are required to get from the outside (or inside - M77) diameter of the workpiece to the required profile, plus finishing allowance.

It then runs the loop repeatedly, substituting X or Z values with ones which gradually move the tool into the workpiece, cutting the profile required.

When complete, the macro quits and Mach will follow the loop for a final time, at the finished profile dimensions, and thus performs the finishing cut.

It is important that the loop you program in, starts and ends at safe stand off positions, in order that when the loop is repeated, it does not make inadvertant cuts on its path.
This will be clearer from the examples.


SPEED / FEED
**************
The speed and feed in first line of loop, will be ignored by roughing cycle 
and are the figures you require for FINISHING only
You should set the speed required for roughing prior to calling the M7? macro

On leaving the M7? macro, speed and feed from first line of loop are implemented and the final finishing cut taken

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 and goes straight to the finishing loop, 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 generates hundreds of lines of code moving from billet diameter down to tool path diameter
and  this generally exceeds the 500 line limit on the unregistered version.
If you want to evaluate in simulation on an unregistered version, change the depth of cut D parameter to a 
higher number like 1 or 2.
This will give far fewer loops and allow the image to be rendered and cut in simulation, even though the depth of cut
would be far too high for actual cutting

WRINKLES AND BUGS
******************

There appears to be an issue with Mach 1.84 not setting flags called OEMLeds correctly to show whether the lathe is in Metric or Imperial unit mode.

This emerged when the macros were tested by Dave Algie in USA who uses Imperial units under 1.84.  He stated that he uses 1.84 because subsequent versions do not cut threads properly.

These macros work perfectly under Mach 2.63, detecting which unit mode is being used, so it appears there is an inconsistancy of interface in earlier Mach from that published.

I have just bypassed this problem by including specific metric and imperial versions for use with Mach 1.84 only.
(Note - the examples files are all metric, if you want to run them in imperial set up, you will need to substitute the X and Z values for something meaningful in inches
I deliberately excluded the examples from the Imperial folders)

I have now tested on the new lockdown version 3.4xx something
They work on this version and the image rendering and VB debugging appear improved, so I have upgraged to this version.


EXAMPLES
*********

If you study the below, very simple, .NC programs, they demonstrate usage of the 3 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!

**************************************************************************************************************
 M71 cuts 3 steps with chamfered edges in ascending diameter
**************************************************************************************************************
%
N10	G90 G94
N20	G21 G18
N21	G40
N30	F600 
N40	G28					
; safe stand off pos - ie home
N41	M6 T0101				
; optional tool change or check is correct one
N50	M4 S1400	 			
; speed to use for roughing loop 
N51	;M71 I0.1 K0.1 D0.25 F140 B55 L11 V56	
; V56 ensures retracts only 1 mm above billet diameter to cut down retractions
N55 	M71 P60 Q140    			
; call M71 to loop between N60 and N140
N60	X55 Z0.1 F70 S2000 			
; start of loop - speed and feed used for finishing run only - note standing off from billet
N61	X33 Z0.1 
N80	X35 Z-2
N90	Z-30
N100	X43 
N101	X45 Z-32
N110	Z-40
N111	X48
N120	X50 Z-42
N130	Z-45
N140	X55 Z-43				
; end of loop - stand clear of part before return
N370	M5 G28					
; stop spindle - go to safe stand off pos
N380 	M30
%


*************************************************************************************************************
; M71 - The below cuts steps again but with radiuses between to show it works with G3 calls
  Note this program uses a V parameter set to 0, see next example for explanation
**************************************************************************************************************

%
N10	G90 G94
N20	G21 G18
N21	G40
N30	F300
N40	G28	 				
; safe stand off pos ie home
N41	M6 T0101				
; optional tool change or check is correct one
N50	M4 S1400	 			
; speed to use for roughing loop 
N51	;M71 I0.1 K0.1 D0.25 F140 B25.5 L7 V0
; dont use stopline with G2 or G3, set V to 0 and it will be ignored
N55 	M71 P250 Q301
N250	G0 X26 Z0.1 S2000 F70			
; speed and feed will only be used for finishing - note standing off from billet
N260	G0 X11
N270	G01 Z-13
N280	G03 X17 Z-16 I1.5 K1.5
N290	G01 Z-28.4
N300	G03 X25 Z-32.4 12 K2
N301	G01 X26 Z0.1				
; end of loop - stand clear of part before return
N310	G28
N370	M5
N380 	M30
%

****************************************************************************************************************
; M71 - This one cuts a ball top finial on a bar using G2 and G3 calls

  Note this program uses a V parameter set to 0
  This is because of the G2 call for the main radius of the ball end
  The co-ordinates passed to G2 are points of origin and turning points for the arc, not absolute co-ordinates
  G2 will calculate and generate the X and Z movements required to achieve this arc, many of which will go outside the stop line
  at early stages of cutting
  It makes no sense to truncate the X values passed to G2 to a stop line setting and doing so will produce a distorted profile

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

%
N10	G90 G94
N20	G21 G18
N21	G40
N30	F600 
N40	G28 
N45	M6 T0101				
; optional tool change or check is correct one
N50	M4 S1200	 			
; speed and feed to use for roughing loop 
N51	;M71 I0.1 K0.1 D0.15 F150 B28.54 L4 V0
N55 	M71 P60 Q91
N60	G0 X29 Z0 F75 S1600 			
; start of loop - speed and feed used for finishing run only
N61	G01 X-0.1 
N90	G2 X21 Z-21 I0 K-14		
N91	G3 X29 Z-28 I+3.5 K0
N370	G28 				  	
; - go to safe stand off pos - ie home
N371	M5     		        		
; stop spindle 
N380 	M30
%



**********************************************************************************************************
 M72 - Faces off with a 10mm radius
 
 Shows that M72 can do profiles in terms of Z axis as well as straight facing cuts
 Reduces billet length by 11.27mm and cuts 10mm radius to end

 Note V parameter is set to +1 because it relates to Z stop line not X
**********************************************************************************************************

%
N10	G90 G94
N20	G21 G18
N21	G40
N30	F600 
N40	G28 
N45	M6 T0101				
; optional tool change or check is correct one
N50	M4 S1400	 			
; speed and feed to use for roughing loop 
N51	;M72 I0.1 K0.1 D0.25 F140 B55 L5 V+1	
; in M72 V is a Z value telling it not to retract further than 1mm off face of billet
N55 	M72 P60 Q100    		        
; call M72 to loop between N60 and N80
N60	G0 X56 Z-11.27 F70 S2000 		
; start of loop - speed and feed used for finishing run only - stand clear of part 
N61	G01 X-0.5 
N80	G0 X45
N90	G2 X55 Z-21.27 R-10		
N100	G01 X56
N370	G28 
N371	M5     		        		; stop spindle - go to safe stand off pos
N380 	M30 
%


**************************************************************************************************************
M77 - Cuts a straight 20mm x 22mm internal bore in end of billet

Feed values are deliberately high so that a simulation does not take all day.
Reduce as required if actually cutting metal

(Assumes that billet has already been pre-drilled with 15mm drill to provide clearance hole for boring bar)

Note V parameter is less than B(start diameter) and is set to prevent bar scraping back of bore on retraction
**************************************************************************************************************

%
N10	G90 G94
N20	G21 G18
N21	G40
N30	F600 					
; feed for G28 move to Home only
N40	G28				
N41	M6 T0202				
; select the boring bar
N50	M4 S600	 				
; start spindle at speed for roughing out
N51	;M77 I0.1 K0.1 D0.15 F140 B15 L4 V14	
; parameters for M77, B governs the initial X value so MUST be correct - see above
N55 	M77 P60 Q81    				
; M77 call giving start and finish lines of loop
N60	G0 X20 Z0.1 F70 S1200 			
; Finished X value, Z stood off billet, feed and speed for finishing cut only
N61	G01 Z-22 				
; take cut
N80	X19					
; reduce X value for clearence in retraction of bar
N81	G0 Z+1					
; retract bar outside workpiece, ready for next iteration of loop
N370	M5 G28					
; all stop - go to Home
N380 	M30
%


