# Machsupport Forum

## Mach Discussion => General Mach Discussion => Topic started by: Osker on December 28, 2007, 08:23:02 AM

Title: Implementation of Logical IF Then Statements
Post by: Osker on December 28, 2007, 08:23:02 AM
Hello,  I am transitioning from TurboCNC to MACH3 and need assistance with implementing Logical IF Then Statements within MACH3.  The following code snippet within TurboCNC would be used to cut a profile on a lathe (Radius, with X.0 being the Center) by comparing the current cutting depth to the targeted profile depth of cut.  If the  profile depth of the X-axis is below or equal to the current depth of cut, there is no change in the depth of the cutting tool.  If the profile depth of cut is above the current depth of cut, the cutting tool moves to the profile depth of cut.  Each coordinate of the profile is evaluated in the same manner to complete the desired shape.

If there is a way within MACH3 to implement a similar approach, please point me in the right direction.

Regards,

Dan

The concept is to start as some X position that is greater than the stock radius, and decrement the value each pass, until the target depth is achieved.  In the following example #3 is the starting radius, #4 is the target X profile coordinate.  #6 is the target Z profile depth of cut.  For example,

N10 #3 = 0.1
N20  #4 = 0.0375  #6 = -0.2878 M98o770
N30  #4 = 0.0575  #6 = -0.3108 M98o770
and so on for each coordinate of the profile.
After the last coordinate, the depth of the pass would be decremented, and the routine would loop again

% Profile Subroutine

N770 If #4 LE #3 M97o800  //If target X <= Current  X , then cut at current X depth
N780 G01 X#4 Z#6 F#2  //Else cut at profile X & Z coordinates
N800 G01 X#3 Z#6 F#2  //Cut at current X depth
Title: Re: Implementation of Logical IF Then Statements
Post by: jimpinder on December 28, 2007, 08:50:00 AM
Dan - the way I have approached this is to write VisualBasic programs. There is a Visual Basic editor in Mach 3.

I write these macros and give them M numbers - M201, M202 etc etc. Visual Basic is a straightforward computer language and If, While, For etc can all be used. My macros include one to thin down a piece of stock to a required diameter over a given length at a desired depth of cut, and one to round the end of a bar to a given radius.

I have included additional DRO's on page one of Mach3 Turn, and I can fill in the various dimensions I require.

All I do is move the cutting tool to a known start point, call up the Macro and away it goes. Macros can be included in ordinary G Code programs as well. They work on a system of calculating the moves required, then writing the code to the computer on the fly, so the only code you see in the program is e.g. M202.

There is a video tutorial on Vis Basic in the collection.

Jim
Title: Re: Implementation of Logical IF Then Statements
Post by: poppabear on December 28, 2007, 10:06:22 AM
Replace these lines
N770 If #4 LE #3 M97o800  //If target X <= Current  X , then cut at current X depth
N780 G01 X#4 Z#6 F#2  //Else cut at profile X & Z coordinates

With

M888.m1s

It will do your "If then else" statement

'M888.m1s here is an example

TargetX=GetVar(4)
Feed=GetVar(2)
TargetZ=GetVar(6)

If TargetX<=XStart Then
Code "G01 X" & XStartRadius & " Z" & TargetZ & " F" & Feed
Else
Code "G01 X" & TargetX & " Z" & TargetZ & " F" & Feed
While IsMoving
Wend
End If

'Scott

Title: Re: Implementation of Logical IF Then Statements
Post by: poppabear on December 28, 2007, 10:07:55 AM
oh, it also replaces this line

N800 G01 X#3 Z#6 F#2  //Cut at current X depth
Title: Re: Implementation of Logical IF Then Statements
Post by: jimpinder on December 28, 2007, 02:15:43 PM
Scott - Am I missing something here - is there a Macro already written to thin down axles and such - i.e. M888. If so where does it get the data from for Var2 3 4 and 6

Jim.
Title: Re: Implementation of Logical IF Then Statements
Post by: Osker on December 29, 2007, 07:55:10 AM
Jim/Scott,

Thanks for the reply and pointing me to the macro feature of Mach3.  In addition to trying out the sample code, I will research the topic and give it a shot.

Dan
Title: Re: Implementation of Logical IF Then Statements
Post by: poppabear on December 29, 2007, 11:01:59 AM
jimpinder,

The way I read his post was, his CAM program put out G-code for Turbo CNC to run, and he wanted to be able to run that code in Mach. I looked over his Code. Mach understands and will run everything but the "If Then Else" type G-code statements that TurboCNC runs.

So, The M888.m1s was a Macro I wrote that pulled in the Values from the Parameters that where already posted, and processed them (basically replaced the if then statement he was asking about).

If he wants to Create a "Wizard" that will do something different than what the Lathe Wizards currently do for creating code, then I figured he would ask about that, in case I misunderstood the post.

Sorry, if any confusion was caused.

Scott
Title: Re: Implementation of Logical IF Then Statements
Post by: jimpinder on December 30, 2007, 10:21:45 AM
No - thats OK - we are obviously on the same wavelength.  I am still relatively new on the Mach3 scene, so I still like to get involved.

As I said, I have written several Macros to do all sorts of little turning jobs that appertain to my particular field, which is building miniature railway equipment, and I find them very useful.

The only problem with them is, once they are running, you loose track of where they are in the cycle. I am still looking for a system to write the G Code in the same style, but, when I enter it into Mach 3 , it shows where you are in the program as it progresses through - if you understand what I mean.

I dont know if you can write G Code in computer style, with If, While etc commands. It would be nice if you could.

JIm
Title: Re: Implementation of Logical IF Then Statements
Post by: Osker on December 31, 2007, 01:13:45 PM
Scott,

Your understanding of my question was correct.  Your sample code was just what I needed to get started.  Between your example and the manual, I was able to get the code to work.  Thanks for pointing me in the right direction.  The Mxx.Mls function will have a lot of utility.  I just need to become more proficient in the use of VS scripts.

One thing I noticed is that the manual (10.9.1, page 10-31) states "The Operator > Macros menu displays a dialog which allows yo to see currently installed macros, to Load, Edit and Save..."  I do not find that option.  I used the VB Script Editor instead.  It worked, but is there a configuration setting that I should change?

Thanks again for the assistance!!

Dan
Title: Re: Implementation of Logical IF Then Statements
Post by: poppabear on December 31, 2007, 04:37:40 PM
no, the "Manual" is in sore, sore, sore, need of revising.......  The one your talking about no longer exist, You used the correct one.

scott
Title: Re: Implementation of Logical IF Then Statements
Post by: Osker on December 31, 2007, 05:20:27 PM
One last request for the day.

Please recommend a reference or provide some guidance for writing macros for Mach3.  I have a lot more code to re-write and having a grasp of the fundamentals will be more efficient than me asking the forum.

Thanks in advance for the assistance.

Dan