在终端输入如下命令
// type in the terminator mkdir -p smartcar_description/src cd smartcar_description/src // package name catkin_create_pkg smartcar_description cd smartcar_description mkdir urdf launch在urdf文件中添加smartcar.urdf 文件
<?xml version="1.0"?> <robot name="smartcar"> <link name="base_link"> <visual> <geometry> <box size="0.25 .16 .05"/> </geometry> <origin rpy="0 0 1.57075" xyz="0 0 0"/> <material name="blue"> <color rgba="0 0 .8 1"/> </material> </visual> </link> <link name="right_front_wheel"> <visual> <geometry> <cylinder length=".02" radius="0.025"/> </geometry> <material name="black"> <color rgba="0 0 0 1"/> </material> </visual> </link> <joint name="right_front_wheel_joint" type="continuous"> <axis xyz="0 0 1"/> <parent link="base_link"/> <child link="right_front_wheel"/> <origin rpy="0 1.57075 0" xyz="0.08 0.1 -0.03"/> <limit effort="100" velocity="100"/> <joint_properties damping="0.0" friction="0.0"/> </joint> <link name="right_back_wheel"> <visual> <geometry> <cylinder length=".02" radius="0.025"/> </geometry> <material name="black"> <color rgba="0 0 0 1"/> </material> </visual> </link> <joint name="right_back_wheel_joint" type="continuous"> <axis xyz="0 0 1"/> <parent link="base_link"/> <child link="right_back_wheel"/> <origin rpy="0 1.57075 0" xyz="0.08 -0.1 -0.03"/> <limit effort="100" velocity="100"/> <joint_properties damping="0.0" friction="0.0"/> </joint> <link name="left_front_wheel"> <visual> <geometry> <cylinder length=".02" radius="0.025"/> </geometry> <material name="black"> <color rgba="0 0 0 1"/> </material> </visual> </link> <joint name="left_front_wheel_joint" type="continuous"> <axis xyz="0 0 1"/> <parent link="base_link"/> <child link="left_front_wheel"/> <origin rpy="0 1.57075 0" xyz="-0.08 0.1 -0.03"/> <limit effort="100" velocity="100"/> <joint_properties damping="0.0" friction="0.0"/> </joint> <link name="left_back_wheel"> <visual> <geometry> <cylinder length=".02" radius="0.025"/> </geometry> <material name="black"> <color rgba="0 0 0 1"/> </material> </visual> </link> <joint name="left_back_wheel_joint" type="continuous"> <axis xyz="0 0 1"/> <parent link="base_link"/> <child link="left_back_wheel"/> <origin rpy="0 1.57075 0" xyz="-0.08 -0.1 -0.03"/> <limit effort="100" velocity="100"/> <joint_properties damping="0.0" friction="0.0"/> </joint> <link name="head"> <visual> <geometry> <box size=".02 .03 .03"/> </geometry> <material name="white"> <color rgba="1 1 1 1"/> </material> </visual> </link> <joint name="tobox" type="fixed"> <parent link="base_link"/> <child link="head"/> <origin xyz="0 0.08 0.025"/> </joint> </robot>在launch文件中添加base.urdf.rviz.launch文件
// type in the terminator <launch> <arg name="model" /> <arg name="gui" default="False" /> <param name="robot_description" textfile="$(find smartcar_description)/urdf/smartcar.urdf" /> <param name="use_gui" value="$(arg gui)"/> <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node> <node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher" /> <node name="rviz" pkg="rviz" type="rviz" args="-d $(find smartcar_description)/urdf.rviz" /> </launch>编译并运行
catkin_make source devel/setup.bash roslaunch smartcar_description base.urdf.rviz.launch注意:
<param name="robot_description" textfile="$(find smartcar_description)/urdf/smartcar.urdf" />和
<param name="robot_description" command="$(find xacro)/xacro --inorder '$(find smartcar_description)/urdf/smartcar.urdf' "/>具有同样的效果,但一般使用第二个。(注意:xacro.py已弃用,且使用 --inorder )
在urdf文件中添加robot1_base.xacro文件
<?xml version="1.0"?> <robot xmlns:xacro="http://www.ros.org/wiki/xacro" xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor" xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller" xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface" name="robot0"> <xacro:property name="length_wheel" value="0.05" /> <xacro:property name="radius_wheel" value="0.05" /> <xacro:macro name="default_inertial" params="mass"> <inertial> <mass value="${mass}" /> <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0" /> </inertial> </xacro:macro> <link name="base_footprint"> <visual> <geometry> <box size="0.001 0.001 0.001"/> </geometry> <origin rpy="0 0 0" xyz="0 0 0"/> </visual> <xacro:default_inertial mass="0.0001"/> </link> <gazebo reference="base_footprint"> <material>Gazebo/Green</material> <turnGravityOff>false</turnGravityOff> </gazebo> <joint name="base_footprint_joint" type="fixed"> <origin xyz="0 0 0" /> <parent link="base_footprint" /> <child link="base_link" /> </joint> <link name="base_link"> <visual> <geometry> <box size="0.2 .3 .1"/> </geometry> <origin rpy="0 0 1.54" xyz="0 0 0.05"/> <material name="white"> <color rgba="1 1 1 1"/> </material> </visual> <collision> <geometry> <box size="0.2 .3 0.1"/> </geometry> </collision> <xacro:default_inertial mass="10"/> </link> <link name="wheel_1"> <visual> <geometry> <cylinder length="${length_wheel}" radius="${radius_wheel}"/> </geometry> <!-- <origin rpy="0 1.5 0" xyz="0.1 0.1 0"/> --> <origin rpy="0 0 0" xyz="0 0 0"/> <material name="black"> <color rgba="0 0 0 1"/> </material> </visual> <collision> <geometry> <cylinder length="${length_wheel}" radius="${radius_wheel}"/> </geometry> </collision> <xacro:default_inertial mass="1"/> </link> <link name="wheel_2"> <visual> <geometry> <cylinder length="${length_wheel}" radius="${radius_wheel}"/> </geometry> <!-- <origin rpy="0 1.5 0" xyz="-0.1 0.1 0"/> --> <origin rpy="0 0 0" xyz="0 0 0"/> <material name="black"/> </visual> <collision> <geometry> <cylinder length="${length_wheel}" radius="${radius_wheel}"/> </geometry> </collision> <xacro:default_inertial mass="1"/> </link> <link name="wheel_3"> <visual> <geometry> <cylinder length="${length_wheel}" radius="${radius_wheel}"/> </geometry> <!-- <origin rpy="0 1.5 0" xyz="0.1 -0.1 0"/> --> <origin rpy="0 0 0" xyz="0 0 0"/> <material name="black"/> </visual> <collision> <geometry> <cylinder length="${length_wheel}" radius="${radius_wheel}"/> </geometry> </collision> <xacro:default_inertial mass="1"/> </link> <link name="wheel_4"> <visual> <geometry> <cylinder length="${length_wheel}" radius="${radius_wheel}"/> </geometry> <!-- <origin rpy="0 1.5 0" xyz="-0.1 -0.1 0"/> --> <origin rpy="0 0 0" xyz="0 0 0" /> <material name="black"/> </visual> <collision> <geometry> <cylinder length="${length_wheel}" radius="${radius_wheel}"/> </geometry> </collision> <xacro:default_inertial mass="1"/> </link> <joint name="base_to_wheel1" type="continuous"> <parent link="base_link"/> <child link="wheel_1"/> <origin rpy="1.5707 0 0" xyz="0.1 0.15 0"/> <axis xyz="0 0 1" /> </joint> <joint name="base_to_wheel2" type="continuous"> <axis xyz="0 0 1" /> <anchor xyz="0 0 0" /> <limit effort="100" velocity="100" /> <parent link="base_link"/> <child link="wheel_2"/> <origin rpy="1.5707 0 0" xyz="-0.1 0.15 0"/> </joint> <joint name="base_to_wheel3" type="continuous"> <parent link="base_link"/> <axis xyz="0 0 1" /> <child link="wheel_3"/> <origin rpy="1.5707 0 0" xyz="0.1 -0.15 0"/> </joint> <joint name="base_to_wheel4" type="continuous"> <parent link="base_link"/> <axis xyz="0 0 1" /> <child link="wheel_4"/> <origin rpy="1.5707 0 0" xyz="-0.1 -0.15 0"/> </joint> </robot>在launch文件中添加 display_xacro.launch文件
<launch> <arg name="model" /> <arg name="gui" default="False" /> <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find smartcar_description)/urdf/robot1_base.xacro'" /> <param name="use_gui" value="$(arg gui)"/> <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node> <node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher" /> <node name="rviz" pkg="rviz" type="rviz" args="-d $(find urdf_tutorial)/urdf.rviz" /> </launch>注意: launch 文件中的package name “smartcar_description” 要与自己设置的package name 一致。 编译并运行
catkin_make source devel/setup.bash roslaunch smartcar_description display_xacro.launch如果想显示joint_state_publisher对话框(通过对话框调节关节角度)type follow:
roslaunch smartcar_description display_xacro.launch gui:=true注意:
<node name="rviz" pkg="rviz" type="rviz" args="-d $(find urdf_tutorial)/urdf.rviz" />打开rviz并显示。(需要指定rviz配置文件,不指定不会出错,需要添加Add RobotModel并将其head选择为fixed frame) 如果没有上面一行,需要type in temrinal :
rosrun rviz rviz特别注意: 对于浮动机座机器人来说,不要设置world无意义的连杆。下面代码不允许出现。 《也许其在RBDL计算动力学中是有用的》
<link name="world"> </link> <joint name="world_base_joint" type="floating"> <origin xyz="-0.8 0.5 0.9" rpy="0 0 0" /> <parent link="world" /> <child link="torsor" /> </joint>