Hello Guest it is October 31, 2024, 07:59:05 PM

Author Topic: Macro VB Script for Lathe Toolchanger  (Read 42125 times)

0 Members and 1 Guest are viewing this topic.

Offline CNCwt

*
  •  65 65
Macro VB Script for Lathe Toolchanger
« on: December 20, 2007, 09:43:26 AM »
Hi,

I need HELP on the Macro VB Scripting for the Lathe Toolchanger below.

This is a rather long description but hope it will help others as well doing the same type of VB Script.

I have a 4-position lathe toolchanger that uses 4 sensor switches to detect its position. I have made an electronic circuit to onvert the 4 positions (decimal) into 2-bit binary (00, 01, 10, 11) and then input these into the parallel port INPUT Pins. Here are the detailed description of the setup:

A)   Turret Tool Post & Relays:
a)   The Turret is a 4-position tool post driven by a 50W AC Motor.
b)   The AC Motor can be rotated CW or CCW controlled by 2 “ICE CUBE” Relays.
c)   The relays have 24VDC coils, Contacts of 3A/30VDC and 3A/240VAC.
d)   The 2 relays are interlatched.[/li]

B)   Tool Position Sensors:
a)   The 4 positions of the turret tool post are sensed by 4 installed hall-effect sensors or switches. The hall-effect sensors can be powered with 24VDC though I used 5VDC.

C)   Electronic Circuit and INPUT/OUTPUT controls using the parallel port:
a)   The electronic circuit was made feasible with the help of John Pearson (aka CJMerlin) without which, this will not be possible.
b)   The 4 hall-effect sensors (switches) are fed into an electronic circuit board and converted into 2-bit binary signals (Decimal Position 1, 2, 3, & 4 are converted into Binary 00, 01, 10, & 11).
c)   The 2-bit binary signals are inputted into 2 INPUTs of the Parallel Port (say, INPUT Pins #13 & #15) and set in Mach3 as INPUT2 & INPUT3.
d)   Mach3 is supposed to sense or monitor the 2 INPUTs and using Macro, Mach3 will know when a particular tool no. has just been sensed.
e)   Using an electronic circuit, 2 OUTPUTs of the Parallel Port are used to trigger the 2 relays of the tool post’s motor. One of the relay is to rotate the tool post CW and the other relay is to rotate the tool post CCW. Let’s say Parallel Port OUTPUT Pins # 6 & #7 and set in Mach3 as OUTPUT2 & OUTPUT3.
f)   Also, the above mentioned circuit interfaced the 5VDC TTL of the parallel port OUTPUT to 24VDC of the coil.

D)   How the auto turret tool post works:
a)   Let’s make the assumption that Tool # 3 is to be used.
b)   The GCode will specify T0301 (Txxyy where xx is the tool no.and yy is the tool offset) or this can be put in manually in MDI.
c)   A Macro will then monitor what is the present tool # in position. This function will be that of Mach3 reading the INPUT  # 2 & 3 to know which tool # is in position.
d)   If the correct tool # (Tool # 3) is in position, no further action will be taken and the rest of the GCodes are executed.
e)   If the required tool # is not in position, thru the Macro, Mach3 will send out a logical high signal pulse to OUTPUT2 and triggers RELAY #1 to raise and turn the turret CW.
f)   By so doing, the tool post will rotate until such a time that Mach3 senses thru the INPUT2 & INPUT3 that the required tool # has just rotated past the sensor.
g)   When Mach3 sensed that the required tool # has just rotated into position, it will send thru OUTPUT2 a logical low and thus stop the tool post’s CW rotation. It will next send a logical high thru OUTPUT3 to trigger RELAY #2 and thus rotate the tool post CCW and lock the tool post by lowering it into position.
h)   Mach3 will next send, thru Macro, a logical low to OUPUT3 to stop RELAY #2 and thus stop the motor of the tool post (say, within 1 second).

E)   The DECIMAL to BINARY relationship and proposed Macro equation to identify the tool position are as follows:
a)   The DECIMAL to BINARY is based on the following:
Tool Position No.       2-bit Binary
(Decimal)   (INPUT2)   (INPUT3)
                       B                    A
1                      0                   0
2                      0                   1
3                      1                   0
4                      1                   1

Below are the pictures of the Lathe and tool changer turret I am working on:
« Last Edit: December 23, 2007, 12:13:04 AM by CNCwt »

Offline CNCwt

*
  •  65 65
Re: Macro VB Script for Lathe Toolchanger
« Reply #1 on: December 20, 2007, 09:47:49 AM »
I have made the following Macro M6Start.m1s by studying the various Macro VB scripts posted in this forum as well as the help of numerous people here) :

'(NOTE: This Tool changer Macro is not working yet due to some problems I have with the sensor 2-bit binary electronic circuit I made. I will update this one soon with a working Macro but using 4 inputs instead of 2.)

'M6Start.m1s
'Input2 is 1st binary bit, 'Input3 is 2nd binary bit
'Output2 triggers 1st relay that rotates turret CW
'Output3 triggers 2nd relay that rotates turret CCW and lock tool in position

Tool = GetSelectedTool()
OldTool = GetCurrentTool()
NewTool = Tool
MaxToolNum = 4      'Max number of tools for the changer

While NewTool > MaxToolNum
NewTool = Question ("Enter New Tool Number up to " & MaxToolNum)
Wend

Call StartTool

While SelectedTool <> NewTool
Call CheckPins
Wend

SelectedTool = NewTool

Call StopTool

SetCurrentTool(NewTool )

'//// Subroutines /////////

Sub StartTool
   ActivateSignal(Output2)
   'Code "G4 P4.0"    'Wait for the tool to rotate past the sensor
   While Ismoving()
   Wend
End Sub


Sub CheckPins
   If Not IsActive(Input3) And Not IsActive(Input2) Then
      NewTool = 1
      End If
   If Not IsActive(Input3) And IsActive(Input2) Then
      NewTool = 2
      End If
   If IsActive(Input3) And Not IsActive(Input2) Then
      NewTool = 3
      End If
   If IsActive(Input3) And IsActive(Input2) Then
      NewTool = 4
      End If
End Sub

Sub Stoptool
   DeActivateSignal(Output2)
   ActivateSignal(Output3) 
   'Code "G4 P1.0"    'Wait for the tool to rotate onto ratchet stop
   While Ismoving()
   Wend
   DeActivateSignal(Output3) 
End Sub
« Last Edit: December 22, 2007, 09:00:19 PM by CNCwt »

Offline CNCwt

*
  •  65 65
Re: Macro VB Script for Lathe Toolchanger
« Reply #2 on: December 20, 2007, 10:00:37 AM »
I made a simple test Brain to see if the INPUT2 & INPUT3 are being sensed and compared with the SelectedTool.

The test shows that upon using Txxyy in MDI, the Brain shows they are being equal and thus should trigger the Relay#2 to stop the turret turning CW BUT... the Macro is not working.

I attached the test Brain here.

Offline CNCwt

*
  •  65 65
Re: Macro VB Script for Lathe Toolchanger
« Reply #3 on: December 20, 2007, 10:01:53 AM »
Can someone take a look and point out where I may have committed the mistakes in the Macro?

Thanks,
Weedy

Offline CNCwt

*
  •  65 65
Re: Macro VB Script for Lathe Toolchanger
« Reply #4 on: December 23, 2007, 12:34:03 AM »
After analyzing my problem with the Tool Changer Macro, I suspect that my electronic circuit that converts the 4 decimal positions to 2-bit binary is not latching fast enough for the Input2 & Input3 to correctly monitor the tool position. This is partly due to its being on a breadboard and I decided to temporarily shelf it.

Instead, I connected directly the 4 sensor/switches to the BOB and used "Input1, Input2, Input3, & Input4" in Mach3 that correspond to the parallel port's Input Pins 11, 12, 13, & 15. My weakest in VB scripting and not enough knowledge of electronics took me some time to realize that the sensors are sinking current (I am not even sure if this is the correct term :)) and thus, the one that is not lighting up in the BOB as well as in the Active signals in the diagnostic page is the active position.

Eventually, I got it to work nice and easy. The turret takes less than a second to a max of 2 seconds to rotate the requested tool into position. It never takes more than 1 complete revolution of the turret to put the requested tool into position.

Here is the working Toolchanger Macro:

'This Macro was based on the 4 position sensors being fed directly into 4 INPUTs
'to the parallel port.
'Input1 is Tool1 (Pin11, Active High), Input2 is Tool2 (Pin12, Active High),
'Input3 is Tool3 (Pin13, Active High), Input4 is Tool4 (Pin15, Active High).
'Since Sensors are sinking current, hence all set to Active High.
'Output2 triggers 1st relay that raises and rotates the turret CW,
'Output3 triggers 2nd relay that rotates turret CCW and lower the turret to lock tool in position.
'OEMDRO(1200) is utilized to store the last used tool (OldTool) and is updated
'upon final execution of the macro. This is resorted to as the Txxyy will overwrite
'the "CurrentTool" DRO.


'M6Start.m1s
OldTool = GetOEMDRO (1200)
Tool = GetSelectedTool()
MaxToolNum = 4      'Max number of tools for the changer

If OldTool = Tool Then
Message ("Selected Tool already loaded")
Exit Sub
End If

While Tool > MaxToolNum
Tool = Question ("Enter New Tool Number up to " & MaxToolNum)
Wend

Call StartTool

While NewTool <> Tool
   Call CheckPins
   While IsMoving()
   Wend
Wend

Call StopTool

Call SetUserDRO (1200, NewTool)
SetCurrentTool(NewTool)

'//// Subroutines /////////

Sub StartTool
   ActivateSignal(Output2)
   'Code "G4 P4.0"    'Wait for the tool to rotate past the sensor
      While IsMoving()
      Wend
End Sub

Sub CheckPins
   If Not IsActive(Input1) Then
      NewTool = 1
      End If
   If Not IsActive(Input2) Then
      NewTool = 2
      End If
   If Not IsActive(Input3) Then
      NewTool = 3
      End If
   If Not IsActive(Input4) Then
      NewTool = 4
      End If
End Sub

Sub Stoptool
   DeActivateSignal(Output2)
      While IsMoving()
      Wend
   ActivateSignal(Output3) 
      Code "G4 P1.0"    'Wait for the tool to rotate onto ratchet stop
      While IsMoving()
      Wend
   DeActivateSignal(Output3)
      While IsMoving()
      Wend
End Sub   





« Last Edit: December 23, 2007, 12:42:22 AM by CNCwt »

Offline CNCwt

*
  •  65 65
Re: Macro VB Script for Lathe Toolchanger
« Reply #5 on: December 23, 2007, 07:08:07 AM »
Below is a snapshot of the OEMDRO(1200) put inside the Mach3 Turn Diagnostic page (encircled in red).
Re: Macro VB Script for Lathe Toolchanger
« Reply #6 on: December 23, 2007, 08:25:34 AM »
Hello Weedy,

I have the same type of ATC, but works with separate sensors of each tool. This Macro definitely works ( parts of code stolen from Brian Barker  ;) ,Comments are unfortunately in German):

'Werkzeugwechslermakro f. Trolcut430, Helmut Heigl, 07/12/21
Sub Main()
OldTool = GetOEMDRO (1200) 'Aktuelles Werkzeug
x = GetToolChangeStart( 0 )'Abfrage Position "X" vor Werkzeugwechsel   
z = GetToolChangeStart( 2 )'Abfrage Position "Z" vor Werkzeugwechsel   
tool = GetSelectedTool()'Auswahl neues Werkzeug   
NewTool = tool
MaxToolNum = 4    'Anzahl Werkzeuge
ToolFreistX = 40   'Freistellwert "X"
ToolFreistZ = 80   'Freistellwert "Z"          

If NewTool = OldTool Then
   Exit Sub
End If
While NewTool > MaxToolNum
NewTool = Question ("Bitte andere Werkzeugnummer eingeben: 1-" & MaxToolNum)
Wend
Code "G00 G53 X" & ToolFreistX & " Z" & ToolFreistZ 'Werkzeug freistellen (über Maschinenkoordinaten!!!)
While IsMoving()
Wend
ActivateSignal(Output1) 'Werkzeughalter drehen "EIN"
Call MovePos(NewTool)
While IsMoving()
Wend
DeActivateSignal(Output1) 'Werkzeughalter drehen "AUS"
Code "G4 P.5" 'Kleine "Weichei"- Zeitverzoegerung, kann eventuell weggelassen werden
While IsMoving()
Wend
ActivateSignal(Output2) 'Wendeschuetz für Halterklemmung "EIN"
Code "G4 P0.5"    'Warten auf Klemmung
While IsMoving()
Wend
DeActivateSignal(Output2) 'Wendeschuetz für Halterklemmung "AUS"
Call SetUserDRO (1200,NewTool)
SetCurrentTool( NewTool )'neues Werkzeug anzeigen
Code "G00 X" & x & " Z" & z 'neues Werkzeug in Arbeitsposition fahren
While IsMoving()
Wend
End Sub

Sub MovePos(ByVal ToolNumber As Integer)



Select Case ToolNumber

       Case Is =1
       ToolNumber=1
           While Not IsActive (Input1) 'Abfrage Werkzeugsensor 1
         Wend
       
       Case Is = 2
       ToolNumber=2
         While Not IsActive (Input2) 'Abfrage Werkzeugsensor 2
         Wend
     
       Case Is = 3
       ToolNumber=3
         While Not IsActive (Input3) 'Abfrage Werkzeugsensor 3
         Wend

       Case Is = 4
       ToolNumber=4
         While Not IsActive (Input4) 'Abfrage Werkzeugsensor 4
         Wend
         
       
End Select

End Sub
           




Greetings,
Helmut
In theory, there is no difference between theory and practice. In practice there is.

Offline CNCwt

*
  •  65 65
Re: Macro VB Script for Lathe Toolchanger
« Reply #7 on: December 23, 2007, 10:59:14 AM »
Hi Helmut,

Thanks for posting your working toolchanger macro. Even though my German is none existing at all, I can guess on the comments' meanings :).

In fact, your macro is almost the same working toolchanger macro I posted (2 posts earlier than your posting). We both utilized the system of Brian Barker by creating an OEMDRO(1200) to have an OldTool DRO so that it will not be overwritten by Txxyy. And both these macros need to use 4 inputs to Mach.

I am still working on changing the 4 sensor positions to 2-bit binary so that I will only need 2 Inputs. This will allow me to use only 1 parallel port instead of 2. My electronic circuit that converts the decimal 4 positions to 2-bit binary is not functioning fast enough to latch the last tool position signal and this creates a situation wherein in between the sensor signals, the 4th tool position (binary 11 is the signal that comes out when it does not latch) is being sent to the Inputs.

I have made a test macro to identify the tool position signals and it identifies the correct tool position. I have made another toolchanger macro for the 2-bit binary position and I know it works except for a particular situation. That is, when the existing tool position is T3, and the requested tool is T4, then the turret will not rotate as the signal that is not latching is 11 which translates to T4. After which, any other subsequent toolchange will be in error. This situation has confirmed my analysis that my circuit is not latching fast enough.

If you are interested to know how I am doing the electronics and the macro for 2 Inputs only, let me know and I can post the details. In fact, without the mentioned circuit problem, I could have make this macro work with a brain that handles the timing as well as the NewTool and OldTool comparison (Note: John Pearson a.k.a. CJMerlin is kind enough to be behind the scene in helping me sort out the electronics as well as the brain solutions though any mistakes or problems is entirely mine as John has no way of knowing how I am implementing it. Thanks John :))

Kind regards,
Weedy




Offline Graham Waterworth

*
  • *
  •  2,731 2,731
  • Yorkshire Dales, England
Re: Macro VB Script for Lathe Toolchanger
« Reply #8 on: December 24, 2007, 05:15:05 AM »
I have always found it better with mach to assign values to pins and check the count, like this :-

Sub CheckPins
   count=0
   If IsActive(Input1) Then
      count=count+1
   End If
   If IsActive(Input2) Then
      count=count + 2
   End If
End Sub

You then have your 4 numbers, 0,1,2,3

Graham.
Without engineers the world stops

Offline CNCwt

*
  •  65 65
Re: Macro VB Script for Lathe Toolchanger
« Reply #9 on: December 24, 2007, 05:54:00 AM »
Hi Graham,

Thanks for that bit of tip. It's nice and clean.

So, all I have to do to have my NewTool correctly is:

Sub CheckPins
   count=0
   If IsActive(Input1) Then
      count=count+1
   End If
   If IsActive(Input2) Then
      count=count + 2
   End If
NewTool = count + 1
End Sub

Right?

Thanks,
Weedy