Skip to content

Add cuda utils for SMART Camera #789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ option(USE_3D_SMART "Use 3D Smart camera?" OFF)
option(WITH_TOOLS "Build tools?" OFF)
option(IGNORE_TARGET_VERSION "Ignore sdk version on target?" OFF)
option(WITH_JS "Build JavaScript bindings?" OFF)
option(CUDA_ON_TARGET "Build cuda_utils bindings?" OFF)

# Set camera flags
if (USE_FXTOF1)
Expand Down Expand Up @@ -94,6 +95,10 @@ endif()
if (WITH_NETWORK)
add_definitions(-DHAS_NETWORK)
endif()
if (CUDA_ON_TARGET)
add_definitions(-DCUDA_ON_TARGET)
endif()


include(FindGit OPTIONAL)
if (GIT_FOUND)
Expand Down
24 changes: 24 additions & 0 deletions bindings/cuda_utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

cmake_minimum_required(VERSION 3.9)
project (aditof_cuda LANGUAGES CXX CUDA)
find_package(CUDA REQUIRED)
IF(CUDA_FOUND)
#ONLY FOR DEBUG
add_definitions(-DDEBUG=1)

add_definitions(-DPATH_TO_CNN_JSON="${CMAKE_SOURCE_DIR}/json_model/")
SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -dlink)
cuda_compile(cuda_utils cuda_utils.cu)
add_library(aditof_cuda SHARED
src/cuda_utils.cu
)
set_target_properties(aditof_cuda PROPERTIES
PUBLIC_HEADER include/cuda_utils.h
)
install(TARGETS aditof_cuda
LIBRARY DESTINATION ../../cuda_lib/
PUBLIC_HEADER DESTINATION ../../cuda_lib/)
install(FILES ${CMAKE_BINARY_DIR}/aditof_cuda.pc
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

ENDIF()
57 changes: 57 additions & 0 deletions bindings/cuda_utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Cuda binding for Smart Camera 3D
Eqipment:
1. Analog Devices Smart Camera (Jetson Nano and FXTOF1)
2. SSH connection to camera

## Using CUDA cores to optimize frame corrections

1. Clone the repository
2. Create bild directory and use the following cmake command for ```3D Smart Camera```:
```console
cmake -DUSE_3D_SMART=1 -DCUDA_ON_TARGET=1 -DWITH_EXAMPLES=1 -DCMAKE_PREFIX_PATH="/opt/glog;/opt/protobuf;/opt/websockets" ..

```
or the following command for ```Jetson Nano``` and ```FXTOF1``` setup:
```console
cmake -DJETSON=1 -DUSE_FXTOF1=1 -DCUDA_ON_TARGET=1 -DWITH_EXAMPLES=1 -DCMAKE_PREFIX_PATH="/opt/glog;/opt/protobuf;/opt/websockets" ..

```
3. Make the project with:
```console
make -j8
```
In order to disable the CUDA cores, set the ```-DCUDA_ON_TARGET``` flag to false.

4. Install Nvidia-GPU driver api and CUDA:
```console
sudo apt-get update
sudo apt-get install nvidia-l4t-jetson-multimedia-api
sudo apt-get install nvidia-cuda
```

[```Note```]: The cuda binding rebuild only when running ```cmake``` commands.
## [```Optional```] In order to install a new camera image with reduced size, use the following instructions

### 1. On camera
* Connect to camera via ssh (Either through ethenet connetion or wifi connection)
* Run the following command lines on the camera:
```console
wget https://swdownloads.analog.com/cse/aditof/smart_camera/img_update/cam_update.tar
tar -xvf cam_update.tar
cd cam_update
sudo bash update-script.sh
```
* After the automatic restart of the camera, open port number 8080 at the same ip address the camera had previously. (```<ip_addr>:8080```)
* Upload the following file via the ```Software upload``` interface: https://swdownloads.analog.com/cse/aditof/smart_camera/img_update/adi-jetson-nano-img_1.0.swu
* After uploading the file and rebooting the camera you can connect again via ssh, and run the following commands (the installation commands may take a few minutes):
```console
sudo apt-get update
sudo apt-get install nvidia-l4t-jetson-multimedia-api
sudo apt-get install nvidia-cuda
```
* add ```nvcc``` to the macro PATH:
```console
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}$
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
```
At this point the camera is ready to be used with cuda API.
83 changes: 83 additions & 0 deletions bindings/cuda_utils/include/cuda_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2019, Analog Devices, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CUDA_UTILS
#define CUDA_UTILS

#include <fstream>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string>
#include <vector>

#define THREAD_PER_BLOCK 1024

class cudaOnTarget {
private:
//correction cahes
double *m_geometry_cache_d;
double *m_distortion_cache_d;
uint16_t *m_depth_cache_d;

// double *m_geometry_cache;
// double *m_distortion_cache;
// uint16_t *m_depth_cache;

//frames and parameters
uint16_t *m_frame_d;
uint16_t *m_frame;
double *m_parameters_d;
double *m_parameters;

public:
void buildGeometryCorrectionCache();
void buildDistortionCorrectionCache();
void buildDepthCorrectionCache();

void applyGeometryCorrection();
void applyDistortionCorrection();
void applyDepthCorrection();

void cpyFrameToGPU(uint16_t *frame);
void cpyFrameFromGPU(uint16_t *frame);
void printFrameFromGPU();

void setParameters(double *parameters);
//Order of parameters:
// double width, double height, double fx, double fy,
// double cx, double cy, double k1, double k2, double k3,
// double x0, double y0, double gain, double offset,
// double pixelMaxValue, double range
void freeAll();
};

#endif // CUDA_UTILS
Loading