Machsupport Forum

Mach Discussion => VB and the development of wizards => Topic started by: MachineMaster on February 25, 2009, 07:14:18 PM

Title: VB and variables in G code
Post by: MachineMaster on February 25, 2009, 07:14:18 PM
I have been looking at MACH3 and testing it on a BOSS 5 mill. So far it has been quite good. I have been running the mill with AHHA software for the last ten years but they are no longer around and I need something to replace it with for when it does fail.
My only problem is that I use variables and parametric code in my G code to do things that would either take many pages of G code or not be possible in G code.
An example would be

; ENGRAVE TIP
M01 L09
N10 G90G75G40 S1200
%THOUS = 1.  * ; THOUSANDS
%HUNDS = 5.  * ; HUNDREDS
%TENS = 2.  * ; TENS
%ONES = 5.  * ; ONES
%MTENS = 0.  * ; MONTH TENS
%MONES = 3.  * ; MONTH ONES
%YTENS = 0.  * ; YEAR TENS
%YONES = 5.  * ; YEAR ONES
T25 M06
%START:
M05
%ZFD=10.0
%FD=8.0
G54
G90 G49
G00 X0 Y0 Z0
M01 L01 ;PUT IN CORRECT L #
;
;
; INSERT ANY OTHER ROUTINE HERE
; NOTE Z AXIS PARAMETERS ARE ZERO = WORK SURFACE
; Z CLEAR IS +0.01. ANY FILE INSERTED HERE WILL RUN THEN
; MOVE TO A TOOL CHANGE POSITION, PAUSE TO MOVE THE PART, DRILL A
; HOLE, CHANGE TOOLS, DO THE ENGRAVING, FOUR NUMBERS WILL BE OUTPUT
; AND THE PROGRAM WILL RETURN TO START AND PAUSE.
;

This is the beginning of a program that engraves a serial number on a part. The program increments the serial number for each part.
Without variables there is no way to run the serial number part of the program.
Can this be done with VB?
Title: Re: VB and variables in G code
Post by: poppabear on February 25, 2009, 11:41:13 PM
yes

you can do it with Parameter programiing and/or call custom macros that you make....... Or even make a "Wizard" that will do it all and post the code for you.

scott
Title: Re: VB and variables in G code
Post by: MachineMaster on March 15, 2009, 07:10:12 PM
I have been studying the "Mach Script Language Reference" and have looked at the Script editing window but I am still at a loss as to how to put it all together for what I need.
The code snip that I posted above was easy to convert to Mach type code using #100=2. etc. but the part I didn't show like the following is what has me stumped. I have eight sections like the following

%MONE:
%NUM = 4.
G00X0.185Y-0.025
IF %MONES EQ 0. GOTO %ZEROZ
IF %MONES EQ 1. GOTO %ONEO
IF %MONES EQ 2. GOTO %TWOT
IF %MONES EQ 3. GOTO %THREET
IF %MONES EQ 4. GOTO %FOURF
IF %MONES EQ 5. GOTO %FIVEF
IF %MONES EQ 6. GOTO %SIXS
IF %MONES EQ 7. GOTO %SEVENS
IF %MONES EQ 8. GOTO %EIGHTE
IF %MONES EQ 9. GOTO %NINEN
M02

and if %MONES = 6 I need to jump to the following code

%SIXS:
G90 G00 Z0.01
G91 G00 X-0.0378 Y-0.07
G90 G01 Z-0.0030 F%ZFD
G91 G03 X0.0327 Y0.046 I-0.0137 J0.0445 F%FD
G03 X-0.038 Y0. I-0.019 J0.
G03 X0.038 Y0. I0.0191 J0.
G90 G00 Z0.01
IF %NUM EQ 2. GOTO %MONE
IF %NUM EQ 4. GOTO %YTEN
IF %NUM EQ 6. GOTO %YONE
IF %NUM EQ 8. GOTO %INCA
IF %NUM EQ 10. GOTO %NUMC
IF %NUM EQ 12. GOTO %NUMB
IF %NUM EQ 14. GOTO %NUMA
GOTO %START

which will engrave a 6 and then either jump to another section of code or return to START depending on the value of %NUM.

Using IF and GOTO in this manner allows the program to engrave sequential serial numbers for each part.

Where does VB come into the picture? Do I write VB code in the G code file or is it used some where else?
Title: Re: VB and variables in G code
Post by: ger21 on March 15, 2009, 07:39:03 PM
You need to put if...then and other VB statements in custom M code macros, and call those in your g-code. Kind of a hack, but the only way to do it in Mach.
Title: Re: VB and variables in G code
Post by: vmax549 on March 15, 2009, 10:24:03 PM
There are several ways it can be done. One would be to program all the code as a MACRO. The second would be to use gcode and sub programming and using some macros for condition and redirection.

(;-) TP
Title: Re: VB and variables in G code
Post by: MachineMaster on March 16, 2009, 02:53:31 AM
Is there a tutorial on macro programming? Is the VB Script Editor where you build a macro? I have been looking at the Wiki and all of the PDF files on the Mach site and am still lost.
Title: Re: VB and variables in G code
Post by: vmax549 on March 16, 2009, 09:57:11 AM
IN the CUTOMISING MACH2 manual it touches on the subject..   I have a macro that does about what you are doing I will have to round it up and post it for you to see the methodology.

(;-) TP

Title: Re: VB and variables in G code
Post by: MachineMaster on March 16, 2009, 01:02:01 PM
Thank you. I would really appreciate that. Often all I need is a good example.
Title: Re: VB and variables in G code
Post by: vmax549 on March 16, 2009, 02:44:47 PM
Here is the macro I run to do sequential S/N . I have a DRO set on the settings screen to show the current S/N number. From there you can reset it to whatever you need as a starting number.

THe macro will  engrave  S/N 000000000  as an example or any number up to 9 digits.



Sub main
Dim A As String
Dim Ch As String
Dim Cnt As Integer

P12 = 0.050 ' (SAFE Z HEIGHT)
P13 = -.005 ' (Z DEPTH)

P10=GetOEMDRO(1050)
A=Str(P10)
For Cnt=1 To Len(A)
  Ch=Mid(A,Cnt,1)
  Select Case Ch
    Case "0" ZERO
    Case "1" ONE
    Case "2" TWO
    Case "3" THREE
    Case "4" FOUR
    Case "5" FIVE
    Case "6" SIX
    Case "7" SEVEN
    Case "8" EIGHT
    Case "9" NINE
  End Select
Next
P10=P10+1
SetOEMDRO(1050,P10)
End Sub

Sub ZERO
message "Zero"
code "G91G0X-.0004Y.1076"
code "G90 G1 Z" & P13 & " F20."
code "G91 G1X.0176 "
code "G2X.0374Y-.0194I-.0021J-.05"
code "X.0164Y-.0417I-.0488J-.0432"
code "G1X-.0009Y-.1012"
code "G2X-.0157Y-.0372I-.0527J.0004"
code "X-.0372Y-.0157I-.0375J.0371"
code "G1X-.0353"
code "G2X-.0372Y.0157I.0004J.0528"
code "X-.0157Y.0372I.0371J.0376"
code "G1Y.1059"
code "G2X.0157Y.0389I.0585J-.001"
code "X.0372Y.0175I.0382J-.033"
code "G1X.0177 G90 Z" & p12
While IsMoving()
Wend
End Sub

Sub ONE
message "One"
code "G91G0X-.0359Y.0719"
code "G90 G1Z" & P13 & " F20."
code "G91 X.0357Y.0361"
code "Y-.2156"
code "X-.0357"
code "X.0718"
code "G90 G0Z" & p12
While IsMoving()
Wend
End Sub

Sub TWO
message "Two"
code "G91G0X-.071Y.0531 "
code "G90 G1Z" & P13 & " F20. "
code "G91 G2X.0157Y.0375I.0535J-.0004 "
code "X.0377Y.0159I.0379J-.0371"
code "G1X.0352 "
code "G2X.0375Y-.0159I-.0005J-.0531 "
code "X.0159Y-.0375I-.0373J-.038 "
code "X-.0164Y-.0383I-.0527J-.0001"
code "G1X-.1256Y-.1213"
code "X.142"
code "G90 G0Z" & p12
While IsMoving()
Wend   
End Sub

Sub THREE
message "Three"
code "G91G0X-.0605Y.0917"
code "G90 G1Z" & P13 & " F20."
code "G91 G2X.0362Y.0159I.0366J-.0341"
code "G1X.034"
code "G2X.0357Y-.0159I-.0006J-.0494"
code "X.0151Y-.0379I-.04J-.0379"
code "X-.0151Y-.0379I-.0552 "
code "X-.0357Y-.0159I-.0363J.0335"
code "X.0357Y-.0159I-.0006J-.0494"
code "X.0151Y-.0379I-.04J-.0379"
code "X-.0151Y-.0374I-.0546J.0002 "
code "X-.0357Y-.0159I-.0363J.0335"
code "G1X-.034 "
code "G2X-.0362Y.0154I.0002J.0505 "
code "G90 G0Z" & p12
While IsMoving()
Wend 
End Sub

Sub FOUR
message "Four"
code "G91G0X.0353Y-.1059"
code "G90 G1Z" & P13 & " F20."
code "G91 Y.2117"
code "X-.1059Y-.1411"
code "X.1412"
code "G90 G0Z" & p12
While IsMoving()
Wend   
End Sub

Sub FIVE
message "Five"
code "G91G0X.0714Y.1071"
code "G90 G1Z" & P13 & " F20."
code "G91 X-.1428 Y-.0714"
code "X.0895"
code "G2X.0374Y-.0159I-.0005J-.0531"
code "X.0159Y-.0374I-.0372J-.0379"
code "G1Y-.0358"
code "G2X-.0159Y-.0378I-.0537J.0003"
code "X-.0374Y-.0159I-.0379J.0372"
code "G1X-.0357"
code "G2X-.0379Y.0159I.0002J.0536"
code "X-.0159Y.0374I.0372J.0379"
code "G90 G0Z" & p12
While IsMoving()
Wend   
End Sub

Sub SIX
message "Six"
code "G91G0X.0555Y.091"
code "G90 G1Z" & P13 & " F20."
code "G91 G3X-.0379Y.0155I-.0376J-.038"
code "G1X-.0352"
code "G3X-.0377Y-.0159I.0002J-.053"
code "X-.0157Y-.0375I.0378J-.0379"
code "G1Y-.1062"
code "G3X.0157Y-.0377I.0533J.0001"
code "X.0377Y-.0157I.0378J.0376"
code "G1X.0352"
code "X.0375Y.0157I-.0004J.0535"
code "X.0159Y.0377I-.0371J.0379"
code "G1Y.0352"
code "G3X-.0159Y.0375I-.0531J-.0005"
code "X-.0375Y.0159I-.038J-.0372"
code "G1X-.0352"
code "G3X-.0377Y-.0159I.0002J-.053"
code "X-.0157Y-.0375I.0378J-.0379"
code "G90 G0Z" & p12
While IsMoving()
Wend   
End Sub

Sub SEVEN
message "Seven"
code "G91G0X-.0714Y.1071"
code "G90 G1Z" & P13 & " F20."
code "G91 X.1428"
code "X-.1071Y-.2142"
code "G90 G0Z" & p12
While IsMoving()
Wend   
End Sub

Sub EIGHT
message "Eight"
code "G91G0X0.Y.1061"
code "G90 G1Z" & P13 & " F20."
code "G91 X.0176"
code "G2X.0373Y-.016I-.0004J-.0525"
code "X.0157Y-.0374I-.0378J-.0378"
code "X-.0157Y-.0372I-.0528J.0004"
code "X-.0373Y-.0157I-.0376J.0371"
code "G1X-.0352"
code "G3X-.0373Y-.0164I.0005J-.0519"
code "X-.0157Y-.0383I.0398J-.0386"
code "X.0159Y-.0361I.05J.0004"
code "X.0375Y-.0151I.0377J.0395"
code "G1X.0348"
code "G3X.0373Y.0151I-.0001J.0539"
code "X.0157Y.0361I-.0346J.0365"
code "X-.016Y.0383I-.0551J-.0004"
code "X-.037Y.0164I-.0378J-.0356"
code "G1X-.0352 G2X-.0373Y.0157I.0003J.0528"
code "X-.0157Y.0372I.0371J.0376"
code "X.0157Y.0374I.0535J-.0004"
code "X.0373Y.016I.0377J-.0365"
code "G1X.0176"
code "G90 G0Z" & p12
While IsMoving()
Wend   
End Sub

Sub NINE
message "Nine"
code "G91G0X-.0555Y-.091"
code "G90 G1Z" & P13 & " F20."
code "G91 G3X.0379Y-.0155I.0376J.038"
code "G1X.0352"
code "G3X.0375Y.0157I-.0004J.0535"
code "X.0159Y.0377I-.0371J.0379"
code "G1Y.1062"
code "G3X-.0159Y.0375I-.0532J-.0005"
code "X-.0375Y.0159I-.038J-.0372"
code "G1X-.0352"
code "G3X-.0377Y-.0159I.0002J-.053"
code "X-.0157Y-.0375I.0378J-.0379"
code "G1Y-.0352"
code "G3X.0157Y-.0377I.0533J.0001"
code "X.0377Y-.0157I.0378J.0376"
code "G1X.0352"
code "G3X.0375Y.0157I-.0004J.0535"
code "X.0159Y.0377I-.0371J.0379"
code "G90 G0Z" & p12
While IsMoving()
Wend
End Sub

Title: Re: VB and variables in G code
Post by: MachineMaster on March 16, 2009, 03:44:05 PM
Thank you. That should give me a good place to start.
My plan is to keep all of the G code in the main program and just do the logic in the macro.
That way I should be able to do away with the processor load of the

While IsMoving()
Wend

statement.
I see the one increment at the end of the first Sub main, but I am still missing how this works.
How do you call the macro? Where are you doing the position moves?
Title: Re: VB and variables in G code
Post by: vmax549 on March 16, 2009, 06:40:11 PM
OOPS that is not the complete code, Sorry. I'll post the complete code shortly.

IF you are going the Gcode direct route it gets more complicated as you have to do the sub program routine useing macros to do the conditional and redirection via the M98 Pxx   routine. I think I have an example of that as well.

(;-) TP
Title: Re: VB and variables in G code
Post by: MachineMaster on March 16, 2009, 09:12:17 PM
This is what I was thinking. I have these variables in the beginning of the program

N10 G90G40
#1000 = 2.       (thousands starting digit)
#100 = 2.         (hundreds starting digit)
#10 = 5.           (tens starting digit)
#1 = 9.            (ones starting digit)
#1210 = 0.       (month tens digit)
#1201 = 1.       (month ones digit)
#2010 = 0.       (year tens digit)
#2001 = 9.       (year ones digit)
T25 M06
 
This engraves  the following

01092259

as the serial number. I am only incrementing the last four numbers. My G code as I have it so far will do that one number using the following

G90G00X0.2500Y-0.0250
M98 P#1210

G00X0.185Y-0.025
M98 P#1201

G00X0.125Y-0.025
M98 P#2010

G00X0.06Y-0.025
M98 P#2001

G90G00X0.00Y-0.025
M98 P#1000

G00X-0.065Y-0.025
M98 P#100

G00X-0.13Y-0.025
M98 P#10

G00X-0.20Y-0.025
M98 P#1

M995                    (This is the macro that I need to write to do the increment)

 O0
G00 Z0.01
G91
G00 X-0.0424 Y-0.0229
F12.0
G90
G01 Z-0.0030 F8.0
G91 G02 X0.0358 Y0. I0.0179 J0. F12.0
G01 Y-0.0293
G02 X-0.0358 Y0. I-0.0179 J0.
G01 Y0.0293
G90 G00 Z0.01
M99
 
O1
G90 G00 Z0.01
G91 G00 X-0.0076 Y-0.0574
G90 G01 Z-0.0030 F8.0
G91 G01 X-0.0126 Y-0.0126 F12.0
G01 Y0.065
G90 G00 Z0.01
G91 G00 X0.0152
G01 G90 Z-0.0030 F8.0
G01 G91 X-0.0309 F12.0
G90 G00 Z0.01
M99

I only included code for two numbers. What I want to do is use the M950 to read #1, #10, #100 and #1000 and increment as needed
and return them to the program. Is that possible?
Title: Re: VB and variables in G code
Post by: MachineMaster on March 17, 2009, 08:49:32 PM
OK, I have changed some of the G code near the end of the file as follows

G00X-0.065Y-0.025
M98 P#100

G00X-0.13Y-0.025
M98 P#10

G00X-0.20Y-0.025
M98 P#1

M950                    (This is the macro that I wrote. See below)
G92.1
M05
G00 G90 X0 Y0 Z0
M01
M98 P200

M02


 O0
G00 Z0.01
G91
G00 X-0.0424 Y-0.0229
F12.0
G90

Macro
a = GetVar (1)
Print a
b = GetVar(10)
Print b
c = GetVar(100)
Print c
d = GetVar(1000)
Print d

a = a+1
If a = 10 Then a = 0
If a = 0 Then b = b + 1
SetVar 1, a
Print a
If b = 10 Then b = 0
If b = 0 Then c = c+1
SetVar 10, b
Print b
If c = 10 Then c = 0
If c = 0 Then d = d+1
SetVar 100, c
Print c
If d = 10 Then d = 0
SetVar 1000, d
Print d

I put the print statements in to see what was happening and it runs fine when called but in the VB assist window it prints the values.
Title: Re: VB and variables in G code
Post by: vmax549 on March 18, 2009, 12:31:37 AM
Here is another example ,

A=0
B=0
C=0
D=0

A= A+1
If A>9 Then B=B+1
If A>9 Then A=0

If B>9 Then C=C+1
If B>9 Then B=0 

If C>9 Then d=D+1
If C>9 Then C=0   

Print " " &D &C &B &A
End

NOTE:  Mach has parameters that are persistant so they hold there value when Mach is shut down. They are Param 500-600. You can store you serial number there so it is always available. You can veiw the vars with the Mach var monitor to check the values.

(;-) TP
Title: Re: VB and variables in G code
Post by: MachineMaster on March 18, 2009, 01:54:10 AM
Thank you. The tip about the persistent variables is great. Now I can do away with the variables at the beginning of the G code and set the new variables to what I want with another macro that I wrote just for that. I just bring it up in the VB assistant, enter what I want and run the macro right in the assistant.
Now I can end the main program with an M30 and not have my variables over written.
Again, Thank you
Title: Re: VB and variables in G code
Post by: vmax549 on March 18, 2009, 03:49:58 PM
Glad I could help out, (;-)  When you are all done and you want to share you can post the finished code here for all to see how you did it.  It can be inspiring when you see an example of something.

(;-) TP
Title: Re: VB and variables in G code
Post by: MachineMaster on March 19, 2009, 11:12:29 PM
Still not getting it. I got my first macro working so I started on another one.

'M79.m1s
V = GetVar (550)
V1 = (V/2.0)
Code "G90 G92 X0. Y0."
Code "G01 X&V1 Y&V1"
Code "G03 X.0 Y&V I-&V1 J.0 G90"
Code "G03 X.0 Y&V I.0 J-&V G90"
Code "G03 X-&V1 Y&V1 I.0 J-&V1 G90"
Code "G01 G90 X.0 Y.0"
Code "G92.1"
End
   
Var 550 is set at .5 and if I put a Print statement in like
Print V1 the out put is the expected .25 but when I try to run M79 in my G code I get

No digits found where real number should be

The G code calling the M79 is

G49G80G90G00
X1.Y-1.F3.
M79
X0Y0
M30

If I modify the Macro to

'M78.m1s
V = GetVar (550)
V1 = (V/2.0)
Code " G90 G92 X0. Y0. "
Code "G01 X.25 Y.25"
Code "G03 X.0 Y.5 I-.25 J.0 G90"
Code "G03 X.0 Y.5 I.0 J-.5 G90"
Code "G03 X-.25 Y.25 I.0 J-.25 G90"
Code "G01 G90 X.0 Y.0"
Code "G92.1"
End
   
with real numbers it works the way I want it to. What am I doing wrong when I am trying to use variables in the G code in the macro?
Title: Re: VB and variables in G code
Post by: MachineMaster on March 20, 2009, 04:03:49 PM
I finally figured it out by just using one variable in one line of code until I got that to work and then added a second variable and worked at it until I got it to work. This is the result

V = GetVar (550)
V1 = V/2.
Code "G90 G92 X0. Y0."
Code "G01 X"&V1 & "Y"&V1
Code "G03 X.0 Y"&V & "I-"&V1 & "J.0 G90"
Code "G03 X.0 Y"&V & "I.0 J-"&V & "G90"
Code "G03 X-"&V1 & "Y"&V1 & "I.0 J-"&V1 & "G90"
Code "G01 G90 X.0 Y.0"
Code "G92.1"
End         

I had to get all of the quotes and &s in the right place.
The way it works is you put "&variable where the number would go and if there is more G code to follow on the same line it looks like this

Code "G01 X"&variable &"Y"&variable &"Z-.25"

There needs to be a space between variable and &"
Title: Re: VB and variables in G code
Post by: MachineMaster on March 22, 2009, 10:40:38 PM
Here is the final version of my M79 hole boring macro.


'M79.m1s
Setvar(550,Param1)
Code"#551=[#550/2]"
Code"G90 G92 X0. Y0."
Code"G01 X[#551] Y[#551]"
Code"G03 X.0 Y[#550] I[-1* #551] J.0 G90"
Code"G03 X.0 Y[#550]I.0 J[-1*#550] G90"
Code"G03 X [-1*#551] Y[#551] I.0 J[-1*#551] G90"
Code"G01 G90 X.0 Y.0"
Code"G92.1"
End

and it will bore any size hole by calling it like this

G0G90X1.Y1. (Position to hole center)
G01Z-.5 F10. (feed to depth)
M79 P1.000
G0Z0

The macro feeds from the center, does a 90deg arc to a point tangent to the circle to be cut, runs a circle, does a 90deg arc toward the center and then feeds back to the center.
Title: Re: VB and variables in G code
Post by: vmax549 on March 24, 2009, 11:09:06 AM
WHat you have there is , it will go to X1Y1 then bore a hole striaght down to -.5 then try to cut the circle (;-) You might want to add in 2 more params.
Title: Re: VB and variables in G code
Post by: MachineMaster on March 24, 2009, 01:32:25 PM
What you have there is , it will go to X1Y1 then bore a hole striaght down to -.5 then try to cut the circle (;-) You might want to add in 2 more params.

Param1 sets #550 with Setvar(550,Param1)
#551 gets set with Code"#551=[#550/2]"
From there everything is in place to cut the circle. I am using it and it works great.
Title: Re: VB and variables in G code
Post by: vmax549 on March 24, 2009, 01:58:48 PM
as long as you can do full depth cuts in one pass(;-)

(;-) TP
Title: Re: VB and variables in G code
Post by: MachineMaster on March 24, 2009, 06:18:08 PM
OK, I see what you mean. I am cutting holes in a plastic box.
For multiple depths using this macro just feed down and call the macro as many times as needed.
I guess it wouldn't be to hard to modify the macro to increment Z down until #552 variable was reached and set #552 in the G-code.