xacro机器人模型文件转urdf文件怎么编写launch文件启动gazebo仿真和在rviz2内显示模型

urdf文件很直白,每个零件的  都要编写一遍,每个零件数据都要自己算出来结果,很麻烦,但是用起来很简单。xacro写的模型文件可以把好多常量提前定义出来,不同大小的机器人只要只要改一下常量,机器人模型就可以重新生成,代码可以复用,编写起来简单多了,但是编写launch启动文件麻烦一些。

urdf编写的小车模型文件:




  
  
    
      
        
      
      
        
      
    
    
    
      
        
      
    
    
      
      
      
    
  
  
  
  
  
    
    
    
  
  
    
      
      
        
      
      
        
      
    
    
    
      
      
        
      
    
    
      
      
      
    
  
  
  
    
    
    
    
  
  
    
      
      
        
      
      
        
      
    
    
    
      
      
        
      
    
    
      
      
      
    
  
  
  
    
    
    
    
  
  
  
    
      
        
      
      
        
      
    
    
    
      
      
        
      
    
    
      
      
    
  
  
  
    Gazebo/Black
  
  
  
    
    
    
    
  
  
  
    
    
    
  
  
  
    
      
        
      
    
    
    
      
        
      
    
    
      
      
      
    
  
  
  
    
    
    
  
  
  
    
      
        
          
          
          ~/out:=imu
        
        
        false
      
      true
      
      100
      true
      
        
        
          
            
              0.0
              2e-4
              0.0000075
              0.0000008
            
          
          
            
              0.0
              2e-4
              0.0000075
              0.0000008
            
          
          
            
              0.0
              2e-4
              0.0000075
              0.0000008
            
          
        
        
        
          
            
              0.0
              1.7e-2
              0.1
              0.001
            
          
          
            
              0.0
              1.7e-2
              0.1
              0.001
            
          
          
            
              0.0
              1.7e-2
              0.1
              0.001
            
          
        
      
    
  
  
  
    
      
        
        
      
      
      left_wheel_joint
      right_wheel_joint
      
      0.52
      
      0.155
      
      
      20
      1.0
      
      
      true
      
      true
      
      true
      
      odom
      
      base_link
    
  
  
  
    
      
      
        
      
    
    
    
      
      
      
    
    
    
      
      
        
      
    
  
  
  
    
    
    
  
  
    
      true
      false
      5
      
        
          
            360
            1.000000
            0.000000
            6.280000
          
        
        
          0.120000
          3.5
          0.015000
        
        
          gaussian
          0.0
          0.01
        
      
      
        
          ~/out:=scan
        
        sensor_msgs/LaserScan
        laser
      
    
  
  
  
    
      
      
        
      
    
    
    
      
      
        
      
    
    
    
      
      
      
    
  
  
  
    
    
    
  
  
  
  
    
    
    
  
  
  
    
      true
      30.0
      
        1.047198
        
          640
          480
          R8G8B8
        
        
          0.05
          3
        
      
      
        0.2
        true
        0.0
        camera_depth_frame
        0.5
        3.0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
      
    
  

xacro编写的小车模型文件:





  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

  
  
    
      
      
      
    
  
  
  
    
      
      
      
    
  
  
  
    
      
      
    
  

  
  
    
      
        
      
      
        
      
    
    
    
      
        
      
    
    
    
  

  
  
  
  
    
    
    
  

  
  
    
      
        
        
          
        
        
          
        
      
      
      
        
        
          
        
      
      
      
    
    
    
      
      
      
      
    
  
  
  
  

  
  
    
      
        
      
      
        
      
    
    
    
      
      
        
      
    
    
    
  
  
  
    Gazebo/Black
  
  
  
    
    
    
    
  
  
  
    
    
    
  
  
  
    
      
        
      
    
    
    
      
        
      
    
    
    
  
  
  
    
    
    
  
  
  
    
      
        
          
          
          ~/out:=imu
        
        
        true
      
      true
      
      100
      true
      
        
        
          
            
              0.0
              2e-4
              0.0000075
              0.0000008
            
          
          
            
              0.0
              2e-4
              0.0000075
              0.0000008
            
          
          
            
              0.0
              2e-4
              0.0000075
              0.0000008
            
          
        
        
        
          
            
              0.0
              1.7e-2
              0.1
              0.001
            
          
          
            
              0.0
              1.7e-2
              0.1
              0.001
            
          
          
            
              0.0
              1.7e-2
              0.1
              0.001
            
          
        
      
    
  
  
  
    
      
        
        
      

      
      left_wheel_joint
      right_wheel_joint

      
      ${base_width+wheel_width}
      
      ${wheel_radius*2}
      

      
      20
      1.0

      
      
      true
      
      true
      
      true

      
      odom
      
      base_link
    
  
  
  
    
      
      
        
      
    
    
    
      
      
      
    
    
    
      
      
        
      
    
  
  
  
    
    
    
  

  
    
      true
      false
      5
      
        
          
            360
            1.000000
            0.000000
            6.280000
          
        
        
          0.120000
          3.5
          0.015000
        
        
          gaussian
          0.0
          0.01
        
      
      
        
          ~/out:=scan
        
        sensor_msgs/LaserScan
        laser
      
    
  
  
  
    
      
      
        
      
    
    
    
      
      
        
      
    
    
    
      
      
      
    
  
  
  
    
    
    
  
  
  

  
    
    
    
  
  
  
    
      true
      30.0
      
        1.047198
        
          640
          480
          R8G8B8
        
        
          0.05
          3
        
      
      
        0.2
        true
        0.0
        camera_depth_frame
        0.5
        3.0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
      
    
  

xacro模型文件需要转成urdf模型文件才能使用

方法1 提前转换:

当前文件夹打开终端输入:base.urdf.xacro > base.urdf生成纯urdf文件。

方法2 在运行launch文件的时候自动转,需要加入xacro解析步骤

package.xml文件内在前加入:xacro

launch文件编写:

# 此launch文件是机器人仿真程序,包含 gazebo启动,机器人仿真生成,机器人模型状态发布
import os
from launch import LaunchDescription
from launch.actions import ExecuteProcess
from launch_ros.actions import Node
from launch_ros.substitutions import FindPackageShare
from launch.substitutions import LaunchConfiguration
import xacro

def generate_launch_description():
    robot_name_in_model = 'jtbot'         #机器人模型名字
    package_name = 'jtbot_description'    #模型包名

    ld = LaunchDescription()
    use_sim_time = LaunchConfiguration('use_sim_time', default='true')
    pkg_share = FindPackageShare(package=package_name).find(package_name) 
    gazebo_world_path = os.path.join(pkg_share, 'world/jt.world')        #世界仿真文件路径
    default_rviz_config_path = os.path.join(pkg_share, 'rviz/mrviz2.rviz')  #rviz配置文件路径
    urdf_xacro_file = os.path.join(pkg_share, 'urdf/jtbot_base.urdf.xacro') #xacro模型文件路径
    #解析xacro模型文件
    doc = xacro.parse(open(urdf_xacro_file))
    xacro.process_doc(doc)
    params = {'robot_description': doc.toxml()}

    # 开启ros Gazebo server
    start_gazebo_cmd = ExecuteProcess(
        cmd=['gazebo',
        '--verbose', 
        gazebo_world_path,
        '-s', 'libgazebo_ros_init.so',   
        '-s', 'libgazebo_ros_factory.so', 
         
         ],
        output='screen')

     # Start Robot State publisher
    start_robot_state_publisher_cmd = Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        output='screen',
        
        parameters=[params,{'use_sim_time': use_sim_time}]
    )    

    # gazebo内生成机器人
    spawn_entity_cmd = Node(
        package='gazebo_ros', 
        executable='spawn_entity.py',
        arguments=['-entity', robot_name_in_model,  '-topic', 'robot_description'], 
        output='screen'
      
        )
	
   

    # Launch RViz
    start_rviz_cmd = Node(
        package='rviz2',
        executable='rviz2',
        name='rviz2',
        output='screen',
        arguments=['-d', default_rviz_config_path]
        )

    ld.add_action(start_gazebo_cmd)
    ld.add_action(spawn_entity_cmd)
    ld.add_action(start_robot_state_publisher_cmd)
    ld.add_action(start_rviz_cmd)


    return ld

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