2025年6月25日 星期三

在 ROS 2 中,用 colcon build 建立的 package 驗證

 在 ROS 2 中,用 colcon build 建立的 package 要能夠出現在 ros2 pkg list 裡,必須要滿足幾個條件。這背後的登錄流程其實是靠安裝階段所產生的一些目錄與檔案,ROS 2 會根據這些來辨識 package 是否存在。


✅ 關鍵是這兩個檔案系統結構:

在你執行:

bash
colcon build

之後,會在你的 workspace 安裝目錄(例如:install/)內建立以下結構:

1. 🔹 套件描述檔(Package Manifest)

bash
install/<package_name>/share/<package_name>/package.xml

這個是 ROS 2 用來登錄 package 的核心資訊來源,包含:

  • <name>(套件名稱)

  • <version>

  • <dependencies>

📌 這個是 ros2 pkg list 檢索的依據之一!


2. 🔹 ROS 套件註冊目錄(ament index)

bash
install/ament_index/resource_index/packages/<package_name>

這是一個空檔或簡單內容的文字檔,但它的存在代表這個 package 可被 ROS 2 搜尋到

📌 ros2 pkg list 其實就是在這個路徑下掃描所有註冊過的套件。


✅ 如何建立這些檔案?

這些檔案會在 colcon build 成功後由 setup.pyCMakeLists.txt 自動產出。要讓它成功登錄,需注意:

🧩 若是 Python package,setup.py 要包含:

python
data_files=[ ('share/ament_index/resource_index/packages', ['resource/' + package_name]), (os.path.join('share', package_name), ['package.xml']), ]

並且 resource/<package_name> 要存在(即使是空檔)。


🔍 檢查登錄狀況的方法

✅ 檢查 package 是否登錄:

bash
ls install/ament_index/resource_index/packages/

你應該會看到:

nginx
ros_maze_house_world roscar4wheel

✅ 檢查某個套件的 manifest 是否存在:

bash
cat install/roscar4wheel/share/roscar4wheel/package.xml

📌 總結:ROS 2 登錄 package 需要什麼?

檔案或目錄作用
install/<pkg>/share/<pkg>/package.xml提供套件資訊
install/ament_index/resource_index/packages/<pkg>登錄在 ROS 套件索引
setup.py or CMakeLists.txt 中正確指定安裝路徑確保安裝時產生上面這些

如果你有 colcon build 成功,但還是沒出現在 ros2 pkg list 中,我可以幫你檢查 build 後的 install 資料夾結構,是否要幫你分析?你只要貼上:

bash
tree -L 3 install/roscar4wheel


沒有留言:

張貼留言