# 06 -1장 Turtlebot(setup)

### 1. Remote PC 설정

#### 1.1. ubuntu 설치 **\[Remote PC]**

1. [Download Ubuntu 18.04](http://releases.ubuntu.com/18.04/)
2. [Tutorial - Install Ubuntu Desktop](https://tutorials.ubuntu.com/tutorial/tutorial-install-ubuntu-desktop#0)

#### 1.2 Install ROS 2 **\[Remote PC]**

TurtleBot3는 ROS2(Dashing Diademata)가 설치된 remote PC 필요. ROS2는 1장에서 설치했다는 가정하에 진행하겠습니다.

참고 : '01장 ROS2 설치'

#### 1.3 ROS2 종속 패키지 설치  **\[Remote PC]**

```
# Install Colcon
$ sudo apt install python3-colcon-common-extensions
# Install Cartographer dependencies
$ sudo apt install -y \
 google-mock \
 libceres-dev \
 liblua5.3-dev \
 libboost-dev \
 libboost-iostreams-dev \
 libprotobuf-dev \
 protobuf-compiler \
 libcairo2-dev \
 libpcl-dev \
 python3-sphinx
# Install Gazebo9
$ curl -sSL http://get.gazebosim.org | sh
$ sudo apt install ros-dashing-gazebo-*
# Install Cartographer
$ sudo apt install ros-dashing-cartographer
$ sudo apt install ros-dashing-cartographer-ros
# Install Navigation2
$ sudo apt install ros-dashing-navigation2
$ sudo apt install ros-dashing-nav2-bringup
# Install vcstool
$ sudo apt install python3-vcstool
```

#### 1.4 TurtleBot3 패키지 설치 **\[Remote PC]** <a href="#install-turtlebot3-packages" id="install-turtlebot3-packages"></a>

```
$ mkdir -p ~/turtlebot3_ws/src
$ cd ~/turtlebot3_ws
$ wget https://raw.githubusercontent.com/ROBOTIS-GIT/turtlebot3/ros2/turtlebot3.repos
$ vcs import src < turtlebot3.repos
$ colcon build --symlink-install
```

#### 1.5 Save Bash Command for Setup **\[Remote PC]**

```
$ echo 'source ~/turtlebot3_ws/install/setup.bash' >> ~/.bashrc
$ echo 'export TURTLEBOT3_MODEL=burger' >> ~/.bashrc
$ echo 'export ROS_DOMAIN_ID=30 #TURTLEBOT3' >> ~/.bashrc
$ source ~/.bashrc // 위에서 bashrc에 설정한 것을 적용.
```

### 2. SBC(Single Board Computer) 설정

#### 2.1 Ubuntu Image File을 다운로드 후 설&#xCE58;**\[Remote PC]** <a href="#download-and-install-ubuntu-image-file" id="download-and-install-ubuntu-image-file"></a>

1. Ubuntu old release 사이트 접속
2. ubuntu-18.04.3-preinstalled-server-arm64+raspi3.img.xz 다

   (<http://old-releases.ubuntu.com/releases/18.04.3/>)
3. microSD에 Ubuntu images 파일 설치
   * Raspberry Pi Imager 다운로드 후 SD카드에 image 파일 설치

     (<https://www.raspberrypi.org/downloads/>)

#### 2.2 Raspberry Pi 3 초기&#xD654;**\[Turtlebot3]** <a href="#download-and-install-ubuntu-image-file" id="download-and-install-ubuntu-image-file"></a>

1. microSD를 TurtleBot3의 SBC에 삽입하여 부팅합니다.&#x20;

   **TIP** : 부팅을 위해서는 SBC에 HDMI cable로 모니터에 LCD, keyboard and mouse를 연결해야합니다.
2. 처음 부팅하여 이름: ubuntu, 비밀번호: ubuntu로 로그인합니다.(비밀번호는 로그인 후 변경합니.)&#x20;
3. netplan을 수정하여 WiFi에 접속합니다.&#x20;

   ('2.3 네트워크 구성 예'에서 다룰 예정입니다.)

\+ 기존(ROS1)에 '모니터/LCD없이 라즈비안 설치 후 SSH접속'하는 방법에 비해 초기 설정은 복잡하지만 연결속도가 빨라짐.

#### 2.3 네트워크 구성 예&#xC2DC;**\[Turtlebot3]** <a href="#download-and-install-ubuntu-image-file" id="download-and-install-ubuntu-image-file"></a>

SBC(Turtlebot3)에서 terminal을 열고, 아래의 command를 작성하여 폴더와 파일을 생성한 후 nano editor를 활용하여 yaml파일을 엽니다.

참고 : yaml파일은 ROS에서 지원하지 않았으나 ROS2부터 지원합니다.

```
$ sudo touch /etc/netplan/01-netcfg.yaml
$ sudo vi /etc/netplan/01-netcfg.yaml
```

yaml파일을 열은 후 아래의 네트워크 설정을 따라합니다.(vi editter 단축키 참고)

```
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: yes
      dhcp6: yes
      optional: true
  wifis:
    wlan0:
      dhcp4: yes
      dhcp6: yes
      access-points:
        "your-wifi-name":
          password: "your-wifi-password"
```

물론 "your-wifi-name"이나 "your-wifi-password"는 현재 Remote PC에서 사용하고 있는 Wifi에 해당하도록 설정하면 됩니다. 위의 설정을 마쳤다면 control + 's'로 저장합니다.

이후 아래의 command를 terminal에 적어 SBC를 재부팅합니다.

```
$ sudo netplan apply
$ reboot
```

시작할 때 네트워크가 없는 경우에도 부팅 지연을 방지하도록 아래의 명령어를 통해 systemd 프로세스를 설정합니다.

```
$ systemctl mask systemd-networkd-wait-online.service
```

이제부터 SSH를 활용하여 Remote PC에서 SBC에 접속하실 수 있습니다.

#### 2.4 Remote PC에서 SBC에 접&#xC18D;**\[Turtlebot3]** <a href="#download-and-install-ubuntu-image-file" id="download-and-install-ubuntu-image-file"></a>

위의 설정을 모두 마쳤다면 Remote PC의 terminal을 통해 아래의 명령어를 작성하여 쉽게 접속할 수 있습니다.

```
ssh ubuntu@<NETWORK IP of Raspberry PI>
```

ubuntu : SBC ID로 설정되어 있는 것을 입력

\<NETWORK IP of Raspberry PI> : SBC의 IP address를 입력 ('<>'빼고)

#### 2.5 Swap설정 **\[Turtlebot3]** <a href="#download-and-install-ubuntu-image-file" id="download-and-install-ubuntu-image-file"></a>

1\. SBC에서 터미널 열기

2\. swap 파일 만들고 설정하기

```
$ sudo fallocate -l 1G /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile
$ sudo vi /etc/fstab

# 내용삽입(vi editor로 열은 파일(/etc/fstab) 내 추가)
/swapfile swap swap defaults 0 0
```

3\. 내용 확인

```
$ sudo free -h

              total        used        free      shared  buff/cache   available
Mem:           912M         97M        263M        4.4M        550M        795M
Swap:          1.0G          0B        1.0G
```

#### 2.6 ROS2 Dashing Diademata **\[Turtlebot3]** <a href="#download-and-install-ubuntu-image-file" id="download-and-install-ubuntu-image-file"></a>

* 참고 : [ROS 2 Installation Guide](https://index.ros.org/doc/ros2/Installation/Dashing/Linux-Install-Debians/)

1\. SBC 터미널 열기

2\. Update and upgrade your software

```
$ sudo apt update && sudo apt upgrade
```

\+ 만약 아래와 같은 오류가 뜬다면?

E: Could not get lock /var/lib/dpkg/lock frontend - open (11: Resource temporarily unavailable)

E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

다음 명령어를 쳐서 해결가능

`sudo killall apt apt-get`

만일 진행중인 프로세스가 없다라고 뜨면, 아래와 같이 하나하나씩 디렉토리를 삭제해주세요.

`sudo rm /var/lib/apt/lists/lock`\
`sudo rm /var/cache/apt/archives/lock`\
`sudo rm /var/lib/dpkg/lock*`

`sudo dpkg --configure -a`&#x20;

`sudo apt update -y`

이후에 다시 아래의 명령어를 치면 성공적으로 진행된다.

`sudo apt-get upgrade`

3\. Setup locale

```
$ 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
```

4\. Setup sources

```
$ sudo apt update && sudo apt install curl gnupg2 lsb-release -y
$ curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
$ sudo sh -c 'echo "deb http://packages.ros.org/ros2/ubuntu `lsb_release -cs` main" > /etc/apt/sources.list.d/ros2-latest.list'
```

5\. Install ROS 2 packages

```
$ sudo apt update
$ sudo apt install ros-dashing-ros-base -y
```

6\. Install TurtleBot3 Packages

```
$ sudo apt install python3-argcomplete python3-colcon-common-extensions libboost-system-dev -y
$ mkdir -p ~/turtlebot3_ws/src && cd ~/turtlebot3_ws/src
$ git clone -b ros2 https://github.com/ROBOTIS-GIT/hls_lfcd_lds_driver.git
$ git clone -b ros2 https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
$ git clone -b ros2 https://github.com/ROBOTIS-GIT/turtlebot3.git
$ git clone -b ros2 https://github.com/ROBOTIS-GIT/DynamixelSDK.git
$ cd ~/turtlebot3_ws/src/turtlebot3
$ rm -r turtlebot3_cartographer turtlebot3_navigation2
$ cd ~/turtlebot3_ws/
$ echo 'source /opt/ros/dashing/setup.bash' >> ~/.bashrc
$ source ~/.bashrc
$ colcon build --symlink-install --parallel-workers 1
```

7\. 환경설정

1\) Domain ID Allocation&#x20;

ROS\_DOMAIN\_ID를 Remote PC와 TurtleBot3 동일하게 설정하고 동일네트워크에 연결되어 있으면 **DDS 통신** 가능합니다. TurtleBot3는 기본 ID가 '0'이지만 '30'으로 변경하는것이 좋습니다.

```
$ echo 'source ~/turtlebot3_ws/install/setup.bash' >> ~/.bashrc
$ echo 'export TURTLEBOT3_MODEL=burger' >> ~/.bashrc
$ echo 'export ROS_DOMAIN_ID=30 #TURTLEBOT3' >> ~/.bashrc
$ source ~/.bashrc
```

2\) OpenCR 포트 설정

다음의 명령은 OpenCR 포트 인증을 TrutleBot3에 할당하는 방법을 보여줍니다.

```
$ cd ~/turtlebot3_ws/src/turtlebot3/turtlebot3_bringup 
$ sudo cp ./99-turtlebot3-cdc.rules /etc/udev/rules.d/ 
$ sudo udevadm control --reload-rules 
$ sudo udevadm trigger
```

### 3.  OpenCR 설정\[Turtlebot3]

1.Install Dependencies to Run 32bit Executables.

```
$ sudo dpkg --add-architecture armhf
$ sudo apt-get update
$ sudo apt-get install libc6:armhf -y
```

2\. Download OpenCR Binaries & Tools for Uploading.

```
$ cd && rm -rf opencr_update.tar.bz2
$ wget https://github.com/ROBOTIS-GIT/OpenCR-Binaries/raw/master/turtlebot3/ROS2/latest/opencr_update.tar.bz2
$ tar -xjf ./opencr_update.tar.bz2
```

3\. Upload ROS 2 Firmware of TurtleBot3 to OpenCR.

```
# Set a port for OpenCR 
$ export OPENCR_PORT=/dev/ttyACM0

# Set a model of TurtleBot3 you are using
$ export OPENCR_MODEL=burger
$ cd ~/opencr_update && ./update.sh $OPENCR_PORT $OPENCR_MODEL.opencr

# 결과
aarch64
arm
OpenCR Update Start..
opencr_ld_shell ver 1.0.0
opencr_ld_main
[  ] file name   	: burger.opencr
[  ] file size   	: 168 KB
[  ] fw_name     	: burger
[  ] fw_ver      	: V180903R1
[OK] Open port   	: /dev/ttyACM0
[  ]
[  ] Board Name  	: OpenCR R1.0
[  ] Board Ver   	: 0x17020800
[  ] Board Rev   	: 0x00000000
[OK] flash_erase 	: 0.96s
[OK] flash_write 	: 1.92s
[OK] CRC Check   	: 10E28C8 10E28C8 , 0.006000 sec
[OK] Download
[OK] jump_to_fw
```

거의 2시간 가량의 긴 설치 및 세팅과정 끝에 모든 setup이 끝났다. 그렇다면 끝났음을 알리는 기계음(도레미파솔라시도)가 들릴것이다. 이제 본격적으로 turtlebot burger을 가지고 놀아보도록 하겠다.
