VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Robot"
   ClientHeight    =   8460
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4050
   LinkTopic       =   "Form1"
   ScaleHeight     =   8460
   ScaleWidth      =   4050
   StartUpPosition =   3  'Windows の既定値
   Begin VB.Frame Frm2 
      BackColor       =   &H00FFCC99&
      BorderStyle     =   0  'なし
      Height          =   5775
      Left            =   360
      TabIndex        =   4
      Top             =   2520
      Width           =   3255
      Begin VB.CommandButton cmdChangeWork 
         Caption         =   "ChangeWork"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   13
         Top             =   5040
         Width           =   2295
      End
      Begin VB.CommandButton cmdChangeTool 
         Caption         =   "ChangeTool"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   12
         Top             =   4440
         Width           =   2295
      End
      Begin VB.CommandButton cmdSpeed 
         Caption         =   "Speed"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   11
         Top             =   3840
         Width           =   2295
      End
      Begin VB.CommandButton cmdDriveEx 
         Caption         =   "DriveEx"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   10
         Top             =   3240
         Width           =   2295
      End
      Begin VB.CommandButton cmdDriveAEx 
         Caption         =   "DriveAEx"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   9
         Top             =   2640
         Width           =   2295
      End
      Begin VB.CommandButton cmdDraw 
         Caption         =   "Draw"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   8
         Top             =   2040
         Width           =   2295
      End
      Begin VB.CommandButton cmdDepart 
         Caption         =   "Depart"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   7
         Top             =   1440
         Width           =   2295
      End
      Begin VB.CommandButton cmdApproach 
         Caption         =   "Approach"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   6
         Top             =   840
         Width           =   2295
      End
      Begin VB.CommandButton cmdMove 
         Caption         =   "Move"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   5
         Top             =   240
         Width           =   2295
      End
   End
   Begin VB.Frame Frm1 
      BackColor       =   &H00CCFFCC&
      BorderStyle     =   0  'なし
      Height          =   2175
      Left            =   360
      TabIndex        =   0
      Top             =   120
      Width           =   3255
      Begin VB.CommandButton cmdDisconnectRobot 
         Caption         =   "Disconnect Robot"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   3
         Top             =   1440
         Width           =   2295
      End
      Begin VB.CommandButton cmdConnectRobot 
         Caption         =   "Connect Robot"
         Enabled         =   0   'False
         Height          =   495
         Left            =   480
         TabIndex        =   2
         Top             =   840
         Width           =   2295
      End
      Begin VB.CommandButton cmdInit 
         Caption         =   "Init"
         Height          =   495
         Left            =   480
         TabIndex        =   1
         Top             =   240
         Width           =   2295
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim caoEng As CaoEngine
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

' -----------------------------Initialization----------------------------
Private Sub cmdInit_Click()

    ' Disconnect Robot
    Call cmdDisconnectRobot_Click

    ' Create Engine
    Set caoEng = Nothing
    Set caoEng = New CaoEngine
    Set caoWS = caoEng.Workspaces(0)

End Sub

' ---------------------------------------------------------------
' Connect with the Robot
' ---------------------------------------------------------------
Private Sub cmdConnectRobot_Click()

    On Error GoTo ErrorProc
    
    ' Create object and connect with VRC
    Set caoCtrl = caoWS.AddController("Robot", "caoProv.DENSO.RC8", "", "Server=192.168.0.1")
    
    Set caoRobot = caoCtrl.AddRobot("VS")
       
    ' Valiable of status of Machine lock SW
    Set caoVal = caoCtrl.AddVariable("@LOCK")

    ' Turned on the Machine lock
    caoVal.Value = True
    
    ' Get armControl authority
    caoRobot.Execute "TakeArm", Array(0, 1)
       
    ' Start motor（The motor cannot start when the machine lock is true）
'    caoRobot.Execute "Motor", Array(1, 0)
       
    ' Set the external movement speed of the robot
    caoRobot.Execute "ExtSpeed", Array(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
    MsgBox Err.Description & " : " & Hex(Err.Number)

End Sub

' ---------------------------------------------------------------
' disconnect from the Robot
' ---------------------------------------------------------------
Private Sub cmdDisconnectRobot_Click()

    On Error Resume Next
    
    ' Disconnect from the Robot
    If Not (caoRobot Is Nothing) Then
        
        If Not (caoCtrl Is Nothing) Then
            ' Stop motor
'            caoRobot.Execute "Motor", Array(0, 0)
            
            ' Release arm control authority
            caoRobot.Execute "GiveArm"

            caoCtrl.Robots.Remove caoRobot.Name
            Set caoRobot = Nothing
        End If
    End If
    
    ' Disconnect from the controller
    If Not (caoCtrl Is Nothing) Then
        If Not (caoEng Is Nothing) Then
            caoEng.Workspaces(0).Controllers.Remove caoCtrl.Name
            Set caoCtrl = 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()

    On Error GoTo ErrorProc
            
    ' Move command
'     caoRobot.Move 1, "P11"
'     caoRobot.Move 1, "P12"
    
    ' Move command
    caoRobot.Move 1, Array(Array(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, Array(Array(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()

    On Error GoTo ErrorProc
    
    ' Approach command
'    caoRobot.Execute "Approach", Array(2, "P11", "@P 100")
    
    ' Approach command
    caoRobot.Execute "Approach", Array(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()

    On Error GoTo ErrorProc
    
    ' Depart command
    caoRobot.Execute "Depart", Array(2, "@P 100")
    
    Exit Sub
    
ErrorProc:
    MsgBox Err.Description & " : " & Hex(Err.Number)

End Sub

Private Sub cmdDraw_Click()

    On Error GoTo ErrorProc
    
    ' Draw command (X=50,Y=50,Z=50)
    caoRobot.Execute "Draw", Array(1, "V(50,50,50)")

    Exit Sub
    
ErrorProc:
    MsgBox Err.Description & " : " & Hex(Err.Number)

End Sub

Private Sub cmdDriveAEx_Click()
    
    On Error GoTo ErrorProc
    
    ' DriveAEx command
    caoRobot.Execute "DriveAEx", Array("@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()

    On Error GoTo ErrorProc
    
    ' DriveEx command
    caoRobot.Execute "DriveEx", Array("@P (1,-7),(2,9),(3,7),(4,15),(5,10),(6,12)")
'    caoRobot.Execute "DriveEx", Array("@0 (1,-35)")
    
    Exit Sub
    
ErrorProc:
    MsgBox Err.Description & " : " & Hex(Err.Number)

End Sub

Private Sub 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()

    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()

    On Error GoTo ErrorProc
    
    ' Change Work command (Work1)
    caoRobot.Change "Work1"
    
    Exit Sub
    
ErrorProc:
    MsgBox Err.Description & " : " & Hex(Err.Number)

End Sub

Private Sub Form_Unload(Cancel As Integer)
    
    ' Delete CaoEngine
    If Not (caoRobot Is Nothing) Then
        Set caoRobot = Nothing
    End If
    If Not (caoCtrl Is Nothing) Then
        Set caoCtrl = Nothing
    End If
    If Not (caoWS Is Nothing) Then
        Set caoWS = Nothing
    End If
    If Not (caoEng Is Nothing) Then
        Set caoEng = Nothing
    End If
        
End Sub

