ros初探xacro

xacro模型文件

xacro裏面的模型仍然是urdf模型,但是從整個模型的管理上發生了很大的變化
作用
1.精簡模型代碼
a、創建宏定義
b、文件包含

2.提供可編程接口
a、常量
b、變量
c、數學計算
d、條件語句

xacro使用方法

常量定義

<xacro:property name="M_PI" value="3.14159"/>

定義標籤:xacro:property 後面跟兩個參數屬性 :1、name 2、value
name是想定義的常量名
value是常量值

常量使用

<origin xyz="0 0 0" rpy="${M_PI/2} 0 0"/>

origin關鍵字;
使用常量 ${ } 在括號裏使用常量 ,在括號裏面可以進行運算

數學計算

<origin xyz="0 ${(motor_length+wheel_length)/2} 0" rpy="0 0 0"/>

在括號裏面可以進行運算

注意:所有數學運算都會轉換成浮點數進行,以保證運算精度

宏定義

<xacro:macro name="name" params="A B C">
   ......具體模型定義(類似函數內容)
</xacro:macro>

定義標籤:xacro:macro 後面跟兩個參數屬性 :1、name 2、params
name:宏定義的名字類似函數名
params:類似函數參數,可以是字符串

宏調用

<name A="A_value" B="B_value" C="C_value" />

文件包含

<xacro:include filename="$(find mbot_descripiton)/urdf/xacro/mbot_base.xacro" />

定義標籤:xacro:include
$(find+功能包)=包的具體路徑

模型顯示

1.方法一:(不常用)
將xacro文件轉化成URDF文件後顯示

$rosrun xacro xacro.py mbot.xacro>mbot.urdf

2.方法二: (常用)
直接調用xacro文件解析器

	<arg name="model" default="$(find xacro)/xacro --inorder '$(find mbot_description)/urdf/xacro/mbot.xacro'" />
	<arg name="gui" default="true" />

	<param name="robot_description" command="$(arg model)" />
	

實戰解讀代碼

<?xml version="1.0" ?>
<robot name="fourwheelrobot" xmlns:xacro="http://www.ros.org/wiki/xacro">
 <!--Defineing namespace-->
 
  <!--property list --> <!--常量定義-->
  <xacro:property name="M_PI" value="3.141592"/>
  <xacro:property name="base_mass"   value="20" /> 
  <xacro:property name="base_length" value="0.80"/>
  <xacro:property name="base_width" value="0.550"/>
  <xacro:property name="base_height" value="0.30"/>
 
  <xacro:property name="wheel_mass"   value="2" />
  <xacro:property name="wheel_radius" value="0.122"/>
  <xacro:property name="wheel_length" value="0.062"/>
  <xacro:property name="wheel_joint_x" value="0.25"/>
  <xacro:property name="wheel_joint_y" value="0.306"/>
  <xacro:property name="wheel_joint_z" value="-0.1"/> 
 
  <!--Defineing the colors--> <!--顏色定義-->
  <material name="black">
    <color rgba="0 0 0 1"/>
  </material>
  <material name="yellow">
    <color rgba="1 0.4 0 1"/>
  </material>
 
  <!-- Macro for inertia matrix --> <!--宏定義了慣性矩陣-->
   <xacro:macro name="cylinder_inertial_matrix" params="m r h">
     <inertial>
        <mass value="${m}" /> <!--由於慣性矩陣以對角線對稱的-->
        <inertia ixx="${m*(3*r*r+h*h)/12}" ixy = "0" ixz = "0"
           iyy="${m*(3*r*r+h*h)/12}" iyz = "0"
           izz="${m*r*r/2}" /> 
        </inertial>
    </xacro:macro>
 
    <xacro:macro name="box_inertial_matrix" params="m a b c">
       <inertial>
          <mass value="${m}" />
           <inertia ixx="${m*(b*b+c*c)/12}" ixy = "0" ixz = "0"
              iyy="${m*(c*c+a*a)/12}" iyz = "0"
              izz="${m*(a*a+b*b)/12}" /> 
       </inertial>
    </xacro:macro>
 
  
  <!--Macro for the wheel --> <!--定義輪子-->
  <xacro:macro name="wheel" params="prefix1 prefix2 reflect1 reflect2">
     <joint name="${prefix1}_${prefix2}_wheel_joint" type="continuous">
       <origin xyz="${reflect1*wheel_joint_x} ${reflect2*wheel_joint_y} ${wheel_joint_z}"/>
       <parent link="base_link"/>
       <child link="${prefix1}_${prefix2}_wheel_link"/>
       <axis xyz="0 1 0"/>
     </joint>
 
     <link name="${prefix1}_${prefix2}_wheel_link">
       <visual>
         <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
         <geometry>
            <cylinder radius="${wheel_radius}" length="${wheel_length}"/>
         </geometry>
         <material name="black"/>
      </visual>
      <collision>
         <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
           <geometry>
               <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
           </geometry>
      </collision>
      <cylinder_inertial_matrix  m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
     </link>
    
     <gazebo reference="${prefix1}_${prefix2}_wheel_link">
       <material>Gazebo/Black</material>
     </gazebo>
 
  <!-- Transmission is important to link the joints and the controller -->
     <transmission name="${prefix1}_${prefix2}_wheel_joint_trans">
         <type>transmission_interface/SimpleTransmission</type>
         <joint name="prefix1}_${prefix2}_wheel_joint" >
             <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
         </joint>
         <actuator name="${prefix1}_${prefix2}_wheel_joint_motor">
             <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
             <mechanicalReduction>1</mechanicalReduction>
         </actuator>
      </transmission>
  </xacro:macro>
 
  <!--start -->
   <xacro:macro name="fourwheelrobot_base_gazebo">
  <link name="base_footprint">
     <visual>
        <origin xyz="0 0 0" rqy="0 0 0"/>
         <geometry>
            <box size="0.001 0.001 0.001"/>
         </geometry>
     </visual>
   </link>
 
  <gazebo reference="base_footprint">
     <turnGravityOff>false</turnGravityOff>
  </gazebo>
 
 
  <joint name="base_footprint_joint" type="fixed">
            <origin xyz="0 0 ${base_height/2}" rpy="0 0 0" />        
            <parent link="base_footprint"/>
            <child link="base_link" />
   </joint> 
 
  <link name="base_link">
    <visual>
      <origin xyz="0 0 0" rqy="0 0 0"/>
      <geometry>
        <box size="${base_length} ${base_width} ${base_height}"/>
      </geometry>
      <material name="yellow"/>
    </visual>
   <collision>
      <origin xyz=" 0 0 0" rpy="0 0 0" />
      <geometry>
        <box size="${base_length} ${base_width} ${base_height}"/>
      </geometry>
   </collision>   
   <box_inertial_matrix  m="${base_mass}" a="${base_length}" b="${base_width}"  c="${base_height}" />
  </link>
 
  <gazebo reference="base_link">
     <material>Gazebo/Yellow</material>
  </gazebo>
    <wheel prefix1="left" prefix2="front" reflect1="1" reflect2="1"/>
    <wheel prefix1="left" prefix2="back" reflect1="-1" reflect2="1"/>
    <wheel prefix1="right" prefix2="front" reflect1="1" reflect2="-1"/>
    <wheel prefix1="right" prefix2="back" reflect1="-1" reflect2="-1"/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章