---
tags: 交接, VSCLab, ROS
---
ROS2 Foxy 安裝與建置
==
{%hackmd B1216Feut %}
ROS2 提供兩種安裝方式,這裡用的是 Debian。和 ROS1 不同,ROS2 的官方安裝教學只包含了最基本的系統本身,而編譯、移動到 package 路徑底下等等功能所需的工具,都需要額外安裝。
## 01 | ROS Foxy 安裝
### 設定 locale
```bash
locale # check for UTF-8
sudo apt update && sudo apt install locales
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
locale # verify settings
```
正確的 locale 列表應該要長這樣:
<img src=https://i.imgur.com/ApRwBKA.png class="pic_center" width=50%>
<br>
### 設定 Sources
```bash
sudo apt update && sudo apt install curl gnupg2 lsb-release
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(source /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
```
### 安裝 ROS 2 Foxy
```bash
sudo apt update
sudo apt install ros-foxy-desktop
```
### 安裝 argcomplete 套件
```bash
sudo apt install -y python3-pip
pip3 install -U argcomplete
```
### 測試
**Terminal #1**
```bash
source /opt/ros/foxy/setup.bash
ros2 run demo_nodes_cpp talker
```
**Terminal #2**
```bash
source /opt/ros/foxy/setup.bash
ros2 run demo_nodes_py listener
```
<br>
## 02 | Colcon 安裝
colcon 是 ROS2 主要所使用的編譯器,這裡用的是「Outside the ROS project」的安裝方案。
```bash
sudo curl -s https://packagecloud.io/install/repositories/dirk-thomas/colcon/script.deb.sh | sudo bash
```
```bash
sudo apt install python3-colcon-common-extensions
```
執行 colcon 編譯時常搭配使用的 argument 有:
```bash
# 指定編譯 package
colcon build --packages-select [PKG_NAME [PKG_NAME …]]
# 跳過指定 package
colcon build --packages-skip [PKG_NAME [PKG_NAME …]]
# 使 python 等直譯語言所撰寫的程式不用重新編譯
colcon build --symlink-install
```
<br>
## 03 | shell 檔案設置
```bash=
# 使用 ROS Dashing
source /opt/ros/foxy/setup.bash
# 使用 auto complete
source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash
```
<br>
---
**參考資料**
+ [ROS2 Foxy 官方安裝教學](https://docs.ros.org/en/foxy/Installation/Ubuntu-Install-Debians.html)
+ [colcon 官方安裝教學](https://colcon.readthedocs.io/en/released/user/installation.html)
+ [colcon 官方 | Package selection arguments](https://colcon.readthedocs.io/en/released/reference/package-selection-arguments.html)
+ [colcon 官方 | ```build``` - Build Packages](https://colcon.readthedocs.io/en/released/reference/verb/build.html)
---