ROS1에서 launch 파일을 부팅시에 자동으로 실행하기

개발을 완료하고, launch 파일을 부팅시에 자동으로 실행하도록 하려면 systemd 서비스를 사용해야 한다.

먼저 실행 스크립트를 작성한다(스크립트의 위치는 임의의 위치여도 상관없음)

~/test.sh

! /bin/bash
source /home/byeongkyu/.bashrc
source /opt/ros/noetic/setup.bash
roslaunch roscpp_tutorials talker_listener.launch

다음으로 systemd 서비스 파일을 생성한다. 서비스 파일의 위치는 /etc/systemd/system 내에 위치한다.

/etc/systemd/system/bringup_ros.service

[Unit]
Description=Bringup ROS launch Test

[Service]
ExecStart=/home/byeongkyu/test.sh
Restart=on-abort

[Install]
WantedBy=multi-user.target

이제 systemd의 데몬을 재시작해주고,

$ sudo systemctl daemon-reload

생성한 서비스를 시작한다.

$ sudo systemctl start bringup_ros.service

서비스의 상태를 확인하면,

$ sudo systemctl status bringup_ros.service
● bringup_ros.service - Bringup ROS launch Test
Loaded: loaded (/etc/systemd/system/bringup_ros.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2020-10-20 13:55:10 KST; 14s ago
Main PID: 35949 (test.sh)
Tasks: 26 (limit: 18700)
Memory: 57.9M
CGroup: /system.slice/bringup_ros.service
├─35949 /bin/bash /home/byeongkyu/test.sh
├─35982 /usr/bin/python3 /opt/ros/noetic/bin/roslaunch roscpp_tutorials talker_listener.launch
├─35990 /usr/bin/python3 /opt/ros/noetic/bin/rosmaster --core -p 11311 -w 3 __log:=/root/.ros/log/6ea0106a-1290-11eb-804a-ffcb70481544/master.log
├─36000 /opt/ros/noetic/lib/rosout/rosout __name:=rosout __log:=/root/.ros/log/6ea0106a-1290-11eb-804a-ffcb70481544/rosout-1.log
├─36003 /opt/ros/noetic/lib/roscpp_tutorials/listener __name:=listener __log:=/root/.ros/log/6ea0106a-1290-11eb-804a-ffcb70481544/listener-2.log
└─36005 /opt/ros/noetic/lib/roscpp_tutorials/talker __name:=talker __log:=/root/.ros/log/6ea0106a-1290-11eb-804a-ffcb70481544/talker-3.log
Oct 20 13:55:23 byeongkyu-XPS-15-7590 test.sh[36003]: [ INFO] [1603169722.778710976]: I heard: [hello world 116]
Oct 20 13:55:23 byeongkyu-XPS-15-7590 test.sh[36003]: [ INFO] [1603169722.878691038]: I heard: [hello world 117]
Oct 20 13:55:23 byeongkyu-XPS-15-7590 test.sh[36003]: [ INFO] [1603169722.978902069]: I heard: [hello world 118]

정상적으로 실행되고 있음을 확인할 수 있다.

이제 부팅시에 시작하도록 하려면, 서비스를 enable 해주면 끝.

$ sudo systemctl enable bringup_ros.service

이제 부팅할 때, 서비스가 자동으로 실행됨.

Install CUDA, cuDNN, Nvidia Driver on Ubuntu 20.04 for Tensorflow2

기존에 설치되어 있는 것들 제거. Ubuntu 20.04에는 기본으로 Nvidia Driver가 설치되어 있으므로 이를 지워주어야 함.

$ sudo apt-get purge nvidia*
$ sudo apt-get autoremove
$ sudo apt-get autoclean

CUDA 설치

1. 레포지토리 키 등록

$ sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub

2. apt에 레포지토리 주소 등록

$ sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64 /" >> /etc/apt/sources.list.d/cuda.list'
$ sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" >> /etc/apt/sources.list.d/cuda.list'

3. CUDA, Nvidia Driver 설치 (CUDA를 설치하면 자동으로 이에 의존된 드라이버 자동으로 설치)

$ sudo apt update
$ sudo apt install cuda
$ sudo apt install cuda-10-1

4. cuDNN 설치를 위한 repo 패키지 설치

$ wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/nvidia-machine-learning-repo-ubuntu2004_1.0.0-1_amd64.deb
$ sudo dpkg -i nvidia-machine-learning-repo-ubuntu2004_1.0.0-1_amd64.deb

5. cuDNN 레포지토리 추가

$ sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" >> /etc/apt/sources.list.d/nvidia-machine-learning.list'

6. cuDNN 설치 (CUDA 버전에 맞는 버전으로 설치해야 함)

$ sudo apt install libcudnn7-dev=7.6.5.32-1+cuda10.1 libcudnn7=7.6.5.32-1+cuda10.1

추후 업데이트시 최신버전으로 업데이트 되는 것을 방지

$ sudo apt-mark hold libcudnn7 libcudnn7-dev
libcudnn7 set on hold.
libcudnn7-dev set on hold.

이와 같이 설치를 완료하고, 재부팅한 다음… 드라이버가 잘 설치되어 있는지 확인

$ nvidia-smi


TensorFlow2 설치

$ sudo apt install python3-pip
$ sudo pip3 install -U tensorflow-gpu

환경변수 셋업

$ echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.1/lib64:/usr/local/cuda-10.2/lib64:/usr/local/cuda-10.1/lib64' >> ~/.bashrc
$ source ~/.bashrc

실행 및 설치 확인

$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
import tensorflow as tf
2020-10-08 15:02:50.803732: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
tf.config.list_physical_devices('GPU')
2020-10-08 15:03:01.717511: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcuda.so.1
2020-10-08 15:03:01.733336: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-08 15:03:01.733585: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1650 computeCapability: 7.5
coreClock: 1.56GHz coreCount: 16 deviceMemorySize: 3.82GiB deviceMemoryBandwidth: 119.24GiB/s
2020-10-08 15:03:01.733603: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
2020-10-08 15:03:01.734679: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcublas.so.10
2020-10-08 15:03:01.735901: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcufft.so.10
2020-10-08 15:03:01.736061: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcurand.so.10
2020-10-08 15:03:01.737163: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcusolver.so.10
2020-10-08 15:03:01.737773: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcusparse.so.10
2020-10-08 15:03:01.740221: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudnn.so.7
2020-10-08 15:03:01.740295: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-08 15:03:01.740563: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-08 15:03:01.740768: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]