學習寫urdf有幾個地方需要注意
1. 一定要記住它使用的是右手坐標系。
x正方向朝左, y 正方向向內, z軸正方向朝上
2. 構建樹結構, 即寫link和joint
3. 每個link的參考坐標系都在它的底部,並與關節的參考坐標系正交,為了添加尺寸,需要指定偏移從一個link到它的關節的子link, 這通過添加origin到每個節點解決。
這么說origin表示的是關節相對於父關節的距離和旋轉, xyz和rpy
用ros wiki上的r2d2實例學習
1.初始版本
<?xml version="1.0"?> <robot name="origins"> <link name="base_link"> <visual> <geometry> <cylinder length="0.6" radius="0.2"/> </geometry> </visual> </link> <link name="right_leg"> <visual> <geometry> <box size="0.6 .2 .1"/> </geometry> </visual> </link> <joint name="base_to_right_leg" type="fixed"> <parent link="base_link"/> <child link="right_leg"/> </joint> </robot>
2. 加入關節origin
因為basekink沒有改變,所以這里省略baselink...
<link name="right_leg"> <visual> <geometry> <box size="0.6 .2 .1"/> </geometry> </visual> </link> <joint name="base_to_right_leg" type="fixed"> <parent link="base_link"/> <child link="right_leg"/> <origin xyz="0.22 0 .25"/>(左移和上移, x軸左為正, z為上, y 垂直平面, 右手坐標系) </joint>
3. 加入link origin y軸 旋轉
因為basekink沒有改變,所以這里省略baselink...
<link name="right_leg"> <visual> <geometry> <box size="0.6 .2 .1"/> </geometry> <origin rpy="0 1.57075 0"/> (y軸旋轉90度) </visual> </link> <joint name="base_to_right_leg" type="fixed"> <parent link="base_link"/> <child link="right_leg"/> <origin xyz="0.22 0 .25"/> </joint>
4. 加入link origin xyz偏移
因為basekink沒有改變,所以這里省略baselink...
<link name="right_leg"> <visual> <geometry> <box size="0.6 .2 .1"/> </geometry> <origin rpy="0 1.57075 0" xyz = “0 0 -0.3 ”/> (y軸旋轉90度, 往下0.3) </visual> </link> <joint name="base_to_right_leg" type="fixed"> <parent link="base_link"/> <child link="right_leg"/> <origin xyz="0.22 0 .25"/> </joint>
5. 給模型加入材質(穿上一件衣服)
<?xml version="1.0"?> <robot name="origins"> <link name="base_link"> <visual> <geometry> <cylinder length="0.6" radius="0.2"/> </geometry> <material name = "blue"> <color rgba = "0 0 .8 1"/> </material> </visual> </link> <link name="right_leg"> <visual> <geometry> <box size="0.6 .2 .1"/> </geometry> <material name = "white"> <color rgba = "1 1 1 1"/> </material> <origin rpy = "0 1.57 0" xyz = "0 0 -0.3"/> </visual> </link>
加上左臂
<?xml version="1.0"?> <robot name="origins"> <link name="base_link"> <visual> <geometry> <cylinder length="0.6" radius="0.2"/> </geometry> <material name = "blue"> <color rgba = "0 0 .8 1"/> </material> </visual> </link> <link name="right_leg"> <visual> <geometry> <box size="0.6 .2 .1"/> </geometry> <material name = "white"> <color rgba = "1 1 1 1"/> </material> <origin rpy = "0 1.57 0" xyz = "0 0 -0.3"/> </visual> </link> <joint name="base_to_right_leg" type="fixed"> <parent link="base_link"/> <child link="right_leg"/> <origin xyz="0.22 0 .25"/> </joint> <link name="left_leg"> <visual> <geometry> <box size="0.6 .2 .1"/> </geometry> <material name = "white"> <color rgba = "1 1 1 1"/> </material> <origin rpy = "0 1.57 0" xyz = "0 0 -0.3"/> </visual> </link> <joint name="base_to_left_leg" type="fixed"> <parent link="base_link"/> <child link="left_leg"/> <origin xyz="-0.22 0 .25"/> 僅僅這里不同, 關節右移所以是負 </joint> </robot>
6. 添加頭,sphere幾何體
注意每定義個link都要為它添加一個關節,否則check_urdf時報錯
Error: Failed to find root link: Two root links found: [base_link] and [head]
沒有給頭link添加關節導致的錯誤。
<link name = "head"> <visual> <geometry> <sphere radius = "0.2"/> </geometry> <material name = "white"> <color rgba = "1 1 1 1"/> </material> </visual> </link> <joint name = "head_swivel" type = "fixed"> <parent link = "base_link"/> <child link = "head"/> <origin xyz = "0 0 0.3"/> </joint>
8. 添加車輪和車輪底座
<link name = "right_wheel_base"> //右車輪底座 <visual> <geometry> <box size = "0.1 .4 .1"/> </geometry> <material name = "yellow"> <color rgba = "1 1 0 1"/> </material> </visual> </link> <joint name = "right_wheel_base_joint" type = "fixed"> <parent link = "right_leg"/> <child link = "right_wheel_base"/> <origin xyz = "0 0 -0.65"/> </joint> <link name = "right_front_wheel"> // 右前輪 <visual> <geometry> <cylinder length = "0.08" radius = "0.04"/> </geometry> <material name = "black"> <color rgba = " 0 0 0 1"/> </material> </visual> </link> <joint name = "right_front_wheel_joint" type = "fixed"> <parent link = "right_wheel_base"/> <child link = "right_front_wheel"/> <origin xyz = "0 .14 -.05" rpy = "1.5 0 1.5"/> // 旋轉了車輪 </joint> <link name = "right_back_wheel"> // 右后輪 <visual> <geometry> <cylinder length = "0.08" radius = "0.04"/> </geometry> <material name = "black"> <color rgba = " 0 0 0 1"/> </material> </visual> </link> <joint name = "right_back_wheel_joint" type = "fixed"> <parent link = "right_wheel_base"/> <child link = "right_back_wheel"/> <origin xyz = "0 -.14 -.05" rpy = "1.5 0 1.5"/> // y軸為負,往后移 </joint>
左輪也是一樣設置,只需把right 改成left即可, 最后效果如下