2025年6月25日 星期三

ROS2 launch >> colcon build 成功編譯 ROS 2 套件

 當你使用 colcon build 成功編譯 ROS 2 套件後,

執行方式取決於你的套件內容(是執行 launch file 還是 node),下面是完整流程與常見執行方式:


✅ 1. source 安裝空間(必須做)

每次開啟新終端機後,都要先 source

bash
source ~/data/ros2_ws3/install/setup.bash

如果沒做這步,ROS 2 只會找 /opt/ros/humble 裡的套件,你自己 build 的找不到。


✅ 2. 執行方式總覽

類型指令範例說明
✅ 執行 launch 檔ros2 launch my_pkg my_launch.launch.py启动多个 node / Gazebo / RViz 等
✅ 執行 node(Python/C++)ros2 run my_pkg my_node启动单个 ROS 2 node
✅ 檢查套件已安裝`ros2 pkg listgrep my_pkg`
✅ 檢查 node 名稱ros2 run my_pkg + Tab自動列出套件內可執行的 node
✅ 檢查 launch 檔是否存在ros2 launch my_pkg + Tab自動補齊所有可執行 launch file

✅ 3. 執行 launch 檔

如果你套件是要啟動 Gazebo、RViz2 或多個 node,使用 .launch.py

bash
ros2 launch roscar4wheel display.launch.py

⚠️ 要確保:

  • launch/display.launch.py 存在

  • setup.py 有包含:

    python
    ('share/' + package_name + '/launch', glob('launch/*.launch.py')),
  • colcon build 過後、source install/setup.bash 有執行


✅ 4. 執行單一 node(適用於 entry_points 有註冊)

如果你有在 setup.py 裡寫:

python
entry_points={ 'console_scripts': [ 'my_node = roscar4wheel.my_node:main', ], },

就可以執行:

bash
ros2 run roscar4wheel my_node

✅ 5. 範例:確認你的 package 狀態

bash
ros2 pkg list | grep roscar4wheel ros2 launch roscar4wheel display.launch.py ros2 run roscar4wheel <按兩次 Tab 看有哪些 node 可用>

🧠 建議:加入 bashrc

bash
echo "source ~/data/ros2_ws3/install/setup.bash" >> ~/.bashrc source ~/.bashrc

這樣以後每次打開 terminal 都不用手動 source。


⛳ 小結:最常見的錯誤原因

錯誤訊息解法
Package not found沒有 source install/setup.bash
launch file not foundsetup.py 沒有安裝 launch 檔,或沒 build
command not foundentry_points 沒寫好,或缺 __init__.py

可以根據你目前的套件結構,補好 setup.pyentry_pointslaunch.py

沒有留言:

張貼留言