Robot Operating System: How to Create a Robot Simulation Model (Part 2)

The Robot Model

<?xml version="1.0"?>
<robot name="bot">
<link name="base_link">
<visual>
<geometry>
<box size="0 0 0"/>
</geometry>
<material name="red">
<color rgba="1 0 0 1"/>
</material>
</visual>
</link>
<link name="torso">
<visual>
<geometry>
<box size="0.6 0.3 0.1"/>
</geometry>
<material name="green">
<color rgba="0 1 0 1"/>
</material>
</visual>
</link>
<joint name="base_link_torso" type="continuous">
<parent link="base_link" />
<child link="torso" />
<origin xyz="0 0 0" rpy="0 0 0"/>
</joint>
<link name="left_wheel_frontside">
<visual>
<geometry>
<cylinder radius="0.04" length="0.05" />
</geometry>
<material name="red">
<color rgba="1 0 0 1"/>
</material>
</visual>
</link>
<joint name="base_link_left_wheel_frontside" type="continuous">
<parent link="base_link" />
<child link="left_wheel_frontside" />
<origin xyz="0.2 0.2 -0.05" rpy="1.570796 0 0"/>
</joint>
<!--
...
-->
</robot>
  • wheel properties: All wheels share the same <cylinder> and <material> declaration. If I want to change e.g. the size of the wheel, I need to change 4 different spaces of the wheel
  • wheel declaration: The complete <link> declaration is repetitive, it only differs in the name
  • joints declaration: Similarly, the <joint> declaration is repetitive, it differs in the name of the links and where to place the links with the <origin> property.

Variables

<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="bot">
<!-- .... -->
</robot>
<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="bot">
<xacro:property name="wheel_radius" value="0.04" />
<xacro:property name="wheel_length" value="0.05" />
<xacro:property name="wheel_color_name" value="red" />
<xacro:property name="wheel_color_rgb" value="1 0 0 1" />
</robot>

Macros

<xacro:macro name="wheel" params="wheel_name">
<link name="${wheel_name}">
<visual>
<geometry>
<cylinder radius="${wheel_radius}" length="${wheel_length}" />
</geometry>
<material name="${wheel_color_name}">
<color rgba="${wheel_color_rgb}"/>
</material>
</visual>
</link>
</xacro:macro>
<xacro:wheel name="right_wheel_frontside" />

Running XACRO from the Command Line

$> rosrun xacro xacro bot.xacro <?xml version="1.0" ?>
<!-- =================================================================================== -->
<!-- | This document was autogenerated by xacro from bot.xacro | -->
<!-- | EDITING THIS FILE BY HAND IS NOT RECOMMENDED | -->
<!-- =================================================================================== -->
<robot name="bot">
<link name="right_wheel_frontside">
<visual>
<geometry>
<cylinder length="0.05" radius="0.04"/>
</geometry>
<material name="red">
<color rgba="1 0 0 1"/>
</material>
</visual>
</link>

Running XACRO form a Launch File

<launch>
<param name="robot_description" command="xacro $(find car-robot)/urdf/bot.xacro" />
<!-- ... -->
</launch>

Finalizing the model: Adding Torso, Base Link, and Joints

<xacro:macro name="box_link" params="name size color color_rgb" >
<link name="${name}">
<visual>
<geometry>
<box size="${size}"/>
</geometry>
<material name="${color}">
<color rgba="${color_rgb}"/>
</material>
</visual>
</link>
</xacro:macro>
<xacro:box_link name="base_link" size="0 0 0" color="red" color_rgb="1 0 0 1" />
<xacro:box_link name="torso" size="0.6 0.3 0.1" color="green" color_rgb="0 1 0 1" />
<xacro:macro name="static_joint" params="name parent child">
<joint name="${name}" type="continuous">
<parent link="${parent}" />
<child link="${child}" />
<origin xyz="0 0 0" rpy="0 0 0"/>
</joint>
</xacro:macro>
<xacro:macro name="wheel_joint" params="name parent child xyz ">
<joint name="${name}" type="continuous">
<parent link="${parent}" />
<child link="${child}" />
<origin xyz="${xyz}" rpy="1.570796 0 0"/>
</joint>
</xacro:macro>
<xacro:wheel name="right_wheel_frontside" />
<xacro:wheel_joint name="base_link_right_wheel_frontside" parent="base_link" child="right_wheel_frontside" xyz="0.2 -0.2 -0.05" />
<xacro:wheel name="right_wheel_backside" />
<xacro:wheel_joint name="base_link_right_wheel_backside" parent="base_link" child="right_wheel_backside" xyz="-0.2 -0.2 -0.05" />
<xacro:wheel name="left_wheel_frontside" />
<xacro:wheel_joint name="base_link_left_wheel_frontside" parent="base_link" child="left_wheel_frontside" xyz="0.2 0.2 -0.05" />
<xacro:wheel name="left_wheel_backside" />
<xacro:wheel_joint name="base_link_left_wheel_backside" parent="base_link" child="left_wheel_backside" xyz="-0.2 0.2 -0.05" />

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store