xacro文件是urdf文件的进阶版,可以通过宏定义,文件包含来精简模型文件。还可以通过定义常量、变量等来反复调用,相当有用哦~~
先贴代码:
<?xml version="1.0" ?> <robot name="fourwheelrobot" xmlns:xacro="http://www.ros.org/wiki/xacro"> <!--Defineing namespace--> <!--property list --> <xacro:property name="M_PI" value="3.141592"/> <xacro:property name="base_mass" value="20" /> <xacro:property name="base_length" value="0.80"/> <xacro:property name="base_width" value="0.550"/> <xacro:property name="base_height" value="0.30"/> <xacro:property name="wheel_mass" value="2" /> <xacro:property name="wheel_radius" value="0.122"/> <xacro:property name="wheel_length" value="0.062"/> <xacro:property name="wheel_joint_x" value="0.25"/> <xacro:property name="wheel_joint_y" value="0.306"/> <xacro:property name="wheel_joint_z" value="-0.1"/> <!--Defineing the colors--> <material name="black"> <color rgba="0 0 0 1"/> </material> <material name="yellow"> <color rgba="1 0.4 0 1"/> </material> <!-- Macro for inertia matrix --> <xacro:macro name="cylinder_inertial_matrix" params="m r h"> <inertial> <mass value="${m}" /> <inertia ixx="${m*(3*r*r+h*h)/12}" ixy = "0" ixz = "0" iyy="${m*(3*r*r+h*h)/12}" iyz = "0" izz="${m*r*r/2}" /> </inertial> </xacro:macro> <xacro:macro name="box_inertial_matrix" params="m a b c"> <inertial> <mass value="${m}" /> <inertia ixx="${m*(b*b+c*c)/12}" ixy = "0" ixz = "0" iyy="${m*(c*c+a*a)/12}" iyz = "0" izz="${m*(a*a+b*b)/12}" /> </inertial> </xacro:macro> <!--Macro for the wheel --> <xacro:macro name="wheel" params="prefix1 prefix2 reflect1 reflect2"> <joint name="${prefix1}_${prefix2}_wheel_joint" type="continuous"> <origin xyz="${reflect1*wheel_joint_x} ${reflect2*wheel_joint_y} ${wheel_joint_z}"/> <parent link="base_link"/> <child link="${prefix1}_${prefix2}_wheel_link"/> <axis xyz="0 1 0"/> </joint> <link name="${prefix1}_${prefix2}_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="black"/> </visual> <collision> <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" /> <geometry> <cylinder radius="${wheel_radius}" length = "${wheel_length}"/> </geometry> </collision> <cylinder_inertial_matrix m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" /> </link> <gazebo reference="${prefix1}_${prefix2}_wheel_link"> <material>Gazebo/Black</material> </gazebo> <!-- Transmission is important to link the joints and the controller --> <transmission name="${prefix1}_${prefix2}_wheel_joint_trans"> <type>transmission_interface/SimpleTransmission</type> <joint name="prefix1}_${prefix2}_wheel_joint" > <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface> </joint> <actuator name="${prefix1}_${prefix2}_wheel_joint_motor"> <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface> <mechanicalReduction>1</mechanicalReduction> </actuator> </transmission> </xacro:macro> <!--start --> <xacro:macro name="fourwheelrobot_base_gazebo"> <link name="base_footprint"> <visual> <origin xyz="0 0 0" rqy="0 0 0"/> <geometry> <box size="0.001 0.001 0.001"/> </geometry> </visual> </link> <gazebo reference="base_footprint"> <turnGravityOff>false</turnGravityOff> </gazebo> <joint name="base_footprint_joint" type="fixed"> <origin xyz="0 0 ${base_height/2}" rpy="0 0 0" /> <parent link="base_footprint"/> <child link="base_link" /> </joint> <link name="base_link"> <visual> <origin xyz="0 0 0" rqy="0 0 0"/> <geometry> <box size="${base_length} ${base_width} ${base_height}"/> </geometry> <material name="yellow"/> </visual> <collision> <origin xyz=" 0 0 0" rpy="0 0 0" /> <geometry> <box size="${base_length} ${base_width} ${base_height}"/> </geometry> </collision> <box_inertial_matrix m="${base_mass}" a="${base_length}" b="${base_width}" c="${base_height}" /> </link> <gazebo reference="base_link"> <material>Gazebo/Yellow</material> </gazebo> <wheel prefix1="left" prefix2="front" reflect1="1" reflect2="1"/> <wheel prefix1="left" prefix2="back" reflect1="-1" reflect2="1"/> <wheel prefix1="right" prefix2="front" reflect1="1" reflect2="-1"/> <wheel prefix1="right" prefix2="back" reflect1="-1" reflect2="-1"/>
<gazebo> <plugin name="skid_steer_drive_controller" filename="libgazebo_ros_skid_steer_drive.so"> <rosDebugLevel>Debug</rosDebugLevel> <publishWheelTF>true</publishWheelTF> <publishTf>1</publishTf> <publishWheelJointState>true</publishWheelJointState> <alwaysOn>true</alwaysOn> <updateRate>100.0</updateRate> <robotNamespace>/</robotNamespace> <leftFrontJoint>left_front_wheel_joint</leftFrontJoint> <rightFrontJoint>right_front_wheel_joint</rightFrontJoint> <leftRearJoint>left_back_wheel_joint</leftRearJoint> <rightRearJoint>right_back_wheel_joint</rightRearJoint> <wheelSeparation>${wheel_joint_y*2}</wheelSeparation> <wheelDiameter>${2*wheel_radius}</wheelDiameter> <broadcastTF>1</broadcastTF> <robotBaseFrame>base_footprint</robotBaseFrame> <torque>20</torque> <topicName>cmd_vel</topicName> <odometryFrame>odom</odometryFrame> <odometryTopic>odom</odometryTopic> </plugin> </gazebo>
</xacro:macro>
</robot>
1.常量定义:
<xacro:property name="M_PI" value="3.141592"/>
常量使用:
<origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
2.变量定义:
变量使用:
(这次例子中没有哎~0-0)
3.宏定义:
<xacro:macro name="cylinder_inertial_matrix" params="m r h"> <inertial> <mass value="${m}" /> <inertia ixx="${m*(3*r*r+h*h)/12}" ixy = "0" ixz = "0" iyy="${m*(3*r*r+h*h)/12}" iyz = "0" izz="${m*r*r/2}" /> </inertial> </xacro:macro>
宏定义调用:
<box_inertial_matrix m="${base_mass}" a="${base_length}" b="${base_width}" c="${base_height}" />
这个例子中还添加了传动件(transmission),滑动驱动控制器插件(plugin),
<!-- Transmission is important to link the joints and the controller --> <transmission name="${prefix1}_${prefix2}_wheel_joint_trans"> <type>transmission_interface/SimpleTransmission</type> <joint name="prefix1}_${prefix2}_wheel_joint" > <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface> </joint> <actuator name="${prefix1}_${prefix2}_wheel_joint_motor"> <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface> <mechanicalReduction>1</mechanicalReduction> </actuator> </transmission>
<gazebo> <plugin name="skid_steer_drive_controller" filename="libgazebo_ros_skid_steer_drive.so"> <rosDebugLevel>Debug</rosDebugLevel> <publishWheelTF>true</publishWheelTF> <publishTf>1</publishTf> <publishWheelJointState>true</publishWheelJointState> <alwaysOn>true</alwaysOn> <updateRate>100.0</updateRate> <robotNamespace>/</robotNamespace> <leftFrontJoint>left_front_wheel_joint</leftFrontJoint> <rightFrontJoint>right_front_wheel_joint</rightFrontJoint> <leftRearJoint>left_back_wheel_joint</leftRearJoint> <rightRearJoint>right_back_wheel_joint</rightRearJoint> <wheelSeparation>${wheel_joint_y*2}</wheelSeparation> <wheelDiameter>${2*wheel_radius}</wheelDiameter> <broadcastTF>1</broadcastTF> <robotBaseFrame>base_footprint</robotBaseFrame> <torque>20</torque> <topicName>cmd_vel</topicName> <odometryFrame>odom</odometryFrame> <odometryTopic>odom</odometryTopic> </plugin> </gazebo>
参考网站: