Repeat GCode with Displacements

Moderators: TomKerekes, dynomotion

Post Reply
Juanjo-Lasing
Posts: 25
Joined: Wed Sep 04, 2019 11:56 am

Repeat GCode with Displacements

Post by Juanjo-Lasing » Tue Feb 04, 2020 11:52 am

Hello,
I am trying to make various repetitions of a structure defined in G-Code but displacing the origin of the structure a distance in each repetition. I’ll try to explain it with an example.

I have a square defined in GCode like this:

Code: Select all

G0 X0 Y0
G1 X10 Y0
G1 X10 Y10
G1 X0 Y10
G1 X0 Y0
And I want to make another 3 squares, with the same measures, but displacing the X coordinates in each one of them.
squ.png
example
squ.png (1.23 KiB) Viewed 2440 times
I check in other topic in this forum that it is possible to make loops in G-Code, but I am not sure if it is possible to write something like this (pseudocode):

Code: Select all

#1 = 20 (increment in X)
#2 = 0 (number of repetitions)
#3 = 4 (number of squares)
O1   (Label 1)
G0 X0+(#1*#2) Y0
G1 X10+(#1*#2) Y0
G1 X10+(#1*#2) Y10
G1 X0+(#1*#2) Y10
G1 X0+(#1*#2) Y0
(Increment #2+1)
(Decrement #3-1)
(Repeat the loop until #3 = 1)
Any help, please?

User avatar
TomKerekes
Posts: 2676
Joined: Mon Dec 04, 2017 1:49 am

Re: Repeat GCode with Displacements

Post by TomKerekes » Tue Feb 04, 2020 5:51 pm

Hi Juanjo-Lasing,

Here is the syntax:

Code: Select all

#1 = 20 (increment in X)
#2 = 0 (number of repetitions)
#3 = 4 (number of squares)

M98 P1 L[#3] (call subroutine #3 times)
M2 (stop)

O1   (Label 1)
G0 X[0+#1*#2] Y0
G1 X[10+#1*#2] Y0
G1 X[10+#1*#2] Y10
G1 X[0+#1*#2] Y10
G1 X[0+#1*#2] Y0
#2 = [#2 + 1] (Increment #2+1)
M99 (return)
Loop.png
Loop.png (8.51 KiB) Viewed 2430 times
Regards,

Tom Kerekes
Dynomotion, Inc.

Juanjo-Lasing
Posts: 25
Joined: Wed Sep 04, 2019 11:56 am

Re: Repeat GCode with Displacements

Post by Juanjo-Lasing » Wed Feb 05, 2020 10:42 am

Thank you Tom, that was perfect.

Another question, is it necessary to include the labels at the end of the file, after M2?
I tried to add new instructions after the label to add new movements but the simulator is unable to leave the loop.

Something like this:

Code: Select all

#1 = 20.2 (increment in X)
#2 = 0 (number of repetitions in X)
#3 = 4 (number of squares in X)

M98 P1 L[#3]

O1   (Label 1)
G0 X[0+#1*#2] Y0
G1 X[10+#1*#2] Y0
G1 X[10+#1*#2] Y10
G1 X[0+#1*#2] Y10
G1 X[0+#1*#2] Y0
#2 = [#2 + 1] (Increment #2+1)
M99 (return)

G0 X-30 Y-30 (new square)
G1 X-40 Y-30
G1 X-40 Y-40
G1 X-30 Y-40
G1 X-30 Y-30

M2 (stop)

User avatar
TomKerekes
Posts: 2676
Joined: Mon Dec 04, 2017 1:49 am

Re: Repeat GCode with Displacements

Post by TomKerekes » Wed Feb 05, 2020 6:38 pm

Hi Juanjo-Lasing,
is it necessary to include the labels at the end of the file, after M2?
I tried to add new instructions after the label to add new movements but the simulator is unable to leave the loop.
That is the normal method. As your code is written after calling the subroutine is finished execution will "fall" into the subroutine code again and erroneously return without any subroutine call.

A subroutine call without any return can be used as an effective "jump". The subroutine call stack is circular to allow this.

Code: Select all

#1 = 20.2 (increment in X)
#2 = 0 (number of repetitions in X)
#3 = 4 (number of squares in X)

M98 P1 L[#3] (call subroutine 1 #3 times)
M98 P2 (jump to label 2)

O1   (Label 1)
G0 X[0+#1*#2] Y0
G1 X[10+#1*#2] Y0
G1 X[10+#1*#2] Y10
G1 X[0+#1*#2] Y10
G1 X[0+#1*#2] Y0
#2 = [#2 + 1] (Increment #2+1)
M99 (return)

O2   (Label 2)
G0 X-30 Y-30 (new square)
G1 X-40 Y-30
G1 X-40 Y-40
G1 X-30 Y-40
G1 X-30 Y-30

M2 (stop)

Jump.png
Jump.png (8.46 KiB) Viewed 2419 times
HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

Juanjo-Lasing
Posts: 25
Joined: Wed Sep 04, 2019 11:56 am

Re: Repeat GCode with Displacements

Post by Juanjo-Lasing » Thu Feb 06, 2020 9:36 am

Understood.

Thanks again Tom.

Post Reply