嵌入式JetSon TX2上定位构图与目标检测环境部署(包括RTAB-MAP、Object Detection API、RealSense、IMU、ROS、CUDA、Tensorflow等配置)

这周为了项目结题在一块崭新的TX2开发板上复现了去年的定位构图与目标检测环境部署,其实各个部分的内容在博主之前的博客中零零碎碎的都提到过,这里重新写篇博客整体总结一下,因为后面还有两块新的TX2等待博主部署环境…流下了熟练工种的泪水QAQ

嵌入式JetSon TX2开发板上定位构图与目标检测环境部署

      • 1、安装与配置ROS Kinetic Kame
      • 2、指定Cmake版本
      • 3、安装与配置OpenCV
      • 4、安装Realsense D435的相机驱动
        • 4.1、安装Librealsense2 SDK
        • 4.2、安装Pyrealsense2 (Python Wapper)
        • 4.3、安装Librealsense_ROS
      • 5、安装IMU驱动
      • 6、搭建RTAB_MAP稠密点云构图
        • 6.1、安装与编译RTAB-MAP
        • 6.2、安装与编译RTAB-MAP_ROS
        • 6.3、使用摄像头RealSense D435运行RTAB-MAP
      • 7、安装与配置CUDA和CUDNN
      • 8、安装与配置Tensorflow-gpu
      • 9、Object Detection API 框架搭建

当前JetSon TX2刷机版本JatsonPack3.2.1,刷机时自带python2.7CUDA9.0
后续所有环境全部依赖python2.7,无需重新安装python3x版本,CUDA9.0也沿用了刷机版本,没有什么问题。
可以先检查是否已写入CUDA9.0的环境变量:
sudo gedit /etc/bash.bashrc
export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH

1、安装与配置ROS Kinetic Kame

ROS官网链接:http://wiki.ros.org/kinetic/Installation/Ubuntu"optional title here"
可查看ROS Kinetic Kame 安装手册。
ROS Kinetic ONLY supports Wily (Ubuntu 15.10), Xenial (Ubuntu 16.04) and Jessie (Debian 8) for debian packages.

(1) 配置Ubuntu软件中心
配置Ubuntu要求允许接受”restricted” “universe”“multiverse”的软件源,一般是系统默认设置,可以在Software & Updates 中查看。

(2)设置sources.list(软件源)
设置计算机从packages.ros.org接收软件包。添加ROS官方软件源与设置秘钥,命令行:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116`

(3)安装 ROS Kinetic
更新Debian包索引,确保系统软件处于最新版,安装桌面完整版,命令行如下:

sudo apt-get update
sudo apt-get install ros-kinetic-desktop-full

(4)设置环境
添加 ROS 环境变量,命令如下:

sudo gedit ~/.bashrc
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc #(使环境变量设置立即生效)

最后初始化以及更新ROS,执行:sudo rosdep initrosdep update,我这里遇到问题:

问题记录-01 -------------------------------------------------------------------------------------------------------

ERROR:cannot download default sources list from:
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list
Website may be down

参考了某技术博客的解决方案,然而依旧没有用处,这里也先记录一下:

sudo c_rehash /etc/ssl/certs
sudo -E rosdep init

-------------------------------------------------------------------------------------------------------------------------
最后直接启动ROS环境进行测试,命令roscore
显示出started core service [/rosout] ,说明 ROS 已经成功安装并配置,于是上述问题忽略。

(5)创建 ROS 工作空间
创建ROS工作空间 catkin workspaces,命令行如下:

mkdir -p ~/catkin_ws/src  #在home下新建文件夹catkin_ws/src
cd ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make #编译工作空间
source devel/setup.bash

2、指定Cmake版本

记不清后续是在哪里遇到了编译时Cmake版本不兼容的问题,于是这里可以先安装指定的Cmake版本。
Jetpack 3.2.1刷机后自带Cmake,于是先删除默认版本:

sudo apt remove cmake
sudo apt purge --auto-remove cmake

之后从官网下载所需版本,我这里使用cmake-3.9.2.tar.gz。编译安装命令行如下:

mkdir build
cd cmake-$version.$build/
./bootstrap
make -j4
sudo make install

移动路径:sudo cp ./bin/cmake /usr/bin/
最后验证安装结果:cmake --version

cmake version 3.9.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).

问题记录-02 -------------------------------------------------------------------------------------------------------
后续在使用Cmake编译时遇到过这个问题,先记录一下,下次安装时可以先把依赖库装好:

Could NOT find Glog (missing: GLOG_INCLUDE_DIR GLOG_LIBRARY)

参考了如下技术贴问题得以解决:
https://stackoverflow.com/questions/65588745/could-not-find-glog-missing-glog-include-dir-glog-library

sudo apt-get install libgflags-dev
sudo apt install libgoogle-glog-dev
sudo apt-get install protobuf-compiler libprotobuf-dev

问题记录-03 -------------------------------------------------------------------------------------------------------
后续在使用Cmake编译时遇到过这个问题,先记录一下,下次安装时也可以先解决:

 fatal error: Eigen/Core: No such file or directory

因为没有安装Eigen3,执行命令行:sudo apt-get install libeigen3-dev
当调用 eigen 库时,如果依旧报错:fatal error: Eigen/Core: No such file or directory
这是因为 eigen 库默认安装在了 /usr/include/eigen3/Eigen 路径下,需使用下面命令映射到 /usr/include 路径下

sudo ln -s /usr/include/eigen3/Eigen /usr/include/Eigen

----------------------------------------------------------------------------------------------------------------------

3、安装与配置OpenCV

安装编译工具与OpenCV依赖包,命令行如下:

sudo apt-get install build-essential -y
sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev -y

安装一些可选包,最好全都装上,命令行如下:

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev -y
sudo apt-get install libgtk2.0-dev -y
sudo apt-get install pkg-config -y

进入目录~/catkin_ws/src,从GitHub下载opencv3.4.0,并将下载的OpenCV解压。
进入OpenCV的目录,编译安装OpenCV 3.4.0源码,命令行如下:

cd opencv-3.4.0/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. 
sudo make -j4
sudo make install

接下来配置OpenCV环境变量,将opencv的库加入到路径,从而让系统可以找到:
执行以下命令行:

cd /etc/ld.so.conf.d/
touch opencv.conf  #新建文件opencv.conf
sudo gedit /etc/ld.so.conf.d/opencv.conf #编辑文件内容

在文件末尾加入/usr/local/lib,保存并退出。
执行命令行使配置生效:sudo ldconfig
继续配置环境变量,编辑.bashrc文件,执行命令行:

sudo gedit /etc/bash.bashrc 

在文件末尾加入:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH,保存并退出。执行命令行使配置立刻生效:

source /etc/bash.bashrc 

下面可以写一个测试代码,验证OpenCV是否已经配置成功。随便找一张图像,放在某路径下,测试代码我这里起名为Rina.cpp

#include 
#include 
using namespace cv;

int main( )
{
    Mat image;

    #将下面的路径替换为自己的图片路径
    image = imread("/home/rina/catkin_ws/src/Rina.png", 1 );
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

编译上述代码并运行:

g++ Rina.cpp -o Rina.o  `pkg-config --cflags --libs opencv`  #编辑,生成.o文件
./Rina.o  #运行

如果能够正确实现图像导入显示功能,则证明OpenCV已正确安装并配置。

这里给出一些OpenCV常用的查询命令行:
查看当前OpenCV版本:pkg-config --modversion opencv
查看OpenCV安装库存放路径:pkg-config opencv --libs
查看OpenCV相关文件的所有存放路径:sudo find / -iname "*opencv*"

4、安装Realsense D435的相机驱动

4.1、安装Librealsense2 SDK

RealSense SDK已经可以直接在JetSon TX2上进行安装,其安装方式有两种,一个是从源码编译安装,一个是直接apt-get安装。
首先为求便捷直接选择apt-get安装,但没能成功,不过还是记录一下正常的安装流程:

方式1:apt-get安装包进行安装:

官方手册:https://github.com/IntelRealSense/librealsense/blob/master/doc/distribution_linux.md#installing-the-packages

注册公匙:

sudo apt-key adv --keyserver keys.gnupg.net --recv-key C8B3A55A6F3EFCDE || sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-key C8B3A55A6F3EFCDE

添加repositories(Ubuntu 16 LTS):

sudo add-apt-repository "deb http://realsense-hw-public.s3.amazonaws.com/Debian/apt-repo xenial main" -u

安装library,developer和debug packages:

sudo apt-get install librealsense2-dkms
sudo apt-get install librealsense2-utils
sudo apt-get install librealsense2-dev
sudo apt-get install librealsense2-dbg

测试RealSense SDK:realsense-viewer

问题记录-04 -------------------------------------------------------------------------------------------------------
这里我遇到的问题是:
在执行sudo apt-get install librealsense2-dkms时依旧无法定位软件安装包,报错如下:

E: Unable to locate package librealsense2-dkms

这里一直没能解决,于是放弃apt安装,直接对源码进行编译。
-------------------------------------------------------------------------------------------------------------------------

方式2:源码编译进行安装:

官方手册:https://github.com/IntelRealSense/librealsense/blob/master/doc/installation.md

目前,截止到2021.07.09,官方提供的最新版本为RealSense SDK 2.48.0
(注意一定要用最新版!!安装前一定去官网检查版本!!)

安装一些依赖库:

sudo apt-get install git libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev libeigen3-dev 
sudo apt-get install libglfw3-dev

编译与安装:

git clone https://github.com/IntelRealSense/librealsense.git
cd librealsense/build
cmake ../
sudo make uninstall && make clean && make && sudo make install

最后,执行realsense-viewer如果能正确显示摄像头采集图像界面,则证明SDK库已成功安装。

测试前需要检查目录/etc/udev/rules.d/下是否有99-realsense-libusb.rules文件,如果没有需要手动生成文件99-realsense-libusb.rules,复制内容如下,然后将文件移至/etc/udev/rules.d/路径下。

##Version=1.1##
# Device rules for Intel RealSense devices (R200, F200, SR300 LR200, ZR300, D400, L500, T200)
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0a80", MODE:="0666", GROUP:="plugdev", RUN+="/usr/local/bin/usb-R200-in_udev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0a66", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0aa3", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0aa2", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0aa5", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0abf", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0acb", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ad0", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="04b4", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ad1", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ad2", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ad3", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ad4", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ad5", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ad6", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0af2", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0af6", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0afe", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0aff", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b00", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b01", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b03", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b07", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b0c", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b0d", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b3a", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b3d", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b48", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b49", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b4b", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b4d", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b52", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b5b", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b5c", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b64", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b68", MODE:="0666", GROUP:="plugdev"

# Intel RealSense recovery devices (DFU)
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ab3", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0adb", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0adc", MODE:="0666", GROUP:="plugdev"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b55", MODE:="0666", GROUP:="plugdev"

# Intel RealSense devices (Movidius, T265)
SUBSYSTEMS=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="8087", ATTRS{idProduct}=="0af3", MODE="0666", GROUP="plugdev"
SUBSYSTEMS=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="8087", ATTRS{idProduct}=="0b37", MODE="0666", GROUP="plugdev"
SUBSYSTEMS=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="03e7", ATTRS{idProduct}=="2150", MODE="0666", GROUP="plugdev"

KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ad5", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor_custom", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ad5", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0af2", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0af2", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0afe", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor_custom", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0afe", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0aff", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor_custom", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0aff", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b00", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor_custom", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b00", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b01", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor_custom", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b01", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b3a", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b3a", RUN+="/bin/sh -c ' chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b3d", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b3d", RUN+="/bin/sh -c ' chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b4b", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b4b", RUN+="/bin/sh -c ' chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b4d", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b4d", RUN+="/bin/sh -c ' chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b5b", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b5b", RUN+="/bin/sh -c ' chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b5c", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b5c", RUN+="/bin/sh -c ' chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b64", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b64", RUN+="/bin/sh -c ' chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b68", MODE:="0777", GROUP:="plugdev", RUN+="/bin/sh -c 'chmod -R 0777 /sys/%p'"
DRIVER=="hid_sensor*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0b68", RUN+="/bin/sh -c ' chmod -R 0777 /sys/%p && chmod 0777 /dev/%k'"

# For products with motion_module, if (kernels is 4.15 and up) and (device name is "accel_3d") wait, in another process, until (enable flag is set to 1 or 200 mSec passed) and then set it to 0.
KERNEL=="iio*", ATTRS{idVendor}=="8086", ATTRS{idProduct}=="0ad5|0afe|0aff|0b00|0b01|0b3a|0b3d|0b64|0b68", RUN+="/bin/sh -c '(major=`uname -r | cut -d \".\" -f1` && minor=`uname -r | cut -d \".\" -f2` && (([ $major -eq 4 ] && [ $minor -ge 15 ]) || [ $major -ge 5 ])) && (enamefile=/sys/%p/name && [ `cat $enamefile` = \"accel_3d\" ]) && enfile=/sys/%p/buffer/enable && echo \"COUNTER=0; while [ \$COUNTER -lt 20 ] && grep -q 0 $enfile; do sleep 0.01; COUNTER=\$((COUNTER+1)); done && echo 0 > $enfile\" | at now'"

注意这里一定需要添加99-realsense-libusb.rules文件,否则会报错,错误内容如下:

failed to open usb interface: 0, error: RS2_USB_STATUS_ACCESS

4.2、安装Pyrealsense2 (Python Wapper)

接下来安装librealsens2的python wapper,即pyrealsense2,在后文目标检测功能处实现librealsense2的python模块调用。这个地方的坑挺多的,需要注意pyrealsense2依赖的python版本以及编译路径,具体在后文进行说明。

pyrealsense2 源码编译 安装说明:
https://github.com/IntelRealSense/librealsense/tree/master/wrappers/python

首先,按照官方手册的说明安装python,我这里直接安装python2版本,执行:sudo apt-get install python python-dev

之后在librealsense文件夹的根目录下执行如下命令行完成pyrealsense2的编译与安装,并指定使用的python库的版本:

cd build
cmake ../ -DBUILD_PYTHON_BINDINGS:bool=true -DPYTHON_EXECUTABLE=/usr/bin/python2.7
make -j4
sudo make install

安装完成后执行如下命令行暂时配置python环境变量:
(也可以将如下命令行写入~/.bashrc中,注意后文在安装Object Detection API时修改了PYTHONPATH,记得执行python脚本时再改回来):

export PYTHONPATH=$PYTHONPATH:/usr/local/lib

最后当执行python脚本时,将librealsense/build/wrappers/python路径下的pyrealsense2.so.2.48pyrealsense2.so.2pyrealsense2.so文件,以及librealsense2.so.2.48librealsense2.so.2librealsense2.so文件拷贝至你要运行的python脚本旁边。

如果不再提示"No Mudule Named Pyrealsense2"错误信息,则证明pyrealsense2已安装成功。

问题记录-05 -------------------------------------------------------------------------------------------------------
这里我遇到的问题是当编译的时候,一直无法git下来curl这个安装包。
建议多试几次,天时地利人和…
--------------------------------------------------------------------------------------------------------------------------

4.3、安装Librealsense_ROS

Realsense官方提供的安装方式依旧有两种,一个是从源码编译安装,一个是直接apt-get安装ros包:

方式1:apt-get安装包进行安装:

官方手册:https://github.com/IntelRealSense/realsense-ros

首先为求便捷也是直接选择apt-get安装,安装步骤如下:

sudo apt-get install ros-kinetic-realsense2-camera 
sudo apt-get install ros-kinetic-rgbd-launch
sudo apt-get install ros-kinetic-realsense2-description

好家伙,难得这一次我直接成功了,于是安装结束。之前有一次安装的时候遇到过如下错误:

问题记录-06 -------------------------------------------------------------------------------------------------------
运行相机ROS节点时roslaunch realsense2_camera rs_camera.launch,发现无法找到realsense2_camera节点,这时建议重新源码编译realsense-ros

方式2:源码编译进行安装:

官方手册:https://github.com/IntelRealSense/realsense-ros

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src/
git clone https://github.com/IntelRealSense/realsense-ros.git
cd realsense-ros/
git checkout `git tag | sort -V | grep -P "^2.\d+\.\d+" | tail -1`
cd ..

编译与安装:

catkin_init_workspace
cd ..
catkin_make clean
catkin_make -DCATKIN_ENABLE_TESTING=False -DCMAKE_BUILD_TYPE=Release
catkin_make install

-----------------------------------------------------------------------------------------------------------------------------

5、安装IMU驱动

由于RealSense D435摄像头不含IMU惯性测量模块,于是我们购入了一块轻量级IMU,型号JY901,集成两个加速度传感器与三个速度传感器(陀螺),用于测量物体三轴姿态角(或角速率)以及加速度,并以此解算出物体的姿态,使得最终的定位结果更加精准。

购买完IMU惯导店家会提供驱动安装包,将其放置在catkin_ws的ROS工作空间中,直接catkin_make进行编译,之后在包含/scripts/jy901.rules文件的路径下,运行如下命令行:

sudo cp ...../scripts/jy901.rules  /etc/udev/rules.d
sudo service udev reload
sudo service udev restart
sudo reboot  #重启系统

6、搭建RTAB_MAP稠密点云构图

RTAB-MAP(Real-Time Appearance-Based Mapping)提供了一个基于外观的定位与构图解决方案,解决大型环境中的在线闭环检测问题。
官方链接:http://introlab.github.io/rtabmap/

6.1、安装与编译RTAB-MAP

源码链接:https://github.com/introlab/rtabmap
官方用户手册:https://github.com/introlab/rtabmap_ros#installation

使用源码安装RTAB-MAP,这里使用的是官方最新版RTAB-MAP v0.20.8,命令行如下:

#安装依赖项 (Qt, PCL, VTK, OpenCV, ...)
sudo apt-get install ros-kinetic-rtabmap ros-kinetic-rtabmap-ros
cd ~
git clone https://github.com/introlab/rtabmap.git rtabmap #rtabmap源码下载
cd rtabmap/build
cmake ..
make -j4
make install

问题记录-07 -------------------------------------------------------------------------------------------------------
在编译过程中遇到如下错误:

/home/nvidia/rtabmap/corelib/src/optimizer/OptimizerGTSAM.cpp:45:42: fatal error: gtsam/sam/BearingRangeFactor.h: No such file or directory

分析原因,应该是没有安装GTSAM依赖项。运行如下命令行安装GTSAM:

git clone https://bitbucket.org/gtborg/gtsam.git
cd gtsam
mkdir build
cd build
cmake ..
make check
sudo make install

-----------------------------------------------------------------------------------------------------------------------------
安装完GTSAM依赖项后重新进行RTAB-MAP的编译,编译成功。

6.2、安装与编译RTAB-MAP_ROS

安装RTAB-MAP的ros包,这里尝试了好几个版本但都无法与源码RTAB-MAP v0.20.8适配,最后尝试安装RTAB-MAP_ROS 0.20.7-kinetic 版本问题得以解决,命令行如下:

cd ~/catkin_ws
git clone https://github.com/introlab/rtabmap_ros.git src/rtabmap_ros #在官网上下载`RTAB-MAP_ROS 0.20.7-kinetic` 版本
catkin_make -j4

6.3、使用摄像头RealSense D435运行RTAB-MAP

首先将rtabmap.launch文件中订阅的topic更改为RealSense D435摄像头发布的信息,如下所示:

<arg name="rgb_topic"               default="/camera/color/image_raw" />
<arg name="depth_topic"             default="/camera/aligned_depth_to_color/image_raw" />
<arg name="camera_info_topic"       default="/camera/color/camera_info" />

之后分别打开三个终端,分别运行如下命令行:

source devel/setup.bash
roslaunch realsense2_camera rs_rgbd.launch 
source devel/setup.bash
roslaunch serial_com_node serial.launch 
#启动RTAB-MAP建图
source devel/setup.bash
roslaunch rtabmap_ros rtabmap.launch rtabmap_args:="--delete_db_on_start"
#或者
source devel/setup.bash
roslaunch rtabmap_ros rtabmap.launch rtabmap_args:="--delete_db_on_start" rviz:=true rtabmapviz:=false

显示如下三维场景重建界面,表明RTAB-MAP已经正确安装,Realsense D435摄像头可以正确运行。
嵌入式JetSon TX2上定位构图与目标检测环境部署(包括RTAB-MAP、Object Detection API、RealSense、IMU、ROS、CUDA、Tensorflow等配置)_第1张图片

7、安装与配置CUDA和CUDNN

由于刷机时自带CUDA9.0,所以这里不再介绍CUDA安装。
下面总结CUDNN安装配置,安装Cudnn 7.1.5版本能够完全适配不出问题,无数次试验过后的结果。

官网下载链接:https://developer.nvidia.com/rdp/cudnn-archive

官网下载对应的cudnn,一定要是arm版本的(aarch64),安装步骤如下:

sudo cp cudnn.h   /usr/local/cuda-9.0/include 
sudo cp lib64/libcudnn* /usr/local/cuda-9.0/lib64
cd /usr/local/cuda-9.0/lib64/
sudo chmod +r libcudnn.so.7.1.5
sudo ln -s libcudnn.so.7.1.5 libcudnn.so.7
sudo ln -s libcudnn.so.7 libcudnn.so   
sudo ldconfig

上述方法我这里尝试了是可行的,另外一种方案是使用JetPack刷机时下载的安装包,所有在JetPack中下载的安装包均存在JetPack/jetpack_download中,将安装包拷出:

cuda9.0安装包 (cuda-repo-l4t-9-0-local_9.0.252-1_arm64.deb)
cudnn安装包 (有3个,libcudnn7_7.1.5.14-1+cuda9.0_arm64.deb,
libcudnn7-dev_7.1.5.14-1+cuda9.0_arm64.deb,libcudnn7-doc_7.1.5.14-1+cuda9.0_arm64.deb)

由于都是 .deb 文件,因此直接使用下面的命令安装即可:

sudo dpkg -i libcudnn7_7.1.5.14-1+cuda9.0_arm64.deb
sudo dpkg -i libcudnn7-dev_7.1.5.14-1+cuda9.0_arm64.deb
sudo dpkg -i libcudnn7-doc_7.1.5.14-1+cuda9.0_arm64.deb

安装完成后,其实是直接安装到了默认安装路径usr/includeusr/lib下的,因此需要将其拷贝到cuda安装路径下:

sudo cp /usr/include/cudnn.h /usr/local/cuda-9.0/include/
sudo cp /usr/lib/aarch64-linux-gnu/libcudnn* /usr/local/cuda-9.0/lib64/

最后可以使用以下指令查看Cudnn的版本:

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

8、安装与配置Tensorflow-gpu

这里也是经过无数次尝试,tensorflow==1.14.0的版本能够完全兼容不出问题,博主这里安装的版本是tensorflow_gpu-1.14.0+nv19.9-cp27-cp27mu-linux_aarch64.whl,安装包的具体来源已经忘记是在哪里找到的了,貌似是淘宝花了5毛在一个CSDN的资源里扒出来的,后面博主也可以上传一下资源方便大家下载。

首先安装pip,执行:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

之后就可以看到pip成功安装了,之后安装tensorflow-gpu:

sudo pip install tensorflow_gpu-1.14.0+nv19.9-cp27-cp27mu-linux_aarch64.whl

安装完成后进入python环境,导入tensorflow查看是否安装成功:

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

问题记录-08 -------------------------------------------------------------------------------------------------------

Cannot uninstall 'enum34'. It is a distutils installed project and thus we ca...

在安装python的路径下找到emum-1.1.2.egg-info文件并删除即可,本文路径为/usr/lib/python2.7/dist-packages
可以在文件里选项查找,直接搜索emum文件名。

sudo rm -rf emum-1.1.2.egg-info

之后再次运行如下代码即可完成TensorFlow的安装。
-----------------------------------------------------------------------------------------------------------------------------

9、Object Detection API 框架搭建

注意,这里一定不要直接git clone https://github.com/tensorflow/models.git获取源代码,而是去官网上的Releases板块中下载与tensorflow-gpu版本对应的源码版本,一定要对应否则会出现很多问题。
官网链接:https://github.com/tensorflow/models/releases

为对应Tensorflow-gpu==1.14.0,这里的版本选择models-1.13.0
下载后放置在新建的tensorflow文件夹中,例如/models/

安装之前,Tensorflow Object Detection API要求protoc版本为2.6.0以上,可以使用protoc --version 命令查看版本,如果低于这个版本的或者编译出错,需要升级。
下载地址:https://github.com/protocolbuffers/protobuf/releases
我这里安装的版本为:protoc-3.17.3-linux-aarch_64.zip,新建protoc_3.3在该文件夹中将压缩包解压。

之后按照如下命令行指令依次安装:

sudo apt-get install protobuf-compiler python-pil python-lxml python-tk
pip install --user Cython
pip install --user contextlib2
pip install --user pillow
pip install --user lxml
pip install --user jupyter -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip install --user matplotlib

# -------------------------------------------------
# COCO API installation
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools <path_to_tensorflow>/models/research/

# -------------------------------------------------
# From tensorflow/models/research/
/home/nvidia/protoc_3.3/bin/protoc object_detection/protos/*.proto --python_out=.

# -------------------------------------------------
# Add Libraries to PYTHONPATH
sudo gedit ~/.bashrc
export PYTHONPATH=$PYTHONPATH:/home/.../tensorflow/models/research/slim
source ~/.bashrc
 
# -------------------------------------------------
python setup.py build
python setup.py install

安装结束后,在/home/.../tensorflow/models/research/路径下执行测试代码:

python object_detection/builders/model_builder_test.py

如果正确显示如下信息,则证明Object Detection API 框架搭建成功,否则有问题一定要解决问题不能跳过,后面使用的时候才不会出现别的bug。

................
----------------------------------------------------------------------
Ran 16 tests in 0.112s

OK

基础的环境到这里就都安装完毕了,快乐下班*( ^ v ^ )/* GO
2021.07.09

你可能感兴趣的:(环境搭建,JetSon,TX2,SSD,Mobile,Net,嵌入式,jetpack,tensorflow,cuda,slam)