Hello Guest it is May 14, 2021, 03:43:07 AM

### Author Topic: How to Pass Values to Subroutines in Mach4  (Read 1657 times)

0 Members and 1 Guest are viewing this topic.

#### Osker

• 41
##### How to Pass Values to Subroutines in Mach4
« on: May 15, 2018, 06:11:25 PM »
The following is a subroutine I used frequently in Mach3 to mill a shape to a final Z depth.  The basic approach is to define a series of variable.  Use those variables to calculate the number of passes required to reach the final Z depth and then pass that value to the subroutine.  This second subroutine ensures that the final depth is reached and executes spring passes.

Does Mach4 support a similar approach?  If so, please give me a hint of where I can find the information within the MAch4 documentation.

Regards,

Dan

N10 % Mill Shape to depth using two subroutines
N20 % Calculates number of passes to reach final depth
N30 % Second subroutine is at final depth & supports
N40 % spring passes at final depth
N50 %
N60 % Standard Variables
N70 %
N80 % #1 = Z Retract
N90 % #2 = Z Start
N100 % #3 = Z Finish
N110 % #4 = Z depth of cut per pass
N120 % #5 = Number of times for subroutine to loop
N130 % #6 = Number of spring passes
N140 %
N150 % Number of passes to reach zero = absolute values of Z Start + Z finish
N160 % divided by depth per pass. Result is a whole number
N170 %
N180 #1 = 0.25    % Z Retract
N190 #2 = 0.03    % Z Start
N200 #3 = -.125   % Z Finish
N210 #4 = 0.05    % Z depth of cut per pass
N220 #5 = FIX[[ABS[#2] + ABS[#3]]/#4] % Number of times to loop
N230 #6 = 2  % Number of Spring Passes
N240 %
N250 M98 P001 Q#5 % Call subroutine o001
N260 %
N270 % Mill remaining depth
N280 %
N290 M98 P002 Q#6 % Call Subroutine o002
N300 %
N310 % End
N320 %
N330 G00 Z#1 % Goto Z Retract
N340 M30
N350 %
o001 % (Mill Shape subroutine )
N370 %
N380 G00 Z#1 % Goto Z Retract
N390 G00 X0.2  Y0.45 % Goto X and Y start REPLACE with actual values
N400 #2 = [#2-#4] % decrement Z depth
N410 G00 Z[#2 + [2*#4]] % Clear work by Z Start + 2 times depth of cut
N420 G01 Z#2 F.25 % Down slow to cutting depth
N430 %
N440 % REPLACE with actual values
N450 G01 X-0.25 Y0.45 F4
N460 G01 Y-0.2
N470 %
N480 M99 % Return from Subroutine
N490 %
N500 %
o002 % (Mill Final Depth of Cut Subroutine)
N520 %
N530 G00 Z#1 % Goto Z Retract
N540 G00 X0.2 Y0.45 % Goto X and Y start REPLACE with actual values
N550 G00 Z[#3 + [2*#4]] % Goto Z Finish + 2 times depth of cut
N560 G01 Z#3 F.25 % Down slow to Z Finish
N570 %
N580 % REPLACE with actual values
N590 G01 X-0.25 Y0.45 F4  % Goto X and Y start
N600 G01 Y-0.2
N610 %
N620 M99 % Return from Subroutine

#### joeaverage

• 6,419
##### Re: How to Pass Values to Subroutines in Mach4
« Reply #1 on: May 16, 2018, 01:31:13 AM »
Hi,
if it works in Mach3 it should work in Mach4.

When all said and done Mach of either flavor is a Gcode interpreter and both should process good Gcode. My experience is that Mach4 is a little stricter
in its adherence to the 'rules' established by Fanuc.

Have you tried this code in Mach4? Not sure but the % symbols may not be Mach4 compatible. The logic should be OK however I think that your choice of pound variables
may clash with previously assigned Mach4 values. I would recommend #500 thru #549.

Have a look at the pound variable assignments which have been identified:
http://www.machsupport.com/forum/index.php/topic,27396.msg193456.html#msg193456

Craig
My wife left with my best friend...
and I miss him!

#### Osker

• 41
##### Re: How to Pass Values to Subroutines in Mach4
« Reply #2 on: May 16, 2018, 11:58:18 AM »
Craig,

Thanks for the assistance.  For clarity, I should have said that the code did not work in MACH4.  After following your suggestions the code is now working.  Here is what I did:

1) Removed all % signs and comments following the % sign

2) Replaced my # variables with variables in the #500 range.   Thanks for the link to the #Var List.  Globasl #Variables also worked.  Any reason to not use global #Variables?

3) Replaced the Q argument with an L argument.  In Mach3, the Q argument specified the number of repetitions.  Mach4 uses the L argument.

Regarding removing all % signs and the comments that followed.  In MACH3, information following the % sign on a line was ignored.  I used the % sign to comment code and to temporarily replace variable values.  Commenting code is critical to good coding.  How does one comment code in MACH4?

Your statement that  in your experience Mach4 is strickter in its adherence to "Rules" established by Faunc prompts me to ask, is there a programing guide that you would suggest?  The Mach3 manual covered the basics of writing G-code,  Enough for a hobbist to survive, but the MACH4 manual does not include such helpful information.

Thanks again.

Dan

#### Cbyrdtopper

• 751
##### Re: How to Pass Values to Subroutines in Mach4
« Reply #3 on: May 16, 2018, 12:25:08 PM »
You can comment with a semicolon  ";"  or put it in Parentheses ( )
The G Code programming manual is a good manual when pertaining to G Code.
For writing Subroutine Macros, Steve pointed me to this book.   Fanuc CNC Custom Macros

https://www.amazon.com/Fanuc-Custom-Macros-Peter-Smid/dp/0831131578/ref=sr_1_1?ie=UTF8&qid=1526487924&sr=8-1&keywords=fanuc+cnc+custom+macros&dpID=51K7GW1TH3L&preST=_SY291_BO1,204,203,200_QL40_&dpSrc=srch

It was very helpful when writing code for our OD Grinders.

#### Osker

• 41
##### Re: How to Pass Values to Subroutines in Mach4
« Reply #4 on: May 16, 2018, 02:11:12 PM »

Thank you for the assistance.  I used my original code to produce two versions.  Both contained the necessary revisions described above.  One version replaced the % signs with a ";" character.  The other version replaced the % signs with enclosing parenthesis "()".  The code containing () worked as expected.  The code containing ; characters produced error messages in a couple of lines.  Perhaps there are hidden characters in the original code  In any event, the Mach4 G-code editor flagged lines which lacked the closing ).  While I could likely find what is causing errors when using the ; symbol, I am going to be lazy and use enclosing parenthesis :>)

Thanks for the link to the Faunc Macros book.  I will check into if further.

Again, "Thanks"

Dan

#### joeaverage

• 6,419
##### Re: How to Pass Values to Subroutines in Mach4
« Reply #5 on: May 16, 2018, 02:35:03 PM »
Hi,
I think the pound variables in the range #500 to #549 are persistent global variables. That is to say that they are written to the .ini file and so are preserved
over different Mach sessions.

Good spotting with the L vs Q. I didn't spot it myself.

Quote
CNC Programming Handbook
by Peter Smid is pretty much the bible for CNC. It is almost too comprehensive to be a quick reference but it is good.

There is one way in which Mach4 Gcode is somewhat stricter than Mach3 Gcode:

Code: [Select]
`N00170 G04 P3.000000N00180 G82 X-31.9000 Y27.1800 Z-3.0000 F100   R1.0000  P0.500000N00190 G82 X-37.3000 Y13.6000 N00200 G82 X-37.3000 Y18.7000 N00210 G82 X-37.3000 Y21.3000 N00220 G82 X-16.8500 Y27.4000 N00230 G82 X-21.6000 Y22.7500 N00240 G82 X-23.7000 Y9.4500  N00250 G82 X-27.4500 Y34.4000 `This is  a fragment of code for drilling holes in circuit board. The code was produced by a PCB-to-GCode utility operating on PCB software EAGLE. It works in Mach3 as is
but not in Mach4. The reason is that the drill cycle G82 is modal, you mention it once and it stays active until you change it. Therefore having G82 on each line is redundant and
worse illegal because successive lines only have the new X and Y co-ords without all the other drill cycle parameters. The code runs in Mach4 if its edited:

Code: [Select]
`N00170 G04 P3.000000N00180 G82 X-31.9000 Y27.1800 Z-3.0000 F100   R1.0000  P0.500000N00190 X-37.3000 Y13.6000 N00200 X-37.3000 Y18.7000 N00210 X-37.3000 Y21.3000 N00220 X-16.8500 Y27.4000 N00230 X-21.6000 Y22.7500 N00240 X-23.7000 Y9.4500  N00250 X-27.4500 Y34.4000 `Each successive line introduces new X and Y cords and the existing drill cycle parameters are assumed constant.....as you can see the Mach4 interpretation is actually the correct one
its just that we became accustomed to dodgy code in Mach3.

Craig
My wife left with my best friend...
and I miss him!

#### Osker

• 41
##### Re: How to Pass Values to Subroutines in Mach4
« Reply #6 on: May 16, 2018, 04:31:33 PM »
Craig,

Thanks for the additional feedback and comments regarding certain modal G-codes.  I suspect that there will be more "Gotchas" before I have fully made the transition to Mach4.  I will check out "The CNC Programing Handbook".  Too much information at this point in time is better than not enough.

Dan

#### Osker

• 41
##### Re: How to Pass Values to Subroutines in Mach4
« Reply #7 on: May 16, 2018, 07:31:52 PM »
For those interested in the topic, attached is the final version of the "Mill to Depth" subroutine.  Thanks to Craig and Chad for their assistance.

Dan