Create robot models through urdf and xacro methods

This article uses urdf and xacro to create a robot model in rviz!

1. Create a robot function package

Created by the catkin_create_pkg command

catkin_create_pkg mrobot_description urdf xacro

2. Create a subdirectory under the mrobot_description package

The folders that need to be created in this file are config, launch, and urdf.

The config file contains the generated urdf model file.

The launch file is placed in this folder.

The urdf folder contains the robot's urdf file.

3. Create a robot urdf model

It should be noted here that the first line in the xml file must not be a comment! ! ! ! ! ! ! !

<?xml version="1.0" ?>
<robot name="mbot">

<!--声明该文件使用XML描述,-->
<!--使用<robot>根标签定义一个机器人模型,模型的名称是“mbot”。-->


<!--描述机器人的地盘-->
    <link name="base_link">
        <visual>
            <origin xyz="0 0 0" rpy="0 0 0"/>
            <geometry>
                <cylinder length="0.16" radius="0.20"/>
            </geometry>
            <material name="yellow">
                <color rgba="1 0.4 0 1"/>
            </material>
        </visual>
    </link>
<!--机器人地盘与左轮的关节-->
    <joint name="left_wheel_joint" type="continuous">
        <origin xyz="0 0.19 -0.05" rpy="0 0 0"/>
        <parent link="base_link"/>
        <child link="left_wheel_link"/>
        <axis xyz="0 1 0"/>
    </joint>
<!--描述机器人的左轮-->
    <link name="left_wheel_link">
        <visual>
            <origin xyz="0 0 0" rpy="1.5707 0 0" />
            <geometry>
                <cylinder radius="0.06" length = "0.025"/>
            </geometry>
            <material name="white">
                <color rgba="1 1 1 0.9"/>
            </material>
        </visual>
    </link>
<!--机器人底盘与右轮的关节-->
    <joint name="right_wheel_joint" type="continuous">
        <origin xyz="0 -0.19 -0.05" rpy="0 0 0"/>
        <parent link="base_link"/>
        <child link="right_wheel_link"/>
        <axis xyz="0 1 0"/>
    </joint>
<!--描述机器人的的右轮-->
    <link name="right_wheel_link">
        <visual>
            <origin xyz="0 0 0" rpy="1.5707 0 0" />
            <geometry>
                <cylinder radius="0.06" length = "0.025"/>
            </geometry>
            <material name="white">
                <color rgba="1 1 1 0.9"/>
            </material>
        </visual>
    </link>
<!--机器人底盘与前支撑轮(作为万向轮)的关节-->
    <joint name="front_caster_joint" type="continuous">
        <origin xyz="0.18 0 -0.095" rpy="0 0 0"/>
        <parent link="base_link"/>
        <child link="front_caster_link"/>
        <axis xyz="0 1 0"/>
    </joint>
<!--描述机器人前支撑轮(也作为万向轮)-->
    <link name="front_caster_link">
        <visual>
            <origin xyz="0 0 0" rpy="0 0 0"/>
            <geometry>
                <sphere radius="0.015" />
            </geometry>
            <material name="black">
                <color rgba="0 0 0 0.95"/>
            </material>
        </visual>
    </link>
<!--机器人底盘与后支撑轮(作为万向轮)的关节-->
    <joint name="back_caster_joint" type="continuous">
        <origin xyz="-0.18 0 -0.095" rpy="0 0 0"/>
        <parent link="base_link"/>
        <child link="back_caster_link"/>
        <axis xyz="0 1 0"/>
    </joint>
<!--描述机器人后支撑轮(也作为万向轮)-->
    <link name="back_caster_link">
        <visual>
            <origin xyz="0 0 0" rpy="0 0 0"/>
            <geometry>
                <sphere radius="0.015" />
            </geometry>
            <material name="black">
                <color rgba="0 0 0 0.95"/>
            </material>
        </visual>
    </link>

</robot>

The meaning of each label is

<link>

        <inertial> #inertial label

                <origin> # relative link coordinates

                <mass> #mass

                <inertial> #moment of inertia

        <visual> #Visual attributes

                <origin> #relative link tag

                <geometry> #shape

                <material> #material

        <collision> #collision attribute

                <origin> # relative coordinates

                <geometry> #shape

</link>



<joint>

        <origin> #Transformation from parent link to child link

        <parent>        #父link

        <child>        #子link

        <axis> #joint axis

        <calibration> #Visual attributes

        <dynamics> #dynamic parameters

        <limit> #joint limit

</joint>

 The inertial tags and collision tags like here will only be used when writing gazebo files.

Fourth, write the launch file

<launch>
	<param name="robot_description" textfile="$(find mbot_description)/urdf/urdf/mbot_base.urdf" />

	<!-- 设置GUI参数,显示关节控制插件 -->
	<param name = "use_gui" value = "true"/>
    <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" />
	
	<!-- 运行rviz可视化界面 -->
	<node name="rviz" pkg="rviz" type="rviz" args="-d $(find mbot_description)/config/mbot_urdf.rviz" required="true" />
</launch>

Most of the content in the launch file remains unchanged. We only need to change the content of the texefile, which points to the location where the urdf file is placed. The writing format is

"$(find package name)/subdirectory/.../filename"

 5. Running results

 

After writing the urdf file, we found that the language written in urdf is too complicated, so let's introduce another method of writing the robot description file——using xacro file.

6. Use xacro to write the robot description model (mbot_base.xacro)

Here is some syntax about xacro, which I will describe in detail in the next article!

<?xml version="1.0"?>
<robot name="mbot" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!--声明该文件使用XML描述,-->
<!--使用xacro时必须要在开头添加上: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"/> <!-- wheel_radius - ( base_length/2 - wheel_joint_z) -->
    <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>
    
    <!-- 车轮的宏定义 相当于C语言中自己定义的函数 -->
    <xacro:macro name="wheel" params="prefix reflect">
        <joint name="${prefix}_wheel_joint" type="continuous">
            <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="continuous">
            <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>

    <!--小车底盘的宏定义-->
    <!--base_link与base_footprint的区别-->
    <!--base_link是固定在机器人本体上的坐标系,通常选择机器人腰部。-->
    <!--base_footprint表示机器人base_link原点在地面上的投影,区别base_link之处是其“z”坐标不同。-->
    <!--一般为了模型不陷入地面,base_footprint的“z”坐标比base_link高。-->
    <xacro:macro name="mbot_base">
        <link name="base_footprint">
            <visual>
                <origin xyz="0 0 0" rpy="0 0 0" />
                <geometry>
                    <box size="1 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>
        <!--宏定义的使用  注意使用时前面一定要加xacro(noetic版本以前的不用加)-->
        <xacro:wheel prefix="left" reflect="-1"/>
        <xacro:wheel prefix="right" reflect="1"/>

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

7. Write the reference file of the xacro robot description file (mbot.xacro)

<?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" />

    <xacro:mbot_base/>

</robot>

8. Write the launch file

<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)" />

    <!-- 设置GUI参数,显示关节控制插件 -->
	<param name="use_gui" value="$(arg gui)"/>

    <!-- 运行joint_state_publisher节点,发布机器人的关节状态  -->
	<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />

	<!-- 运行robot_state_publisher节点,发布tf  -->
	<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

    <!-- 运行rviz可视化界面 -->
	<node name="rviz" pkg="rviz" type="rviz" args="-d $(find mbot_description)/config/mbot.rviz" required="true" />

</launch>

What we need to change here is the content in the default, the content is the address of the xacro file, and the format is

"$(find xacro)/xacro --inorder '$(find package name)/subdirectory/.../filename

 The running results here are exactly the same as the running results of the model written in the urdf file.

Summarize

This paper writes a robot model through two description methods of urdf and xacro. Although the superiority of xacro has not yet been reflected in the amount of code, the advantages of xacro in large projects will be obvious. Because xacro has the function of defining functions (macro definition), calculation functions, etc., it can reduce a lot of repetitive work for us.

Guess you like

Origin blog.csdn.net/weixin_54963942/article/details/128389186#comments_27117101