ROS中gazebo工具学习(使用gazebo加载机器人模型)

gazebo是ROS中的一个实现物理仿真的工具包,gazebo本身就是一款机器人的仿真软件,基于ODE的物理引擎,可以模拟机器人以及环境中的很多物理特性。
注意,gazebo和rviz是不同的工具包,不要混淆。

类似于rviz工具,在gazebo工具中也可以加载机器人模型。
加载的步骤:
(1)安装gazebo工具包
(2)新建工程,将包的路径位置加入到环境变量ROS_PACKAGE_PATH中
(3)新建.xacro文件并编辑内容
(4)新建.world文件并编辑内容
(5)新建.launch文件并编辑内容
(6)使用命令显示

其中.xacro文件包括机器人模型信息的文件,.world是gazebo环境地图文件,.launch为启动脚本。

(1)安装gazebo工具包

sudo apt-get install ros-hydro-gazebo-ros-pkgs ros-hydro-gazebo-roscontrol
   
   
   
   
  • 1

安装成功
运行

rosrun gazebo_ros gazebo
   
   
   
   
  • 1

打开gazebo工具界面
如图
这里写图片描述

(2)新建工程,将包的路径位置加入到环境变量ROS_PACKAGE_PATH中

a)新建工程
在我的目录/root/dev/workspace/下

roscreate-pkg gazebotest urdf xacro
   
   
   
   
  • 1

b)增加环境变量

(3)新建.xacro文件并编辑内容

a)新建文件夹urdf
mkdir urdf
b)在urdf文件夹下编辑robot1_xacro.xacro文件如下



<robot xmlns:xacro="http://www.ros.org/wiki/xacro" 
    xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor"
        xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller"
        xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface"
    name="robot1_xacro">


    <xacro:property name="length_wheel" value="0.05" />
    <xacro:property name="radius_wheel" value="0.05" />
    <xacro:macro name="default_inertial" params="mass">
               <inertial>
                       <mass value="${mass}" />
                       <inertia ixx="1.0" ixy="0.0" ixz="0.0"
                                iyy="1.0" iyz="0.0"
                                izz="1.0" />
               inertial>
    xacro:macro>

    <link name="base_footprint">
        <visual>
            <geometry>
                    <box size="0.001 0.001 0.001"/>
                geometry>
            <origin rpy="0 0 0" xyz="0 0 0"/>
        visual>
        <xacro:default_inertial mass="0.0001"/>
    link>

    <gazebo reference="base_footprint">
        <material>Gazebo/Greenmaterial>
        <turnGravityOff>falseturnGravityOff>
    gazebo>

    <joint name="base_footprint_joint" type="fixed">
        <origin xyz="0 0 0" />
        <parent link="base_footprint" />
        <child link="base_link" />
    joint>


    <link name="base_link">
        <visual>
            <geometry>
                    <box size="0.2 .3 .1"/>
                geometry>
            <origin rpy="0 0 1.54" xyz="0 0 0.05"/>
            <material name="white">
                <color rgba="1 1 1 1"/>
            material>
        visual>
        <collision>
            <geometry>
                    <box size="0.2 .3 0.1"/>
            geometry>
        collision>
        <xacro:default_inertial mass="10"/>
    link>

    <link name="wheel_1">
        <visual>
                <geometry>
                    <cylinder length="${length_wheel}" radius="${radius_wheel}"/>
                geometry>
            
            <origin rpy="0 0 0" xyz="0 0 0"/>
            <material name="black">
                <color rgba="0 0 0 1"/>
            material>
        visual>
        <collision>
            <geometry>
                    <cylinder length="${length_wheel}" radius="${radius_wheel}"/>
            geometry>
        collision>
        <xacro:default_inertial mass="1"/>
    link>

    <link name="wheel_2">
        <visual>
                <geometry>
                    <cylinder length="${length_wheel}" radius="${radius_wheel}"/>
                geometry>
            
            <origin rpy="0 0 0" xyz="0 0 0"/>
            <material name="black"/>
        visual>
        <collision>
            <geometry>
                    <cylinder length="${length_wheel}" radius="${radius_wheel}"/>
            geometry>
        collision>
        <xacro:default_inertial mass="1"/>

    link>

    <link name="wheel_3">
        <visual>
                <geometry>
                    <cylinder length="${length_wheel}" radius="${radius_wheel}"/>
                geometry>
            

            <origin rpy="0 0 0" xyz="0 0 0"/>
            <material name="black"/>
        visual>
        <collision>
            <geometry>
                    <cylinder length="${length_wheel}" radius="${radius_wheel}"/>
            geometry>
        collision>
        <xacro:default_inertial mass="1"/>
    link>

    <link name="wheel_4">
        <visual>
                <geometry>
                    <cylinder length="${length_wheel}" radius="${radius_wheel}"/>
                geometry>
        
            <origin rpy="0 0 0" xyz="0 0 0" />
            <material name="black"/>
        visual>
        <collision>
            <geometry>
                    <cylinder length="${length_wheel}" radius="${radius_wheel}"/>
            geometry>
        collision>
        <xacro:default_inertial mass="1"/>

    link>



 <joint name="base_to_wheel1" type="continuous">
   <parent link="base_link"/>
   <child link="wheel_1"/>
   <origin rpy="1.5707 0 0" xyz="0.1 0.15 0"/>
   <axis xyz="0 0 1" />
 joint>

 <joint name="base_to_wheel2" type="continuous">
   <axis xyz="0 0 1" />
   <anchor xyz="0 0 0" />
   <limit effort="100" velocity="100" />
   <parent link="base_link"/>
   <child link="wheel_2"/>
   <origin rpy="1.5707 0 0" xyz="-0.1 0.15 0"/>
joint>

 <joint name="base_to_wheel3" type="continuous">
   <parent link="base_link"/>
   <axis xyz="0 0 1" />
   <child link="wheel_3"/>
   <origin rpy="1.5707 0 0" xyz="0.1 -0.15 0"/>
 joint>

 <joint name="base_to_wheel4" type="continuous">
   <parent link="base_link"/>
   <axis xyz="0 0 1" />
   <child link="wheel_4"/>
   <origin rpy="1.5707 0 0" xyz="-0.1 -0.15 0"/>
 joint>
robot>
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165

可以看到标签设置了物理属性如颜色为绿色

(4)新建.world文件并编辑内容

a)新建文件夹world
mkdir world
b)在world 文件夹下新建并编辑robot.world文件
如下


<sdf version="1.4">
  

  <world name="default">
    <include>
      <uri>model://ground_planeuri>
    include>

    
    <include>
      <uri>model://sunuri>
    include>

    
    <gui fullscreen='0'>
      <camera name='user_camera'>
        <pose>4.927360 -4.376610 3.740080 0.000000 0.275643 2.356190pose>
        <view_controller>orbitview_controller>
      camera>
    gui>

  world>
sdf>
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

发现文件的参数就是配置些灯光视角参数。

(5)新建.launch文件并编辑内容

新建lauch文件夹并新建gazebo.lauch如下


<launch>

  
  <arg name="paused" default="true"/>
  <arg name="use_sim_time" default="false"/>
  <arg name="gui" default="true"/>
  <arg name="headless" default="false"/>
  <arg name="debug" default="true"/>

  
   <include file="$(find gazebo_ros)/launch/empty_world.launch"> 
     <arg name="world_name" value="$(find gazebotest)/worlds/robot.world"/>
    <arg name="debug" value="$(arg debug)" />
    <arg name="gui" value="$(arg gui)" />
    <arg name="paused" value="$(arg paused)"/>
    <arg name="use_sim_time" value="$(arg use_sim_time)"/>
    <arg name="headless" value="$(arg headless)"/>
  include>

  
  <arg name="model" />
  <param name="robot_description" 
     command="$(find xacro)/xacro.py $(arg model)" />

  
   <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
    args="-urdf -model robot1 -param robot_description -z 0.05"/> 

launch>
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

注意着行代表表示加载的.world文件位置,(find gazebotest)返回的是包的绝对路径。

"world_name" value="$(find gazebotest)/worlds/robot.world"/>
   
   
   
   
  • 1

(6)使用命令显示

使用如下命令

roslaunch gazebotest gazebo.launch model:="$(rospack find gazebotest)/urdf/robot1_xacro.xacro"
//或者使用绝对路径命令
roslaunch gazebotest gazebo.launch model:="/root/dev/workspace/gazebotest/urdf/robot1_xacro.xacro"
   
   
   
   
  • 1
  • 2
  • 3

效果如图
这里写图片描述

你可能感兴趣的:(ROS中gazebo工具学习(使用gazebo加载机器人模型))