In this article, we describe how to assign a value to variable while writing CNC program on CNC machines.
A value can be assigned to a variable in the general format;
#i = <some value or arithmetic expression>;
where i is the variable number. On the left-hand side, in place of i, an expression may also be used. Some examples are;
#1 = 10; (Stores 00010.000 in variable #1)
#1 = #1 + 1; (Redefines #1 by storing 00011.000 in it)
The term “NC statement” has been used without formally defining it. It is a program block involving at least one NC address, such as G, M, F, S, T, X, Y, and Z, except codes for calling a macro program (such as G65, G66, etc.) On the other hand, a macro statement simply assigns a value to a variable (#i =<some value or an arithmetic expression>), or jumps to a specified block number (GOTO_ and IF GOTO_), or uses a conditional statement (IF_THEN_, WHILE_DO_, and END_), or calls a macro program. To put it simply, a macro statement does not directly cause physical machine movement, whereas an NC statement directly controls the machine movement. An NC statement may or may not use macro variables/functions.
There are two major differences in the way the control treats NC statements and macro statements:
- If the program is executed in the single-block mode (there is a switch for this purpose on the MOP), its execution stops at the end of each NC statement, and proceeds to the next block only after the CYCLE START button is pressed again. However, the execution does not stop at the end of a macro statement, it proceeds to the next block. If it is desired to execute the macro statements also in single-block mode, set parameter 6000#5 to 1. In a normal situation, such a requirement would never arise, because a macro statement does not involve machine movement. However, in case of an error in the program, execute the macro statements one at a time to check the intermediate calculations.
- Although the program execution is block by block, the control prereads the next block and interprets it in advance, to speed up the execution. In the radius compensation mode, two blocks are preread, because the control needs to position the tool properly at the end of the current block, to suit the next path segment. However, all (or as many as possible) sequential macro statements are read and evaluated immediately. In fact, the control does not count a macro statement as a block. An NC statement constitutes a block.
Coming back to the discussion on defining variables, note that a variable always stores a value with minimum three decimal places, if the total number of digits does not exceed eight. If less than three decimal places are used, zeroes are added.
#1 = 1234; (Stores 01234.000)
#1 = 12345; (Stores 12345.000)
#1 = 123456 ; (Stores 123456.00)
#1 = 1234567; (Stores 1234567.0)
#1 = 12345678; (Stores 12345678)
If more than eight digits are specified, the additional digits might be converted to 0, after rounding up to eight digits (irrespective of decimal position), which may give unexpected results, as explained in the following example:
#1 = 123456.789; (Stores 123456.790 in variable #1)
#2 = 123456.794; (Stores 123456.790 in variable #2)
#3 = #2 – #1; (Stores 0.000 in variable #3)
However, in the Fanuc Oi control, specifying more than eight digits, for any value, generates an error message, “TOO MANY DIGITS,” and terminates the program execution. It will not store values like 123456.789 in a variable, and will display the error message. If, however, more than eight digits result after an arithmetic calculation, rounding is automatically done up to eight digits, and no error (alarm message) or warning (operator’s message) appear.
A variable can also be defined in a conditional manner;
#10 = 10 ;
#25 = 5;
IF [#10 GT #25] THEN #25 = #25 + 10; (TRUE condition, so #25 becomes 15.000)