Hello Guest it is March 28, 2024, 06:14:59 AM

Author Topic: Macro for Rotary Tool Changer  (Read 6842 times)

0 Members and 1 Guest are viewing this topic.

Macro for Rotary Tool Changer
« on: November 05, 2018, 04:22:53 AM »
Hello there, I need your help.

8 tools, BCD encoder (4bit) - CSmio inputs 14, 13, 12, 11
Geneva Cam sensor (one impulse per rotation of the cam) - CSmio input 10
Clutch/Brake operated by one relay - CSmio output 2
Three-phase tool changer motor - CSmio output 2

In order to make a tool change:
1. Activate - CSmio output 2
2. Wait for correct BCD - CSmio inputs 14, 13, 12, 11
3. When desired BCD achieved wait for Geneva Cam sensor - CSmio input 10
4. DeActivate - CSmio output 2

I modified sample macro from CS-Lab to suit my setup. However, I don't know VB good enough to make it work properly.
Now it stops on random tool station, sometimes even in between. It completely ignores the cam sensor.
I'd appreciate any help.

Code: [Select]
'------------------------------------------------------------------------------
' CS-Lab s.c. - Auto toolchanger for lathe (rotary type of ATC)
' (C) 2011
' v1.2 - 2011-11-05
'------------------------------------------------------------------------------
' CONFIG
'------------------------------------------------------------------------------
Const TOOLCOUNT = 8 ' max Tool #
' Actual ATC position inputs (CSMIO-IP inputs)
Const InT1 = 14
Const InT2 = 13
Const InT3 = 14 And 13
Const InT4 = 12
Const InT5 = 14 And 12
Const InT6 = 13 And 12
Const InT7 = 14 And 13 And 12
Const InT8 = 11
' ATC sensors inputs
Const GenevaSensor = 10 ' Geneva Cam on position
' digital outputs for ATC control (CSMIO outputs)
Const OutRun = 2 ' unlock and rotate
'------------------------------------------------------------------------------
' program variables - don't modify
'------------------------------------------------------------------------------
Dim ToolNew, ToolOld, ToolSlot As Integer
Dim ToolRdy As Boolean
Dim ATC_CloseRetry
Dim InToolSens(TOOLCOUNT)
'------------------------------------------------------------------------------
  ' MACRO START
'------------------------------------------------------------------------------
if(GetOEMLed(800)) Then ' MACH is at RESET condition
Message("System is at ESTOP - Tool Change terminated")
Sleep(150)
WaitForMove
End
End If

  SetUserLED(1001, 1) ' User LED - toolchange in progress (Pause Feed Hold condition)
 
  InToolSens(0) = InT1
  InToolSens(1) = InT2
  InToolSens(2) = InT3
  InToolSens(3) = InT4
  InToolSens(4) = InT5
  InToolSens(5) = InT6
  InToolSens(6) = InT7
  InToolSens(7) = InT8

' Get actual tool # and new tool #
ToolOld = GetCurrentTool()
ToolNew = GetSelectedTool()

' little trick for double slots ATC
If(ToolNew >= 10) Then
ToolSlot = ToolNew - 10
Else
ToolSlot = ToolNew
End If

If(ToolOld >= 10) Then
ToolOld = ToolOld - 10
End If

' The same position - no need to move
If (ToolSlot = ToolOld) Then
SetCurrentTool( ToolNew )
Message("ATC is on position "& ToolSlot & " and new tool is " & ToolNew)
WaitForMove
SetUserLED(1001, 0) ' Pause Feed Hold off
End
End If

If (ToolSlot > TOOLCOUNT) Then
SetCurrentTool( ToolOld )
SetUserLED(1001, 0) ' Pause Feed Holf off
DoOEMButton(1003) ' Stop
DoOEMButton(1002) ' G-Code Rewind
Sleep(150)
Message("There is no tool T" & ToolNew & " in ATC")
Sleep(150)
End
End If


' T0 is a fake tool, no need to change
If (ToolSlot = 0) Then
ToolNew = 0
SetCurrentTool( ToolNew )
Message("Succesful loaded tool T" & ToolNew)
WaitForMove
SetUserLED(1001, 0) ' Pause Feed Hold off
End
End If


' unlock and rotate ATC
RunATC
Sleep(150)
' wait to ATC will be on the desired position
ToolRdy = false
While(Not ToolRdy)
If(GetCsmioIn((InToolSens(ToolSlot - 1)))) Then
ToolRdy = true
End If
Wend


' stop rotation and lock ATC
ATC_CloseRetry = 0
' Check lock sensor, retry lock if needed
While(Not GetCsmioIn(GenevaSensor) And ATC_CloseRetry < 4)
StopATC
ATC_CloseRetry = ATC_CloseRetry + 1
Wend

' double check for succesful ATC lock
If (Not GetCsmioIn(GenevaSensor)) Then
SetCurrentTool( 0 )
SetUserLED(1001, 0) ' Pause Feed Hold off
' DoOEMButton(1021) ' Reset
DoOEMButton(1002) ' G-Code Rewind
Sleep(150)
Message("ATC lock alert. Check ATC.")
Sleep(150)
End
End If
 
SetCurrentTool( ToolNew )
Message("Succesful loaded tool T" & ToolNew)
Sleep(150)
WaitForMove
SetUserLED(1001, 0) ' Pause Feed Hold off
End
' -----------------------------------------------------------------------------
Public Function GetCsmioIn (n As Integer) As Boolean
Dim reg As Integer

If(n < 16) Then
reg = 90
Else
reg = 91
n = n - 16
End If

If(GetInBit(reg, n)) Then
GetCsmioIn = true
Else
GetCsmioIn = false
End If
Exit Function
End Function
' -----------------------------------------------------------------------------
Sub RunATC ()
Call SetCsmioOut(OutRun, true)
Sleep(150)
End Sub
' -----------------------------------------------------------------------------
Sub StopATC ()
Call SetCsmioOut(OutRun, false)
Sleep(150)
End Sub
' -----------------------------------------------------------------------------
Public Sub SetCsmioOut (ByVal n As Integer, ByVal state As Boolean)
If(state) Then
SetOutBit(90, n)
Else
ResetOutBit(90, n)
End If
End Sub
' -----------------------------------------------------------------------------
Sub WaitForMove ()
While IsMoving()
Sleep(15)
Wend
End Sub                                                       

Offline TPS

*
  •  2,501 2,501
    • View Profile
Re: Macro for Rotary Tool Changer
« Reply #1 on: November 06, 2018, 03:11:18 PM »
the original macro was written for a 8Bit binary toolpos Input, not for BCD Input

something like this might work

Code: [Select]
'------------------------------------------------------------------------------
' CS-Lab s.c. - Auto toolchanger for lathe (rotary type of ATC)
' (C) 2011
' v1.2 - 2011-11-05
' v1.3 - 2018-11-06 TPS modified for BCD coded toolpos
'------------------------------------------------------------------------------
' CONFIG
'------------------------------------------------------------------------------
Const TOOLCOUNT = 8 ' max Tool #
' Actual ATC position inputs (CSMIO-IP inputs)
Const BCD1 = 14
Const BCD2 = 13
Const BCD4 = 12
Const BCD8 = 11

' ATC sensors inputs
Const GenevaSensor = 10 ' Geneva Cam on position
' digital outputs for ATC control (CSMIO outputs)
Const OutRun = 2 ' unlock and rotate
'------------------------------------------------------------------------------
' program variables - don't modify
'------------------------------------------------------------------------------
Dim ToolNew, ToolOld, ToolSlot As Integer
Dim ToolRdy As Boolean
Dim ATC_CloseRetry
Dim ActATCPos
'------------------------------------------------------------------------------
  ' MACRO START
'------------------------------------------------------------------------------
if(GetOEMLed(800)) Then ' MACH is at RESET condition
Message("System is at ESTOP - Tool Change terminated")
Sleep(150)
WaitForMove
End
End If

  SetUserLED(1001, 1) ' User LED - toolchange in progress (Pause Feed Hold condition)
 

' Get actual tool # and new tool #
ToolOld = GetCurrentTool()
ToolNew = GetSelectedTool()

' little trick for double slots ATC
If(ToolNew >= 10) Then
ToolSlot = ToolNew - 10
Else
ToolSlot = ToolNew
End If

If(ToolOld >= 10) Then
ToolOld = ToolOld - 10
End If

' The same position - no need to move
If (ToolSlot = ToolOld) Then
SetCurrentTool( ToolNew )
Message("ATC is on position "& ToolSlot & " and new tool is " & ToolNew)
WaitForMove
SetUserLED(1001, 0) ' Pause Feed Hold off
End
End If

If (ToolSlot > TOOLCOUNT) Then
SetCurrentTool( ToolOld )
SetUserLED(1001, 0) ' Pause Feed Holf off
DoOEMButton(1003) ' Stop
DoOEMButton(1002) ' G-Code Rewind
Sleep(150)
Message("There is no tool T" & ToolNew & " in ATC")
Sleep(150)
End
End If


' T0 is a fake tool, no need to change
If (ToolSlot = 0) Then
ToolNew = 0
SetCurrentTool( ToolNew )
Message("Succesful loaded tool T" & ToolNew)
WaitForMove
SetUserLED(1001, 0) ' Pause Feed Hold off
End
End If


' unlock and rotate ATC
RunATC
Sleep(150)
' wait to ATC will be on the desired position
ToolRdy = false
While(Not ToolRdy)
'read the BCD code
ActATCPos = 0
If GetCsmioIn(BCD1) Then
ActATCPos = ActATCPos + 1
End If
If GetCsmioIn(BCD2) Then
ActATCPos = ActATCPos + 2
End If
If GetCsmioIn(BCD4) Then
ActATCPos = ActATCPos + 4
End If
If GetCsmioIn(BCD8) Then
ActATCPos = ActATCPos + 8
End If

If (ActATCPos = (ToolSlot - 1)) Then
ToolRdy = true
End If
Wend


' stop rotation and lock ATC
ATC_CloseRetry = 0
' Check lock sensor, retry lock if needed
While(Not GetCsmioIn(GenevaSensor) And ATC_CloseRetry < 4)
StopATC
ATC_CloseRetry = ATC_CloseRetry + 1
Wend

' double check for succesful ATC lock
If (Not GetCsmioIn(GenevaSensor)) Then
SetCurrentTool( 0 )
SetUserLED(1001, 0) ' Pause Feed Hold off
' DoOEMButton(1021) ' Reset
DoOEMButton(1002) ' G-Code Rewind
Sleep(150)
Message("ATC lock alert. Check ATC.")
Sleep(150)
End
End If
 
SetCurrentTool( ToolNew )
Message("Succesful loaded tool T" & ToolNew)
Sleep(150)
WaitForMove
SetUserLED(1001, 0) ' Pause Feed Hold off
End
' -----------------------------------------------------------------------------
Public Function GetCsmioIn (n As Integer) As Boolean
Dim reg As Integer

If(n < 16) Then
reg = 90
Else
reg = 91
n = n - 16
End If

If(GetInBit(reg, n)) Then
GetCsmioIn = true
Else
GetCsmioIn = false
End If
Exit Function
End Function
' -----------------------------------------------------------------------------
Sub RunATC ()
Call SetCsmioOut(OutRun, true)
Sleep(150)
End Sub
' -----------------------------------------------------------------------------
Sub StopATC ()
Call SetCsmioOut(OutRun, false)
Sleep(150)
End Sub
' -----------------------------------------------------------------------------
Public Sub SetCsmioOut (ByVal n As Integer, ByVal state As Boolean)
If(state) Then
SetOutBit(90, n)
Else
ResetOutBit(90, n)
End If
End Sub
' -----------------------------------------------------------------------------
Sub WaitForMove ()
While IsMoving()
Sleep(15)
Wend
End Sub         

anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: Macro for Rotary Tool Changer
« Reply #2 on: November 06, 2018, 05:12:58 PM »
Thanks for quick response. I'll try part of the code you published tomorrow.
Yesterday I changed part of my code to:
Code: [Select]
Const InT1 = 14
Const InT2 = 13
Const InT3 = 12
Const InT4 = 11
Because I guessed that this part might give a problem and yes, the code start to work a bit different (of course the tool numbers were not correct, but at least I could check the rest of the code).

I found another issue with this part:
Code: [Select]
' stop rotation and lock ATC
ATC_CloseRetry = 0
' Check lock sensor, retry lock if needed
While(Not GetCsmioIn(GenevaSensor) And ATC_CloseRetry < 4)
StopATC
ATC_CloseRetry = ATC_CloseRetry + 1
Wend
I figure out, that due to ATC design difference this part of the code won't work properly on my machine.
I changed it to:
Code: [Select]
' stop rotation and lock ATC ver2
CamRdy = false
While(Not CamRdy)
If(GetCsmioIn(GenevaSensor)) Then
StopATC
CamRdy = true
End If
Wend
It works in the right way, but there is a lag of about 150ms between GenevaSensor=true and relay off (checked with oscilloscope attached to Csmio inputs/outputs), so the geneva gear/cam stops about 60deg after the right position.
I don't know where is this lag coming from.

I also can't find any documentation about Csmio macro functions like GetCsmioIn, SetCsmioOut and stuff like that.

Offline TPS

*
  •  2,501 2,501
    • View Profile
Re: Macro for Rotary Tool Changer
« Reply #3 on: November 07, 2018, 02:45:23 AM »
I also can't find any documentation about Csmio macro functions like GetCsmioIn, SetCsmioOut and stuff like that.


These are only subroutines, witch are at the bottom of your macro.
they use Standard VB Code, only to make the Access to the CSMIO In-/Outputs easier.
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: Macro for Rotary Tool Changer
« Reply #4 on: December 08, 2018, 05:17:49 PM »
Code: [Select]
ToolRdy = false
While(Not ToolRdy)
'read the BCD code
ActATCPos = 0
If GetCsmioIn(BCD1) Then
ActATCPos = ActATCPos + 1
End If
If GetCsmioIn(BCD2) Then
ActATCPos = ActATCPos + 2
End If
If GetCsmioIn(BCD4) Then
ActATCPos = ActATCPos + 4
End If
If GetCsmioIn(BCD8) Then
ActATCPos = ActATCPos + 8
End If

If (ActATCPos = (ToolSlot - 1)) Then
ToolRdy = true
End If
Wend

This code doesn't work properly while the turret is rotating.
It works perfectly when it's stationary.

Offline TPS

*
  •  2,501 2,501
    • View Profile
Re: Macro for Rotary Tool Changer
« Reply #5 on: December 09, 2018, 04:28:02 AM »
ok i think the Problem is that the Inputs are not updated synchron.

try this code:

Code: [Select]
ToolRdy = false
While(Not ToolRdy)
If GetCsmioIn(BCD1) or GetCsmioIn(BCD2) or GetCsmioIn(BCD4) or GetCsmioIn(BCD8) then
'give the inputs a chance to update
Sleep(150)

'then read the BCD code
ActATCPos = 0
If GetCsmioIn(BCD1) Then
ActATCPos = ActATCPos + 1
End If
If GetCsmioIn(BCD2) Then
ActATCPos = ActATCPos + 2
End If
If GetCsmioIn(BCD4) Then
ActATCPos = ActATCPos + 4
End If
If GetCsmioIn(BCD8) Then
ActATCPos = ActATCPos + 8
End If

If (ActATCPos = (ToolSlot - 1)) Then
ToolRdy = true
End If
End If
Wend

anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: Macro for Rotary Tool Changer
« Reply #6 on: December 09, 2018, 12:39:28 PM »
TPS, thanks for help.
This is not final, but it's working well enough to go further with this machine :-)

Code: [Select]
'------------------------------------------------------------------------------
' CS-Lab s.c. - Auto toolchanger for lathe (rotary type of ATC)
' (C) 2011
' v1.2 - 2011-11-05
' v1.3 - 2018-11-06 TPS modified for BCD coded toolpos
' v1.4 - 2018-12-01 lion_rapid modified for CW/CCW rotation of ATC
' v1.41- 2018-12-03 lion_rapid removed "little trick for double slots"
' v1.5 - 2018-12-09 TPS modified for stable BCD decoding
' v1.6 - 2018-12-09 lion_rapid added Public sub"toolchange" and other changes
'------------------------------------------------------------------------------
' CONFIG
'------------------------------------------------------------------------------
Const TOOLCOUNT = 8 ' max Tool #
' Actual ATC position inputs (CSMIO-IP inputs)
Const BCD1 = 14
Const BCD2 = 13
Const BCD4 = 12
Const BCD8 = 11
' ATC sensors inputs
Const GenevaSensor =  10 ' Geneva Cam on position
' digital outputs for ATC control (CSMIO outputs)
Const Brake =  4 ' clutch / brake switch
Const OutRunCW =  5 ' unlock and rotate clockwise
Const OutRunCCW =  6 ' unlock and rotate counter clockwise
'------------------------------------------------------------------------------
' program variables - don't modify
'------------------------------------------------------------------------------
Dim ToolNew, ToolOld, CWturn, CCWturn, ActATCPos As Integer
Dim ToolRdy As Boolean
'------------------------------------------------------------------------------
  ' MACRO START
'------------------------------------------------------------------------------
If(GetOEMLed(800)) Then ' MACH is at RESET condition
Message("System is at ESTOP - Tool Change terminated")
Sleep(150)
WaitForMove
End
End If

  SetUserLED(1001, 1) ' User LED - toolchange in progress (Pause Feed Hold condition)

' Get actual tool # and new tool #
ToolOld = GetCurrentTool()
ToolNew = GetSelectedTool()

' The same position - no need to move
If (ToolNew = ToolOld) Then
SetCurrentTool( ToolNew )
Message("ATC is on position T "& ToolNew)
WaitForMove
SetUserLED(1001, 0) ' Pause Feed Hold off
End
End If

If (ToolNew > TOOLCOUNT) Then
SetCurrentTool( ToolOld )
SetUserLED(1001, 0) ' Pause Feed Holf off
DoOEMButton(1003) ' Stop
DoOEMButton(1002) ' G-Code Rewind
Sleep(150)
Message("Tool number too high (above 8)")
Sleep(150)
End
End If

' T0 is a fake tool, no need to change
If (ToolNew = 0) Then
ToolNew = 0
SetCurrentTool( ToolNew )
Message("Succesful loaded tool T" & ToolNew)
WaitForMove
SetUserLED(1001, 0) ' Pause Feed Hold off
End
End If

Call ToolChange

' check ATC position
If(GetCsmioIn(GenevaSensor)) Then
SetCurrentTool( ToolNew )
Message("Succesful loaded tool T" & ToolNew)
Sleep(150)
WaitForMove
SetUserLED(1001, 0) ' Pause Feed Hold off
Else
SetCurrentTool( 0 )
ToolOld = GetCurrentTool()
Message("New tool" & ToolNew)
Call ToolChange
End If

' double check for succesful ATC lock
' If (Not GetCsmioIn(GenevaSensor)) Then
' SetUserLED(1001, 0) ' Pause Feed Hold off
' DoOEMButton(1021) ' Reset
' DoOEMButton(1002) ' G-Code Rewind
' Sleep(150)
' Message("ATC lock alert. Check ATC.")
' Sleep(150)
' End
' End If
' -----------------------------------------------------------------------------
Public Function GetCsmioIn (n As Integer) As Boolean
Dim reg As Integer

If(n < 16) Then
reg = 90
Else
reg = 91
n = n - 16
End If

If(GetInBit(reg, n)) Then
GetCsmioIn = true
Else
GetCsmioIn = false
End If
Exit Function
End Function
' -----------------------------------------------------------------------------
Sub RunATCcw ()
Call SetCsmioOut(OutRunCW, true)
Call SetCsmioOut(Brake, true)
Sleep(150)
End Sub
' -----------------------------------------------------------------------------
Sub RunATCccw ()
Call SetCsmioOut(OutRunCCW, true)
Call SetCsmioOut(Brake, true)
Sleep(150)
End Sub
' -----------------------------------------------------------------------------
Sub StopATC ()
Call SetCsmioOut(Brake, false)
Call SetCsmioOut(OutRunCW, false)
Call SetCsmioOut(OutRunCCW, false)
Sleep(150)
End Sub
' -----------------------------------------------------------------------------
Public Sub SetCsmioOut (ByVal n As Integer, ByVal state As Boolean)
If(state) Then
SetOutBit(90, n)
Else
ResetOutBit(90, n)
End If
End Sub
' -----------------------------------------------------------------------------
Sub WaitForMove ()
While IsMoving()
Sleep(15)
Wend
End Sub                                                               
' -----------------------------------------------------------------------------
Public Sub ToolChange ()
CWturn = ToolNew - ToolOld
If CWturn < 0 Then
CWturn = CWturn + TOOLCOUNT
End If
CCWturn = TOOLCOUNT - CWturn
If CWturn < CCWturn Then
'unlock and rotate ATC
RunATCcw
Sleep(100)
'wait to ATC will be on the desired position
ToolRdy = false
While(Not ToolRdy)
If GetCsmioIn(BCD1) or GetCsmioIn(BCD2) or GetCsmioIn(BCD4) or GetCsmioIn(BCD8) then
'give the inputs a chance to update
Sleep(150)
'then read the BCD code
ActATCPos = 0
If GetCsmioIn(BCD1) Then
ActATCPos = ActATCPos + 1
End If
If GetCsmioIn(BCD2) Then
ActATCPos = ActATCPos + 2
End If
If GetCsmioIn(BCD4) Then
ActATCPos = ActATCPos + 4
End If
If GetCsmioIn(BCD8) Then
ActATCPos = ActATCPos + 8
End If

If (ActATCPos = ToolNew) Then
Sleep(160)
StopATC
ToolRdy = true
End If
End If
Wend
Else
'unlock and rotate ATC
RunATCccw
Sleep(100)
'wait to ATC will be on the desired position
ToolRdy = false
While(Not ToolRdy)
If GetCsmioIn(BCD1) or GetCsmioIn(BCD2) or GetCsmioIn(BCD4) or GetCsmioIn(BCD8) then
'give the inputs a chance to update
Sleep(150)
'then read the BCD code
ActATCPos = 0
If GetCsmioIn(BCD1) Then
ActATCPos = ActATCPos + 1
End If
If GetCsmioIn(BCD2) Then
ActATCPos = ActATCPos + 2
End If
If GetCsmioIn(BCD4) Then
ActATCPos = ActATCPos + 4
End If
If GetCsmioIn(BCD8) Then
ActATCPos = ActATCPos + 8
End If

If (ActATCPos = ToolNew) Then
Sleep(150)
StopATC
ToolRdy = true
End If
End If
Wend
End If
End Sub

Offline TPS

*
  •  2,501 2,501
    • View Profile
Re: Macro for Rotary Tool Changer
« Reply #7 on: December 10, 2018, 12:14:46 AM »
so waht are the "rest" Problems?
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: Macro for Rotary Tool Changer
« Reply #8 on: December 10, 2018, 02:40:23 AM »
Lag on CSMIO/Mach3 inputs doesn't allow me to stop the ATC on the cam signal (genevasensor).
The max current from the cam sensor doesn't allow to drive typical relay directly, so can't use your idea from another thread http://forum.machsupport.com/forum/index.php/topic,38681.0.html
So, I made a workaround, I'm switching the motor off after 160ms (CW) and 150ms (CCW) after the right tool has been detected.
After that only checking the cam signal.
This works for 99% of the time, I've added retry if that's not the case, but if something goes really wrong it will end up in an infinite loop.
I'll come back to the ATC later.

Offline TPS

*
  •  2,501 2,501
    • View Profile
Re: Macro for Rotary Tool Changer
« Reply #9 on: December 11, 2018, 01:32:33 PM »

Lag on CSMIO/Mach3 inputs doesn't allow me to stop the ATC on the cam signal (genevasensor).

made a Suggestion in the other thread

The max current from the cam sensor doesn't allow to drive typical relay directly, so can't use your idea from another thread

cam sensor -> optocoupler -> relay

So, I made a workaround, I'm switching the motor off after 160ms (CW) and 150ms (CCW) after the right tool has been detected.
After that only checking the cam signal.


you sayed it, it is a Workaround but not a solution. see cam sensor -> optocoupler -> relay -> there is allways a solution


This works for 99% of the time, I've added retry if that's not the case, but if something goes really wrong it will end up in an infinite loop.
I'll come back to the ATC later.


Workaround -> solution Problem again


 
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.