xacro文件使用
一、xacro的基础语法
案例1:属性设置和算数运算
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 1.属性定义 -->
<xacro:property name="PI" value="3.1415926"/>
<xacro:property name="radius" value="0.03"/>
<!-- 2.属性声明 -->
<myUsePropertyxxx name="${PI}"/>
<myUsePropertyxxx name="${radius}"/>
<!-- 3.算术运算 -->
<myUsePropertyYyy result="${PI/2}"/>
<myUsePropertyYyy result="${radius*2}"/>
</robot>
rosrun xacro xacro demo02_field.urdf.xacro
<?xml version="1.0" ?>
<!-- =================================================================================== -->
<!-- | This document was autogenerated by xacro from demo02_field.urdf.xacro | -->
<!-- | EDITING THIS FILE BY HAND IS NOT RECOMMENDED | -->
<!-- =================================================================================== -->
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 2.属性声明 -->
<myUsePropertyxxx name="3.1415926"/>
<myUsePropertyxxx name="0.03"/>
<!-- 3.算术运算 -->
<myUsePropertyYyy result="1.5707963"/>
<myUsePropertyYyy result="0.06"/>
</robot>
name分别被替换了,并且可以参与算数运算。
案例2:宏的定义与宏的调用
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 1.宏的定义 -->
<xacro:macro name="getSum" params="num1 num2">
<result value="${num1+num2}"/>
</xacro:macro>
<!-- 2.宏的调用 -->
<xacro:getSum num1="1" num2="5"/>
</robot>
rosrun xacro xacro demo03_macro.urdf.xacro
<?xml version="1.0" ?>
<!-- =================================================================================== -->
<!-- | This document was autogenerated by xacro from demo03_macro.urdf.xacro | -->
<!-- | EDITING THIS FILE BY HAND IS NOT RECOMMENDED | -->
<!-- =================================================================================== -->
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<result value="6"/>
</robot>
案例3:文件包含
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 演示文件包含 -->
<xacro:include filename="demo02_field.urdf.xacro"/>
<xacro:include filename="demo03_macro.urdf.xacro"/>
</robot>
rosrun xacro xacro demo04_sum.urdf.xacro
<?xml version="1.0" ?>
<!-- =================================================================================== -->
<!-- | This document was autogenerated by xacro from demo04_sum.urdf.xacro | -->
<!-- | EDITING THIS FILE BY HAND IS NOT RECOMMENDED | -->
<!-- =================================================================================== -->
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 2.属性声明 -->
<myUsePropertyxxx name="3.1415926"/>
<myUsePropertyxxx name="0.03"/>
<!-- 3.算术运算 -->
<myUsePropertyYyy result="1.5707963"/>
<myUsePropertyYyy result="0.06"/>
<result value="6"/>
</robot>
二、小车案例(用xacro实现)
1.实验框架
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 1.base_footprint -->
<!-- 2.小车底盘 -->
<!-- 3.小车驱动轮 -->
<!-- 4.小车万向轮 -->
</robot>
一些基础定义:
<xacro:property name="PI" value="3.141"/>
<material name="black">
<color rgba="0.0 0.0 0.0 1.0" />
</material>
<xacro:property name="base_footprint_radius" value="0.001" />
<xacro:property name="base_link_radius" value="0.1" />
<xacro:property name="base_link_length" value="0.08" />
<xacro:property name="earth_space" value="0.015" />
2.base_footprint的xacro文件
<link name="base_footprint">
<visual>
<geometry>
<sphere radius="${base_footprint_radius}" />
</geometry>
</visual>
</link>
3.base_link的xacro文件
<link name="base_link">
<visual>
<geometry>
<cylinder radius="${base_link_radius}" length="${base_link_length}" />
</geometry>
<origin xyz="0 0 0" rpy="0 0 0" />
<material name="yellow">
<color rgba="1 1 0.0 1" />
</material>
</visual>
</link>
<joint name="base_link2base_footprint" type="fixed">
<parent link="base_footprint" />
<child link="base_link" />
<origin xyz="0 0 ${earth_space + base_link_length / 2 }" />
</joint>
4.左右驱动轮的xacro文件
<xacro:property name="wheel_radius" value="0.0325" />
<xacro:property name="wheel_length" value="0.015" />
<xacro:macro name="add_wheels" params="name flag">
<link name="${name}_wheel">
<visual>
<geometry>
<cylinder radius="${wheel_radius}" length="${wheel_length}" />
</geometry>
<origin xyz="0.0 0.0 0.0" rpy="${PI / 2} 0.0 0.0" />
<material name="red">
<color rgba="1 0 0.0 1" />
</material>
</visual>
</link>
<joint name="${name}_wheel2base_link" type="continuous">
<parent link="base_link" />
<child link="${name}_wheel" />
<origin xyz="0 ${flag * base_link_radius} ${-(earth_space + base_link_length / 2 - wheel_radius) }" />
<axis xyz="0 1 0" />
</joint>
</xacro:macro>
<xacro:add_wheels name="left" flag="1" />
<xacro:add_wheels name="right" flag="-1" />
5.前后支撑轮的xacro文件
<xacro:property name="support_wheel_radius" value="0.0075" />
<xacro:macro name="add_support_wheel" params="name flag" >
<link name="${name}_wheel">
<visual>
<geometry>
<sphere radius="${support_wheel_radius}" />
</geometry>
<origin xyz="0 0 0" rpy="0 0 0" />
<material name="black" />
</visual>
</link>
<joint name="${name}_wheel2base_link" type="continuous">
<parent link="base_link" />
<child link="${name}_wheel" />
<origin xyz="${flag * (base_link_radius - support_wheel_radius)} 0 ${-(base_link_length / 2 + earth_space / 2)}" />
<axis xyz="1 1 1" />
</joint>
</xacro:macro>
<xacro:add_support_wheel name="front" flag="1" />
<xacro:add_support_wheel name="back" flag="-1" />
6.完整xacro文件
<robot name="my_base" xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:property name="PI" value="3.141"/>
<material name="black">
<color rgba="0.0 0.0 0.0 1.0" />
</material>
<xacro:property name="base_footprint_radius" value="0.001" />
<xacro:property name="base_link_radius" value="0.1" />
<xacro:property name="base_link_length" value="0.08" />
<xacro:property name="earth_space" value="0.015" />
<link name="base_footprint">
<visual>
<geometry>
<sphere radius="${base_footprint_radius}" />
</geometry>
</visual>
</link>
<link name="base_link">
<visual>
<geometry>
<cylinder radius="${base_link_radius}" length="${base_link_length}" />
</geometry>
<origin xyz="0 0 0" rpy="0 0 0" />
<material name="yellow">
<color rgba="1 1 0.0 1" />
</material>
</visual>
</link>
<joint name="base_link2base_footprint" type="fixed">
<parent link="base_footprint" />
<child link="base_link" />
<origin xyz="0 0 ${earth_space + base_link_length / 2 }" />
</joint>
<xacro:property name="wheel_radius" value="0.0325" />
<xacro:property name="wheel_length" value="0.015" />
<xacro:macro name="add_wheels" params="name flag">
<link name="${name}_wheel">
<visual>
<geometry>
<cylinder radius="${wheel_radius}" length="${wheel_length}" />
</geometry>
<origin xyz="0.0 0.0 0.0" rpy="${PI / 2} 0.0 0.0" />
<material name="red">
<color rgba="1 0 0.0 1" />
</material>
</visual>
</link>
<joint name="${name}_wheel2base_link" type="continuous">
<parent link="base_link" />
<child link="${name}_wheel" />
<origin xyz="0 ${flag * base_link_radius} ${-(earth_space + base_link_length / 2 - wheel_radius) }" />
<axis xyz="0 1 0" />
</joint>
</xacro:macro>
<xacro:add_wheels name="left" flag="1" />
<xacro:add_wheels name="right" flag="-1" />
<xacro:property name="support_wheel_radius" value="0.0075" />
<xacro:macro name="add_support_wheel" params="name flag" >
<link name="${name}_wheel">
<visual>
<geometry>
<sphere radius="${support_wheel_radius}" />
</geometry>
<origin xyz="0 0 0" rpy="0 0 0" />
<material name="black" />
</visual>
</link>
<joint name="${name}_wheel2base_link" type="continuous">
<parent link="base_link" />
<child link="${name}_wheel" />
<origin xyz="${flag * (base_link_radius - support_wheel_radius)} 0 ${-(base_link_length / 2 + earth_space / 2)}" />
<axis xyz="1 1 1" />
</joint>
</xacro:macro>
<xacro:add_support_wheel name="front" flag="1" />
<xacro:add_support_wheel name="back" flag="-1" />
</robot>
7.launch文件加载xacro
<launch>
<param name="robot_description" command="$(find xacro)/xacro $(find mbot_description)/urdf/xacro/demo05_car_base.urdf.xacro" />
<!-- 设置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="state_publisher" />
<!-- 运行rviz可视化界面 -->
<node name="rviz" pkg="rviz" type="rviz" args="-d $(find mbot_description)/config/demo06_car_base.rviz" required="true" />
</launch>
三、传感器模型案例
分别建立四个xacro文件:demo06_camera.urdf.xacro、demo07_lidar.urdf.xacro、car.urdf.xacro,还有上一节已经建立好的小车底盘的xacro文件demo05_car_base.urdf.xacro。建立一个launch文件用于调用xacro文件,launch(demo07_car.launch)文件内容如下:
<launch>
<param name="robot_description" command="$(find xacro)/xacro $(find mbot_description)/urdf/xacro/car.urdf.xacro" />
<!-- 设置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="state_publisher" />
<!-- 运行rviz可视化界面 -->
<node name="rviz" pkg="rviz" type="rviz" args="-d $(find mbot_description)/config/demo06_car_base.rviz" required="true" />
</launch>
1.摄像头xacro文件
<robot name="my_camera" xmlns:xacro="http://wiki.ros.org/xacro">
<xacro:property name="camera_length" value="0.01" />
<xacro:property name="camera_width" value="0.025" />
<xacro:property name="camera_height" value="0.025" />
<xacro:property name="camera_x" value="0.08" />
<xacro:property name="camera_y" value="0.0" />
<xacro:property name="camera_z" value="${base_link_length / 2 + camera_height / 2}" />
<link name="camera">
<visual>
<geometry>
<box size="${camera_length} ${camera_width} ${camera_height}" />
</geometry>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
<material name="green">
<color rgba="0 1 0.0 1" />
</material>
</visual>
</link>
<joint name="camera2base_link" type="fixed">
<parent link="base_link" />
<child link="camera" />
<origin xyz="${camera_x} ${camera_y} ${camera_z}" />
</joint>
</robot>
2.雷达xacro文件
<robot name="my_laser" xmlns:xacro="http://wiki.ros.org/xacro">
<xacro:property name="support_length" value="0.15" />
<xacro:property name="support_radius" value="0.01" />
<xacro:property name="support_x" value="0.0" />
<xacro:property name="support_y" value="0.0" />
<xacro:property name="support_z" value="${base_link_length / 2 + support_length / 2}" />
<link name="support">
<visual>
<geometry>
<cylinder radius="${support_radius}" length="${support_length}" />
</geometry>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
<material name="red">
<color rgba="0.8 0.2 0.0 0.8" />
</material>
</visual>
</link>
<joint name="support2base_link" type="fixed">
<parent link="base_link" />
<child link="support" />
<origin xyz="${support_x} ${support_y} ${support_z}" />
</joint>
<xacro:property name="laser_length" value="0.05" />
<xacro:property name="laser_radius" value="0.03" />
<xacro:property name="laser_x" value="0.0" />
<xacro:property name="laser_y" value="0.0" />
<xacro:property name="laser_z" value="${support_length / 2 + laser_length / 2}" />
<link name="laser">
<visual>
<geometry>
<cylinder radius="${laser_radius}" length="${laser_length}" />
</geometry>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
<material name="blue">
<color rgba="0.0 0.0 1 1" />
</material>
</visual>
</link>
<joint name="laser2support" type="fixed">
<parent link="support" />
<child link="laser" />
<origin xyz="${laser_x} ${laser_y} ${laser_z}" />
</joint>
</robot>
3.整合底盘和传感器的xacro文件
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<xacro:include filename="demo05_car_base.urdf.xacro"/>
<xacro:include filename="demo06_camera.urdf.xacro"/>
<xacro:include filename="demo07_lidar.urdf.xacro"/>
</robot>