588,569 active members*
5,513 visitors online*
Register for free
Login
IndustryArena Forum > Machine Controllers Software and Solutions > Fanuc > How do you clear variables after the prg. is done??
Results 1 to 19 of 19
  1. #1
    Join Date
    Nov 2005
    Posts
    219

    How do you clear variables after the prg. is done??

    I need to add something at the end of a prg. that clears all variables.

    something like
    #1=0;
    #1=[#1thru#499]

    I know this example wont work but Im just trying to get my point across.

    thanks
    Jon

  2. #2
    Join Date
    Jan 2007
    Posts
    355
    How about:

    N1000 #1=2 ( SET LOOP COUNT )
    N1010 #[#1]=0
    N1020 #1=#1+1
    N1030 IF [#1 NE 500] GOTO 1010 ( LOOP THRU 499)
    N1040 #1=0

    Not sure about the syntax, but it might work...

  3. #3
    Join Date
    Jan 2007
    Posts
    91
    #1=#1
    N1
    WHILE[#1LE33]DO1
    #[#1]=0
    #1=#1+1
    END1

    I use this type of can to clear varibles but I'm not at work, to check, so this may not be exactly right. I believe you will need to set a different can for each group of varibles. #1 - #33 , #100 - #199... Otherwise you will probably get a varible not found type error. The above example will clear the locals.
    -Jim

  4. #4
    Join Date
    Nov 2005
    Posts
    219
    Quote Originally Posted by jamesweed View Post
    #1=#1
    N1
    WHILE[#1LE33]DO1
    #[#1]=0
    #1=#1+1
    END1

    I use this type of can to clear varibles but I'm not at work, to check, so this may not be exactly right. I believe you will need to set a different can for each group of varibles. #1 - #33 , #100 - #199... Otherwise you will probably get a varible not found type error. The above example will clear the locals.
    -Jim
    What do I change to clear the #100-#199?
    I would assume this

    WHILE[#100LE199]DO1

  5. #5
    Join Date
    May 2006
    Posts
    82
    #1=#1
    N1
    #[#1]=0
    #1=#1+1
    WHILE[#1LE33]DO1
    END1

    I thinks you want the WHILE statement preceding the end statement so you are not in an everlasting loop.

  6. #6
    Join Date
    Jan 2007
    Posts
    91
    Quote Originally Posted by lgreeves View Post
    #1=#1
    N1
    #[#1]=0
    #1=#1+1
    WHILE[#1LE33]DO1
    END1

    I thinks you want the WHILE statement preceding the end statement so you are not in an everlasting loop.
    I think either way will work...

    #1=100
    N1
    #[#1]=0
    #1=#1+1
    WHILE[#1LE199]DO1
    END1

    This I believe will clear #100-#199, if not just tinker with it a bit because this is close if not right way.

  7. #7
    Join Date
    Nov 2006
    Posts
    174

    #0 not 0

    The above examples will asign a value of 0 (zero) to the variables and not clear them. You need to use #0 not 0 to clear them. There is a difference. #0 is always null.

    e.g. #[#1]=#0 not #[#1]=0



    ChattaMan

  8. #8
    Join Date
    Nov 2005
    Posts
    219
    Ok, I went to work this morning and played with this some. This is what I get when I use this...

    #1=100
    WHILE[#1LE149]DO1
    #[#1]=#0
    #1=#1+1
    END1

    THIS WORKS, CLEARS 100-149

    #1=500
    WHILE[#1LE531]DO1
    #[#1]=#0
    #1=#1+1
    END1

    THIS WORKS, CLEARS 500-531

    #1=1
    WHILE[#1LE33]DO1
    #[#1]=#0
    #1=#1+1
    END1

    THIS DOES NOT WORK THOU.
    THE QUESTION IS WHY????
    Can someone explain to me what the "WHILE" and "DO" and "END" statements mean?

    I believe "LE" stands for "LESS THAN" right?
    #0 is null and can not be written too,but can be referenced.

    I want to understand exactly how and why this works so I can possibly work thru this myself.


    thanks guys
    Jon

  9. #9
    Join Date
    Sep 2005
    Posts
    767
    In your last macro example, you're using #1 as a counter, and the first thing you do is set it to "1" then, inside your loop, you set it to null, so the loop doesn't execute.

    If you start the loop with #1=2, then the loop will clear all the variables between 2 and 33, and when the loop is finished #1 will be set to "34". You can then just add another statement setting #1=#0 after the END1, like so:

    #1=2
    WHILE[#1LE33]DO1
    #[#1]=#0
    #1=#1+1
    END1
    #1=#0


    LE meand "Less than or Equal To"

  10. #10
    Join Date
    Nov 2005
    Posts
    219
    Ok, that makes sense now. There are still a few things I dont understand about it thou.

    This line inpaticular #[#1]=#0
    Why is there a "#" sign out side of the brackets with no number?? I thought the brackets were for calculations, and I dont see any???

    Is the DO1 setting the counter to use#1 for the loop then???I think so??

    thanks
    Jon

  11. #11
    Join Date
    Sep 2005
    Posts
    767
    The statement:

    #[#1]

    is an example of "indirect addressing". The variable #1 can have any value from 1 to 34 in your example program, so when you start the loop with the variable #1 set to "2", the statement above is equal to "#2". When you increment the value of #1 to "3", then the statement above is the same as "#3".

    Normally, a variable just refers to the memory address of a value. The memory address stays the same, but the VALUE saved in that memory location can be anything.

    The statement in your example is an address of an address of a value. Hence the term "indirect addressing". You can even compound this concept by using an address of an address of an address of a value, like so:

    #[#[#1]]

    .. but this is is too confusing for me. It makes my brains hurt.

  12. #12
    Join Date
    Nov 2005
    Posts
    219
    Dan, do you know of any books that cover this kind of macro material??


    I have not seen anything like this in my Yellow Fanuc manual.

    thanks
    Jon

  13. #13
    Join Date
    Sep 2005
    Posts
    767
    There are a lot of "tricks" to programming macros, and the Fanuc documentation only gives you an overview.

    You might want to visit Mike Lynch's website at www.cncci.com Mike sells a lot of training materials for CNC programming, and he's the Tech editor for Modern Machine Shop magazine. He frequently writes columns featuring macro programming techniques, and many of his past columns would make good reading.

  14. #14
    Join Date
    Jan 2007
    Posts
    91
    "Is the DO1 setting the counter to use#1 for the loop then???I think so??"

    What we have left off our examples to each other is the N1 line number.
    The WHILE command cans everthing between N1 and END1. I might be wrong on this but I think the N1 can be directly above or below the WHILE command??? Have to check it out.

    #1=2
    N1
    WHILE[#1LE33]DO1
    #[#1]=#0
    #1=#1+1
    END1
    #1=#0

    The N1 isn't treated like a normal line number ( I think ) ex...

    N1 *****
    N2 *****
    .
    .
    .
    N32 *****
    N33 #1=2
    N1
    WHILE[#1LE33]DO1
    #[#1]=#0
    #1=#1+1
    END1
    N5#1=#0

    Would not jump back up to beginning of program. Also on a side note, the locals ( #1 - #33 ) should turn vacant at M02, M30, or reset. May not have to worry about having sub reset these. Also the locals have different levels, dont have to worry about this unless your main program calls a submacro and passes values to it. Or if a sub calls anouther sub...
    Can get very confusing. Start simple and go from there. MacroB is fun easy to learn and very powerful.
    -Jim

  15. #15
    Join Date
    Sep 2005
    Posts
    767
    You are correct that the local variables #1 thru #33 become vacant automatically when you exit the macro. Those variable numbers are referred to as "local" variables because they only hold their values within the macro. If this macro calls another macro, calls another subroutine, or returns to the calling program, these variables no longer have their values.

    If you want to jump from one macro to another and have a variable hold its value, use the "Common" variables from #100 and up. The variables in the #500 + range hold their values even when the power is turned off and back on. The "System" variables are in the #1000 + range, and refer to internal system registers, like the current axis positions, I/O signals, current feedrate, etc. Mostly, you would only read the system variables to make decisions within your macro.

    When making calculations within your macro, use the local variables whenever possible. When passing values from one macro to another, use the Common variables, and when you need to remember something when the power turns off then on again, use the #500 variables.

    The DO1 statement and the END1 statement operate as a pair. They have nothing to do with the N-numbers in your program. The WHILE statement poses a "condition" that must be met (true or false). If this condition is met, then the code between DO1 and END1 are executed. After END1, the WHILE condition is tested again, and this loop continues until the WHILE statement comes back false. Then, the program resumes from the line after the END1.

    You can have lots of DO1 --- END1 pairs within your macro. But, if you put a loop WITHIN another loop, use DO1 --- END1 for one loop, and DO2 --- END2 for the other loop. For example:

    #1=1
    WHILE[#1LE33]DO1
    (blah, blah)
    #2=1
    WHILE[#2LE55]DO2
    (blah blah)
    #2=#2+1
    DO2
    #1=#1+1
    END1

    This example executes a 55-step loop 33 times, so the code between DO2 and END2 executes 1815 times! Be sure that your loops always use DOx and ENDx numbers in pairs, and keep the program structured. It's possible (but not good programming practice) to jump out of a "DO" loop with a GOTO statement. Try to avoid that if possible. Let the DO loop finish it's job.

    While on the subject ... GOTO statements do refer to N-numbers, so if you have a "GOTO1" in your macro, that will make it jump to the N1 line. A GOTOx statement is used to make a simple jump in the program, and is not the best way to execute a "loop". It takes longer for the Fanuc to use a GOTO statement to search backwards in the program, so WHILE--DO is a better choice for loops. When juming FORWARD in a progam, GOTO works just fine.

  16. #16
    Join Date
    Nov 2005
    Posts
    219
    I am probably using them the wrong way to begin with but I will try to explain it.

    I run a Lathe with a supspindle, It has a LNS 12 foot Barfeeder on one side and a LNS Blaze air vacum unloader on the other side. We make about 200-300 parts in a 10 hr day. 99% of them are sucked out of the subspindle by the Blaze air unloader. We make pins that are basically used for a hinging point. They range in size from .750 O.D. x 2" long to 2.5 O.D. x 29." long.

    Right now I have 195 part prg. in the machine. Out of the 195 parts there are probably 90 that I would classify as being in the same family of parts.

    This family of parts gets, snap ring grooves, chamfered on both ends, and drilled and tapped on the ends, and one cross hole thru the pin.

    There are many combinations of the above process's with varying part lengths and diameters that make up the 90 or so parts that are in the same "Family".

    We dont use M30, in the prg.'s to rely on them to reset the variables 1-33.
    We have M99's at the end of all the prg. and use a parts counter installed next to the fanuc 18-I controller. It uses an M12 to count 1 part completed right before the M99.

    Ok back to the way I use 1-33 variables.
    At the front of a part prg. I will have something like this.


    #1=1 (If #1=1 it will drill and tap main spindle)
    #2=2 (If #2=2 it will drill and tap sub spindle)
    #3=3 (If #3=3 it will groove main spindle)
    #4=4 (If #4=4 it will groove sub spindle)


    There is alot more info but I am just trying to give the basic concept.

    After it loads all the info to define the part, I have been using the same sub call to run all 90 or so parts "M98 P9983".

    Inside of that I have been using something like this before each operation to decide what gets done to it or not.

    IF[#1EQ1]GOTO1000
    IF[#1NE1]GOTO1001
    N1000 M98 9981 (DRILL AND TAP ON MAIN SUB CALL)
    N1001

    This way I can define all the attributes of a part individually, with just changing a couple values. It makes programming new parts much easier,but I know I need to learn more and do it right.

    Anyway, when the counter reaches the specifed amount and stops at the end of the prg. it does not reset the variables 1-33 because it never seen a M30 or MO2, and if the operator does not manually hit the reset button it will keep those values, and if the next prg. does not overrite them, then it will run the same M98 P9983 sub call and make the wrong part.

    Im thinking about making seperate G65 prog. for each operation. and define the part with the 1-33 variables for each operation and then transfer those values to the 100 variables inside of the Macro call to do various calculations for the chamfer and grooving operations. One thing I am kind of proud of is I wrote a macro to not only chamfer the ends but put radius on the corners of the chamfer. This macro works on all different diameters,chamfer size, and radius sizes. It use 3 constants to define the start and end points of the radius and chamfer. They are the Stock O.D., Chamfer size and Radius size and it will do the rest.

    thanks
    Jon

  17. #17
    Join Date
    Feb 2006
    Posts
    338
    While this is a good learning exercise about macros, think about why you would need to clear the variables. You should not be using any variables in your programs that you have not specifically set in the program. Therefore the previous values do not matter. This is just a good practice to minimize chances for error.

    Something else to note is that #1 - #33 are per program, up to 5 copies at once. Which is the limit for nesting programs, original program plus 4 nested macros. These start as #0 (null) when the program is called unless provided by arguments when calling the macro ie. G65 P1002 R3.2 Z2.1

    #100 variables are reset to #0 when power to the machine is off.
    #500 variables remain even after power is turned off.

    When using variables, keep this in mind. Only use #500 variables when you want to keep that value. Always set #100 in your program. And the #1-#33 use internally, and for passing values into the called program via arguments. You can verify they were specified by checking if they are <> #0

    For the 16i control this is all explained in the B-63014EN/02 operator manual section 15 (custom macros).

  18. #18
    Join Date
    Feb 2006
    Posts
    338
    Oops missed the second page. Well the last post can still be useful.

    Quote Originally Posted by theemudracer View Post
    IF[#1EQ1]GOTO1000
    IF[#1NE1]GOTO1001
    N1000 M98 9981 (DRILL AND TAP ON MAIN SUB CALL)
    N1001
    You don't need IF[#1EQ1]GOTO1000 in the above.

    Anyhow, There are a few ways to approach this. First I would use either a #500 variable, or the machine parts counter to keep track of the parts needed till it is done. Then have the program end M30.

    Programs O1-195 contain all the variables you set to make that part. Call the program for that family, passing the parameters to that program. Loop thru counting down the parts needed, and exit at zero. Set your #500 variable for the desired count and run the O1-195 program you need.

    If you wanted to be able to run different parts back to back, start with a program to control that O8000. Start with a program that sets up to 10 (or what ever) part #'s (O1-195) and counts for each. If the count is 0 do nothing, otherwise call a program that sets your parameters for that part, followed by the main program that machines the parts. When that part is done, it returns to the O8000 program and moves to the next.


    O1
    #1=1 (If #1=1 it will drill and tap main spindle)
    #2=2 (If #2=2 it will drill and tap sub spindle)
    #3=3 (If #3=3 it will groove main spindle)
    #4=4 (If #4=4 it will groove sub spindle)
    G65 P1001 A#1 B#2 C#3 I#4
    M99

    O2
    #1=1 (If #1=1 it will drill and tap main spindle)
    #2=0 (If #2=2 it will drill and tap sub spindle)
    #3=3 (If #3=3 it will groove main spindle)
    #4=0 (If #4=4 it will groove sub spindle)
    G65 P1001 A#1 B#2 C#3 I#4
    M99
    .
    .
    .

    O8000
    #1=95 (first part)
    #2=10 (first part count)
    #3=83 (second part)
    #4=100 (second part count)
    ...
    If [#500eq0] goto 10
    #3000=1(500 NE 0 previous program not finished)
    (#3000 stops the program with a user error)
    N10

    #500=#2 (set counter)
    If [#500eq0] goto 9000 (zero qty, exit)
    G65 P#1 (Run first part number)

    #500=#4 (set counter)
    If [#500eq0] goto 9000 (zero qty, exit)
    G65 P#3 (Run second part number)
    ...

    N9000
    M30

    Actually I would do more checking ect so that if the program was stopped, I could continue with the production run without losing the part count. The above is not tracing which part # it is on.

    You could also make the part that sets #500 and calls the part program a loop that exits if the count is zero if you wanted to.

  19. #19
    Join Date
    Nov 2005
    Posts
    219
    Food for thought definetly,

    thanks
    Jon

Similar Threads

  1. Local variables
    By jorgehrr in forum G-Code Programing
    Replies: 4
    Last Post: 02-19-2007, 10:03 PM
  2. System variables
    By jorgehrr in forum G-Code Programing
    Replies: 8
    Last Post: 02-19-2007, 02:26 AM
  3. Variables/Macro use ????
    By theemudracer in forum G-Code Programing
    Replies: 2
    Last Post: 12-11-2006, 04:47 PM
  4. Clear Acrylic Fender Stratocaster Guitar
    By HygloCompany in forum Glass, Plastic and Stone
    Replies: 0
    Last Post: 02-17-2006, 06:49 PM
  5. Clear rubber question?
    By DrStein99 in forum Composites, Exotic Metals etc
    Replies: 24
    Last Post: 02-15-2006, 05:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •