What's new
What's new

IF statement inside a While loop - FANUC MACRO B help

avongil

Aluminum
Joined
Dec 31, 2014
Location
Easton, PA
I am having trouble with an if statement inside a while loop. I just want to turn on tool load monitoring as soon as the tool is inside of the cut. The code bellow is what I use in my cutoff cycle.


Here is what I have that is not working out:

(5041 IS CURRENT X POSITION)

#520=#5041(REMEMBER START POSITION)
WHILE[#5041GT#513]DO1
IF[[#5041-#513]LT#521]THEN#521=#5041-#513+.002(PREVENT OVERRUN)
G1U-#521F#511(PECK AT FEEDRATE)
G4P#522(DWELL)
IF[#5041LT#500]DO2
M201 (IF IN CUT TURN ON TLM)
END2
END1

M200 (TURN OFF TLM)


----

here are the values of the parameters.

#500=.505(MATERIAL DIAMETER)
#501=1.030(PART LENGTH)
#502=.118(C-OFF WIDTH)
#503=[#501+#502](CALCULATED CUT POINT)
#504=.02(SUB FACE AMMOUNT)
#505=1262(STOP TOOL)
#506=0404(CUT OFF TOOL NUMBER)
#507=204(CUT OFF TOOL ROT DIR DUAL)
#517=004(CUT OFF TOOL ROT DIR SINGLE)
#508=1000(CUT OFF MAXIMUM SPEED)
#509=200(CUT OFF SURFACE SPEED)
#510=.06(CUT OFF STOCK CLEARANCE)
#511=.002(CUT OFF FEED RATE)
#512=.700(SUB SWALLOW AMMOUNT)
#513=-.06(PART OFF X FINAL POS)

(FOR PECK CUTOFF)
#521=.04(PECK AMMOUNT)
#522=100(DWELL)

(FOR PULLING BEFORE CUT)
(MUST SET PULL TO 0 IF NO PULL)
(OR TOP CUT WILL CRASH)
#533=0(PULL AMMOUNT)
#534=20.0(PULL SPEED)



----


Any any better ways to do it are more than welcome!
 
Last edited:
IF[[#5041-#513]LT[#521]]THEN#521=[#5041-#513+.002](PREVENT OVERRUN)

Depending on system omitting those can mean trouble.

"IF[#5041LT#500]DO2" <- This is illegal. You could convert the if to a while.

I only skimmed code but that's off top of head.
 
I am having trouble with an if statement inside a while loop. I just want to turn on tool load monitoring as soon as the tool is inside of the cut. The code bellow is what I use in my cutoff cycle.


Here is what I have that is not working out:

(5041 IS CURRENT X POSITION)

#520=#5041(REMEMBER START POSITION)
WHILE[#5041GT#513]DO1
IF[[#5041-#513]LT#521]THEN#521=#5041-#513+.002(PREVENT OVERRUN)
G1U-#521F#511(PECK AT FEEDRATE)
G4P#522(DWELL)
IF[#5041LT#500]DO2
M201 (IF IN CUT TURN ON TLM)
END2
END1

M200 (TURN OFF TLM)


Any any better ways to do it are more than welcome!

Hello avongil,
1. Which Conditional Statement (IF) are you having issues with?

2. The Second Do Loop is not required. It could simply be a Conditional Statement

3. It would help to understand your macro if you were to supply some values for the Variables used and their purpose. I take it that #500 is the Workpiece Diameter?

4. It's poor practice to use Common, Nonvolatile Variables (Global Scope), when Local Variables would do.

5. You won't be able to read (accurately) #5041 while the axis is moving.

6. If you don't have a command to halt buffering immediately before #520=#5041, #520 will result in a value close to the Start of the Move rather than at the End of the Move. For example:

G00 X10.0 Z10.0
G01 X-30.0
#520=#5041

The above will result in a value close to 10.0 and certainly not -30.0

#5061 is the 1st Axis System Variable for Skip signal position. Linear interpolation can be commanded by specifying an axial move following the G31 command, just like G01. If an External Skip Signal is not detected before the target value is reached, then Skip Signal Position Variable for the focus axis will take on the coordinate at the end of the move. This method would work infinitely better than using #5041.

Regards,

Bill
 
what I really just want to do is:

IF[#5041LT#500] then issue a M201 command.

will change to 5061 - thanks for the tip. It has been working ok for about a year, but since this is a cutoff tool the X location is not super important so it worked
 
I just updated the first post to show the value of the parameters.

I am thinking I also want to turn off the tool load monitoring macro before it gets to the end of its cut.

Here is what I think I am going to try:
It is not recommended to use variable for M commands, but I cannot think of another way to do this. As for the 500 series variable, I do need to change that but I need to change a bunch of subs and all my main programs. I will leave it as is for now.

That do you think of this approach. The goal is to turn on doosans Tool load monitor inside the cut and off right before the cut is done. It is a cutoff tool, so I want the machine to stop if the tool breaks.

I can increase the air cutting time in the TLM, but I am afraid that it wont so anything since the air time at top could be more than the no load time inside the cut due to the dwells.

#520=#5061(REMEMBER START POSITION)
WHILE[#5061GT#513]DO1
IF[[#5041-#513]LT#521]THEN#521=#5041-#513+.002(PREVENT OVERRUN)

G1U-#521F#511(PECK AT FEEDRATE)

IF[#5041LT#520-.1]#535=201(TURN ON TLM)
IF[#5041LT#513+.1]#535=200(TURN OFF TLM)
M#535(TURN ON/OFF TLM)

G4P#522(DWELL)
END1

M200 (TURN OFF TLM)
 
#520=#5061(REMEMBER START POSITION)
WHILE[#5061GT#513]DO1
IF[[#5041-#513]LT#521]THEN#521=#5041-#513+.002(PREVENT OVERRUN)

G1U-#521F#511(PECK AT FEEDRATE)

IF[#5041LT#520-.1]#535=201(TURN ON TLM)
IF[#5041LT#513+.1]#535=200(TURN OFF TLM)
M#535(TURN ON/OFF TLM)

G4P#522(DWELL)
END1

M200 (TURN OFF TLM)

Hello avongil,

#520=#5061 can't be used in that manner unless the X Axis Slide was moved to position with the following command:

G31 X_ _

Without an External Skip Signal, System Variable #5061 will take on the value of the value specified in the Command Block at the end of travel. As G31 acts in the same way as G01, moving to an initial position in other than Rapid Mode may not be desirable.

An accurate result for recording the X Start Position can be achieved with the following:

G00 X_ _
G31 (or G53) (STOP BUFFERING)
#520=#5041

With a Peck of 0.04" and the following Conditional Statement:

IF[#5041LT#520-.1]#535=201(TURN ON TLM)

with a difference of 0.1 (#520 - .1), neither of the following Conditional Statements:

IF[#5041LT#520-.1]#535=201(TURN ON TLM)
IF[#5041LT#513+.1]#535=200(TURN OFF TLM)

will Test True and therefore, #535 will remain Vacant, or, because its a Nonvolatile Variable, the value it was set to previously (the classic reason for not using #500 Series Variable when not necessary) for the first two cycles of the LOOP. Accordingly, when M#535 is executed for the first time, either the wrong "M" code will be executed, or an error may be raised. You would need to Initialize #535, probably to 200, before commencing the DO LOOP.

Following is the pseudo code generated by your Macro in its present form, but with #5061 replaced with #5041 and the appropriate Stop Buffering code in place:
Code:
G1 U-0.0400 F0.0020
G31
M00
G4 P100
G1 U-0.0400 F0.0020
G31
M00
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M201
G4 P100
G1 U-0.0400 F0.0020
G31
M200
G4 P100
G1 U-0.0400 F0.0020
G31
M200
G4 P100
G1 U-0.0270 F0.0020
G31
M200
G4 P100
M200

There is no Stop to Buffering before the #5041 in the Conditional Statement; accordingly, #5041 in the Conditional Statements won't reflect the position at the end of the Peck Move, if that is what you are aiming for. This may, or may not work in your favor, as the TLM may turn on earlier than you expected, or later than desired when into the next Peck Move. However, TML won't be turned off until the Peck Move after the one you're expecting it to. If you were to program G31, or G53 before the Conditional Statements, an accurate Current Position for the End of the Peck Move will be returned.

In your previous example using the 2nd DO LOOP, I'm not sure if "IF[#5041LT#500]DO2", as in your following example, is legal or not.

IF[#5041LT#500]DO2
M201 (IF IN CUT TURN ON TLM)
END2

I've never seen a DO LOOP as the subject of a Conditional Statement, but I've not seen any reference to it not being allowed.

WHILE is also a Conditional operative. Accordingly, logic would dictate that an IF Conditional statement may be legal.

The following DO LOOP without a WHILE is quite legal

DO2
-----
-----
-----
END2

and will result in an Endless Loop.

If your example above using the IF Statement to Start the DO LOOP is legal syntax, it would also result in an Endless Loop, with no means of escape. Accordingly, when seeking help with an issue, you need to explain what the problem is. For example, you may have been getting a Macro Statement Syntax Error, indicating that the Macro Statement is not legal, or you may have had the program hang inside the DO2 LOOP and not know why.

There is nothing wrong with an "M" code being represented by a Variable. However, following is my interpretation of your Macro, configured is a way that the actual "M" code can be specified and with code to Stop Buffering to give you an accurate End of Peck, Current Position.

#1= 0.505 (X START - MATERIAL DIAMETER)
#2= -0.04(PECK AMMOUNT)
#3=-.06(PART OFF X FINAL POS)
#4= #1 [COPY OF MATERIAL DIAMETER]
#9= 0.002

WHILE [#1 GT #3]DO1
#1=#1+#2
IF[#1 LT #3]TH #1=#3 (PREVENT OVERCUT)
G01 X#1 F#9 (PECK AT FEEDRATE)
G31 (STOP BUFFERING)
IF[[#5041 LE #4] AND [#5041 GT [#3+0.1]]] GOTO10
M200 (TURN OFF TLM)
GOTO20
N10
M201 (TURN ON TLM)
N20
G04P#522 (DWELL)
END1


Following is the pseudo code generated by the Macro above, showing TLM On/Off being executed each cycle of the LOOP respectively.


Code:
G01 X0.4650 F0.002
G31
M201
G04 P100
G01 X0.4250 F0.002
G31
M201
G04 P100
G01 X0.3850 F0.002
G31
M201
G04 P100
G01 X0.3450 F0.002
G31
M201
G04 P100
G01 X0.3050 F0.002
G31
M201
G04 P100
G01 X0.2650 F0.002
G31
M201
G04 P100
G01 X0.2250 F0.002
G31
M201
G04 P100
G01 X0.1850 F0.002
G31
M201
G04 P100
G01 X0.1450 F0.002
G31
M201
G04 P100
G01 X0.1050 F0.002
G31
M201
G04 P100
G01 X0.0650 F0.002
G31
M201
G04 P100
G01 X0.0250 F0.002
G31
M200
G04 P100
G01 X-0.0150 F0.002
G31
M200
G04 P100
G01 X-0.0550 F0.002
G31
M200
G04 P100
G01 X-0.0600 F0.002
G31
M200
G04 P100

If having the TLM On Command continually executed each cycle of the LOOP and similarly the TLM Off Command when the program advances that far, is an issue (it shouldn't be), then the following code shown in Red can be added to avoid this and give the pseudo code shown below.

#1 = 0.505 (X START - MATERIAL DIAMETER)
#2 = -0.04 (PECK AMMOUNT)
#3 = -0.06 (PART OFF X FINAL POS)
#4 = #1 (COPY OF MATERIAL DIAMETER)
#5 = 0 (INITIALISE TLM STATUS)
#9 = 0.002 (FEED RATE)
#522 = 100 (DWELL PERIOD)

WHILE [#1 GT #3] DO1
#1 = #1 + #2
IF [#1 LT #3] TH #1 = #3 (PRE#ENT O#ERCUT)
G01 X#1 F#9 (PECK AT FEEDRATE)
G31 (STOP BUFFERING)
IF [[#5041 LT #4] And [#5041 GT [#3 + 0.1]]] GOTO10
IF [#5 EQ 0] GOTO20
M200 (TURN OFF TLM)
#5 = 0
GOTO20
N10
IF [#5 EQ 1] GOTO20
M201 (TURN ON TLM)
#5 = 1
N20
G04 P#522 (DWELL)
END1

Following is the pseudo code generated by the Macro above, showing TLM On/Off only being executed once each .


Code:
G01 X0.4650 F0.002
G31
M201
G04 P100
G01 X0.4250 F0.002
G31
G04 P100
G01 X0.3850 F0.002
G31
G04 P100
G01 X0.3450 F0.002
G31
G04 P100
G01 X0.3050 F0.002
G31
G04 P100
G01 X0.2650 F0.002
G31
G04 P100
G01 X0.2250 F0.002
G31
G04 P100
G01 X0.1850 F0.002
G31
G04 P100
G01 X0.1450 F0.002
G31
G04 P100
G01 X0.1050 F0.002
G31
G04 P100
G01 X0.0650 F0.002
G31
G04 P100
G01 X0.0250 F0.002
G31
M200
G04 P100
G01 X-0.0150 F0.002
G31
G04 P100
G01 X-0.0550 F0.002
G31
G04 P100
G01 X-0.0600 F0.002
G31
G04 P100

Regards,

Bill
 
Last edited:








 
Back
Top