依照ROS.org的教程来
melodic/Installation/Ubuntu - ROS Wiki
github: https://github.com/ros-industrial/universal_robot
mkdir -p ~/UR5_ws/src
cd ~/UR5_ws/src
catkin_init_workspace
git clone -b melodic-devel https://github.com/ros-industrial/universal_robot.git
cd ..
rosdep update
rosdep install --rosdistro melodic --ignore-src --from-paths src
catkin_make
source devel/setup.bash
cd ~/UR5_ws/src/universal_robot
rm -rf ur_driver
git clone https://github.com/ros-industrial/ur_modern_driver.git
用IDE打开ur_modern_driver中的ur_hardware_interface.cpp
将所有“controller_it->hardware_interface”替换成“controller_it->type”.
cd ~/UR5_ws
catkin_make
在universal/ur_modern_driver/src下新建一个CPP文件,命名为ur_test.cpp,将下面代码写入CPP里
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
//初始化,其中ur_test为节点名
ros::init(argc, argv, "ur_test");
//多线程
ros::AsyncSpinner spinner(1);
//开启新的线程
spinner.start();
//初始化需要使用move group控制的机械臂中的arm group
moveit::planning_interface::MoveGroupInterface arm("manipulator");
//允许误差
arm.setGoalJointTolerance(0.001);
//允许的最大速度和加速度
arm.setMaxAccelerationScalingFactor(0.2);
arm.setMaxVelocityScalingFactor(0.2);
// 控制机械臂先回到水平位置,这个位置下载的文件中已经设置好了
arm.setNamedTarget("home");
arm.move();
sleep(1);
//控制机械臂到达竖直位置,这个位置下载的文件中也设置好了
arm.setNamedTarget("up");
arm.move();
sleep(1);
// 控制机械臂先回到初始化位置
arm.setNamedTarget("home");
arm.move();
sleep(1);
ros::shutdown();
return 0;
}
Warnning:以上代码是我从其他文章复制的,效果是将机械臂水平放置,如果有干涉,请修改代码。也可以参考《ROS机器人开发实践》胡春旭 P325 "Moveit!编程学习" 编写python代码
然后打开ur_modern_driver文件的CMakelists.txt.
在19行,就是find_package()里的tf下面,添加
rospy
moveit_msgs
moveit_ros_perception
moveit_ros_planning_interface
178行添加
add_executable(ur_test src/ur_test.cpp)
target_link_libraries(ur_test ${catkin_LIBRARIES})
189行添加
intstall(TARGETS ur_driver ur_hardware_interface ur_test
重新编译工作空间
cd ~/UR5_ws
catkin_make
右上角加号:
设置内容,其中PC的地址为172.22.22.1,而UR5设置的地址是172.22.22.2。前面三个需要相同,使其保持在同一个局域网内,最后一位按照自己情况修改,不能PC和UR5同一地址。
检测电脑和UR5机械臂网络是否连接成功
ifconfig
测试连接情况
ping 172.22.22.2
time比较小就代表连接好了
cd ~/UR5_ws
roscore
#新终端
source devel/setup.bash
roslaunch ur_modern_driver ur5_bringup.launch limited:=true robot_ip:=172.22.22.2 use_lowbandwidth_trajectory_follower:=true
#新终端
source devel/setup.bash
roslaunch ur5_moveit_config ur5_moveit_planning_execution.launch limited:=true
#新终端
source devel/setup.bash
roslaunch ur5_moveit_config moveit_rviz.launch config:=true
#新终端
source devel/setup.bash
rosrun ur_modern_driver ur_test
感谢:作者:I LYXQ-V M 编程芝士 《ROS机器人开发实践》胡春旭