Lesson 2: Variables Lesson 2 - Variables Values in normal CNC programs, like coordinates, spindle speeds, and feed-rates, are hard-and-fixed values, commonly called constants. Custom Macro allows you to incorporate values that change from time to time. Values that can change are called variables. Objectives After completing this lesson, students should be able to: Define variables Describe arguments Describe local variables Describe common variables Describe permanent common variables Describe what system variables do Describe how to incorporate variables in programs Introduction If you have experience programming with any computer programming language, you already know many of the computer-related functions of Custom Macro. However, we do not wish to assume that all readers understand computer programming. For this reason, we describe each computer-related feature from scratch. And we begin presentations in a rather basic manner, so even computer programming novices will be able to follow. We recommend one more time that newcomers to computer programming purchase a beginner’s book on computer programming. It will reinforce your understanding of what can be done with computer-related features of Custom Macro. It will also relate some of the computer programming thought-process (mentality) that is not included in this text. Topics related to structured programming and flow-charting logic, for example, are nicely explained in book for computer programming, but are not addressed in this text. Experienced computer programmers will still need to study the computer-related features of Custom Macro in order to learn the syntax of the Custom Macro language, as well as to discover related CNC applications for computer-programming-like commands. What are variables? One of the most important features of any programming language, including any version of parametric programming, is the ability to incorporate variables. Though the syntax and structure for variable usage changes from one version of parametric programming or computer programming to another, the general applications remain remarkably similar. If you have experience with computer programming, you know what variables are. The use and application of variables in Custom Macro are much the same as they are in any programming language. However, people that have no computer programming experience may find it somewhat difficult to understand the concept of variables. For this reason, we begin with two simple analogies to help you understand variables. Comparison to offsets Variables are temporary storage locations (called registers) for numerical values. A value can be placed into a variable register and stored for use later. In this sense, you can think of any tool offset as being a kind of variable. With a tool offset, a value is stored in an offset register for (later) use by a program. Depending FANUC CERT - Custom Macro Page 1 ©2015 CNC Concepts, Inc. Lesson 2: Variables upon the compensation type, the offset value could represent (among other things) the length of a tool, a milling cutter radius, a wear amount, or a single-point turning tool’s nose radius. With tool length compensation, for example, the offset register represents a tool length compensation value (commonly the tool's length). The programmer includes a command in the program that points at the cutting tool’s tool length compensation offset number. With FANUC, a G43 command specifies that tool length compensation is being instated and an H word specifies the offset number. Consider this command: N015 G43 H02 Z0.1 It tells this CNC to look in tool length compensation offset register number two to find the tool length compensation value for this tool. Since the programmer will not know the length of each tool as the program is written, they can utilize each of the machine’s tool offsets as a kind of variable. In essence, the programmer is creating a program that will function correctly regardless of the cutting tool's eventual length. At the machine during setup, the setup person will measure the tool length compensation value for tool number two and enter it into tool offset number two. In this way, the setup person is manually assigning the value of a variable that corresponds to tool number two’s length compensation value. This value means nothing to the control until the tool length compensation command (the G43 command) is executed. The value of any variable can be changed whenever necessary, just as any tool offset value can be changed at any time. During the production run, for instance, the operator can adjust the tool length compensation offset value as needed to compensate for tool wear. To summarize the similarity between offsets and variables, variables are storage locations (registers) for values. Like offsets, a value (possibly the result of a calculation) can be stored in the variable register for future use. Like offsets, the control will not know the meaning of the variable until it is referenced in some way during the program's execution. And like offsets, the values of most variables can be shown on the display screen. Some variables can even be entered and/or modified by the CNC operator though the MDI panel keyboard and display screen, just like offsets. Flexibility of variables Variables allow much more flexibility than tool offsets. In almost all cases, a given tool offset register is used for only one form of compensation, and the compensation type is quite rigid in its use of offsets. With tool length compensation, for example, the offset can only be used to represent the tool length compensation value. If you do not like the way your tool length compensation feature functions, there is nothing you can do about it. By comparison, a Custom Macro variable can be used to represent almost anything. Among the countless things variables can represent are: an axis position, an arc radius, a chamfer size, a spindle speed or feedrate, a tool diameter, a tool station number, and almost anything else you can think of. Indeed, your own imagination and ingenuity are the only things that limit what variables can represent. Comparison to memories in a calculator Variables within Custom Macro programs can also be thought of as like memories in an electronic calculator. With a calculator, you can store a value that is needed within your calculations. This memory value can be quickly referenced as required by pressing a button or two. Calculator memory registers are usually referenced with a memory number (memory 1, memory 2, memory 3, etc.). In like fashion, variables within Custom Macro programs can be used to represent constant values (possibly the result of an arithmetic expression) and can be referenced as required within the Custom Macro program. And like calculator memories, variables in Custom Macro are reference by a variable number. Here are some of the many applications for variables: • Variables are used to get arguments (input data) into the Custom Macro program to tell it how to behave. • Variables work as temporary storage registers for values that will be used throughout the Custom Macro program. • Variables give the programmer the ability to set and step counters. • Variables can be used to set flags that tell the Custom Macro how to behave. • Variables can be used as constant values that seldom change. FANUC CERT - Custom Macro Page 2 ©2015 CNC Concepts, Inc. Lesson 2: Variables As with any computer programming language, there are different types of variables in Custom Macro. Most versions of parametric programming, including Custom Macro, do not require that you declare the variable type as you must do in many computer programming languages. Even so, the Custom Macro programmer must understand the different variable types in order to use them appropriately in Custom Macro programs. Variables types There are five types of variables in Custom Macro. With but one exception, all variables in Custom Macro are represented by a pound sign (#) and a number. The number next to the pound sign specifies the variable number and also implies its type. Arguments Arguments specify input data (values that can change from one time the Custom Macro program is used the next). They tell the Custom Macro program how to behave right now - or this time. They are especially helpful in part-family and user-created canned cycle applications. There are actually two ways to use arguments in Custom Macro based on which of these two these application categories is being developed. Arguments with user created canned cycle applications With user-created canned cycles, it is usually best to specify arguments in a G65 command, as is briefly introduced in lesson one. Since user-created canned cycles only machine a small portion of the workpiece, and since they may have to be executed several times by the main program, G65 makes a very convenient way of passing data to the Custom Macro program since it resembles a control-based canned cycle command. The G65 command Although the G65 command is actually a CNC-related feature of Custom Macro and most CNC-related features are introduced after we're finished describing computer-related features, we must introduce it now. It is essential for understanding user created canned cycle applications, and we use it often throughout this text. The G65 command is the most common way of specifying arguments for user-created canned cycle applications (there are others, which are presented in lesson seven). Within the G65 command, a P word specifies the program number of the Custom Macro program. Then you specify the list of arguments. Here is a list of argument letter-addresses allowed in Custom Macro when argument assignment number one is used (we'll describe another argument assignment type in lesson seven). A, B, C, D, E, F, H, I, J, K, M, Q, R, S, T, U, V, W, X, Y, and Z As you can see, not all letters of the alphabet are allowed as arguments. Letters G, L, N, O, and P are missing. You must choose from those letter-addresses that are allowed by Custom Macro. For reasons we will explain much later, we also recommend avoiding combinations of I, J, and K (you can use one of these without issue). Though not all letter-addresses are allowed as arguments, Custom Macro gives you remarkable flexibility with argument naming. In most cases, a logical argument name can be chosen (A for angle, R for radius, T for tool, D for depth, and so on). When programming for user created canned cycle applications, you will want to take advantage of this argument naming flexibility. IMPORTANT! Include a decimal point with every argument value specified in the G65 command. Without a decimal point, the machine might use the fixed format for the letter-address word to interpret the argument value (a parameter controls how values specified will be interpreted without a decimal point). With fixed format, the interpretation method changes from one word type to another. The letter-address X, for example, has a four place decimal format in the inch mode (depending upon the increment system). If the decimal point is left out, a value of X1 could be taken as X0.0001 (again, depending upon the parameter setting). However, letter-address B has a three place format. A value of B1 (without the decimal point) could be taken as B0.001. So be sure to include a decimal point in every letter-address argument to avoid confusion regarding decimal point placement. Consider the drawing shown in the next illustration. FANUC CERT - Custom Macro Page 3 ©2015 CNC Concepts, Inc. Lesson 2: Variables The right side of this workpiece must be milled. The previous illustration shows an example of a very simple application. We limit our discussion at this time to argument name selection and will not (yet) show the Custom Macro program. Notice that the right side of this workpiece must be milled. Based on knowing your company’s work, maybe you know that this is but one of many different (yet similar) workpieces that requires the right side to be milled. Rather than writing the redundant command needed to mill this surface in over 300 programs, you wish to write a user defined canned cycle Custom Macro program to mill the right side of any workpiece. Consider the next illustration. These are the letter-address arguments used for this user created canned cycle application. FANUC CERT - Custom Macro Page 4 ©2015 CNC Concepts, Inc. Lesson 2: Variables The previous illustration shows the arguments chosen to represent values that change from one workpiece to another. Since every dimension relative to the right side of the workpiece changes, every dimension requires an argument assignment. Note that only the dimensions related to milling the right side require arguments. By the way, the illustration demonstrates a very good way of graphically documenting your Custom Macro for everyone to see. Anyone involved with this application will easily know the meaning of each argument being assigned. Note also that arguments are chosen in a logical manner. Letter-addresses X, Y, and Z represent the position of the corner of this workpiece. Letter-address W represents the width of the workpiece and H is the height. Letter-address T represents the thickness. Letter-address D represents the tool diameter and F represents the machining feed-rate. Since this program will be used often, argument names should be easy to remember. As the Custom Macro programmer, you select argument names used with your applications. Do so in as logical a manner as possible. Assuming the Custom Macro program has already been written and verified, here is a program that will machine the entire workpiece that includes a call to the right-end-milling Custom Macro program. Program: O0001 (Program number) N005 T01 M06 (Place 1” end mill in spindle) N010 G54 G90 S400 M03 T02 (Select coordinate system, absolute mode, turn spindle on, and get next tool ready) N015 G00 X5.0 Y0 (Rapid over to the workpiece) N020 G43 H01 Z0.1 (Instate tool length compensation, move up close to work surface) N025 G65 P1000 X0. Y0. Z0. W5.0 H3.0 T0.5 D1.0 F5.0 (Mill right side) N030 G91 G28 Z0 M19 (Move to tool change position, orient spindle) N035 M01 (Optional stop) N040 T02 M06 (Place 1/2 drill two in spindle) N045 G54 G90 S800 M03 T01 (Select coordinate system, absolute mode, start spindle, get tool number one ready) N050 G00 X0.5 Y0.5 (Rapid to first hole position) N055 G43 H02 Z0.1 (Instate tool length compensation, rapid up close to work surface) N060 G81 R0.1 Z-0.85 F4.0 (Drill first hole) N065 Y2.5 (Drill second hole) N070 X4.5 (Drill third hole) N075 Y0.5 (drill fourth hole) N080 G80 (Cancel cycle) N085 G91 G28 Z0 M19 (Return to tool change position, orient spindle) N090 M30 (End of program) Once again, note how little of this program deals with the milling of the right side. Only one command is involved (line N025). If more workpieces must be milled by this program, it is easy to specify more calling commands (G65) to program number O1000. Before we can show the Custom Macro program for this application, you must be exposed to another variable type, called local variables. Local variables will be discussed a little later. FANUC CERT - Custom Macro Page 5 ©2015 CNC Concepts, Inc. Lesson 2: Variables Arguments with part-family applications While G65 is a very helpful command and is usually the method of choice for user-created canned cycle applications, it is not always the most convenient way to get data into a Custom Macro. When programming part-family applications, the entire workpiece is being machined by a Custom Macro program (or group of programs). It is usually more convenient to specify argument values at the beginning of the program rather than call the Custom Macro program from another program. This involves using another type of Custom Macro variable. Common variables can be used for this purpose. With the exception of letter-address arguments, naming for all variables in Custom Macro begins with a pound sign (#). The number that follows the pound sign implies variable type. Common variables range in the #100 series. At least fifty common variables are available (it is likely that your machine has more), ranging from #100 through (at least) #149. We will explain common variables in greater detail later. For now, we will only discuss their use as arguments in part-family applications. Though the naming of each argument must be well documented, common variables make the best arguments with part-family applications. We use the workpiece shown earlier again for our example. But we will change the criteria for the application. Now we are not only milling the right end of the workpiece for a variety of different workpieces. Instead, this workpiece is but one of over 100 workpieces in a part-family. All of the workpieces have the four 1/2” diameter holes. All holes are 0.5 in from each edge. This new criteria (machining the entire workpiece) changes the application from a user-created canned cycle application to a part-family application. The next illustration shows the documentation for this application, including the related common variable arguments. Argument documentation for example part-family application. Notice that the argument names themselves (#100 - #108) have no special meaning. There is no way to relate the variable number to the workpiece attribute it represents. For this reason, argument meanings must be well documented in the program as well as in any supporting documentation for this program. Here is the Custom Macro program that will machine any workpiece in the part-family. O0002 (Program number) (Argument assignments) #100 = 0 (Left side position in X) FANUC CERT - Custom Macro Page 6 ©2015 CNC Concepts, Inc. Lesson 2: Variables #101 = 0 (Lower side position in Y) #102 = 0 (Top surface of workpiece in Z) #103 = 5.0 (Length of workpiece in X) #104 = 3.0 (Width of workpiece in Y) #105 = 0.5 (Workpiece thickness) #106 = 1.0 (Milling cutter diameter) #107 = 400 (RPM for milling cutter) #108 = 5.0 (Feed-rate of milling cutter in IPM) (Machining portion of the program) N005 T01 M06 (Place 1” end mill in spindle) N010 G54 G90 S#107 M03 T02 (Select coordinate system, absolute mode, turn spindle on, and get next tool ready) N015 G00 X[#100 + #103 + #106/2] Y[#101 - 0.1 - #106/2] (Rapid over to the workpiece) N020 G43 H01 Z[#102 + 0.1] (Instate tool length compensation, move up close to work surface) N025 G01 Z[#102 - #105 - 0.1] F50.0 (Fast feed past surface) N030 Y[#101 + #104 + 0.1 +#106/2] F#108 (Mill right side) N035 G00 Z[#102 + 0.1] (Rapid to above top surface) N035 G91 G28 Z0 M19 (Move to tool change position, orient spindle) N040 M01 (Optional stop) N045 T02 M06 (Place 1/2 drill two in spindle) N050 G54 G90 S800 M03 T01 (Select coordinate system, absolute mode, start spindle, get tool number one ready) N055 G00 X[#100 + 0.5] Y[#101 + 0.5] (Rapid to first hole position) N060 G43 H02 Z[#102 + 0.1] (Instate tool length compensation, rapid up close to work surface) N065 G81 R[#102 + 0.1] Z[#102 - #105 - 0.18] F4.0 (Drill first hole) N070 Y[#101 + #104 - 0.5] (Drill second hole) N075 X[#100 + #103 - 0.5] (Drill third hole) N080 Y[#101 + 0.5] (drill fourth hole) N085 G80 (Cancel cycle) N090 G91 G28 Z0 M19 (Return to tool change position, orient spindle) N095 M30 (End of program) Since this is the first complete Custom Macro programming example, we want to explain it is some detail. First of all, notice how well the common variable arguments #100 through #108 are documented. A message in parentheses is placed next to each argument to tell anyone viewing this program exactly what each common variable argument means. An operator or setup person could confidently modify these arguments during setup. Second, notice that arguments are only required for those things that change from one workpiece in the partfamily to another. Any values that do not change (like the speed and feed for 1/2” drill, the 0.5 inch distance from each corner to each hole location, the 0.1” approach distance, etc.) are treated as a constant values. These constant values, of course, cannot be changed by modifying argument values. FANUC CERT - Custom Macro Page 7 ©2015 CNC Concepts, Inc. Lesson 2: Variables Third, notice that several calculations are being done within motion commands to determine coordinate values. While arithmetic functions are discussed in detail in lesson three, it should be relatively easy to determine what is happening. Look first at line N015. The X and Y coordinates depend on the result of a calculation. Based on the current values of the arguments set at the beginning of this program, the X value will be calculated as 5.5 (the result of 0 + 5.0 + 1.0 / 2). The Y value will be calculated as -0.6 (the result of 0 - 0.1 - 1.0 / 2, which is negative). When executed, this command will appear to the control as N015 G00 X5.5 Y-0.6. Keep in mind, however, that if the values of #100, #103, and/or #106 are changed (as will be necessary for other workpieces in the family), the coordinates calculated in line N015 will also change. Finally, notice the brackets () that enclose the calculations being made to determine the values of CNC words. These brackets are not parentheses. Custom Macro requires the use of brackets when making calculations to determine the value of CNC words. Brackets are described in greater detail during our discussion of arithmetic functions in lesson three. While this is a very simple part-family application, it should nicely stress how arguments can be introduced into part-family Custom Macro programs. Though we are just scratching the surface of what can be done, it introduces the improved functionality that Custom Macro provides. Please note once more the distinction between argument assignment for user-created canned cycle applications and argument assignment for part-family applications. With part families, the Custom Macro is machining the entire workpiece. Assigning arguments in the main program with common variables is usually the most convenient method of argument assignment. On the other hand, since user-created canned cycle applications always require a separate Custom Macro program to perform the canned-cycle-like operation, and since this program may be executed several times when machining one workpiece, arguments must be set in the main program (in the G65 command). You must be able to modify arguments from one time the user-created-canned-cycle Custom Macro program is executed to the next. Local variables Local variables have two purposes. The primary purpose for local variables is to represent letter-address arguments that are specified in a G65 command. The secondary purpose is for use in general purpose calculations (just like common variables which are described a little later). Local variables with argument assignment number one As you now know, when a Custom Macro program is called with a G65 command, arguments take the form of letter-addresses. And again, allowable letter-address arguments with argument assignment number one include: A, B, C, D, E, F, H, I, J, K, M, Q, R, S, T, U, V, W, X, Y, and Z Though it may be a little confusing, you are not allowed to directly reference an argument within the Custom Macro program with its letter-address. If you try to do so, the machine will confuse the argument name (A, B, C, X, Y, Z, etc.) with the actual CNC meaning for the letter-address. For this reason, you must reference arguments in the Custom Macro program by their corresponding local variables. Like all variable types in Custom Macro (except letter-address arguments), local variables begin with a pound sign and are specified with a number. The next table shows the list containing all allowable letter-address arguments and their corresponding local variable numbers. This is the most popular form of argument assignment, called argument assignment number one. FANUC CERT - Custom Macro Page 8 ©2015 CNC Concepts, Inc. Lesson 2: Variables Letter Local Variable I #4 U #21 A #1 J #5 V #22 B #2 K #6 W #23 C #3 M #13 X #24 D #7 Q #17 Y #25 E #8 R #18 Z #26 F #9 S #19 H #11 T #20 Letter-address arguments and their representing local variables when using argument assignment number one. When a G65 command is executed, two things will happen. First, all local variables that have corresponding letter-address arguments in the G65 command will be set. Second, the Custom Macro specified by the P word will be executed. During the execution of the Custom Macro, local variables will contain the values of arguments specified in the G65 command. Within the Custom Macro, local variables must be used whenever you need to reference argument values specified in the G65 calling command. As an example, consider this command: N025 G65 P1000 X0. Y0. Z0. W5.0 H3.0 T0.5 D1.0 F5.0 (Mill right side) It first sets the local variables corresponding to letter-addresses X, Y, Z, W, H, T, D, and F. In this case, local variables will be set as follows: • • • • • • • • #24 (for X) set to 0 #25 (for Y) set to 0 #26 (for Z) set to 0 #23 (for W) set to 5.0 #11 (for H) set to 3.0 #20 (for T) set to 0.5 #7 (for D) set to 1.0 #9 (for F) set to 5.0. Second, the machine will execute program O1000. When the programmer needs to reference the value of argument X within the Custom Macro program, #24 must be used. It is also important to know that all other local variables (#1, #2, #3, etc.) will not be set (or better stated, they will be set to nothing), and will have no value when the Custom Macro is executed. FANUC refers to this condition (having no value) as vacant. Computer programmers may refer to this condition as null. Do not confuse vacant (or null) with zero. Zero is a value. When a variable is vacant, it has no value. We will discuss implications of vacant variables later. As the name implies, local variables remain active only during the execution of the Custom Macro program. At the completion of the program (the M99 command), all local variables are set back to vacant. So again, local variables only remain active during the execution of the Custom Macro. Here is a complete example that uses the workpiece shown earlier (milling the right side of the workpiece). The arguments used for this user-created canned cycle application are shown in the previous illustration. Once again, here is the main program. Program: O0001 (Program number) N005 T01 M06 (Place 1” end mill in spindle) N010 G54 G90 S400 M03 T02 (Select coordinate system, absolute mode, turn spindle on, and get next tool ready) N015 G00 X5.0 Y0 (Rapid over to the workpiece) FANUC CERT - Custom Macro Page 9 ©2015 CNC Concepts, Inc. Lesson 2: Variables N020 G43 H01 Z0.1 (Instate tool length compensation, move up close to work surface) N025 G65 P1000 X0. Y0. Z0. W5.0 H3.0 T0.5 D1.0 F5.0 (Mill right side) N030 G91 G28 Z0 M19 (Move to tool change position, orient spindle) N035 M01 (Optional stop) N040 T02 M06 (Place 1/2 drill two in spindle) N045 G54 G90 S800 M03 T01 (Select coordinate system, absolute mode, start spindle, get tool number one ready) N050 G00 X0.5 Y0.5 (Rapid to first hole position) N055 G43 H02 Z0.1 (Instate tool length compensation, rapid up close to work surface) N060 G81 R0.1 Z-0.85 F4.0 (Drill first hole) N065 Y2.5 (Drill second hole) N070 X4.5 (Drill third hole) N075 Y0.5 (drill fourth hole) N080 G80 (Cancel cycle) N085 G91 G28 Z0 M19 (Return to tool change position, orient spindle) N090 M30 (End of program) Here are the local variable representations of letter-address arguments specified in line N025. X: #24, Y: #25, Z: #26, W: #23, H: #11, T: #20, D: #7, F: #9 As stated previously, line N025 will mill the right side of any workpiece. Armed with your new knowledge of local variables, you should now be able to understand the Custom Macro program: O1000 (Custom Macro to mill right side) N1 G00 X[#24 + #23 + #7/2] Y[#25 - 0.1 - #7/2] (Rapid to starting position) N2 G01 Z[#26 - #20 - 0.1] F50. (Fast feed to work surface) N3 Y[#25 + #11 + #7/2] F#9 (Mill right side) N4 G00 Z[#26 + 0.1] Rapid to above workpiece) N5 M99 (End of Custom Macro) Coordinates are the result of arithmetic calculations, just as they were in the part-family example. In line N1, for example, and with the call statement set as it is in line N025 of the main program, the X position will be calculated as X5.5 because letter-address argument X (referenced by #24) is set to 0, W (referenced by #23) is set to 5.0, and D (referenced by #7) is set to 1.0. The control will calculate this X position as 0 + 5.0 + 1.0/2 which results in a value of 5.5. Local variables with argument assignment number two The implications of argument assignment number two are described, and an example is shown, in lesson seven. For now, we just want to point out that this form of argument assignment exists. It is seldom needed, but there are applications that can benefit from its use. The vast majority of applications requiring a G65 calling command will require argument assignment number one. The next table shows local variable assignments when argument assignment number two is used. FANUC CERT - Custom Macro Page 10 ©2015 CNC Concepts, Inc. Lesson 2: Variables Letter Local Variable K (3) #12 K (7) #24 A #1 I (4) #13 I (8) #25 B #2 J (4) #14 J (8) #26 C #3 K (4) #15 K (8) #27 I (1) #4 I (5) #16 I (9) #28 J (1) #5 J (5) #17 J (9) #29 K (1) #6 K (5) #18 K (9) #30 I (2) #7 I (6) #19 I (10) #31 J (2) #8 J (6) #20 J (10) #32 K (2) #9 K (6) #21 K (10) #33 I (3) #10 I (7) #22 J (3) #11 J (7) #23 Letter-address arguments and their representing local variables when using argument assignment number two. Notice letter-addresses A, B, C are followed by ten sets of I, J, and K. Consider this G65 command: N050 G65 A2.0 B3.0 C2.0 I3.0 J4.0 K3.0 I5.5 J8.0 K3.3 Here is how the local variables will be assigned: • • • • • • • • • #1 (for A) set to 2.0 #2 (for B) set to 3.0 #3 (for C) set to 2.0 #4 (for the first I) set to 3.0 #5 (for the first J) set to 4.0 #6 (for the first K) set to 3.0 #7 (for the second I) set to 5.5 #8 (for the second J) set to 8.0 #9 (for the second K) set to 8.0 Again, we explain why you might consider using this method of argument assignment in lesson seven. How do you specify which argument assignment type you are using? Actually, you don't. Selection is automatic. You simply specify the letter-address arguments you need. If you specify letters addresses other than A, B, C, I, J, and K, in a G65 command, the machine will use argument assignment number one. If you specify only these letter-addresses, it will use argument assignment number two. This is the reason we caution you during the description of arguments for user created canned cycle applications about not using combinations of I, J, and K in G65 commands with argument assignment number one. You can use one of them (I, J, or K) without concern. But odd things can happen if you include two or all three of them and they are not in alphabetical order within the G65 command. We explain what can happen in lesson seven. More on vacancy As stated, when a letter-address argument is left out of the call statement, its corresponding local variable is set to vacant when the Custom Macro is called by the G65 command. The Custom Macro representation for vacancy is #0. In the previous right-side-milling Custom Macro, for example, all arguments must be specified in the G65 command in order for the Custom Macro program to work. By the way, we call arguments that must be included in the call statement mandatory arguments. In our example, all arguments are mandatory arguments. Based on the way the Custom Macro is written, if the programmer using this Custom Macro program forgets to include one or more of the mandatory arguments in the G65 command, it will be difficult to predict what will happen. Surely the Custom Macro will not behave as expected. FANUC CERT - Custom Macro Page 11 ©2015 CNC Concepts, Inc. Lesson 2: Variables However, by knowing that a local variable is left set to vacant (#0) if it is not included in the call statement, and by knowing how to perform tests in the Custom Macro (shown in lesson four), the Custom Macro programmer can have the program test all local variables being used. If any are vacant, the person using the Custom Macro program has made a mistake, and the Custom Macro program can generate an alarm to halt the machine before any damage can be done. We will show how to perform tests and generate alarms in lesson four. This is one of several times when understanding vacancy is important. Keeping track of local variables Admittedly, local variable numbers do not make much sense in argument assignment number one. Even for experienced Custom Macro programmers, it is difficult to remember which local variable corresponds to a given letter-address argument. For this reason, you must keep the appropriate argument assignment table handy as you write Custom Macros. One suggestion is to write the letter-address argument (maybe in a different color) next to the local variable number as you write the Custom Macro program. For example, say you must reference the value of letteraddress argument D in the Custom Macro. When you look at the local variable assignment table, you find that local variable #7 represents letter-address D. As you write #7 in the Custom Macro, also write the letter “D” in a different color close by to remind you of which letter-address argument #7 represents. Local variables at different program nest levels As with sub-programming, it is possible to call one Custom Macro program from another. Consider these three programs: O0001 (Main Program - nest level zero) N005 T01 M06 N010 G90 G54 S500 M03 N015 G43 H01 Z0.1 M08 N020 G65 P1001 A3.0 B2.0 C4.0 . . . N140 M30 (End of main program) O1001 (Custom Macro program - nest level one) (#1 will be 3.0) . . . G65 P1002 A5.0 R3.0 F4.0 . . . M99 O1002 (Custom Macro program - nest level two) (#1 will be 5.0) . . . M99 FANUC CERT - Custom Macro Page 12 ©2015 CNC Concepts, Inc. Lesson 2: Variables One Custom Macro program (O1002) is being called from another (O1001). This is called program nesting. The main program (O0001) is at nest level zero. When program O1001 is called (from O0001), it becomes nest level one. When program O1002 is called (from O1001), it becomes nest level two. FANUC allows program nesting of up to five levels, meaning this process could be repeated three more times. When the M99 in program O1002 is executed, the CNC is sent back to program O1001 (and back to nest level one). When the M99 in program O1001 is executed, the CNC is sent back to program O0001 (and back to nest level zero). While this is not a complete example, our point has to do with local variable assignments. When you use a G65 command and call a custom macro, the local variable assignments are set and will be retained at the current nest level, even when another G65 command is used. When the CNC eventually returns to the program, the local variables will still be properly set for that nest level. The CNC creates a new (and complete) set of local variable assignments in each nest level. In line N020 of the main program, for example, the G65 command calls program O1001. Local variables for letter-addresses A, B, and C are set (#1, #2, and #3). This set of local variables for nest level one (along with all others that are set to vacant) will be active whenever the CNC is executing program O1001. The G65 command in program O1001 calls program O1002 and another complete set of local variables will be assigned for nest level two, including #1 (for letter-address A), #18 for letter-address R, and #9 for letteraddress F. And again, all other local variables will be set to vacant. Letter-address A is being used in both G65 commands. It is surely used for two different purposes, and there will be no conflict related to the setting of local variable #1 while these programs are running. While the CNC is executing program O1001 (even after returning from executing program O1002), the current value of local variable #1 will be 3.0 (from letter-address A in line N050 of the main program). While the CNC is executing program O1002, the value of #1 will be 5.0. Each program nest level with have its own complete set of local variable assignments. Secondary purpose for local variables: general purpose calculations Like other variables, you can use local variables to represent just about anything, as long as they are not being used to represent letter-address arguments, and as long as the values they represent do not require sharing among programs. Remember, local variables are set back to vacant whenever an M99 or M30 command is executed. Whenever you are using argument assignment number one (almost always), you can use local variables #27 through #33 without fear of overwriting a local variable being used to represent a letter-address argument. Additionally, local variables #10, #12, #14, #15, and #16 are not used in argument assignment number one, providing you with five more. You can, of course, use any local variable for this secondary purpose as long as you are sure that the local variable is not being used to represent a letter-address argument. In Custom Macro programs that have not been called with a G65 command, of course, you will have all thirty-three local variables available for general purpose calculations. Reusing local variables For complex applications, it may be necessary to conserve variables so you don't run out. While having thirty-three local variables may sound like a lot, you may have applications that require more, even within one Custom Macro program. Once you are finished with a local variable in one portion of the program, you can reuse it in another. You just have to be careful to ensure that you are truly finished with its use before using it again. This concept of conserving variables is only applicable for complex applications, and there's a bit more to it. You should use the appropriate variable type whenever possible. For local variables, use them whenever it is not necessary to share its value among programs (which is often). In this way you can conserver the use of other variable types (which are yet to be introduced) for times when there is a need to share values among programs. NOTE: You may not be faced with this kind of challenge for a while. Your first few Custom Macro applications will probably be pretty simple. It won't matter which variable type you choose. But it doesn't hurt to start off using good habits. We will revisit this concept of conserving variables in upcoming lessons. FANUC CERT - Custom Macro Page 13 ©2015 CNC Concepts, Inc. Lesson 2: Variables Displaying local variable values FANUC controls allow you to display local variable values. This can be quite helpful when verifying Custom Macro programs. Though the procedure to view variables varies among control models, you will find the variable pages on current control models within the [OFS/SET] (offset and setting) display screen pages. Look for the soft key named MACRO. Use the page up and down keys to scroll through the variable registers. Common variables Common variables are introduced during the discussion of arguments used in part-family applications. Again, common variables range in the #100 series and at least fifty are available, from #100 through at least #149. It is likely that your machine has more. If your machine has two hundred common variables, for instance, they will range from #100 through #199. As the name implies, common variables remain active even after the execution of the Custom Macro program. Most FANUC CNCs are set to retain common variable values (again, in the #100 series) until the power to the machine is turned off, at which time they are set back to vacant. Parameter setting: A parameter (number 6001, bit 6 for a FANUC 0iD) controls what happens to common variables when an end-of-program M30 or M02 word is executed. If this parameter is set to 1, common variables (#100-#199) will not be cleared when an end-of-program command (or when the RESET button is pressed). This is our recommended setting. If this parameter is set to 0, common variables are set to vacant when the end-of-program word is executed (and when the RESET button is pressed). Parameters vary among FANUC control models. You must consult your FANUC manual to find the related parameter for other controls. While the global nature of common variables has special implications, for now, we will concentrate on basic uses for common variables. Along with being used as arguments in part-family applications, common variables have a variety of applications. (Many of these applications may be better handled with local variables, as long as the application does not require the sharing of values among programs.) To minimize redundant calculations In the simple right-end-milling Custom Macro application shown earlier, the diameter of the end mill is passed to the Custom Macro program (with D). Yet in the Custom Macro program, it is necessary to utilize the end mill’s radius. The redundant calculation #7/2 is done three times in this Custom Macro program. While this is a simple calculation and is only performed three times, imagine that the end mill is being used to machine several surfaces. In this case, it is likely that the expression #7/2 would be repeated many times. These calculations are tedious and error prone to write, and force the CNC to perform excessive calculations. This can result in wasted program execution time. Common variables can be used to minimize redundant calculations. The result of a redundant calculation can be stored into a common variable early on in the Custom Macro program. From this point, the variable can be used instead of the redundant calculation. Here is the Custom Macro program to mill the right side of a workpiece again, modified to take advantage of this technique. O1000 (Custom Macro to mill right side) N1 #100 = #7/2 N2 G00 X[#24 + #23 + #100] Y[#25 - 0.1 - #100] (Rapid to starting position) N3 G01 Z[#26 - #20 - 0.1] F50. (Fast feed to work surface) N4 Y[#25 + #11 + #100] F#9 (Mill right side) N5 G00 Z[#26 + 0.1] (Rapid to above workpiece) N6 M99 (End of Custom Macro) For general purpose calculations Common variables can be used for general purpose calculations. Here is another version of the right-sidemilling Custom Macro program that calculates all positioning coordinates up-front, storing the results of all calculations in common variables. The movement commands simply reference the values previously calculated. O1000 (Custom Macro to mill right side) FANUC CERT - Custom Macro Page 14 ©2015 CNC Concepts, Inc. Lesson 2: Variables N1 #100 = #24 + #23 + #7/2 N2 #101 = #25 - 0.1 - #7/2 N3 #102 = #26 - #20 - 0.1 N4 #103 = #25 + #11 + #7/2 N5 #104 = #26 + 0.1 N2 G00 X#100 Y#101 (Rapid to starting position) N3 G01 Z#102 F50. (Fast feed to work surface) N4 Y#103 F#9 (Mill right side) N5 G00 Z#104 (Rapid to above workpiece) N6 M99 (End of Custom Macro) Although we do not recommend calculating coordinates in this manner (it is better to do so as is shown in previous examples to minimize the potential for adding program execution time), this example shows how common variables can be used to store values that will be used later. As you develop your own Custom Macro programs, there will be many times calculations must be made. As counters Looping (which is shown in lesson four) often requires that variables change each time the loop is executed. Common variables can be used to represent the loop counter and any variable that must be initialized at the loop's beginning and changed after each execution at the loop's end. As flags It is sometimes necessary to set a flag that tells the Custom Macro program how to behave. For example, you may wish to set up criteria in your right-side milling Custom Macro program that allows the milling cutter to climb or conventional mill. A special flag argument will specify which milling method you intend to use. Other examples of when flags are required (that we will discuss in greater detail during our discussion of conditional branching in lesson four) include right hand versus left hand tooling, inch versus metric input, and whether or not coolant is to be used. While letter-address arguments in the G65 command are often used to set flags, the global nature of common variables makes it possible to maintain the values of flags even among several programs. IMPORTANT! With the exception of flags, the variable applications just shown can probably be handled with local variables. For simple applications, it really doesn't matter which variable type you choose. But for applications that will require lots of values be shared by multiple programs, use local variables to represent values that need not be shared among programs. Reserve common variables for times when values must be shared among programs. Displaying common variables FANUC CNCs allow you to display common variable values. This can be quite helpful when verifying Custom Macro programs. Though the procedure to view variables varies among control models, you will find the variable pages on current control models within the [OFS/SET] (offset and setting) display screen pages. Permanent common variables These variables range in the #500 series. FANUC CNCs will have at least ten permanent common variables (from #500 through #509). Most have many more, possibly ranging from #500 through #999. As the name implies, these variables are retained even after the power is turned off. In this sense, they are much like tool offsets. And like tool offsets, most CNCs let you modify the values of permanent common variables through the MDI panel keyboard and display screen. Since some CNCs have so few permanent common variables (again, as few as ten), be sure to reserve their use for those applications when it is necessary to retain data from day to day. One example is a with utility Custom Macro program for part counting. Say you wish to set up a part counter. The machine will run 750 workpieces and then halt the machine (this application is shown in lesson six). As you begin running production, your Custom Macro based part counter will begin counting workpieces. It isn’t very likely that you will finish all 750 workpieces before the end of the day/shift. It will more likely take several days to complete the production run. In this case, you will need your part counting Custom Macro program to remember where it left off from day to day (after the power is turned back on). If you store the FANUC CERT - Custom Macro Page 15 ©2015 CNC Concepts, Inc. Lesson 2: Variables current part count in a #500 series permanent common variable, the part count value will not be lost when the power is turned off. Do you have a touch-probing system? Permanent common variables are also used to retain data when certain accessory devices are used. If you have a spindle-probing system, for example, and if Custom Macro is used to program the probe, it is quite likely that at least some permanent common variables are already being used to store calibration values for the probing system. Before you begin using any #500 series permanent common variables, you must confirm that they are not already being used for other purposes. Probe calibration values stored in permanent common variables are commonly called system constants. Creating your own system constants Think of any constant hard-and-fixed value that you use throughout your CNC programming efforts. One that may come to mind is your rapid approach and feed-off distance. When programming in the inch mode, for example, many programmers like to stay a constant 0.100 in away from the surface being approached before staring a cutting motion. When cutting is completed, many programmers feed the tool away from the workpiece by this same 0.100 in amount. While this approach distance is quite safe, when large production lot sizes must be run, the time required for each approach and feed-off (at the cutting feed-rate) may be excessive. If all approach and feed-off movements are programmed with hard-and-fixed values (calculated by the programmer), it can be quite difficult to change the approach and retract values without a massive overhaul of the CNC program. If instead, a permanent common variable is used in which to store the rapid approach and feed-off distance (you could even use one permanent common variable for each), and if this variable is referenced in every CNC program during every tool’s approach and feed-off movement, changing the approach/feed-off distance will be as simple as changing one permanent common variable. And as stated, permanent common variables can be entered and changed through the keyboard and display screen, just like tool offsets. As you will see later, some FANUC control models even let you place a documenting label next to some of them which will be shown on the variable display screen page. Consider this program that drills a few holes and mills a slot. It assumes the rapid approach/feed-off distance is stored in permanent common variable #500. O0003 (Program that allows easy approach/retract distance change) N005 T01 M06 (Place 1/2 drill in spindle) N010 G54 G90 S800 M03 T02 (Select coordinate system, absolute mode, turn spindle on, and get tool number two ready) N015 G00 X.5 Y.5 (Rapid to first hole location) N020 G43 H01 Z#500 (Instate tool length compensation, rapid up to workpiece) N025 G81 R#500 Z-.85 F3.5 (Drill first hole) N030 Y3.5 (Drill second hole) N035 X4.5 (Drill third hole) N040 Y.5 (Drill fourth hole) N045 G80 (Cancel cycle) N050 G91 G28 Z0 (Return to tool change position) N055 M01 (Optional stop) N060 T02 M06 (Place 3/8 drill in spindle) N065 G54 G90 S950 M03 T03 (Select coordinate system, absolute mode turn spindle on and get tool three ready) N075 G00 X3.0 Y2.0 (Rapid to hole location) N080 G43 H02 Z#500 (Instate tool length compensation, rapid to approach position) N085 G81 R#500 Z-.55 F3.5 (Drill hole) N090 G80 (Cancel cycle) N095 G91 G28 Z0 (Return to tool change position) FANUC CERT - Custom Macro Page 16 ©2015 CNC Concepts, Inc. Lesson 2: Variables N100 M01 (Optional stop) N105 T03 M06 (Place 1/4” end mill in spindle) N110 G54 G90 S1000 M03 T01 (Select coordinate system, absolute mode turn spindle on and get tool one ready) N115 G00 X3.75 Y-[.125 + #500] (Rapid to slot approach position) N120 G43 H03 Z-0.125 (Instate tool length compensation, rapid to work surface) N125 G01 Y[4.125 + #500] F2.5 (Mill slot) N130 G00 Z#500 (Rapid to above work surface) N135 G91 G28 Z0 (Return to tool change position) N140 M30 (End of program) Any time a tool in this program approaches or feeds off (lines N020, N025, N080, N085, N115, N125, and N130), permanent common variable #500 is referenced. In lines N115 and N125, notice how the programmer can even perform a calculation that has the control figure out the correct approach/feed-off position based on the value of permanent common variable #500. With this program, changing the approach/feed-off distance will be as easy as changing the value of permanent common variable #500. Displaying permanent common variables FANUC controls allow you to display permanent common variable values. As with offset registers, you can even enter and modify permanent common variable values manually, using the MDI panel keyboard. Viewing variable values can be quite helpful when verifying Custom Macro programs. Though the procedure to display variables varies among control models, you will find the variable pages on current control models within the [OFS/SET] (offset and setting) display screen pages. Offsets as variables Any unused offset register can be used as a permanent common variable. We show all of the related offsetrelated system variables later (in lesson six). For now, we just mention one type, those used for tool length compensation on machining centers. With one popular FANUC control model, system variables ranging in the #2000 series are used to provide access to tool length compensation offset registers. #2001, for example, provides access to offset register number one. #2099 provides access to offset register number ninety-nine. If you are in need of more variables in an application, and if you are absolutely sure that certain offset registers will never be needed for normal CNC-related uses, you can use the unused offset registers as permanent common variables. Write-protecting permanent common variables Though this will probably not be of immediate importance, FANUC provides the ability to write protect a series of permanent common variables. This ability is helpful when the Custom Macro application uses a permanent common variable as a system constant (more on system constants will be presented later). Parameter setting: Two parameters are involved with write-protecting permanent common variables. Parameter numbers vary among FANUC control models. With a 0iD FANUC control, parameter number 6031 specifies the first permanent common variable and parameter number 6032 specifies the last permanent common variable in a series to be protected. If parameter number 6031 is set to 511 and parameter number 6032 is set to 520, permanent common variables from #511 through #520 will be write-protected. You must consult your FANUC manual to find the related parameters for other control models. Labeling permanent common variables It is possible to specify a label of up to eight characters for permanent common variables #500 through #549. With older control models, this label will appear on the display screen next to the variable's value, letting everyone know the purpose for the variable. With newer control models, the label can be used to reference the permanent common variable instead of using the #500 series number. The Custom Macro function SETVN (for set variable name) is used for this purpose. Multiple permanent common variables can be labeled in one SETVN command. Here is an example: FANUC CERT - Custom Macro Page 17 ©2015 CNC Concepts, Inc. Lesson 2: Variables SETVN 505[LENGTH, WIDTH, HEIGHT] Once this command is executed (one time), the display screen on older FANUC controls will show the word LENGTH next to the value of permanent common variable #505. WIDTH will be displayed next to #506. And HEIGHT will be displayed next to #507. With the SETVN command above executed (again, but one time), newer controls let you reference the value of permanent common variable #505 with either #505 or #LENGTH. The same goes for #506 and #507 (#WIDTH for #506 and #HEIGHT for #507). System variables Many of the CNC-related features of Custom Macro are accessed with system variables. These variables range from #1000 through about #12000. Many can also be referenced by names that are easier to remember than the system variable numbers (like [#_DATE] instead of #3011. System variables allow you to access many CNC functions, including current axis position, current values of offsets, alarm generation, and much more. System variables are described in lesson six. Summary of Custom Macro variables As you have seen, there are five types of variables in Custom Macro. 1) Arguments are used in part-family and user-created canned cycle applications to get input data into the Custom Macro. 2) Local variables, which get reset to vacant at the completion of the Custom Macro (at M99), are used to represent letter-address arguments coming from the G65 command. 3) Common variables, which can also be used as arguments (in family-of-parts applications), are used for a variety of purposes including general purpose calculations, counters, and minimizing redundant calculations. 4) Permanent common variables are used for applications which require that the value of a variable is retained from day to day. 5) System variables which allow access to many CNC related features of Custom Macro. While you should have a pretty good idea of what Custom Macro variables can do, remember that all presentations to this point have been rather elementary. There are some advanced implications yet to be discussed. We will address these advanced implications in up-coming lessons. Key Points for Lesson 2: Variables are temporary storage registers for values. Arguments provide input data. A G65 command is one way to get arguments into a Custom Macro program. Local variables can be used to reference letter-address arguments from within a Custom Macro program. Local variables can also be used for general purpose calculations. Common variables are used for general purpose calculations and especially when values must be shared among programs. Permanent common variables are required for applications when values must be retained from day to day. FANUC CERT - Custom Macro Page 18 ©2015 CNC Concepts, Inc.