# 前言: 你有可能讀過這份筆記:[WSL Ubuntu ROS gazebo 環境實作](https://hackmd.io/qMBSwzaxQiuouKfqZnZ0aQ?view) 以下的操作環境基礎皆承襲上述筆記的狀態 且接下來預設你: 1.會使用linux環境 2.會使用基礎ros 3.會sdf、urdf檔(xml語法) **#注意以下範例指令與程式碼中的<your_xxx>請替換成你自己命名的名稱!** # Refferences: 1.[使用ROS開啟Gazebo](https://axxx2934.medium.com/%E4%BD%BF%E7%94%A8ros%E9%96%8B%E5%95%9Fgazebo-92271e0ec410) 2.[ROS 安裝與教學與Gazebo](https://hackmd.io/@afB3H1HCSXqZfB9gz4ZmqA/rJVmRaUmi) 3.[一個營建系學生的韌體之路-自走車篇](https://ithelp.ithome.com.tw/users/20140433/ironman/6692) 4.[ROS系統控制自走車搭配點雲雷達(隧道檢測裝置) ](https://ithelp.ithome.com.tw/m/users/20129807/ironman/3282) # 1.建立及載入基礎空白世界 1.在workspace中建立gazebo的package * `cd ~/catkin_ws/src` * `catkin_create_pkg <your_pkg> std_msgs rospy roscpp gazebo_ros gazebo_plugins` 2.退回workspace層級編譯 * `cd ~/catkin_ws` * `catkin_make` 3.package內建立`launch`及`worlds`資料夾 * `cd ~/catkin_ws/src/<your_pkg>` * `mkdir launch` * `mkdir worlds` 4.在`worlds`資料夾中建立一個`.world`檔 * `cd ~/catkin_ws/src/<your_pkg>/worlds` * `code <your_world>.world` <your_world>.world: ``` <?xml version="1.0" ?> <sdf version="1.4"> <world name="default"> <!-- a global light source --> <include> <uri>model://ground_plane</uri> </include> <!-- a ground plane --> <include> <uri>model://sun</uri> </include> </world> </sdf> ``` 5.在`launch`資料夾中建立一個`.launch`檔 * `cd ~/catkin_ws/src/<pkg_name>/launch` * `code <your_launch>.launch` <your_launch>.launch: ``` <launch> <!-- We resume the logic in empty_world.launch, changing only the name of the world to be launched --> <include file="$(find gazebo_ros)/launch/empty_world.launch"> <arg name="world_name" value="$(find <your_pkg>)/worlds/<your_world>"/> <!-- more default parameters can be changed here --> </include> </launch> ``` 6.回到`catkin_ws`做`catkin_make`與`source devel/setup.bash` * `cd ~/catkin_ws` * `catkin_make` * `source devel/setup.bash` 7.啟動gazebo並載入世界,啟動`.launch`,可見到基礎空白世界 * `roslaunch <your_pkg> <your_launch>.launch` # 2.建立你的機器人並放入世界(.urdf) 1.package內建立`urdf`資料夾 * `cd ~/catkin_ws/src/<your_pkg>` * `mkdir urdf` 2.在`urdf`資料夾中建立一個`.urdf`檔 * `cd ~/catkin_ws/src/<your_pkg>/urdf` * `code <your_urdf>.urdf` <your_urdf>.urdf: ``` <?xml version="1.0"?> <robot name="practice_car"> <!-- 顏色材質 --> <material name="blue"> <color rgba="0 0 0.5 1"/> </material> <material name="red"> <color rgba="0.5 0 0 1"/> </material> <material name="green"> <color rgba="0 0.5 0 1"/> </material> <material name="white"> <color rgba="1 1 1 1"/> </material> <!-- 車體 --> <link name="base_link"/> <link name="chaiss"> <visual> <geometry> <box size="0.6 0.4 0.2"/> </geometry> <origin xyz="0 0 0.1" rpy="0 0 0"/> <material name="blue"/> </visual> <collision> <geometry> <box size="0.6 0.4 0.2"/> </geometry> <origin xyz="0 0 0.1" rpy="0 0 0"/> </collision> <inertial> <origin xyz="0 0 0.1" rpy="0 0 0"/> <mass value="1.0"/> <inertia ixx="0.017" ixy="0" ixz="0" iyy="0.033" iyz="0" izz="0.043" /> </inertial> </link> <joint name="base_joint" type="fixed"> <parent link="base_link"/> <child link="chaiss"/> <origin xyz="0 0 0.1" rpy="0 0 0"/> </joint> <!-- 右輪 --> <link name="right_wheel"> <visual> <geometry> <cylinder radius="0.1" length="0.05" /> </geometry> <origin xyz="0 0 0" rpy="1.57 0 0" /> <material name="white"/> </visual> <collision> <geometry> <cylinder radius="0.1" length="0.05" /> </geometry> <origin xyz="0 0 0" rpy="1.57 0 0" /> </collision> <inertial> <origin xyz="0 0 0" rpy="1.57 0 0" /> <mass value="0.5"/> <inertia ixx="0.00135" ixy="0" ixz="0" iyy="0.00135" iyz="0" izz="0.0025" /> </inertial> </link> <joint name="right_wheel_joint" type="continuous"> <parent link="chaiss"/> <child link="right_wheel"/> <origin xyz="-0.15 -0.225 0" rpy="0 0 0"/> <axis xyz="0 1 0"/> </joint> <!-- 左輪 --> <link name="left_wheel"> <visual> <geometry> <cylinder radius="0.1" length="0.05" /> </geometry> <origin xyz="0 0 0" rpy="1.57 0 0" /> <material name="white"/> </visual> <collision> <geometry> <cylinder radius="0.1" length="0.05" /> </geometry> <origin xyz="0 0 0" rpy="1.57 0 0" /> </collision> <inertial> <origin xyz="0 0 0" rpy="1.57 0 0" /> <mass value="0.5"/> <inertia ixx="0.00135" ixy="0" ixz="0" iyy="0.00135" iyz="0" izz="0.0025" /> </inertial> </link> <joint name="left_wheel_joint" type="continuous"> <parent link="chaiss"/> <child link="left_wheel"/> <origin xyz="-0.15 0.225 0" rpy="0 0 0"/> <axis xyz="0 1 0"/> </joint> <!-- 牛眼輪 --> <link name="front_wheel"> <visual> <geometry> <sphere radius="0.1" /> </geometry> <material name="red"/> </visual> <collision> <geometry> <sphere radius="0.1" /> </geometry> </collision> <inertial> <mass value="0.5"/> <inertia ixx="0.002" ixy="0" ixz="0" iyy="0.002" iyz="0" izz="0.002" /> </inertial> </link> <joint name="front_wheel_joint" type="fixed"> <parent link="chaiss"/> <child link="front_wheel"/> <origin xyz="0.2 0 0" rpy="0 0 0"/> </joint> </robot> ``` 3.一樣回到`catkin_ws`做`catkin_make`與`source devel/setup.bash` 4.若你的世界已經關閉則再次開啟世界 5.將機器人生成進世界中,可以看到世界中出現你的小車 * ``rosrun gazebo_ros spawn_model -file `rospack find <your_pkg>`/urdf/<your_urdf>.urdf -urdf -x 0 -y 0 -z 1 -model <your_urdf>`` # 3. ![005JteS5gy1hkbfc674sgj30u013sqcx](https://hackmd.io/_uploads/Sy9z9t88ye.jpg)