ros2 foxy gazebo中加载世界以及机器

ros2中gazebo加载机器人的方式与ros1中有很大不同,一共有三种方式加载模型.
#使用-file 打开sdf文件
#使用-database需要先将模型路径添加到GAZEBO_MODEL_PATH ‘export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:model_path’ 而且模型需要参照gazebo中的标准写法才能成功加载
#使用-topic订阅机器人话题 话题由robot_state_publisher发布 其打开的是urdf文件

最后打开gazebo的.py文件如下:

import os
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import ThisLaunchFileDir
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
from launch.actions import ExecuteProcess
from launch.substitutions import LaunchConfiguration

def generate_launch_description():
    gazebo_dir = os.path.join(get_package_share_directory('gazebo_ros'),'launch')
    # world = os.path.join(get_package_share_directory('gis_gazebo'), 'worlds', 'test.world')

    urdf = os.path.join(
        get_package_share_directory('gis_gazebo'),
        'urdf','gis_car.urdf')

    model_path = os.path.join(
        get_package_share_directory('gis_gazebo'),
        'models','pipe1.sdf')

    use_sim_time = LaunchConfiguration('use_sim_time', default=True) 

    return LaunchDescription([
        #退出gazebo时gzserver经常关不掉,所以打开gazebo前先关掉gzserver
        ExecuteProcess(
            cmd=['killall','-q', 'gzserver'],
            output='screen'),
        ExecuteProcess(
            cmd=['killall','-q', 'gzclient'],
            output='screen'),

        IncludeLaunchDescription(
            PythonLaunchDescriptionSource([gazebo_dir, '/gzserver.launch.py']),
            # launch_arguments={
            #     'world': world
            #     }.items(),
        ),
        

        IncludeLaunchDescription(
            PythonLaunchDescriptionSource([gazebo_dir ,'/gzclient.launch.py'])),


        #使用-file 打开sdf文件 
        #使用-database需要先将模型路径添加到GAZEBO_MODEL_PATH  'export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:model_path'  而且模型需要参照gazebo中的标准写法才能成功加载
        #使用-topic订阅机器人话题 话题由robot_state_publisher发布  其打开的是urdf文件
        Node(package='gazebo_ros', node_executable='spawn_entity.py',
                        arguments=[
                            '-x','0.0',
                            '-y','0.0',
                            '-z','2.0',
                            '-Y','0.0',  #yaw
                            '-entity', 'car',   #gazebo中机器命名
                            # '-database', 'cardboard_box',
                            # '-file',model_path,
                            '-topic','robot_description',
                            ],
                        output='screen'),

        Node(
            package='robot_state_publisher',
            node_executable='robot_state_publisher',
            node_name='robot_state_publisher',
            output='screen',
            parameters=[{'use_sim_time': use_sim_time},
                {'publish_frequency':100.0},
                ],
            arguments=[urdf]
            ),

    ])


其中 world模型可以先打开gazebo,随意创建模型后另存为world.

urdf文件如下:






 

 
     
       
     
     
       
     
   
  
   
      
       
      
   
  
   
     
     
   

 
 

  
     
      
     
          
     
   

   
      
       
     
   

   
     
     
   
  

 
 
     
      
     
          
     
   

   
      
       
     
   

   
     
     
   
  

 
  
     
      
     
          
     
   

   
      
       
     
   

   
     
     
   
  

 
  
     
      
     
          
     
   

   
      
       
     
   

   
     
     
   
  

 
  
  
  
  
 

 
  
  
  
  
 

 
  
  
  
  
 

 
  
  
  
  
 





  



  
  
  
  



  



  
  
  
  




    
      cmd_vel
      odom
      odom
      100.0
      base_link
      true
    


 
    true
    
      true
      100
      
      __default_topic__
      
        imu
        base_link
        100.0
        0.0
        0 0 0
        0 0 0
        imu_link
      
      0 0 0 0 0 0
    
  



    
    
    0 0 0 0 0 0 
    true
    20
    
      
        
          360
          1
          -2.093
          2.093
        
      

      
        0.3
        10
        0.01
      
      
          gaussian
          0.0
          0.005
      
    
    
    
    
      /scan
      scan_link
    

    
      
        /test
        joint_states:=joint_states_test
      
      100
      br_joint
      bl_joint
      fr_joint
      fl_joint
    


  







sdf文件如下:





   
      0 0 0 0 0 0

      
      
        0 0 0 0 0 0
        
          1.14395
          
            0.126164
            0
            0
            0.416519
            0
            0.481014
          
        
        
          
            
              2.4 1.2 2
            
          
        
        
          
            
              2.4 1.2 2
            
          
        
      

     
        -1 -0.6 -1 -1.5707 0 0
        
          2
          
            0.145833
            0
            0
            0.145833
            0
            0.125
          
        
        
          
            
              0.35
            
          
        
        
          
            
              0.35
            
          
          
            
              
                1
                1
                0
                0
              
            
            
              
                0
                0.2
                1e+13
                1
                0.01
                0.01
              
            
          
        
      

      
        -1 0.6 -1 -1.5707 0 0
        
          2
          
            0.145833
            0
            0
            0.145833
            0
            0.125
          
        
        
          
            
              0.35
            
          
        
        
          
            
              0.35
            
          
          
            
              
                1
                1
                0
                0
              
            
            
              
                0
                0.2
                1e+13
                1
                0.01
                0.01
              
            
          
        
      

      
        1 -0.6 -1 -1.5707 0 0
        
          2
          
            0.145833
            0
            0
            0.145833
            0
            0.125
          
        
        
          
            
              0.35
            
          
        
        
          
            
              0.35
            
          
          
            
              
                1
                1
                0
                0
              
            
            
              
                0
                0.2
                1e+13
                1
                0.01
                0.01
              
            
          
        
      

      
        1 0.6 -1 -1.5707 0 0
        
          2
          
            0.145833
            0
            0
            0.145833
            0
            0.125
          
        
        
          
            
              0.35
            
          
        
        
          
            
              0.35
            
          
          
            
              
                1
                1
                0
                0
              
            
            
              
                0
                0.2
                1e+13
                1
                0.01
                0.01
              
            
          
        
      

      
        1 0.6 -1 -1.5707 0 0
        
      

      
        base_footprint
        base_link
      0.0 0.0 0.00 0 0 0
      

      
        base_link
        fl_wheel        
      

      
        base_link
        fr_wheel     
      

      
    

    
    
        base_link
        bl_wheel
        
          0 0 1
          
            -1.79769e+308
            1.79769e+308
          
        
      

      
        base_link
        br_wheel
        
          0 0 1
          
            -1.79769e+308
            1.79769e+308
          
        
      
      
      
        base_link
        steering_wheel
        
          0 0 1
          
            -1.79769e+308
            1.79769e+308
          
        
      

     
        
          cmd_vel:=cmd_vel
          odom:=odom
        	

        br_wheel_joint
        bl_wheel_joint

        1.2
        0.7

        20
        1.0

        true
        true
        true
        
        odom
        odom
        base_footprint

      

      


      


         
      
          
           joint_states:=joint_states
          

          30
          br_wheel_joint
          bl_wheel_joint
      
     
      
        0 0 1.2 0 0 0
        
          
            
              0.2 0.2 0.2
            
          
        
        
          
            
              0.2 0.2 0.2
            
          
          100.0
        
         
      
        
          true
          true         
          20
          
            
              
                720
                0.5
                -3.1415
                3.1415
              
              
            
            
              0.2
              50.0	           
            
	          
              gaussian
              0.0
              0.01
            
              
          
            
              
              ~/out:=scan
            
            sensor_msgs/LaserScan
          

        
    


    
        base_link
        ray_link    
    

    
      
        true
        100
         
          
            
              
                0.0
                2e-4
              
            
            
              
                0.0
                2e-4
              
            
            
              
                0.0
                2e-4
              
            
          
          
            
              
                0.0
                1.7e-2
              
            
            
              
                0.0
                1.7e-2
              
            
            
              
                0.0
                1.7e-2
              
            
          
        
        
          
            ~/out:=imu
          
        
      
    

    
      base_link
      imu_link
      0.0 0 0.0 0 0 0
      
        0 0 1
      
    

    
      0 0 10 0 0 0
      
          true
          30
          
            
              
                
                  0.0
                  2e-4
                
              
              
                
                  0.0
                  2e-4
                
              
            
          
          
            
              ~/out:=gps_data
            
          
        
    
    
    
    
      base_link
      gps_link
    

     

    
 


你可能感兴趣的:(ros2,gazebo)