What's new
What's new

Macro programming sample for IF THEN

Mouse

Aluminum
Joined
Jun 14, 2013
Location
Colorado, USA
Hello, I'm looking for just a basic sample of macro programming, IF THEN format for a fanuc controller. What I am looking to do is use a macro number as a part counter and have a line in the program that will send the program directly to an M30. A GOTO command that sends the program to a sequence number could also work for me.
 
To expand upon that a tad...IF the condition (i.e. EQ, GT, LT) inside the brackets is true THEN the command after the bracket is executed, otherwise it is ignored and the program goes to the next line. The book "Fanuc Custom CNC Macros" by Peter Smid did a great job for me and it goes over more than just Fanuc controls but they're all pretty much the same.
 
Thank you, I kept putting in the "N" on what would be the sequence call out. GOTON1, is what I tried when I attempted this one. Again, thank you.
 
Generically, if your control supports true IF-THEN statements you can try something simple like this:

%
O1234 (IF-THEN TEST)
#101 = 1
IF [#101 EQ 1] THEN #102 = 1
IF [#101 EQ 2] THEN #102 = 2
M30
%

The situation you describe is one where GOTO is probably easier. If you wanted to test multiple conditions at once then I could see an IF-THEN working better. I prefer programming with them whenever I can, because you can simplify the code quite a bit.
 
Ask 5 machinists for a solution to a problem and you'll get (at least) 5 different answers. So here's my nickels worth:

G40 G80 etc...

WHILE[#500 LE 100]DO1

(Put your program here)

#500=#500+1

END1

M30


If your counter needs to survive a power on/off cycle stick with the #500 and up variables. They'll still be there when you turn the controller back on. Variables of #100-#199 will reset to zero if you power the controller off and on.

Since you said Fanuc, there's a Work Counter page that will do what you want. Press the Custom button then the right arrow key until you see "Condition". Press that and it will take you to the Work Counter page with Total, Preset, and Count displayed. Put the number of parts you need in the Preset, zero the Count and Bob's your uncle. It's not nearly as cool as macro programming but it's there if you need it.
 
.....Since you said Fanuc, there's a Work Counter page that will do what you want. Press the Custom button then the right arrow key until you see "Condition". Press that and it will take you to the Work Counter page with Total, Preset, and Count displayed. Put the number of parts you need in the Preset, zero the Count and Bob's your uncle. It's not nearly as cool as macro programming but it's there if you need it.

Implementation of the parts counter is determined by the machine builder, not Fanuc. Not all machines have it and not all work the same.
 
Hello Mouse,
As pointed out by Vancbiker, not all machines have a parts counter, as its MTB specific. However, the System Variables for:

1. Number of machined parts (completion number - #3901)
2. Number of required parts (target number - #3902)

are available.

If making your own Counter Routine, its generally important that that the Target Number and the Parts Machined are maintained when power to the control is cycled. This can be achieved by using a Common, Nonvolatile Macro Variable (=> #500). However, if you use these Variables, you need to be sure that they are not being used by other Macro Programs, such as proprietary MTB's Macros, or Tool/Part measuring Macro Programs. Accordingly, a safer option is to use the purpose supplied #3901 and #3902 System Variables.

You could create a Macro Programs for Parts Counting Increment, Parts Counting Reset and Parts Number Target Setting using a Custom "M" Code to call the Macro Programs. Accordingly, you would insert the M code for Parts Counting Increment at the appropriate place in you Part Program to increment the Counter.

Regards,

Bill
 
The default setting (parameter 6700#0 = 0) of the machine increments
the part count by 1 whenever M02, M30, or the M-code number
specified in parameter 6710 (which normally contains 30 only,
signifying M30) is executed. If it is desired to increment the part count
only after the M-code specified in parameter 6710 is executed (which
may be the same as or different from 02 or 30, but not 0, 98, or 99), set
parameter 6700#0 = 1.
 
does anyone know if this statement will work or is there some other way of writing this?

IF[#900 EQ 1. OR 2.]THEN #901 = 10.
 
does anyone know if this statement will work or is there some other way of writing this?

IF[#900 EQ 1. OR 2.]THEN #901 = 10.

Just do it as two not equal statements

IF[#900 NEQ1]GOTO1
#901=10

N1
IF[#900 NEQ2]GOTO2
#901=10
N2

BEGIN PROGRAM HERE

If 900 is equal to either 1 or 2, one of those lines will set 901=10.

If 900 is any number other than 1 or 2, the control will skip to 2 and begin the program.
 
does anyone know if this statement will work or is there some other way of writing this?

IF[#900 EQ 1. OR 2.]THEN #901 = 10.
Hello 19jeecode85,
Your above syntax won't work, but the following will:

IF[[#900 EQ 1.] OR [#900 EQ 2.]]THEN #901 = 10.

However, the use of logical operations in conditional decision statements is set via parameter. For Fanuc controls FS15 forward, it's parameter bit 6006.1 set to "1"

Regards,

Bill
 
Kind of off on a tangent, but I recently figured out how to implement a GOSUB function:

#100 = 1001 (GOSUB RETURN LINE)
GOTO 2001
N1001
.
.
.
GOTO 9999

N2001
(GOSUB ROUTINE)
GOTO#100

N9999
M30

I'm using this to keep everything in one file and save sub call depth.
 








 
Back
Top