使用TPC控制UR3机器臂python代码
记录下自己学习UR3的过程
UR3的消息格式
代码
使用时机器臂IP:192.168.0.1,电脑IP:192.168.0.2。python版本:3.8
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#SHIJIANGAN
from socket import *
import struct
import array
import time as T
class UR3:
def __init__(self):
ip_port=('192.168.0.1',30003)
tcp_client=socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
self.tcp_client = tcp_client
self.back_log=5
self.buffer_size=1060
def read_data(self):
'''读取数据:返回运行时间
返回工具在基坐标系下的坐标
返回各个关节角度
'''
# format 格式:
# !转为大端
# I 解析数据长度
# I 132d 解析全部数据
formatLength = struct.Struct('! I')
format = struct.Struct("! I 132d")
recv_bytes = self.tcp_client.recv(self.buffer_size) # 接收数据
# print(formatLength.unpack(recv_bytes[0:4])[0]) # 查看收到的数据总数
clean_data = array.array('d', [0] * 132)
clean_data = format.unpack(recv_bytes) # 解析成数据
time = clean_data[1] # 取出机械臂运行时间
tool_vector_actual = clean_data[56:61] # 取出工具在基坐标系下的坐标
q_actual = self.rad_2_deg(clean_data[32:37]) # 取出各个关节角度
print(u'开启时间:%s,\n工具在base下的坐标: %s,\n各个关节角度: %s \n'%(
time/60,
tool_vector_actual,
q_actual
))
def movej(self, action):
'''移动到指定的点,但不一定保持直线运动
参数:actio字符串类型,如'[0,-1.57,0,-1.57,0,0]',数值为弧度
'''
cartesian = False
a_joint = 1.4 # 加速度
v_joint = 0.75 # 速度
t = 0 # 移动时间
radius =0 # 弧度
message = 'movej({}{},a={},v={},t={},r={})'.format('q' if cartesian
else '',
action,
a_joint,
v_joint,
t,
radius)+'\n'
meg = bytes(message,encoding="utf-8")
# print(meg.decode())
self.tcp_client.send(meg)
T.sleep(2) # 每次命令发送后需要等待移动
你可能用到的其他命令的格式