URDF model optimization-xacro model

Overview

This article mainly talks about the advanced version of the URDF model-the xacro model.
For the establishment of URDF model, please refer to this article:
Robot Simulation-URDF Modeling .

1. Model comparison

1. URDF model

  • The model is verbose with too much repetition;
  • Parameter modification is troublesome and not convenient for secondary development;
  • No parameter calculation function

2. URDF advanced version-xacro model

  • Streamline model code

Create macro definition
file contains

  • Provide programmable interface

Constant
Variable
Mathematical Calculation
Conditional Statement

Second, the definition of the xacro model

1. Constant

Constant definition

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

Constant use

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

2. Mathematical calculation

Mathematical calculation

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

注意:所有数学运算都会转换成浮点数进行,以保证运算精度

3. Macro definition

Macro definition

<xacro:macro name="name" params="A B C">
   ......
</xacro:macro>

Macro call

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

4. File contains

File contains

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

Three, xacro creates a car model

instruction

cd ~/catkin_ws/src/mbot_description/urdf
mkdir xacro
cd xacro
sudo gedit mbot_base.xacro 

Insert picture description here
Paste the following content into the text

<?xml version="1.0"?>
<robot name="mbot" xmlns:xacro="http://www.ros.org/wiki/xacro">
    <xacro:property name="M_PI" value="3.1415926"/>
    <xacro:property name="base_radius" value="0.20"/>
    <xacro:property name="base_length" value="0.16"/>

    <xacro:property name="wheel_radius" value="0.06"/>
    <xacro:property name="wheel_length" value="0.025"/>
    <xacro:property name="wheel_joint_y" value="0.19"/>
    <xacro:property name="wheel_joint_z" value="0.05"/>

    <xacro:property name="caster_radius" value="0.015"/> 
    <xacro:property name="caster_joint_x" value="0.18"/>
  
    <material name="yellow">
        <color rgba="1 0.4 0 1"/>
    </material>
    <material name="black">
        <color rgba="0 0 0 0.95"/>
    </material>
    <material name="gray">
        <color rgba="0.75 0.75 0.75 1"/>
    </material>
   
   
    <xacro:macro name="wheel" params="prefix reflect">
        <joint name="${prefix}_wheel_joint" type="fixed">
            <origin xyz="0 ${reflect*wheel_joint_y} ${-wheel_joint_z}" rpy="0 0 0"/>
            <parent link="base_link"/>
            <child link="${prefix}_wheel_link"/>
            <axis xyz="0 1 0"/>
        </joint>

        <link name="${prefix}_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="gray" />
            </visual>
        </link>
    </xacro:macro>
  
    <xacro:macro name="caster" params="prefix reflect">
        <joint name="${prefix}_caster_joint" type="fixed">
            <origin xyz="${reflect*caster_joint_x} 0 ${-(base_length/2 + caster_radius)}" rpy="0 0 0"/>
            <parent link="base_link"/>
            <child link="${prefix}_caster_link"/>
            <axis xyz="0 1 0"/>
        </joint>

        <link name="${prefix}_caster_link">
            <visual>
                <origin xyz="0 0 0" rpy="0 0 0"/>
                <geometry>
                    <sphere radius="${caster_radius}" />
                </geometry>
                <material name="black" />
            </visual>
        </link>
    </xacro:macro>
   
        <xacro:macro name="mbot_base">  
        <link name="base_footprint">   
            <visual>
                <origin xyz="0 0 0" rpy="0 0 0" />
                <geometry>
                    <box size="0.001 0.001 0.001" />
                </geometry>
            </visual>
        </link>

        <joint name="base_footprint_joint" type="fixed">
            <origin xyz="0 0 ${base_length/2 + caster_radius*2}" rpy="0 0 0" />        
            <parent link="base_footprint"/>
            <child link="base_link" />
        </joint>

        <link name="base_link">
            <visual>
                <origin xyz=" 0 0 0" rpy="0 0 0" />
                <geometry>
                    <cylinder length="${base_length}" radius="${base_radius}"/>
                </geometry>
                <material name="yellow" />
            </visual>
        </link>

        <wheel prefix="left" reflect="-1"/> 
        <wheel prefix="right" reflect="1"/> 

        <caster prefix="front" reflect="-1"/> 
        <caster prefix="back" reflect="1"/> 
    </xacro:macro>
</robot>

instruction

sudo gedit mbot.xacro

Paste to text

<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
    <xacro:include filename="$(find mbot_description)/urdf/xacro/mbot_base.xacro" /> 
    <mbot_base/>    
</robot>

instruction

cd ~/catkin_ws/src/mbot_description/launch
mkdir xacro
cd xacro
sudo gedit display_mbot_base_xacro.launch

Insert picture description here
Paste to text

<launch>
	<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)" />
    
	<param name="use_gui" value="$(arg gui)"/>
   
	<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />
	
	<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />
   
	<node name="rviz" pkg="rviz" type="rviz" args="-d $(find mbot_description)/config/mbot.rviz" required="true" />
</launch>

View the demo effect

roslaunch mbot_description display_mbot_base_xacro.launch

Insert picture description here

Pay attention to modify Fixed Framethe mapinstead base_link; then click addAdd robotmodel.

Four, summary and reference materials

1. Summary

The advanced version of the xacro model of the URDF model, obviously due to the URDF model, the modeling process is also much simpler.

2. Reference materials

Use Xacro to write a ROS car model .

Guess you like

Origin blog.csdn.net/QWERTYzxw/article/details/115030321