welcom ! Handel home

2025年6月23日 星期一

ROS 2 (Humble) 中建置「迷宮環境(Maze)」模擬場景 的完整說明

關於 ROS 2 (Humble) 中建置「迷宮環境(Maze)」模擬場景 的完整說明,

包含如何使用 .STL 格式建模物件、載入到 Gazebo 中,

以及整合 ROS 2 模組(如地圖、定位、導航)。


🔷 一、什麼是 STL 格式(Stereolithography)

  • .STL 是一種表示三角形網格的 3D 模型格式,常用於機構建模、3D 列印。

  • 特點:

    • 僅描述幾何形狀(不包含顏色 / 材質 / 質感)

    • 可匯入到 Gazebo 作為靜態碰撞體或視覺模型

STL 在 Gazebo 中會透過 <mesh> 標籤載入成 visualcollision 組件。


🔷 二、建立迷宮 STL 並匯入 Gazebo

1️⃣ 建模(你可使用以下任一工具)

  • Blender:免費強大的 3D 建模軟體(可輸出 .stl

  • Fusion 360 / FreeCAD:適合精密機械建模

  • TinkerCAD:簡易建模(教育用途)

匯出時請注意單位(cm / m),Gazebo 使用公尺為單位。


2️⃣ STL 模型放置到資源資料夾

建議結構如下(符合 ROS package):

bash
my_maze_world/ ├── meshes/ │ └── maze_model.stl ├── models/ │ └── maze/ │ ├── model.config │ └── model.sdf ├── launch/ │ └── maze_world.launch.py ├── worlds/ │ └── maze.world

3️⃣ 建立 model.sdf(用於 Gazebo 載入 STL)

xml
<!-- models/maze/model.sdf --> <sdf version="1.6"> <model name="maze"> <static>true</static> <link name="maze_link"> <visual name="maze_visual"> <geometry> <mesh> <uri>model://maze/meshes/maze_model.stl</uri> <scale>1 1 1</scale> </mesh> </geometry> </visual> <collision name="maze_collision"> <geometry> <mesh> <uri>model://maze/meshes/maze_model.stl</uri> <scale>1 1 1</scale> </mesh> </geometry> </collision> </link> </model> </sdf>

4️⃣ 建立 model.config

xml
<!-- models/maze/model.config --> <model> <name>maze</name> <version>1.0</version> <sdf version="1.6">model.sdf</sdf> <author> <name>Handel</name> <email>you@example.com</email> </author> <description>Maze simulation model</description> </model>

🔷 三、建立 Gazebo 世界檔(maze.world)

xml
<!-- worlds/maze.world --> <sdf version="1.6"> <world name="default"> <include> <uri>model://sun</uri> </include> <include> <uri>model://ground_plane</uri> </include> <include> <uri>model://maze</uri> </include> </world> </sdf>

🔷 四、ROS 2 Humble 中整合 Gazebo

1️⃣ 安裝 ROS 2 Gazebo 套件(如尚未安裝)

bash
sudo apt install ros-humble-gazebo-ros-pkgs ros-humble-gazebo-ros2-control

2️⃣ 建立 launch file

python
# launch/maze_world.launch.py from launch import LaunchDescription from launch_ros.actions import Node from launch.actions import ExecuteProcess def generate_launch_description(): return LaunchDescription([ ExecuteProcess( cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so', './worlds/maze.world'], cwd='<YOUR_PACKAGE_PATH>', output='screen' ) ])

或使用 gazebo_ros 提供的 gazebo.launch.py,只需指定世界檔。


🔷 五、啟動迷宮模擬環境

bash
cd ~/ros2_ws colcon build source install/setup.bash ros2 launch my_maze_world maze_world.launch.py

🔷 六、常見延伸整合

功能使用方式
導入 SLAM 地圖建構加入 SLAM node + SLAM toolbox (slam_toolbox)
AMCL 定位使用 nav2_bringup,載入 .pgm + .yaml 地圖並配置初始位姿
自走導航使用 Nav2 的 lifecycle bringup、設定路徑、goal pose
加入自走車模型使用 URDF 或 XACRO 匯入機器人模型,搭配差速控制與 sensor plugin
使用 Rviz2 配合觀察ros2 run rviz2 rviz2,載入機器人與地圖顯示,設定 Frame = map/base_link

✅ 小結

模組任務
.stl建立迷宮模型
model.sdf將 STL 載入 Gazebo 物件模型
maze.world定義整個模擬場景
launch.py啟動 Gazebo + 模型
Nav2導航與避障
RViz2觀察 SLAM / AMCL / 路徑等結果

如需:

  • 範例 STL 模型 + SDF + 世界地圖

  • 輸出 .pgm/.yaml 地圖給 Nav2 用

  • 加入機器人模型與差速控制器

我可以幫你整理完整的 ROS 2 專案架構與腳本,是否需要一份 zip 專案樣板或 Docker 化模擬環境?

沒有留言: