0% found this document useful (0 votes)
132 views10 pages

The Use of Variables, Macros and Subroutines and Compatibility With FANUC Parametric Programming ......................... 3

This document discusses variables, macros, and subroutines in CNC programming and their compatibility with FANUC parametric programming. It describes numeric and alphanumeric variables, including their naming conventions and uses. It also covers defining and invoking macros/subroutines in both the FANUC 6M compatible style and the Ah-ha! programming style supported by Artisan CNC. Specific examples are provided to illustrate passing parameters, looping subroutines, and returning from macros.

Uploaded by

Jérôme GRANGE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views10 pages

The Use of Variables, Macros and Subroutines and Compatibility With FANUC Parametric Programming ......................... 3

This document discusses variables, macros, and subroutines in CNC programming and their compatibility with FANUC parametric programming. It describes numeric and alphanumeric variables, including their naming conventions and uses. It also covers defining and invoking macros/subroutines in both the FANUC 6M compatible style and the Ah-ha! programming style supported by Artisan CNC. Specific examples are provided to illustrate passing parameters, looping subroutines, and returning from macros.

Uploaded by

Jérôme GRANGE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Table of Contents

1 The Use of Variables, Macros and Subroutines and


Compatibility with FANUC Parametric Programming .........................3
1.1 Variables and their use 3
1.1.1 Introduction 3
1.1.2 Use of Variables in Programming 3
1.1.3 FANUC 6M Numeric Variables 4
1.1.4 Ah-ha! Alphanumeric Variables 5
1.2 Macros and Subroutines 6
1.2.1 Introduction 6
1.2.2 FANUC 6M - Compatible Programming 7
Introduction 7
Defining Mainline Programs, Macros, and Subroutines 7
Example 1
Invoking a Macro or Subroutine from
a Mainline G-Code program 8
Passing Parameters to a FANUC-style macro
Use of the “L” parameter for „looping on a subroutine‟ 10
Returning from a Macro or Subroutine 11
1.2.3 Ah-ha! Style Programming 12
Introduction 12
Defining Mainline Programs, Macros, and Subroutines 12
Passing Parameters to an Ah-ha! Style Subroutine
Example 2
Invoking a Macro or Subroutine from
a Mainline G-Code program 15
Returning from a Macro or Subroutine 15
........................ 1.3 Variables, Macros, and Subroutines - Miscellaneous Information
and Things to Remember 16
Example 3 - Use of Numeric Variables in a FANUC-style macro
Example 4 - A macro written in the Ah-ha! Programming Style
1 The Use of Variables, Macros and Subroutines and
Compatibility with FANUC Parametric Programming

1.1 Variables and Their Use

1.1.1 Introduction
The Artisan CNC system supports the use of „variables‟ in CNC programming. Through the use of
variables, great flexibility and power can be built into relatively simple G-Code sequences. Artisan CNC
supports variables in two distinct styles. The first is the style used by the FANUC 6M controller. These
variables are known as „Numeric Variables‟ and are described in an upcoming section. This feature is
provided to support customers‟ pre-existing FANUC 6M programs. The other major style of variables
supported by Artisan CNC are called „Alphanumeric‟ (or simply „Alpha‟) variables. This style of
variables is unique to the Artisan CNC software. Alpha variables allow considerable freedom in
programming and enable G-Code programs of great power to be easily developed.

1.1.2 The Use of Variables in Programming


When a G-Code program is written to use variables properly, the program takes on an „intelligence‟
not previously possible. Variables can supply a program with information from the outside world that the
program can use to modify (or completely change) its behavior. For example, a routine to cut a simple
circular pocket can be instructed to cut a pocket of any size. The depth of cut in a program may be altered
to suit. A rectangular pocket‟s location, length, width, orientation, fillet radii, etc., may all be changed
according to the needs of a job. The possibilities are limited only by the ingenuity of the programmer.
Variables can modify the execution of a machining program in two different ways. First, the basic
logic of a program may remain the same, while details of the machined features may be changed by
information stored in variables. For instance, we may wish to machine a bolt circle on a flange. This bolt
circle may contain 12 holes, equally spaced, on a radius of 3.000 inches. However, 2 different
configurations of this flange may be required, each with a different size bolt hole. In this case, we could
supply the bolt hole diameter in a variable, and write the G-Code program so as to mill each bolt hole to
the diameter stored in the variable. In this way, 1 G-Code program could mill a flange with any size bolt
holes as long as the number of holes and radius did not change.
Variables can also be used by the program to modify its basic logic in accordance with information
stored in the variables. Suppose we wish to machine bolt circles on 2 different flanges. Also suppose that
1 flange requires 12 holes on a radius of 3.000 inches, and the other requires 16 holes on a radius of 3.375
inches. In this case, the number of holes in the circle and the bolt circle radius could be stored in
variables. The program could then be written to mill any number of holes (of any spacing) on any radius
we desire.

1.1.3 FANUC 6M Numeric Variables


The FANUC 6M controller supports a type of variable known as a “Numeric” Variable. A Numeric
Variable‟s name takes the form, “#XXX”, where “X” indicates a digit 0-9. Please note that the variable
name must begin with the pound sign, “#”. The FANUC 6M supports numeric variables that come in two
different types.
The first Numeric Variable type is called a “local” variable. There are 33 legal local variable
names, #1 through #33. Local variables are intended to be used within subroutines and macros. As the
name implies, a local variable is “local” to a subroutine or macro. This simply means that a variable
called “#1” that is defined in Macro 1 will not be destroyed if we make a call to Macro 2. In fact, Macro
2 can create its own variable called “#1” and the data created in Macro 1 will not be destroyed. Any
macro or subroutine that we create can have its own “individual” copies of the variables #1 through #33,
and these will not get confused between different subroutines. This provides us with an excellent way to
store a large amount of data in several different subroutines. However, since local variables behave this
way, they cannot be used to pass information back and forth between mainline programs and subroutines.
The second type of Numeric Variable is the “common” variable. There are 160 legal common
variable names. These are #100 through #149, and #500 through #509. Unlike local variables, common
variables do not change as we go back and forth between mainline programs, macros, and subroutines. In
other words, #100 will have the same value no matter where we reference it in our program.

1.1.4 Ah-ha! Alphanumeric Variables


The second style of variables that is supported by Artisan CNC are called „Alphanumeric‟ variables,
or simply „Alpha‟ variables. The capability to use Alpha variables was added to the Artisan CNC
program to provide a more flexible and powerful programming environment than would be possible with
Numeric variables alone.
Names for Alpha variables are different than those for Numeric variables. An Alpha variable name
takes the form: “%XXXXXXXX” where “X” indicates any alphabetic letter or digit. Please note that
Alpha variable names must begin with a percent sign ( “ % “). Also, only the first 8 characters after the
“%” are considered significant. In other words, any letters in the variable name after the 8th one will be
ignored. It is currently recommended that, if there are any numeric digits in the variable name, they be
preceded by alphabetic characters. For example, “%VAR1” would be considered correct, whereas
“%1VAR” would not. The use of Alpha Variables is illustrated in several upcoming examples.

1.2 Macros and Subroutines

1.2.1 Introduction
Macros and Subroutines greatly extend the capabilities of the standard G-Code programming
language. In their most basic form, a macro provides the ability to execute a block of G-Code repeatedly,
while only having to write it once. In addition, information known as “parameters” can be communicated
(or “passed”) to a macro or subroutine. These parameters can be acted on by the subroutine in any
manner we wish. This enables us to control the behavior of a subroutine from one execution to the next.
Artisan CNC provides for declaring and executing macros and subroutines in a manner that
provides compatibility with the FANUC 6M controller. In addition, the naming conventions for
subroutines have been extended in a manner similar to the extensions for variable names. The use of
macros with Alpha names (known as „symbolic macros‟) and Alpha variables is known in this guide as
“Ah-ha! style programming”. As we will see, these extensions significantly increase both the ease with
which programs can be written, and the power available to the user.
Please note that, with the FANUC 6M controller, a „macro‟ and a „subroutine‟ have different
definitions. A „macro‟ is defined as a piece of code that is invoked using one technique, and a
„subroutine‟ is invoked using another. Both techniques are supported by Artisan CNC. Since there are no
functional differences between „macros‟ and „subroutines‟ under the Artisan system, the terms „macro‟
and „subroutine‟ are used interchangeably in this guide.
1.2.2 FANUC 6M Compatible Programming
Introduction
Artisan CNC supports a macro capability that is designed to provide compatibility with the FANUC
6M controller. This includes the naming convention for macros, the syntax used to invoke (or „call‟)
those macros from the mainline program, and the syntax used to return control to the main program. At
the time of production of this guide, the major difference between Artisan CNC and the FANUC 6M lies
in the method of entering programs into the control. With the FANUC 6M controller, programs may be
defined in any order desired. On the Artisan CNC system, the mainline program must be defined first,
followed by the macros. Macros may be defined in any order. This limitation stems from the fact that on
a PC-based CNC control, G-Code programs are essentially 1 large text file, with execution starting at the
beginning of the file. For FANUC 6M programs that are pre-existing on magnetic media such as floppy
diskette, a text editor program can be used to move the mainline program to the front of the file such that
it precedes all of the macros. This will enable these programs to execute normally.

Defining Mainline programs, Macros, and Subroutines (Fanuc)


When writing FANUC compatible programs, the mainline program and macros/subroutines are
defined in exactly the same way. A program is given an identifier (which is typically 4 digits) and this is
preceded by an uppercase “O” (not a numeric zero) or a colon,” : “. The only differences that set a
mainline program apart from a subroutine in the Artisan CNC system are as follows:

The mainline program must be the first routine defined in the G-Code file.
Macros or subroutines must end with an M99 code, whereas mainline programs typically end with
either an M02, M30, or M31.
A short G-Code file with a mainline program and one subroutine might look like this:
Example 1
:0001
N20 T4M06
N22 G80 G90 G40
N24 G01 X-3.0 Y-0.33
N26 M98 P2 L10
N28 M30

:0002
N5 G91
N6 G01 X0.035
N7 G01 Y-0.12
N8 M99
Note the definition of the mainline program as program number 1 (:0001) and the subroutine as
program number 2 (:0002). The steps required to actually call the subroutine and exit from it are
described in the following sections.

Invoking a Macro or Subroutine from a Mainline G-Code Program (Fanuc)


Invoking (or „calling‟) a macro or subroutine can be done by two different methods. The first of
these is M98, and was illustrated in Example 1 above. The syntax is as follows: M98 PXXXX
(LXX)
XXXX indicates the identifier number of the macro or subroutine. Note that in the case of calling
the macro 0002 in Example 1, the leading zeros may be specified in the M98 call, although they are not
required. In other words, “M98 P0002” will work the same as “M98 P2”. The „L‟ parameter is optional,
as indicated by the parentheses above. This parameter is discussed in an upcoming section.
The other method used for calling a FANUC-style numeric macro is G65. The usage is identical to
the M98 call, i.e., “G65 P2” or “G65 P0002”. The G65 call method also has the capability to pass
parameters to the macro that it is calling. As we discussed earlier, passing parameters to a macro enables
us to „tell a macro what to do‟ by passing data to it. The macro could then be written to behave in
different ways depending on the data that we pass to it.

Passing Parameters to a FANUC-style macro


Up to 21 separate parameters can be passed to a FANUC-style macro. Suppose that we wish to drill
a hole to a depth of 0.3 inches. Also, suppose that the location of the hole is to be at X=1.500”,
Y=0.750”. We could pass the X-coordinate, Y-coordinate, and the depth to a macro (let‟s call it macro
0005) that does the drilling. The syntax for doing this is as follows:
G65 P0005 A1.500 B0.750 C0.3
Notice that we assign a single-letter name to each parameter. We can assign up to 21 of these.
These 21 letters are the A to Z alphabet minus the letters G, L, N, O, and P.
The numbers that go along with the letters (A, B, and C in this case) will show up in the macro as
local numeric variables (see page 4 for a discussion of this). What local variables will contain our data?
This will depend on which letters we assign our data to. The correspondence between the alphabetic
letters in the G65 call and local numeric variables in the macro is as follows:

A #1 ..................................................................................................... Q #17
B #2 ....................................................................................... ............. R #18
C #3 ....................................................................................... .............. S #19
D #7 ....................................................................................... ............. T #20
E #8 ....................................................................................... ............. U #21
F #9 ....................................................................................... ............. V #22
H #11 ..................................................................................... ............ W #23
I #4 ....................................................................................... ............. X #24
J #5 ....................................................................................... ............. Y #25
K #6 ....................................................................................... ............. Z #26
M #13

From this mapping information, our parameters in the previous example would show up in the
macro in the following way:

The value 1.500, intended to be the X-coordinate of our hole, would be in #1.
The value 0.750, intended to be the Y-coordinate of our hole, would be in #2.
The value 0.3, the depth of our hole, would be in #3.

From here, we could move to our hole‟s X-Y coordinates by issuing something like this:
G00 X #1 Y #2

We should note that the order in which the letter/number pairs appear is not important. We could
have called the macro like this:
G65 P0005 B0.750 C0.3 A1.500

and the result would be exactly the same. If we called the macro like this:
G65 P0005 B1.500 C0.750 A0.3

the 1.500 would now show up in the macro in variable #2. The value 0.750 would now be in
variable #3, and the value 0.3 would now be in variable #1.

Use of the “L” parameter for „looping‟ on a subroutine (Fanuc)


Often, we would like to be able to execute a given subroutine several times in succession. The
FANUC 6M and Artisan CNC systems support this using a parameter called “L”. If we refer to Example
1, the mainline program (program 0001) issues an M98 call to a macro (macro 0002 in this case). We
wish to execute macro 0002 ten times in succession. This is done by assigning an “L” to the M98 call,
and specifying 10 executions, as shown in Example 1.

Returning from a Macro or Subroutine (Fanuc)


When a macro or subroutine completes execution, we would like to return control to the mainline
program. This is done by issuing a “Function Return” code, M99. If we refer to Example 1, we can see
that macro 0002 terminates with an M99 code.
Important: The M99 code MUST be the last command in a macro or subroutine.
The M99 code is used to return from a macro regardless of whether a G65 or an M98 was used to
call the macro in the first place.

1.2.3 Ah-ha! Style Programming


Introduction
The Artisan CNC software contains extensions to the basic FANUC-compatible provisions for both
variables and macros/subroutines. These extensions are referred to in this guide as “Ah-ha! Style
Programming”. The Ah-ha! programming style permits significantly greater flexibility in programming,
and places fewer restrictions on the programmer. In addition, programs written in the Ah-ha! style are
considerably easier to read, understand, and maintain.

Defining Mainline Programs, Macros, and Subroutines (Ah-ha!)


A mainline G-Code program written for the Artisan CNC package does not require “definition” per
se. As stated earlier, Artisan implements a G-Code program as simply a text file that adheres to certain
syntactical requirements. The first group of legal G-Code commands in a file (up to a Program Stop or
Cycle Start command) are considered to constitute the mainline program. The program may be given a
number similar to that used in the FANUC 6M programming style, but this is not necessary.
Macros and subroutines are defined in a manner which is similar to that of FANUC 6M style
programming. However, the rules for names of the macros are different. A macro or subroutine is named
according to the same rules as those for Alpha variables. A macro name takes the form,
“%XXXXXXXX”. The name must begin with a percent sign (“ % “). Following the percent sign can be
up to 8 alphabetic or numeric characters. Also, as with variable names, alphabetic characters in the name
should precede numeric digits. The macro is defined with an “O” in front of it. The macro definition
takes the form:
O %XXXXXXXX
Macros or subroutines which are called from a mainline program may also have parameters passed
to them. As described earlier, passing the proper data to a subroutine can turn a fairly simple piece of G-
Code into a very powerful tool. In addition, the parameters passed to a subroutine are not limited to
“hard-coded” numbers as described previously. Parameters passed to a subroutine may themselves be
variables which have been calculated in the mainline program.

Passing Parameters to an Ah-ha! Style Subroutine


If a macro or subroutine requires parameters, those parameters are supplied I the form of a list
immediately following the macro name. The list of parameters is enclosed in parentheses. Declaration of
a macro with parameters looks like this:
%XXXXXXXX ( %P1, %P2, %P3, %P4……)
In the above example, the parameters are listed as %P1 through %P4, but these parameters could
have any name that adheres to the rules for Ah-ha! style variable names (i.e., begins with a percent sign (“
% “) and is 8 characters or less in length).
Suppose we wish to drill an array of holes. Let us also suppose that we know how to calculate the
X and Y coordinates for each hole, as well as calculate the hole depth. We wish to pass these data to a
macro which performs the actual hole drilling operation. Such a sequence of G-Code might look like the
following:
Example 2
%X1 = 1.250
%X2 = %X1 + 0.500
%X3 = %X2 + 0.500
%Y1 = 1.000
%Y2 = 1.500
%Y3 = 2.000
%DEPTH=0.750
G65 %DRILL(%X1 , %Y1 , %DEPTH)
G65 %DRILL(%X2 , %Y2 , %DEPTH)
G65 %DRILL(%X3 , %Y2 , %DEPTH)
more G-Code statements
more G-Code statements
M30
O %DRILL( %PARM1 , %PARM2 , %PARM3)
G00 Z 1.000
G00 X %PARM1 Y %PARM2
G81 Z %PARM3 R 0.1
G80
M99
We should take note of several things shown in this example:
The mainline program has no name. As stated earlier, this is not necessary. Mainline programs do
not require names.
Notice that we have set up X and Y variables for 3 different holes. We named these variables
“%X1”, “%X2”, and so on. However, we could have named them anything provided that the name
adheres to the Ah-ha! style requirements for variable names.
Note that the values of several of the variables have been calculated based on the values of other
variables. Artisan CNC possesses a strong arithmetic computing capability. A complete discussion of
this capability is beyond the scope of this guide. However, this capability is documented in the Artisan
CNC Software Manual from Ah-ha! Design Group.
Note that the subroutine called “ %DRILL “ does not use the same variable names as we did in our
mainline program. Notice that in the declaration of “ %DRILL”, 3 parameters were set up in the
parameter list. The values that we pass to “ %DRILL “ from the mainline program are „plugged in‟ to
these variables in the “ %DRILL “ subroutine. This is a very important point!! Not having to match
variable names enables us to write a subroutine only once, and use it over and over again from many
different mainline programs. The subroutine can become part of our “programming toolbox”.

Invoking a Macro or Subroutine from a Mainline G-Code Program (Ah-ha!)


When programming in the Ah-ha! style, there are 2 methods by which a macro or subroutine may
be called. The first of these is G65. Example 2 illustrates the use of G65. A parameter list must be
included if the macro being called requires parameters.
A macro or subroutine may also be called with the M98 function. Both the G65 and M98 functions
are supported to maintain compatibility with the FANUC 6M controller. At this time, G65 and M98 are
identical to one another in their function under the Artisan CNC system.
Please note that regardless of which function is used to initiate the macro call, the macro name (both
in the call and the macro definition) must adhere to the previously stated rules for macro names for Ah-
ha! style programming.

Returning from a Macro or Subroutine (Ah-ha!)


When a macro or subroutine completes execution, control must return to the mainline program. The
function code used to do this is M99. Note in Example 2 that the macro “ %DRILL “ terminates with an
M99 code. M99 is used to return to the mainline program regardless of whether a G65 or M98 was used
to initiate the macro call.
Important: The M99 code MUST be the last command in a macro or subroutine.

1.3 Variables, Macros, and Subroutines - Miscellaneous Information and Things to


Remember
Various important points and “tidbits” of information which do not fit into the previous sections, but
which require documentation, are listed in this section.
The FANUC 6M style of Numeric Variables and the Ah-ha! style of Alpha Variables are both ways
of storing data in the CNC control‟s memory, and then manipulating this data in whatever manner suits
us. Even though the two styles have different rules for naming the variables, it is important to understand
the following ideas:
The data that we can store in Numeric and Alpha Variables is exactly the same.
The way in which we can manipulate Numeric and Alpha variables is exactly the same.
ONLY THE RULES FOR GIVING THEM THEIR NAMES ARE DIFFERENT.
If maximum compatibility with existing FANUC 6M macros is of concern, we recommend
that the following section be read.
************************************************************************
As shipped from the factory, the Artisan CNC package requires all numeric constants in G-Code
programs to have a decimal point appended to them to guarantee that the numeric data is interpreted. For
example, the statement, “ #101 = 2.0 “ will work correctly, whereas “#101 = 2” will not. This need for
the decimal point comes from the fact that Artisan CNC interprets numeric data in two different ways,
depending on whether there is a decimal point included or not. If no decimal point is present, an
interpretation called “m.n” (pronounced “m dot n”) is used. In this form, the location of the decimal point
is implied. The parameters “m” and “n” are used to indicate how many digits are assumed to be to the left
of the decimal point, and to the right of the decimal point, respectively. Artisan‟s default settings for “m”
and “n” are 3 and 4, in that order. A string of digits is assumed to look like: mmm.nnnn When digits are
read, the mmm.nnnn field is filled in starting from the right. For example, if we simply specify a number
as “2”, without a decimal point, the mmm.nnnn field is filled in from the right, which places the “2” in the
tenths of thousandths place, or mmm.nnn2. So, just a “2” without a decimal point is interpreted as 0.0002
if m and n are left at their default settings.
If we wish to program without decimal points, (i.e., if we wish “ #101 = 2 to work correctly, as in
the above example), we may do so by making a simple change within Artisan CNC. If we tell Artisan to
make the value of “n” zero, then Artisan will assume that there are no digits to the right of the decimal
point. The values of m and n can be changed in the following way:
 Press ALT^S from the Artisan main display to go to the “Setup” menu.
 Press Option 6, “Tool path file setup” menu.
 Press Option 3, “Digits to the right of the decimal point”. The default number „4‟ will appear in a
window. Enter a number of zero (“0”, ENTER). Press the Escape key to get back to the main
display.
When the above steps are performed, statements such as “%VAR1 = %VAR / 2” and “ #101 = 2
“ will interpret the 2 as 2.0, not as 0.0002.
A 2nd change we can make to increase FANUC compatibility has to do with the character that is
recognized by Artisan as a “comment” character. When Artisan encounters a comment character,
everything on the line to the right of the comment character is ignored. Artisan‟s default comment
character is a semi-colon „;‟. Many FANUC 6M macros use parentheses to designate a comment. If you
wish to run macros using comments in parentheses, the comment character must be changed from a semi-
colon to a left parenthesis „(„. To do this, the following line should be placed at the beginning of the G-
Code file that calls the macro:
(*CMST „(„*)
Type everything on the above line, including both of the asterisks and all of the parentheses. This
will cause Artisan to interpret the „(„ character as designating the start of a comment. Note that when
using this feature, brackets „[„ and „]‟ must be used in calculation expressions, since the use of the left
parenthesis will cause the rest of the line to be ignored.

Example 3
Use of Numeric Variables in a FANUC-style macro
O0001
#100=1
#101=3.0
#102=1.5
#103=0.5
#104=400
#105=3.5
#106=3.
#107=2.0
G90 G54 S #104 M03
G00 X #101 Y #102
G43 H #106
G01 Z0.1
G01 Z -#103 F [ #105 / 2]
Y [#102 + #107 / 2 - #100 / 2]
G02 J -[ #107 / 2 - #100 / 2]
G01 Y #102
G00 Z0.1
M30

Example 4
A macro written in the Ah-ha! Programming Style
O %MAC1
%V100 = 1.0
%V101 = 3.0
%V102 = 1.5
%V103 = 0.5
%V104 = 400.0
%V105 = 3.5
%V106 = 3.0
%V107 = 2.0
G90 G54 S %V104 M03
G00 X %V101 Y %V102
G43 H %V106
G01 Z0.1
%FEED = %V105 / 2.0
G01 Z -%V103 F %FEED
%YCOORD = %V102 + %V107 / 2.0 - %V100 / 2.0
G01 Y %YCOORD
%JVECT = - (%V107 / 2.0 - %V100 / 2.0)
G02 J %JVECT
G01 Y %V102
G00 Z 0.1
M30

You might also like