首先创建软件包来存储机器人模型
catkin_create_pkg mastering_ros_robot_description_pkg roscpp tf geometry_msgs urdf rviz xacro
安装为安装的软件包
1 sudo apt install ros-melodic-urdf 2 sudo apt install ros-melodic-sacro
sudo apt install liburdfdom-tools
sudo apt install ros-melodic-joint-state-publisher-gui
melodic为ros版本下载时改为自己对应版本即可
然后创建一个urdf模型
1 <?xml version="1.0"?> 2 <robot name="pan_tilt"> 3 4 <link name="base_link"> 5 6 <visual> 7 <geometry> 8 <cylinder length="0.01" radius="0.2"/> 9 </geometry> 10 <origin rpy="0 0 0" xyz="0 0 0"/> 11 <material name="yellow"> 12 <color rgba="1 1 0 1"/> 13 </material> 14 </visual> 15 16 <collision> 17 <geometry> 18 <cylinder length="0.03" radius="0.2"/> 19 </geometry> 20 <origin rpy="0 0 0" xyz="0 0 0"/> 21 </collision> 22 <inertial> 23 <mass value="1"/> 24 <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/> 25 </inertial> 26 </link> 27 28 <joint name="pan_joint" type="revolute"> 29 <parent link="base_link"/> 30 <child link="pan_link"/> 31 <origin xyz="0 0 0.1"/> 32 <axis xyz="0 0 1" /> 33 <limit effort="300" velocity="0.1" lower="-3.14" upper="3.14"/> 34 <dynamics damping="50" friction="1"/> 35 </joint> 36 37 <link name="pan_link"> 38 <visual> 39 <geometry> 40 <cylinder length="0.4" radius="0.04"/> 41 </geometry> 42 <origin rpy="0 0 0" xyz="0 0 0.09"/> 43 <material name="red"> 44 <color rgba="0 0 1 1"/> 45 </material> 46 </visual> 47 <collision> 48 <geometry> 49 <cylinder length="0.4" radius="0.06"/> 50 </geometry> 51 <origin rpy="0 0 0" xyz="0 0 0.09"/> 52 </collision> 53 <inertial> 54 <mass value="1"/> 55 <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/> 56 </inertial> 57 </link> 58 59 <joint name="tilt_joint" type="revolute"> 60 <parent link="pan_link"/> 61 <child link="tilt_link"/> 62 <origin xyz="0 0 0.2"/> 63 <axis xyz="0 1 0" /> 64 <limit effort="300" velocity="0.1" lower="-4.64" upper="-1.5"/> 65 <dynamics damping="50" friction="1"/> 66 </joint> 67 68 <link name="tilt_link"> 69 <visual> 70 <geometry> 71 <cylinder length="0.4" radius="0.04"/> 72 </geometry> 73 <origin rpy="0 1.5 0" xyz="0 0 0"/> 74 <material name="green"> 75 <color rgba="1 0 0 1"/> 76 </material> 77 </visual> 78 <collision> 79 <geometry> 80 <cylinder length="0.4" radius="0.06"/> 81 </geometry> 82 <origin rpy="0 1.5 0" xyz="0 0 0"/> 83 </collision> 84 <inertial> 85 <mass value="1"/> 86 <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/> 87 </inertial> 88 89 </link> 90 91 92 </robot>
注意在在</robot> 后面不能有换行 否则可能出错
标签<link>即为连杆 <joint>为关节 <visual>描述连杆的可见外观
使用命令可以检查urdf是否包含错误
check_urdf pan_tilt.urdf
以下即为成功
pan_tilt.urdf robot name is: pan_tilt ---------- Successfully Parsed XML --------------- root Link: base_link has 1 child(ren) child(1): pan_link child(1): tilt_link
然后可以在rviz上查看
创建一个view_demo.launch启动文件
1 <launch> 2 <arg name="model" /> 3 <param name="robot_description" textfile="$(find mastering_ros_robot_description_pkg)/urdf/pan_tilt.urdf" /> 4 <param name="use_gui" value="true"/> 5 6 <node name="joint_state_publisher_gui" pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" /> 7 <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" /> 8 <node name="rviz" pkg="rviz" type="rviz" args="-d $(find mastering_ros_robot_description_pkg)/urdf.rviz" required="true" /> 9 10 </launch>
然后启动模型
roscore
roslaunch mastering_ros_robot_description_pkg view_demo.launch
出现的WARN 可以忽略
然后出现下图
在map那里选择base_link
然后Add添加RobotModel就可以显示模型
在三维空间中, 要精确描述一个刚性体的姿态, 仅仅使用他的xyz坐标是不够的, 还需要使用rpy. rpy角是描述船舶在海中航行时姿态的一种方法.
将船的行驶方向取为z轴, 绕z轴旋转称为滚动(Roll), 绕y轴旋转称为俯仰(Pitch), 绕x轴旋转称为偏转(Yaw).
只能沿着y轴旋转, 则需要添加<axis xyz="0 1 0"/>
ros-urdf标签