VERSION 5.00
Begin VB.Form frmTracking 
   BorderStyle     =   1  '固定(実線)
   Caption         =   "Tracking"
   ClientHeight    =   930
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   5670
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   930
   ScaleWidth      =   5670
   StartUpPosition =   3  'Windows の既定値
   Begin VB.Frame Frame2 
      Caption         =   "ConveyerTracking"
      Height          =   795
      Left            =   60
      TabIndex        =   1
      Top             =   60
      Width           =   5535
      Begin VB.Timer Timer1 
         Enabled         =   0   'False
         Interval        =   100
         Left            =   5160
         Top             =   510
      End
      Begin VB.CommandButton cmdExit 
         Caption         =   "Exit"
         Height          =   375
         Left            =   4170
         TabIndex        =   4
         Top             =   240
         Width           =   1215
      End
      Begin VB.CommandButton cmdTerm 
         Caption         =   "Stop"
         Height          =   375
         Left            =   2820
         TabIndex        =   3
         Top             =   240
         Width           =   1215
      End
      Begin VB.CommandButton cmdDummyIO 
         Caption         =   "Dummy I/O"
         Height          =   375
         Left            =   1470
         TabIndex        =   2
         Top             =   240
         Width           =   1215
      End
      Begin VB.CommandButton cmdExecute 
         Caption         =   "Start"
         Height          =   375
         Left            =   120
         TabIndex        =   0
         Top             =   240
         Width           =   1215
      End
   End
End
Attribute VB_Name = "frmTracking"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private caoEng As New CaoEngine
Private caoCtrls As CaoControllers
Private caoRob As CaoRobot
Private WithEvents caoCtrl  As CaoController
Attribute caoCtrl.VB_VarHelpID = -1
Private caoMess As CaoMessage

Private m_bTermSignal As Boolean
Private m_bDetectedSignal As Boolean
Private m_vDetectedPos As Variant 'The position where the workpiece is detected
Private m_bDoEvents As Boolean

Private Declare Function GetTickCount Lib "kernel32" () As Long

' ----------------------------------------------------
Private Sub Form_Load()
    
    Set caoEng = New CaoEngine
    Set caoCtrls = caoEng.Workspaces(0).Controllers

End Sub

' ----------------------------------------------------
Private Sub Form_Unload(Cancel As Integer)

    Disconnect
    
    Set caoCtrls = Nothing
    Set caoEng = Nothing
    
End Sub

' ----------------------------------------------------
Private Sub Connect()
    Dim sConn As String
    
    Disconnect
    
    sConn = "Conn=eth:192.168.0.1" 'IP address of RC7M
    
    Set caoCtrl = caoCtrls.Add("RC1", "CaoProv.DENSO.NetwoRC", "", sConn)
    Set caoRob = caoCtrl.AddRobot("RC1")
    caoRob.AddVariable ("@BUSY_STATUS")
    
End Sub

' ----------------------------------------------------
Private Sub Disconnect()

    If Not caoCtrl Is Nothing Then
        Set caoRob = Nothing
        caoCtrl.Robots.Clear
    End If

    If Not caoCtrls Is Nothing Then
        caoCtrls.Clear
        Set caoCtrl = Nothing
    End If
    
    If Not caoMess Is Nothing Then
        Set caoMess = Nothing
    End If
 
End Sub

' ----------------------------------------------------
Private Sub cmdExecute_Click()
On Error GoTo ErrProc

    Connect

    'Tracking operation
    ConveyerTracking
    
    Disconnect

    Exit Sub
ErrProc:
    MsgBox "ERROR:" + Err.Description
End Sub

' ----------------------------------------------------
'*****************************************************
' Set the workpiece detected signal ON
Private Sub cmdDummyIO_Click()
    MsgBox "Turn the I/O [48] ON (Hand-In No.1)"
End Sub

'*****************************************************

' ----------------------------------------------------
Private Sub cmdTerm_Click()
    m_bTermSignal = True
End Sub

' ----------------------------------------------------
'Do semothings all at once
Private Sub MyDoEvents()

    If m_bDoEvents Then Exit Sub
    m_bDoEvents = True
    'To the Windows system
    DoEvents
    
    'To workpiece detection
    WorkpieceDetection
    m_bDoEvents = False
    
End Sub

' ----------------------------------------------------
'Tracking operation
' [Required RC7M settings]
'   Hand I/O No.1 = IO48 for workpiece detection
' [Required points]
'   P50 : robot home position
'   P70 : to eject workpiece
' [temporary points]
'   P60 : workpiece detected position
'   P61 : to chunck workpiece
Private Sub ConveyerTracking()

    Dim vRet As Variant
    Dim vPos As Variant
    Dim vCurPos As Variant
    Dim lRest As Long
    Dim lErr As Long
    Dim bExit As Boolean
    
    With caoCtrl
        .Variables.Clear
        .AddVariable "P60"
        .AddVariable "P61"
        .AddVariable "IO48" 'Hand I/O No.1
    End With
    
    m_bDoEvents = False
    m_bTermSignal = False 'Turn OFF the workpiece recognition processing termination signal
    With caoRob
        .Execute "ResetTrackMove" 'Canceling conveyer 1 tracking
        'Initializing conveyer 1 data buffer
        .Execute "TrackDataInitialize", 1
        'Set a tracking start/stop range
        .Execute "SetTrackStartArea", Array(1, -200, 200)
    
        .Speed -1, 100 'SPEED=100
        .Move 1, "P50" 'Goto P50 Home Position
        '.Move 1, Array(50, "P") 'Goto P50 Home Position
        
        '********************************************************
        ' *** INSERT YOUR ORIGINAL CODE HERE ! ***
        'Start the workpiece recognition task
        '********************************************************
        'The workpiece recognition results are always monitored after here
        
        Do
            MyDoEvents 'Do something all at once
            
            'Repeated until the workpiece recognition processing termination signal is ON
            If m_bTermSignal = True Then Exit Do
            
            If .Execute("TrackDataNum", 1) > 0 Then 'If the conveyer tracking data buffer has data,
                
                'data is obtained from the conveyer tracking data buffer
                vRet = .Execute("TrackDataGet", Array(1, 0))
                    ' -> Return( Array(<rest>, P(<x>,<y>,<z>,<rx>,<ry>,<rz>,<fig>))
                    
                lRest = vRet(0) 'rest info
                If lRest >= 0 Then 'When workpiece data is obtained normally
                    vPos = vRet(1) 'P type
                    'vPos(2) = 100#  'Set a position in the direction of Z.
                    vPos(3) = 0: vPos(4) = 0: vPos(5) = 90 'Set a posture at the tracking operation. RX=0,RY=0,RZ=0
                    vPos(6) = 0 'Set a robot figure at the tracking operation.
                    caoCtrl.Variables("P60").Value = vPos
    
                    'Waiting until the workpiece subject to tracking reaches the tracking start range
                    bExit = False
                    Do
                        vRet = .Execute("WaitTrackMoveEx", Array(1, vPos, 0)) 'Timeout is 0 sec
                        Select Case vRet '(1)upstream |<-(0)->| (2)downstream
                        Case 1 'Upstream (timeout)
                            'MyDoEvents 'Do something all at once
                            WaitingFor 100
                        Case 0 'Into the tracking range
                            bExit = True
                        Case 2 'Downstream
                            GoTo DoNext
                        End Select
                    Loop Until bExit = True
                    
                    'Obtaining the operation terminating position in the case of PTP operation for the workpiece subject to tracking
                    vRet = .Execute("CurTrackPosEx", Array(1, vPos, 1))
                    ' -> Return( <err_info>, P(<x>,<y>,<z>,<rx>,<ry>,<rz>,<fig> )
                    lErr = vRet(0) 'error info
                    If lErr = 0 Then
                        vCurPos = vRet(1)  'P type
                        vCurPos(2) = vCurPos(2) + 70 'Z+70
                        caoCtrl.Variables("P61").Value = vCurPos
        
                        '--------------------------------------------------------
                        'MOVE P,P61
                        .Move 1, "P61", "NEXT"
                        '.Move 1, Array(61, "P", ""), "NEXT"
                        WaitingForRobotMotion
                        '--------------------------------------------------------
                        
                        .Execute "SetTrackMove", 1 'Conveyer 1 tracking is effective
        
                            'Chunck a workpiece
                            '--------------------------------------------------------
                            'APPROACH P,P60,@P 70, NEXT
                            .Execute "APPROACH", Array(1, "P60", "@P 70", "NEXT")
                            '.Execute "APPROACH", Array(1, Array(60, "P"), Array(70, "", "@P"), "NEXT")
                            WaitingForRobotMotion
                            
                            'MOVE P, P60, NEXT
                            .Move 1, "P60", "NEXT"
                            '.Move 1, Array(60, "P"), "NEXT"
                            WaitingForRobotMotion

                            WaitingFor 100 'Chucking a workpiece (such as IO(49) = 1)
                            
                            'DEPART P,@P 30, NEXT
                            .Execute "DEPART", Array(1, "@P 30", "NEXT")
                            '.Execute "DEPART", Array(1, Array(30, "", "@P"), "NEXT")
                            WaitingForRobotMotion
                            '--------------------------------------------------------
                        
                        .Execute "ResetTrackMove" 'Canceling conveyer 1 tracking
                        
                        'Eject the workpiece
                        '--------------------------------------------------------
                        'APPROACH P,P70,@P 70, NEXT
                        .Execute "APPROACH", Array(1, "P70", "@P 70", "NEXT")
                        '.Execute "APPROACH", Array(1, Array(70, "P"), Array(70, "", "@P"), "NEXT")
                        WaitingForRobotMotion
                        
                        'Move P,@E P70, NEXT
                        .Move 1, "@E P70", "NEXT"
                        '.Move 1, Array(70, "P", "@E"), "NEXT"
                        WaitingForRobotMotion
                        
                        'DEPART P,@P 70, NEXT
                        .Execute "DEPART", Array(1, "@P 70", "NEXT")
                        '.Execute "DEPART", Array(1, Array(70, "", "@P"), "NEXT")
                        WaitingForRobotMotion
                        '--------------------------------------------------------
                        
                    End If
                End If
DoNext:
            End If
        Loop
            
    End With

End Sub

' ----------------------------------------------------
'Workpiece detection
Private Sub WorkpieceDetection()
    
    Static lInvalidTick As Long
    If m_bDetectedSignal = True Then
        'Waiting for 200msec (To don't detect same workpieces)
        If (GetTickCount() - lInvalidTick) > 200 Then
            m_bDetectedSignal = False ' Turn OFF
        Else
            Exit Sub
        End If
    End If
    
    'Workpiece recognition results are always monitored
    '********************************************************
    ' *** INSERT YOUR ORIGINAL CODE HERE ! ***
    ' (such as following code)
    Static bIOVal As Boolean
    Dim bStatus As Boolean
    bStatus = caoCtrl.Variables("IO48").Value
    If bStatus = True Then
        If bIOVal = False Then
            m_bDetectedSignal = True 'the workpiece is detected !
            'vDetectedPos indicates the position where the workpiece is detected
            Dim rPos(3) As Single
            rPos(0) = -400: rPos(1) = 400: rPos(2) = 100 '(X=-400, Y=400, Z=100)
            m_vDetectedPos = rPos()
            bIOVal = True
        End If
    Else
        bIOVal = False
    End If
    '********************************************************
    
    If m_bDetectedSignal = True Then
    
        Debug.Print "Detected!", GetTickCount()
        'Save the detected workpiece data to the conveyer tracking data buffer
        caoRob.Execute "TrackDataSet", Array(1, 1, m_vDetectedPos)
        
        lInvalidTick = GetTickCount()
    End If
    
End Sub

' ----------------------------------------------------
'Waiting for <lTickCount> msec
Private Sub WaitingFor(ByVal lTickCount As Long)
    Dim lStartTick As Long
    Dim lNowTick As Long
    
    lStartTick = GetTickCount()
    Do
        MyDoEvents 'Do semothings all at once
        lNowTick = GetTickCount()
        If (lNowTick - lStartTick) > lTickCount Then Exit Do
    Loop

End Sub

' ----------------------------------------------------
'Waiting for robot motion
Private Sub WaitingForRobotMotion()

    Do
        MyDoEvents 'Do something all at once
        
        'Exit if the termination signal ON
        If m_bTermSignal = True Then Exit Do
        
        'Judges whether execution of running motion commands is complete
        If Not caoRob.Variables("@BUSY_STATUS").Value Then Exit Do
        
        WaitingFor 50 'Waiting for 50 msec
    Loop

End Sub

' ----------------------------------------------------
Private Sub cmdExit_Click()
    Unload Me
End Sub

