Wiki
環境準備與源碼地圖
本文由簡體中文內容確定性轉換,並受版本化術語表保護。
本套教程只使用 ROS 2 Humble。先準備能重複運行的環境,再開始讀源碼;學習工作區由你自己創建。
1.本節目標
先分清三件事:Ubuntu 22.04 提供操作系統環境;工作區保存你編寫的包;源碼閲讀目錄保存框架實現。安裝軟件包,不等於獲得它完整的 .cpp 源碼。
以下操作默認使用普通用户和 Bash 終端。軟件安裝與系統配置需要 sudo,創建工作區、構建和運行 ROS 程序不需要提權。
2.示例 1:準備 ROS 2 Humble 並檢查版本
先查看系統版本和字符編碼:
lsb_release -ds
locale
系統應為 Ubuntu 22.04,語言環境應支持 UTF-8。如果尚未安裝 ROS 2 Humble,先按 ROS 2 Humble 的 Ubuntu 安裝説明 配置語言環境和 ROS 2 軟件源。完成軟件源配置及該説明中的系統更新步驟後,安裝桌面版:
sudo apt-get update
sudo apt-get install -y ros-humble-desktop
已經安裝 Humble 的用户可以直接檢查環境:
source /opt/ros/humble/setup.bash
printenv ROS_DISTRO
ros2 --help
ROS_DISTRO 應輸出 humble。如果 setup.bash 不存在,先完成 Humble 安裝;如果版本不符,打開新終端並檢查是否自動加載了其它 ROS 發行版。
3.示例 2:安裝基礎開發與控制依賴
sudo apt-get update
sudo apt-get install -y build-essential cmake \
ros-humble-ros2-control ros-humble-ros2-controllers \
ros-humble-xacro ros-humble-robot-state-publisher \
ros-humble-rviz2 ros-humble-joint-state-publisher-gui \
python3-colcon-common-extensions python3-rosdep git ripgrep
安裝後檢查編譯工具和框架包的位置:
g++ --version
cmake --version
ros2 pkg prefix controller_manager
編譯器需要支持 C++17,不要求固定的補丁版本。最後一條應返回包的安裝前綴;如果仍找不到包,先確認安裝成功,並在當前終端重新 source Humble。
Fortress 依賴放在機械臂物理仿真,串口依賴放在虛擬串口與模擬下位機,按學習進度安裝。
4.示例 3:由你創建官方實例工作區
本套教程使用兩個工作區:~/ros2_control_demos_ws 運行官方實例;~/ros2_control_humble_ws 從從零創建學習工作區開始保存你親手編寫的包。它們都是你學習時執行命令創建的。
mkdir -p ~/ros2_control_demos_ws/src
cd ~/ros2_control_demos_ws/src
git clone --branch humble https://github.com/ros-controls/ros2_control_demos.git
cd ros2_control_demos
git checkout c273b1316cce043c252c593dc6a232ea57174690
cd ~/ros2_control_demos_ws
rosdep update --rosdistro humble
rosdep install --from-paths src --ignore-src -r -y --rosdistro humble
source /opt/ros/humble/setup.bash
colcon build --symlink-install --packages-up-to \
ros2_control_demo_example_1 ros2_control_demo_example_2 \
ros2_control_demo_example_3 ros2_control_demo_example_9 \
--cmake-args -DBUILD_TESTING=OFF
source install/setup.bash
如果提示 rosdep 尚未初始化,先執行一次 sudo rosdep init,再執行 rosdep update。不要重複初始化已經存在的配置。
這裏用 --packages-up-to,會包含工作區內的依賴包。當前 demos 中有 ros2_control_demo_test_utils,只選擇 example_1 而遺漏依賴會導致構建失敗;即使關閉測試,也應讓 colcon 正確解析依賴。
5.示例 4:下載供閲讀的框架源碼
閲讀目錄與 src 分開,避免 colcon 無意間編譯整套框架並覆蓋發行包。
mkdir -p ~/ros2_control_humble_reference
cd ~/ros2_control_humble_reference
git clone --depth 1 --branch 2.54.3 https://github.com/ros-controls/ros2_control.git
git clone --depth 1 --branch 2.54.1 https://github.com/ros-controls/ros2_controllers.git
git clone --depth 1 --branch 2.15.1 https://github.com/ros-controls/realtime_tools.git
git clone --depth 1 --branch 4.9.0 https://github.com/ros-controls/control_msgs.git
git clone --depth 1 --branch 0.7.21 https://github.com/ros-controls/gz_ros2_control.git
| 倉庫 | 本教程採用版本 | 首先閲讀的位置 |
|---|---|---|
| ros2_control | 2.54.3 | controller_manager、hardware_interface、controller_interface |
| ros2_controllers | 2.54.1 | forward_command_controller、joint_trajectory_controller、diff_drive_controller |
| realtime_tools | 2.15.1 | realtime_buffer.hpp |
| control_msgs | 4.9.0 | control_msgs/action/FollowJointTrajectory.action |
| gz_ros2_control | 0.7.21 | gz_ros2_control/src、gz_ros2_control_demos |
這些標籤用於對應教程中的源碼講解。Humble 安裝包可能具有不同的補丁版本;如果 API 或參數不同,先檢查安裝版本,再對照匹配的標籤。
dpkg-query -W ros-humble-ros2-control ros-humble-ros2-controllers \
ros-humble-realtime-tools ros-humble-control-msgs
ros2 pkg prefix hardware_interface
6.新終端的統一約定
每個終端都要 source。運行官方例子時 source demos 工作區;運行自編工程時 source learning 工作區。不要在 .bashrc 中同時疊加不相關的機械人工作區。一個 ROS domain 中一次只運行一個本教程機械人,避免多個 /controller_manager 和 /joint_states 相互干擾。
本教程不要求設置特定的 ROS_DOMAIN_ID。相關終端應使用一致的 domain 配置;若需要隔離多台機械人或多組實驗,再為各組選擇不同的 domain,並在同組終端中保持一致。
7.本節總結
source 決定當前終端能找到哪一套包;colcon build 產生安裝空間;pluginlib 從安裝空間查找插件。下載了源碼卻沒有構建、安裝和 source,系統不會自動使用它。