Hello Guest it is April 19, 2024, 01:26:25 AM

Author Topic: VB Script for toolchanger  (Read 11505 times)

0 Members and 1 Guest are viewing this topic.

VB Script for toolchanger
« 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!
« Last Edit: March 17, 2014, 02:08:57 PM by nickkinsman »

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: VB Script for toolchanger
« Reply #1 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.
Re: VB Script for toolchanger
« Reply #2 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

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: VB Script for toolchanger
« Reply #3 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.
Re: VB Script for toolchanger
« Reply #4 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

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: VB Script for toolchanger
« Reply #5 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.
Re: VB Script for toolchanger
« Reply #6 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!
Please Review! VB Script for 4-position toolchanger.
« Reply #7 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   

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: VB Script for toolchanger
« Reply #8 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

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: VB Script for toolchanger
« Reply #9 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.