Option Strict Off
Option Explicit On
Imports ORiN2.interop.CAO

Friend Class Form1
	Inherits System.Windows.Forms.Form
    Dim caoEng As CaoEngine
    Dim caoWSs As CaoWorkspaces
    Dim caoWS As CaoWorkspace
    ' Robot objects
    Dim caoCtrl As CaoController ' Robot Controller
    Dim caoRobot As CaoRobot     ' for Robot Motion
    Dim caoVal As CaoVariable    ' for MachineRock SW
	
	Private Sub cmdInit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdInit.Click
        ' Create Engine
        caoEng = Nothing
        caoEng = New CaoEngine
        caoWSs = caoEng.Workspaces
        caoWS = caoWSs.Item(0)

        cmdInit.Enabled = False
        cmdConnectRobot.Enabled = True
	End Sub
	
    ' ---------------------------------------------------------------
    ' Connect with the Robot
    ' ---------------------------------------------------------------
    Private Sub cmdConnectRobot_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdConnectRobot.Click

        On Error GoTo ErrorProc

        ' Create object and connect with VRC
        caoCtrl = caoWS.AddController("Robot", "caoProv.DENSO.RC8", "", "Server=192.168.0.1")

        caoRobot = caoCtrl.AddRobot("VS")

        ' Valiable of status of Machine lock SW
        caoVal = caoCtrl.AddVariable("@LOCK")

        ' Turned on the Machine lock
        caoVal.Value = True

        ' Get armControl authority
        caoRobot.Execute("TakeArm", New Object() {0, 1})

        ' Start motoriThe motor cannot start when the machine lock is truej
        'caoRobot.Execute("Motor", New Object() {1, 0})

        ' Set the external movement speed of the robot
        caoRobot.Execute("ExtSpeed", New Object() {50, 25, 25})

        ' Enable command buttons
        cmdConnectRobot.Enabled = False
        cmdDisconnectRobot.Enabled = True
        cmdMove.Enabled = True
        cmdApproach.Enabled = True
        cmdDepart.Enabled = True
        cmdDraw.Enabled = True
        cmdDriveAEx.Enabled = True
        cmdDriveEx.Enabled = True
        cmdSpeed.Enabled = True
        cmdChangeTool.Enabled = True
        cmdChangeWork.Enabled = True

        Exit Sub

ErrorProc:
        cmdDisconnectRobot_Click(cmdDisconnectRobot, New System.EventArgs())
        MsgBox(Err.Description & " : " & Hex(Err.Number))

    End Sub
	
    ' ---------------------------------------------------------------
    ' disconnect from the Robot
    ' ---------------------------------------------------------------
    Private Sub cmdDisconnectRobot_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDisconnectRobot.Click

        On Error Resume Next

        If Not (caoVal Is Nothing) Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(caoVal)
            caoVal = Nothing
        End If

        ' Disconnect from the Robot
        If Not (caoRobot Is Nothing) Then

            If Not (caoCtrl Is Nothing) Then
                ' Stop motor
                'caoRobot.Execute("Motor", New Object() {0, 0})

                ' Release arm control authority
                caoRobot.Execute("GiveArm")

                Dim robots As CaoRobots = caoCtrl.Robots
                robots.Remove(caoRobot.Name)

                System.Runtime.InteropServices.Marshal.ReleaseComObject(caoRobot)
                caoRobot = Nothing

                System.Runtime.InteropServices.Marshal.ReleaseComObject(robots)
                robots = Nothing
            End If
        End If

        ' Disconnect from the controller
        If Not (caoCtrl Is Nothing) Then
            If Not (caoWS Is Nothing) Then
                Dim ctrls As CaoControllers = caoWS.Controllers
                ctrls.Remove(caoCtrl.Name)

                System.Runtime.InteropServices.Marshal.ReleaseComObject(caoCtrl)
                caoCtrl = Nothing

                System.Runtime.InteropServices.Marshal.ReleaseComObject(ctrls)
                ctrls = Nothing
            End If
        End If

        ' Disable command buttons
        cmdConnectRobot.Enabled = True
        cmdDisconnectRobot.Enabled = False
        cmdMove.Enabled = False
        cmdApproach.Enabled = False
        cmdDepart.Enabled = False
        cmdDraw.Enabled = False
        cmdDriveAEx.Enabled = False
        cmdDriveEx.Enabled = False
        cmdSpeed.Enabled = False
        cmdChangeTool.Enabled = False
        cmdChangeWork.Enabled = False

    End Sub
	
	Private Sub cmdMove_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdMove.Click
		
		On Error GoTo ErrorProc
		
        ' Move command
        'caoRobot.Move(1, "P11")
        'caoRobot.Move(1, "P12")

        ' Move command
        caoRobot.Move(1, New Object() {New Object() {524, 0, 330, 180, 0, 180, 5}, "P", "@0"})
        caoRobot.Move(1, "@P P(535, -200, 250, 170, -5, 150, 5)")
		
        ' Move command
        'caoRobot.Move(1, New Object() {New Object() {0, 45, 90, 0, 45, 0}, "J", "@0"})
        'caoRobot.Move(1, "J(-22, 61, 76, 14, 48, -2)")

		Exit Sub
		
ErrorProc: 
		MsgBox(Err.Description & " : " & Hex(Err.Number))
		
	End Sub
	
	Private Sub cmdApproach_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdApproach.Click
		
		On Error GoTo ErrorProc
		
        ' Approach command
        'caoRobot.Execute("Approach", New Object() {2, "P11", "@P 100"})
		
        ' Approach command
        caoRobot.Execute("Approach", New Object() {2, "P(524, 0, 330, 180, 0, 180, 5)", "@P 100"})

		Exit Sub
		
ErrorProc: 
		MsgBox(Err.Description & " : " & Hex(Err.Number))
		
	End Sub
	
	Private Sub cmdDepart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDepart.Click
		
		On Error GoTo ErrorProc
		
        ' Depart command
        caoRobot.Execute("Depart", New Object() {2, "@P 100"})
		
		Exit Sub
		
ErrorProc: 
		MsgBox(Err.Description & " : " & Hex(Err.Number))
		
	End Sub
	
	Private Sub cmdDraw_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDraw.Click
		
		On Error GoTo ErrorProc
		
        ' Draw command (X=50,Y=50,Z=50)
        caoRobot.Execute("Draw", New Object() {1, "V(50,50,50)"})
		
		Exit Sub
		
ErrorProc: 
		MsgBox(Err.Description & " : " & Hex(Err.Number))
		
	End Sub
	
	Private Sub cmdDriveAEx_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDriveAEx.Click
		
		On Error GoTo ErrorProc
		
        ' DriveAEx command
        caoRobot.Execute("DriveAEx", New Object() {"@P (1,10),(2,50),(3,75),(4,14),(5,55),(6,10)"})
		
		Exit Sub
		
ErrorProc: 
		MsgBox(Err.Description & " : " & Hex(Err.Number))
		
	End Sub
	
	Private Sub cmdDriveEx_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDriveEx.Click
		
		On Error GoTo ErrorProc
		
        ' DriveEx command
        caoRobot.Execute("DriveEx", New Object() {"@P (1,-7),(2,9),(3,7),(4,15),(5,10),(6,12)"})
        'caoRobot.Execute("DriveEx", New Object() {"@0 (1,-35)"})

		Exit Sub
		
ErrorProc: 
		MsgBox(Err.Description & " : " & Hex(Err.Number))
		
	End Sub
	
	Private Sub cmdSpeed_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSpeed.Click
		
		On Error GoTo ErrorProc
		
        ' Speed command (Internal speed of 75%)
        caoRobot.Speed(-1, 75)
		
		Exit Sub
		
ErrorProc: 
		MsgBox(Err.Description & " : " & Hex(Err.Number))
		
	End Sub
	
	Private Sub cmdChangeTool_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdChangeTool.Click
		
		On Error GoTo ErrorProc
		
        ' Change Tool command (Tool1)
        caoRobot.Change("Tool1")
		
		Exit Sub
		
ErrorProc: 
		MsgBox(Err.Description & " : " & Hex(Err.Number))
		
	End Sub
	
	Private Sub cmdChangeWork_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdChangeWork.Click
		
		On Error GoTo ErrorProc
		
        ' Change Work command (Work1)
        caoRobot.Change("Work1")
		
		Exit Sub
		
ErrorProc: 
		MsgBox(Err.Description & " : " & Hex(Err.Number))
		
	End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Call cmdDisconnectRobot_Click(sender, e)

        ' Delete CaoEngine
        If Not (caoWS Is Nothing) Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(caoWS)
            caoWS = Nothing
        End If
        If Not (caoWSs Is Nothing) Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(caoWSs)
            caoWSs = Nothing
        End If
        If Not (caoEng Is Nothing) Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(caoEng)
            caoEng = Nothing
        End If
    End Sub
End Class