로봇 프로그래밍

팔 동작 - (5) Dobot Magician Python 프로그래밍

edblab 2023. 3. 31. 22:34
728x90

Dobot Magician Python 프로그램에서 로봇의 동작을 구현하기 위하여 setPTPCmd()를 사용한다. 로봇 동작을 구현할 때 하드웨어 로봇의 동작 수행과 컴퓨터 처리 속도가 차이가 나므로 비동기 장치를 해 주어야 한다.

이 프로그램은 총 5번 x 축 방향으로 왕복하는 명령를 실행시킨다. 명령을 실행시킬 때 마지막 명령어의 Queue Index를 lastIndex에 저장한 후,  dType.SetQueuedCmdStartExec() 함수로 Queue의 명령을 실행시킨다. 그 후 LastIndex와 현재까지 실제 실행된 명령어 Index(dType.GetQueuedCmdCurrentIndex(api)[0])를 비교하여 마지막 명령이 실행될 때까지 기다린다.

# 2023/3/31 test020
# move 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]

    # Start to Execute Command Queue
    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)

print("waiting, lastIndex, Index(dType.GetQueuedCmdCurrentIndex(api)[0])를 통해 프로그램이 Queue의 명령어가 실행기를 기다리는 것을 확인할 수 있다.