重要提示:在xacro文件虽然是xml文件,但是如果在xacro文件中随意插入自定义标签,虽然check_urdf不会报错,但是最后rviz2解析的时候会出现错误,例如,如果在上述xml文件中加入以下代码将出现显示异常:
<material name="blank">
<color rgba="0 0 0 0"/>
</material>
上述代码在使用check_urdf时候或者执行的时候不会报任何错误,但是显示出来的rviz2机器人模型异常。我猜测是加入了异常的便签引起,删除以后即可正常显示各个部件
total.xacro文件
<robot name="half_total" xmlns:xacro="http://wiki.ros.org/wiki/xacro">
<xacro:include filename="$(find urdf_pkg)/urdf/base.xacro"/>
<xacro:include filename="$(find urdf_pkg)/urdf/camera.xacro"/>
<xacro:include filename="$(find urdf_pkg)/urdf/radar.xacro"/>
</robot>
base.xacro文件
<robot name="half" xmlns:xacro="http://wiki.ros.org/wiki/xacro">
<xacro:property name="PI" value="3.1415926"/><!--定义一个变量PI,后边用来调用的-->
<xacro:property name="base_footprint_radius" value="0.001"/>
<xacro:property name="base_radius" value="0.1"/>
<xacro:property name="base_length" value="0.08"/>
<xacro:property name="ground_clearance" value="0.015"/>
<link name="base_footprint">
<visual>
<geometry>
<sphere radius="${base_footprint_radius}"/>
</geometry>
</visual>
</link>
<link name="base">
<visual>
<geometry>
<cylinder radius="${base_radius}" length="${base_length}"/>
</geometry>
<origin xyz="0 0 0" rpy="0 0 0"/>
<material name="yellow">
<color rgba="0.3 0.3 0.0 0.5"/>
</material>
</visual>
</link>
<joint name="base2base_footprint" type="fixed">
<parent link="base_footprint"/>
<child link="base"/>
<origin xyz="0 0 ${ground_clearance+base_length/2}"/>
</joint>
<xacro:property name="wheel_radius" value="0.0325"/>
<xacro:property name="wheel_length" value="0.015"/>
<xacro:macro name="add_wheel" 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"/>
<material name="black"/>
</visual>
</link>
<joint name="${name}_wheel2base" type="continuous">
<parent link="base"/>
<child link="${name}_wheel"/>
<origin xyz="0 ${flag*base_radius} ${-(ground_clearance+base_length/2-wheel_radius)}"/>
<axis xyz="0 1 0"/>
</joint>
</xacro:macro>
<xacro:add_wheel name="left" flag="1"/>
<xacro:add_wheel name="right" flag="-1"/>
<xacro:property name="support_wheel_radius" value="0.0075">
</xacro:property>
<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.0 0.0"/>
<material name="black">
</material>
</visual>
</link>
<joint name="${name}_wheel2base" type="continuous">
<parent link="base"/>
<child link="${name}_wheel"/>
<origin xyz="${flag*(base_radius-support_wheel_radius)} 0 ${-(base_length/2+ground_clearance/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>
camera.xacro文件
<robot name="camera_2" 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"/>
<xacro:property name="camera_z" value="${base_length/2+camera_height/2}"/>
<link name="camera">
<visual>
<geometry>
<box size="${camera_length} ${camera_width} ${camera_height}"/>
</geometry>
<origin xyz="0 0 0" rpy="0 0 0"/>
<material name="black"/>
</visual>
</link>
<joint name="camra2base" type="fixed">
<parent link="base"/>
<child link="camera"/>
<origin xyz="${camera_x} ${camera_y} ${camera_z}"/>
</joint>
</robot>
radar.xacro
<robot name="radar" xmlns:xacro="http://wiki.ros.org/wiki/xacro">
<xacro:property name="holder_length" value="0.15"/>
<xacro:property name="holder_radius" value="0.01"/>
<xacro:property name="holder_x" value="0"/>
<xacro:property name="holder_y" value="0"/>
<xacro:property name="holder_z" value="${base_length/2+holder_length/2}"/>
<link name="holder">
<visual>
<geometry>
<cylinder radius="${holder_radius}" length="${holder_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="holder2base" type="fixed">
<parent link="base"/>
<child link="holder"/>
<origin xyz="${holder_x} ${holder_y} ${holder_z}"/>
</joint>
<xacro:property name="radar_length" value="0.05"/>
<xacro:property name="radar_radius" value="0.03"/>
<xacro:property name="radar_x" value="0"/>
<xacro:property name="radar_y" value="0"/>
<xacro:property name="radar_z" value="${holder_length/2+radar_length/2}"/>
<link name="radar">
<visual>
<geometry>
<cylinder radius="${radar_radius}" length="${radar_length}"/>
</geometry>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/>
<material name="black"/>
</visual>
</link>
<joint name="radar2holder" type="fixed">
<parent link="holder"/>
<child link="radar"/>
<origin xyz="${radar_x} ${radar_y} ${radar_z}"/>
</joint>
</robot>
如果把代码里面的<material name="black"></material>赋予实际数值就会变成这样---看不见
因此代码里面的那些个有个material便签但是没有实际数值的地方都是用来通过编译的,用来欺骗软件检查的。不是用来真实的显示颜色的,如果是要真实显示颜色就要赋予实际的非0像素值。因为黑色在rivz2里面看不见