Machsupport Forum

Mach Discussion => VB and the development of wizards => Topic started by: nickkinsman on March 17, 2014, 02:06:38 PM

Title: VB Script for toolchanger
Post by: nickkinsman on March 17, 2014, 02:06:38 PM
Hello;

I'm setting up Mach3 to run a lathe with a 4 position toolpost on it.  I've generated a pass at the VB script with the help of a friend (I'm no programmer) and I wanted to see if someone could proof it and see if it would work.  I just downloaded & configured Mach3, so it would be used on the most current version.  Here are the specs on the toolchanger and what I am attempting to do.

-4 position changer, air actuated so it only spins in one direction (CW, in case it matters).  Added complexity is the changer can hold multiple tools on a single face.  The changer only moves 1 step at a time, and requries another input to progress another step - always in the same direction.  i.e. it has 4 faces, to get from face 2 back to face 1, it has to progress 3 steps.

It recieves a 24V momentary (pushbutton type) signal which tells it to start actuating, and it finishes 1 step with no other input, automatically seats.  It also has a 24V feedback signal that changes state between seated, or turning.  (I'm not sure of the exact state between the two, still have to check).  

What I thought to do with the code was build a table that would equate a tool to a face, numbering the tools around the holder sequentially 3 times (giving option for 3 tools on a face) and equating that tool number to a face 1 through 4.  This way the code could get the current tool and find out what face that equated too, get the requsted tool - what face that equated too, then run the loop to step the toolchanger around until the values were equal.


Here is the script:
Sub Main
              Dim currentLocation As Integer
              Dim targetLocation As Integer
              Dim currentToolID As Integer
              Dim targetToolID As Integer
              Dim tool (13) As Integer

              tool (0) = 0
              tool (1) = 1
              tool (2) = 2
              tool (3) = 3
              tool (4) = 4
              tool (5) = 1
              tool (6) = 2
              tool (7) = 3
              tool (8) = 4
              tool (9) = 1
              tool (10) = 2
              tool (11) = 3
              tool (12) = 4

              currentTool = GetCurrentTool()
              targetTool = GetSelectedTool()
              
              currentLocation = tool(currentTool)
              targetLocation = tool(targetTool)

              While currentLocation <> targetLocation
                            If Not IsActive(InputX) Then
         ActivateSignal(OutputX)
                                   Sleep (250) 'Signal index pin for 0..25sec
                                DeActivateSignal(OutputX)
                                While IsActive(InputX) Then
                                                 Sleep (50) 'Allow toolchanger to seat
                                  Wend

                                currentLocation = currentLocation + 1
                                If currentLocation > 4 Then
                                              currentLocation = 1 'resets current location back to 1 since completed 1 revolution
                                End If
      End If
              Wend
End Sub


One question we had that is important is will Mach3 return an integer when presented with the GetCurrentTool?  If not I need to know what it sends so I can decode it!
Thanks, feedback appreciated!
Title: Re: VB Script for toolchanger
Post by: stirling on March 19, 2014, 07:50:37 AM
Looks OK(ish) but you don't really need an array. The relationship between tool# and face# is simply face = INT(tool / 3).

Your check for existing movement before you rotate is spurious. If somehow it IS moving already you'd be better bailing out because a) it shouldn't be and b) your calculated face number will be invalid by the time it stops.

Finally, anyone doing a rollover 1,2,3,4,1,2 etc. might want to look up the MOD operator.
Title: Re: VB Script for toolchanger
Post by: nickkinsman on March 19, 2014, 12:33:36 PM
Thanks for checking it out.  Can you elaborate on the function you describe, INT(tool/3)?  I assume that is nearest integar of tool number divided by 3, but I don't see how that would work - tool 1/3 = face1, tool 2/3 =face1, tool3/3= face1?

The real purpose of the check command on rotation was for the looping of multiple rotation steps, to be sure it doesn't 'push the button' again before it is seated at the end of prior loop, as this messes with the toolchanger.  but I agree it is pretty redundant when we are already checking for motion at the end of the while loop before proceeding.

Thanks.
Nick
Title: Re: VB Script for toolchanger
Post by: stirling on March 19, 2014, 02:52:31 PM
Can you elaborate on the function you describe, INT(tool/3)?  I assume that is nearest integar of tool number divided by 3, but I don't see how that would work - tool 1/3 = face1, tool 2/3 =face1, tool3/3= face1?
LOL - trust me - I've been doing this software sh1t for a loooooong time.

Number your 12 tools from 0 to 11 and your 4 faces from 0 to 3 and see what happens. (If for some reason you prefer counting from 1 then just add it afterwards).

The real purpose of the check command on rotation was for the looping of multiple rotation steps, to be sure it doesn't 'push the button' again before it is seated at the end of prior loop, as this messes with the toolchanger.  but I agree it is pretty redundant when we are already checking for motion at the end of the while loop before proceeding.

The crux is in your statement that I've bolded. Just change the word "pretty" for "completely" and we're cool.
Title: Re: VB Script for toolchanger
Post by: nickkinsman on March 20, 2014, 03:11:28 PM
I confess after noodling it for awhile, that I can't figure out how to replace the array with the INT(tool / 3) function in the coding.  I understand the While section and what that does, but I admit that I don't know what I shoud replace or not in the 'Dim' and array section, setting up the While section to do the changes. 

Another question too, can I use 0 as the base tool?  I looked in Mach and it reserves the 0 position and you can't add to it?

Thanks for helping.  PS, I took out that redundant code!
Nick

Title: Re: VB Script for toolchanger
Post by: stirling on March 21, 2014, 06:22:15 AM
My apologies Nick - it's my mistake (I was not having a good day). I now see your tools are alternated around the faces not sequentially around them. The INT(tool / 3) would place them like this: (diagrams are for CW rotation but the maths doesn't actually care)

Code: [Select]
   2 1 0
     0
 3       11
 4 1   3 10
 5       9
     2
   6 7 8

Whereas I now see you want them:

Code: [Select]
   8 4 0
     0
 1       11
 5 1   3 7
 9       3
     2
   2 6 10

In this case the relationship between your faces and tools is:

Code: [Select]
face = tool mod 4 (where face = 0 to 3 and tool = 0 to 11)

However to allow your tools to be numbered from 1 rather than from 0 just adjust this to:

Code: [Select]
face = (tool - 1) mod 4 (face will still be 0 to 3 but that's fine)

putting this together your toolchange could look like this. (un-tested but I think it's right)

Code: [Select]
selectedTool = getSelectedTool()
currentFace = (getCurrentTool() - 1) mod 4
targetFace = (selectedTool - 1) mod 4

'rotate turret required number of "clicks"
For click = 1 To (targetFace - currentFace + 4) Mod 4 'rotate accounting for rollover

  ActivateSignal(OutputX) 'activate momentary command to rotate

  While Not IsActive(InputX) Then 'wait for turret to move
    Sleep (10)
  Wend

  DeActivateSignal(OutputX) 'deactivate momentary command

  While IsActive(InputX) Then 'wait for turret to stop
    Sleep (10)
  Wend

Next

'not forgetting to update to the new tool
SetCurrentTool selectedTool

A couple of notes:

There's nothing wrong per se in using an array it's just that it's unnecessary when the relationship can be calculated easily.
I've taken out the explicit timings for the signals so that it uses what's actually happening instead.
I'm assuming this is code for your M6Start macro.
There is no check for the turret already rotating at the time you command a tool change - I'll leave that to you to decide whether you think it necessary.
Title: Re: VB Script for toolchanger
Post by: nickkinsman on March 24, 2014, 01:31:52 PM
Thank you very much for revisiting the coding - your 2nd interpretation is exactly right.  I will load up this and give it a go, I'll respond back on how it all works out, probably not for a couple weeks as I am still in the hookup stage, terminating all the wires for the controls so I still have to get that done before I can test much for this.  I'll make sure to respond on how it goes so anyone else can reference that it works for the future - unclosed threads is one of my pet peeves as it makes using the information very hard!

Did get the Viper servo's powered up last night though, so making progress!
Title: Please Review! VB Script for 4-position toolchanger.
Post by: nickkinsman on April 04, 2014, 11:55:24 AM
So, I finally finished terminating all the wires and tried the code.  I am not seeing action when I call for a tool change in MDI. 

I've checked the signal out to trigger the changer works (Output2), signal in from the changer is set to 'active' when it is moving (Input2), all verified in Mach diagnostics.

I did a stepthru of  the code in the VB editor, and it actually triggered the toolpost once!!! But then never again ???  Sooooo close.....When I open Mach, I've set the tool mdi to '1' as the starting spot.  Then I was doing a T202 M06 to call for tool two.   I have the config set to 'auto' on toolchange options.  PS - I took out the 'then' pieces, I was getting a compile error - I looked at the Mach guides and they didn't seem to be needed for a 'While' command set.

I would really appreciate if someone could take a look at the code and verify if it all looks good, here is the exact code that is in the macros/Lathe/M6start file:

'M6Start.m1s
Sub Main()
selectedTool = GetSelectedTool()
currentFace = (GetCurrentTool() - 1) Mod 4
targetFace = (selectedTool - 1) Mod 4

'rotate turret required number of "clicks"

For click = 1 To (targetFace - currentFace + 4) Mod 4 'rotate accounting for rollover

   ActivateSignal(Output2) 'activate momentary command to rotate
      Sleep (100)
   DeActivateSignal(Output2) 'deactivate momentary command

   While IsActive(Input2) 'wait for turret to stop
      Sleep (100)
   Wend
Next

SetCurrentTool selectedTool 'not forgetting to update to the new tool 

End Sub   

Title: Re: VB Script for toolchanger
Post by: stirling on April 05, 2014, 04:21:51 AM
sorry about the "thens" on the whiles - that's what happens when you modify someone else's code (you have the while...then in your first code) - I should've spotted that.

So does my code work once you removed the "thens"? - Not sure why you've taken the first while out but it's your choice.

Also you don't need to wrap it in a sub

i.e.

Code: [Select]
selectedTool = getSelectedTool()
currentFace = (getCurrentTool() - 1) mod 4
targetFace = (selectedTool - 1) mod 4

'rotate turret required number of "clicks"
For click = 1 To (targetFace - currentFace + 4) Mod 4 'rotate accounting for rollover

  ActivateSignal(Output2) 'activate momentary command to rotate

  While Not IsActive(Input2) 'wait for turret to move
    Sleep (10)
  Wend

  DeActivateSignal(Output2) 'deactivate momentary command

  While IsActive(Input2) 'wait for turret to stop
    Sleep (10)
  Wend

Next

'not forgetting to update to the new tool
SetCurrentTool selectedTool
Title: Re: VB Script for toolchanger
Post by: stirling on April 06, 2014, 05:57:57 AM
wrote this a bit blind because obviously I don't have such a toolchanger. However I just made a simple simulator to check the code and it works fine here with one caveat. After starting Mach, I have to initialize the current actual tool in the tool number DRO before I call the first M6 - otherwise it doesn't know where it is to start - i.e. it thinks it's on tool 0 which equates to face -1. Don't know how you auto-toolchanger guys do your toolchange init but that will need adding somehow.
Title: Re: VB Script for toolchanger
Post by: nickkinsman on April 08, 2014, 08:27:15 AM
No problem, again very appreciative that you took the time to help me work it out!  Good learning experience for me too. 

OK, I'll try it again, I tried several times, the only time I saw action was when I stepped thru the code in the VB editor once, it triggered a move.  But then any time I tried a step thru again, it did not trigger.  It could be possible too maybe I don't have enough time on the sleep for the output signal 'button push' and it's not initializing fast enough....I'll check that too. 

When I took out the 'then' the VB editor step thru worked, and I did get that one movement one time, but could not get it to work again. Now that I am noodling it a bit it's probably a timing issue on the signal....I'll try some iterations of sleep time and see what it does. 

And yes, agree with you I figured I would have to tell it what tool it has on startup - I thought Mach would keep the tool as there is a 'persistant tool'  setting in Mach config, but it doesn't seem to remember  :(

I took out the first while statement because the latching of the signal is very good, it does not latch until fully seated, so I figured was not necessary.  Initially I wasn't sure if it only signaled on motion or not, but it turned out that it doesn't signal until the changer is fully seated. 

Thanks, Nick

Thanks for looking at it! 
Title: Troubleshooting
Post by: nickkinsman on April 09, 2014, 09:43:43 AM
OK, so...no solution yet....

I tried a bunch of iterations with the code and here is what I found;

As written, still no action.  When I FIRST opened Mach3 and immediately opened the VB editor and did a step-thru of the code, it stepped thru the 'For' section and did 1 toolchange step.  After that, it no longer will step thru the 'For' section, it skips right over it every time.  

Then I went into MDI, changed the tool to '1' in the DRO.  Did a T202 M6.  The computer bings, and the tool immediately changes to 2.  No action at all on the toolchange.  It acts like it is not reading the code, or if it is, it is immediatley skipping to the end.  I checked again, the toolchange is set to 'auto'.

I then looked at the code to try some things - I deleted the 'Mod 4' at the end of the 'for' statement on a whim and tried the step thru.  With that gone, it steps thru the 'for' statement, and I was able to verify that all the steps in the for statement work perfectly.  Every time I step thru, it completes a tool change very well.  The only modification I made was to increase the sleep time on the output signal, as it takes a moment for the input signal to latch on start of the toolchange.  So that's good.

I reapplied the 'Mod 4' portion and tried to do stepthru, still no tool change, it skips right over the 'for' section.  

I also retried to do a toolchange in the MDI, (both with and without the Mod 4) still no action.  

In summary, it seems like the issue may be in the counting statements at the start of the code, or in the 'For' statement line, is the only thing I can think of, as the actual toolchange seems to work well.  I don't really understand teh MOD command and what it is doing, so I'm not sure how to troubleshoot it.

Just to check, I am running the trial verison of Mach to proof it out before I buy the full license, is there a limitation on the toolchange function?  I know threading was not enabled but I didn't think toolchange was an issue.  

Thanks.  
Title: Re: VB Script for toolchanger
Post by: stirling on April 09, 2014, 10:55:18 AM
Let's just check a couple of things.

To use it for real you have to:

1) have the code (and nothing else) in M6Start.m1s (in the correct profile folder)
2) Start Mach
3) Enter 1 into the tool # DRO (to initialize at a known point - we'll deal with this later)
4) Switch to MDI
5) Enter M6 T(any number between 1 and 12)
6) Repeat 5 as you like.

OR - if you want to step through the code and see it working you have to:

1) have absolutely NOTHING in M6Start.m1s (in the correct profile folder)
2) Start Mach
3) Enter 1 into the tool # DRO (to initialize at a known point - we'll deal with this later)
4) Switch to MDI
5) Enter M6 T(any number between 1 and 12)
6) step through the code
7) repeat from 5 as you like.

I've done both here (quite a few times now ;D) and it works sweet as a nut every time.

Just tried it without my license and it works fine also in demo mode.
Title: Re: VB Script for toolchanger
Post by: nickkinsman on April 09, 2014, 11:56:07 AM
Well, I am glad to hear it is working for someone, that is a big positive!!!

I'll run thru your steps tonight verbatim and give it another try.  I certainly can see why it would not work on step thru as no tool change on the inital step is asked for, so it skips the clicks.  Anyway, here's fingers crossed!

Thanks.
Nick
Title: Re: VB Script for toolchanger
Post by: stirling on April 10, 2014, 05:27:29 AM
Not being a lathe person I just realized that tool change syntax appears to be different to mills. However it makes no odds. I just tried T0202 or whatever in MachTurn and the routine I gave you still works a charm (just drop the M6 command though in my blerb above). Note that although you don't appear to state M6 in the command the code still goes in M6Start.m1s.

Maybe if some lathe guru could comment perhaps but I think the above is correct.
Title: Re: VB Script for toolchanger
Post by: nickkinsman on April 10, 2014, 08:27:16 AM
Hello Stirling;

I was able to get the code to work, my stupidity - had it in the generic lathe config vs. the one for my lathe.  So great news there anyway!!

But...it only gives me 1 step of the turret no matter what tool I try to change to.  i.e. - start with tool 1 in the DRO, ask for tool 3, it does only 1 step.

I started to research the MOD command today because I would like to understand it better, but I am a little stumped on how the math works, for instance using the 1--> 3 example;

current face = (1-1) mod 4 = 0div4 = 0, ok good so far
target face = (3-1) mod 4 = 2div4= 0, remainder of 0.5 = 1? (partial remainder?), answer should be 2 though, correct?

Does the Mod command act like a divisor, where a Mod b = a/b (and uses the remainder)?  And how does it work for fractional numbers?  I'm going off wikipedia here, I could not find a good example in the manuals, especially for fractional. 

Thanks.
Nick

Title: Re: VB Script for toolchanger
Post by: stirling on April 10, 2014, 09:36:31 AM
current face = (1-1) mod 4 = 0div4 = 0, ok good so far
target face = (3-1) mod 4 = 2div4= 0, remainder of 0.5 = 1? (partial remainder?), answer should be 2 though, correct?

Close but no cigar.

The mod(ulus) is the remainder after division

forget fractional numbers for the moment because we're not using them.
if both operands are integers then the division and result are integer also. so....

current face = (1-1) mod 4 = 0/4 = 0 with 0 remainder
target face = (3-1) mod 4 = 2/4 = 0 with 2 remainder

mod is the classic mechanism for handling rollovers.

copy and paste this code exactly as is into your M6Start.m1s and do a few toolchanges and get back

Code: [Select]
selectedTool = getSelectedTool()
currentTool = getCurrentTool()
currentFace = (currentTool - 1) Mod 4
targetFace = (selectedTool - 1) Mod 4
clicks = (targetFace - currentFace + 4) Mod 4
MsgBox "Selected Tool=" & selectedTool & _
       " Current Tool=" & currentTool & _
       " Current Face=" & currentFace & _
       " Target Face=" & targetFace & _
       " Clicks=" & clicks
SetCurrentTool selectedTool
Title: Re: VB Script for toolchanger
Post by: nickkinsman on April 10, 2014, 10:44:49 AM
I'll do that and see what the responses are. 

So, in summary, the mod function doesn't care if it is 'a mod b' or 'b mod a', it just takes the remainder after dividing into the larger number? (That is what I meant by acting like a divisor, depending if the first integar is larger than second, would be a fraction at the start.)

Nick
Title: Re: VB Script for toolchanger
Post by: stirling on April 10, 2014, 11:34:17 AM
So, in summary, the mod function doesn't care if it is 'a mod b' or 'b mod a', it just takes the remainder after dividing into the larger number? (That is what I meant by acting like a divisor, depending if the first integar is larger than second, would be a fraction at the start.)

Nick

It matters very much which number comes first and which comes second. a mod b is most definitely not the same as b mod a. Which is largest and which is smallest is irrelevant. ;)

a mod b evaluates to the remainder after the integer division of a/b. period.

but don't get hung up over this - just run that last piece of code and let me know what happens. If it doesn't give you the correct results then it's for some other reason - it has nothing to do with mod. The code is correct.
Title: Re: VB Script for toolchanger
Post by: nickkinsman on April 10, 2014, 02:28:57 PM
OK, I think I understand the Mod function (Modulus) in Mach3 VB script, figured I'd post it here in case someone else needs to reference;

For 'a Mod b',

If a<b, uses full 'a' value as product.  i.e. '2 mod 3' = 2.  (this is what was confusing me).
If a > or = b, divide 'a' by 'b' and the remainder is your product.  i.e. for '9 mod 4', 4 goes into 9 twice evenly (8 ), with a remainder of 1 so 1 is the product.

Hope this helps, it was confusing me, but now it is all clear!   :D

And Stirling, you are absolutely right, it all works out correctly!  

Nick
Title: All Done VB Script for 4 position turret lathe toolchanger
Post by: nickkinsman on April 10, 2014, 09:25:28 PM
And we have a winner  :)

Turns out the 'turret rotating' signal, Input2, was not latching fast enough, so the 'While' command was not taking effect becuase there was not enough delay between the output signalling and the while.  In essence it was skipping right on and doing further 'clicks' while the turret was still in motion.  I also added a MachMsg as an initial step, as lack of air pressure also drives the Input1 active, so it reminds me to connect air pressure if I ask for a toolchange without air connected.  Since I turn off the air in my garage, this is pretty handy.

Again, thanks for alll the help.  Here is the final working code from my M6Start.m1s file, for anyone's reference.  Again this is a 4-position rotary toolpost on a lathe which holds multiple tools, and I am numbering my tools sequentially counterclockwise around the holder (as you look at it with bird's eye view).  Holder rotates clockwise.

'M6Start.m1s

If IsActive(Input2) Then
   MachMsg ("Connect Air Pressure to Turret!!","Turret Index Warning",MachMsgTypeOK)
End If

selectedTool = GetSelectedTool()
currentTool = GetCurrentTool()
currentFace = (currentTool - 1) Mod 4
targetFace = (selectedTool - 1) Mod 4

'rotate turret required number of "clicks"

For click = 1 To (targetFace - currentFace + 4) Mod 4 'rotate accounting for rollover

   ActivateSignal(Output2) 'activate momentary command to rotate
      Sleep (500)
   DeActivateSignal(Output2) 'deactivate momentary command and wait for movement signal
      Sleep (1500)
   While IsActive(Input2) 'wait for turret to stop
      Sleep (100)
   Wend
Next

SetCurrentTool selectedTool 'not forgetting to update to the new tool


Title: Re: VB Script for toolchanger
Post by: stirling on April 11, 2014, 03:13:01 AM
OK, I think I understand the Mod function (Modulus) in Mach3 VB script, figured I'd post it here in case someone else needs to reference;

For 'a Mod b',

If a<b, uses full 'a' value as product.  i.e. '2 mod 3' = 2.  (this is what was confusing me).
If a > or = b, divide 'a' by 'b' and the remainder is your product.  i.e. for '9 mod 4', 4 goes into 9 twice evenly (8 ), with a remainder of 1 so 1 is the product.

Hope this helps, it was confusing me, but now it is all clear!   :D

And Stirling, you are absolutely right, it all works out correctly! 

Nick

Hi Nick - Pleased it's working for you.

However - your mod understanding is still incorrect.

It's simply INTEGER division.

In INTEGER division (by definition) there are no FRACTIONAL parts. So it's as simple as dividing the a by b and taking the INTEGER result. Whatever is "left" after that INTEGER division is the MODULUS.

at NO point is it of ANY RELEVANCE WHATSOEVER whether a<b or a>=b


Ian
Title: Re: VB Script for toolchanger
Post by: nickkinsman on April 14, 2014, 12:37:00 PM
OK!  I will have to settle for not really understanding it, but it certainly works!!  Good thing there are others out there that do.  

And the machmsg thing is pretty sweet too!

Finally, Mach3 does seem to be retaining the last tool loaded, so that takes care of having to set the initial tool in the DRO as well!!  Perfect.

Again, thank you very much Ian for sticking with it and helping me thru my ignorance!  You are the role model for what a forum moderator should do!

Nick