:~/ros2_compose/ros2_data/ros2_ws4/src/twowheel_bot
├── CMakeLists.txt
├── package.xml
├── setup.cfg
├── setup.py
├── <rviz>├── <resource>
├──< include >
├── <launch>
├── <src>
├── <twowheel_bot>
└── <urdf>
CMakeLists.txt
ROS 2(ament_cmake)的建置設定。即使主要寫 Python,也常放一個最小 CMake 以相容 colcon build;若有 C++ 節點/庫,就在這裡 add_executable()/install()。
package.xml
套件中繼資料與相依(<depend>rclpy</depend> 等)。rosdep、建置系統與發佈都依此辨識。
setup.cfg
Python 套件安裝行為的輔助設定(例如安裝腳本路徑)。可有可無,視專案需求。
setup.py
Python 套件打包/安裝腳本(ament_python)。在這裡宣告:
要安裝的資料檔(URDF、launch、rviz…)
entry_points.console_scripts:把 Python 函式掛成可執行檔(如 simple_controller = twowheel_bot.simple_controller:main)
include/
C++ 標頭檔放這裡(例如 include/twowheel_bot/xxx.hpp)。若只做 Python 可留空或不使用。
launch/
/放啟動檔(.launch.py),用來一次啟多個節點/參數,例如:
display.launch.py:載入 URDF、啟 robot_state_publisher、開 RViz。
teleop_demo.launch.py:啟 Python 控制節點並帶參數。
resource/
ament 資源索引目錄;裡面通常有一個與套件同名的空檔(resource/twowheel_bot),讓 ros2 pkg 能發現此套件。
rviz/
RViz 設定檔(.rviz)。例如 tf_view.rviz:預先配置 Fixed Frame、Grid、RobotModel 等。
src/
C++ 原始碼目錄(與 include/ 一起用)。例如 src/driver.cpp、src/node.cpp;最後由 CMakeLists.txt 生成可執行檔。
twowheel_bot/
Python 套件模組根目錄(對應 setup.py 的 packages=[...])。常見內容:
__init__.py:標記為 Python 套件。
simple_controller.py:發佈 /cmd_vel 的 Python 節點(rclpy),支援參數與鍵盤控制等。
其他共用程式(utils、drivers)。
urdf/
機器人描述檔(.urdf / .xacro)、網格 meshes/(若有)。例如 twowheel.urdf.xacro:底盤+左右輪的連結、關節、慣性、材質。
Python + C++ 可共存:twowheel_bot/ 走 Python;src//include/ 走 C++。兩邊分別由 setup.py 與 CMakeLists.txt 負責安裝。
新增/改檔後記得:colcon build → source install/setup.bash。
要讓 RViz/URDF/launch 等隨套件安裝,記得在 setup.py 的 data_files 把相對路徑列進去(或在 CMake 用 install(DIRECTORY ...))。
Python + C++ 可共存:twowheel_bot/ 走 Python;src//include/ 走 C++。兩邊分別由 setup.py 與 CMakeLists.txt 負責安裝。
新增/改檔後記得:colcon build → source install/setup.bash。
要讓 RViz/URDF/launch 等隨套件安裝,記得在 setup.py 的 data_files 把相對路徑列進去(或在 CMake 用 install(DIRECTORY ...))。
__init__.py:讓此資料夾成為可匯入的 Python 套件。
nodes/:很薄的執行入口(main()),entry_points 指到這裡最清楚,例如 twowheel_bot.nodes.teleop:main。
controllers/:控制迴路、狀態機(PID、路徑追蹤、導航策略…)。
drivers/:硬體 I/O(馬達、IMU、里程計、序列埠、CAN、GPIO)。
utils/(或 common/):共用工具(參數處理、時間/座標換算、logging helper)。
kinematics/:運動學/座標轉換(差速/全向/逆運動學)。
teleop/:鍵盤/搖桿/遙控橋接。
interfaces/:對 ROS 介面(msg/service/action)的包裝、型別轉換。
simulation/:Gazebo/仿真專用節點或橋接(spawn、reset、時鐘)。
config/(也可放在套件根目錄):YAML 參數檔(啟動時 --ros-args --params-file 載入)。
tests/:單元/整合測試(pytest)
meshes/
用途:存放機器人 3D 幾何模型,供 URDF/SDF 在 visual(外觀)與 collision(碰撞)使用。
常見副檔名:.stl、.dae(Collada)、.obj、貼圖 .png/.jpg。
重點建議
單位與座標:URDF 以 公尺(m) 為單位;遵循 REP-103(X 前、Y 左、Z 上)。
原點:網格本身的原點最好在輪心/連結中心;若不在,改用 URDF <origin ...> 修正位置/角度。
視覺 vs 碰撞:碰撞模型要低面數。可分資料夾:
meshes/
visual/ # 高精度外觀
collision/ # 低多邊形碰撞
貼圖/材質:.dae 可綁貼圖;Gazebo 也可用 material script。把貼圖一併放進套件。
URDF 參考寫法:
<visual>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<mesh filename="package://twowheel_bot/meshes/visual/base.stl" scale="1 1 1"/>
</geometry>
</visual>
<collision>
<geometry>
<mesh filename="package://twowheel_bot/meshes/collision/base.stl"/>
</geometry>
</collision>
scripts/
用途:存放可執行腳本(多為 Python 節點或 Shell 工具)。ROS 傳統慣例用 scripts/(複數)。
重點建議(ROS 2/ament_python)
Python 節點首選:把程式放 套件模組目錄(例 twowheel_bot/)並用 entry_points 暴露;scripts/ 通常放 shell 或不需作為 Python 模組匯入的工具。
Shebang & 權限:腳本需有 shebang、執行權限:
#!/usr/bin/env python3
chmod +x scripts/calibrate_wheels.py
安裝到套件(兩種方式):
entry_points(推薦):程式放在 twowheel_bot/ 內,於 setup.py:
entry_points={
'console_scripts': [
'simple_controller = twowheel_bot.simple_controller:main',
'teleop_key = twowheel_bot.teleop.key:main',
],
}
安裝 scripts 資料夾:用 data_files 讓腳本以檔案形式安裝(適合 .sh、工具腳本):
data_files=[
('share/ament_index/resource_index/packages', ['resource/twowheel_bot']),
('share/twowheel_bot', ['package.xml']),
('share/twowheel_bot/launch', ['launch/display.launch.py']),
('share/twowheel_bot/rviz', ['rviz/tf_view.rviz']),
('share/twowheel_bot/urdf', ['urdf/twowheel.urdf.xacro']),
('share/twowheel_bot/scripts',['scripts/calibrate_wheels.py']), # ←
]
執行時:
ros2 run twowheel_bot simple_controller # entry_points
# 或(若用純 scripts 檔案)
ros2 pkg prefix twowheel_bot && ./install/.../share/twowheel_bot/scripts/calibrate_wheels.py
小結
meshes/:放 3D 模型;注意單位、原點、座標與「視覺/碰撞」分離。
scripts/:放可執行腳本;ROS 2 建議 Python 節點以 entry_points 方式發佈,scripts/ 多用於 shell/工具或非模組化 Python 腳本。