Using macros

One of the main features of xacro is that it supports macros. We can use xacro to  reduce the length of complex definitions. Here is a xacro definition we used in our code for inertial:

<xacro:macro name="inertial_matrix" params="mass"> 
  <inertial> 
       <mass value="${mass}" /> 
          <inertia ixx="0.5" ixy="0.0" ixz="0.0" 
          iyy="0.5" iyz="0.0" izz="0.5" /> 
   </inertial> 
</xacro:macro> 

Here, the macro is named inertial_matrix, and its parameter is mass. The mass parameter can be used inside the inertial definition using ${mass}. We can replace each inertial code with a single line, as given here:

  <xacro:inertial_matrix mass="1"/> 

The xacro definition improved the code readability and reduced the number of lines compared to urdf. Next, we will ...

Get Mastering ROS for Robotics Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.