首先創建軟件包來存儲機器人模型
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標簽