How it works…

To begin with xacro, we have to specify a namespace so that the file is parsed properly, as shown in the following code snippet:

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

We must remember that the file must have an xacro extension in place of URDF:

  1. Constants:
    • We can use xacro to declare constant values, just like we do in any other programming language. For example, in the case of the mobile robot which has four wheels with the same values for length and radius, this can be declared and defined as follows:
<xacro:property name="length_wheel" value="0.05" /> 
<xacro:property name="radius_wheel" value="0.05" /> 
    • These values can be used in the following ways, later in the code:
${name_of_variable}: 
<cylinder length="${length_wheel}" radius="${radius_wheel}"/>
  1. Mathematics:
    • We can also develop complex mathematical expressions in the ${} construct using basic operations, including +, -, *, /, unary minus, and parentheses. However, exponential and modulus are not supported:
<cylinder radius="${wheel_diameter/2}" length="0.1"/> 
<origin xyz="${reflection*(width+0.04)} 0 0.25" /> 

Thus, the parameterized design is possible by using mathematics in xacro.

  1. Macros:
    • One of the most useful elements of the xacro package would be macros. For example, in our 3D robot model design description, we will use the following macro for inertial:
<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> 
    • We can compare the size of mobile_robot.urdf and arm_robot.urdf with mobile_robot.xacro and arm_robot.xacro, respectively, where we will have to eliminate at least 30 duplicate lines without much effort; however, it is possible to reduce this further by using a structure or modular design with more macros and variables.
    • We can convert the .xacro file to .urdf, which can be used with rviz as in the previous section by using the following command:
$ rosrun xacro xacro.py "'rospack find robot_description'/urdf/mobile_robot.xacro" > "'rospack find robot_description'/urdf/mobile_robot_processed.urdf" 
$ rosrun xacro xacro.py "'rospack find robot_description'/urdf/arm_robot.xacro" > "'rospack find robot_description'/urdf/arm_robot_processed.urdf" 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.186.173