適合初學者的ROS機器人教程(3): ROS下使用Python對UR5機器人建模與控制

做者:知乎@Ai醬node

  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命令行終端裏面執行。python

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能夠可視化的顯示消息並且還能夠監聽消息。不知道如今運行的節點有哪些,它也能夠可視化顯示。git

UR5有用的一些消息接口

獲取機械臂速度和位置信息監聽topic:/arm_controller/state
獲取機械臂位置和姿態、角速度和速度監聽topic:/gazebo/link_states。pose是位姿,twist是線速度角速度。
能獲取意味着能發送命令控制它。
還有能夠獲取位置和線速度方法:joint_statesgithub

怎麼編程控制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_ur5app

#!/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這個節點獲取。
在這裏插入圖片描述dom

相關文章
相關標籤/搜索