% CIRCPOCKET.TAP

( SET PARAMETERS- Transfer this bit to your main program then run this subroutine )

% #001= 010     Enter tool number 
% #002= 5.0     Enter tool diameter 
% #003= 100     Enter non cutting feedrate 
% #004= 050     Enter cutting feedrate 
% #005= 025     Enter finishing feedrate 
% #006= 0.5     Enter cutting depth per pass 
% #007= 0.1     Enter pocket depth finishing cut size 
% #008= 2000    Enter spindle speed
% #009= 100     Enter pocket diameter
% #010= 100     Enter % cutter overlap
% #011= 5.0     Enter Z axis pocket depth 
% #012= 10.0    Enter x axis starting centre 
% #013= 10.0    Enter y axis starting centre 
% #014= 0.0     Enter z axis starting height 
% #015= 2.0     Enter z axis safe start and end position
% #016= 0.1     Enter finishing cut size for diameter  

( CALCULATE PARAMETERS FOR PROGRAM EXECUTION )

#050= #012                                           ( circle pocket centre x pos )
#051= #013                                           ( circle pocket centre y pos )
#052= [[#002 *[[100-#010] /100]] / 2]                ( x centre of upper semi-circular cuts )
#053= #012                                           ( x centr of lower semi-circular cuts )
#054= [#002 *[[100-#010] /100]]                      ( distance between cut paths )
#055= [#009 - #016]                                  ( pocket diameter for current calcs )
#056= [#002 + #054]                                  ( amount of stock removed first cut )      
#057= 0                                              ( current number of arcs cut )
#058= 0                                              ( store for last x position ) 
#059= [#050+[[#002*[[100-#010]/100]]+[#002/2]]]      ( x axis position for first cut)
#060= #059                                           ( x position to go to this cut )
#061= 0                                              ( spare )
#062= [#011 - #007]                                  ( z axis travel minus finish cut )
#063= FIX[#062/#006]                                 ( number of full z axis moves down )
#064= [#062-[#063*#006]]                             ( Remainder of z axis full passes )
#065= #006                                           ( set depth of cut )
#066= #003                                           ( current axis feedrate )
#067= 0                                              ( spare )
#068= 0                                              ( last arc to be cut 0=lower 1= upper )
#069= 0                                              ( x centre of arc for last cut upper or lower )
#070= 0                                              ( calculated position of X move )
#071= 0                                              ( calculated position of Y move )
#072= 0                                              ( lower arcs cut counter )
#073= 0                                              ( upper arcs cut counter )
#074= #014                                           ( calculated z position for diameter finish cuts )
#075= FIX [[[ #055 / 2]-#059] /#054]                 ( number of cuts on RHS of pocket)
#076= FIX [[[ #055 / 2]-[#002 / 2]] /#054]           ( number of cuts on LHS of pocket)
#077= [#075 + #076]                                  ( number of 180 degree arcs to cut )
#078= FUP [#077 / 2]                                 ( half the number of 180 degree cuts, no of times to run sub 21)

#102= 0                                              ( decision making parameter )
#103= 0                                              ( decision making parameter )
#104= 0                                              ( decision making parameter )


( START THE PROGRAM )

G40
M06 T#001                                            ( select tool )
M03 S #008                                           ( start spindle )
M08                                                  ( start coolant )
G01 F #066 Z #015                                    ( move z to safe height )

G01 F #066 x 0.0 Y 0.0                               ( move x and y to machine zero )
G01 F #066 x #050 Y #051                             ( move x and y to start position )

g42                                                  ( turn on cutter compensation )
#070 = [#050 - [#002/2]]
#071 = 0
G01 x #070 y#071                                     ( move axis to start position )

G01 F #066 Z #014                                    ( move z to material top position )

#077 = [#075 + #076]                                 ( number of 180 degree arcs to cut )
#057= 0                                              ( current number of arcs cut )
M98 P001 L #063                                      ( do cuts to achieve final depth less final and finish cuts )


% is there a final cut after all the fixed depth of cuts are done

#102 = #064                                          ( argument for deciding which subroutine to run )
#103 = [#102 + ABS[#102]]                            ( if 102 is-ve or 0 #103 = 0 if 102 +ve #103 is positive number )
#104 = [1 XOR #103]                                  ( if #103 = 0 #104 = 1 if 103 not zero #104 = 1 )

M98 P[#104 + 8]                                      ( if #104 = 0 run sub 8 if #104= 1 run sub 9 )


% is there a finishing cut to apply to depth

#102 = #007                                          ( argument for deciding which subroutine to run )
#103 = [#102 + ABS[#102]]                            ( if 102 is-ve or 0 #103 = 0 if 102 +ve #103 is positive number )
#104 = [1 XOR #103]                                  ( if #103 = 0 #104 = 1 if 103 not zero #104 = 1 )

M98 P[#104 + 10]                                     ( if #104 = 0 run sub 10 if #104= 1 run sub 11 )

% is there a finishing cut to apply to diameter

#102 = #016                                          ( argument for deciding which subroutine to run )
#103 = [#102 + ABS[#102]]                            ( if 102 is-ve or 0 #103 = 0 if 102 +ve #103 is positive number )
#104 = [1 XOR #103]                                  ( if #103 = 0 #104 = 1 if 103 not zero #104 = 1 )
  
M98 P[#104 + 12]                                     ( if #104 = 0 run sub 12 if #104= 1 run sub 13 )

g40
#066 = #003                                          ( turn off cutter compensation )
G01 F #066 Z #015                                    ( move z to start position )
G01 F#066 x #050 y #051                              ( move x & y to start position )
M09                                                  ( stop coolant )
M05                                                  ( stop spindle )

M99                                                  ( end program )


%============================================================================================================

O001
( Move back to start x & y then do z down and do complete pocket )

#066 = #003                                          ( set feedrate )
G01 F #066 x #050 Y #051                             ( move x and y to start position )


#066 = #004                                          ( set feedrate )
G91                                                  ( incremental mode )
G01  z [#065*-1] F#066                               ( move z axis down )
G90                                                  ( back to absolute move )
#057= 0                                              ( reset current number of arcs cut for next pass )
#072= 0                                              ( reset lower cut counter for next pass )
#073= 0                                              ( reset upper cut counter for next pass )
M98 P002                                             ( machine 1 complete pass of pocket )

M99

%============================================================================================================

O002
( decisions for cutting a pocket )


% can the cutter + the % overlap fit within the pocket

#102 = [#055 - [#002 + #054]]                        ( argument for deciding which subroutine to run ) 
#103 = [#102 + ABS[ #102] ]                          ( if 102 is-ve or 0 #103 = 0 if 102 +ve #103 is positive number )
#104 = [1 XOR #103]                                  ( if #103 = 0 #104 = 1 if 103 not zero #104 = 1 )

M98 P[#104 + 3]                                      ( if #104 = 0 run sub 3 if #104= 1 run sub 4 )

% are successive cuts required

#102 = [#077 - #057]                                 ( argument for deciding which subroutine to run )
#103 = [#102 + ABS[#102]]                            ( if 102 is-ve or 0 #103 = 0 if 102 +ve #103 is positive number )
#104 = [1 XOR #103]                                 ( if #103 = 0 #104 = 1 if 103 not zero #104 = 1 )

M98 P[#104 + 20] L #078                              ( if #104 = 0 run sub 20 if #104= 1 run sub 21 )


% when you get to here the cutter should be on the pocket edge

#102 = #068                                          ( argument for deciding which subroutine to run )
#103 = [#102 + ABS[#102]]                            ( if 102 is-ve or 0 #103 = 0 if 102 +ve #103 is positive number )
#104 = [1 XOR #103]                                  ( if #103 = 0 #104 = 1 if 103 not zero #104 = 1 )

M98 P[#104 + 6]                                      ( if #104 = 0 run sub 6 if #104= 1 run sub 7 )

M99

%============================================================================================================

O003
( first 180 degree cut subroutine )


F #066                                               ( set the cutting feedrate )
#060 = [ [#002 / 2] * -1 ]                           ( assign the end position of the x axis )
G01 x #060 Y #051
#060 = #059                                          ( assign the end position of the x axis )
G02 X #060 Y #051 I #052 J0.0                          ( cut the first arc )

M99

%============================================================================================================

O004
( cutter too big to cut the hole )

M01                                                  ( STOP PROGRAM HERE )

M99

%============================================================================================================

O006
( full 360 degree cleanup of pocket from lh side of pocket )

#060 = [ #050 - [ #055 / 2 ]]                        ( calculate the destination x position )
F #066
G02 X #060 Y0.0  I 0.0 J 0.0
G40 
G01 F #003 x #050 Y #051                             ( move x and y to start position )  

M99

%============================================================================================================

O007
( full 360 degree cleanup of pocket from rh side of pocket )

#060 = [ #050 + [ #055 / 2 ]]                        ( calculate the destination x position )
F #066                                               ( set feedrate )
G02 X#060 Y 0.0  I 0.0 J 0.0                         ( do the move )
g40
G01 F #003 x #050 Y #051                             ( move x and y to start position )

M99

%============================================================================================================

O008
( move the z axis to its final cut position and cut the pocket )

#065= #064                                           ( set z move remainder of full cut depth )
#075= FIX [[[ #055 / 2]-#059] /#054]                 ( number of cuts on RHS of pocket)
#076= FIX [[[ #055 / 2]-[#002 / 2]] /#054]           ( number of cuts on LHS of pocket)
#077 = [#075 + #076]                                 ( number of 180 degree arcs to cut )
#057= 0                                              ( reset current number of arcs cut for next pass )
#072= 0                                              ( reset lower cut counter for next pass )
#073= 0                                              ( reset upper cut counter for next pass )
M98 P001                                             ( do single cut of pocket )

M99

%============================================================================================================

O009
( do nothing here )

M99

%============================================================================================================

O010
( move the z axis to its finish cut position and cut pocket )

#065= #007                                           ( set z move final cut depth )

#057= 0                                              ( reset current number of arcs cut for next pass )
#072= 0                                              ( reset lower cut counter for next pass )
#073= 0                                              ( reset upper cut counter for next pass )
#066= #005                                           ( set final cut feedrate )
M98 P001                                             ( do single cut of pocket )

M99

%============================================================================================================

O011
( do nothing here )

M99

%============================================================================================================

O012
( return and do bore finishing cut if required )

G01 F #066 Z #015                                    ( move z axis to safe height )
#055 = #009                                          ( set cut diameter to pocket size )
#075= FIX [[[ #055 / 2]-#059] /#054]                 ( number of cuts on RHS of pocket)
#076= FIX [[[ #055 / 2]-[#002 / 2]] /#054]           ( number of cuts on LHS of pocket)
#077= [#075 + #076]                                  ( number of 180 degree arcs to cut )
G01 F #066                                           ( set cutting feedrate )
% g42                                                  ( turn on cutter compensation )
#070 = [#050 - [#055 / 2]]                           ( calculate cutter to edge of pocket )
G01 F #066 X #070 Y #051                             ( move cutter to edge of pocket )
G01 F #066 Z #014                                    ( move cutter down to job)

M98 P014 L #063                                      ( do the cuts to achieve final depth less finish cuts )

#074 = [#062 * -1]                                   ( calculate the final z axis depth position )
G02 F #066 X #070 Y #051 I #050 J #051 Z #074        ( do helical cut of the bore to final depth )

#074 = [#011 * -1]                                   ( calculate the finish z axis depth position )
G02 F #066 X #070 Y #051 I #050 J #051 Z #074        ( do helical cuts of the bore to finish size )
G02 F #066 X #070 Y #051 I #050 J #051               ( do clean up cut of the bore to finish size )

#074 = #062                                          ( calc z position to pull back of bottom of bore )
#066 = #003                                          ( set feedrate )
G01 F #066 X #050 Y #051 Z #074                      ( pull back from wall and base of pocket )
G01 F #066 Z #015                                    ( move to end position ) 

M99

%============================================================================================================

O013
( do nothing here )

M99

%============================================================================================================

O014
( do the diameter finishing cuts )

#070 = [#050 - [#055 / 2]]                           ( edge of pocket position )
#074 = [#074 - #006]                                 ( calculate the next z axis depth position )
G02 F #066 X #070 Y #051 I #050 J #051 Z #074        ( do helical cuts of the bore to finish size )

M99

%============================================================================================================

O020
( subsequent regular 180 degree cuts )

% are there any more arcs to cut

#102 = [#077 - #057]                                 ( argument for deciding which subroutine to run )
#103 = [#102 + ABS[#102]]                            ( if 102 is-ve or 0 #103 = 0 if 102 +ve #103 is positive number )
#104 = [1 XOR #103]                                  ( if #103 = 0 #104 = 1 if 103 not zero #104 = 1 )
  
M98 P[#104 + 24 ]                                    ( if #104 = 0 run sub 24 if #104= 1 run sub 25 )

% are there any more arcs to cut

#102 = [#077 - #057]                                 ( argument for deciding which subroutine to run )
#103 = [#102 + ABS[#102]]                            ( if 102 is-ve or 0 #103 = 0 if 102 +ve #103 is positive number )
#104 = [1 XOR #103]                                  ( if #103 = 0 #104 = 1 if 103 not zero #104 = 1 )

M98 P[#104 + 26]                                     ( if #104 = 0 run sub 26 if #104= 1 run sub 27 )

M99

%============================================================================================================

O021
( do nothing here )

M99

%============================================================================================================

O024
( cut the next lower arc )
% calculate the next end point

#068 = 0                                             ( last arc cut was lower lower = 0 upper = 1 )
#060 = [#050-[#002 / 2] - [ #054* [ #072 + 1 ] ]]    ( calculate the destination x position )
#058 = #060                                          ( remember x position for later calcs )
F #066                                               ( set feedrate )
G02 X #060 Y 0.0 I #053 J0.0                         ( do the cut )
#072 = [#072 + 1]                                    ( increase number of lower arcs cut )
#057 = [#057 + 1]                                    ( increase number of total arcs cut )
  
M99

%============================================================================================================

O025
( decide if cutter is on outside size or oddball cut is needer )

% work out if an oddball cut is remaining and do 180 degree lower cut

#102 = [#055 - #056 - [#077 * #054]]                 ( argument for deciding which subroutine to run )
#103 = [#102 + ABS[#102]]                            ( if 102 is-ve or 0 #103 = 0 if 102 +ve #103 is positive number )
#104 = [1 XOR #103]                                  ( if #103 = 0 #104 = 1 if 103 not zero #104 = 1 )

M98 P[#104 + 28]                                     ( if #104 = 0 run sub 28 if #104= 1 run sub 29 )

M99

%============================================================================================================

O026
( cut the next upper arc )
% calculate the next end point

#068 = 1                                             ( last arc cut was lower lower = 0 upper = 1 )
#060 = [#059 + [ #054* [ #073 + 1 ]]]                ( calculate the destination x position )
#058 = #060                                          ( remember x position for later calcs )
F #066                                               ( set the feedrate )
G02 X #060 Y 0.0 I #052 J0.0                         ( do the cut )
#073 = [#073 + 1]                                    ( increase number of upper arcs cut )
#057 = [#057 + 1]                                    ( increase number of total arcs cut )

M99

%============================================================================================================

O027
( decide if cutter is on outside size or oddball cut is needer )

% work out if an oddball cut is remaining and do 180 degree upper cut

#102 = [#055 - #056 - [#077 * #054]]                 ( argument for deciding which subroutine to run )
#103 = [#102 + ABS[#102]]                            ( if 102 is-ve or 0 #103 = 0 if 102 +ve #103 is positive number )
#104 = [1 XOR #103]                                  ( if #103 = 0 #104 = 1 if 103 not zero #104 = 1 )

M98 P[#104 + 30]                                     ( if #104 = 0 run sub 30 if #104= 1 run sub 31 )
M99

%============================================================================================================

O028
( cut the last lower arc to bring cutter to outside diameter )


#060 = [ #050 - [ #055 / 2 ]]                        ( calculate the destination x position )
#069 = [[#060 - [#058*-1]] / 2 ]                     ( calculate centre for this move )
#058 = #060                                          ( remember x position for later calcs )
F #066                                               ( set feedrate )
G02 X #060 Y 0.0 I #069 J0.0                         ( do the cut )

M99

%============================================================================================================

O029
( do nothing here )

M99

%============================================================================================================

O030
( cut the last upper arc to bring cutter to outside diameter )

#060 = [ #050 + [ #055 / 2 ]]                        ( calculate the destination x position )
#069 = [[#060 - [#058*-1]] / 2 ]                     ( calculate centre for this move )
#058 = #060                                          ( remember x position for later calcs )
F #066                                               ( set the feedrate )
G02 X #060 Y 0.0 I #069 J0.0                         ( do the cut )

M99

%============================================================================================================

O031
( do nothing here )

M99

%============================================================================================================

M99                ( end program )

%============================================================================================================