3D Depth Sensor for Raspberry Pi 3


Depth sensors such as OPT8241 CDK from Texas Instruments enable a wide array of new applications, ranging from people counting to gesture control to robotics and other dimensioning applications.  3D sensing together with a WiFi-enabled embedded system, will enable powerful IoT applications.  This blog post describes how to integrate OPT8241 CDK with Raspberry Pi 3.

Download Ubuntu MATE

Download UbuntuMATE 16.04 LTS image into your Ubuntu Linux box and unzip the *.img.xz file:

     xz -d filename.img.xz

where filename.img.xz is the downloaded image.

Program Micro SD Card

The next step  is program the image into the micro SD card.  Insert the micro SD card into your Ubuntu Linux PC and check the SD card’s assigned device name this way:

     dmesg | tail    

Usually you should see something like mmcblk0 or sdc. Remember the device name. Eject or unmount the micro SD card, and then program the micro SD card using this command:

     dd if=filename.img of=/dev/mmblk0 bs=1M

Replace filename and mmblk0 with your own filename and device name.

Check Disk Partitions

The Ubuntu Mate image downloaded may not be configured to use of the full storage capacity of your micro SD card. To ensure the full storage capacity is used, run gparted:

     sudo apt-get install gparted
     sudo gparted

Click on the Linux partition (not the boot partition), and select Check from the menu, and option will be provided to resize the selected partition to the full capacity.

Booting the Image

Once programming is completed, insert the micro SD card into RPi3 and power it up with HDMI and USB keyboard and mouse connected.  Answer a series of questions and enable WiFi connection. WiFi may become disconnected, but no worries, just reboot. When booting and general OS setup is completed, login and create a subdirectory Software at the home directory to keep all the new software we will download:

     mkdir Software

Setup Swapfile

Create a swap file to supplement the limited 1GB RAM available on RPi3. A swap file is space on the storage device used as virtual memory, useful in compiling massive projects.

First, check that there is no active swap by entering the command:

     free -h

Create a 2 GB swap file by the following commands:

     sudo fallocate -l 2G /swapfile
     ls -lh /swapfile   # Check that the file is indeed 2GB
     sudo chmod 600 /swapfile  # Make the file only accessible to root
     sudo mkswap /swapfile  # Identifies the file as swap space
     sudo swapon /swapfile  # Enables swap file
     sudo swapon --show    # Verifies that the swap file is working

To make the swap permanent, add the below line to /etc/fstab and reboot:

     /swapfile   none   swap   sw   0   0

Setup Prerequisites

To prepare the software development on RPi3 we need setup some prerequisite tools:

     sudo apt-get update
     sudo apt-get install git build-essential linux-libc-dev
     sudo apt-get install cmake cmake-gui swig3.0
     sudo apt-get install libusb-1.0-0-dev libusb-dev libudev-dev
     sudo apt-get install mpi-default-dev openmpi-bin openmpi-common  
     sudo apt-get install libflann1.8 libflann-dev
     sudo apt-get install libeigen3-dev
     sudo apt-get install libboost-all-dev
     sudo apt-get install libvtk5.10-qt4 libvtk5.10 libvtk5-dev
     sudo apt-get install libqhull* libgtest-dev
     sudo apt-get install freeglut3-dev pkg-config
     sudo apt-get install libxmu-dev libxi-dev 
     sudo apt-get install mono-complete
     sudo apt-get install qt-sdk openjdk-8-jdk openjdk-8-jre

Build Point Cloud Library

Point Cloud Library is needed by Voxel SDK, and can be obtained by:

     sudo add-apt-repository ppa:v-launchpad-jochen-sprickerhof-de/pcl
     sudo apt-get update
     git clone https://github.com/PointCloudLibrary/pcl.git

Now you should find pcl in the ~/Software directory.  Create a release directory and follow the cmake build process:

     mkdir release
     cd release
           -DBUILD_GPU=ON -DBUILD_apps=ON -DBUILD_examples=ON 
           -DCMAKE_INSTALL_PREFIX=/usr ..

The make will take over 7 hours. Once the build finishes, install it by:

     sudo make install

Build Voxel SDK

In the same Software directory, clone Voxel SDK:

     cd ~/Software
     git clone https://github.com/3dtof/voxelsdk.git

Go the voxelsdk directory and edit the top level CMakeList.txt with the below change:

     set(ARCH armhf)


     #add_definitions(-msse2 -pthread -std=c++11 -fPIC -ffast-math)
     add_definitions(-pthread -std=c++11 -fPIC -ffast-math -fpermissive)

Also the CMakeList.txt in the VoxelPCL subdirectory change the “wily” option by removing “1.7” from all the libpcl-*1.7, then change “wily” to “xenial”. (Note: Ubuntu 16.04 codename is “xenial”).

Create a build directory and run cmake-gui:

     mkdir build
     cd build
     cmake-gui ..

Change CMAKE_INSTALL_PREFIXto /usr, and make all Python to use Python 2.7 include and library instead of Python 3.5m. After you made these changes, click Configure and then Generate to create the makefile.

To build Voxel SDK enter:


During build you may get an error about duplicated “standard container” macro definition between one in swig3.0 and one in Voxel SDK–resolve it by commenting out the problematic macro in the Voxel SDK SWIG directory.

After a successful build, install Voxel SDK by:

     sudo make install

This will install need header files to /usr/include/voxel-0.6.5 and libraries to /usr/lib/voxel. Create a symbolic link as follow:

     sudo ln -s /usr/include/voxel-0.6.5 /usr/include/voxel

This is required before building demo applications below.

Build DemoApplications

Now we will test the installed Voxel SDK with demo applications available on GitHub:

     cd ~/Software
     git clone https://github.com/3dtof/DemoApplications.git
     cd DemoApplications/TinTin/people_tracking

Now remove the -msse2 flag from CMakeLists.txt.

     #add_definitions(-msse2 -pthread -std=c++11 -fPIC -ffast-math)
     add_definitions(-pthread -std=c++11 -fPIC -ffast-math -fpermissive)

If your machine does not have OpenCV installed, install it this way:

     sudo apt-get install libopencv-dev

Now we are ready to build the demo application:

     mkdir build
     cd build
     cmake ..

and you should see the demo running. If the demo is dropping frames or having the “slow forward pipeline” warnings, reduce the frame dimension by editing the main() inside the source file PeopleTracking.cpp so it looks like this:

     //Horus eye(80, 60)
     Horus eye(160, 120)
     //Horus eye(320, 240)

Now run make again and everything should be working smoothly.

Below is a video of the setup running one of the applications in the DemoApplications repository.

8 thoughts on “3D Depth Sensor for Raspberry Pi 3

    • From your command line prompt it looks like you launched cmake from ‘Software/release’ but the build files are written to ‘Software/pcl/release’. Please first reconcile that issue.

  1. For your info, using the latest Raspian Jessie Pixel, I was not able to install the libvtk stuff). BTW, there are some lines around QT, Mono and JDK. Do we really need these for getting the Kinect to run? They need another GB on flash…

  2. Ralph

    Hi there!

    I was just trying to go through all these steps when two errors occured:
    1st: Download failed: http/ppa.launchpad.net/v-launchpad-jochen-sprickerhof-de/pcl/ubuntu/dists/xenial/main/binary-armhf/Packager
    –> ignored or older ones used.

    2nd error came up when I enter the cmake -DCMAKE (…)
    “The source directory “/home/ralph” does not appear to contain CMakeLists.txt.”

    I am anything but pro at programming but I somehow have to make this work. Is there a connection between these errors or do you have any suggestion on how to make this work? 🙂

    Of course, you can contact me directly via email.

    Best regards,

    • Ralph

      I am running UbuntuMATE.
      Also, I re-entered the cmake command today (without the “..” at the end) and was able to start the make process. Though, after 3-4% the following errors occur:
      c++: internal compiler error: Killed (program cc1plus)

      full error can be found here (screenshot): https://prnt.sc/fop2yd

      Thanks for your help!

    • Ralph,

      cmake works needs to find CMakeLists.txt, which sets in the root directory containing the source. To build the source, create a ‘build’ directory under the source directory, then ‘cd’ into it. Then issue the “cmake .. ” command. That’s the correct way build.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s