我照着这篇文章搭了个smartcar小车的urdf文件
https://blog.csdn.net/qq_16775293/article/details/88189184
接着把urdf文件改成xacro文件,遵照古月居的教程来的,再用launch文件打开rviz看看我的小车,代码如下
<launch>
<!-- 调用xacro文件解析器 -->
<arg name="model" default="$(find xacro)/xacro --inorder '$(find smartcar_description)/urdf/xacro/smartcar.xacro'" />
<arg name="gui" default="true" />
<!-- 解析结果放进robot_description参数中 -->
<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 smartcar_description)/config/smartcar.rviz" required="true" />
</launch>
其中,smartcar.xacro和smartcar_base.xacro文件代码如下
<?xml version = "1.0" ?>
<robot name="smartcar" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- PROPERTY LIST -->
<xacro:property name="M_PI" value="3.1415926"/>
<xacro:property name="box_length" value="0.25"/>
<xacro:property name="box_width" value="0.16"/>
<xacro:property name="box_height" value="0.05"/>
<xacro:property name="wheel_radius" value="0.025"/>
<xacro:property name="wheel_length" value="0.02"/>
<xacro:property name="wheel_joint_x" value="0.08"/>
<xacro:property name="wheel_joint_y" value="0.1"/>
<xacro:property name="wheel_joint_z" value="0.03"/>
<!-- 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>
<material name="blue">
<color rgba="0 0.5 0.8 1"/>
</material>
<!-- Macro for robot wheel -->
<xacro:macro name="front_wheel" params="prefix reflect">
<joint name="${prefix}_front_wheel_joint" type="continuous">
<origin xyz="${reflect*wheel_joint_x} ${wheel_joint_y} ${-wheel_joint_z}" rpy="0 ${M_PI/2} 0"/>
<parent link="base_link"/>
<child link="${prefix}_front_wheel"/>
<axis xyz="0 0 1"/>
</joint>
<link name="${prefix}_front_wheel">
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
</geometry>
<material name="gray" />
</visual>
</link>
</xacro:macro>
<xacro:macro name="back_wheel" params="prefix reflect">
<joint name="${prefix}_back_wheel_joint" type="continuous">
<origin xyz="${reflect*wheel_joint_x} ${-wheel_joint_y} ${-wheel_joint_z}" rpy="0 ${M_PI/2} 0"/>
<parent link="base_link"/>
<child link="${prefix}_back_wheel"/>
<axis xyz="0 0 1"/>
</joint>
<link name="${prefix}_back_wheel">
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
</geometry>
<material name="gray" />
</visual>
</link>
</xacro:macro>
<xacro:macro name="smartcar_base">
<link name="base_footprint">
<visual>
<origin xyz="0 0 0" rpy="0 0 ${M_PI/2}" />
<geometry>
<box size="0.001 0.001 0.001" />
</geometry>
</visual>
</link>
<joint name="base_footprint_joint" type="fixed">
<origin xyz="0 0 ${box_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" rpy="0 0 ${M_PI/2}" />
<geometry>
<box size="${box_length} ${box_width} ${box_height}"/>
</geometry>
<material name="blue" />
</visual>
</link>
<xacro:front_wheel prefix="left" reflect="-1"/>
<xacro:front_wheel prefix="right" reflect="1"/>
<xacro:back_wheel prefix="left" reflect="-1"/>
<xacro:back_wheel prefix="right" reflect="1"/>
</xacro:macro>
</robot>
<?xml version="1.0"?>
<robot name="smartcar" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- This xacro includes all parts of the robot. The mbot_base is the main body and we may retrofit another devices -->
<xacro:include filename="$(find smartcar_description)/urdf/xacro/smartcar_base.xacro" />
<xacro:smartcar_base/>
</robot>
然后我加了一些gazebo的物理参数进去,得到的smartcar_base_gazebo.xacro以及smartcar_gazebo.xacro文件如下
<?xml version= "1.0" ?>
<robot name="smartcar" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- PROPERTY LIST -->
<xacro:property name="M_PI" value="3.1415926"/>
<xacro:property name="box_length" value="0.25"/>
<xacro:property name="box_width" value="0.16"/>
<xacro:property name="box_height" value="0.05"/>
<xacro:property name="wheel_radius" value="0.025"/>
<xacro:property name="wheel_length" value="0.02"/>
<xacro:property name="wheel_joint_x" value="0.08"/>
<xacro:property name="wheel_joint_y" value="0.1"/>
<xacro:property name="wheel_joint_z" value="0.03"/>
<xacro:property name="caster_joint_x" value="0.08"/>
<xacro:property name="caster_joint_y" value="0.1"/>
<xacro:property name="caster_joint_z" value="0.03"/>
<xacro:property name="caster_radius" value="0.025"/>
<xacro:property name="caster_length" value="0.02"/>
<xacro:property name="box_mass" value="1"/>
<xacro:property name="wheel_mass" value="0.1"/>
<xacro:property name="caster_mass" value="0.05"/>
<!-- 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>
<material name="blue">
<color rgba="0 0.5 0.8 1"/>
</material>
<!-- Macro for interial matrix -->
<xacro:macro name="cuboid_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*(a*a+c*c)/12}" iyz="0" izz="${m*(a*a+b*b)/12}"/>
</inertial>
</xacro:macro>
<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>
<!-- Macro for robot wheel -->
<xacro:macro name="wheel" params="prefix reflect">
<joint name="${prefix}_wheel_joint" type="continuous">
<origin xyz="${reflect*wheel_joint_x} ${wheel_joint_y} ${-wheel_joint_z}" rpy="0 ${M_PI/2} 0"/>
<parent link="base_link"/>
<child link="${prefix}_wheel_link"/>
<axis xyz="1 0 0"/>
</joint>
<link name="${prefix}_wheel_link">
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<cylinder radius="${wheel_radius}" length= "${wheel_length}"/>
</geometry>
<material name="gray" />
</visual>
<collision>
<origin xyz="0 0 0" rpy="0 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="${prefix}_wheel_link">
<material>Gazebo/Gray</material>
</gazebo>
<!-- Transmission is important to link the joints and the controller -->
<transmission name="${prefix}_wheel_joint_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="${prefix}_wheel_joint" >
<hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
</joint>
<actuator name="${prefix}_wheel_joint_motor">
<hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>
</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} ${reflect*caster_joint_y} ${-caster_joint_z}" rpy="0 ${M_PI/2} 0"/>
<parent link="base_link"/>
<child link="${prefix}_caster_link"/>
<axis xyz="1 0 0"/>
</joint>
<link name="${prefix}_caster_link">
<visual>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<cylinder radius="${caster_radius}" length= "${caster_length}"/>
</geometry>
<material name="black" />
</visual>
<collision>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<cylinder radius="${caster_radius}" length= "${caster_length}"/>
</geometry>
</collision>
<cylinder_inertial_matrix m="${caster_mass}" r="${caster_radius}" h="${caster_length}" />
</link>
<gazebo reference="${prefix}_caster_link">
<material>Gazebo/Black</material>
</gazebo>
</xacro:macro>
<!-- Macro for base of smartcar -->
<xacro:macro name="smartcar_base">
<link name="base_footprint">
<visual>
<origin xyz="0 0 0" rpy="0 0 ${M_PI/2}" />
<geometry>
<box size="0.001 0.001 0.001" />
</geometry>
</visual>
</link>
<gazebo reference="$base_footprint">
<turnGravituOff>false</turnGravituOff>
</gazebo>
<joint name="base_footprint_joint" type="fixed">
<origin xyz="0 0 ${box_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" rpy="0 0 ${M_PI/2}" />
<geometry>
<box size="${box_length} ${box_width} ${box_height}"/>
</geometry>
<material name="blue" />
</visual>
<collision>
<origin xyz=" 0 0 0" rpy="0 0 ${M_PI/2}" />
<geometry>
<box size="${box_length} ${box_width} ${box_height}"/>
</geometry>
</collision>
<cuboid_inertial_matrix m="${box_mass}" a="${box_width}" b="${box_length}" c="${box_height}"/>
</link>
<gazebo reference="$base_link">
<material>Gazebo/Blue</material>
</gazebo>
<xacro:wheel prefix="left" reflect="-1"/>
<xacro:wheel prefix="right" reflect="1"/>
<xacro:caster prefix="left" reflect="-1"/>
<xacro:caster prefix="right" reflect="1"/>
<!-- controller -->
<gazebo>
<plugin name="differential_drive_controller"
filename="libgazebo_ros_diff_drive.so">
<rosDebugLevel>Debug</rosDebugLevel>
<publishWheelTF>true</publishWheelTF>
<robotNamespace>/</robotNamespace>
<publishTf>1</publishTf>
<publishWheelJointState>true</publishWheelJointState>
<alwaysOn>true</alwaysOn>
<updateRate>100.0</updateRate>
<legacyMode>true</legacyMode>
<leftJoint>left_wheel_joint</leftJoint>
<rightJoint>right_wheel_joint</rightJoint>
<wheelSeparation>${wheel_joint_y*2}</wheelSeparation>
<wheelDiameter>${2*wheel_radius}</wheelDiameter>
<broadcastTF>1</broadcastTF>
<wheelTorque>30</wheelTorque>
<wheelAcceleration>1.8</wheelAcceleration>
<commandTopic>cmd_vel</commandTopic>
<odometryFrame>odom</odometryFrame>
<odometryTopic>odom</odometryTopic>
<robotBaseFrame>base_footprint</robotBaseFrame>
</plugin>
</gazebo>
</xacro:macro>
</robot>
<?xml version="1.0"?>
<robot name="smartcar" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- This xacro includes all parts of the robot. The mbot_base is the main body and we may retrofit another devices -->
<xacro:include filename="$(find smartcar_description)/urdf/xacro/gazebo/smartcar_base_gazebo.xacro" />
<xacro:smartcar_base_gazebo/>
</robot>
运行view_smartcar_gazebo_empty_world文件,让机器人在gazebo空环境中显现,代码如下
<launch>
<!-- 设置launch文件的参数 -->
<arg name="paused" default="false"/>
<arg name="use_sim_time" default="true"/>
<arg name="gui" default="true"/>
<arg name="headless" default="false"/>
<arg name="debug" default="false"/>
<!-- 运行gazebo仿真环境 -->
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="debug" value="$(arg debug)" />
<arg name="gui" value="$(arg gui)" />
<arg name="paused" value="$(arg paused)"/>
<arg name="use_sim_time" value="$(arg use_sim_time)"/>
<arg name="headless" value="$(arg headless)"/>
</include>
<!-- 加载机器人模型描述参数 -->
<param name="robot_description" command="$(find xacro)/xacro '$(find smartcar_description)/urdf/xacro/gazebo/smartcar_gazebo.xacro'" />
<!-- 运行joint_state_publisher节点,发布机器人的关节状态 -->
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
<!-- 运行robot_state_publisher节点,发布tf -->
<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
<param name="publish_frequency" type="double" value="50.0" />
</node>
<!-- 在gazebo中加载机器人模型-->
<node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
args="-urdf -model mrobot -param robot_description"/>
</launch>
结果运行这个launch文件,给我报错
我要是把smartcar_gazebo.xacro中的xacro:smartcar_base_gazebo去了倒是不报这个错误,但是就会报另一个错误,说我no link elements found in urdf files
请教好心的大佬这到底啥情况?