'!TITLE "ACube_SampleCode"

' Author: Asyril
' This program shows the typical commands to pilot a Asycube. 
' The provider works exclusively with TCP/IP communication. For the Asycube models that integrate only a RS232/485 interface, 
' a TCP/IP <-> RS232/485 converter is required
' ********************************************************************************************************
' The Asycube is not configured via the script and must be tuned for vibration settings using the Asyril's user interface .
' ********************************************************************************************************

Sub Main 
	
 	Dim feeder as Object
    On Error Goto ErrorMgmt

	' Creating  an instance of the Asycube controller
	' Creating a feeder object for a feeder that integrates TCP/IP

    feeder = cao.AddController("Feeder1", "CaoProv.Asyril.Asycube", "", "conn=TCP:192.168.127.254:4001,Timeout=10000")

	Dim partNbr as Integer = 0
	Dim feedLimit as Integer = 5
	Dim partLocZone as Integer = 3
	Dim mvCenterDuration as Integer = 100

	'************************************************************************
	' Insert here the dialog with vision to get the number and localization of parts
	' partNbr = VisionSystem.QueryPart(...)
	' partLocZone = VisionSystem.QueryPartLocZone(...)
	' mvCenterDuration = VisionSystem.QueryCenterDistanceDelay(...)
	' To synchronize the backlight with the camera use the "BackLight.Flash" command
	' call feeder.Execute("!Backlight.Flash","") or use the digital input "synchroBackLight"
	'************************************************************************
	
	'Feeding mort part if necessary
	If partNbr < feedLimit Then
		call feeder.Execute("Feed.Forward",1000)
	End if

	' Spreading parts ont the platform by choosing the appropriate vibrations depending on the part mean localization zone
	
	Select Case partLocZone
		Case 1
			call feeder.Execute("Move.ForwardRight",mvCenterDuration)
		Case 2
			call feeder.Execute("Move.Right",mvCenterDuration)
		Case 3
			call feeder.Execute("Move.BackwardRight",mvCenterDuration)
		Case 4
			call feeder.Execute("Move.Forward",mvCenterDuration)
		Case 5
			'no vibration
		Case 6
			call feeder.Execute("Move.Backward",mvCenterDuration)
		Case 7
			call feeder.Execute("Move.ForwardLeft",mvCenterDuration)
		Case 8
			call feeder.Execute("Move.Left",mvCenterDuration)
		Case 9
			call feeder.Execute("BackwardLeft", mvCenterDuration)
	End Select
	
	'Spreading the part by doing the flip

	call feeder.Execute("Move.Flip",300) 
	
	'Disconnect
	Disconnect :
	cao.Controllers.Remove feeder.Index
	feeder = Nothing
	Exit Sub

	ErrorMgmt:
	' Treat the errors here
	PrintMsg "Error..."
	Resume Disconnect

End Sub