关键术语
urdf:urdf的全称是 Unified Robot Description Format ,即:统一的机器人描述格式. 该格式本质上是xml格式. 在ROS中,我们可以通过编写urdf文件来建立机器人模型,并通过rviz工具进行可视化.
xacro:urdf文件只能是一个单一的文件,我们不能在urdf文件中包含其他的urdf文件,因此,当我们建立比较复杂并且各模块复用程度较高的机器人模型的时候,通过urdf创建的机器人代码量会很大,为了减少代码量,ROS同时也支持xacro宏来创建机器人模型. xacro支持常量和块调用,并且支持加减乘除等功能,最后我们还可以通过ROS提供的工具来将xacro文件转换成urdf文件.
创建工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ..
catkin_make
在工作空间中创建功能包
cd ~/catkin_ws/src
catkin_create_pkg custom_urdf roscpp urdf tf rviz geometry_msgs
cd ..
catkin_make
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
向功能包添加机器人的urdf模型
roscd custom_urdf
mkdir rviz
mkdir launch
mkdir urdf && cd urdf
gedit four_wheels_car.urdf
向four_wheels_car.urdf
文件添加如下的内容:
<?xml version="1.0" ?>
<robot name="four_wheels_car" xmlns:xacro="http://ros.org/wiki/xacro">
<link name="base_link">
<visual>
<geometry>
<box size="0.2 .3 .1"/>
</geometry>
<origin rpy="0 0 0" xyz="0 0 0.05"/>
<material name="white">
<color rgba="1 1 1 1"/>
</material>
</visual>
</link>
<joint name="wheel01_joint" type="fixed">
<parent link="base_link"/>
<child link="wheel01_link"/>
<origin xyz = " 0 0 0" rpy = "0 0 0"/>
</joint>
<link name="wheel01_link">
<visual>
<geometry>
<cylinder length="0.05" radius="0.05"/>
</geometry>
<origin rpy="0 1.57 0" xyz="0.1 0.1 0"/>
<material name="black">
<color rgba="0 0 0 1"/>
</material>
</visual>
</link>
<joint name="wheel02_joint" type="fixed">
<parent link="base_link"/>
<child link="wheel02_link"/>
<origin xyz = "0 0 0" rpy = "0 0 0"/>
</joint>
<link name="wheel02_link">
<visual>
<geometry>
<cylinder length="0.05" radius="0.05"/>
</geometry>
<origin rpy="0 1.57 0" xyz="-0.1 0.1 0"/>
<material name="black"/>
</visual>
</link>
<joint name="wheel03_joint" type="fixed">
<parent link="base_link"/>
<child link="wheel03_link"/>
<origin xyz = "0 0 0" rpy = "0 0 0"/>
</joint>
<link name="wheel03_link">
<visual>
<geometry>
<cylinder length="0.05" radius="0.05"/>
</geometry>
<origin rpy="0 1.57 0" xyz="0.1 -0.1 0"/>
<material name="black"/>
</visual>
</link>
<joint name="wheel04_joint" type="fixed">
<parent link="base_link"/>
<child link="wheel04_link"/>
<origin xyz = "0 0 0" rpy = "0 0 0"/>
</joint>
<link name="wheel04_link">
<visual>
<geometry>
<cylinder length="0.05" radius="0.05"/>
</geometry>
<origin rpy="0 1.57 0" xyz="-0.1 -0.1 0"/>
<material name="black"/>
</visual>
</link>
</robot>
保存文件并退出.
编写launch启动文件
roscd custom_urdf/launch
gedit view_car.launch
输入如下的内容,保存并退出:
<launch>
<arg name = "model"/>
<param name = "robot_description" textfile = "$(find custom_urdf)/urdf/four_wheels_car.urdf"/>
<param name = "use_gui" value = "true"/>
<node name = "joint_state_publisher"
pkg = "joint_state_publisher"
type = "joint_state_publisher"/>
<node name = "robot_state_publisher"
pkg = "robot_state_publisher"
type = "state_publisher"/>
<node name = "rviz"
pkg = "rviz"
type = "rviz"
args = "-d $(find custom_urdf)/rviz/urdf.rviz"
required = "true"/>
</launch>
注意:节点rviz中的args参数加载功能包的 rviz 目录下的 .rviz 配置文件,因为一开始我们并没有配置rviz任何选项,所以该配置文件并不存在,只有当我们通过launch文件打开了rviz,通过配置rviz然后保存配置文件到功能包的rviz目录下,并将配置文件另存为urdf.rviz文件,当我们再次启动rviz节点的时候,rviz就会自动加载配置文件,我们就不需要手动设置了.
启动节点,通过rviz查看机器人模型
roslaunch custom_urdf view_car.launch
向功能包添加机器人的xacro文件
pan_tilt.xacro
<?xml version="1.0" ?>
<robot name="pan_tilt" xmlns:xacro="http://ros.org/wiki/xacro">
<xacro:property name = "base_link_length" value = "0.01"/>
<xacro:property name = "base_link_radius" value = "0.2"/>
<xacro:property name = "pan_link_length" value = "0.4"/>
<xacro:property name = "pan_link_radius" value = "0.04"/>
<xacro:property name = "tilt_link_length" value = "0.4"/>
<xacro:property name = "tilt_link_radius" value = "0.04"/>
<xacro:macro name = "inertial_matrix" params = "mass">
<inertial>
<mass value = "${mass}"/>
<inertia ixx = "0.5" ixy = "0.0" ixz = "0.0"
iyy = "0.5" iyz = "0.0"
izz = "0.5"/>
</inertial>
</xacro:macro>
<link name = "base_link">
<visual>
<geometry>
<cylinder length = "${base_link_length}" radius = "${base_link_radius}"/>
</geometry>
<origin xyz = "0 0 0" rpy = "0 0 0"/>
<material name = "yellow">
<color rgba = "1 1 0 1"/>
</material>
</visual>
<collision>
<geometry>
<cylinder length="${base_link_length+0.02}" radius="0.2"/>
</geometry>
<origin rpy="0 0 0" xyz="0 0 0"/>
</collision>
<xacro:inertial_matrix mass="1"/>
</link>
<joint name="pan_joint" type="revolute">
<parent link="base_link"/>
<child link="pan_link"/>
<origin xyz="0 0 0.1"/>
<axis xyz="0 0 1" />
<limit effort="300" velocity="0.1" lower="-3.14" upper="3.14"/>
<dynamics damping="50" friction="1"/>
</joint>
<link name="pan_link">
<visual>
<geometry>
<cylinder length="${pan_link_length}" radius="${pan_link_radius}"/>
</geometry>
<origin rpy="0 0 0" xyz="0 0 0.09"/>
<material name="red">
<color rgba="0 0 1 1"/>
</material>
</visual>
<collision>
<geometry>
<cylinder length="${pan_link_length}" radius="${pan_link_radius+0.02}"/>
</geometry>
<origin rpy="0 0 0" xyz="0 0 0.09"/>
</collision>
<xacro:inertial_matrix mass="1"/>
</link>
<joint name="tilt_joint" type="revolute">
<parent link="pan_link"/>
<child link="tilt_link"/>
<origin xyz="0 0 0.2"/>
<axis xyz="0 1 0" />
<limit effort="300" velocity="0.1" lower="-4.64" upper="-1.5"/>
<dynamics damping="50" friction="1"/>
</joint>
<link name="tilt_link">
<visual>
<geometry>
<cylinder length="${tilt_link_length}" radius="${tilt_link_radius}"/>
</geometry>
<origin rpy="0 1.5 0" xyz="0 0 0"/>
<material name="green">
<color rgba="1 0 0 1"/>
</material>
</visual>
<collision>
<geometry>
<cylinder length="${tilt_link_length}" radius="${tilt_link_radius+0.2}"/>
</geometry>
<origin rpy="0 1.5 0" xyz="0 0 0"/>
</collision>
<xacro:inertial_matrix mass="1"/>
</link>
</robot>
编写启动文件
view_demo_xacro.launch
<launch>
<arg name="model"/>
<param name="robot_description"
command="$(find xacro)/xacro --inorder $(find custom_urdf)/urdf/pan_tilt.xacro"/>
<param name="use_gui" value="true"/>
<node name="joint_state_publisher"
pkg="joint_state_publisher"
type="joint_state_publisher"/>
<node name="robot_state_publisher"
pkg="robot_state_publisher"
type="state_publisher"/>
<node name="rviz"
pkg="rviz"
type="rviz"
args="-d $(find custom_urdf)/urdf.rviz"
required="true" />
</launch>
在rviz中查看机器人模型
roslaunch custom_urdf view_demo_xacro.launch
我们可以把 view_demo_xacro.launch 文件中的如下内容:
<param name="robot_description" command="$(find xacro)/xacro --inorder $(find custom_urdf)/urdf/pan_tilt.xacro"/>
换成:
<param name="robot_description" command="$(find xacro)/xacro $(arg model)"/>
这样我们可以通过传入参数来加载机器人模型:
roscd custom_urdf
roslaunch custom_urdf view_demo_xacro.launch model:=urdf/pan_tilt.xacro
完成了机器人简单的urdf和xacro文件的编写
Enjoy It!