로봇 프로그래밍

그리퍼 동작 - (6) Dobot Magician 파이썬 프로그래밍

edblab 2023. 3. 31. 23:33
728x90

그리퍼의 동작을 제어하는 SetEndEffectorGripper()를 사용하여 반복적으로 그리퍼를 열고 닫는 시험을 진행하였다.

# 2023/3/31 test030
# gripper test - 그리퍼  제어

import threading
import DobotDllType as dType

CON_STR = {
    dType.DobotConnect.DobotConnect_NoError:  "DobotConnect_NoError",
    dType.DobotConnect.DobotConnect_NotFound: "DobotConnect_NotFound",
    dType.DobotConnect.DobotConnect_Occupied: "DobotConnect_Occupied"}

# Load Dll and get the CDLL object
api = dType.load()

# Connect Dobot
state = dType.ConnectDobot(api, "", 115200)[0]
print("Connect status:",CON_STR[state])

if (state == dType.DobotConnect.DobotConnect_NoError):
    
    # Clean Command Queued
    dType.SetQueuedCmdClear(api)
    
    # Async Motion Params Setting (x, y, z, r)
    dType.SetHOMEParams(api, 220, 0, 0, 0, isQueued = 1)
    dType.SetPTPJointParams(api, 200, 200, 200, 200, 200, 200, 200, 200, isQueued = 1)
    dType.SetPTPCommonParams(api, 100, 100, isQueued = 1)

    # Async Home
    dType.SetHOMECmd(api, temp = 0, isQueued = 1)

    # Async PTP Motion (동작은 X 방향으로 왕복 5회)
    for i in range(0, 5):
        if i % 2 == 0:
            offset = 50
        else:
            offset = -50
        lastIndex = dType.SetPTPCmd(api, dType.PTPMode.PTPMOVLXYZMode, 220 + offset, 0, 0, 0, isQueued = 1)[0]
        if i % 2 == 0:
            lastIndex = dType.SetEndEffectorGripper(api, 1, 1, isQueued=1)[0]
        else:
            lastIndex = dType.SetEndEffectorGripper(api, 1, 0, isQueued=1)[0]


    # Start to Execute Command Queue
    lastIndex = dType.SetEndEffectorGripper(api, 0, 0, isQueued=1)[0]
    dType.SetQueuedCmdStartExec(api)

    # lastIndex를 확인한 명령이 실행 될  때까지 기다린다.
    while lastIndex > dType.GetQueuedCmdCurrentIndex(api)[0]:
        print("wating...",lastIndex, dType.GetQueuedCmdCurrentIndex(api)[0])
        dType.dSleep(100)

    # Stop to Execute Command Queued
    dType.SetQueuedCmdStopExec(api)

# Disconnect Dobot
dType.DisconnectDobot(api)