ros学习编写urdf和xacro

29 篇文章 4 订阅

环境:ubuntu18

创建URDF模型

创建包:
catkin_create_pkg mrobot_description urdf xacro
编辑URDF文件:
:mrobot_ws/src/mrobot_description/urdf/urdf$ gedit mrobot_base.urdf

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

    <!-- BASE -->
    <link name="base_link"> 
    <visual> 
    <origin xyz=" 0 0 0" rpy="0 0 0" /> 
    <geometry> 
    <cylinder length="0.005" radius="0.13"/> 
    </geometry> 
    <material name="yellow"> 
    <color rgba="1 0.4 0 1"/> 
    </material> 
    </visual> 
    </link> 

    <!-- motor -->
    <joint name="base_left_motor_joint" type="fixed"> 
    <origin xyz="-0.055 0.075 0" rpy="0 0 0" /> 
    <parent link="base_link"/> 
    <child link="left_motor" /> 
    </joint> 

    <link name="left_motor"> 
    <visual> 
    <origin xyz="0 0 0" rpy="1.5707 0 0" /> 
    <geometry> 
    <cylinder radius="0.02" length = "0.08"/> 
    </geometry> 
    <material name="gray"> 
    <color rgba="0.75 0.75 0.75 1"/> 
    </material> 
    </visual> 
    </link> 

    <joint name="base_right_motor_joint" type="fixed"> 
    <origin xyz="-0.055 -0.075 0" rpy="0 0 0" /> 
    <parent link="base_link"/> 
    <child link="right_motor" />
    </joint> 

    <link name="right_motor"> 
    <visual> 
    <origin xyz="0 0 0" rpy="1.5707 0 0" /> 
    <geometry> 
    <cylinder radius="0.02" length = "0.08" /> 
    </geometry> 
    <material name="gray"> 
    <color rgba="0.75 0.75 0.75 1"/> 
    </material> 
    </visual> 
    </link> 

    <!-- wheel -->
    <joint name="left_wheel_joint" type="continuous"> 
    <origin xyz="0 0.0485 0" rpy="0 0 0"/> 
    <parent link="left_motor"/> 
    <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.033" length = "0.017"/> 
    </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.0485 0" rpy="0 0 0"/> 
    <parent link="right_motor"/> 
    <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.033" length = "0.017"/> 
    </geometry> 
    <material name="white"> 
    <color rgba="1 1 1 0.9"/> 
    </material> 
    </visual> 
    </link> 

    <!-- castor -->
    <joint name="front_castor_joint" type="fixed"> 
    <origin xyz="0.1135 0 -0.0165" rpy="0 0 0"/> 
    <parent link="base_link"/> 
    <child link="front_castor_link"/> 
    <axis xyz="0 1 0"/> 
    </joint> 

    <link name="front_castor_link"> 
    <visual> 
    <origin xyz="0 0 0" rpy="1.5707 0 0"/> 
    <geometry> 
    <sphere radius="0.0165" /> 
    </geometry> 
    <material name="black"> 
    <color rgba="0 0 0 0.95"/> 
    </material> 
    </visual> 
    </link> 
</robot>

编辑launch文件:
:mrobot_ws/src/mrobot_description/launch$ gedit display_mrobot_base_urdf.launch

<launch> 
    <param name="robot_description" textfile="$(find mrobot_description)/urdf/urdf/mrobot_chassis.urdf" /> 
    <!-- 设置GUI参数,显示关节控制插件 --> 
    <param name="use_gui" value="true"/> 
    <!-- 运行joint_state_publisher节点,发布机器人的关节状态 --> 
    <node name="joint_state_publisher_gui" pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" /> 
    <!-- 运行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 mrobot_description)/config/mrobot_urdf.rviz" required="true" /> 
</launch>

运行launch文件报错,原因是找不到这个文件$(find mrobot_description)/config/mrobot_urdf.rviz,可以手动打开rviz,保存一个rviz文件在这个路径下。

在这里插入图片描述

使用xacro

变量定义

    <!-- PROPERTY LIST -->
    <xacro:property name="M_PI" value="3.1415926"/>
    <xacro:property name="base_radius" value="0.13"/>
    <xacro:property name="base_length" value="0.005"/>

    <xacro:property name="wheel_radius" value="0.033"/>
    <xacro:property name="wheel_length" value="0.017"/>
    <xacro:property name="wheel_joint_y" value="0.0485"/>

    <xacro:property name="motor_radius" value="0.02"/>
    <xacro:property name="motor_length" value="0.08"/>
    <xacro:property name="motor_joint_x" value="-0.055"/>
    <xacro:property name="motor_joint_y" value="0.075"/>
    
    <xacro:property name="caster_radius" value="0.0165"/>
    <xacro:property name="caster_joint_x" value="0.1135"/>
    <xacro:property name="caster_joint_z" value="-0.0165"/>

宏定义


    <!-- Macro for robot wheel -->
    <xacro:macro name="wheel" params="prefix reflect">
        <joint name="${prefix}_wheel_joint" type="continuous">
            <origin xyz="0 ${reflect*wheel_joint_y} 0" rpy="0 0 0"/>
            <parent link="${prefix}_motor_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>

    <!-- Macro for robot caster -->
    <xacro:macro name="caster" params="prefix reflect">
        <joint name="${prefix}_caster_joint" type="continuous">
            <origin xyz="${reflect*caster_joint_x} 0 ${caster_joint_z}" 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="${M_PI/2} 0 0" />
                <geometry>
                    <sphere radius="${caster_radius}" />
                </geometry>
                <material name="gray" />
            </visual>
        </link>
    </xacro:macro>

    <!-- Macro for robot motor -->
    <xacro:macro name="motor" params="prefix reflect">
        <joint name="${prefix}_motor_joint" type="fixed">
            <origin xyz="${motor_joint_x} ${reflect*motor_joint_y} 0" rpy="0 0 0"/>
            <parent link="base_link"/>
            <child link="${prefix}_motor_link"/>
            <axis xyz="0 1 0"/>
        </joint>

        <link name="${prefix}_motor_link">
            <visual>
                <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
                <geometry>
                    <cylinder radius="${motor_radius}" length = "${motor_length}"/>
                </geometry>
                <material name="black" />
            </visual>
        </link>
    </xacro:macro>

整个文件
/mrobot_ws/src/mrobot_description/urdf/xacro$ gedit mrobot_base.xacro

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

    <!-- PROPERTY LIST -->
    <xacro:property name="M_PI" value="3.1415926"/>
    <xacro:property name="base_radius" value="0.13"/>
    <xacro:property name="base_length" value="0.005"/>

    <xacro:property name="wheel_radius" value="0.033"/>
    <xacro:property name="wheel_length" value="0.017"/>
    <xacro:property name="wheel_joint_y" value="0.0485"/>

    <xacro:property name="motor_radius" value="0.02"/>
    <xacro:property name="motor_length" value="0.08"/>
    <xacro:property name="motor_joint_x" value="-0.055"/>
    <xacro:property name="motor_joint_y" value="0.075"/>
    
    <xacro:property name="caster_radius" value="0.0165"/>
    <xacro:property name="caster_joint_x" value="0.1135"/>
    <xacro:property name="caster_joint_z" value="-0.0165"/>


    <!-- Defining the colors used in this robot -->
    <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>



    <!-- Macro for robot wheel -->
    <xacro:macro name="wheel" params="prefix reflect">
        <joint name="${prefix}_wheel_joint" type="continuous">
            <origin xyz="0 ${reflect*wheel_joint_y} 0" rpy="0 0 0"/>
            <parent link="${prefix}_motor_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>

    <!-- Macro for robot caster -->
    <xacro:macro name="caster" params="prefix reflect">
        <joint name="${prefix}_caster_joint" type="continuous">
            <origin xyz="${reflect*caster_joint_x} 0 ${caster_joint_z}" 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="${M_PI/2} 0 0" />
                <geometry>
                    <sphere radius="${caster_radius}" />
                </geometry>
                <material name="gray" />
            </visual>
        </link>
    </xacro:macro>

    <!-- Macro for robot motor -->
    <xacro:macro name="motor" params="prefix reflect">
        <joint name="${prefix}_motor_joint" type="fixed">
            <origin xyz="${motor_joint_x} ${reflect*motor_joint_y} 0" rpy="0 0 0"/>
            <parent link="base_link"/>
            <child link="${prefix}_motor_link"/>
            <axis xyz="0 1 0"/>
        </joint>

        <link name="${prefix}_motor_link">
            <visual>
                <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
                <geometry>
                    <cylinder radius="${motor_radius}" length = "${motor_length}"/>
                </geometry>
                <material name="black" />
            </visual>
        </link>
    </xacro:macro>




    <!-- Macro for robot base -->
    <xacro:macro name="mrobot_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 + 0.03}" rpy="0 0 0" />        
            <parent link="base_link"/>
            <child link="base_footprint" />
        </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>

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

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

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

/mrobot_ws/src/mrobot_description/urdf/xacro$ gedit mrobot.xacro

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

/mrobot_ws/src/mrobot_description/launch/xacro$ gedit display_mrobot_xacro.launch

<launch> 

    <arg name="model" default="$(find xacro)/xacro --inorder '$(find mrobot_description)/urdf/xacro/mrobot.xacro'" />

    <param name="robot_description" command="$(arg model)" />

    <!-- 设置GUI参数,显示关节控制插件 --> 
    <param name="use_gui" value="true"/> 
    <!-- 运行joint_state_publisher节点,发布机器人的关节状态 --> 
    <node name="joint_state_publisher_gui" pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" /> 
    <!-- 运行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 mrobot_description)/config/mrobot_urdf.rviz" required="true" /> 
</launch>

中断运行:

ubuntu@ubuntu-vpv:~/mrobot_ws/src/mrobot_description/launch/xacro$ roslaunch mrobot_description display_mrobot_xacro.launch 
... logging to /home/ubuntu/.ros/log/eb8eb2ca-e603-11ea-97a0-000c29b7d468/roslaunch-ubuntu-vpv-9397.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

xacro: in-order processing became default in ROS Melodic. You can drop the option.
Deprecated: xacro tag 'mrobot_base' w/o 'xacro:' xml namespace prefix (will be forbidden in Noetic)
when processing file: /home/ubuntu/mrobot_ws/src/mrobot_description/urdf/xacro/mrobot.xacro
Use the following command to fix incorrect tag usage:
find . -iname "*.xacro" | xargs sed -i 's#<\([/]\?\)\(if\|unless\|include\|arg\|property\|macro\|insert_block\)#<\1xacro:\2#g'

started roslaunch server http://ubuntu-vpv:34713/

SUMMARY
========

PARAMETERS
 * /robot_description: <?xml version="1....
 * /rosdistro: melodic
 * /rosversion: 1.14.7
 * /use_gui: True

NODES
  /
    joint_state_publisher_gui (joint_state_publisher_gui/joint_state_publisher_gui)
    robot_state_publisher (robot_state_publisher/robot_state_publisher)
    rviz (rviz/rviz)

auto-starting new master
process[master]: started with pid [9411]
ROS_MASTER_URI=http://localhost:11311

setting /run_id to eb8eb2ca-e603-11ea-97a0-000c29b7d468
process[rosout-1]: started with pid [9422]
started core service [/rosout]
process[joint_state_publisher_gui-2]: started with pid [9425]
process[robot_state_publisher-3]: started with pid [9430]
process[rviz-4]: started with pid [9431]

在这里插入图片描述
Deprecated: xacro tag 'mrobot_base' w/o 'xacro:' xml namespace prefix (will be forbidden in Noetic)
Noetic有做改动,这里用的是Melodic.

代码:https://gitee.com/juzhango/urdf_xacro.git

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值