Posted by: davidhayden | August 20, 2011

Use Sub Programming to make your own canned cycles


Canned Cycles used in NC programming are great time savers, but often don’t do exactly what we want them to.

Gopal posted a great question today about the limitations of the G83 canned cycle.  Here is what he had to say after reading Proven Tips and Tactics: Understanding Canned Cycles.

[ I need to drill 200 holes of Diameter 4.0mm to a depth of 50mm (Z-50.0) and then continue Diameter 3.0mm to a depth of 110.0mm (Z-110.0).

There is no problem for drilling Dia 4.0 upto Z-50.0 wirh a G83 code. But the Diameter 3.0mm hole, I want to go rapid upto Z-50.0 (because already drilled Dia 4.0 upto this depth) and then peck 2mm and clear the chip at Z3.0 and continue 2mm peck and clear chips at Z3.0 until Z-110. reaches. Is there any way I can do this in Fanuc or Sinumerik? If I give the R value 3.0, The drilling cycle starts from Z3 and lot of time wastage. If I give the R value -50.0, The chip is clearing at z-50 and not Z3. Any solution for this? ] – Gopal

    That is an interesting situation and one that can be solved, not with canned cycles, but with sub programming.

Sub programming techniques allow us to creat our own CNC canned cycles.  While they may take a little effort to create in the beginning, once created they can be use over and over. 

So what are CNC sub programs and how do you use them?

Sub-program are similar to any other NC / CNC program except they are usually more simple.  Generally, sub-programs will only be a series of moves or positions.

The significant difference in a sub-program is that the end code will be an M99.  The M99 commands the control to return to program from which the sub-program was called.

When you want to call up a sub-program, use the command M98P(xxxx).  M98 commands the control to leave the current program and complete the instructions in the sub program specified by the P-code.

THE FOLLOWING IS A DIAGRAM OF THE SUB-PROGRAM PROCESS

CNC Sub Programming Example

From 7 Easy Steps to CNC Programming P.48

To interpret this diagram start at the beginning.  As you read down you see at line n30 the program calls, with an M98, the sub program 80.  Moving through program 80 at line n15 the sub-program 85 is called.  Program 85 runs down to the M99 which returns control to program 80.  Program 80 continues processing until it reaches its M99 command.  This M99 returns control to the main program. 

From here the main program continues until it gets to line n70 where the control is once again passed to programs 80 and 85.  After programs 80 and 85 finish their processing the control is returned to the main program which continues until it reaches the end of the program at the M30.

You see from the example above, CNC controls allow sub programs to be nested.   This can be very useful too.

So how could we use CNC sub programming to address Gopal’s Question?

Starting the main program, it could be structured something like this?

MAIN CNC PROGRAM   750

NOTE: THIS CODE IS FOR REFERENCE ONLY!  IT HAS NOT BEEN TESTED FOR ERRORS

O750 (main program)
N005 (these are the )
N010 (typical start up)
N015 (blocks you normally)
N020 (to prepare the machine)
N025 G0 X  Y (position to first hole)
N030 G43/G44 Z3.0 (rapid to Z using your offset mode)
N035 M98 P751 (call sub program 751)
N040 G0 X Y (move to new position)
N045 M98 P751 (call sub program 751 again)
N050 G0 X Y (repeat until all holes complete)
N055 ……..
N060 ………
N065 G0 G28 G91 Z0 M9 (safely return to Z home)
N070 G28 X0 Y0 M5 (return to tool change position)
N075 M0 M1 / M30 / M2 – (however you want to terminate the process)

Notice this Main CNC program only sets the machine in motion and moves to the location of the first hole.  The main program does not do any drilling in this example.  The actual drilling will be accomplished by the sub program shown below.  

It’s a little long, but in a few lines you will see the pattern of what the CNC sub program doing.   After the first few lines, you will see that the only change is in the drilling depth of each pass.

CNC SUB PROGRAM 751  

NOTE: THIS CODE IS FOR REFERENCE ONLY!  IT HAS NOT BEEN TESTED FOR ERRORS

O751 (sub program to do drilling)
Z-50. (rapids to Z-50.0 to save time)
G1 Z-52.0 Fxxx  (Drills to depth at desired feed rate)
G0 Z3.0  (rapids to Z3.0 to clear chips)
Z-50.0  (rapids to Z-50.0 to save time)
G1 Z-54.0
G0 Z3.0
Z-50.0
G1 Z-56.0
G0 Z3.0
Z-50.0
G1 Z-58.0
G0 Z3.0
Z-50.0
G1 Z-60.0
G0 Z3.0
Z-50.0
G1 Z-62.0
G0 Z3.0
Z-50.0
G1 Z-64.0
G0 Z3.0
Z-50.0
G1 Z-66.0
G0 Z3.0

NOTE: THIS CODE IS FOR REFERENCE ONLY!  IT HAS NOT BEEN TESTED FOR ERRORS
Z-50.0
G1 Z-68.0
G0 Z3.0
Z-50.0
G1 Z-70.0
G0 Z3.0
Z-50.0
G1 Z-72.0
G0 Z3.0
Z-50.0
G1 Z-74.0
G0 Z3.0
Z-50.0
G1 Z-76.0
G0 Z3.0
Z-50.0
G1 Z-78.0
G0 Z3.0
Z-50.0
G1 Z-78.0
G0 Z3.0
Z-50.0
G1 Z-80.0
G0 Z3.0
Z-50.0
G1 Z-82.0
G0 Z3.0
Z-50.0
G1 Z-84.0
G0 Z3.0
Z-50.0
G1 Z-86.0
G0 Z3.0
Z-50.0
G1 Z-88.0
G0 Z3.0
Z-50.0
G1 Z-90.0
G0 Z3.0
Z-50.0
G1 Z-92.0
G0 Z3.0
Z-50.0
G1 Z-94.0
G0 Z3.0
Z-50.0
G1 Z-96.0
G0 Z3.0
Z-50.0
G1 Z-98.0
G0 Z3.0
Z-50.0
G1 Z-100.0
G0 Z3.0
Z-50.0
G1 Z-102.0
G0 Z3.0
Z-50.0
G1 Z-104.0
G0 Z3.0
Z-50.0
G1 Z-106.0
G0 Z3.0
Z-50.0
G1 Z-108.0
G0 Z3.0
Z-50.0
G1 Z-110.0
G0 Z3.0
M99 (retuns control to the main program where it left off)

 The CNC sub program may look a little intimidating to write but it wasn’t.  Once I had the structure correct for the first few lines, I cut-n-paste the rest of the program.   All I had to do after that was to edit the lines for the drilling depth.  All other lines stayed the same.

So How is the CNC sub program reusable like canned cycles?

If your CNC control is capable of sub programming, and most controls are these days, any NC program can call any available sub program.  So, in this case, any program that has the line
    M98 P751
will call this exact sub program.

But wait, isn’t this useful only if I want to drill these exact holes in some other part?

Yes and no.  That is true if you don’t edit the code.   But once you have made a template for a sub program, you can reuse the structure but change the start, stop, clearance points and so on.  Once you have made the new sub program simply save it as a different number.

A good strategy is to reserve a set of numbers for your sub programs.   For example, you might store all your sub programs in the 5000 series of numbers.  Your first sub program would be 5000, the next 5001 and so on.

You can even get more creative with your numbering.   Sub programs 5000-5099 could be drilling sub programs.   Sub programs 5100-5199 could be sub programs to mill pockets.  Sub programs 5200-5299 could be sub programs for some other repetitive features, and so on.

Sub programs are not just for drilling.

You probably realized from the creative numbering examples above, that sub programs are not limited to drilling routines.   Sub programs can be used for any feature or shape, or for changing pallets, indexing table, or whatever.   Any repetitive CNC commands you do can be put in a sub program.

Great question Gopal, thanks for asking.

Advertisements

Responses

  1. Interesting…

  2. Hi my name is vaughan. I would like to knw if there is an easier way of programming a sub program for counterbore for eg.
    When we write a program we go G1 Z-1 F500; M98 PO1234; Z-2 ; M98 PO1234; Z-3;M98 PO1234 ______Z-40.;M98PO1234. Is ther a better way of doin that till it comes to minus 40 instead of writing it. Line by line. It takes to long. Please advice me

    • I assume you are talking about circular interpolation of a counterbore and not using the canned cycle available for counter boring.
      I would have to know more what you are trying to do, but one way to shorten macro programs is to take advnatage of incremental programming and looping. Using the L command and incremental the tool will go Z-1 and since using incremental, the tool will Feed down another Z1 each time it loops so you have the original feed and 3 loops to get to Z-4

      Go to Counterbore location
      Rapid to clearance above the part
      feed to face of the part or start of the counterbore
      Call sub P1234 L3
      G90 then Z back to clearance location
      move to next hole and call the sub again

      Sub O1234
      G91 Z-1 F500
      X or y move to get out to Counterbore size
      G2 (I or J as applicable)
      G1 x to original location
      M99


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: