Hello Guest it is March 28, 2024, 07:20:10 PM

Author Topic: Newbie question on G-code looping (While, for, etc.)  (Read 33434 times)

0 Members and 1 Guest are viewing this topic.

Newbie question on G-code looping (While, for, etc.)
« on: March 20, 2010, 04:49:05 PM »
This is driving me nuts. I am trying to write a simple G-code program for Mach 3 that needs a simple loop. I have been searching for some example code of how to do a "WHILE" or "FOR" loop and I find examples but when I go to use them in Mach 3 it keeps giving me a error on the first line. Here is a example I tried and did not get to work.
 
N20 $WHILE P1<= 10
N30 P1=P1+1
N40...
N50...
N60 $ENDWHILE

Here is another that I tried and did not get to work
WHILE[P1LE10] DO1
P1=[P1+1]
;ENTER CODE TO LOOP HERE
END1

I am at a total loss as to what the proper syntax and structure is for Mach 3. Can anyone help with this simple loop?
Another question, is there a PDF or tutorial some where that documents all the loops and how to properly code them? I am also looking for a good book. I have seen the reccomendations on CNC by peter smid. It sounds good but does it cover the mach 3 flavor of G-code? I talked to several people where I worked about my problem and they said it all depends on what controlled you are using. They work in 2-3 different ones and they siad that they are similar but very different also.

Confused

Scott

Offline Graham Waterworth

*
  • *
  •  2,668 2,668
  • Yorkshire Dales, England
    • View Profile
Re: Newbie question on G-code looping (While, for, etc.)
« Reply #1 on: March 21, 2010, 06:34:36 PM »
The only way to do loops at the moment is by using subs or VB macros

You can use # vars and operators but no IF's or WHILE, GOTO's or conditions LE, etc.

Search the forum for M98 and you will find lots of examples to do loops and conditions.

Graham
Without engineers the world stops
Re: Newbie question on G-code looping (While, for, etc.)
« Reply #2 on: March 24, 2010, 08:46:36 PM »
Thank you for clearing that up for me. I had found some examples that had loops but they must have been VB or something. I am going to give M98 a try on my machine tonight. I bet I can get it to work now.

Thanks
Scott

Offline Deano

*
  •  15 15
    • View Profile
Re: Newbie question on G-code looping (While, for, etc.)
« Reply #3 on: July 09, 2011, 01:13:19 PM »

I am at a total loss as to what the proper syntax and structure is for Mach 3. Can anyone help with this simple loop?
Another question, is there a PDF or tutorial some where that documents all the loops and how to properly code them? I am also looking for a good book. I have seen the reccomendations on CNC by peter smid. It sounds good but does it cover the mach 3 flavor of G-code? I talked to several people where I worked about my problem and they said it all depends on what controlled you are using. They work in 2-3 different ones and they siad that they are similar but very different also.

Confused

Scott
Graham, your reply to Riddler was helpful, but his question about Mach3 g code version vs Smid slipped by. I'm searching for the same. (I posted earlier today) Your reply to Fastest1 (Subroutine confusion, Oct 02, 2010) was great, but maybe a compilation of all the great basic explanations would save hours of noobie search time. :)

Offline Graham Waterworth

*
  • *
  •  2,668 2,668
  • Yorkshire Dales, England
    • View Profile
Re: Newbie question on G-code looping (While, for, etc.)
« Reply #4 on: July 11, 2011, 07:14:26 PM »
Hi Deano,

what is it you want to know, give me a clue as to what you want to do and I will try and explain it in simple terms, to write a whole document would take way too long but snippets of code I can do.

Graham
Without engineers the world stops

Offline Graham Waterworth

*
  • *
  •  2,668 2,668
  • Yorkshire Dales, England
    • View Profile
Re: Newbie question on G-code looping (While, for, etc.)
« Reply #5 on: July 11, 2011, 07:35:07 PM »
To do a simple loop you can do this :-

%
O0001 (MAIN PROGRAM)

G21 G40 G80 (STANDARD SETUP LINES, METRIC, CLEAR OFFSETS, CANCEL CANNED CYCLES)

N1 (TOOL 1)
G54 (SELECT FIXTURE OFFSET 1)
T1 M6 (SELECT TOOL 1)
G00 G90 G43 X0 Y0 Z25. H1 S2000 M3 (RAPID 3 AXIS MOVE TO START POINT IN ABSOLUTE AND START SPINDLE)
Z1. (RAPID 1MM ABOVE JOB)
G01  Z0  F150. (FEED TO TOP OF JOB)
X25. F200. (FEED OUT TO 25MM)
M98 P0002 L20 (CALL SUB PROGRAM O0002 20 TIMES)
G00 G90 X0 Y0 (RAPID TO CENTRE OF CIRCLE IN ABSOLUTE MODE)
Z0 (RAPID CLEAR OF JOB)
M30

O0002 (SUB PROGRAM)
(THIS SUB IS CALLED 20 TIMES FROM MAIN PROGRAM)
(BECAUSE IT IS IN INC MODE THE TOOL WILL MOVE Z-1MM EACH PASS AND CREATE)
(A HOLE 50MM DIA + TOOL DIA TO A DEPTH OF 20MM)
G91 (CHANGE TO INCREMENTAL)
G01 Z-1. F50. (FEED DOWN 1MM)
G03 I-25. F150. (MOVE IN A CCW CIRCLE)
M99
%

Once all the remarks are removed the code is very compact and easily changed to alter the bore size.

One thing to remember is that the L command has a range of 0 to 99.

Graham
« Last Edit: July 11, 2011, 07:44:04 PM by Graham Waterworth »
Without engineers the world stops
Re: Newbie question on G-code looping (While, for, etc.)
« Reply #6 on: October 01, 2012, 02:40:30 AM »
IF YOU WANT TO DO A MULTI FIXTURE PROGRAM YOU COULD TRY THIS


O0001
#101=1(tool number)
#102=-.02(TAPPER BLOCK)
G90
G10  L2 P1 X-10. Y-10.  Z-10.(G54 WORK OFFSET)
G10  L2 P2 X-10. Y-11.  Z-10.(G55 WORK OFFSET)
G10  L2 P3 X-10. Y-12.  Z-10.(G56 WORK OFFSET)
T#101 M6
M0
#1=54(STARTING OFFSET)
G0 G90 G#1 X0.5 Y0. S1200 M3
G43 H#101 D#101 Z1.
G0 Z0.1
M98 P0002 L3 (HOW MANY TIMES IT TAKES YOU TO GET TO FINAL OFFSET)
G0 Z10.
M30

O0002
G0 G90 G#1 X0.5 Y0. S1200 M3
G0 Z0.1
g1 z-.1
G1 X5. Z[-.1+#102] F20.(TAPPER BLOCK)
G0 Z.1
#1=[#1+1](CURENT OFFSET PLUS 1)
M99
%

Offline Deano

*
  •  15 15
    • View Profile
Re: Newbie question on G-code looping (While, for, etc.)
« Reply #7 on: February 11, 2013, 08:10:22 AM »
Thanks, Graham and Dslicker, for your response. Sorry this is so long in coming, lots of distractions and a  death in the family changed the direction of my efforts for a time.  I have a new problem, but I think it needs to be posted in a different thread.  Thanks again.