-- coding: utf-8 --

import os import sys import socket import threading import time os.add_dll_directory('C:/Users/yyy/Desktop/工比代码/2023/RobotLoader') import DobotDllType as dType

创建socket客户端变量

socket_client1 = ''

定义全局变量

i = 1 j = 0 k = 0 m = 1

定义机械臂状态,用键值存储

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

加载机械臂动态链接库DobotDll.dll

api = dType.load()

连接机械臂

state = dType.ConnectDobot(api, 'COM6', 115200)[0]

打印机械臂连接状态

print('Connect status:',CON_STR[state])

设置机械臂末端为夹爪

dType.SetEndEffectorParams(api, 59.7, 0, 0, 1)

初始化清空机械臂的指令

dType.SetQueuedCmdClear(api)

开始执行队列指令

dType.SetQueuedCmdStartExec(api) print('Starting...')

def weizhi1(): time_start1=time.time() dType.SetPTPCmdEx(api, 0,247, -157, 48, 12, isQueued=1) #抓取物料 dType.SetEndEffectorGripper(api, 1, 1, isQueued=1) while True: time_end1 = time.time() if(time_end1-time_start1) > 2: break else : pass

def weizhi2(): if j==0 and i==4: time.sleep(3)#3 else: time_start2=time.time() dType.SetPTPCmdEx(api, 0, (197 + j50), (-157 + i46), -2+(m*50), 12, isQueued=1) dType.SetEndEffectorGripper(api, 1, 1, isQueued=1) while True: time_end2 = time.time() if(time_end2-time_start2) > 3:#3 break else : pass

传送带运行设置

def target_reach(): # 传送带速度19000pulse/s dType.SetEMotorEx(api, 0, 1, 19000, isQueued=1)#dType.SetEMotorEx(api, 0, 1, 19000, isQueued=1) time_start = time.time() weizhi2() socket_client.send('arrive'.encode('utf-8')) #传送带运行到中间2s时发信号:解决左臂重复接收信号问题。 while True: time_end = time.time() # 3.5秒后传送带停止 if (time_end - time_start) > 4:#if (time_end - time_start) > 3: break else: pass

码垛堆叠多个方块

def pileup(): #移点 dType.SetPTPCmdEx(api, 0, 197, -157, 48, 12, isQueued=1)

# 抓取
dType.SetEndEffectorGripper(api, 1, 1, isQueued=1)
# 延时400ms
dType.dSleep(400)
# 放置                   
dType.SetPTPCmdEx(api, 0, 11, -275, 74, 12, isQueued=1)   #y=-279 R=20
# 释放
dType.SetEndEffectorGripper(api, 1, 0, isQueued=1)
#传送带运行
dType.SetEMotorEx(api, 0, 1, 19000, isQueued=1)#dType.SetEMotorEx(api, 0, 1, 19000, isQueued=1)
#计时开始
time_start = time.time()
weizhi1()
dType.dSleep(2000)#2000                 
socket_client.send('arrive'.encode('utf-8'))     #传送带运行到中间2s时发信号:解决左臂重复接收信号问题。
while True:
    time_end = time.time()
    # 4秒后传送带停止
    if (time_end - time_start) > 4:#4
        break
    else :
        pass
# 传送带停止,触发相机拍照定位                                                           
dType.SetEMotorEx(api, 0, 0, 0, isQueued=1)

#抓到传送带上面
dType.SetPTPCmdEx(api, 0, 11, -275, 74, 12, isQueued=1)#dType.SetPTPCmdEx(api, 0, -56, -255, 74, 20, isQueued=1)      #y=-279 R=20
pass

def pileup1():

global i,j,k,m
# 默认释放夹爪
dType.SetEndEffectorGripper(api, 1, 0, isQueued=1)

# 传送带固定运行3.5s
target_reach()
# 传送带停止,触发相机拍照定位                                                           
dType.SetEMotorEx(api, 0, 0, 0, isQueued=1)

# 一层
dType.SetPTPCmdEx(api, 0, (197 + j*50), (-157 + i*46), -2+(m*50), 12, isQueued=1)

if j==1 and i==0 and m==1:
      dType.SetPTPCmdEx(api, 0,247, -157, 48, 12, isQueued=1)
      dType.SetEndEffectorGripper(api, 1, 1, isQueued=1)
      time.sleep(0.4)
      dType.SetPTPCmdEx(api, 0, 11, -275, 74, 20, isQueued=1)         #x=-56  y=-279
else:
    pass
    

# 抓取
dType.SetEndEffectorGripper(api, 1, 1, isQueued=1)
# 延时400ms
dType.dSleep(200)#400
# 放置                   
dType.SetPTPCmdEx(api, 0, 11, -275, 74, 20, isQueued=1)                 #x=-56 y=-279

if j < 1 and i <= 3:
    j = 1
elif j == 1 and i < 3:
    j = 0
    i += 1
elif j== 1 and i==3 and m==1:
    m=0
    i=0
    j=0
elif j== 1 and i==3 and m==0:
    m=1
    i=4
    j=0
    pass

def pileup2(): #释放物料 dType.SetEndEffectorGripper(api, 1, 0, isQueued=1) #延时200ms #dType.dSleep(200) # 抓取(按照行列式) # 传送带固定运行3.5s target_reach() # 传送带停止,触发相机拍照定位
dType.SetEMotorEx(api, 0, 0, 0, isQueued=1) sys.exit() print('机械臂结束上料')

初始化TCP客户端,连接视觉服务端,等待信号

def TCPClient_Vision(): global socket_client socket_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: ip_Vision = '127.0.0.1' port = 4000 socket_client.connect((ip_Vision, port)) print('Successful to initialize tcpclient_Vision.') except Exception as e: print(e) while True: try: data = socket_client.recv(1024).decode('utf-8') print(data) checkdata = data[:5] # 当接收到run指令开始上料 if checkdata == 'run': print('机械臂上料') if j==0 and i==4: pileup2() checkdata = 'null'

            else:
                 pileup1()
                 checkdata = 'null'
    except UnicodeDecodeError:
        print('error')
    #time.sleep(0.1)

主程序入口

if name == 'main': # 新增 ,创建TCP客户端,与视觉软件通信 client_vision = threading.Thread(target=TCPClient_Vision) # 设置子线程为守护线程,防止退出主线程时,子线程仍在运行 client_vision.setDaemon(True) # 启动子线程 client_vision.start() time.sleep(0.5) print('机械臂开始上料') pileup()
while True: pass #dType.SetHOMEParams(api, 46.5823, 207, 85, 0, isQueued = 1)

Dobot 机械臂码垛堆叠 Python 程序

原文地址: https://www.cveoy.top/t/topic/nVCA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录