DonkeyCar for Jetson Nano-Setup Jetson Nano

From Waveshare Wiki
Jump to: navigation, search

Guides of DonkeyCar

Step 1. Install libraries

Please make sure that you have install image and it could start normally. Open terminal and install libraries as below

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential python3 python3-dev python3-pip python3-pandas python3-opencv python3-h5py libhdf5-serial-dev hdf5-tools nano ntp

Step 2. Setup virtual environment

sudo pip3 install virtualenv
python3 -m virtualenv -p python3 env
echo "source env/bin/activate" >> ~/.bashrc
source ~/.bashrc

Step3. Install OpenCV

  • The first step of Insalling Opencv is to define the Swap-space
  • Jetson Nano has only 4GB RAM, it is not engough for building Opencv. To avoid from memory crashing, we should define swap-space for Jetson Nano
# Turn off swap
sudo swapoff /var/swapfile
# Allocates 4G of additional swap space at /var/swapfile
sudo fallocate -l 4G /var/swapfile
# Permissions
sudo chmod 600 /var/swapfile
# Make swap space
sudo mkswap /var/swapfile
# Turn on swap
sudo swapon /var/swapfile
# Automount swap space on reboot
sudo bash -c 'echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab'
# Reboot
sudo reboot
  • Install libraries for OpenCV
# Update
sudo apt-get update
sudo apt-get upgrade
# Pre-requisites
sudo apt-get install build-essential cmake unzip pkg-config
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3-dev
  • Download source codes of OpenCV
# Create a directory for opencv
mkdir -p projects/cv2
cd projects/cv2
 
# Download sources
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.0.zip
 
# Unzip
unzip opencv.zip
unzip opencv_contrib.zip
 
# Rename
mv opencv-4.1.0 opencv
mv opencv_contrib-4.1.0 opencv_contrib
  • Enter the virtual environment env
source ~/env/bin/activate
# Install Numpy
pip install numpy
  • Configure Cmake
# Create a build directory
cd projects/cv2/opencv
mkdir build
cd build
 
# Setup CMake
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    # Contrib path
    -D OPENCV_EXTRA_MODULES_PATH=~/projects/cv2/opencv_contrib/modules \
    # Your virtual environment's Python executable
    # You need to specify the result of echo $(which python)
    -D PYTHON_EXECUTABLE=~/env/bin/python \
    -D BUILD_EXAMPLES=ON ../opencv
  • Comple the OpenCV by the following commands
  • Note that if you didn't setup the swap-space, the compilation process may fail because of memory crash.
make -j2
  • Install Opencv
# Install OpenCV
sudo make install
sudo ldconfig
  • Link the OpenCV libraries to the virtual environment virtualenv
# Go to the folder where OpenCV's native library is built
cd /usr/local/lib/python3.6/site-packages/cv2/python-3.6
# Rename
mv cv2.cpython-36m-xxx-linux-gnu.so cv2.so
# Go to your virtual environments site-packages folder
cd ~/env/lib/python3.6/site-packages/
# Symlink the native library
ln -s /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.so cv2.so
  • Run the following command and you will get information as below:
ls -la
total 48
drwxr-xr-x 10 user user 4096 Jun 16 13:03 .
drwxr-xr-x  5 user user 4096 Jun 16 07:46 ..
lrwxrwxrwx  1 user user   60 Jun 16 13:03 cv2.so -> /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.so
-rw-r--r--  1 user user  126 Jun 16 07:46 easy_install.py
drwxr-xr-x  5 user user 4096 Jun 16 07:47 pip
drwxr-xr-x  2 user user 4096 Jun 16 07:47 pip-19.1.1.dist-info
drwxr-xr-x  5 user user 4096 Jun 16 07:46 pkg_resources
drwxr-xr-x  2 user user 4096 Jun 16 07:46 __pycache__
drwxr-xr-x  6 user user 4096 Jun 16 07:46 setuptools
drwxr-xr-x  2 user user 4096 Jun 16 07:46 setuptools-41.0.1.dist-info
drwxr-xr-x  4 user user 4096 Jun 16 07:47 wheel
drwxr-xr-x  2 user user 4096 Jun 16 07:47 wheel-0.33.4.dist-info
  • Run python, and test the OpenCv
import cv2
 
# Should print 4.1.0
print(cv2.__version__)
(env) jetson@jetson:~$ python 
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.1.0
>>> quit()

Step 4. Install DonkeyCar Python codes

  • Clone donkeycar codes from Github
cd ~/projects
https://github.com/waveshare/donkeycar
cd donkeycar
git checkout master
pip install -e .[nano]
pip install --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v42 tensorflow-gpu==1.13.1+nv19.3

Step 5. Create DonkeyCar

  • Create donkeycar example
donkey createcar --path ~/mycar

After runing, files will be generated and saved in the directly ~/mycar

  • Open he myconfig.py file and modify the camera parameters.
nano myconfig.py
  • The camera we use is based on Sony IMX219, so we need to change it to CSIC and set the resolution to 224*224
#CAMERA
CAMERA_TYPE = "CSIC"   # (PICAM|WEBCAM|CVCAM|CSIC|V4L|MOCK)
IMAGE_W = 224
IMAGE_H = 224