2025年6月25日 星期三

ROScar.urdf build roscar4wheel package for ros2_ws3 node

 你的 ROScar.urdf 已成功放置於 ROS 2 套件目錄 /ros2_ws3/src/roscar4wheel/urdf/roscar.urdf

接下來是建立這個 URDF 模型於 ROS 2 (Humble) + Gazebo 的完整步驟範例。


✅ 建立 ROS 2 套件 roscar4wheel

bash
cd ~/ros2_ws3/src ros2 pkg create --build-type ament_cmake roscar4wheel

✅ 套件目錄結構應為:

arduino
roscar4wheel/ ├── CMakeLists.txt ├── package.xml ├── launch/ │ └── display.launch.py ├── urdf/ │ └── roscar.urdf

✅ 編寫 launch 檔載入模型 + Gazebo

🔹 launch/display.launch.py

python
from launch import LaunchDescription from launch_ros.actions import Node import os from ament_index_python.packages import get_package_share_directory def generate_launch_description(): pkg_path = get_package_share_directory('roscar4wheel') urdf_path = os.path.join(pkg_path, 'urdf', 'roscar.urdf') return LaunchDescription([ Node( package='gazebo_ros', executable='spawn_entity.py', arguments=[ '-entity', 'roscar', '-file', urdf_path, '-x', '0', '-y', '0', '-z', '0.1' ], output='screen' ) ])

✅ 修改 CMakeLists.txt

加入:

cmake
install(DIRECTORY launch urdf DESTINATION share/${PROJECT_NAME} )

✅ 修改 package.xml

加入依賴:

xml
<exec_depend>gazebo_ros</exec_depend> <exec_depend>xacro</exec_depend>

✅ 編譯並啟動流程

bash
cd ~/ros2_ws3 colcon build --packages-select roscar4wheel source install/setup.bash gazebo # 開啟 Gazebo 手動或自訂 world ros2 launch roscar4wheel display.launch.py

✅ 額外建議(若模型未顯示):

  • 確認 URDF 中 mesh 路徑是否為 package://roscar4wheel/... 相對正確

  • 若有使用 .xacro 檔請先轉成 .urdf

  • 若需自動啟動 Gazebo,可在 launch 中加入:

python
from launch.actions import ExecuteProcess ExecuteProcess( cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so'], output='screen' )

是否需要我幫你整理成一份完整 zip 專案(含 launch、CMakeLists.txt、package.xml)?可直接放入 ros2_ws3 使用。 

沒有留言:

張貼留言