> 技术文档 > UR机械臂TCP/IP通讯控制(ur_rtde)_ur机械臂控制

UR机械臂TCP/IP通讯控制(ur_rtde)_ur机械臂控制

目录

一、准备工作(机器人连接、外部控制)

二、通过tcp/ip控制机器人

1.tcp/ip通信协议/通信端口

2.URScript脚本例程

(1)30001端口

(2)30003端口:

1)movej

2)movel

3)get_current_pos()

4)get_current_tcp()

三.ur_rtde

1.官方网址

2.RTDE简单使用参考

1)ur_rtde安装

2)python简单使用例子

3)异步移动例子


一、准备工作(机器人连接、外部控制)

参考:

UR5机械臂+ROS noetic+Ubuntu20.04+moveit实物和仿真驱动_noetic 仿真ur5-CSDN博客

UR机械臂学习(5-1):驱动真实机械臂准备工作——示教器配置_melodic中ur3驱动真实机械臂-CSDN博客

二、通过tcp/ip控制机器人

        URScript是UR公司基于Python语言编写的编程语言

1.tcp/ip通信协议/通信端口

参考:UR机器人通信端口和协议_客户端接口(30001-30003)-CSDN博客

2.URScript脚本例程

(1)30001端口

参考文章:http://t.csdnimg.cn/0wo2k,如果想一次性发送一段程序给机器人,需要将发送的程序按照如下格式发送:

程序演示:设置socket通信,选择TCP通信协议,设置机器人ip端口,发送目标程序

import socketimport structdef test_move(): mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # change the robot IP address here host = \'192.168.55.101\' port = 30001 mySocket.connect((host, port)) script_text=\"def test_move():\\n\" \\ \" global P_start_p=p[.6206, -.1497, .2609, 2.2919, -2.1463, -.0555]\\n\" \\ \" global P_mid_p=p[.6206, -.1497, .3721, 2.2919, -2.1463, -.0555]\\n\" \\ \" global P_end_p=p[.6206, -.1497, .4658, 2.2919, -2.1463, -.0555]\\n\" \\ \" while (True):\\n\" \\ \" movel(P_start_p, a=1.2, v=0.25)\\n\" \\ \" movel(P_mid_p, a=1.2, v=0.25)\\n\" \\ \" movel(P_end_p, a=1.2, v=0.25)\\n\" \\ \" end\\n\" \\ \"end\\n\" mySocket.send((script_text + \"\\n\").encode()) mySocket.close()

(2)30003端口:

1)movej

URScript的movej可以控制机械臂运动到给定的目标关节角度,其中运动的时间可以通过速度加速度控制,也可以通过设定时间来控制机械臂运动,时间t优先于速度加速度。参考官方文档如下:

程序演示:设置socket通信,选择TCP通信协议,设置机器人ip端口,发送目标程序

import socketimport structimport numpy as npdef degree_to_rads(joints): rads = [angle * np.pi / 180 for angle in joints] return radsdef ur5_movej(joints,a,v,t): IP = \'192.168.55.101\' PORT = 30003 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((IP, PORT)) joints_str = \",\".join(map(str, joints)) strL = \"movej([{0}], a={1}, v={2}, t={3}, r=0)\\n\".format(joints_str,a,v,t).encode() print(strL) s.send(strL) s.close()if __name__ == \'__main__\': q0 = degree_to_rads([90, -70, 70, -40, 0, 0]) ur5_movej(joints=q0,a=0.9,v=0.9,t=2)
2)movel

URScript的movel可以控制机械臂在笛卡尔空间坐标系运动,通过给定机械臂笛卡尔空间坐标系下的(x,y,z,rx,ry,rz)  运动到给定的目标位姿,其中运动的时间可以通过速度加速度控制,也可以通过设定时间来控制机械臂运动,时间t优先于速度加速度。参考官方文档如下:

程序演示:设置socket通信,选择TCP通信协议,设置机器人ip端口,发送目标程序

import socketimport structimport numpy as npdef degree_to_rads(joints): rads = [angle * np.pi / 180 for angle in joints] return radsdef ur5_movel(pose,a,v,t): IP = \'192.168.55.101\' PORT = 30003 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((IP, PORT)) pose_str = \",\".join(map(str, pose)) strL = \"movel(p[{0}], a={1}, v={2}, t={3}, r=0)\\n\".format(pose_str,a,v,t).encode() print(strL) s.send(strL) s.close()if __name__ == \'__main__\': pose1=[0.55275006,-0.09083575,0.485071,2.73019624,-1.12719368,0.15573756] ur5_movel(pose=pose1,a=0.5,v=0.5,t=0)
3)get_current_pos()

get_current_pos():获取机械臂当前的关节角度

下表为30003实时反馈端口机器人信息1044字节数据格式,来源官方文档,根据30003反馈的信息可以知道通过socket通讯返回的recv(1116)信息中,32-37位置为实际机械臂关节角度,即data[252:300]

程序演示:

def get_current_pos(): IP = \'192.168.55.101\' PORT = 30003 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((IP, PORT)) data = s.recv(1116) pos = struct.unpack(\'!6d\',data[252:300]) #解析32-37位置实际机械臂关节角度 npPos = np.asarray(pos) s.close() return npPos
4)get_current_tcp()

get_current_tcp():获取机械臂在末端工具坐标系的当前位姿(x,y,z,rx,ry,rz)

根据30003实时反馈端口机器人信息1044字节数据格式,及30003反馈的信息可以知道通过socket通讯返回的recv(1116)信息中,56-61位置为实际机械臂在末端工具坐标系的当前位姿(x,y,z,rx,ry,rz),即data[444:492]

程序演示:

def get_current_tcp(): IP = \'192.168.55.101\' PORT = 30003 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((IP, PORT)) data = s.recv(1116) position = struct.unpack(\'!6d\',data[444:492]) #解析56-61位置实际末端坐标系位置,前三位为x,y,z位置信息, #后三位为rx,ry,rz旋转向量信息 npPosition = np.asarray(position) s.close() return npPosition

三.ur_rtde

自 UR 软件版本 3.5 起,Realtime 接口已被弃用,建议使用更新的实时数据交换(RTDE)接口,

ur_rtde与上述的区别是,ur_rtde已经将相关的socket/ TCP/IP通信封装好,能够直接调用封装好的接口实现上述的功能。

1.官方网址

Installation — ur_rtde 1.6.0 documentation

2.RTDE简单使用参考

1)ur_rtde安装

linux系统安装

sudo add-apt-repository ppa:sdurobotics/ur-rtdesudo apt-get updatesudo apt install librtde librtde-dev

如果只是想要简单的使用python编程控制

pip install --user ur_rtde

2)python简单使用例子

import rtde_controlrtde_c = rtde_control.RTDEControlInterface(\"127.0.0.1\")rtde_c.moveL([-0.143, -0.435, 0.20, -0.001, 3.12, 0.04], 0.5, 0.3)

3)异步移动例子

import rtde_controlimport rtde_receiveimport timertde_c = rtde_control.RTDEControlInterface(\"127.0.0.1\")rtde_r = rtde_receive.RTDEReceiveInterface(\"127.0.0.1\")init_q = rtde_r.getActualQ()# Target in the robot basenew_q = init_q[:]new_q[0] += 0.20# Move asynchronously in joint space to new_q, we specify asynchronous behavior by setting the async parameter to# \'True\'. Try to set the async parameter to \'False\' to observe a default synchronous movement, which cannot be stopped# by the stopJ function due to the blocking behaviour.rtde_c.moveJ(new_q, 1.05, 1.4, True)time.sleep(0.2)# Stop the movement before it reaches new_qrtde_c.stopJ(0.5)# Target in the Z-Axis of the TCPtarget = rtde_r.getActualTCPPose()target[2] += 0.10# Move asynchronously in cartesian space to target, we specify asynchronous behavior by setting the async parameter to# \'True\'. Try to set the async parameter to \'False\' to observe a default synchronous movement, which cannot be stopped# by the stopL function due to the blocking behaviour.rtde_c.moveL(target, 0.25, 0.5, True)time.sleep(0.2)# Stop the movement before it reaches targetrtde_c.stopL(0.5)# Move back to initial joint configurationrtde_c.moveJ(init_q)# Stop the RTDE control scriptrtde_c.stopScript()

摘抄