Hello Guest it is March 28, 2024, 03:39:29 PM

Author Topic: VB and variables in G code  (Read 17291 times)

0 Members and 1 Guest are viewing this topic.

VB and variables in G code
« 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?

Offline poppabear

*
  • *
  •  2,235 2,235
  • Briceville, TN, USA
    • View Profile
Re: VB and variables in G code
« Reply #1 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
fun times
Re: VB and variables in G code
« Reply #2 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?

Offline ger21

*
  • *
  •  6,295 6,295
    • View Profile
    • The CNC Woodworker
Re: VB and variables in G code
« Reply #3 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.
Gerry

2010 Screenset
http://www.thecncwoodworker.com/2010.html

JointCAM Dovetail and Box Joint software
http://www.g-forcecnc.com/jointcam.html

vmax549

*
Re: VB and variables in G code
« Reply #4 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
Re: VB and variables in G code
« Reply #5 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.

vmax549

*
Re: VB and variables in G code
« Reply #6 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

Re: VB and variables in G code
« Reply #7 on: March 16, 2009, 01:02:01 PM »
Thank you. I would really appreciate that. Often all I need is a good example.

vmax549

*
Re: VB and variables in G code
« Reply #8 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

Re: VB and variables in G code
« Reply #9 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?