ros gazebo机械臂仿真,手动控制与MoveIt自动控制

本文总结归纳古月居胡春旭ros机械臂教程,给出了一些error的解决方法,补充了通过python运行moveit。十分建议去看github huchunxu源代码的repository。

创建机械臂的xacro模型

首先创建一个工作空间,在工作空间中创建arm_description功能包。功能包中创建一个urdf文件夹,放入机械臂模型文件arm.xacro




    
    
        
    

    
        
    

    
        
    

    
        
    

    
    

    
    
    

    
    
    

    
    
    

    
    
    

    
    
    

    
    
    

    
    
    
    

    
    
    
    

    
    

    
    
        
            
            
        
    

    
    
        
        
        
    
    
        
              
                  
                       
                  
              
        
        
            
            
                
            
        
        
    

    
    
        
            
            
                
            
            
        
        
            
            
                
            
        
        
    

    
        
        
        
        
        
        
    

    
    
        
            
            
                
            
            
        
        
            
            
                
            
        
        
    

    
        
        
        
        
        
        
    

    
    
        
            
            
                
            
            
        

        
            
            
                
            
        
        
    

    
        
        
        
        
        
        
    

    
    
        
            
            
                
            
            
        
        
            
            
                
            
        
        
    

    
        
        
        
        
        
        
    

    
    
        
            
            
                
            
            
        
        
            
            
                
            
        
        
    

    
        
        
        
        
        
        
    

    
    
        
            
            
                
            
            
        
        
            
            
                
            
        
        
    

    
        
        
        
        
        
        
    

    
    
        
            
            
                
            
            
        
        
            
            
                
            
        
        
    

    
        
        
        
        
        
        
    

    
    
    
        
            
            
                
            
            
        
        
    

    
        
        
        
    

    
    
        
            
            
                
            
            
        
        
    

    
    

    
      
      
      
    

    
    
        Gazebo/White
    
    
        Gazebo/White
    
    
        Gazebo/Blue
    
    
        Gazebo/White
    
    
        Gazebo/Blue
    
    
        Gazebo/Black
    
    
        Gazebo/White
    
    
        Gazebo/Blue
    
    
        Gazebo/White
    
    
        Gazebo/White
    
    
    
    
        
            transmission_interface/SimpleTransmission
            
                hardware_interface/PositionJointInterface
            
            
                hardware_interface/PositionJointInterface
                1
            
        
    
    
    
    
    
    
    
    
    

    
    
        
            /arm
        
    

Transmission for ROS Control那一部分相当于是给关节绑定上电机,通过控制电机控制关节运动。

再创建一个launch文件夹,创建view_arm.launch文件


    
    
    

    
    

    
    
    
    
    

    
    

运行这个launch文件,在rviz中选择fixed frame,再加载Robot Model,就可以看见机械臂了。

在Gazebo中手动控制机械臂

在工作空间中再创建一个arm_gazebo功能包,添加launch文件夹,创建arm_world.launch



  
  
  
  
  
  

  
  
    
    
    
    
    
  

  
   


  
   


功能是:创建一个环境,并且将机器人模型放进去。

下面要做的是加载控制器,首先要设定pid控制器的参数。在arm_gazebo功能包中创建config文件夹,创建arm_gazebo_control.yaml,给6个关节设定pid控制器参数。

arm:
  # Publish all joint states -----------------------------------
  joint_state_controller:
    type: joint_state_controller/JointStateController
    publish_rate: 50  
  
  # Position Controllers ---------------------------------------
  joint1_position_controller:
    type: position_controllers/JointPositionController
    joint: joint1
    pid: {p: 100.0, i: 0.01, d: 10.0}
  joint2_position_controller:
    type: position_controllers/JointPositionController
    joint: joint2
    pid: {p: 100.0, i: 0.01, d: 10.0}
  joint3_position_controller:
    type: position_controllers/JointPositionController
    joint: joint3
    pid: {p: 100.0, i: 0.01, d: 10.0}
  joint4_position_controller:
    type: position_controllers/JointPositionController
    joint: joint4
    pid: {p: 100.0, i: 0.01, d: 10.0}
  joint5_position_controller:
    type: position_controllers/JointPositionController
    joint: joint5
    pid: {p: 100.0, i: 0.01, d: 10.0}
  joint6_position_controller:
    type: position_controllers/JointPositionController
    joint: joint6
    pid: {p: 100.0, i: 0.01, d: 10.0}
 

在launch文件夹中创建arm_gazebo_controller.launch,实现的作用是加载刚才创建的pid参数yaml文件,创建控制器。



    
    

    
    

    
    
        
    



最后再创建一个arm_gazebo_control.launch文件,其作用是,运行world launch和controller launch两个launch文件。



    
       

    
       

运行arm_gazebo_control.launch文件,再另外打开两个terminal,分别运行

rqt
rosrun rviz rviz

通过rqt发送指令,可以在gazebo和rviz中同时看到机械臂的动作。

ros gazebo机械臂仿真,手动控制与MoveIt自动控制_第1张图片

配置MoveIt

rosrun moveit_setup_assistant moveit_setup_assistant

依次加载模型,生成碰撞矩阵,生成规划组,如下图所示,一个arm规划组和一个gripper规划组,注意“3”那里,新版本的moveit那里不一样,空着就好。

ros gazebo机械臂仿真,手动控制与MoveIt自动控制_第2张图片

ros gazebo机械臂仿真,手动控制与MoveIt自动控制_第3张图片 

ros gazebo机械臂仿真,手动控制与MoveIt自动控制_第4张图片 

ros gazebo机械臂仿真,手动控制与MoveIt自动控制_第5张图片

 添加一个初始位姿,命名为home

ros gazebo机械臂仿真,手动控制与MoveIt自动控制_第6张图片

 配置终端夹爪 ros gazebo机械臂仿真,手动控制与MoveIt自动控制_第7张图片

最后再添加作者信息,生成配置文件就可以了。命名为arm_moveit_config,和前面创建的两个功能包arm_description, arm_gazebo并列放在一起。

用moveIt控制gazebo中的机械臂

arm_gazebo功能包中创建关节轨迹控制器,一个控制器包含两个部分:yaml配置文件和launch运行文件,yaml配置文件放在功能包的config文件夹中,里面写着控制器的参数,运行文件放在launch文件夹中,用于运行控制器。

arm_moveit_config功能包中创建一个gazebo控制器,同样是一组.yaml和.launch文件(注意这个launch文件的后缀是.launch.xml,这个文件自动存在,不需要创建,我们要改内容),launch文件加载yaml文件,分别放在config文件夹和launch文件夹。以及另外一个单独的.launch(moveit_planning_execution.launch)。

moveit功能包中的控制器和gazebo功能包中的控制器的关系是:通过ros的Action机制,moveit功能包中的控制器发布关节轨迹点指令,gazebo功能包中的控制器控制关节电机是关节到达轨迹点。

还要在arm_gazebo功能包中创建一个关节状态控制器,用于发布关节状态和tf变换。这个是为了在rviz中可视化用。

总结:一共三个控制器7个文件(3个.yaml和4个.launch)

arm_gazebo功能包中的命名为:trajectory_control.yaml    arm_trajectory_controller.launch

arm_gazebo_joint_states.yaml      arm_gazebo_states.launch

arm_moveit_config功能包中的命名为:controllers_gazebo.yaml    arm_moveit_controller_manager.launch.xml

moveit_planning_execution.launch

以上文件的内容整理在后文。最后再创建一个.launch,其作用是一次性运行上面的所有.launch。命名为arm_bringup_moveit.launch,放在arm_gazebo功能包的launch文件夹。

运行最后写的那个.launch,就可以通过rviz中的moveit插件来控制gazebo中的机械臂了。

roslaunch marm_gazebo arm_bringup_moveit.launch

 trajectory_control.yaml 内容:

arm:
  arm_joint_controller:
    type: "position_controllers/JointTrajectoryController"
    joints:
      - joint1
      - joint2
      - joint3
      - joint4
      - joint5
      - joint6

    gains:
      joint1:   {p: 1000.0, i: 0.0, d: 0.1, i_clamp: 0.0}
      joint2:   {p: 1000.0, i: 0.0, d: 0.1, i_clamp: 0.0}
      joint3:   {p: 1000.0, i: 0.0, d: 0.1, i_clamp: 0.0}
      joint4:   {p: 1000.0, i: 0.0, d: 0.1, i_clamp: 0.0}
      joint5:   {p: 1000.0, i: 0.0, d: 0.1, i_clamp: 0.0}
      joint6:   {p: 1000.0, i: 0.0, d: 0.1, i_clamp: 0.0}


  gripper_controller:
    type: "position_controllers/JointTrajectoryController"
    joints:
      - finger_joint1
    gains:
      finger_joint1:  {p: 50.0, d: 1.0, i: 0.01, i_clamp: 1.0}

 arm_trajectory_controller.launch 内容:



    

    

arm_gazebo_joint_states.yaml 内容:

arm:
  # Publish all joint states -----------------------------------
  joint_state_controller:
    type: joint_state_controller/JointStateController
    publish_rate: 50  
  

arm_gazebo_states.launch 内容:


    
    

    

    
    
        
    



controllers_gazebo.yaml内容:

controller_manager_ns: controller_manager
controller_list:
  - name: arm/arm_joint_controller
    action_ns: follow_joint_trajectory
    type: FollowJointTrajectory
    default: true
    joints:
      - joint1
      - joint2
      - joint3
      - joint4
      - joint5
      - joint6

  - name: arm/gripper_controller
    action_ns: follow_joint_trajectory
    type: FollowJointTrajectory
    default: true
    joints:
      - finger_joint1
      - finger_joint2

arm_moveit_controller_manager.launch.xml内容:


  
  
  

  
  
  
  
  

moveit_planning_execution.launch内容:


 # The planning and execution components of MoveIt! configured to 
 # publish the current configuration of the robot (simulated or real)
 # and the current state of the world as seen by the planner
 
  
 
 # The visualization component of MoveIt!
 

  
  
     
    [/arm/joint_states]
  

arm_bringup_moveit.launch内容:


  
    
    

    
       

    
    

    
    

一些可能出现的报错:

Tried to advertise on topic but the topic is already advertised

解决办法:

arm_moveit_config功能包中,sensors_3d.yaml文件中,出现了重名topic,修改第二个topic的名称

ros gazebo机械臂仿真,手动控制与MoveIt自动控制_第8张图片

 Unable to identify any set of controllers that can actuate the specified joints:

解决办法:修改

工作空间/src/marm_moveit_config/launch/trajectory_execution.launch.xml

删掉 pass_all_args="true"

ros gazebo机械臂仿真,手动控制与MoveIt自动控制_第9张图片

通过python的接口运行moveit

上面是通过rviz中的插件来控制moveit,下面通过py文件来运行moveit,不需要在rviz中操作moveit插件。

还是先运行arm_bringup_moveit.launch

再运行这个py文件,group_name是在moveit_setup_assistant中配置的group,在joint_goal那里设定关节目标角度 弧度制。


import sys
import rospy
import moveit_commander
from moveit_commander import PlanningSceneInterface


moveit_commander.roscpp_initialize(sys.argv)
rospy.init_node('move_group_python_interface_tutorial', anonymous=True)

## Instantiate a `RobotCommander`_ object. Provides information such as the robot's
## kinematic model and the robot's current joint states
robot = moveit_commander.RobotCommander()

## Instantiate a `PlanningSceneInterface`_ object.  This provides a remote interface
## for getting, setting, and updating the robot's internal understanding of the
## surrounding world:
scene = moveit_commander.PlanningSceneInterface()

## Instantiate a `MoveGroupCommander`_ object.  This object is an interface
## to a planning group (group of joints).  In this tutorial the group is the primary
## arm joints in the Panda robot, so we set the group's name to "panda_arm".
## If you are using a different robot, change this value to the name of your robot
## arm planning group.
## This interface can be used to plan and execute motions:
group_name = "arm"
move_group = moveit_commander.MoveGroupCommander(group_name)
joint_goal = move_group.get_current_joint_values()
print(joint_goal)
joint_goal[0] = 0
joint_goal[1] = 0.9
joint_goal[2] = 0
joint_goal[3] = 0


# The go command can be called with joint values, poses, or without any
# parameters if you have already set the pose or joint target for the group
move_group.go(joint_goal, wait=True)

# Calling ``stop()`` ensures that there is no residual movement
move_group.stop()

## END_SUB_TUTORIAL

# For testing:
current_joints = move_group.get_current_joint_values()
print(current_joints)

如果是pycharm运行py文件,注意interpreter选择,我的虚拟机安装的是ubuntu18 和ros melodic,需要python2.7的interpreter,另外还要把melodic的python包放进pycharm的project structure。另外,在pycharm中添加两个环境变量。以上。

ros gazebo机械臂仿真,手动控制与MoveIt自动控制_第10张图片

你可能感兴趣的:(机器人)