# การใช้งาน Simulation ใน Gazebo ในบทนี้จะอธิบายเกี่ยวกับการ จำลองการทำงานของหุ่นยนต์ TurtleBot3 Simulation บน Gazebo ซึ่งเป็นเครื่องมือสำคัญใน ROS 2 สำหรับการทดสอบและพัฒนาหุ่นยนต์โดยไม่จำเป็นต้องมีฮาร์ดแวร์จริง (https://emanual.robotis.com/docs/en/platform/turtlebot3/simulation/) ## ติดตั้งแพ็กเกจที่จำเป็น **สามารถข้ามไปทำข้อ 11.3 เปิดใช้งาน Simulation ใน Gazebo ได้เลย เนื่องจาก Ubuntu ที่ให้นั้นได้ติดตั้งแพ็กเกจเป็นที่เรียบร้อยแล้ว** ติดตั้ง Gazebo ```{code-block} bash sudo apt update sudo apt install ros-humble-gazebo-* -y ``` ```{image} images/c15/15.1.1.png :width: 80% :align: center ``` ```{image} images/c15/15.1.2.png :width: 80% :align: center ``` ติดตั้ง Cartographer (ใช้สำหรับ SLAM) ```{code-block} bash sudo apt install ros-humble-cartographer ros-humble-cartographer-ros -y ``` ```{image} images/c15/15.1.3.png :width: 80% :align: center ``` ติดตั้ง Navigation2 (ใช้สำหรับนำทาง) ```{code-block} bash sudo apt install ros-humble-navigation2 ros-humble-nav2-bringup -y ``` ```{image} images/c15/15.1.4.png :width: 80% :align: center ``` ## ติดตั้งแพ็กเกจ TurtleBot3 **สามารถข้ามไปทำข้อ 11.3 เปิดใช้งาน Simulation ใน Gazebo ได้เลย เนื่องจาก Ubuntu ที่ให้นั้นได้ติดตั้งแพ็กเกจเป็นที่เรียบร้อยแล้ว** เปิด Terminal แล้วติดตั้งแพ็กเกจที่จำเป็นของ TurtleBot3 ```{code-block} bash mkdir -p ~/turtlebot3_ws/src cd ~/turtlebot3_ws/src git clone -b humble https://github.com/ROBOTIS-GIT/DynamixelSDK.git git clone -b humble https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b humble https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b humble https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git ``` ```{image} images/c15/15.2.1.png :width: 80% :align: center ``` ```{code-block} bash cd ~/turtlebot3_ws && colcon build --symlink-install ``` ```{image} images/c15/15.2.2.png :width: 80% :align: center ``` ตั้งค่า Environment สำหรับ TurtleBot3 ในไฟล์ bashrc ด้วยคำสั่ง: ```{code-block} bash gedit ~/.bashrc ``` จากนั้นเพิ่มคำสั่งดังนี้ และกด Save: ```{code-block} bash source ~/turtlebot3_ws/install/setup.bash echo 'export ROS_DOMAIN_ID=30 #TURTLEBOT3' echo 'source /usr/share/gazebo/setup.sh' export TURTLEBOT3_MODEL=burger ``` ```{image} images/c15/15.2.3.png :width: 80% :align: center ``` ## เปิดใช้งาน Simulation ใน Gazebo เมื่อแพ็กเกจจำเป็นถูกติดตั้งเรียบร้อยแล้ว สามารถเลือก Environment (โลกจำลอง) โดยยกมาให้ทดสอบ 2 แบบ ดังนี้ \ Empty World ```{code-block} bash ros2 launch turtlebot3_gazebo empty_world.launch.py ``` ```{image} images/c15/15.3.1.png :width: 80% :align: center ``` TurtleBot3 World ```{code-block} bash ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py ``` ```{image} images/c15/15.3.2.png :width: 80% :align: center ```