2025年6月24日 星期二

ros_maze_house.stl 模型

 ros_maze_house.stl 模型已整理至以下目錄結構:

markdown
ros_maze_house/ └── meshes/ └── ros_maze_house.stl

接下來提供如何在 ROS 2 + Gazebo 11(Focal, Foxy 適用) 的 Docker 環境中,將該 STL 模型載入 Gazebo,並建立 ROS 套件的完整流程。


✅ 一、建立 ROS 2 套件 ros_maze_house_world

1. 建議結構

bash
ros2_ws/ └── src/ └── ros_maze_house_world/ ├── launch/ │ └── maze_world.launch.py ├── models/ │ └── maze_house/ │ ├── model.config │ ├── model.sdf │ └── meshes/ │ └── ros_maze_house.stl └── worlds/ └── maze_house.world

✅ 二、編寫模型 SDF 檔案 model.sdf

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

✅ 三、建立 model.config

xml
<model> <name>maze_house</name> <version>1.0</version> <sdf version="1.6">model.sdf</sdf> <author> <name>ros-user</name> <email>ros@example.com</email> </author> <description>A STL-based maze model</description> </model>

✅ 四、建立 Gazebo 世界檔 maze_house.world

xml
<sdf version="1.6"> <world name="default"> <include> <uri>model://sun</uri> </include> <include> <uri>model://ground_plane</uri> </include> <include> <uri>model://maze_house</uri> </include> </world> </sdf>

✅ 五、建立 ROS 2 Launch 檔 maze_world.launch.py

python
from launch import LaunchDescription from launch.actions import ExecuteProcess from ament_index_python.packages import get_package_share_directory import os def generate_launch_description(): pkg_path = get_package_share_directory('ros_maze_house_world') world_path = os.path.join(pkg_path, 'worlds', 'maze_house.world') return LaunchDescription([ ExecuteProcess( cmd=[ 'gazebo', '--verbose', world_path, '-s', 'libgazebo_ros_factory.so' ], output='screen' ) ])

✅ 六、使用 Docker 建立 ROS 2 + Gazebo 環境

bash
docker run -it --rm \ --name ros2_gazebo_maze \ --env="DISPLAY" \ --env="QT_X11_NO_MITSHM=1" \ --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \ --volume="$HOME/.Xauthority:/root/.Xauthority:rw" \ --volume="$HOME/ros2_ws:/root/ros2_ws:rw" \ --net=host \ osrf/ros:foxy-desktop

⚠️ 若用 ROS 2 Humble 請使用 osrf/ros:humble-desktop,Gazebo 版本需切換為 Fortress。


✅ 七、Docker 容器中執行步驟

bash
cd /root/ros2_ws source /opt/ros/foxy/setup.bash colcon build --packages-select ros_maze_house_world source install/setup.bash ros2 launch ros_maze_house_world maze_world.launch.py

✅ 八、設定 GAZEBO_MODEL_PATH(避免模型找不到)

bash
export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:/root/ros2_ws/install/ros_maze_house_world/share/ros_maze_house_world/models

沒有留言:

張貼留言