
 
 'CINCINNATI-750 MACH3 CNC SOFTWARE RETROFIT
   

'------------------------------------------------------------------------------------------------------------
Dim acttool As Integer
Dim newtool As Integer
Dim Slot As Integer					'Slot is OEMDRO(1220) this will keep position of carousel
Dim HomeSensor As Integer
Dim Count As Integer
Const MaxTool		= 24			'MaxTool number
'------------------------------------------------------------------------------------------------------------

Sub Main()
   
	Slot=GetOEMDro(1220)
 
	HomeSensor=(GetOEMTRIG10)
	Count=(GetOEMTRIG11)
 
	'let's do some basic check's
		'Get the new ATC Pos
		newtool = GetSelectedTool()		' get the newtool
		acttool = GetCurrentTool()		' get the acttool	
		Message "New ATC Position = " &newtool
		Sleep(200)

		' new and acttool = 0
		If ((newtool = 0) and (acttool = 0)) Then
			DoButton(1003)
			message("new and old tool = 0 -> abort toolchange  !")
			Sleep(500)
			Exit Sub
		End If	

		' new and acttool are the same
		If newtool = acttool Then
			message("acttool and newtool are the same -> do nothing!")
			Sleep(500)
			Exit Sub
		End If
 
		'check toolnumber in range
		If  (((newtool > MaxTool) Or (newtool < 1)) And (newtool <> 0)) Then
			DoButton(1003)
			Message (" toolnumber " & newtool & " is not in range (1-" &MaxTool &"). abort toolchange  ! ")
			MsgBox  (" toolnumber " & newtool & " is not in range (1-" &MaxTool &"). abort toolchange  ! ")
			Exit Sub
		End If		

 
  TIS1=GETOEMDRO(1201)       'TOOL IN SLOT
  TIS2=GetOemDro(1202)
  TIS3=GetOemDro(1203)
  TIS4=GetOemDro(1204)
  TIS5=GetOemDro(1205)
  TIS6=GetOemDro(1206)
  TIS7=GetOemDro(1207)
  TIS8=GetOemDro(1208)
  TIS9=GetOemDro(1209)
  TIS10=GetOemDro(1210)
  TIS11=GetOemDro(1211)
  TIS12=GetOemDro(1212)
  TIS13=GetOemDro(1213)
  TIS14=GetOemDro(1214)
  TIS15=GetOemDro(1215)
  TIS16=GetOemDro(1216)
  TIS17=GetOemDro(1217)
  TIS18=GetOemDro(1218)
  TIS19=GetOemDro(1219)
  TIS20=GetOemDro(1230)
  TIS21=GetOemDro(1231)
  TIS22=GetOemDro(1232)
  TIS23=GetOemDro(1233)
  TIS24=GetOemDro(1234)
 
	If NewTool=TIS1 Or NewTool=TIS2 Or NewTool=TIS3 Or NewTool=TIS4 Or NewTool=TIS5 Or NewTool=TIS6 _
	Or NewTool=TIS7 Or NewTool=TIS8 Or NewTool=TIS9 Or NewTool=TIS10 Or NewTool=TIS11 Or NewTool=TIS12 _
	Or NewTool=TIS13 Or NewTool=TIS14 Or NewTool=TIS15 Or NewTool=TIS16 Or Newtool=TIS17 Or NewTool=TIS18 _
	Or NewTool=TIS19 Or NewTool=TIS20 Or NewTool=TIS21 Or NewTool=TIS22 Or NewTool=TIS23 Or NewTool=TIS24 Then
   
		CODE "M19"
		GoToSafeZ()
		While IsMoving
		Wend
		Message "TOOL IN CAROUSEL"

		'only unload tool
		If newtool = 0 Then
			'turn carousel if not in Position
			If newtool <> Slot Then
				LeastTravel(newtool)
				SetOEMDro(1220,newtool)		
			End if
			'now unload tool
			UnloadSpindle
		End If	
	
		'only load tool
		If acttool = 0 Then
			'turn carousel if not in Position
			If atctool <> Slot Then
				LeastTravel(Slot)		
				SetOEMDro(1220,Slot)		
			End if
			'now load tool
			LoadSpindle
		End If	

		'full tool change
		If ((acttool <> 0) And (newtool <> 0)) Then
			'unload tool
			'turn carousel if not in Position
			If newtool <> Slot Then
				LeastTravel(newtool)		
				SetOEMDro(1220,newtool)		
			End if
			'now unload tool
			UnloadSpindle
			'load tool
			'turn carousel if not in Position
			If acttool <> Slot Then
				LeastTravel(Slot)		
				SetOEMDro(1220,Slot)		
			End if
			'now load tool
			LoadSpindle			
		End If	

		SetUserDro(1224,newtool)
		SetCurrentTool(newtool)
		Code "G43H" & newtool
		DeActivateSignal(Output17)
		While IsMoving
		Wend


	End If
 
End Sub

'''''Sequence to determine the least travel to move for TC, CW or CCW
'atp = Actual tool position, rtp = Requested tool positon
Function LeastTravel(ByVal ATCPos As Integer)

   NewPos = GetOEMDro(1220)
   OldPos = GetOEMDro(1224)
   

   NTool =24 ' Number of tools changer holds
   CWPos = NewPos - OldPos ' Assume a CW move
   If CWPos < 0 Then ' CWPos < 0 ==> rtp < atp
      CWPos = CWPos + NTool
   End If
   CCWPos = NTool - CWPos
   If CWPos < CCWPos Then
      Call CW(CWPos)
   Else
      Call CCW(CCWPos)
   End If
End Function

Function CW(ByVal Pos As Integer)'Rotation CW
	Dim x As Integer

	ActivateSignal(OUTPUT16)'UnLock Pin
	While IsActive(OEMTRIG12)
		Sleep (200)
	Wend
	ActivateSignal(OUTPUT15)'Turns Carousel CW
	For x=1 To Pos
      'While IsActive(OEMTRIG11)
         Sleep(500)
      'Wend
      While IsActive(OEMTRIG11)
         'Sleep(10)
      Wend
      
	Next x
        CTPos=x
	DeactivateSignal(OUTPUT16)
	Deactivatesignal(OUTPUT15)
	ActivateSignal(Output14)
	Sleep (300) 
	DeActivateSignal(Output14)
	SetCurrenttool(NewTool)
	SetUserDro(1220,NewTool)
End Function

Function CCW(ByVal Pos as Integer)'Rotation CCW
	Dim y As Integer
	ActivateSignal(Output16)'UnLock Pin
	While IsActive (OEMTRIG12)
		Sleep (200)
	Wend
	Activatesignal(OUTPUT14)'Turn Carousel CCW
	For y=1 To Pos
      'While IsActive(OEMTRIG11)
         Sleep(500)
      'Wend
      While IsActive(OEMTRIG11)
         'Sleep(10)
      Wend
	Next y
	CTPos=y
	DeactivateSignal(OUTPUT16)
	Deactivatesignal(OUTPUT14)
	ActivateSignal(Output15)
	Sleep (300)
	DeActivateSignal(Output15)
	SetCurrentTool(NewTool)
	SetUserDro(1220,NewTool)
   'SetUserDro(1224,NewTool)
End Function
 
 
 
Sub LoadSpindle()
       Sleep (1000)
       ActivateSignal(Output12)
	ActivateSignal(Output9)
	While IsActive(OEMTRIG3)=0
		Sleep (300)
	Wend
	ActivateSignal(Output10)
	DeActivateSignal(Output12)
	While IsActive(OEMTRIG5)=0
		Sleep (300)
	Wend
	DeActivateSignal(Output9)
	While IsActive(OEMTRIG2)=0
	Wend
	ActivateSignal(Output13)
	While IsActive(OEMTRIG9)=0
		Sleep (300)
	Wend
	Code "M7"
	ActivateSignal(Output9)
	While IsActive(OEMTRIG3)=0
	Wend
	ActivateSignal(Output11)
	While IsActive(OEMTRIG7)=0
		Sleep (300)
	Wend
	DeActivateSignal(Output10)
	While IsActive(OEMTRIG4)=0
		Sleep (500)
	Wend
	Code "M9"
	DeActivateSignal(Output11)
	While IsActive(OEMTRIG6)=0
		Sleep (500)
	Wend
	DeActivateSignal(Output9)
	While IsActive(OEMTRIG3) = 1
		Sleep (2000)
	Wend
	DeActivateSignal(Output13)
	Sleep (1000)
	ActivateSignal(Output12)
	While IsActive(OEMTRIG8)=0
	Wend
	DeActivateSignal(Output12)
	Message "Spindle loaded"
	Sleep(1000)
End Sub   

Sub UnloadSpindle()
       Sleep (1000)
	ActivateSignal (Output13)'move tool arm to spindle on
		While IsActive (OEMTRIG9)=0
		Sleep (300)
	Wend
	
	ActivateSignal (Output9)'extend tool arm
	While IsActive (OEMTRIG3)=0
		Sleep (300)
	Wend
	ActivateSignal (Output11)'PBD unlock
	While IsActive (OEMTRIG7)=0
		Sleep (300)
	Wend
	Code "M7"
	ActivateSignal (Output10)'tool arm down
	While IsActive (OEMTRIG5)=0
		Sleep (300)
	Wend
	DeActivateSignal(Output9)
	While IsActive(OEMTRIG2)=0
	Wend
	DeActivateSignal (Output13)'move tool arm to spindle off
        Sleep (100)
	ActivateSignal (Output12)'move tool arm to carousel on
	While IsActive (OEMTRIG8)=0
		Sleep (300)
	Wend
	Code "M9"
	ActivateSignal(Output9)
	While Isactive(OEMTRIG3)=0
	Wend
	DeActivateSignal (Output10)'tool arm up
	While IsActive (OEMTRIG4)=0
		Sleep (1000)
	Wend
	DeActivatesignal (Output9)'tool arm retract
	DeActivateSignal (Output12)'move tool arm to carousel off
	DeActivateSignal (Output11)'PBD lock
	Message "Spindle Is Empty"
	Sleep(1000)
End Sub   


  
  