Ubuntu安裝ROS2
根據官方Distributions說明,Dashing Diademata
爲Ubuntu 18.04上支持最長的版本,下一個長期版本目標系統爲 Ubuntu 20.04,因此作爲學習的話,建議安裝該版本,同時經過簡單測試,當前ROS2對一般開發者並不算太友好,很多功能包也沒有,不建議作爲主力開發使用。
ROS2架構說明可參考ROS2探索總結(六)——迎接ROS2.0時代的到來
一、安裝
- 設置本地環境
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8
- 設置軟件源
sudo apt update && sudo apt install curl gnupg2 lsb-release
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64,arm64] http://packages.ros.org/ros2/ubuntu `lsb_release -cs` main" > /etc/apt/sources.list.d/ros2-latest.list'
- 安裝
sudo apt update
- 完整版
sudo apt install ros-dashing-desktop
- 基礎版,無GUI工具
sudo apt install ros-dashing-ros-base
- 安裝
argcomplete
(可選)
用於ROS2的命令行工具
sudo apt install python3-argcomplete
- 配置環境
若多版本共存,或以ros1爲主力開發,不要寫入到.bashrc
,使用ros2前在終端手動輸入加載ros2環境
source /opt/ros/dashing/setup.bash
單版本使用,或以ros2爲主力開發
echo "source /opt/ros/dashing/setup.bash" >> ~/.bashrc
- 安裝
RMW implementation
RMW implementation
爲中間件,默認使用FastRTPS
sudo apt update
sudo apt install ros-dashing-rmw-opensplice-cpp # for OpenSplice
sudo apt install ros-dashing-rmw-connext-cpp # for RTI Connext (requires license agreement)
切換中間件
RMW_IMPLEMENTATION=rmw_opensplice_cpp
: OpenSplice
RMW_IMPLEMENTATION=rmw_connext_cpp
: RTI Connext(Bouncy新增)
- 安裝
ros1_bridge
ros1_bridge
用於ros1和ros2通訊,使ros2可以使用ros1的功能包。
sudo apt update
sudo apt install ros-dashing-ros1-bridge
- 測試
ros2 run demo_nodes_cpp talker
ros2 run demo_nodes_cpp listener
二、使用
- 安裝
colcon
在Bouncy
版本後,ROS2採用colcon
替代 ament_tools
用於程序編譯。
sudo apt install python3-colcon-common-extensions
- 創建工作空間
mkdir -p ~/ros2_example_ws/src
cd ~/ros2_example_ws
完整目錄結構
src
,程序目錄build
,編譯的緩存信息和中間文件install
,編譯輸出文件目錄log
,colcon
調用的各種日誌信息
當前目錄結構(編譯後會自動生成其餘目錄)
.
└── src
1 directory, 0 files
- 創建功能包
這裏直接clone例程。
git clone https://github.com/ros2/examples src/examples
切換版本到Bouncy
cd ~/ros2_example_ws/src/examples/
git checkout $ROS_DISTRO
cd ~/ros2_example_ws
當前目錄結構
.
└── src
└── examples
├── CONTRIBUTING.md
├── LICENSE
├── rclcpp
├── rclpy
└── README.md
4 directories, 3 files
手動創建功能包
usage: ros2 pkg create [-h] [--package_format {2,3}]
[--description DESCRIPTION] [--license LICENSE]
[--destination-directory DESTINATION_DIRECTORY]
[--build-type {cmake,ament_cmake}]
[--dependencies DEPENDENCIES [DEPENDENCIES ...]]
[--maintainer-email MAINTAINER_EMAIL]
[--maintainer-name MAINTAINER_NAME]
[--cpp-node-name CPP_NODE_NAME]
[--cpp-library-name CPP_LIBRARY_NAME]
package_name
- 編譯
colcon build --symlink-install
當前目錄結構
.
├── build
├── install
├── log
└── src
4 directories, 0 files
- 加載環境
編譯完成後,在install
目錄下會生成配置文件setup.bash
,運行該文件可以加載運行所需相關環境,確保在運行節點前在終端運行該文件。
. install/setup.bash
- 測試
啓動訂閱節點
ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function
啓動發佈節點
ros2 run examples_rclcpp_minimal_publisher publisher_member_function
參考
Installing ROS2 via Debian Packages