适合初学者的ROS机器人教程(3): ROS下使用Python对UR5机器人建模与控制

作者:知乎@Ai酱

  1. 安装UR机器人ROS包
    $ sudo apt-get install ros-kinetic-universal-robot
  2. 查询下看看有哪些包是和UR机器相关
    $ sudo rospack list | grep ur
    ur5_moveit_config /opt/ros/kinetic/share/ur5_moveit_config moveit!配置功能包
    ur_description /opt/ros/kinetic/share/ur_description UR机器模型包
    ur_gazebo /opt/ros/kinetic/share/ur_gazebo UR机器人仿真包
    ur_kinematics /opt/ros/kinetic/share/ur_kinematics  UR机器人运动学求解
    
  3. 在Gazebo中启动UR5机器人
    $ roslaunch ur_gazebo ur5.launch
  4. 了解一个机器人首先得了解怎么发送命令控制它。在ROS中是用过topic和service这两种通信机制实现发送命令和接收数据。topic是单方面通信,就是我只管发不管收,或只管收不发东西。service这种是双向通信,我既可以发给你,你也可以反馈信息给我。他们通信的格式叫做message(不是我们理解的消息),这是指消息的格式。
  5. 查看下UR机器人给我们提供了那些topic通信接口。
~$ rostopic list
/arm_controller/command
/arm_controller/follow_joint_trajectory/cancel
/arm_controller/follow_joint_trajectory/feedback
/arm_controller/follow_joint_trajectory/goal
/arm_controller/follow_joint_trajectory/result
/arm_controller/follow_joint_trajectory/status
/arm_controller/state
/calibrated
/clock
/gazebo/link_states
/gazebo/model_states
/gazebo/parameter_descriptions
/gazebo/parameter_updates
/gazebo/set_link_state
/gazebo/set_model_state
/gazebo_gui/parameter_descriptions
/gazebo_gui/parameter_updates
/joint_states
/rosout
/rosout_agg
/tf
/tf_static

使用MoveIt控制UR5机器人参见:https://blog.csdn.net/varyshare/article/details/89212971
运行UR5机器人三行命令,每行命令都是得在一个新的独立terminal命令行终端里面执行。

roslaunch ur_gazebo ur5.launch limited:=true
roslaunch ur5_moveit_config ur5_moveit_planning_execution.launch sim:=true limited:=true
roslaunch ur5_moveit_config moveit_rviz.launch config:=true

rqt可视化非常有用:启动rqt命令 rosrun rqt_gui rqt_gui,rqt简直是ROS中的J20啊,太有用了。不知道下载的包各消息接口的时候可以用rqt可以可视化的显示消息而且还可以监听消息。不知道现在运行的节点有哪些,它也可以可视化显示。

UR5有用的一些消息接口

获取机械臂速度和位置信息监听topic:/arm_controller/state
获取机械臂位置和姿态、角速度和速度监听topic:/gazebo/link_states。pose是位姿,twist是线速度角速度。
能获取意味着能发送命令控制它。
还有可以获取位置和线速度方法:joint_states

怎么编程控制UR5机器人

首先把安装的包都加入到catkin工作空间
catkin config --extend /opt/ros/kinetic

catkin 命令不存在?请尝试这个安装catkin sudo apt-get install ros-kinetic-catkin python-catkin-tools
python控制UR5机器人随机转动参考:https://github.com/vfdev-5/move_group_tutorial_ur5

#!/usr/bin/python
# Gazebo UR5
# Send joint values to UR5 using messages
#

from std_msgs.msg import Header
from trajectory_msgs.msg import JointTrajectory
from math import *
from random import uniform
from trajectory_msgs.msg import JointTrajectoryPoint
import rospy
# [0.0, -pi/2, pi/2, pi/3, 0, -pi/10]
# waypoints = [[uniform(-pi, pi) for _ in range(0,6)], [0,0,0,0,0,0]]

def main():

    rospy.init_node('send_joints')
    pub = rospy.Publisher('/arm_controller/command',
                          JointTrajectory,
                          queue_size=10)


    # Create the topic message
    traj = JointTrajectory()
    traj.header = Header()
    # Joint names for UR5
    traj.joint_names = ['shoulder_pan_joint', 'shoulder_lift_joint',
                        'elbow_joint', 'wrist_1_joint', 'wrist_2_joint',
                        'wrist_3_joint']

    rate = rospy.Rate(1)
    cnt = 0
    pts = JointTrajectoryPoint()
    traj.header.stamp = rospy.Time.now()

    while not rospy.is_shutdown():

        pts.positions = [uniform(0,pi),uniform(0,-pi/2),uniform(0,pi),uniform(0,pi),uniform(0,pi),uniform(0,pi)]
        pts.time_from_start = rospy.Duration(1.0)
        cnt+=1
        cnt%=2
        # Set the points to the trajectory
        traj.points = []
        traj.points.append(pts)
        # Publish the message
        pub.publish(traj)
        rate.sleep()

if __name__ == '__main__':
    try:
        main()
    except rospy.ROSInterruptException:
        print ("Program interrupted before completion")

MoveIt!工作机制

所有的接口都是和名字叫做move_group的这个节点通信。
机械臂关节点信息是通过Robot Sensors这个节点获取。
在这里插入图片描述

知乎 https://www.zhihu.com/people/yuanmuou/activities
原文地址:https://www.cnblogs.com/ailitao/p/11047310.html