UR5 机械臂的环境配置与使用

使用的机械臂是优傲机器人(Universal Robots)公司的 UR5机械臂,代码部分使用的封装好的API是 RTDE(real time data exchange)

安装部分

在 linux 中安装 ur_rtde库 很简单只需要运行以下的命令即可

1
2
3
sudo add-apt-repository ppa:sdurobotics/ur-rtde
sudo apt-get update
sudo apt install librtde librtde-dev

CmakeLists 文件

安装 ur_rtde 之后,你可以使用 cmake 在自己的程序中使用这个库。在项目文件夹中新建 CMakeLists.txt 文件,内容如下:

1
2
3
4
5
6
7
# UR5 表示工程的名字
cmake_minimum_required(VERSION 3.5)
project(UR5)

find_package(ur_rtde REQUIRED)
add_executable(UR5 main.cpp)
target_link_libraries(UR5 PRIVATE ur_rtde::rtde)

电脑连接 UR5

要想使用 UR5 , 我们的保证电脑和UR5 在同一个局域网中。将机械臂段的网线连接到电脑。
根据如图所示,在机械臂的控制面板中机械臂设置网络 ip 为192.168.3.110,子网掩码为 255.255.255.0。在电脑的设置中网络中手动设置网络。
机械臂设置网络
电脑设置ip地址

需要保证 ip 的前三部分一样
在 shell 中运用 ping 命令查看机械臂的ip是否可达.

1
ping 192.168.3.110

实例程序

在当前的项目文件中新建 main.cpp文件,测试代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <ur_rtde/rtde_control_interface.h>
#include <thread>
#include <chrono>

using namespace ur_rtde;
using namespace std::chrono;

int main(int argc, char* argv[])
{
//注意修改为 机械臂的 ip 地址
RTDEControlInterface rtde_control("192.168.3.110");
if(rtde_control.isConnected()) {
std::cout << "连接成功"<<std::endl;
} else {
std::cout << "连接不成功"<<std::endl;
}
// 运行路径参数
std::vector<double> task_frame = {0, 0, 0, 0, 0, 0};
std::vector<int> selection_vector = {0, 0, 1, 0, 0, 0};
std::vector<double> wrench_down = {0, 0, -10, 0, 0, 0};
std::vector<double> wrench_up = {0, 0, 10, 0, 0, 0};
int force_type = 2;
double dt = 1.0/500; // 2ms
std::vector<double> limits = {2, 2, 1.5, 1, 1, 1};
std::vector<double> joint_q = {-1.54, -1.83, -2.28, -0.59, 1.60, 0.023};

// Move to initial joint position with a regular moveJ
rtde_control.moveJ(joint_q);

// Execute 500Hz control loop for a total of 4 seconds, each cycle is ~2ms
for (unsigned int i=0; i<2000; i++)
{
auto t_start = high_resolution_clock::now();
// First we move the robot down for 2 seconds, then up for 2 seconds
if (i > 1000)
rtde_control.forceMode(task_frame, selection_vector, wrench_up, force_type, limits);
else
rtde_control.forceMode(task_frame, selection_vector, wrench_down, force_type, limits);
auto t_stop = high_resolution_clock::now();
auto t_duration = std::chrono::duration<double>(t_stop - t_start);

if (t_duration.count() < dt)
{
std::this_thread::sleep_for(std::chrono::duration<double>(dt - t_duration.count()));
}
}

rtde_control.forceModeStop();
rtde_control.stopScript();

return 0;
}

编译连接程序,生成可执行文件。

1
2
3
pwd
cmake .
make

在 pwd 路径下此时会生成可执行文件,运行的效果如下

1
sudo ./UR5