ROS/ROS2Foxy

Xacro to reduce the code of URDF

leohycho 2022. 6. 29. 13:17

Namespace :

<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro"  name="my_robot">

Usage :

# Constants

   1 <xacro:property name="width" value="0.2" />
   2 <xacro:property name="bodylen" value="0.6" />
   3 <link name="base_link">
   4     <visual>
   5         <geometry>
   6             <cylinder radius="${width}" length="${bodylen}"/>
   7         </geometry>
   8         <material name="blue"/>
   9     </visual>
  10     <collision>
  11         <geometry>
  12             <cylinder radius="${width}" length="${bodylen}"/>
  13         </geometry>
  14     </collision>
  15 </link>

means 

   1   <link name="base_link">
   2     <visual>
   3       <geometry>
   4         <cylinder length="0.6" radius="0.2"/>
   5       </geometry>
   6       <material name="blue"/>
   7     </visual>
   8     <collision>
   9       <geometry>
  10         <cylinder length="0.6" radius="0.2"/>
  11       </geometry>
  12     </collision>
  13   </link>

 

# Simple Math

 <link name="${witdth*bodylen}"/>

means

 <link name="0.12"/>

 

# Macro

Simple Macro

   1 <xacro:macro name="default_origin">
   2     <origin xyz="0 0 0" rpy="0 0 0"/>
   3 </xacro:macro>
   4 <xacro:default_origin />

means

   1 <origin rpy="0 0 0" xyz="0 0 0"/>

 

Parameterized Macro

   1     <xacro:macro name="default_inertial" params="mass">
   2         <inertial>
   3                 <mass value="${mass}" />
   4                 <inertia ixx="1.0" ixy="0.0" ixz="0.0"
   5                      iyy="1.0" iyz="0.0"
   6                      izz="1.0" />
   7         </inertial>
   8     </xacro:macro>

   1 <xacro:default_inertial mass="10"/>

means

   2         <inertial>
   3                 <mass value="10" />
   4                 <inertia ixx="1.0" ixy="0.0" ixz="0.0"
   5                      iyy="1.0" iyz="0.0"
   6                      izz="1.0" />
   7         </inertial>

 

Parameterized block Macro

   1 <xacro:macro name="blue_shape" params="name *shape">
   2     <link name="${name}">
   3         <visual>
   4             <geometry>
   5                 <xacro:insert_block name="shape" />
   6             </geometry>
   7             <material name="blue"/>
   8         </visual>
   9         <collision>
  10             <geometry>
  11                 <xacro:insert_block name="shape" />
  12             </geometry>
  13         </collision>
  14     </link>
  15 </xacro:macro>
  16 
  17 <xacro:blue_shape name="base_link">
  18     <cylinder radius=".42" length=".01" />
  19 </xacro:blue_shape>

means

   2     <link name="base_link">
   3         <visual>
   4             <geometry>
   5                 <cylinder radius=".42 lenghth=".01" />
   6             </geometry>
   7             <material name="blue"/>
   8         </visual>
   9         <collision>
  10             <geometry>
  11                 <cylinder radius=".42 lenghth=".01" />
  12             </geometry>
  13         </collision>
  14     </link>   

 

Examples : 

# Definition of xacro

    <xacro:macro name="inertial_box" params="mass x y z *origin">
        <inertial>
            <xacro:insert_block name="origin"/>
            <mass value="${mass}" />
            <inertia ixx="${(1/12) * mass * (y*y+z*z)}" ixy="0.0" ixz="0.0"
                    iyy="${(1/12) * mass * (x*x+z*z)}" iyz="0.0"
                    izz="${(1/12) * mass * (x*x+y*y)}" />
        </inertial>
    </xacro:macro>

# Usage of xacro

    <link name="slider_link">
        <visual>
            <origin xyz="0 0 0.075" rpy="0 0 0"/>
            <geometry>
                <box size="0.5 0.25 0.15" />
            </geometry>
            <material name="blue" />
        </visual>
        <collision>
            <origin xyz="0 0 0.075" rpy="0 0 0"/>
            <geometry>
                <box size="0.5 0.25 0.15" />
            </geometry>
        </collision>
        <xacro:inertial_box mass="0.5" x="0.5" y="0.25" z="0.15">
            <origin xyz="0 0 0.075" rpy="0 0 0"/>
        </xacro:inertial_box>
    </link>

 

 

 

Reference

[1] http://wiki.ros.org/urdf/Tutorials/Using%20Xacro%20to%20Clean%20Up%20a%20URDF%20File

 

urdf/Tutorials/Using Xacro to Clean Up a URDF File - ROS Wiki

Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. Using Xacro to Clean Up a URDF File Descriptio

wiki.ros.org

[2] https://github.com/ros/xacro/wiki

 

GitHub - ros/xacro: Xacro is an XML macro language. With xacro, you can construct shorter and more readable XML files by using m

Xacro is an XML macro language. With xacro, you can construct shorter and more readable XML files by using macros that expand to larger XML expressions. - GitHub - ros/xacro: Xacro is an XML macro ...

github.com

 

 

'ROS > ROS2Foxy' 카테고리의 다른 글

Launch arguments for a launch file in ROS2  (0) 2022.07.21
Examples of launch.py  (0) 2022.07.01
ROS2 Multi-Machines for RPi4B and WSL2  (0) 2022.06.23
URDF using gazebo  (0) 2022.06.17
URDF using xacro  (0) 2022.06.17