DonkeyCar for Jetson Nano 教程一、Jetson nano安装

步骤1、安装依赖

默认你的jetson nano已经安装镜像并启动。打开控制终端,输入如下命令安装依赖库

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

步骤2、设置虚拟环境

  1. pip3 install virtualenv
  2. python3 -m virtualenv -p python3 env
  3. echo "source env/bin/activate" >> ~/.bashrc
  4. source ~/.bashrc

步骤3、安装OpenCV

  • 构建OpenCV的第一步是在Jetson Nano上定义交换空间。
  • Jetson Nano具有4GB RAM。这不足以从源代码构建OpenCV。因此,我们需要在Nano上定义交换空间以防止内存崩溃。
  1. # Turn off swap
  2. sudo swapoff /var/swapfile
  3. # Allocates 4G of additional swap space at /var/swapfile
  4. sudo fallocate -l 4G /var/swapfile
  5. # Permissions
  6. sudo chmod 600 /var/swapfile
  7. # Make swap space
  8. sudo mkswap /var/swapfile
  9. # Turn on swap
  10. sudo swapon /var/swapfile
  11. # Automount swap space on reboot
  12. sudo bash -c 'echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab'
  13. # Reboot
  14. sudo reboot
  • 安装OpenCV依赖库
  1. # Update
  2. sudo apt-get update
  3. sudo apt-get upgrade
  4. # Pre-requisites
  5. sudo apt-get install build-essential cmake unzip pkg-config
  6. sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
  7. sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
  8. sudo apt-get install libxvidcore-dev libx264-dev
  9. sudo apt-get install libgtk-3-dev
  10. sudo apt-get install libatlas-base-dev gfortran
  11. sudo apt-get install python3-dev
  • 下载OpenCV的源代码
  1. # Create a directory for opencv
  2. mkdir -p projects/cv2
  3. cd projects/cv2
  4.  
  5. # Download sources
  6. wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.0.zip
  7. wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.0.zip
  8.  
  9. # Unzip
  10. unzip opencv.zip
  11. unzip opencv_contrib.zip
  12.  
  13. # Rename
  14. mv opencv-4.1.0 opencv
  15. mv opencv_contrib-4.1.0 opencv_contrib
  • 进入到虚拟环境env
  1. source ~/env/bin/activate
  2. # Install Numpy
  3. pip install numpy
  • 设置Cmake配置
  1. # Create a build directory
  2. cd projects/cv2/opencv
  3. mkdir build
  4. cd build
  5.  
  6. # Setup CMake
  7. cmake -D CMAKE_BUILD_TYPE=RELEASE \
  8. -D CMAKE_INSTALL_PREFIX=/usr/local \
  9. -D INSTALL_PYTHON_EXAMPLES=ON \
  10. -D INSTALL_C_EXAMPLES=OFF \
  11. -D OPENCV_ENABLE_NONFREE=ON \
  12. # Contrib path
  13. -D OPENCV_EXTRA_MODULES_PATH=~/projects/cv2/opencv_contrib/modules \
  14. # Your virtual environment's Python executable
  15. # You need to specify the result of echo $(which python)
  16. -D PYTHON_EXECUTABLE=~/env/bin/python \
  17. -D BUILD_EXAMPLES=ON ../opencv
  • 使用以下命令编译源码,编译时间会比较长,请耐心等待。
  • 注意:如果前面没有设置交换空间,此时编译可能会因为内存不足导致进程关闭。
  1. make -j2
  • 安装OpenCV
  1. # Install OpenCV
  2. sudo make install
  3. sudo ldconfig
  • 将构建的OpenCV库链接到虚拟环境virtualenv
  1. # Go to the folder where OpenCV's native library is built
  2. cd /usr/local/lib/python3.6/site-packages/cv2/python-3.6
  3. # Rename
  4. mv cv2.cpython-36m-xxx-linux-gnu.so cv2.so
  5. # Go to your virtual environments site-packages folder
  6. cd ~/env/lib/python3.6/site-packages/
  7. # Symlink the native library
  8. ln -s /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.so cv2.so
  • 运行以下命令会看到类似如下信息
  1. ls -la
  1. total 48
  2. drwxr-xr-x 10 user user 4096 Jun 16 13:03 .
  3. drwxr-xr-x 5 user user 4096 Jun 16 07:46 ..
  4. lrwxrwxrwx 1 user user 60 Jun 16 13:03 cv2.so -> /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.so
  5. -rw-r--r-- 1 user user 126 Jun 16 07:46 easy_install.py
  6. drwxr-xr-x 5 user user 4096 Jun 16 07:47 pip
  7. drwxr-xr-x 2 user user 4096 Jun 16 07:47 pip-19.1.1.dist-info
  8. drwxr-xr-x 5 user user 4096 Jun 16 07:46 pkg_resources
  9. drwxr-xr-x 2 user user 4096 Jun 16 07:46 __pycache__
  10. drwxr-xr-x 6 user user 4096 Jun 16 07:46 setuptools
  11. drwxr-xr-x 2 user user 4096 Jun 16 07:46 setuptools-41.0.1.dist-info
  12. drwxr-xr-x 4 user user 4096 Jun 16 07:47 wheel
  13. drwxr-xr-x 2 user user 4096 Jun 16 07:47 wheel-0.33.4.dist-info
  • 运行Python 执行以下,测试OpenCV是否成功
  1. import cv2
  2.  
  3. # Should print 4.1.0
  4. print(cv2.__version__)
  1. (env) jetson@jetson:~$ python
  2. Python 3.6.8 (default, Jan 14 2019, 11:02:34)
  3. [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import cv2
  6. >>> print(cv2.__version__)
  7. 4.1.0
  8. >>> quit()

步骤4、安装Donkeycar Python代码

  • 从Github获取最新的donkeycar程序
  1. cd ~/projects
  2. git clone https://github.com/waveshare/donkeycar
  3. cd donkeycar
  4. git checkout master
  5. pip install -e .[nano]
  6. pip install --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v42 tensorflow-gpu==1.13.1+nv19.3

步骤5、从模板创建Donkeycar

  • 使用以下命令创建donkeycar实例
  1. donkey createcar --path ~/mycar

程序运行后~/mycar目录下回自动生成一系列控制donkeycar的文件。

  • 打开如下文件,找到摄像头参数设置部分修改摄像头参数
  1. nano myconfig.py

Jetson Nano使用基于Sony IMX219的相机,摄像头改为CSIC,分辨率设置为224*224.

  1. #CAMERA
  2. CAMERA_TYPE = "CSIC" # (PICAM|WEBCAM|CVCAM|CSIC|V4L|MOCK)
  3. IMAGE_W = 224
  4. IMAGE_H = 224