What's new
What's new

Subroutine on fanuc oi-tc

cuttergrinder

Hot Rolled
Joined
Mar 16, 2007
Location
Salem,Ohio
I have never used a subroutine or sub program but i tried writing a program with the main program as O0002 and i have a sub at the bottom with O1000. I used m98 p1000 to call the sub . I loaded the program with rs232 and it loaded fine but loaded as 2 seperate programs. How can i get it to load as 1.
 
I have never used a subroutine or sub program but i tried writing a program with the main program as O0002 and i have a sub at the bottom with O1000. I used m98 p1000 to call the sub . I loaded the program with rs232 and it loaded fine but loaded as 2 seperate programs. How can i get it to load as 1.
Change the O1000 to N1000 and call as a local sub.

Check this thread:

"Local" Subroutines on Fanuc
 
Just stack (copy and paste) your main program with all you subs into one large file separated as the sample below shows. Remember a colon : on a Fanuc is the same as the typical start and ending percent sign. (%) When you copy this file into the control it will automatically separate them as if you copied them one at a time. Basically double % signs at the head, one between each following sub or program, and another % at the very end.

If you have a complete job on your machine - Main Program and Multiple Subs - and if you have their numbering somewhat consecutive, you can copy them as a group which will end up looking like the sample below. Note the sample below is a small fraction of the actual file which has maybe 10 subs. (Production job with 16 work offsets) After you get to the Program Edit Output Screen, the syntax for group copy is as an example - O5100,O5121 OUTPUT. The comma being the statement that basically says - Include everything between this first number and the following one.

Sorry this is a mill program, but it's all the same idea and functions.

%
:5100(LATCH COVER BOTTOMS 16UP)

(MORI B PALLET)

(USES 5102 - 5121 SUBROUTINES)

T5M6(5/16 END MILL)
G17G20G40G49G54G80G90G98

G0X0.3Y-0.4175
G43Z0.1H5S9471M3T10
M8
M98P5102
/G55
/M98P5102
/G56
/M98P5102

Blah-Blah - Blah

/G54P1
/M98P5103
G54P2
M98P5103
M9
G53Z0.Y0.
M30
:5102(LATCH CVR 5/16 EM PLUS SUB)

G0X0.3Y-0.4175
G1Z-0.4F95.
Y-1.0825F69.
Z-0.688F25.

Blah-Blah-Blah

G1Y-0.4375F90.
G3X7.1159Y-0.2735R0.165F40.
G1G40X7.075Y-0.4375
G0Z0.1
M99
%


BY the way... it is normal that all the subs are separate files on the control. The sub calls in your Main Program will go get what it needs, (as long as it finds a file/sub with the correct file number) run it, then return to the main program. I've never used the Local Sub thing talked about earlier.

Hope this helps
 
I have never used a subroutine or sub program but i tried writing a program with the main program as O0002 and i have a sub at the bottom with O1000. I used m98 p1000 to call the sub . I loaded the program with rs232 and it loaded fine but loaded as 2 seperate programs. How can i get it to load as 1.


Use Q instead of P, and use N instead of O, and make sure there is an M30 before the N, otherwise it will be executed for a second time

M98 Q1000 (<--- LOCAL SUBROUTINE CALL)
...
...
...
M30
N1000 (<----SUBROUTINE )
...
...
...
M99
%
 
OK thanks guys. I tried changing the O1000 to N1000 but the machine wouldn't run the sub that way. I will try the "Q" on Monday. I am still having the problem where the comments get stripped out the program when I load by rs 232. Anybody have a fix for that?
 
cuttergrinder
you can not just change the Oxxxx program to Nxxxx and make this work fanuc is usually not set up to do this
you would have to go into the parameters and change 6005 bit 0 to a 1 to allow you to use the subroutine. The post by jancollc is correct just follow the thread he has listed as it does a good job of explaining it
 
This M98P Q subroutine route people are talking about here sounds promising. I've written and used thousands of subroutines but have never used it. Off the cuff I think it's best use might be programs with a single or just a few subs.

The thing about combining subroutines into the main program sounds neat and tidy, but in reality it is anything but, except for proven programs used on repeat work. Here's why. Program size.

Part of the beauty of subroutines is the compartmental nature of them. Tools with complicated and sometimes lengthy tool paths can be dealt with on their own in a separate file. The program example I gave earlier has maybe ten or more subroutines of various lengths. When they're combined as they are into a single file that can be copied to the control as one, the file can get huge, and therefor time consuming and confusing to scroll through while working on it. For those of you that squirt out post processor stuff and never really look at the G code, none of this likely matters. I can understand that. Those that manual or semi manual program like I do and I believe the OP does, meaning those in direct and constant contact with the G code file while programming, wading through a monster file is nobody's idea of fun, and likely leaves greater room for errors.

I get it how many hate to have to copy multiple files for a job. With some luxuries built into many machine communication software interfaces, this copying is not as tough as people make it out to be, but I'm not about to deny people and their hatred for something. Sometimes I will combine subs and mains into one file for ease of transfer, but using the method I use which creates separate files automatically on the control while being loaded. I believe this makes it easier to move around at the control and change or fix discreet parts of programs or subs that need it. When a job is done you use the Save Chain of Programs functionality mentioned earlier and your all-in-one for future copying is re-created.

Once a program is fully proven and becomes repeat work without change, I see no problem with any method, because by that time all the program management has been dealt with so there's nothing more to look at. By that point big is no longer bad.

Ha... I doubt any of this will change the way experienced subroutiners do their thing, but maybe those just venturing down the path will pick up something useful.
 
Here is the program I wrote. It was mostly created with cam except for the sub program. I just thought it would be easier to keep track of for later use if it was all in one program. But when I load it to the machine, it splits it into 2 programs.

%
O0002 (grooving)

N12 T0707
N13 G0 G54 X25. Z8.
N14 G50 S100
N15 G99 G20 G90 G00
N16 G97 S100 M04
N17 G00 Z-0.642
N18 G00 X8.5048 Z-0.642
N19 M98 P1000
N20
N21 G00 X8.5048 Z-0.622
N22 M98 P1000
N23
N24 G00 X8.5048 Z-0.602
N25 M98 P1000
N26
N27 G00 X8.5048 Z-0.582
N28 M98 P1000
N29
N30 G00 X8.5048 Z-0.562
N31 M98 P1000
N32
N33 G00 X8.5048 Z-0.542
N34 M98 P1000
N35
N36 G00 X8.5048 Z-0.522
N37 M98 P1000
N38
N39 G00 X8.5048 Z-0.502
N40 M98 P1000
N41
N42 G00 X8.5048 Z-0.482
N43 M98 P1000
N44
N45 G00 X8.5048 Z-0.462
N46 M98 P1000
N47
N48 G00 X8.5048 Z-0.442
N49 M98 P1000
N50
N51 G00 X8.5048 Z-0.422
N52 M98 P1000
N53
N54 G00 X8.5048 Z-0.402
N55 M98 P1000
N56
N57 G00 X8.5048 Z-0.382
N58 M98 P1000
N59
N60 G00 X8.5048 Z-0.362
N61 M98 P1000
N62
N63 G00 X8.5048 Z-0.342
N64 M98 P1000
N65
N66 G00 X8.5048 Z-0.322
N67 M98 P1000
N68
N69 G00 X8.5048 Z-0.302
N70 M98 P1000
N71
N72 G00 X8.5048 Z-0.282
N73 M98 P1000
N74
N75 G00 X8.5048 Z-0.262
N76 M98 P1000
N77
N78 G00 X8.5048 Z-0.242
N79 M98 P1000
N80
N81 G00 X8.5048 Z-0.222
N82 M98 P1000
N83
N84 G00 X8.5048 Z-0.202
N85 M98 P1000
N86
N87 G00 X8.5048 Z-0.182
N88 M98 P1000
N89
N90 G00 X8.5048 Z-0.162
N91 M98 P1000
N92
N93 G00 X8.5048 Z-0.142
N94 M98 P1000
N95
N96 G00 X8.5048 Z-0.122
N97 M98 P1000
N98
N99 G00 X8.5048 Z-0.102
N100 M98 P1000
N101
N102 G00 X8.5048 Z-0.082
N103 M98 P1000
N104
N105 G00 X8.5048 Z-0.062
N106 M98 P1000
N107
N108 G00 X8.2956 Z-0.5794
N109 M98 P1000
N110
N111 G00 X8.2956 Z-0.5594
N112 M98 P1000
N113
N114 G00 X8.2956 Z-0.5394
N115 M98 P1000
N116
N117 G00 X8.2956 Z-0.5194
N118 M98 P1000
N119
N120 G00 X8.2956 Z-0.4994
N121 M98 P1000
N122
N123 G00 X8.2956 Z-0.4794
N124 M98 P1000
N125
N126 G00 X8.2956 Z-0.4594
N127 M98 P1000
N128
N129 G00 X8.2956 Z-0.4394
N130 M98 P1000
N131
N132 G00 X8.2956 Z-0.4194
N133 M98 P1000
N134
N135 G00 X8.2956 Z-0.3994
N136 M98 P1000
N137
N138 G00 X8.2956 Z-0.3794
N139 M98 P1000
N140
N141 G00 X8.2956 Z-0.3594
N142 M98 P1000
N143
N144 G00 X8.2956 Z-0.3394
N145 M98 P1000
N146
N147 G00 X8.2956 Z-0.3194
N148 M98 P1000
N149
N150 G00 X8.2956 Z-0.2994
N151 M98 P1000
N152
N153 G00 X8.2956 Z-0.2794
N154 M98 P1000
N155
N156 G00 X8.2956 Z-0.2594
N157 M98 P1000
N158
N159 G00 X8.2956 Z-0.2394
N160 M98 P1000
N161
N162 G00 X8.2956 Z-0.2194
N163 M98 P1000
N164
N165 G00 X8.2956 Z-0.1994
N166 M98 P1000
N167
N168 G00 X8.2956 Z-0.1794
N169 M98 P1000
N170
N171 G00 X8.2956 Z-0.1594
N172 M98 P1000
N173
N174 G00 X8.2956 Z-0.1394
N175 M98 P1000
N176
N177 G00 X8.2956 Z-0.1194
N178 M98 P1000
N179
N180 G00 X8.1504 Z-0.454
N181 M98 P1000
N182
N183 G00 X8.1504 Z-0.434
N184 M98 P1000
N185
N186 G00 X8.1504 Z-0.414
N187 M98 P1000
N188
N189 G00 X8.1504 Z-0.394
N190 M98 P1000
N191
N192 G00 X8.1504 Z-0.374
N193 M98 P1000
N194
N195 G00 X8.1504 Z-0.354
N196 M98 P1000
N197
N198 G00 X8.1504 Z-0.334
N199 M98 P1000
N200
N201 G00 X8.1504 Z-0.314
N202 M98 P1000
N203
N204 G00 X8.1504 Z-0.294
N205 M98 P1000
N206
N207 G00 X8.1504 Z-0.274
N208 M98 P1000
N209
N210 G00 X8.1504 Z-0.254
N211 M98 P1000
N212
N213 G00 X8.1504 Z-0.234
N214 M98 P1000
N215 G00 X25. Z 8.
N216 M30

O1000
N218 G32 U-.7 W .35 F.866
N219 W 10.
N220 U.7 W .35
N221 M99
 
I'm still such a newb on lathe programming and the alternate method of subroutine calling, I'll leave that to others for now.

What I want to ask is this, and it's about you losing your comments when copying to your control.

I ask... is what you printed above EXACTLY what you're trying to put in the control? If so I can tell you that the reason it's stripping your comments out is because all comments need to be in ALL CAPS. Fanuc controls don't have a clue about lower case letters.

I do all my programming with the CAPS LOCK on.

I can see that without labels your programs will have little meaning as numbers alone on the control.
 
Here is the program I wrote. It was mostly created with cam except for the sub program. I just thought it would be easier to keep track of for later use if it was all in one program. But when I load it to the machine, it splits it into 2 programs.

%
O0002 (grooving)

N12 T0707
N13 G0 G54 X25. Z8.
N14 G50 S100
N15 G99 G20 G90 G00
N16 G97 S100 M04
N17 G00 Z-0.642
N18 G00 X8.5048 Z-0.642
N19 M98 P1000
N20
N21 G00 X8.5048 Z-0.622
N22 M98 P1000
N23
N24 G00 X8.5048 Z-0.602
N25 M98 P1000
etc.
etc.
etc.
N215 G00 X25. Z 8.
N216 M30

O1000
N218 G32 U-.7 W .35 F.866
N219 W 10.
N220 U.7 W .35
N221 M99

As others have mentioned, setting parameter bit #6005.0 to "1" will allow the call of a Local Subprogram (one contained within the Main Program). It will also allow calling an External Subprogram and starting at a specified Sequence Number within the External Sub. The Syntax for this is:
M98 P_ _ Q_ _

Where
1. P_ _ is address character and Program Number of the External Subprogram
and
2. Q_ _ is the Sequence Number in the Subprogram from where execution is to commence

As your program example is a repeat of the same Subprogram at different coordinates, you could also call your Subprogram in a Modal manner as follows using G66. G66 is actually a Modal Call of a Macro Program, allowing arguments to be passed to Macro Program, but there is no requirement to pass any arguments, therefore, you can simply call your simple Subprogram and have it remain Modal.

The way it works, is that you locate the tool at the first Coordinate Set where you want to call the Subprogram. You then call the Subprogram using the Modal Call G66, whereupon the Subprogram will execute once and on its completion, control returns to the Main Program. Each time a coordinate set is executed, the Subprogram will execute. This sequence will continue until the Modal Mode of the Subprogram is cancelled with G67.

The following example is your program coordinates being used to call Subprogram O1000 without having to repeat the Subprogram Call.

N1 T0707
G0 G54 X25. Z8.
G50 S100
G99 G20 G90 G00
G97 S100 M04
G00 Z-0.642
G00 X8.5048 Z-0.642
G66 P1000 <Subprogram Modal Call
G00 X8.5048 Z-0.622
X8.5048 Z-0.602
X8.5048 Z-0.582
X8.5048 Z-0.562
X8.5048 Z-0.542
X8.5048 Z-0.522
X8.5048 Z-0.502
X8.5048 Z-0.482
X8.5048 Z-0.462
X8.5048 Z-0.442
X8.5048 Z-0.422
X8.5048 Z-0.402
X8.5048 Z-0.382
X8.5048 Z-0.362
X8.5048 Z-0.342
X8.5048 Z-0.322
X8.5048 Z-0.302
X8.5048 Z-0.282
X8.5048 Z-0.262
X8.5048 Z-0.242
X8.5048 Z-0.222
X8.5048 Z-0.202
X8.5048 Z-0.182
X8.5048 Z-0.162
X8.5048 Z-0.142
X8.5048 Z-0.122
X8.5048 Z-0.102
X8.5048 Z-0.082
X8.5048 Z-0.062
X8.2956 Z-0.5794
X8.2956 Z-0.5594
X8.2956 Z-0.5394
X8.2956 Z-0.5194
X8.2956 Z-0.4994
X8.2956 Z-0.4794
X8.2956 Z-0.4594
X8.2956 Z-0.4394
X8.2956 Z-0.4194
X8.2956 Z-0.3994
X8.2956 Z-0.3794
X8.2956 Z-0.3594
X8.2956 Z-0.3394
X8.2956 Z-0.3194
X8.2956 Z-0.2994
X8.2956 Z-0.2794
X8.2956 Z-0.2594
X8.2956 Z-0.2394
X8.2956 Z-0.2194
X8.2956 Z-0.1994
X8.2956 Z-0.1794
X8.2956 Z-0.1594
X8.2956 Z-0.1394
X8.2956 Z-0.1194
X8.1504 Z-0.454
X8.1504 Z-0.434
X8.1504 Z-0.414
X8.1504 Z-0.394
X8.1504 Z-0.374
X8.1504 Z-0.354
X8.1504 Z-0.334
X8.1504 Z-0.314
X8.1504 Z-0.294
X8.1504 Z-0.274
X8.1504 Z-0.254
X8.1504 Z-0.234
G67 <Subprogram Modal Call Cancel
X25. Z8.
M30
%
 
I'm still such a newb on lathe programming and the alternate method of subroutine calling, I'll leave that to others for now.

What I want to ask is this, and it's about you losing your comments when copying to your control.

I ask... is what you printed above EXACTLY what you're trying to put in the control? If so I can tell you that the reason it's stripping your comments out is because all comments need to be in ALL CAPS. Fanuc controls don't have a clue about lower case letters.

I do all my programming with the CAPS LOCK on.

I can see that without labels your programs will have little meaning as numbers alone on the control.
Thank you I did not know the comments had to be in all caps. Our Mazak does not require that.
 
As your program example is a repeat of the same Subprogram at different coordinates, you could also call your Subprogram in a Modal manner as follows using G66. G66 is actually a Modal Call of a Macro Program, allowing arguments to be passed to Macro Program, but there is no requirement to pass any arguments, therefore, you can simply call your simple Subprogram and have it remain Modal.

The way it works, is that you locate the tool at the first Coordinate Set where you want to call the Subprogram. You then call the Subprogram using the Modal Call G66, whereupon the Subprogram will execute once and on its completion, control returns to the Main Program. Each time a coordinate set is executed, the Subprogram will execute. This sequence will continue until the Modal Mode of the Subprogram is cancelled with G67.

AngelW, this awesome. Where do you learn all this stuff? Lol
 
AngelW, This is how my sub is written.

O1000
N218 G32 U-.7 W .35 F.866
N219 W 10.
N220 U.7 W .35
N221 M99

This is how you said I could write the main program.

N1 T0707
G0 G54 X25. Z8.
G50 S100
G99 G20 G90 G00
G97 S100 M04
G00 Z-0.642
G00 X8.5048 Z-0.642
G66 P1000 <Subprogram Modal Call
G00 X8.5048 Z-0.622
X8.5048 Z-0.602
X8.5048 Z-0.582
X8.5048 Z-0.562
X8.5048 Z-0.542

Would these coordinates all need a G00 before them? I fear that the control would still be on G32 when it returns from the sub without placing the G00 in front of these coordinates.
 
Being somewhat inexperienced in lathe programming this is partially a shot in the dark. Any reason the program couldn't run like this? Or is it just unnecessary complication? To be honest, this program strikes me as something that could use programming trickery well beyond what I'm offering here. Even past a semi-basic thing like single or two level nested subs, but I question the need being there.

Shot in the dark -

%
:O1000 (MULTI GROOVE JOB)

T0707 (GROOVING TOOL)

G00 G54 X25. Z8.
G50 S100
G99 G20 G90
G97 S100 M04
Z-0.642
X8.5048
M98P1001L28
G0X8.2956 Z-0.5794
M98P1002L23
G0X8.1504 Z-0.454
M98P1003L11
X25.Z8.
M30

:O1001 (GROOVY SUB 1)

G32 U-0.7 W 0.35 F0.866
W10.
U0.7 W0.35
G0X8.5048W-0.02
M99

:O1002 (GROOVY SUB 2)

G32 U-0.7 W 0.35 F0.866
W10.
U0.7 W0.35
G0X8.2956W-0.02
M99

:O1003 (GROOVY SUB 3)

G32 U-0.7 W 0.35 F0.866
W10.
U0.7 W0.35
G0X8.1504W-0.02
M9
%

Maybe-sorta-kinda?

Not sure about the need of the G0's in the last line of the subs and the in between subroutine calls of the main program. May not have the repeat counts exactly right either. I'm not 100% about the change over area from one sub to the next whether it runs into anything, including the part.
 
AngelW, This is how my sub is written.

O1000
N218 G32 U-.7 W .35 F.866
N219 W 10.
N220 U.7 W .35
N221 M99

This is how you said I could write the main program.

N1 T0707
G0 G54 X25. Z8.
G50 S100
G99 G20 G90 G00
G97 S100 M04
G00 Z-0.642
G00 X8.5048 Z-0.642
G66 P1000 <Subprogram Modal Call
G00 X8.5048 Z-0.622
X8.5048 Z-0.602
X8.5048 Z-0.582
X8.5048 Z-0.562
X8.5048 Z-0.542

Would these coordinates all need a G00 before them? I fear that the control would still be on G32 when it returns from the sub without placing the G00 in front of these coordinates.

Yes. Specify G00 either in each Movement Block between the G66 and 67, or at the end of your Subprogram is required.
 
Being somewhat inexperienced in lathe programming this is partially a shot in the dark.
%
:O1000 (MULTI GROOVE JOB)

T0707 (GROOVING TOOL)

G00 G54 X25. Z8.
G50 S100
G99 G20 G90
G97 S100 M04
Z-0.642
X8.5048
M98P1001L28

:O1001 (GROOVY SUB 1)

G32 U-0.7 W 0.35 F0.866
W10.
U0.7 W0.35
G0X8.5048W-0.02
M99

Hello 13engines,
That approach works when there is a consistent move, as in the OP's case, but your example code would have the tool end up over 280" in Z up the lathe bed. Consideration of the Incremental W10.0 and two W0.35 moves is required. Also, the Z Absolute Move in the OP's Main Program is shifting the Z Start in a plus direction. Accordingly, the Incremental shift (W) in the Z Start Point would have to be +0.02.

To take into consideration the W10.0 and two W0.35 moves, your original Block:

G0X8.5048W-0.02

would have to be something like the following:

G0 X8.5048 W-[10.0 + 0.7 - 0.02]

The above Block would result in a W0.02 shift from the preceding Z Start Point.

Regards,

Bill
 
Ha... shot in the dark. Looks like I missed. Thanks Bill for the clarity. Honestly I hadn't taken the time to gather a real clue about what was going on with the first thee lines of the sub including the G32, which to my recollection I've not used yet. I was counting on the idea that those 3 lines were being read over and over in the original setup, so no harm done continuing that trend. Guess not.
 
This is also an other way to write the grooving cycle (while/do statement If it is enabled)
Just write it for the 3 cycles being used




N12 T0707
G0 G54 X25. Z8.
G50 S100
G99 G20 G90 G00
G97 S100 M04
G00 Z-0.642
G00 X8.5048 Z-0.642

(FIRST GROOVE)

#100=-.642(START OF CUT)
#101=-0.062(END OF CUT)
#102=.02(DEPTH OF CUT)

WHILE[#100LT#101]DO1

IF[#100GT#101]THEN#100=#101

G0X8.5048 Z#100

G32 U-.7 W .35 F.866
W 10.
U.7 W .35

#100=#100+#102

END1
 








 
Back
Top