Hello Guest it is April 19, 2024, 11:28:25 AM

Author Topic: Subroutine confusion.  (Read 7472 times)

0 Members and 1 Guest are viewing this topic.

Offline Fastest1

*
  •  920 920
  • Houston, TX
    • View Profile
Subroutine confusion.
« on: October 02, 2010, 06:43:10 PM »
So I have never really understood the term ( i thought I did but a few words seem very similar here). When using an M98 to call a subroutine like this
M98 P00001 L10 (doesnt this mean to call up a subroutine #1? And loop it 10 times?)

Is this subroutine a defined amount set up prior? If yes where are they stored? I am ass/u/me/ing they must be set up at an earlier time and stored numerically.  I notice that generally when they are called, lower in the code it will give a definiition of what that sub is doing, is that like a local control you can edit on the fly? I mean by editing say the depth in the description of the sub at the bottom?
I want to die in my sleep like my grandfather, not like the passengers in the car! :-)

Offline Graham Waterworth

*
  • *
  •  2,672 2,672
  • Yorkshire Dales, England
    • View Profile
Re: Subroutine confusion.
« Reply #1 on: October 03, 2010, 06:11:42 AM »
A sub program can be anything from a single gcode command to a complete multi tool program.

The simplest use is to allow repetition of a series of moves, e.g. you have 10 holes to drill & tap on 10mm centres along the X axis.

%
O0001(DRILL HOLES 10MM CTS)

G21 G40 G00 G80
G91 G28 Z0

N1(CENTRE DRILL)
T1 M6
G54 G00 G90 G43 X0 Y0 Z25. H1 S1000 M3
M8 (COOLANT ON)
G82 Z-2. R2. P250 F75. (DRILL CYCLE WITH A DWELL OF .25 SECONDS)
M98 P0002 (CALL SUB O0002)
G80 (CANCEL CYCLE)
G91 G28 Z0(MOVE TO TOOL CHANGE)
M1

N2(5MM DRILL)
T2 M6
G54 G00 G90 G43 X0 Y0 Z25. H2 S1500 M3
M8
G81 Z-10. R1. F150.
M98 P0002
G80
G91 G28 Z0
M1

N3(TAP M6)
T3 M6
G54 G00 G90 G43 X0 Y0 Z25. H3 S100 M3
M8
G84 Z-12. R3. F100.
M98 P0002
G80
G91 G28 Y0 Z0
M30

O0002(SUB PROGRAM)
X10.
X20.
X30.
X40.
X50.
X60.
X70.
X80.
X90.
M99
%

We call the sub program O0002 three times, this saves writing out the gcode three times in the main program. This makes for less errors easy editing and smaller gcode files.

There are some simple rules to follow when using sub programs

1.  The first line of a program with sub programs should be a % sign only

2.  The last line of a program with sub programs should be a % sign only

3.  Sub programs should be listed after the main program

4.  Subs should start with a unique Onnnn number and finish with M99

5.  The main program must end with an M2, M30 or M47

6.  Subs calling subs can be up to 7 deep.

7.  Subs can be called from the subroutines directory within Mach3 by using its file name e.g. M98 (SUB01.TAP)

8.  A sub call with an Lnn in the line will call the sub nn times, the value of nn can be in the range of 1 to 99. E.g. M98 P0002 L10

I will add more to this on request.

Regards
Graham
Without engineers the world stops

Offline Dan13

*
  •  1,208 1,208
    • View Profile
    • DY Engineering
Re: Subroutine confusion.
« Reply #2 on: October 03, 2010, 09:45:17 AM »
Thanks Graham! Nice write up on sub routines.

Dan

Offline Fastest1

*
  •  920 920
  • Houston, TX
    • View Profile
Re: Subroutine confusion.
« Reply #3 on: October 03, 2010, 09:56:45 AM »
Graham
   Your tireless efforts to teach us is appreciated. Even the amount of information given in response was incredible. I will definitely read and reread this a few times as it has given me a quite few more questions.
 
   However I am still confused about the location of the subroutines. If I look in my Mach folder under subroutines, I find 1 file. It is labeled "subtest.tap". I might have created this file at some point in the past but I dont remember doing it. I have never called a sub though I have attempted with no success a few months ago but other projects sidetracked me. These are the contents of that file.
G0X1
G0Y1
G0X0
G0Y0
M99
When looking at this file it doesnt seem like it would work or throw an error with what little I know. I would think it would have a conflict as there are 2 points for the same axis in both X & Y. Isnt that what this file is saying?

"A sub program can be anything from a single gcode command to a complete multi tool program."
Thank you for this simple statement as this was a big question on the extent of what a sub can do. With terms like macros, subroutines, looping and the like, these terms can be very confusing.

  

I want to die in my sleep like my grandfather, not like the passengers in the car! :-)

Offline Fastest1

*
  •  920 920
  • Houston, TX
    • View Profile
Re: Subroutine confusion.
« Reply #4 on: October 03, 2010, 10:36:02 AM »
I posed that question because I had placed a rotary table upon my mill with the workpiece paralleling the X axis. The workpiece is held horizontally and referred to as "A" ( though I am not sure it really is "A" in this configuration but the "A" commands do spin it like I desired). I all of a sudden had this idea that hand coding was what I wanted to do since my Z was now an end mill in almost a lathe configuration. Watching "Simpsons36"( http://www.youtube.com/watch?v=2KNit__LJE4&videos=h667Lr8IJS4) creations on YouTube (also a member on cnczone) used to be absolutely baffling but I believe I am seeing how he achieves the desired result. Just recently I saw his shaving video also referred to as single point broaching? I dont think he uses CAD at all for this. It is just amazing at how many ways there are to solve a problem. Plus on a Sherline/A2ZCNC mill, it actually seems much more capable than it is as a lathe or a mill alone. I would like to post a g code I wrote here in the last day or so and see just where and how you would shorten it. The code cuts a spiral along the X axis 6 times (every 60 degrees) into a piece of bar stock and ends up looking like a ball screw or fluted column. I cut it with a ball end mill and though the results were as intended there was a lot of repitition and copy and pasting going on. I am sure there is a better way.
« Last Edit: October 03, 2010, 10:44:28 AM by Fastest1 »
I want to die in my sleep like my grandfather, not like the passengers in the car! :-)

Offline Graham Waterworth

*
  • *
  •  2,672 2,672
  • Yorkshire Dales, England
    • View Profile
Re: Subroutine confusion.
« Reply #5 on: October 03, 2010, 12:47:58 PM »
Post your code and a sketch of what you are trying to achieve and I will take a look.

Graham
Without engineers the world stops

Offline Fastest1

*
  •  920 920
  • Houston, TX
    • View Profile
Re: Subroutine confusion.
« Reply #6 on: October 03, 2010, 02:15:22 PM »
Unfortunately I have no sketch as this was hand coded. I believe only now am I starting to grasp the different uses of the g codes ( I have been doing a lot of reading of Peter Smids and it is deep for a person weak in the mathematics needed). I have a lot of comments to add on what I think I should have done now after this attachment was written and executed. It was an idea and I was tired of waiting and had to try something. Fortunately it worked and did so pretty well if time isnt of a concern. After sleeping on it and realizing what the code was doing (maybe) I think there was a lot of wasted motion. All in all it was fun and rewarding though nothing of significance was made, possibibly a column for my daughters "Barbie" houses?
I want to die in my sleep like my grandfather, not like the passengers in the car! :-)

Offline Graham Waterworth

*
  • *
  •  2,672 2,672
  • Yorkshire Dales, England
    • View Profile
Re: Subroutine confusion.
« Reply #7 on: October 03, 2010, 03:26:20 PM »
This is my version assuming I have interpreted what you were trying to do.

%
G20 G40
T1 M6
G92 A0
G00 G90 X0 Y0 Z.1 A0
G01 Z-.0214 F360.
M98 P0002 L6
G01 Z-.022 F360.
M98 P0002 L6
G01 Z-.03 F360.
M98 P0002 L6
G01 Z-.0375 F360.
M98 P0002 L6
G00 Z.1
M30

O0002(SUB)
G91
G01 X3. A1080. F1080.
G00 Z.1
X-3. A60.
Z-.1
G90
M99
%

Edit : Revised code to remove excess rapids.

Graham
« Last Edit: October 03, 2010, 04:40:55 PM by Graham Waterworth »
Without engineers the world stops

Offline Fastest1

*
  •  920 920
  • Houston, TX
    • View Profile
Re: Subroutine confusion.
« Reply #8 on: October 03, 2010, 04:54:35 PM »
Graham
Thanks for the rewrite.  A few more questions if you dont mind.
Line 4?
G01 Z-.0214 F360. Wouldnt this lower my Z at a feedrate of 360ipm (yikes on a Sherline) with a .0214 doc? and do it 6 times as indicated by L6?
O0002(SUB)
G91                              
G01 X3. A1080. F1080.
G00 Z.1
X-3. A60.
Z-.1
G90
M99
 Does this mean it goes to (G90 an absolute location of X0 A0?) then go to X3 while rotating 3 times, retracting Z to .1 and then rotating 60 degrees and (G91 incrementally moving down from the prior absolute location?) Am I close?
 Also as stated before regarding the sub, besides it physically being the last 8 lines of code, is it stored anywhere else? Or is it just referred to at the top of the file and then the definition given at the end?. I am beginning to think the sub is created in the program and could be stored and referred to permanently but not necessarily saved unless aditional steps are taken to do so.
 The more I look at your code and try to understand it, there is a lot more going on than I thought. The order is so different than what I would expected. Almost like reading from the bottom up, then right when I think I understand a little bit. It goes to another block entirely.
« Last Edit: October 03, 2010, 04:57:35 PM by Fastest1 »
I want to die in my sleep like my grandfather, not like the passengers in the car! :-)

Offline Graham Waterworth

*
  • *
  •  2,672 2,672
  • Yorkshire Dales, England
    • View Profile
Re: Subroutine confusion.
« Reply #9 on: October 03, 2010, 05:14:00 PM »
The feed rate and depth of cut are taken from your original gcode.

The sub program is stored along with the main program in the same file. The main program calls the sub at the end of the file every time it is needed.

If you want to use the same sub program in many different gcode files then is must be stored in the subroutines folder inside the Mach3 folder and called with a M98 (filename.tap) command.

%
G20 G40
T1 M6
G92 A0 (zero out a axis)
G00 G90 X0 Y0 Z.1 A0 (move to start position)
G01 Z-.0214 F360. (feed to depth)
M98 P0002 L6 (call sub)
G01 Z-.022 F360.
M98 P0002 L6
G01 Z-.03 F360.
M98 P0002 L6
G01 Z-.0375 F360.
M98 P0002 L6
G00 Z.1
M30

O0002(SUB)
G91                             (incremental mode)
G01 X3. A1080. F1080.  (feed to x3. rotating as you go)
G00 Z.1                        (jump up in z by .1)
X-3. A60.                      (rapid back to x0 and rotate 60 degs)
Z-.1                              (back down to cutting depth)
G90                              (absolute mode)
M99                              (end of sub)
%

Graham
Without engineers the world stops