確保支持環境語言 UTF-8
將 ROS2 apt repository 添加到系統
首先須確保 Ubuntu Universe repository 已啟用。
sudo apt install software-properties-common
sudo add-apt-repository universe
接著加入 ROS2 金鑰
sudo apt update && sudo apt install curl
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
更新 sources list
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
更新 apt
sudo apt update
安裝 Desktop 版
sudo apt install ros-foxy-desktop python3-argcomplete
安裝 ROS2 命令自動補全工具
sudo apt install libpython3-dev python3-pip
pip3 install argcomplete
安裝檢查依賴包工具
sudo apt install -y python3-rosdep
pip3 install argcomplete
colcon
編譯工具安裝
sudo apt install python3-colcon-common-extensions git
設定 ROS2 環境變量
source /opt/ros/foxy/setup.bash
echo "source /opt/ros/foxy/setup.bash" >>.bashrc
設定 colcon 自動補全工具環境變量
echo "source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash" >> ~/.bashrc
刷新環境變量
source ~/.bashrc
在其中一個終端機,啟動 C++ 發佈 node talker
source /opt/ros/foxy/setup.bash
ros2 run demo_nodes_cpp talker
在另一個終端機,啟動 Python 接收 node listener
:
source /opt/ros/foxy/setup.bash
ros2 run demo_nodes_py listener
colcon_cd
跟 ROS1 的 roscd 類似
設定 colcon_cd 環境變量路徑
echo "source /usr/share/colcon_cd/function/colcon_cd.sh" >> ~/.bashrc
設置 colcon_cd 打開的根目錄,預設是ros2_install
,根據自己實際情況設置。
未來打算將 workspace 叫做 ros2_ws
,故改成:
echo "export _colcon_cd_root=~/ros2_ws" >> ~/.bashrc
刷新環境變量
source ~/.bashrc