├── .github └── workflows │ └── manual.yml ├── CMakeLists.txt ├── CMakePreBuild.sh ├── CODEOWNERS ├── LICENSE.md ├── README.md ├── c ├── aiAgent.cpp ├── aiAgent.h ├── deepRL.h ├── dqnAgent.cpp ├── dqnAgent.h ├── pyTensor.cpp ├── pyTensor.h ├── pyTorch.h ├── rlAgent.cpp └── rlAgent.h ├── cuda ├── cudaPlanar.cu └── cudaPlanar.h ├── docs └── images │ ├── DQN-cartpole.png │ ├── LunarLander.png │ ├── gazebo.png │ └── jetson-reinforcement-header.jpg ├── gazebo ├── ArmPlugin.cpp ├── ArmPlugin.h ├── CMakeLists.txt ├── PropPlugin.cpp ├── PropPlugin.h ├── gazebo-arm.sh ├── gazebo-arm.world ├── gazebo-pkgs │ ├── Dockerfile │ ├── LICENSE │ ├── README.md │ ├── TODO.md │ ├── gazebo_grasp_plugin │ │ ├── CHANGELOG.rst │ │ ├── CMakeLists.txt │ │ ├── include │ │ │ └── gazebo_grasp_plugin │ │ │ │ ├── GazeboGraspFix.h │ │ │ │ └── GazeboGraspGripper.h │ │ ├── package.xml │ │ └── src │ │ │ ├── GazeboGraspFix.cpp │ │ │ └── GazeboGraspGripper.cpp │ ├── gazebo_state_plugins │ │ ├── CHANGELOG.rst │ │ ├── CMakeLists.txt │ │ ├── config │ │ │ ├── GazeboMapPublisher.yaml │ │ │ ├── GazeboObjectInfo.yaml │ │ │ └── WorldPlugins.yaml │ │ ├── include │ │ │ └── gazebo_state_plugins │ │ │ │ ├── GazeboMapPublisher.h │ │ │ │ └── GazeboObjectInfo.h │ │ ├── launch │ │ │ └── plugin_loader.launch │ │ ├── package.xml │ │ ├── src │ │ │ ├── GazeboMapPublisher.cpp │ │ │ └── GazeboObjectInfo.cpp │ │ └── test │ │ │ └── object_info_request.cpp │ ├── gazebo_test_tools │ │ ├── CHANGELOG.rst │ │ ├── CMakeLists.txt │ │ ├── config │ │ │ ├── FakeObjectRecognizer.yaml │ │ │ └── ObjectTFBroadcaster.yaml │ │ ├── include │ │ │ └── gazebo_test_tools │ │ │ │ ├── FakeObjectRecognizer.h │ │ │ │ └── gazebo_cube_spawner.h │ │ ├── launch │ │ │ ├── fake_object_recognizer.launch │ │ │ ├── gazebo_fake_object_recognition.launch │ │ │ ├── object_tf_broadcaster.launch │ │ │ └── spawn_and_recognize_cube.launch │ │ ├── package.xml │ │ ├── src │ │ │ ├── FakeObjectRecognizer.cpp │ │ │ ├── SetGazeboPhysicsClient.cpp │ │ │ ├── cube_spawner.cpp │ │ │ ├── cube_spawner_node.cpp │ │ │ └── fake_object_recognizer_node.cpp │ │ ├── srv │ │ │ └── RecognizeGazeboObject.srv │ │ └── test │ │ │ └── fake_object_recognizer_cmd.cpp │ └── gazebo_world_plugin_loader │ │ ├── CHANGELOG.rst │ │ ├── CMakeLists.txt │ │ ├── config │ │ └── WorldPluginsTemplate.config │ │ ├── include │ │ └── gazebo_world_plugin_loader │ │ │ └── GazeboPluginLoader.h │ │ ├── launch │ │ └── plugin_loader_template.launch │ │ ├── package.xml │ │ └── src │ │ └── GazeboPluginLoader.cpp └── meshes │ ├── gripper │ ├── finger_left.dae │ ├── finger_left_collision.dae │ ├── finger_right.dae │ ├── finger_right_collision.dae │ └── gripper_base.dae │ └── kr210l150 │ ├── README.md │ ├── collision │ ├── base_link.stl │ ├── link_1.stl │ ├── link_2.stl │ ├── link_3.stl │ ├── link_4.stl │ ├── link_5.stl │ └── link_6.stl │ └── visual │ ├── base_link.dae │ ├── link_1.dae │ ├── link_2.dae │ ├── link_3.dae │ ├── link_4.dae │ ├── link_5.dae │ └── link_6.dae ├── lua ├── DQN.lua ├── NEON.c ├── catchDQN.lua ├── catchENV.lua ├── hello.lua ├── test-cutorch.lua ├── test-lua.lua ├── test-packages.lua └── test-torch.lua ├── python ├── CRNN.py ├── DQN.py ├── RL.py ├── gym-DQN.py ├── gym-RL.py ├── gym-actorCritic.py ├── test-interop.py └── test-torch.py ├── samples ├── CMakeLists.txt ├── catch │ ├── CMakeLists.txt │ └── catch.cpp └── fruit │ ├── CMakeLists.txt │ ├── fruit.cpp │ ├── fruitEnv.cpp │ └── fruitEnv.h ├── tools ├── CMakeLists.txt ├── console │ ├── CMakeLists.txt │ ├── main.lua.cpp │ └── main.py.cpp ├── diagnostic │ ├── CMakeLists.txt │ ├── main.lua.cpp │ └── main.py.cpp └── input │ ├── CMakeLists.txt │ └── main.cpp └── utils ├── .gitignore ├── CMakeLists.txt ├── LICENSE.md ├── README.md ├── camera ├── gst-camera │ ├── CMakeLists.txt │ └── gst-camera.cpp ├── gstCamera.cpp ├── gstCamera.h ├── gstUtility.cpp ├── gstUtility.h ├── v4l2-console │ ├── CMakeLists.txt │ └── v4l2-console.cpp ├── v4l2-display │ ├── CMakeLists.txt │ └── v4l2-display.cpp ├── v4l2Camera.cpp └── v4l2Camera.h ├── commandLine.cpp ├── commandLine.h ├── cuda ├── cudaFont.cu ├── cudaFont.h ├── cudaMappedMemory.h ├── cudaNormalize.cu ├── cudaNormalize.h ├── cudaOverlay.cu ├── cudaOverlay.h ├── cudaRGB.cu ├── cudaRGB.h ├── cudaResize.cu ├── cudaResize.h ├── cudaUtility.h ├── cudaYUV-NV12.cu ├── cudaYUV-YUYV.cu ├── cudaYUV-YV12.cu └── cudaYUV.h ├── data ├── fontmapA.png └── fontmapB.png ├── display ├── gl-display-test │ ├── CMakeLists.txt │ └── gl-display-test.cpp ├── glDisplay.cpp ├── glDisplay.h ├── glTexture.cpp ├── glTexture.h └── glUtility.h ├── input ├── devInput.cpp ├── devInput.h ├── devJoystick.cpp ├── devJoystick.h ├── devKeyboard.cpp └── devKeyboard.h ├── loadImage.cpp ├── loadImage.h ├── pi.h └── rand.h /.github/workflows/manual.yml: -------------------------------------------------------------------------------- 1 | # Workflow to ensure whenever a Github PR is submitted, 2 | # a JIRA ticket gets created automatically. 3 | name: Manual Workflow 4 | 5 | # Controls when the action will run. 6 | on: 7 | # Triggers the workflow on pull request events but only for the master branch 8 | pull_request_target: 9 | types: [opened, reopened] 10 | 11 | # Allows you to run this workflow manually from the Actions tab 12 | workflow_dispatch: 13 | 14 | jobs: 15 | test-transition-issue: 16 | name: Convert Github Issue to Jira Issue 17 | runs-on: ubuntu-latest 18 | steps: 19 | - name: Checkout 20 | uses: actions/checkout@master 21 | 22 | - name: Login 23 | uses: atlassian/gajira-login@master 24 | env: 25 | JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }} 26 | JIRA_USER_EMAIL: ${{ secrets.JIRA_USER_EMAIL }} 27 | JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }} 28 | 29 | - name: Create NEW JIRA ticket 30 | id: create 31 | uses: atlassian/gajira-create@master 32 | with: 33 | project: CONUPDATE 34 | issuetype: Task 35 | summary: | 36 | Github PR [Assign the ND component] | Repo: ${{ github.repository }} | PR# ${{github.event.number}} 37 | description: | 38 | Repo link: https://github.com/${{ github.repository }} 39 | PR no. ${{ github.event.pull_request.number }} 40 | PR title: ${{ github.event.pull_request.title }} 41 | PR description: ${{ github.event.pull_request.description }} 42 | In addition, please resolve other issues, if any. 43 | fields: '{"components": [{"name":"Github PR"}], "customfield_16449":"https://classroom.udacity.com/", "customfield_16450":"Resolve the PR", "labels": ["github"], "priority":{"id": "4"}}' 44 | 45 | - name: Log created issue 46 | run: echo "Issue ${{ steps.create.outputs.issue }} was created" 47 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 2.8) 3 | project(jetson-reinforcement) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # -std=gnu++11 6 | 7 | set(BUILD_DEPS "YES" CACHE BOOL "If YES, will install dependencies into sandbox. Automatically reset to NO after dependencies are installed.") 8 | set(BUILD_OPENBLAS "YES" CACHE BOOL "If YES, will download & build OpenBLAS (for Torch) into sandbox") 9 | set(USE_PYTHON "YES" CACHE BOOL "If YES, will build PyTorch and PyTorch examples") 10 | set(USE_LUA "NO" CACHE BOOL "If YES, will build LUA, Torch, and Torch examples") 11 | 12 | 13 | # configure CUDA 14 | find_package(CUDA) 15 | 16 | set( 17 | CUDA_NVCC_FLAGS 18 | ${CUDA_NVCC_FLAGS}; 19 | -O3 20 | -gencode arch=compute_37,code=sm_37 21 | -gencode arch=compute_53,code=sm_53 22 | -gencode arch=compute_60,code=sm_60 23 | -gencode arch=compute_61,code=sm_61 24 | -gencode arch=compute_62,code=sm_62 25 | ) 26 | 27 | 28 | # if this is the first time running cmake, perform pre-build dependency install script (or if the user manually triggers re-building the dependencies) 29 | if( ${BUILD_DEPS} ) 30 | message("Launching pre-build dependency installer script...") 31 | 32 | execute_process(COMMAND sh ../CMakePreBuild.sh ${BUILD_OPENBLAS} ${USE_PYTHON} ${USE_LUA} 33 | WORKING_DIRECTORY ${PROJECT_BINARY_DIR} 34 | RESULT_VARIABLE PREBUILD_SCRIPT_RESULT) 35 | 36 | set(BUILD_DEPS "NO" CACHE BOOL "If YES, will install dependencies into sandbox. Automatically reset to NO after dependencies are installed." FORCE) 37 | message("Finished installing dependencies") 38 | endif() 39 | 40 | 41 | # setup project output paths 42 | set(PROJECT_OUTPUT_DIR ${PROJECT_BINARY_DIR}/${CMAKE_SYSTEM_PROCESSOR}) 43 | set(PROJECT_INCLUDE_DIR ${PROJECT_OUTPUT_DIR}/include) 44 | 45 | file(MAKE_DIRECTORY ${PROJECT_OUTPUT_DIR}/bin) 46 | file(MAKE_DIRECTORY ${PROJECT_INCLUDE_DIR}) 47 | 48 | message("-- system arch: ${CMAKE_SYSTEM_PROCESSOR}") 49 | message("-- output path: ${PROJECT_OUTPUT_DIR}") 50 | message("-- nvcuda path: ${CUDA_TOOLKIT_ROOT_DIR}") 51 | 52 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_OUTPUT_DIR}/bin) 53 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_OUTPUT_DIR}/lib) 54 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_OUTPUT_DIR}/lib) 55 | 56 | 57 | # set preprocessor defines 58 | if( ${USE_LUA} ) 59 | add_definitions(-DUSE_LUA) 60 | include_directories(${PROJECT_BINARY_DIR}/torch/include ${PROJECT_BINARY_DIR}/torch/include/TH ${PROJECT_INCLUDE_DIR}) 61 | link_directories(${PROJECT_BINARY_DIR}/torch/lib) 62 | endif() 63 | 64 | if( ${USE_PYTHON} ) 65 | add_definitions(-DUSE_PYTHON) 66 | include_directories(/usr/include/python2.7 ${PROJECT_BINARY_DIR}/pytorch/torch/lib/include ${PROJECT_BINARY_DIR}/pytorch/torch/csrc ${PROJECT_BINARY_DIR}/pytorch/torch/csrc/cuda ${PROJECT_BINARY_DIR}/pytorch ${PROJECT_BINARY_DIR}/pytorch/torch/lib/include/TH ${PROJECT_INCLUDE_DIR}) 67 | link_directories(${PROJECT_BINARY_DIR}/pytorch/torch/lib ${PROJECT_BINARY_DIR}/pytorch/torch) 68 | link_directories(${CUDA_TOOLKIT_ROOT_DIR}/lib64) 69 | endif() 70 | 71 | 72 | 73 | 74 | # library sources 75 | file(GLOB deepRLSources c/*.cpp cuda/*.cu) 76 | file(GLOB deepRLIncludes c/*.h cuda/*.h) 77 | 78 | cuda_add_library(jetson-reinforcement SHARED ${deepRLSources}) 79 | 80 | 81 | # transfer all headers to the include directory 82 | foreach(include ${deepRLIncludes}) 83 | message("-- Copying ${include}") 84 | configure_file(${include} ${PROJECT_INCLUDE_DIR} COPYONLY) 85 | endforeach() 86 | 87 | 88 | # Python post-build steps 89 | if( ${USE_PYTHON} ) 90 | target_link_libraries(jetson-reinforcement python2.7 _C ATen cuda nvrtc) 91 | 92 | # transfer all python scripts to bin directory 93 | file(GLOB pyScripts python/*.py) 94 | 95 | foreach(pyScript ${pyScripts}) 96 | message("-- Copying ${pyScript}") 97 | configure_file(${pyScript} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COPYONLY) 98 | endforeach() 99 | endif() 100 | 101 | 102 | # LUA post-build steps 103 | if( ${USE_LUA} ) 104 | target_link_libraries(jetson-reinforcement luajit luaT TH THC) 105 | 106 | # transfer all LUA scripts to bin directory 107 | file(GLOB luaScripts lua/*.lua lua/*.ipynb) 108 | 109 | foreach(luaScript ${luaScripts}) 110 | message("-- Copying ${luaScript}") 111 | configure_file(${luaScript} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COPYONLY) 112 | endforeach() 113 | 114 | endif() 115 | 116 | 117 | add_subdirectory(gazebo) 118 | add_subdirectory(samples) 119 | add_subdirectory(tools) 120 | add_subdirectory(utils) 121 | 122 | target_link_libraries(jetson-reinforcement jetson-utils) 123 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @udacity/active-public-content -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | -------------------------------------------------------------------------------- /c/aiAgent.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #include "aiAgent.h" 6 | 7 | 8 | // constructor 9 | aiAgent::aiAgent() 10 | { 11 | 12 | } 13 | 14 | 15 | // destructor 16 | aiAgent::~aiAgent() 17 | { 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /c/aiAgent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #ifndef __AI_AGENT_H_ 6 | #define __AI_AGENT_H_ 7 | 8 | 9 | #include "pyTensor.h" 10 | 11 | 12 | /** 13 | * TypeID typedef 14 | */ 15 | typedef uint64_t TypeID; 16 | 17 | 18 | /** 19 | * AI Agent base class, given a state predicts the best action. 20 | */ 21 | class aiAgent 22 | { 23 | public: 24 | /** 25 | * Destructor 26 | */ 27 | virtual ~aiAgent(); 28 | 29 | /** 30 | * From the input state, predict the next action (inference) 31 | * This function isn't used during training, for that see NextReward() 32 | */ 33 | virtual bool NextAction( Tensor* state, int* action ) = 0; 34 | 35 | /** 36 | * Load model checkpoint 37 | */ 38 | virtual bool LoadCheckpoint( const char* filename ) = 0; 39 | 40 | /** 41 | * Save model checkpoint 42 | */ 43 | virtual bool SaveCheckpoint( const char* filename ) = 0; 44 | 45 | /** 46 | * GetType 47 | */ 48 | virtual TypeID GetType() const { return TYPE_AI; } 49 | 50 | /** 51 | * IsType 52 | */ 53 | bool IsType( TypeID type ) const { return ((GetType() & type) == type); } 54 | 55 | /** 56 | * TypeID 57 | */ 58 | const TypeID TYPE_AI = (1 << 0); 59 | 60 | protected: 61 | aiAgent(); 62 | }; 63 | 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /c/deepRL.h: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #ifndef __DEEP_REINFORCEMENT_LEARNER_H_ 6 | #define __DEEP_REINFORCEMENT_LEARNER_H_ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "pyTensor.h" 13 | 14 | #include "aiAgent.h" 15 | #include "rlAgent.h" 16 | #include "dqnAgent.h" 17 | 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /c/dqnAgent.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * deepRL 3 | */ 4 | 5 | #include "dqnAgent.h" 6 | 7 | 8 | // constructor 9 | dqnAgent::dqnAgent() 10 | { 11 | 12 | } 13 | 14 | 15 | // destructor 16 | dqnAgent::~dqnAgent() 17 | { 18 | 19 | } 20 | 21 | // Create 22 | dqnAgent* dqnAgent::Create(uint32_t width, uint32_t height, uint32_t channels, uint32_t numActions, 23 | const char* optimizer, float learning_rate, uint32_t replay_mem, uint32_t batch_size, 24 | float gamma, float epsilon_start, float epsilon_end, float epsilon_decay, 25 | bool use_lstm, int lstm_size, bool allow_random, bool debug_mode) 26 | { 27 | if( width == 0 || height == 0 || channels == 0 || numActions == 0 ) 28 | return NULL; 29 | 30 | dqnAgent* agent = new dqnAgent(); 31 | 32 | if( !agent ) 33 | return NULL; 34 | 35 | if( !agent->Init(width, height, channels, numActions, "DQN", 36 | DEFAULT_NEXT_ACTION, DEFAULT_NEXT_REWARD, DEFAULT_LOAD_MODEL, DEFAULT_SAVE_MODEL, 37 | optimizer, learning_rate, replay_mem, batch_size, gamma, 38 | epsilon_start, epsilon_end, epsilon_decay, 39 | use_lstm, lstm_size, allow_random, debug_mode)) 40 | { 41 | return NULL; 42 | } 43 | 44 | return agent; 45 | } 46 | 47 | 48 | 49 | // NextAction 50 | bool dqnAgent::NextAction( Tensor* state, int* action ) 51 | { 52 | if( !state || !action ) 53 | return false; 54 | 55 | return rlAgent::NextAction(state, action); 56 | } 57 | 58 | 59 | // NextReward 60 | bool dqnAgent::NextReward( float reward, bool end_episode ) 61 | { 62 | return rlAgent::NextReward(reward, end_episode); 63 | } 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /c/dqnAgent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #ifndef __DEEP_Q_LEARNING_AGENT_H_ 6 | #define __DEEP_Q_LEARNING_AGENT_H_ 7 | 8 | 9 | #include "rlAgent.h" 10 | 11 | 12 | /** 13 | * Deep Q-Learner Agent 14 | */ 15 | class dqnAgent : public rlAgent 16 | { 17 | public: 18 | /** 19 | * Create a new DQN agent training instance, 20 | * the dimensions of a 2D image are expected. 21 | */ 22 | static dqnAgent* Create( uint32_t width, uint32_t height, uint32_t channels, uint32_t numActions, 23 | const char* optimizer="RMSprop", float learning_rate=0.001, 24 | uint32_t replay_mem=10000, uint32_t batch_size=64, float gamma=0.9, 25 | float epsilon_start=0.9, float epsilon_end=0.05, float epsilon_decay=200, 26 | bool use_lstm=true, int lstm_size=256, bool allow_random=true, bool debug_mode=false); 27 | 28 | /** 29 | * Destructor 30 | */ 31 | virtual ~dqnAgent(); 32 | 33 | /** 34 | * From the input state, predict the next action (inference) 35 | * This function isn't used during training, for that see NextReward() 36 | */ 37 | virtual bool NextAction( Tensor* state, int* action ); 38 | 39 | /** 40 | * Next action with reward (training) 41 | */ 42 | virtual bool NextReward( float reward, bool end_episode ); 43 | 44 | /** 45 | * GetType 46 | */ 47 | virtual TypeID GetType() const { return TYPE_DQN; } 48 | 49 | /** 50 | * TypeID 51 | */ 52 | const TypeID TYPE_DQN = TYPE_RL | (1 << 2); 53 | 54 | protected: 55 | dqnAgent(); 56 | }; 57 | 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /c/pyTensor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #include "pyTensor.h" 6 | #include "pyTorch.h" 7 | 8 | #include "cudaMappedMemory.h" 9 | 10 | 11 | #if USE_PYTHON 12 | 13 | //------------------------------------------------------------------------------- 14 | extern THCState* state; 15 | //------------------------------------------------------------------------------- 16 | 17 | 18 | // constructor 19 | static pyTensor* new_pyTensor() 20 | { 21 | pyTensor* t = new pyTensor(); 22 | 23 | t->cpuTensor = NULL; 24 | t->gpuTensor = NULL; 25 | 26 | t->pyTensorCPU = NULL; 27 | t->pyTensorGPU = NULL; 28 | 29 | t->cpuPtr = NULL; 30 | t->gpuPtr = NULL; 31 | 32 | t->width = 0; 33 | t->height = 0; 34 | t->depth = 0; 35 | 36 | t->elements = 0; 37 | t->size = 0; 38 | 39 | return t; 40 | } 41 | 42 | 43 | // Alloc 44 | pyTensor* pyTensor::Alloc( uint32_t elements ) 45 | { 46 | if( elements == 0 ) 47 | return NULL; 48 | 49 | return Alloc(elements, 1); 50 | } 51 | 52 | 53 | // Alloc 54 | pyTensor* pyTensor::Alloc( uint32_t width, uint32_t height=1, uint32_t depth ) 55 | { 56 | if( width == 0 || height == 0 || depth == 0 ) 57 | return NULL; 58 | 59 | 60 | // allocate new tensor wrapper object 61 | pyTensor* t = new_pyTensor(); 62 | 63 | if( !t ) 64 | return NULL; 65 | 66 | t->width = width; 67 | t->height = height; 68 | t->depth = depth; 69 | t->elements = width * height * depth; 70 | t->size = t->elements * sizeof(float); 71 | 72 | 73 | // allocate CUDA shared memory 74 | if( !cudaAllocMapped((void**)&t->cpuPtr, (void**)&t->gpuPtr, t->size) ) 75 | { 76 | printf("[deepRL] failed to alloc CUDA buffers for tensor size %zu bytes\n", t->size); 77 | return NULL; 78 | } 79 | 80 | // allocate CPU tensor storage descriptor 81 | THFloatStorage* cpuStorage = THFloatStorage_newWithData((float*)t->cpuPtr, t->elements); 82 | 83 | if( !cpuStorage ) 84 | { 85 | printf("[deepRL] failed to alloc CPU THFloatStorage\n"); 86 | return 0; 87 | } 88 | 89 | // convey the size and dimensions of the tensor 90 | long sizedata[3] = { t->depth, t->height, t->width };//{ t->elements }; // { height, width }; 91 | long stridedata[3] = { t->width * t->height, t->width, 1 };//{ 1 }; // { width, 3 } (for YUV) 92 | 93 | THLongStorage* sizeStorage = THLongStorage_newWithData(sizedata, /*1*/3); 94 | THLongStorage* strideStorage = THLongStorage_newWithData(stridedata, /*1*/3); 95 | 96 | // alloc CPU tensor 97 | t->cpuTensor = THFloatTensor_new(); 98 | 99 | if( !t->cpuTensor ) 100 | { 101 | printf("[deepRL] failed to create CPU THFloatTensor()\n"); 102 | return NULL; 103 | } 104 | 105 | THFloatTensor_setStorage(t->cpuTensor, cpuStorage, 0LL, sizeStorage, strideStorage); 106 | 107 | // get the PyObject for CPU tensor 108 | t->pyTensorCPU = THPFloatTensor_New(t->cpuTensor); 109 | 110 | if( !t->pyTensorCPU ) 111 | { 112 | printf("[deepRL] failed to get PyObject from THFloatTensor\n"); 113 | return NULL; 114 | } 115 | 116 | // confirm that the CUDA THCState global variable has been set 117 | // note this should have occurred after 'import torch' was run in python 118 | printf("[deepRL] pyTorch THCState 0x%08X\n", state); 119 | 120 | if( !state ) 121 | { 122 | printf("[deepRL] pyTorch THCState is NULL\n"); 123 | return NULL; 124 | } 125 | 126 | // alloc GPU tensor storage descriptor 127 | THCudaStorage* gpuStorage = THCudaStorage_newWithData(state, (float*)t->gpuPtr, t->elements); 128 | 129 | if( !gpuStorage ) 130 | { 131 | printf("[deepRL] failed to alloc GPU THCudaStorage\n"); 132 | return NULL; 133 | } 134 | 135 | // alloc GPU tensor and set the storage to our descriptor 136 | t->gpuTensor = THCudaTensor_new(state); 137 | 138 | if( !t->gpuTensor ) 139 | { 140 | printf("[deepRL] failed to create GPU THCudaTensor()\n"); 141 | return NULL; 142 | } 143 | 144 | THCudaTensor_setStorage(state, t->gpuTensor, gpuStorage, 0LL, sizeStorage, strideStorage); 145 | 146 | // get the pyObject for GPU tensor 147 | t->pyTensorGPU = THCPFloatTensor_New(t->gpuTensor); 148 | 149 | if( !t->pyTensorGPU ) 150 | { 151 | printf("[deepRL] failed to get PyObject from THFloatTensor\n"); 152 | return NULL; 153 | } 154 | 155 | return t; 156 | } 157 | 158 | #endif 159 | 160 | -------------------------------------------------------------------------------- /c/pyTensor.h: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #ifndef __PY_TENSOR_H_ 6 | #define __PY_TENSOR_H_ 7 | 8 | #include 9 | #include 10 | 11 | #ifdef USE_PYTHON 12 | 13 | struct THFloatTensor; 14 | struct THCudaTensor; 15 | 16 | #ifndef PyObject_HEAD 17 | struct _object; 18 | typedef _object PyObject; 19 | #endif 20 | 21 | 22 | /** 23 | * Tensor wrapper for working with Torch/pyTorch. 24 | */ 25 | struct pyTensor 26 | { 27 | /** 28 | * Allocate a Torch float tensor mapped to CPU/GPU. 29 | */ 30 | static pyTensor* Alloc( uint32_t elements ); 31 | 32 | /** 33 | * Allocate a Torch float tensor mapped to CPU/GPU. 34 | */ 35 | static pyTensor* Alloc( uint32_t width, uint32_t height, uint32_t depth=1 ); 36 | 37 | 38 | // tensor objects 39 | THFloatTensor* cpuTensor; 40 | THCudaTensor* gpuTensor; // (THCudaTensor defined as THCudaFloatTensor in THCGenerateAllTypes) 41 | 42 | PyObject* pyTensorCPU; // Python handle to cpuTensor 43 | PyObject* pyTensorGPU; // Python handle to gpuTensor 44 | 45 | float* cpuPtr; 46 | float* gpuPtr; 47 | 48 | uint32_t width; 49 | uint32_t height; 50 | uint32_t depth; 51 | 52 | size_t elements; 53 | size_t size; 54 | }; 55 | 56 | 57 | typedef pyTensor Tensor; 58 | 59 | 60 | #endif 61 | #endif 62 | -------------------------------------------------------------------------------- /c/pyTorch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #ifndef __PY_TORCH_H_ 6 | #define __PY_TORCH_H_ 7 | 8 | #ifdef USE_PYTHON 9 | 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /cuda/cudaPlanar.cu: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #include "cudaUtility.h" 6 | 7 | 8 | 9 | // Convert an image (uchar3) from being interleaved by pixel, to planar band-sequential FP32 BGR 10 | template __global__ void gpuPackedToPlanarBGR( T* input, int iWidth, float* output, int oWidth, int oHeight, 11 | float2 scale, float range_multiplier ) 12 | { 13 | const int x = blockIdx.x * blockDim.x + threadIdx.x; 14 | const int y = blockIdx.y * blockDim.y + threadIdx.y; 15 | const int n = oWidth * oHeight; 16 | 17 | if( x >= oWidth || y >= oHeight ) 18 | return; 19 | 20 | const int dx = ((float)x * scale.x); 21 | const int dy = ((float)y * scale.y); 22 | 23 | const T in = input[ dy * iWidth + dx ]; 24 | const float3 bgr = make_float3(in.z, in.y, in.x); 25 | 26 | output[n * 0 + y * oWidth + x] = bgr.x * range_multiplier; 27 | output[n * 1 + y * oWidth + x] = bgr.y * range_multiplier; 28 | output[n * 2 + y * oWidth + x] = bgr.z * range_multiplier; 29 | } 30 | 31 | 32 | // cudaPackedToPlanarBGR 33 | cudaError_t cudaPackedToPlanarBGR( uchar3* input, size_t inputWidth, size_t inputHeight, 34 | float* output, size_t outputWidth, size_t outputHeight ) 35 | { 36 | if( !input || !output ) 37 | return cudaErrorInvalidDevicePointer; 38 | 39 | if( inputWidth == 0 || outputWidth == 0 || inputHeight == 0 || outputHeight == 0 ) 40 | return cudaErrorInvalidValue; 41 | 42 | const float2 scale = make_float2( float(inputWidth) / float(outputWidth), 43 | float(inputHeight) / float(outputHeight) ); 44 | 45 | const float range_multiplier = 1.0f; 46 | 47 | // launch kernel 48 | const dim3 blockDim(8, 8); 49 | const dim3 gridDim(iDivUp(outputWidth,blockDim.x), iDivUp(outputHeight,blockDim.y)); 50 | 51 | gpuPackedToPlanarBGR<<>>(input, inputWidth, output, outputWidth, outputHeight, scale, range_multiplier); 52 | 53 | return CUDA(cudaGetLastError()); 54 | } 55 | 56 | 57 | // cudaRGBAToPlanarBGR 58 | cudaError_t cudaRGBAToPlanarBGR( float4* input, size_t inputWidth, size_t inputHeight, const float2& inputRange, 59 | float* output, size_t outputWidth, size_t outputHeight, const float2& outputRange ) 60 | { 61 | if( !input || !output ) 62 | return cudaErrorInvalidDevicePointer; 63 | 64 | if( inputWidth == 0 || outputWidth == 0 || inputHeight == 0 || outputHeight == 0 ) 65 | return cudaErrorInvalidValue; 66 | 67 | const float2 scale = make_float2( float(inputWidth) / float(outputWidth), 68 | float(inputHeight) / float(outputHeight) ); 69 | 70 | const float range_multiplier = outputRange.y / inputRange.y; 71 | 72 | // launch kernel 73 | const dim3 blockDim(8, 8); 74 | const dim3 gridDim(iDivUp(outputWidth,blockDim.x), iDivUp(outputHeight,blockDim.y)); 75 | 76 | gpuPackedToPlanarBGR<<>>(input, inputWidth, output, outputWidth, outputHeight, scale, range_multiplier); 77 | 78 | return CUDA(cudaGetLastError()); 79 | } 80 | 81 | 82 | // cudaRGBAToPlanarBGR 83 | cudaError_t cudaRGBAToPlanarBGR( float4* input, size_t inputWidth, size_t inputHeight, 84 | float* output, size_t outputWidth, size_t outputHeight ) 85 | { 86 | return cudaRGBAToPlanarBGR(input, inputWidth, inputHeight, make_float2(0.0f, 1.0f), 87 | output, outputWidth, outputHeight, make_float2(0.0f, 1.0f)); 88 | } 89 | 90 | -------------------------------------------------------------------------------- /cuda/cudaPlanar.h: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #ifndef __CUDA_PLANAR_IMAGE_H_ 6 | #define __CUDA_PLANAR_IMAGE_H_ 7 | 8 | 9 | #include "cudaUtility.h" 10 | 11 | 12 | /** 13 | * Convert a packed uchar3 image to a floating-point planar BGR image (band-sequential) 14 | */ 15 | cudaError_t cudaPackedToPlanarBGR( uchar3* input, size_t inputWidth, size_t inputHeight, 16 | float* output, size_t outputWidth, size_t outputHeight ); 17 | 18 | /** 19 | * Convert a packed float4 image to a floating-point planar BGR image (band-sequential) 20 | */ 21 | cudaError_t cudaRGBAToPlanarBGR( float4* input, size_t inputWidth, size_t inputHeight, 22 | float* output, size_t outputWidth, size_t outputHeight ); 23 | 24 | 25 | /** 26 | * Convert a packed float4 image to a floating-point planar BGR image (band-sequential) 27 | */ 28 | cudaError_t cudaRGBAToPlanarBGR( float4* input, size_t inputWidth, size_t inputHeight, const float2& inputRange, 29 | float* output, size_t outputWidth, size_t outputHeight, const float2& outputRange ); 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /docs/images/DQN-cartpole.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/docs/images/DQN-cartpole.png -------------------------------------------------------------------------------- /docs/images/LunarLander.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/docs/images/LunarLander.png -------------------------------------------------------------------------------- /docs/images/gazebo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/docs/images/gazebo.png -------------------------------------------------------------------------------- /docs/images/jetson-reinforcement-header.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/docs/images/jetson-reinforcement-header.jpg -------------------------------------------------------------------------------- /gazebo/ArmPlugin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #ifndef __GAZEBO_ARM_PLUGIN_H__ 6 | #define __GAZEBO_ARM_PLUGIN_H__ 7 | 8 | #include "deepRL.h" 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | 36 | namespace gazebo 37 | { 38 | 39 | /** 40 | * ArmPlugin 41 | */ 42 | class ArmPlugin : public ModelPlugin 43 | { 44 | public: 45 | ArmPlugin(); 46 | 47 | virtual void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/); 48 | virtual void OnUpdate(const common::UpdateInfo & /*_info*/); 49 | 50 | float resetPosition( uint32_t dof ); // center servo positions 51 | 52 | bool createAgent(); 53 | bool updateAgent(); 54 | bool updateJoints(); 55 | 56 | void onCameraMsg(ConstImageStampedPtr &_msg); 57 | void onCollisionMsg(ConstContactsPtr &contacts); 58 | 59 | static const uint32_t DOF = 3; // active degrees of freedom in the arm 60 | 61 | private: 62 | float ref[DOF]; // joint reference positions 63 | float vel[DOF]; // joint velocity control 64 | float dT[3]; // IK delta theta 65 | 66 | rlAgent* agent; // AI learning agent instance 67 | bool newState; // true if a new frame needs processed 68 | bool newReward; // true if a new reward's been issued 69 | bool endEpisode; // true if this episode is over 70 | float rewardHistory; // value of the last reward issued 71 | Tensor* inputState; // pyTorch input object to the agent 72 | void* inputBuffer[2]; // [0] for CPU and [1] for GPU 73 | size_t inputBufferSize; 74 | size_t inputRawWidth; 75 | size_t inputRawHeight; 76 | float jointRange[DOF][2]; // min/max range of each arm joint 77 | float actionJointDelta; // amount of offset caused to a joint by an action 78 | float actionVelDelta; // amount of velocity offset caused to a joint by an action 79 | int maxEpisodeLength; // maximum number of frames to win episode (or <= 0 for unlimited) 80 | int episodeFrames; // frame counter for the current episode 81 | bool testAnimation; // true for test animation mode 82 | bool loopAnimation; // loop the test animation while true 83 | uint32_t animationStep; 84 | float resetPos[DOF]; 85 | float lastGoalDistance; 86 | float avgGoalDelta; 87 | int successfulGrabs; 88 | int totalRuns; 89 | int runHistoryIdx; 90 | int runHistoryMax; 91 | bool runHistory[20]; 92 | 93 | physics::ModelPtr model; 94 | event::ConnectionPtr updateConnection; 95 | physics::JointController* j2_controller; 96 | 97 | gazebo::transport::NodePtr cameraNode; 98 | gazebo::transport::SubscriberPtr cameraSub; 99 | 100 | gazebo::transport::NodePtr collisionNode; 101 | gazebo::transport::SubscriberPtr collisionSub; 102 | }; 103 | 104 | } 105 | 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /gazebo/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8 FATAL_ERROR) 2 | 3 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # -std=gnu++11 4 | 5 | find_package(Boost REQUIRED COMPONENTS system) 6 | include_directories(${Boost_INCLUDE_DIRS}) 7 | link_directories(${Boost_LIBRARY_DIRS}) 8 | 9 | include (FindPkgConfig) 10 | if (PKG_CONFIG_FOUND) 11 | pkg_check_modules(GAZEBO gazebo) 12 | endif() 13 | 14 | 15 | if (GAZEBO_FOUND) 16 | 17 | message("-- Gazebo7 installation detected, building plugins...") 18 | 19 | include_directories(${GAZEBO_INCLUDE_DIRS} gazebo-pkgs/gazebo_grasp_plugin/include) 20 | link_directories(${GAZEBO_LIBRARY_DIRS}) 21 | #link_directories(${GAZEBO_LIBRARY_DIRS} gazebo-pkgs/gazebo_grasp_plugin/include) 22 | 23 | add_library(gazeboArmPlugin SHARED ArmPlugin.cpp) 24 | add_library(gazeboPropPlugin SHARED PropPlugin.cpp) 25 | add_library(gazeboGraspPlugin SHARED gazebo-pkgs/gazebo_grasp_plugin/src/GazeboGraspFix.cpp gazebo-pkgs/gazebo_grasp_plugin/src/GazeboGraspGripper.cpp) 26 | 27 | target_link_libraries(gazeboArmPlugin jetson-reinforcement ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES} ${PROTOBUF_LIBRARIES} pthread) 28 | target_link_libraries(gazeboPropPlugin jetson-reinforcement ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES} ${PROTOBUF_LIBRARIES} pthread) 29 | target_link_libraries(gazeboGraspPlugin jetson-reinforcement ${GAZEBO_LIBRARIES} ${Boost_LIBRARIES} ${PROTOBUF_LIBRARIES} pthread) 30 | 31 | # transfer all gazebo scripts to bin directory 32 | file(GLOB gazeboScripts *.world *.sdf *.sh) 33 | 34 | foreach(gazeboScript ${gazeboScripts}) 35 | message("-- Copying ${gazeboScript}") 36 | configure_file(${gazeboScript} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COPYONLY) 37 | endforeach() 38 | 39 | endif() 40 | 41 | # create symbolic link for meshes 42 | execute_process( COMMAND "${CMAKE_COMMAND}" "-E" "create_symlink" "${PROJECT_SOURCE_DIR}/gazebo/meshes" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/meshes" ) 43 | 44 | -------------------------------------------------------------------------------- /gazebo/PropPlugin.cpp: -------------------------------------------------------------------------------- 1 | #include "PropPlugin.h" 2 | 3 | 4 | namespace gazebo 5 | { 6 | 7 | // Register this plugin with the simulator 8 | GZ_REGISTER_MODEL_PLUGIN(PropPlugin) 9 | 10 | //--------------------------------------------------------------------------------------- 11 | std::vector props; 12 | 13 | size_t GetNumProps() 14 | { 15 | return props.size(); 16 | } 17 | 18 | PropPlugin* GetProp( size_t index ) 19 | { 20 | return props[index]; 21 | } 22 | 23 | PropPlugin* GetPropByName( const char* name ) 24 | { 25 | if( !name ) 26 | return NULL; 27 | 28 | const size_t numProps = props.size(); 29 | 30 | for( size_t n=0; n < numProps; n++ ) 31 | { 32 | if( strcmp(props[n]->model->GetName().c_str(), name) == 0 ) 33 | return props[n]; 34 | } 35 | 36 | return NULL; 37 | } 38 | 39 | void ResetPropDynamics() 40 | { 41 | const size_t numProps = props.size(); 42 | 43 | for( size_t n=0; n < numProps; n++ ) 44 | props[n]->ResetDynamics(); 45 | } 46 | 47 | void RandomizeProps() 48 | { 49 | const size_t numProps = props.size(); 50 | 51 | for( size_t n=0; n < numProps; n++ ) 52 | props[n]->Randomize(); 53 | } 54 | 55 | //--------------------------------------------------------------------------------------- 56 | 57 | 58 | // Plugin init 59 | void PropPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/) 60 | { 61 | printf("PropPlugin::Load('%s')\n", _parent->GetName().c_str()); 62 | 63 | // Store the pointer to the model 64 | this->model = _parent; 65 | 66 | // Store the original pose of the model 67 | this->originalPose = model->GetWorldPose(); 68 | 69 | // Listen to the update event. This event is broadcast every simulation iteration. 70 | this->updateConnection = event::Events::ConnectWorldUpdateBegin(boost::bind(&PropPlugin::OnUpdate, this, _1)); 71 | 72 | // Track this object in the global Prop registry 73 | props.push_back(this); 74 | } 75 | 76 | 77 | // Called by the world update start event 78 | void PropPlugin::OnUpdate(const common::UpdateInfo & /*_info*/) 79 | { 80 | // Apply a small linear velocity to the model. 81 | //this->model->SetLinearVel(math::Vector3(.03, 0, 0)); 82 | 83 | /*const math::Pose& pose = model->GetWorldPose(); 84 | 85 | printf("%s location: %lf %lf %lf\n", model->GetName().c_str(), pose.pos.x, pose.pos.y, pose.pos.z); 86 | 87 | const math::Box& bbox = model->GetBoundingBox(); 88 | 89 | printf("%s bounding: min=%lf %lf %lf max=%lf %lf %lf\n", model->GetName().c_str(), bbox.min.x, bbox.min.y, bbox.min.z, bbox.max.x, bbox.max.y, bbox.max.z); 90 | */ 91 | /*const math::Box& bbox = model->GetBoundingBox(); 92 | 93 | const math::Vector3 center = bbox.GetCenter(); 94 | const math::Vector3 bbSize = bbox.GetSize(); 95 | 96 | printf("%s bounding: center=%lf %lf %lf size=%lf %lf %lf\n", model->GetName().c_str(), center.x, center.y, center.z, bbSize.x, bbSize.y, bbSize.z); 97 | */ 98 | } 99 | 100 | 101 | // Reset the model's dynamics and pose to original 102 | void PropPlugin::ResetDynamics() 103 | { 104 | model->SetAngularAccel(math::Vector3(0.0, 0.0, 0.0)); 105 | model->SetAngularVel(math::Vector3(0.0, 0.0, 0.0)); 106 | model->SetLinearAccel(math::Vector3(0.0, 0.0, 0.0)); 107 | model->SetLinearVel(math::Vector3(0.0, 0.0, 0.0)); 108 | 109 | model->SetWorldPose(originalPose); 110 | } 111 | 112 | 113 | inline float randf( float rand_min, float rand_max ) 114 | { 115 | const float r = float(rand()) / float(RAND_MAX); 116 | return (r * (rand_max - rand_min)) + rand_min; 117 | } 118 | 119 | 120 | // Randomize 121 | void PropPlugin::Randomize() 122 | { 123 | model->SetAngularAccel(math::Vector3(0.0, 0.0, 0.0)); 124 | model->SetAngularVel(math::Vector3(0.0, 0.0, 0.0)); 125 | model->SetLinearAccel(math::Vector3(0.0, 0.0, 0.0)); 126 | model->SetLinearVel(math::Vector3(0.0, 0.0, 0.0)); 127 | 128 | math::Pose pose = originalPose; 129 | 130 | pose.pos.x = randf(0.35f, 0.45f); 131 | pose.pos.y = randf(-1.5f, 0.2f); 132 | pose.pos.z = 0.0f; 133 | 134 | printf("prop random pos: %f %f %f\n", pose.pos.x, pose.pos.y, pose.pos.z); 135 | model->SetWorldPose(pose); 136 | } 137 | 138 | } 139 | 140 | -------------------------------------------------------------------------------- /gazebo/PropPlugin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * http://github.com/dusty-nv/jetson-reinforcement 3 | */ 4 | 5 | #ifndef __GAZEBO_PROP_PLUGIN_H__ 6 | #define __GAZEBO_PROP_PLUGIN_H__ 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | 36 | 37 | namespace gazebo 38 | { 39 | 40 | /** 41 | * PropPlugin 42 | */ 43 | class PropPlugin : public ModelPlugin 44 | { 45 | public: 46 | virtual void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/); 47 | virtual void OnUpdate(const common::UpdateInfo & /*_info*/); 48 | 49 | void ResetDynamics(); 50 | void Randomize(); 51 | 52 | physics::ModelPtr model; // Pointer to the model 53 | 54 | private: 55 | 56 | // Original pose the prop was created in 57 | math::Pose originalPose; 58 | 59 | // Pointer to the update event connection 60 | event::ConnectionPtr updateConnection; 61 | }; 62 | 63 | 64 | /** 65 | * Retrieve the number of global Prop objects 66 | */ 67 | size_t GetNumProps(); 68 | 69 | /** 70 | * Retrieve a global Prop object by index 71 | */ 72 | PropPlugin* GetProp( size_t index ); 73 | 74 | /** 75 | * Retrieve a global Prop object by name 76 | */ 77 | PropPlugin* GetPropByName( const char* name ); 78 | 79 | /** 80 | * Reset all prop poses and dynamics 81 | */ 82 | void ResetPropDynamics(); 83 | 84 | /** 85 | * Randomize prop locations 86 | */ 87 | void RandomizeProps(); 88 | 89 | } 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /gazebo/gazebo-arm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo " " 4 | echo "configuring gazebo7 plugin paths" 5 | echo "previous GAZEBO_PLUGIN_PATH=$GAZEBO_PLUGIN_PATH" 6 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 7 | echo "script directory $SCRIPT_DIR" 8 | 9 | MY_PLUGIN_PATH=$SCRIPT_DIR/../lib 10 | echo "plugin path $MY_PLUGIN_PATH" 11 | 12 | export GAZEBO_PLUGIN_PATH=$MY_PLUGIN_PATH:$GAZEBO_PLUGIN_PATH 13 | echo "new GAZEBO_PLUGIN_PATH=$GAZEBO_PLUGIN_PATH" 14 | 15 | echo " " 16 | echo "starting gazebo7 simulator" 17 | gazebo gazebo-arm.world --verbose 18 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM jenniferbuehler/general-message-pkgs 2 | 3 | MAINTAINER Jennifer Buehler 4 | 5 | # Install required ROS dependencies 6 | RUN apt-get update && apt-get install -y \ 7 | ros-indigo-gazebo-ros \ 8 | ros-indigo-eigen-conversions \ 9 | ros-indigo-roslint \ 10 | && rm -rf /var/lib/apt/lists/ 11 | 12 | COPY gazebo_grasp_plugin /catkin_ws/src/gazebo_grasp_plugin 13 | COPY gazebo_state_plugins /catkin_ws/src/gazebo_state_plugins 14 | COPY gazebo_test_tools /catkin_ws/src/gazebo_test_tools 15 | COPY gazebo_world_plugin_loader /catkin_ws/src/gazebo_world_plugin_loader 16 | 17 | # Build 18 | RUN bin/bash -c "source /.bashrc \ 19 | && cd /catkin_ws \ 20 | && catkin_make \ 21 | && catkin_make install" 22 | 23 | RUN bin/bash -c "source .bashrc" 24 | 25 | CMD ["bash","-l"] 26 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/README.md: -------------------------------------------------------------------------------- 1 | # gazebo-pkgs 2 | 3 | A collection of tools and plugins for Gazebo. 4 | 5 | Please also refer to [the wiki](https://github.com/JenniferBuehler/gazebo-pkgs/wiki) for more information. 6 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/TODO.md: -------------------------------------------------------------------------------- 1 | - Include the local implementation of joint controller and joint trajectory execution (gazbo implementation) in these packages. 2 | 3 | # Possible future contributions 4 | 5 | gazebo: 6 | - the higher-level controller with position and velocity 7 | - a program which helps find PID values, either with neural network or Ziegler-Nichols method 8 | 9 | # Objects 10 | 11 | - move away from object_msgs and replace with object_recognition_msgs/RecognizedObject.msg. The bounding_mesh 12 | can be used to replace primitives, which then have to be supported in object_moveit/ObjectMessageGenerator. 13 | 14 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_grasp_plugin/CHANGELOG.rst: -------------------------------------------------------------------------------- 1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2 | Changelog for package gazebo_grasp_plugin 3 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 4 | 5 | 1.0.2 (2018-01-06) 6 | ------------------ 7 | * Merge branch jacknlliu-fix-c11-error 8 | * fix build error with c++ 11 9 | * Contributors: Jack Liu, Jennifer Buehler 10 | 11 | 1.0.1 (2016-06-08) 12 | ------------------ 13 | * Fixed cmake files for jenkins builds 14 | * Contributors: Jennifer Buehler 15 | 16 | 1.0.0 (2016-06-07) 17 | ------------------ 18 | * Initial release 19 | * Contributors: Jennifer Buehler 20 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_grasp_plugin/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.3) 2 | project(gazebo_grasp_plugin) 3 | 4 | ## Find catkin macros and libraries 5 | ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) 6 | ## is used, also find other catkin packages 7 | find_package(catkin REQUIRED COMPONENTS 8 | gazebo_ros 9 | geometry_msgs 10 | roscpp 11 | std_msgs 12 | ) 13 | 14 | find_package(gazebo REQUIRED) 15 | 16 | ## Uncomment this if the package has a setup.py. This macro ensures 17 | ## modules and global scripts declared therein get installed 18 | ## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html 19 | # catkin_python_setup() 20 | 21 | ################################### 22 | ## catkin specific configuration ## 23 | ################################### 24 | ## The catkin_package macro generates cmake config files for your package 25 | ## Declare things to be passed to dependent projects 26 | ## INCLUDE_DIRS: uncomment this if you package contains header files 27 | ## LIBRARIES: libraries you create in this project that dependent projects also need 28 | ## CATKIN_DEPENDS: catkin_packages dependent projects also need 29 | ## DEPENDS: system dependencies of this project that dependent projects also need 30 | catkin_package( 31 | INCLUDE_DIRS include 32 | LIBRARIES gazebo_grasp_fix 33 | CATKIN_DEPENDS gazebo_ros geometry_msgs roscpp std_msgs 34 | DEPENDS gazebo_ros 35 | ) 36 | 37 | ########### 38 | ## Build ## 39 | ########### 40 | # check c++11 / c++0x 41 | include(CheckCXXCompilerFlag) 42 | CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) 43 | CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X) 44 | if(COMPILER_SUPPORTS_CXX11) 45 | set(CMAKE_CXX_FLAGS "-std=c++11") 46 | elseif(COMPILER_SUPPORTS_CXX0X) 47 | set(CMAKE_CXX_FLAGS "-std=c++0x") 48 | else() 49 | message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler. Suggested solution: update the pkg build-essential ") 50 | endif() 51 | 52 | ## Specify additional locations of header files 53 | ## Your package locations should be listed before other locations 54 | # include_directories(include) 55 | include_directories( 56 | include 57 | ${catkin_INCLUDE_DIRS} 58 | ${GAZEBO_INCLUDE_DIRS} 59 | ) 60 | 61 | link_directories( 62 | ${GAZEBO_LIBRARY_DIRS} 63 | ${catkin_LIBRARY_DIRS} 64 | ) 65 | 66 | ## Declare a cpp library 67 | add_library(gazebo_grasp_fix SHARED src/GazeboGraspFix.cpp src/GazeboGraspGripper.cpp) 68 | 69 | ## Add cmake target dependencies of the executable/library 70 | ## as an example, message headers may need to be generated before nodes 71 | # add_dependencies(gazebo_grasp_plugin_node gazebo_grasp_plugin_generate_messages_cpp) 72 | add_dependencies(gazebo_grasp_fix ${catkin_EXPORTED_TARGETS}) 73 | 74 | ## Specify libraries to link a library or executable target against 75 | 76 | target_link_libraries(gazebo_grasp_fix 77 | ${GAZEBO_LIBRARIES} 78 | ${Boost_LIBRARIES} 79 | ) 80 | 81 | ############# 82 | ## Install ## 83 | ############# 84 | 85 | # all install targets should use catkin DESTINATION variables 86 | # See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html 87 | 88 | ## Mark executable scripts (Python etc.) for installation 89 | ## in contrast to setup.py, you can choose the destination 90 | # install(PROGRAMS 91 | # scripts/my_python_script 92 | # DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} 93 | # ) 94 | 95 | ## Mark executables and/or libraries for installation 96 | install(TARGETS gazebo_grasp_fix 97 | ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} 98 | LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} 99 | RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} 100 | ) 101 | 102 | ## Mark cpp header files for installation 103 | install(DIRECTORY include/${PROJECT_NAME}/ 104 | DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} 105 | FILES_MATCHING PATTERN "*.h" 106 | ) 107 | 108 | install(DIRECTORY launch 109 | DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} 110 | FILES_MATCHING PATTERN "*.launch" 111 | ) 112 | 113 | ## Mark other files for installation (e.g. launch and bag files, etc.) 114 | # install(FILES 115 | # # myfile1 116 | # # myfile2 117 | # DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} 118 | # ) 119 | 120 | ############# 121 | ## Testing ## 122 | ############# 123 | 124 | ## Add gtest based cpp test target and link libraries 125 | # catkin_add_gtest(${PROJECT_NAME}-test test/test_gazebo_grasp_plugin.cpp) 126 | # if(TARGET ${PROJECT_NAME}-test) 127 | # target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}) 128 | # endif() 129 | 130 | ## Add folders to be run by python nosetests 131 | # catkin_add_nosetests(test) 132 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_grasp_plugin/include/gazebo_grasp_plugin/GazeboGraspGripper.h: -------------------------------------------------------------------------------- 1 | #ifndef GAZEBO_GAZEBOGRASPGRIPPER_H 2 | #define GAZEBO_GAZEBOGRASPGRIPPER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace gazebo { 12 | 13 | /** 14 | * \brief Helper class for GazeboGraspFix which holds information for one arm. 15 | * Attaches /detaches objects to the palm of this arm. 16 | * 17 | * \author Jennifer Buehler 18 | */ 19 | class GazeboGraspGripper { 20 | public: 21 | GazeboGraspGripper(); 22 | GazeboGraspGripper(const GazeboGraspGripper& o); 23 | virtual ~GazeboGraspGripper(); 24 | 25 | /** 26 | * 27 | * \param disableCollisionsOnAttach when an object is attached, collisions with it will be disabled. This is useful 28 | * if the robot then still keeps wobbling. 29 | */ 30 | bool Init(physics::ModelPtr& _model, 31 | const std::string& _gripperName, 32 | const std::string& palmLinkName, 33 | const std::vector& fingerLinkNames, 34 | bool _disableCollisionsOnAttach, 35 | std::map& _collisions); 36 | 37 | const std::string& getGripperName() const; 38 | 39 | /** 40 | * Has the link name (URDF) 41 | */ 42 | bool hasLink(const std::string& linkName) const; 43 | 44 | /** 45 | * Has the collision link name (Gazebo collision element name) 46 | */ 47 | bool hasCollisionLink(const std::string& linkName) const; 48 | 49 | bool isObjectAttached() const; 50 | 51 | const std::string& attachedObject() const; 52 | 53 | /** 54 | * \param gripContacts contact forces on the object sorted by the link name colliding. 55 | */ 56 | bool HandleAttach(const std::string& objName); 57 | void HandleDetach(const std::string& objName); 58 | 59 | private: 60 | 61 | physics::ModelPtr model; 62 | 63 | // name of the gripper 64 | std::string gripperName; 65 | 66 | // names of the gripper links 67 | std::vector linkNames; 68 | // names and Collision objects of the collision links in Gazebo (scoped names) 69 | // Not necessarily equal names and size to linkNames. 70 | std::map collisionElems; 71 | 72 | physics::JointPtr fixedJoint; 73 | 74 | physics::LinkPtr palmLink; 75 | 76 | // when an object is attached, collisions with it may be disabled, in case the 77 | // robot still keeps wobbling. 78 | bool disableCollisionsOnAttach; 79 | 80 | // flag holding whether an object is attached. Object name in \e attachedObjName 81 | bool attached; 82 | // name of the object currently attached. 83 | std::string attachedObjName; 84 | }; 85 | 86 | } 87 | #endif // GAZEBO_GAZEBOGRASPGRIPPER_H 88 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_grasp_plugin/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | gazebo_grasp_plugin 4 | 1.0.2 5 | 6 | Gazebo Model plugin(s) which handle/help grasping in Gazebo. 7 | 8 | 9 | 10 | Jennifer Buehler 11 | 12 | 13 | 14 | 15 | 16 | GPLv3 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Jennifer Buehler 27 | 28 | 29 | 30 | 31 | catkin 32 | gazebo_ros 33 | gazebo 34 | geometry_msgs 35 | roscpp 36 | std_msgs 37 | gazebo_ros 38 | gazebo 39 | geometry_msgs 40 | roscpp 41 | std_msgs 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_state_plugins/CHANGELOG.rst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/gazebo/gazebo-pkgs/gazebo_state_plugins/CHANGELOG.rst -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_state_plugins/config/GazeboMapPublisher.yaml: -------------------------------------------------------------------------------- 1 | # the topic onto which to continuously publish 2 | # the map. 3 | publish_map_topic: "/gazebo/map" 4 | 5 | # frequency to publish the map 6 | map_pub_frequency: 1 7 | 8 | # topic onto which to publish the metadata 9 | # information of the map. Only will be published 10 | # if publish_map_topic is not empty. 11 | map_metadata_topic: "/map_metadata" 12 | 13 | # topic onto which to provide a service to 14 | # request the map. 15 | request_map_service: "/dynamic_map" 16 | 17 | # The frame ID to assign to the map 18 | map_frame_id: "/map" 19 | 20 | # the name of the robot in Gazebo 21 | robot_name: "robot" 22 | 23 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_state_plugins/config/GazeboObjectInfo.yaml: -------------------------------------------------------------------------------- 1 | # Continuously publish info of gazebo objects? 2 | # This should be false if another plugin publishes 3 | # gazebo object info. 4 | publish_world_objects: false 5 | 6 | # the topic onto which to publish object info of 7 | # type object_msgs/Object 8 | world_objects_topic: "/gazebo_objects" 9 | 10 | # The topic onto which to publish the service 11 | # which can be used to request object info 12 | request_object_service: "/gazebo_objects/get_info" 13 | 14 | # frame of the objects poses that will be published. 15 | # Should usually be 'world' 16 | objects_frame_id: "world" 17 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_state_plugins/config/WorldPlugins.yaml: -------------------------------------------------------------------------------- 1 | # Parameters read by GazeboPluginLoader: contains 2 | # names and library files of Gazebo World Plugins to be 3 | # loaded. 4 | 5 | # name of world to load the plugins on. 'default' or 6 | # empty string should yield the default world. 7 | world_name: "default" 8 | 9 | # names and filenames of plugins to load 10 | world_plugins: 11 | - name: gazebo_object_info 12 | file: libgazebo_object_info.so 13 | # - name: gazebo_map_publisher 14 | # file: libgazebo_map_publisher.so 15 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_state_plugins/include/gazebo_state_plugins/GazeboMapPublisher.h: -------------------------------------------------------------------------------- 1 | #ifndef GAZEBO_GAZEBOMAPPUBLISHER_H 2 | #define GAZEBO_GAZEBOMAPPUBLISHER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace gazebo 13 | { 14 | 15 | /** 16 | * Publishes a map of the grid in gazebo to a topic that the map is supposed to be published to. 17 | * 18 | * A service is also provided to request a map (service name can be specified with ros paramter 19 | * "request_map_service") of type nav_msgs/GetMap. 20 | * 21 | * The plugin parameters can be specified in a YAML file, 22 | * which needs to be loaded onto the parameter server 23 | * under **namespace gazebo_state_plugins**, as follows: 24 | * 25 | * ``` 26 | * # the topic onto which to continuously publish 27 | * # the map. 28 | * publish_map_topic: "/gazebo/map" 29 | * 30 | * # frequency to publish the map 31 | * map_pub_frequency: 1 32 | * 33 | * # topic onto which to publish the metadata 34 | * # information of the map. Only will be published 35 | * # if publish_map_topic is not empty. 36 | * map_metadata_topic: "/map_metadata" 37 | * 38 | * # topic onto which to provide a service to 39 | * # request the map. 40 | * request_map_service: "/dynamic_map" 41 | * 42 | * # The frame ID to assign to the map 43 | * map_frame_id: "/map" 44 | * 45 | * # the name of the robot in Gazebo 46 | * robot_name: "robot" 47 | * ``` 48 | * 49 | * \author Jennifer Buehler 50 | */ 51 | class GazeboMapPublisher : public WorldPlugin 52 | { 53 | public: 54 | 55 | GazeboMapPublisher(); 56 | void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf); 57 | 58 | private: 59 | 60 | bool requestMap(nav_msgs::GetMap::Request &req, nav_msgs::GetMap::Response &res); 61 | 62 | void advertEvent(const ros::TimerEvent& e); 63 | 64 | void onWorldUpdate(); 65 | 66 | // forward declaration 67 | class CollisionMapRequest; 68 | 69 | //see http://gazebosim.org/wiki/Tutorials/1.4/custom_messages#Collision_Map_Creator_Plugin 70 | bool createMap(const CollisionMapRequest &msg, const std::string& map_frame, nav_msgs::OccupancyGrid& map); 71 | 72 | CollisionMapRequest getCollisionRequest(); 73 | 74 | nav_msgs::MapMetaData getMetaData(); 75 | 76 | nav_msgs::OccupancyGrid getMap(); 77 | 78 | //map resolution (m/cell) 79 | float map_resolution; 80 | 81 | //height of obstacles to consider for generating the map 82 | float map_height; 83 | double map_offset_x; 84 | double map_offset_y; 85 | double map_len_x; 86 | double map_len_y; 87 | 88 | ros::NodeHandle node; 89 | 90 | std::string MAP_TOPIC; 91 | std::string MAP_FRAME; 92 | std::string METADATA_TOPIC; 93 | std::string REQUEST_MAP_SERVICE; 94 | std::string ROBOT_NAME; 95 | float MAP_PUB_FREQ; 96 | 97 | physics::WorldPtr world; 98 | 99 | event::ConnectionPtr update_connection; 100 | ros::Publisher map_pub; 101 | ros::Publisher meta_pub; 102 | 103 | ros::ServiceServer request_map_srv; 104 | 105 | ros::Timer publishTimer; 106 | 107 | bool worldChangedSinceLastAdvert; 108 | }; 109 | 110 | } 111 | #endif // GAZEBO_GAZEBOMAPPUBLISHER_H 112 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_state_plugins/include/gazebo_state_plugins/GazeboObjectInfo.h: -------------------------------------------------------------------------------- 1 | #ifndef GAZEBO_GAZEBOOBJECTINFO_H 2 | #define GAZEBO_GAZEBOOBJECTINFO_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | namespace gazebo 17 | { 18 | 19 | /** 20 | * A Gazebo *World* plugin which publishes information about objects in the gazebo world 21 | * via a ROS message (object_msgs::Object). Until now, only bounding box 22 | * representations of the objects are supported. 23 | * 24 | * Also provides a service to request information about an object directly. 25 | * 26 | * The plugin parameters can be specified in a YAML file, 27 | * which needs to be loaded onto the parameter server 28 | * under **namespace gazebo_state_plugins**, as follows: 29 | * 30 | * ``` 31 | * # Continuously publish info of gazebo objects? 32 | * publish_world_objects: true 33 | * 34 | * # the topic onto which to publish object info of 35 | * # type object_msgs/Object 36 | * world_objects_topic: "/gazebo_objects" 37 | * 38 | * # The topic onto which to publish the service 39 | * # which can be used to request object info 40 | * request_object_service: "/gazebo_objects/get_info" 41 | * 42 | * # frame of the objects poses that will be published. 43 | * # Should usually be 'world' 44 | * objects_frame_id: "world" 45 | * ``` 46 | * 47 | * \author Jennifer Buehler 48 | */ 49 | class GazeboObjectInfo : public WorldPlugin 50 | { 51 | public: 52 | typedef object_msgs::Object ObjectMsg; 53 | typedef object_msgs::ObjectInfo ObjectInfoMsg; 54 | 55 | GazeboObjectInfo(); 56 | 57 | void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf); 58 | 59 | private: 60 | 61 | bool requestObject(object_msgs::ObjectInfo::Request &req, object_msgs::ObjectInfo::Response &res); 62 | 63 | void advertEvent(const ros::TimerEvent& e); 64 | 65 | 66 | //publishes a model made up of its bounding boxes 67 | void onWorldUpdate(); 68 | 69 | shape_msgs::SolidPrimitive * getSolidPrimitive(physics::CollisionPtr& c); 70 | 71 | //returns a model made up of its bounding boxes 72 | ObjectMsg createBoundingBoxObject(physics::ModelPtr& model, bool include_shape); 73 | 74 | private: 75 | //ros::NodeHandle node; 76 | bool PUBLISH_OBJECTS; 77 | std::string WORLD_OBJECTS_TOPIC; 78 | std::string REQUEST_OBJECTS_TOPIC; 79 | std::string ROOT_FRAME_ID; 80 | 81 | physics::WorldPtr world; 82 | 83 | event::ConnectionPtr update_connection; 84 | ros::Publisher object_pub; 85 | 86 | ros::ServiceServer request_object_srv; 87 | 88 | ros::Timer publishTimer; 89 | 90 | std::vector lastGeneratedObjects; 91 | bool reGenerateObjects; 92 | }; 93 | 94 | } 95 | 96 | #endif // GAZEBO_GAZEBOOBJECTINFO_H 97 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_state_plugins/launch/plugin_loader.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_state_plugins/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | gazebo_state_plugins 4 | 1.0.2 5 | 6 | Offers topics and services related to the state of the world in Gazebo. 7 | 8 | 9 | 10 | Jennifer Buehler 11 | 12 | 13 | 14 | 15 | GPLv3 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | Jennifer Buehler 25 | 26 | 27 | 28 | catkin 29 | geometry_msgs 30 | roscpp 31 | gazebo_world_plugin_loader 32 | shape_msgs 33 | std_msgs 34 | nav_msgs 35 | object_msgs 36 | object_msgs_tools 37 | eigen_conversions 38 | gazebo_world_plugin_loader 39 | object_msgs 40 | object_msgs_tools 41 | eigen_conversions 42 | geometry_msgs 43 | roscpp 44 | shape_msgs 45 | std_msgs 46 | nav_msgs 47 | 48 | gazebo 49 | gazebo_ros 50 | gazebo_ros 51 | gazebo 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_state_plugins/test/object_info_request.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define DEFAULT_REQUEST_OBJECTS_TOPIC "world/request_object" 11 | 12 | int main (int argc, char **argv) 13 | { 14 | ros::init(argc, argv, "gazebo_object_info_client"); 15 | 16 | if (argc != 2) 17 | { 18 | ROS_INFO_STREAM("usage: "<"); 19 | return 1; 20 | } 21 | 22 | ros::NodeHandle n; 23 | std::string REQUEST_OBJECTS_TOPIC; 24 | 25 | n.param("gazebo_state_plugins/request_object_service", REQUEST_OBJECTS_TOPIC, DEFAULT_REQUEST_OBJECTS_TOPIC); 26 | ROS_INFO("Got objects topic name: <%s>", REQUEST_OBJECTS_TOPIC.c_str()); 27 | 28 | ros::ServiceClient client = n.serviceClient(REQUEST_OBJECTS_TOPIC); 29 | object_msgs::ObjectInfo srv; 30 | srv.request.name = argv[1]; 31 | srv.request.get_geometry=true; 32 | 33 | if (client.call(srv) && srv.response.success) 34 | { 35 | ROS_INFO("Result:"); 36 | std::cout< 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | 18 | namespace gazebo_test_tools 19 | { 20 | 21 | /** 22 | * Provides a service which publishes a object_msgs/Object, 23 | * after the a service request of type *gazebo_test_tools/RecognizeGazeboObject* is received. 24 | * 25 | * Note that the class gazebo_state_plugins/GazeboObjectInfo also offers the functionality 26 | * to continusously publish object_msgs/Object.msg, but it does so for *all* existing 27 | * objects (except the ones excluded at start-up). 28 | * With the FakeObjectRecognizer, it is possible to switch on/off the publishing 29 | * of object_msgs/Object information for specific objects only at various times. 30 | * Because of this, when you use FakeObjectRecogniser, you will need to make sure that 31 | * gazebo_state_plugins/GazeboObjectInfo is run with this parameter set to false: 32 | * 33 | * ``gazebo_state_plugins/publish_world_objects: false`` 34 | * 35 | * This is because in this case, you want the FakeObjectRecognizer to be the one to publish 36 | * the information instead of GazeboObjectInfo. 37 | * 38 | * \author Jennifer Buehler 39 | * \date February 2016 40 | **/ 41 | class FakeObjectRecognizer { 42 | private: 43 | 44 | typedef object_msgs::Object ObjectMsg; 45 | typedef object_msgs::ObjectInfo ObjectInfoMsg; 46 | 47 | public: 48 | 49 | FakeObjectRecognizer(); 50 | virtual ~FakeObjectRecognizer(); 51 | 52 | private: 53 | 54 | bool recognizeObject(gazebo_test_tools::RecognizeGazeboObject::Request &req, gazebo_test_tools::RecognizeGazeboObject::Response &res); 55 | 56 | void publishRecognitionEvent(const ros::TimerEvent& e); 57 | 58 | /** 59 | * Like queryObjectInfo but waits a maximum of \e timeout and re-requests 60 | * the information every \e checkStep seconds. 61 | */ 62 | bool waitForQueryObjectInfo(const std::string& name, object_msgs::Object& object, 63 | bool include_geometry, float timeout, float checkStep, bool printErrors); 64 | 65 | /** 66 | * Uses the object information service to request object information 67 | */ 68 | bool queryObjectInfo(const std::string& name, object_msgs::Object& object, 69 | bool include_geometry, bool printErrors); 70 | 71 | std::string OBJECTS_TOPIC; 72 | 73 | // service name/topic to request information about 74 | // objects with object_msgs/ObjectInfo.srv 75 | std::string SERVICE_REQUEST_OBJECT_TOPIC; 76 | 77 | // service which will be offered to recognise 78 | // an object, of type gazebo_test_tools/TriggerRecognition.srv. 79 | std::string SERVICE_RECOGNISE_OBJECT_TOPIC; 80 | 81 | 82 | // service of under which a object_msgs_tools/RegisterObject can 83 | // be sent in order to start publishing /tf information about an 84 | // object. 85 | std::string SERVICE_REGISTER_OBJECT_TF_TOPIC; 86 | 87 | // Recognised objects which are to be continuously published 88 | // as recognised are published at this rate. 89 | float PUBLISH_RECOGNISED_OBJECT_RATE; 90 | 91 | ros::Publisher object_pub; 92 | ros::ServiceClient object_info_client; 93 | ros::ServiceClient register_object_tf_client; 94 | 95 | ros::ServiceServer recognize_object_srv; 96 | 97 | // all objects which were set to be continuously published 98 | std::set addedObjects; 99 | 100 | // mutex for addedObjects 101 | boost::mutex addedObjectsMtx; 102 | 103 | ros::Timer publishTimer; 104 | 105 | ros::NodeHandle node; 106 | }; 107 | 108 | } // namespace gazebo_test_tools 109 | 110 | #endif // GAZEBO_TEST_TOOLS_FAKEOBJECTRECOGNISER_H 111 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_test_tools/include/gazebo_test_tools/gazebo_cube_spawner.h: -------------------------------------------------------------------------------- 1 | #ifndef GAZEBO_TEST_TOOLS_GAZEBOCUBESPAWNER 2 | #define GAZEBO_TEST_TOOLS_GAZEBOCUBESPAWNER 3 | 4 | #include 5 | 6 | namespace gazebo_test_tools { 7 | 8 | /** 9 | * Spawns a cube of given size, position and orientation into 10 | * Gazebo. 11 | * 12 | * \author Jennifer Buehler 13 | */ 14 | class GazeboCubeSpawner { 15 | public: 16 | GazeboCubeSpawner(ros::NodeHandle &n); 17 | 18 | void spawnCube(const std::string& name, const std::string& frame_id, 19 | float x, float y, float z, float qx, float qy, float qz, float qw, 20 | float w=0.05, float h=0.05, float d=0.05, float mass=0.05); 21 | 22 | /** 23 | * \param isCube if true, spawn a cube. If false, spawn cylinder, 24 | * where \e w is the radius and \e h is the height (\e d will be ignored). 25 | */ 26 | void spawnPrimitive(const std::string& name, const bool isCube, 27 | const std::string& frame_id, 28 | float x, float y, float z, float qx, float qy, float qz, float qw, 29 | float w=0.05, float h=0.05, float d=0.05, float mass=0.05); 30 | 31 | private: 32 | 33 | ros::NodeHandle nh; 34 | ros::ServiceClient spawn_object; 35 | 36 | }; 37 | 38 | } // namespace 39 | 40 | #endif // GAZEBO_TEST_TOOLS_GAZEBOCUBESPAWNER 41 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_test_tools/launch/fake_object_recognizer.launch: -------------------------------------------------------------------------------- 1 | 2 | # configuration file for the fake object recogniser 3 | 4 | 5 | # load ROS parameters from configuratoin file 6 | 7 | 8 | # name of the node 9 | 10 | 11 | ##################################### 12 | # Lauch the fake object recogniser node 13 | ##################################### 14 | 15 | 16 | ##################################### 17 | # Load the object tf publisher along 18 | # with the fake object detector so that 19 | # recognised objects also can 20 | # get published on tf 21 | ##################################### 22 | 23 | # use the default config file profided in this package 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_test_tools/launch/gazebo_fake_object_recognition.launch: -------------------------------------------------------------------------------- 1 | 2 | ##################################### 3 | # Load Gazebo world plugins 4 | ##################################### 5 | # Load parameters for Gazebo World Plugins to load 6 | 7 | 8 | ##################################### 9 | # Load Gazebo fake object detector 10 | ##################################### 11 | 12 | # use the default config file profided in this package 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_test_tools/launch/object_tf_broadcaster.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_test_tools/launch/spawn_and_recognize_cube.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_test_tools/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | gazebo_test_tools 4 | 1.0.2 5 | 6 | Tools to help testing and tutorials 7 | with Gazebo. For example, a helper which 8 | spawns a cube in cazebo. 9 | 10 | 11 | 12 | Jennifer Buehler 13 | 14 | 15 | 16 | 17 | GPSv3 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Jennifer Buehler 27 | 28 | 29 | 30 | 31 | catkin 32 | gazebo_ros 33 | gazebo 34 | geometry_msgs 35 | roscpp 36 | std_msgs 37 | object_msgs 38 | message_generation 39 | gazebo_ros 40 | gazebo 41 | geometry_msgs 42 | roscpp 43 | std_msgs 44 | object_msgs 45 | 46 | message_runtime 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_test_tools/src/cube_spawner_node.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(int argc, char** argv) { 5 | ros::init(argc, argv, "gazebo_cube_spawner"); 6 | 7 | if (argc < 5) 8 | { 9 | ROS_INFO("Usage: %s [x y z] [frame_id].",argv[0]); 10 | return 0; 11 | } 12 | 13 | ros::NodeHandle node; 14 | gazebo_test_tools::GazeboCubeSpawner spawner(node); 15 | 16 | ROS_INFO("Running spawn cube once.."); 17 | 18 | std::string name=argv[1]; 19 | float x=0; 20 | float y=0; 21 | float z=0; 22 | 23 | std::string frame_id="world"; 24 | if (argc>2) x=atof(argv[2]); 25 | if (argc>3) y=atof(argv[3]); 26 | if (argc>4) z=atof(argv[4]); 27 | if (argc>5) frame_id=argv[5]; 28 | 29 | float dim=0.05; 30 | float mass=0.05; 31 | 32 | spawner.spawnCube(name,frame_id,x,y,z,0,0,0,1,dim,dim,dim,mass); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_test_tools/src/fake_object_recognizer_node.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char** argv) 5 | { 6 | ros::init(argc, argv, "fake_object_recognizer"); 7 | gazebo_test_tools::FakeObjectRecognizer recognizer; 8 | ros::spin(); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_test_tools/srv/RecognizeGazeboObject.srv: -------------------------------------------------------------------------------- 1 | # name of the object in Gazebo which is to be detected 2 | # and published as object_msgs/Object.msg 3 | string name 4 | 5 | # If true, continuously re-publish the current 6 | # state of the object (continuously "re-recognize"). 7 | # If false, and the object was previously set to 8 | # republish, this will stop the re-publishing. 9 | bool republish 10 | 11 | --- 12 | 13 | # If true, found the object in Gazebo and published the message. 14 | bool success 15 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_test_tools/test/fake_object_recognizer_cmd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define DEFAULT_RECOGNIZE_OBJECT_TOPIC "/gazebo/recognize_object" 5 | 6 | int main(int argc, char** argv) 7 | { 8 | ros::init(argc, argv, "fake_object_recognizer_cmd"); 9 | 10 | if (argc != 3) 11 | { 12 | ROS_INFO_STREAM("usage: "< "); 13 | return 1; 14 | } 15 | 16 | ros::NodeHandle n; 17 | std::string RECOGNIZE_OBJECT_TOPIC; 18 | 19 | n.param("gazebo_test_tools/recognize_object_service", RECOGNIZE_OBJECT_TOPIC, DEFAULT_RECOGNIZE_OBJECT_TOPIC); 20 | ROS_INFO("Got recognize object service topic name: <%s>", RECOGNIZE_OBJECT_TOPIC.c_str()); 21 | 22 | ros::ServiceClient client = n.serviceClient(RECOGNIZE_OBJECT_TOPIC); 23 | gazebo_test_tools::RecognizeGazeboObject srv; 24 | srv.request.name = argv[1]; 25 | srv.request.republish = atoi(argv[2]); 26 | 27 | if (client.call(srv)) 28 | { 29 | ROS_INFO("Result:"); 30 | std::cout< 13 | # file: lib.so 14 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_world_plugin_loader/include/gazebo_world_plugin_loader/GazeboPluginLoader.h: -------------------------------------------------------------------------------- 1 | #ifndef GAZEBO_GAZEBOPLUGINLOADER_H 2 | #define GAZEBO_GAZEBOPLUGINLOADER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace gazebo 9 | { 10 | 11 | /** 12 | * System plugin which can be used to load World plugins 13 | * dynamically, without the need of having to put them in 14 | * the Gazebo world file. 15 | * 16 | * The plugins to be loaded can be specified in a YAML file, 17 | * which needs to be loaded onto the parameter server 18 | * under **namespace gazebo_state_plugins**, as follows: 19 | * 20 | * ``` 21 | * world_name: "default" 22 | * world_plugins: 23 | * - name: gazebo_object_info 24 | * file: libgazebo_object_info.so 25 | * - name: gazebo_map_publisher 26 | * file: libgazebo_map_publisher.so 27 | * ``` 28 | * 29 | * **Usage:** 30 | * 31 | * This plugin is a system plugin which has to be loaded 32 | * together with gazebo, e.g.: 33 | * 34 | * ``gzserver -s libgazebo_world_plugin_loader.so`` 35 | * 36 | * **Limitation:** 37 | * 38 | * The method 39 | * *WorldPlugin::Load(physics::WorldPtr, sdf::ElementPtr)* 40 | * on the plugins will be called with a NULL sdf::ElementPtr. 41 | * So the plugins should alternatively support reading their 42 | * parameters from the ROS parameter server. 43 | * 44 | * \author Jennifer Buehler 45 | */ 46 | class GazeboPluginLoader : public SystemPlugin 47 | { 48 | public: 49 | GazeboPluginLoader(); 50 | 51 | void Load(int argc, char ** argv); 52 | 53 | private: 54 | void onWorldCreate(); 55 | event::ConnectionPtr update_connection; 56 | }; 57 | 58 | } 59 | #endif // GAZEBO_GAZEBOPLUGINLOADER_H 60 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_world_plugin_loader/launch/plugin_loader_template.launch: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_world_plugin_loader/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | gazebo_world_plugin_loader 4 | 1.0.2 5 | 6 | Offers a *system* plugin which can be 7 | used to load other *world* plugins after the world has been 8 | loaded, so it doesn't have to be specified in the Gazebo "world" file. 9 | 10 | 11 | 12 | Jennifer Buehler 13 | 14 | 15 | 16 | 17 | GPLv3 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Jennifer Buehler 27 | 28 | 29 | 30 | catkin 31 | roscpp 32 | gazebo_ros 33 | gazebo 34 | 35 | roscpp 36 | gazebo_ros 37 | gazebo 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /gazebo/gazebo-pkgs/gazebo_world_plugin_loader/src/GazeboPluginLoader.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | using gazebo::GazeboPluginLoader; 7 | 8 | GZ_REGISTER_SYSTEM_PLUGIN(GazeboPluginLoader) 9 | 10 | GazeboPluginLoader::GazeboPluginLoader() : 11 | SystemPlugin() 12 | { 13 | update_connection = event::Events::ConnectWorldCreated(boost::bind(&GazeboPluginLoader::onWorldCreate, this)); 14 | } 15 | 16 | void GazeboPluginLoader::Load(int argc, char ** argv){ 17 | } 18 | 19 | void GazeboPluginLoader::onWorldCreate(){ 20 | ROS_INFO("On-World-Create: Reading list of Gazebo world plugins to be loaded from parameters..."); 21 | 22 | ros::NodeHandle node("/gazebo_state_plugins"); 23 | 24 | ROS_INFO_STREAM("Reading parameters from namespace "<LoadPlugin(file, name, _sdf); 57 | } 58 | 59 | ROS_INFO("Eligible plugins loaded."); 60 | } 61 | -------------------------------------------------------------------------------- /gazebo/meshes/kr210l150/README.md: -------------------------------------------------------------------------------- 1 | Mesh files present in this folder were obtained from: 2 | https://github.com/ros-industrial/kuka_experimental 3 | -------------------------------------------------------------------------------- /gazebo/meshes/kr210l150/collision/base_link.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/gazebo/meshes/kr210l150/collision/base_link.stl -------------------------------------------------------------------------------- /gazebo/meshes/kr210l150/collision/link_1.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/gazebo/meshes/kr210l150/collision/link_1.stl -------------------------------------------------------------------------------- /gazebo/meshes/kr210l150/collision/link_2.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/gazebo/meshes/kr210l150/collision/link_2.stl -------------------------------------------------------------------------------- /gazebo/meshes/kr210l150/collision/link_3.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/gazebo/meshes/kr210l150/collision/link_3.stl -------------------------------------------------------------------------------- /gazebo/meshes/kr210l150/collision/link_4.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/gazebo/meshes/kr210l150/collision/link_4.stl -------------------------------------------------------------------------------- /gazebo/meshes/kr210l150/collision/link_5.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/gazebo/meshes/kr210l150/collision/link_5.stl -------------------------------------------------------------------------------- /gazebo/meshes/kr210l150/collision/link_6.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/gazebo/meshes/kr210l150/collision/link_6.stl -------------------------------------------------------------------------------- /lua/NEON.c: -------------------------------------------------------------------------------- 1 | static void THFloatVector_fill_NEON(float *x, const float c, const ptrdiff_t n) { 2 | long i = 0; 3 | 4 | for(; i < n-4; i += 4) 5 | { 6 | x[i] = c; 7 | x[i+1] = c; 8 | x[i+2] = c; 9 | x[i+3] = c; 10 | } 11 | 12 | for(; i < n; i++) 13 | x[i] = c; 14 | 15 | } 16 | 17 | 18 | static void THFloatVector_diff_NEON(float *z, const float *x, const float *y, const ptrdiff_t n) { 19 | long i = 0; 20 | 21 | for(; i < n-4; i += 4) 22 | { 23 | z[i] = x[i] - y[i]; 24 | z[i+1] = x[i+1] - y[i+1]; 25 | z[i+2] = x[i+2] - y[i+2]; 26 | z[i+3] = x[i+3] - y[i+3]; 27 | } 28 | 29 | for(; i < n; i++) 30 | z[i] = x[i] - y[i]; 31 | 32 | } 33 | 34 | 35 | static void THFloatVector_scale_NEON(float *y, const float c, const ptrdiff_t n) { 36 | long i = 0; 37 | 38 | for(; i < n-4; i +=4) 39 | { 40 | y[i] *= c; 41 | y[i+1] *= c; 42 | y[i+2] *= c; 43 | y[i+3] *= c; 44 | } 45 | 46 | for(; i < n; i++) 47 | y[i] *= c; 48 | } 49 | 50 | static void THFloatVector_mul_NEON(float *y, const float *x, const ptrdiff_t n) { 51 | long i = 0; 52 | 53 | for(; i < n-4; i += 4) 54 | { 55 | y[i] *= x[i]; 56 | y[i+1] *= x[i+1]; 57 | y[i+2] *= x[i+2]; 58 | y[i+3] *= x[i+3]; 59 | } 60 | 61 | for(; i < n; i++) 62 | y[i] *= x[i]; 63 | } 64 | 65 | static void THFloatVector_add_NEON(float *y, const float *x, const float c, const ptrdiff_t n) { 66 | long i = 0; 67 | 68 | for(;i < n-4; i += 4) 69 | { 70 | y[i] += c * x[i]; 71 | y[i+1] += c * x[i+1]; 72 | y[i+2] += c * x[i+2]; 73 | y[i+3] += c * x[i+3]; 74 | } 75 | 76 | for(; i < n; i++) 77 | y[i] += c * x[i]; 78 | } 79 | -------------------------------------------------------------------------------- /lua/catchENV.lua: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/lua/catchENV.lua -------------------------------------------------------------------------------- /lua/hello.lua: -------------------------------------------------------------------------------- 1 | 2 | -- this is a one-line comment, beginning after the double-dash 3 | print('HELLO from LUA!') 4 | 5 | --[[ this is 6 | a multi-line 7 | block comment --]] 8 | 9 | -- variables 10 | myVar = 16.0 -- variables are floating-point doubles 11 | local myVar2 = 32.0 12 | 13 | print('my variable equals ' .. myVar) 14 | 15 | 16 | -- tables 17 | list = {1, 2, 3} 18 | map = {x = 10, y = 20, z = 30} 19 | 20 | print('list ' .. list[1]) -- indexing starts with 1 21 | print('map.x ' .. map.x) 22 | 23 | 24 | -- loops and branches 25 | for i=1,10 do 26 | if i == 1 then 27 | print("one") 28 | elseif i == 2 then 29 | print("two") 30 | else 31 | print(i) 32 | end 33 | end 34 | 35 | 36 | -- functions 37 | function multiply(a, b) 38 | return a * b 39 | end 40 | 41 | multiplyResult = multiply(map.x, map.y) 42 | 43 | print('multiply = ' .. multiplyResult) 44 | 45 | 46 | -- we're done here 47 | print('goodbye!') 48 | 49 | 50 | -------------------------------------------------------------------------------- /lua/test-cutorch.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- deepRL - environment verification script 3 | -- 4 | 5 | print('[deepRL] hello from within Torch/Lua environment (time=' .. os.clock() .. ')') 6 | print('[deepRL] loading Lua packages...') 7 | 8 | print('[deepRL] loading torch...') 9 | require 'torch' 10 | 11 | print('[deepRL] loading nn...') 12 | require 'nn' 13 | 14 | print('[deepRL] loading cutorch...') 15 | require 'cutorch' 16 | 17 | print('cutorch.hasHalf == ' .. tostring(cutorch.hasHalf) ) 18 | 19 | if cutorch.hasHalf then 20 | print('half tensor test...') 21 | a = torch.CudaHalfTensor(3) 22 | b = torch.CudaHalfTensor(3) 23 | print(torch.cmul(a,b)) 24 | end 25 | 26 | print('[deepRL] loading cudnn...') 27 | require 'cudnn' 28 | 29 | print('[deepRL] done loading packages. (time=' .. os.clock() .. ')') 30 | 31 | print('[deepRL] engaging torch test. (time=' .. os.clock() .. ')') 32 | torch.test() 33 | print('[deepRL] engaging torch test. (time=' .. os.clock() .. ')') 34 | 35 | print('[deepRL] engaging nn test. (time=' .. os.clock() .. ')') 36 | nn.test() 37 | print('[deepRL] engaging nn test. (time=' .. os.clock() .. ')') 38 | 39 | print('[deepRL] engaging cutorch test. (time=' .. os.clock() .. ')') 40 | cutorch.test() 41 | print('[deepRL] finished cutorch test. (time=' .. os.clock() .. ')') 42 | 43 | -------------------------------------------------------------------------------- /lua/test-lua.lua: -------------------------------------------------------------------------------- 1 | 2 | -- this is a one-line comment, beginning after the double-dash 3 | print('testing LUAJIT51 scripting language') 4 | 5 | --[[ this is 6 | a multi-line 7 | block comment --]] 8 | 9 | -- variables 10 | myVar = 16.0 -- variables are floating-point doubles 11 | local myVar2 = 32.0 12 | 13 | print('my variable equals ' .. myVar) 14 | 15 | 16 | -- tables 17 | list = {1, 2, 3} 18 | map = {x = 10, y = 20, z = 30} 19 | 20 | print('list ' .. list[1]) -- indexing starts with 1 21 | print('map.x ' .. map.x) 22 | 23 | 24 | -- loops and branches 25 | for i=1,10 do 26 | if i == 1 then 27 | print("one") 28 | elseif i == 2 then 29 | print("two") 30 | else 31 | print(i) 32 | end 33 | end 34 | 35 | 36 | -- functions 37 | function multiply(a, b) 38 | return a * b 39 | end 40 | 41 | multiplyResult = multiply(map.x, map.y) 42 | 43 | print('multiply = ' .. multiplyResult) 44 | 45 | 46 | -- we're done here 47 | print('goodbye!') 48 | 49 | 50 | -------------------------------------------------------------------------------- /lua/test-packages.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- deepRL - environment verification script 3 | -- 4 | 5 | print('[deepRL] hello from within Torch/Lua environment (time=' .. os.clock() .. ')') 6 | print('[deepRL] loading Lua packages...') 7 | 8 | print('[deepRL] loading torch...') 9 | require 'torch' 10 | 11 | print('[deepRL] loading cutorch...') 12 | require 'cutorch' 13 | 14 | print('cutorch.hasHalf == ' .. tostring(cutorch.hasHalf) ) 15 | 16 | if cutorch.hasHalf then 17 | print('half tensor test...') 18 | a = torch.CudaHalfTensor(3) 19 | b = torch.CudaHalfTensor(3) 20 | print(torch.cmul(a,b)) 21 | end 22 | 23 | print('[deepRL] loading nn...') 24 | require 'nn' 25 | 26 | print('[deepRL] loading cudnn...') 27 | require 'cudnn' 28 | 29 | print('[deepRL] loading math...') 30 | require 'math' 31 | 32 | print('[deepRL] loading nnx...') 33 | require 'nnx' 34 | 35 | print('[deepRL] loading optim...') 36 | require 'optim' 37 | 38 | print('[deepRL] done loading packages. (time=' .. os.clock() .. ')') 39 | -------------------------------------------------------------------------------- /lua/test-torch.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- deepRL - environment verification script 3 | -- 4 | 5 | print('[deepRL] hello from within Torch/Lua environment (time=' .. os.clock() .. ')') 6 | print('[deepRL] loading Lua packages...') 7 | 8 | print('[deepRL] loading torch...') 9 | require 'torch' 10 | 11 | print('[deepRL] loading nn...') 12 | require 'nn' 13 | 14 | print('[deepRL] done loading packages. (time=' .. os.clock() .. ')') 15 | 16 | print('[deepRL] engaging torch test. (time=' .. os.clock() .. ')') 17 | torch.test() 18 | print('[deepRL] engaging torch test. (time=' .. os.clock() .. ')') 19 | 20 | print('[deepRL] engaging nn test. (time=' .. os.clock() .. ')') 21 | nn.test() 22 | print('[deepRL] engaging nn test. (time=' .. os.clock() .. ')') 23 | 24 | 25 | -------------------------------------------------------------------------------- /python/CRNN.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | 3 | 4 | class BidirectionalLSTM(nn.Module): 5 | 6 | def __init__(self, nIn, nHidden, nOut): 7 | super(BidirectionalLSTM, self).__init__() 8 | 9 | self.rnn = nn.LSTM(nIn, nHidden, bidirectional=True) 10 | self.embedding = nn.Linear(nHidden * 2, nOut) 11 | 12 | def forward(self, input): 13 | recurrent, _ = self.rnn(input) 14 | T, b, h = recurrent.size() 15 | t_rec = recurrent.view(T * b, h) 16 | 17 | output = self.embedding(t_rec) # [T * b, nOut] 18 | output = output.view(T, b, -1) 19 | 20 | return output 21 | 22 | 23 | class CRNN(nn.Module): 24 | 25 | def __init__(self, imgH, nc, nclass, nh, n_rnn=2, leakyRelu=False): 26 | super(CRNN, self).__init__() 27 | assert imgH % 16 == 0, 'imgH has to be a multiple of 16' 28 | 29 | ks = [3, 3, 3, 3, 3, 3, 2] 30 | ps = [1, 1, 1, 1, 1, 1, 0] 31 | ss = [1, 1, 1, 1, 1, 1, 1] 32 | nm = [64, 128, 256, 256, 512, 512, 512] 33 | 34 | cnn = nn.Sequential() 35 | 36 | def convRelu(i, batchNormalization=False): 37 | nIn = nc if i == 0 else nm[i - 1] 38 | nOut = nm[i] 39 | cnn.add_module('conv{0}'.format(i), 40 | nn.Conv2d(nIn, nOut, ks[i], ss[i], ps[i])) 41 | if batchNormalization: 42 | cnn.add_module('batchnorm{0}'.format(i), nn.BatchNorm2d(nOut)) 43 | if leakyRelu: 44 | cnn.add_module('relu{0}'.format(i), 45 | nn.LeakyReLU(0.2, inplace=True)) 46 | else: 47 | cnn.add_module('relu{0}'.format(i), nn.ReLU(True)) 48 | 49 | convRelu(0) 50 | cnn.add_module('pooling{0}'.format(0), nn.MaxPool2d(2, 2)) # 64x16x64 51 | convRelu(1) 52 | cnn.add_module('pooling{0}'.format(1), nn.MaxPool2d(2, 2)) # 128x8x32 53 | convRelu(2, True) 54 | convRelu(3) 55 | cnn.add_module('pooling{0}'.format(2), 56 | nn.MaxPool2d((2, 2), (2, 1), (0, 1))) # 256x4x16 57 | convRelu(4, True) 58 | convRelu(5) 59 | cnn.add_module('pooling{0}'.format(3), 60 | nn.MaxPool2d((2, 2), (2, 1), (0, 1))) # 512x2x16 61 | convRelu(6, True) # 512x1x16 62 | 63 | self.cnn = cnn 64 | self.rnn = nn.Sequential( 65 | BidirectionalLSTM(512, nh, nh), 66 | BidirectionalLSTM(nh, nh, nclass)) 67 | 68 | def forward(self, input): 69 | # conv features 70 | conv = self.cnn(input) 71 | b, c, h, w = conv.size() 72 | print('1 conv.size() = ' + str(conv.size())) 73 | #assert h == 1, "the height of conv must be 1" 74 | #conv = conv.squeeze(2) 75 | #conv = conv.permute(2, 0, 1) # [w, b, c] 76 | conv = conv.squeeze() 77 | print('2 conv.size() = ' + str(conv.size())) 78 | conv = conv.permute(2, 1, 0) 79 | print('3 conv.size() = ' + str(conv.size())) 80 | 81 | # rnn features 82 | output = self.rnn(conv) 83 | 84 | return output 85 | -------------------------------------------------------------------------------- /python/gym-RL.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import gym 3 | import numpy as np 4 | from itertools import count 5 | 6 | import torch 7 | import torch.nn as nn 8 | import torch.nn.functional as F 9 | import torch.optim as optim 10 | import torch.autograd as autograd 11 | from torch.autograd import Variable 12 | 13 | 14 | parser = argparse.ArgumentParser(description='PyTorch REINFORCE example') 15 | parser.add_argument('--env', metavar='N', default='CartPole-v0') 16 | parser.add_argument('--gamma', type=float, default=0.99, metavar='G', 17 | help='discount factor (default: 0.99)') 18 | parser.add_argument('--seed', type=int, default=543, metavar='N', 19 | help='random seed (default: 543)') 20 | parser.add_argument('--render', action='store_true', 21 | help='render the environment') 22 | parser.add_argument('--log-interval', type=int, default=10, metavar='N', 23 | help='interval between training status logs (default: 10)') 24 | args = parser.parse_args() 25 | 26 | # print(gym.envs.registry.all()) 27 | 28 | # if gpu is to be used 29 | use_cuda = torch.cuda.is_available() 30 | FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor 31 | LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor 32 | ByteTensor = torch.cuda.ByteTensor if use_cuda else torch.ByteTensor 33 | Tensor = FloatTensor 34 | 35 | print('use_cuda: ' + str(use_cuda)) 36 | print('use_env: ' + args.env) 37 | env = gym.make(args.env) 38 | env.seed(args.seed) 39 | torch.manual_seed(args.seed) 40 | 41 | num_actions = env.action_space.n 42 | print('num actions: ' + str(num_actions)) 43 | 44 | print('observation space: ' + str(env.observation_space.shape)) 45 | print(env.observation_space.low) 46 | print(env.observation_space.high) 47 | observation_dim = env.observation_space.shape[0] 48 | print('observation dimensions: ' + str(observation_dim)) 49 | 50 | 51 | class Policy(nn.Module): 52 | def __init__(self): 53 | super(Policy, self).__init__() 54 | self.affine1 = nn.Linear(observation_dim, 128) 55 | self.affine2 = nn.Linear(128, num_actions) 56 | 57 | self.saved_actions = [] 58 | self.rewards = [] 59 | 60 | def forward(self, x): 61 | x = F.relu(self.affine1(x)) 62 | action_scores = self.affine2(x) 63 | return F.softmax(action_scores) 64 | 65 | 66 | policy = Policy() 67 | 68 | if use_cuda: 69 | print('detected CUDA support') 70 | policy.cuda() 71 | 72 | optimizer = optim.Adam(policy.parameters(), lr=1e-2) 73 | 74 | 75 | def select_action(state): 76 | state = torch.from_numpy(state).float().unsqueeze(0) 77 | state = state.cuda() 78 | probs = policy(Variable(state)) 79 | action = probs.multinomial() 80 | policy.saved_actions.append(action) 81 | return action.data 82 | 83 | 84 | def finish_episode(): 85 | R = 0 86 | rewards = [] 87 | for r in policy.rewards[::-1]: 88 | R = r + args.gamma * R 89 | rewards.insert(0, R) 90 | rewards = torch.Tensor(rewards) 91 | rewards = (rewards - rewards.mean()) / (rewards.std() + np.finfo(np.float32).eps) 92 | for action, r in zip(policy.saved_actions, rewards): 93 | action.reinforce(r) 94 | optimizer.zero_grad() 95 | autograd.backward(policy.saved_actions, [None for _ in policy.saved_actions]) 96 | optimizer.step() 97 | del policy.rewards[:] 98 | del policy.saved_actions[:] 99 | 100 | 101 | running_reward = 10 102 | for i_episode in count(1): 103 | state = env.reset() 104 | for t in range(10000): # Don't infinite loop while learning 105 | action = select_action(state) 106 | state, reward, done, _ = env.step(action[0,0]) 107 | if args.render: 108 | env.render() 109 | policy.rewards.append(reward) 110 | if done: 111 | break 112 | 113 | running_reward = running_reward * 0.99 + t * 0.01 114 | finish_episode() 115 | if i_episode % args.log_interval == 0: 116 | print('Episode {}\tLast length: {:5d}\tAverage length: {:.2f}'.format( 117 | i_episode, t, running_reward)) 118 | if running_reward > 200: 119 | print("Solved! Running reward is now {} and " 120 | "the last episode runs to {} time steps!".format(running_reward, t)) 121 | break 122 | -------------------------------------------------------------------------------- /python/gym-actorCritic.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import gym 3 | import numpy as np 4 | from itertools import count 5 | from collections import namedtuple 6 | 7 | import torch 8 | import torch.nn as nn 9 | import torch.nn.functional as F 10 | import torch.optim as optim 11 | import torch.autograd as autograd 12 | from torch.autograd import Variable 13 | 14 | 15 | parser = argparse.ArgumentParser(description='PyTorch actor-critic example') 16 | parser.add_argument('--env', metavar='N', default='CartPole-v0') 17 | parser.add_argument('--gamma', type=float, default=0.99, metavar='G', 18 | help='discount factor (default: 0.99)') 19 | parser.add_argument('--seed', type=int, default=543, metavar='N', 20 | help='random seed (default: 1)') 21 | parser.add_argument('--render', action='store_true', 22 | help='render the environment') 23 | parser.add_argument('--log-interval', type=int, default=10, metavar='N', 24 | help='interval between training status logs (default: 10)') 25 | args = parser.parse_args() 26 | 27 | 28 | # if gpu is to be used 29 | use_cuda = torch.cuda.is_available() 30 | FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor 31 | LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor 32 | ByteTensor = torch.cuda.ByteTensor if use_cuda else torch.ByteTensor 33 | Tensor = FloatTensor 34 | 35 | print('use_cuda: ' + str(use_cuda)) 36 | print('use_env: ' + args.env) 37 | env = gym.make(args.env) 38 | env.seed(args.seed) 39 | torch.manual_seed(args.seed) 40 | 41 | print('action_space ' + str(env.action_space)) 42 | num_actions = env.action_space.n 43 | print('num actions: ' + str(num_actions)) 44 | 45 | print('observation space: ' + str(env.observation_space.shape)) 46 | print(env.observation_space.low) 47 | print(env.observation_space.high) 48 | observation_dim = env.observation_space.shape[0] 49 | print('observation dimensions: ' + str(observation_dim)) 50 | 51 | 52 | SavedAction = namedtuple('SavedAction', ['action', 'value']) 53 | class Policy(nn.Module): 54 | def __init__(self): 55 | super(Policy, self).__init__() 56 | self.affine1 = nn.Linear(observation_dim, 128) 57 | self.action_head = nn.Linear(128, num_actions) 58 | self.value_head = nn.Linear(128, 1) 59 | 60 | self.saved_actions = [] 61 | self.rewards = [] 62 | 63 | def forward(self, x): 64 | x = F.relu(self.affine1(x)) 65 | action_scores = self.action_head(x) 66 | state_values = self.value_head(x) 67 | return F.softmax(action_scores), state_values 68 | 69 | 70 | model = Policy() 71 | 72 | if use_cuda: 73 | model.cuda() 74 | 75 | optimizer = optim.Adam(model.parameters(), lr=3e-2) 76 | 77 | 78 | def select_action(state): 79 | state = torch.from_numpy(state).float().unsqueeze(0) 80 | state = state.cuda() 81 | probs, state_value = model(Variable(state)) 82 | action = probs.multinomial() 83 | model.saved_actions.append(SavedAction(action, state_value)) 84 | return action.data 85 | 86 | 87 | def finish_episode(): 88 | R = 0 89 | saved_actions = model.saved_actions 90 | value_loss = 0 91 | rewards = [] 92 | for r in model.rewards[::-1]: 93 | R = r + args.gamma * R 94 | rewards.insert(0, R) 95 | rewards = torch.Tensor(rewards) 96 | rewards = (rewards - rewards.mean()) / (rewards.std() + np.finfo(np.float32).eps) 97 | for (action, value), r in zip(saved_actions, rewards): 98 | reward = r - value.data[0,0] 99 | action.reinforce(reward) 100 | value_loss += F.smooth_l1_loss(value, Variable(torch.Tensor([r]).cuda())) 101 | optimizer.zero_grad() 102 | final_nodes = [value_loss] + list(map(lambda p: p.action, saved_actions)) 103 | gradients = [torch.ones(1).cuda()] + [None] * len(saved_actions) 104 | autograd.backward(final_nodes, gradients) 105 | optimizer.step() 106 | del model.rewards[:] 107 | del model.saved_actions[:] 108 | 109 | 110 | running_reward = 10 111 | for i_episode in count(1): 112 | state = env.reset() 113 | for t in range(30000): # Don't infinite loop while learning 114 | action = select_action(state) 115 | state, reward, done, _ = env.step(action[0,0]) 116 | if args.render: 117 | env.render() 118 | model.rewards.append(reward) 119 | if done: 120 | break 121 | 122 | running_reward = running_reward * 0.99 + t * 0.01 123 | finish_episode() 124 | if i_episode % args.log_interval == 0: 125 | print('Episode {}\tLast length: {:5d}\tAverage length: {:.2f}'.format( 126 | i_episode, t, running_reward)) 127 | if running_reward > 200: 128 | print("Solved! Running reward is now {} and " 129 | "the last episode runs to {} time steps!".format(running_reward, t)) 130 | break 131 | -------------------------------------------------------------------------------- /python/test-interop.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | print('hello from test-interop.py script') 4 | 5 | import argparse 6 | parser = argparse.ArgumentParser(description='test-interop') 7 | parser.add_argument('--foo', action='store_true',help='foo flag') 8 | parser.add_argument('--bar', action='store_true',help='bar flag') 9 | args = parser.parse_args() 10 | 11 | print('foo: ' + str(args.foo)) 12 | print('bar: ' + str(args.bar)) 13 | 14 | 15 | def add( x, y ): 16 | print('inside function add()') 17 | return x + y 18 | 19 | def mul( x, y ): 20 | print('inside function mul()') 21 | return x * y 22 | -------------------------------------------------------------------------------- /python/test-torch.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | print('hello from test-torch.py script') 4 | print('importing torch...') 5 | 6 | import torch 7 | 8 | print('import complete.') 9 | print('cuda available: ' + str(torch.cuda.is_available())) 10 | print('testing basic tensor operations') 11 | 12 | a = torch.cuda.FloatTensor(2).zero_() 13 | print(a) 14 | b = torch.randn(2).cuda() 15 | print(b) 16 | c = a + b 17 | print(c) 18 | 19 | 20 | def foo( x ): 21 | print('tensor interop test') 22 | print(x) 23 | #return x.dim() 24 | #return x.size()[0] 25 | #return x.sum() 26 | return x.std() 27 | 28 | 29 | -------------------------------------------------------------------------------- /samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | add_subdirectory(catch) 3 | add_subdirectory(fruit) -------------------------------------------------------------------------------- /samples/catch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | file(GLOB catchSources *.cpp) 4 | file(GLOB catchIncludes *.h ) 5 | 6 | add_executable(catch ${catchSources}) 7 | target_link_libraries(catch jetson-reinforcement) 8 | -------------------------------------------------------------------------------- /samples/fruit/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | file(GLOB fruitSources *.cpp) 4 | file(GLOB fruitIncludes *.h ) 5 | 6 | add_executable(fruit ${fruitSources}) 7 | target_link_libraries(fruit jetson-reinforcement) 8 | -------------------------------------------------------------------------------- /tools/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | add_subdirectory(console) 3 | #add_subdirectory(diagnostic) 4 | add_subdirectory(input) 5 | -------------------------------------------------------------------------------- /tools/console/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | file(GLOB consoleSources *.cpp) 3 | file(GLOB consoleIncludes *.h ) 4 | 5 | add_executable(deepRL-console ${consoleSources}) 6 | target_link_libraries(deepRL-console jetson-reinforcement) 7 | -------------------------------------------------------------------------------- /tools/console/main.lua.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * deepRL 3 | */ 4 | 5 | #include 6 | 7 | #ifdef USE_LUA 8 | 9 | extern "C" 10 | { 11 | #include 12 | #include 13 | #include 14 | } 15 | 16 | #include 17 | #include 18 | 19 | 20 | 21 | bool init( const char* script_filename ) 22 | { 23 | lua_State* L = luaL_newstate(); 24 | 25 | if( !L ) 26 | { 27 | printf("[deepRL] failed to create lua_State (luaL_newstate returned NULL)\n"); 28 | return false; 29 | } 30 | 31 | printf("[deepRL] created new lua_State\n"); 32 | 33 | luaL_openlibs(L); 34 | printf("[deepRL] opened LUA libraries\n"); 35 | printf("[deepRL] loading '%s'\n\n", script_filename); 36 | 37 | // load and run file 38 | const int res = luaL_dofile(L, script_filename); 39 | 40 | if( res == 1 ) 41 | { 42 | printf("Error executing resource: %s\n", script_filename); 43 | const char* luastr = lua_tostring(L,-1); 44 | 45 | if( luastr != NULL ) 46 | printf("%s\n", luastr); 47 | } 48 | 49 | printf("\n[deepRL] closing lua_State\n"); 50 | lua_close(L); 51 | 52 | return true; 53 | } 54 | 55 | 56 | int main( int argc, char** argv ) 57 | { 58 | printf("deepRL-console (lua)\n\n"); 59 | 60 | const char* script_filename = "test-packages.lua"; 61 | 62 | if( argc > 1 ) 63 | script_filename = argv[1]; 64 | 65 | if( !init(script_filename) ) 66 | { 67 | printf("failed to run lua script '%s', exiting deepRL-console\n", script_filename); 68 | return 0; 69 | } 70 | 71 | return 0; 72 | } 73 | 74 | #endif 75 | 76 | -------------------------------------------------------------------------------- /tools/console/main.py.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * deepRL 3 | */ 4 | 5 | #ifdef USE_PYTHON 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | int main( int argc, char** argv ) 15 | { 16 | PyObject *pName, *pModule, *pDict, *pFunc; 17 | PyObject *pArgs, *pValue; 18 | 19 | printf("deepRL-console (python)\n"); 20 | 21 | if (argc < 3) { 22 | fprintf(stderr,"Usage: call pythonfile funcname [args]\n"); 23 | return 1; 24 | } 25 | 26 | Py_Initialize(); 27 | 28 | // setup arguments to python script 29 | int py_argc = 3; 30 | char* py_argv[3]; 31 | 32 | py_argv[0] = argv[1]; 33 | py_argv[1] = "--foo"; 34 | py_argv[2] = "--bar"; 35 | 36 | PySys_SetArgv(py_argc, py_argv); 37 | //PySys_SetArgv(argc, argv); 38 | 39 | pName = PyString_FromString(argv[1]); 40 | 41 | // load the script 42 | pModule = PyImport_Import(pName); 43 | Py_DECREF(pName); 44 | 45 | if (pModule != NULL) 46 | { 47 | pFunc = PyObject_GetAttrString(pModule, argv[2]); 48 | /* pFunc is a new reference */ 49 | 50 | if (pFunc && PyCallable_Check(pFunc)) 51 | { 52 | pArgs = PyTuple_New(argc - 3); 53 | 54 | for (int i = 0; i < argc - 3; ++i) 55 | { 56 | pValue = PyInt_FromLong(atoi(argv[i + 3])); 57 | 58 | if (!pValue) 59 | { 60 | Py_DECREF(pArgs); 61 | Py_DECREF(pModule); 62 | fprintf(stderr, "Cannot convert argument\n"); 63 | return 1; 64 | } 65 | 66 | /* pValue reference stolen here: */ 67 | PyTuple_SetItem(pArgs, i, pValue); 68 | } 69 | 70 | // call the function 71 | pValue = PyObject_CallObject(pFunc, pArgs); 72 | 73 | Py_DECREF(pArgs); 74 | 75 | if (pValue != NULL) 76 | { 77 | printf("Result of call: %ld\n", PyInt_AsLong(pValue)); 78 | Py_DECREF(pValue); 79 | } 80 | else 81 | { 82 | Py_DECREF(pFunc); 83 | Py_DECREF(pModule); 84 | PyErr_Print(); 85 | fprintf(stderr,"Call failed\n"); 86 | return 1; 87 | } 88 | } 89 | else 90 | { 91 | if (PyErr_Occurred()) 92 | PyErr_Print(); 93 | 94 | fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]); 95 | } 96 | 97 | Py_XDECREF(pFunc); 98 | Py_DECREF(pModule); 99 | } 100 | else 101 | { 102 | PyErr_Print(); 103 | fprintf(stderr, "Failed to load \"%s\"\n", argv[1]); 104 | return 1; 105 | } 106 | 107 | Py_Finalize(); 108 | return 0; 109 | } 110 | 111 | #endif 112 | 113 | -------------------------------------------------------------------------------- /tools/diagnostic/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | file(GLOB diagSources *.cpp) 3 | file(GLOB diagIncludes *.h ) 4 | 5 | add_executable(deepRL-diagnostic ${diagSources}) 6 | target_link_libraries(deepRL-diagnostic jetson-reinforcement) 7 | -------------------------------------------------------------------------------- /tools/diagnostic/main.lua.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * deepRL 3 | */ 4 | 5 | #include 6 | 7 | 8 | #ifdef USE_LUA 9 | 10 | extern "C" 11 | { 12 | #include 13 | #include 14 | #include 15 | //#include 16 | } 17 | 18 | #include 19 | #include 20 | 21 | 22 | #define SCRIPT_FILENAME "test-packages.lua" 23 | 24 | 25 | bool init() 26 | { 27 | lua_State* L = luaL_newstate(); 28 | 29 | if( !L ) 30 | { 31 | printf("[deepRL] failed to create lua_State (luaL_newstate returned NULL)\n"); 32 | return false; 33 | } 34 | 35 | printf("[deepRL] created new lua_State\n"); 36 | 37 | luaL_openlibs(L); 38 | printf("[deepRL] opened lua libraries\n"); 39 | 40 | // load and run file 41 | const int res = luaL_dofile(L, SCRIPT_FILENAME); 42 | 43 | if( res == 1 ) 44 | { 45 | printf("Error executing resource: %s\n", SCRIPT_FILENAME); 46 | const char* luastr = lua_tostring(L,-1); 47 | 48 | if( luastr != NULL ) 49 | printf("%s\n", luastr); 50 | } 51 | 52 | printf("[deepRL] closing lua_State\n"); 53 | lua_close(L); 54 | 55 | return true; 56 | } 57 | 58 | 59 | int main( int argc, char** argv ) 60 | { 61 | printf("deepRL-diagnostic (lua)\n\n"); 62 | 63 | if( !init() ) 64 | { 65 | printf("failed to init lua, exiting deepRL-console\n"); 66 | return 0; 67 | } 68 | 69 | return 0; 70 | } 71 | 72 | #endif 73 | 74 | -------------------------------------------------------------------------------- /tools/input/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | file(GLOB inputSources *.cpp) 3 | file(GLOB inputIncludes *.h ) 4 | 5 | add_executable(deepRL-input ${inputSources}) 6 | target_link_libraries(deepRL-input jetson-reinforcement) 7 | -------------------------------------------------------------------------------- /tools/input/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * deepRL 3 | */ 4 | 5 | #include "devInput.h" 6 | 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | printf("deepRL-input\n"); 11 | 12 | // scan for devices 13 | DeviceList devices; 14 | InputDevices::Enumerate(devices); 15 | 16 | // create input manager device 17 | InputDevices* mgr = InputDevices::Create(); 18 | 19 | if( !mgr ) 20 | return 0; 21 | 22 | // enable verbose debug text 23 | mgr->Debug(); 24 | 25 | // poll for updates 26 | while(true) 27 | { 28 | mgr->Poll(0); 29 | } 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /utils/.gitignore: -------------------------------------------------------------------------------- 1 | build/ -------------------------------------------------------------------------------- /utils/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 2.8) 3 | 4 | # determine if jetson-utils is being built as a submodule inside another repo, 5 | # or if it's being build standalone (if the later, we need to do some configuration) 6 | get_directory_property(hasParent PARENT_DIRECTORY) 7 | 8 | if(hasParent) 9 | message("-- jetson-utils: building as submodule, ${hasParent}") 10 | 11 | # configure Qt 12 | find_package(Qt4 REQUIRED) 13 | include(${QT_USE_FILE}) 14 | add_definitions(${QT_DEFINITIONS}) 15 | else() 16 | message("-- jetson-utils: building as standalone") 17 | 18 | # standalone project 19 | project(jetson-utils) 20 | 21 | # -std=gnu++11 22 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 23 | 24 | # Qt is used to load images (installed by ubuntu-desktop) 25 | find_package(Qt4 REQUIRED) 26 | include(${QT_USE_FILE}) 27 | add_definitions(${QT_DEFINITIONS}) 28 | 29 | # setup CUDA 30 | find_package(CUDA) 31 | 32 | set( 33 | CUDA_NVCC_FLAGS 34 | ${CUDA_NVCC_FLAGS}; 35 | -O3 36 | -gencode arch=compute_37,code=sm_37 37 | -gencode arch=compute_53,code=sm_53 38 | -gencode arch=compute_60,code=sm_60 39 | -gencode arch=compute_61,code=sm_61 40 | -gencode arch=compute_62,code=sm_62 41 | ) 42 | 43 | # setup project output paths 44 | set(PROJECT_OUTPUT_DIR ${PROJECT_BINARY_DIR}/${CMAKE_SYSTEM_PROCESSOR}) 45 | set(PROJECT_INCLUDE_DIR ${PROJECT_OUTPUT_DIR}/include) 46 | 47 | file(MAKE_DIRECTORY ${PROJECT_INCLUDE_DIR}) 48 | file(MAKE_DIRECTORY ${PROJECT_OUTPUT_DIR}/bin) 49 | 50 | message("-- system arch: ${CMAKE_SYSTEM_PROCESSOR}") 51 | message("-- output path: ${PROJECT_OUTPUT_DIR}") 52 | 53 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_OUTPUT_DIR}/bin) 54 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_OUTPUT_DIR}/lib) 55 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_OUTPUT_DIR}/lib) 56 | 57 | # build C/C++ interface 58 | include_directories(${PROJECT_INCLUDE_DIR}) 59 | #include_directories(/usr/include/gstreamer-1.0 /usr/lib/aarch64-linux-gnu/gstreamer-1.0/include /usr/include/glib-2.0 /usr/include/libxml2 /usr/lib/aarch64-linux-gnu/glib-2.0/include/) 60 | 61 | endif() 62 | 63 | include_directories(/usr/include/gstreamer-1.0 /usr/include/glib-2.0 /usr/include/libxml2 /usr/lib/aarch64-linux-gnu/gstreamer-1.0/include /usr/lib/x86_64-linux-gnu/gstreamer-1.0/include/ /usr/lib/aarch64-linux-gnu/glib-2.0/include/ /usr/lib/x86_64-linux-gnu/glib-2.0/include/) 64 | 65 | 66 | # build library 67 | file(GLOB jetsonUtilitySources *.cpp camera/*.cpp cuda/*.cu display/*.cpp input/*.cpp) 68 | file(GLOB jetsonUtilityIncludes *.h camera/*.h cuda/*.h display/*.h input/*.h) 69 | 70 | cuda_add_library(jetson-utils SHARED ${jetsonUtilitySources}) 71 | target_link_libraries(jetson-utils Qt4::QtGui GL GLEW gstreamer-1.0 gstapp-1.0) 72 | 73 | 74 | # transfer all headers to the include directory 75 | foreach(include ${jetsonUtilityIncludes}) 76 | message("-- Copying ${include}") 77 | configure_file(${include} ${PROJECT_INCLUDE_DIR} COPYONLY) 78 | endforeach() 79 | 80 | # copy image data 81 | file(GLOB imageData data/*.png data/*.jpg) 82 | #file(GLOB imageData ${PROJECT_SOURCE_DIR}/data/*.png ${PROJECT_SOURCE_DIR}/data/*.jpg) 83 | 84 | foreach(image ${imageData}) 85 | message("-- Copying ${image}") 86 | file(COPY ${image} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) 87 | #configure_file(${include} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COPYONLY) 88 | endforeach() 89 | 90 | 91 | # post-build standalone steps 92 | if(hasParent) 93 | # nop 94 | else() 95 | # setup `make install` stuff 96 | foreach(include ${jetsonUtilityIncludes}) 97 | install(FILES "${include}" DESTINATION include/jetson-utils) 98 | endforeach() 99 | 100 | # install the shared library 101 | install(TARGETS jetson-utils DESTINATION lib/jetson-utility EXPORT jetson-utilityConfig) 102 | 103 | # install the cmake project, for importing 104 | #install(EXPORT jetson-utilityConfig DESTINATION share/jetson-utility/cmake) 105 | endif() 106 | 107 | 108 | # build tests/sample executables 109 | add_subdirectory(camera/gst-camera) 110 | add_subdirectory(camera/v4l2-console) 111 | add_subdirectory(camera/v4l2-display) 112 | add_subdirectory(display/gl-display-test) 113 | -------------------------------------------------------------------------------- /utils/LICENSE.md: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | -------------------------------------------------------------------------------- /utils/README.md: -------------------------------------------------------------------------------- 1 | # jetson-utils 2 | C/C++ wrapper Linux utilities for NVIDIA Jetson TX1/TX2 - camera, HID, GStreamer, CUDA, OpenGL/XGL 3 | -------------------------------------------------------------------------------- /utils/camera/gst-camera/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | file(GLOB gstCameraSources *.cpp) 3 | file(GLOB gstCameraIncludes *.h ) 4 | 5 | add_executable(gst-camera ${gstCameraSources}) 6 | target_link_libraries(gst-camera jetson-utils) 7 | -------------------------------------------------------------------------------- /utils/camera/gstCamera.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __GSTREAMER_CAMERA_H__ 24 | #define __GSTREAMER_CAMERA_H__ 25 | 26 | #include 27 | #include 28 | 29 | 30 | struct _GstAppSink; 31 | class QWaitCondition; 32 | class QMutex; 33 | 34 | 35 | /** 36 | * gstreamer CSI camera using nvcamerasrc (or optionally v4l2src) 37 | * @ingroup util 38 | */ 39 | class gstCamera 40 | { 41 | public: 42 | // Create camera 43 | static gstCamera* Create( int v4l2_device=-1 ); // use onboard camera by default (>=0 for V4L2) 44 | static gstCamera* Create( uint32_t width, uint32_t height, int v4l2_device=-1 ); 45 | 46 | // Destroy 47 | ~gstCamera(); 48 | 49 | // Start/stop streaming 50 | bool Open(); 51 | void Close(); 52 | 53 | // Capture YUV (NV12) 54 | bool Capture( void** cpu, void** cuda, unsigned long timeout=ULONG_MAX ); 55 | 56 | // Takes in captured YUV-NV12 CUDA image, converts to float4 RGBA (with pixel intensity 0-255) 57 | // Set zeroCopy to true if you need to access ConvertRGBA from CPU, otherwise it will be CUDA only. 58 | bool ConvertRGBA( void* input, void** output, bool zeroCopy=false ); 59 | 60 | // Image dimensions 61 | inline uint32_t GetWidth() const { return mWidth; } 62 | inline uint32_t GetHeight() const { return mHeight; } 63 | inline uint32_t GetPixelDepth() const { return mDepth; } 64 | inline uint32_t GetSize() const { return mSize; } 65 | 66 | // Default resolution, unless otherwise specified during Create() 67 | static const uint32_t DefaultWidth = 1280; 68 | static const uint32_t DefaultHeight = 720; 69 | 70 | private: 71 | static void onEOS(_GstAppSink* sink, void* user_data); 72 | static GstFlowReturn onPreroll(_GstAppSink* sink, void* user_data); 73 | static GstFlowReturn onBuffer(_GstAppSink* sink, void* user_data); 74 | 75 | gstCamera(); 76 | 77 | bool init(); 78 | bool buildLaunchStr(); 79 | void checkMsgBus(); 80 | void checkBuffer(); 81 | 82 | _GstBus* mBus; 83 | _GstAppSink* mAppSink; 84 | _GstElement* mPipeline; 85 | 86 | std::string mLaunchStr; 87 | 88 | uint32_t mWidth; 89 | uint32_t mHeight; 90 | uint32_t mDepth; 91 | uint32_t mSize; 92 | 93 | static const uint32_t NUM_RINGBUFFERS = 16; 94 | 95 | void* mRingbufferCPU[NUM_RINGBUFFERS]; 96 | void* mRingbufferGPU[NUM_RINGBUFFERS]; 97 | 98 | QWaitCondition* mWaitEvent; 99 | 100 | QMutex* mWaitMutex; 101 | QMutex* mRingMutex; 102 | 103 | uint32_t mLatestRGBA; 104 | uint32_t mLatestRingbuffer; 105 | bool mLatestRetrieved; 106 | 107 | void* mRGBA[NUM_RINGBUFFERS]; 108 | int mV4L2Device; // -1 for onboard, >=0 for V4L2 device 109 | 110 | inline bool onboardCamera() const { return (mV4L2Device < 0); } 111 | }; 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /utils/camera/gstUtility.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __GSTREAMER_UTILITY_H__ 24 | #define __GSTREAMER_UTILITY_H__ 25 | 26 | 27 | #include 28 | 29 | 30 | /** 31 | * LOG_GSTREAMER printf prefix 32 | * @ingroup util 33 | */ 34 | #define LOG_GSTREAMER "[gstreamer] " 35 | 36 | 37 | /** 38 | * gstreamerInit 39 | * @ingroup util 40 | */ 41 | bool gstreamerInit(); 42 | 43 | 44 | /** 45 | * gst_message_print 46 | * @ingroup util 47 | */ 48 | gboolean gst_message_print(_GstBus* bus, _GstMessage* message, void* user_data); 49 | 50 | 51 | 52 | #endif 53 | 54 | -------------------------------------------------------------------------------- /utils/camera/v4l2-console/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | file(GLOB v4l2ConsoleSources *.cpp) 3 | file(GLOB v4l2ConsoleIncludes *.h ) 4 | 5 | add_executable(v4l2-console ${v4l2ConsoleSources}) 6 | target_link_libraries(v4l2-console jetson-utils) 7 | -------------------------------------------------------------------------------- /utils/camera/v4l2-console/v4l2-console.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "v4l2Camera.h" 24 | 25 | #include 26 | #include 27 | //#include 28 | #include 29 | 30 | 31 | bool signal_recieved = false; 32 | 33 | void sig_handler(int signo) 34 | { 35 | if( signo == SIGINT ) 36 | { 37 | printf("received SIGINT\n"); 38 | signal_recieved = true; 39 | } 40 | } 41 | 42 | 43 | 44 | int main( int argc, char** argv ) 45 | { 46 | printf("v4l2-console\n args (%i): ", argc); 47 | 48 | /* 49 | * verify parameters 50 | */ 51 | for( int i=0; i < argc; i++ ) 52 | printf("%i [%s] ", i, argv[i]); 53 | 54 | printf("\n"); 55 | 56 | if( argc < 2 ) 57 | { 58 | printf("v4l2-console: 0 arguments were supplied.\n"); 59 | printf("usage: v4l2-console \n"); 60 | printf(" ./v4l2-console /dev/video0\n"); 61 | 62 | return 0; 63 | } 64 | 65 | const char* dev_path = argv[1]; 66 | printf("v4l2-console: attempting to initialize video device '%s'\n\n", dev_path); 67 | 68 | if( signal(SIGINT, sig_handler) == SIG_ERR ) 69 | printf("\ncan't catch SIGINT\n"); 70 | 71 | /* 72 | * create the camera device 73 | */ 74 | v4l2Camera* camera = v4l2Camera::Create(dev_path); 75 | 76 | if( !camera ) 77 | { 78 | printf("\nv4l2-console: failed to initialize video device '%s'\n", dev_path); 79 | return 0; 80 | } 81 | 82 | printf("\nv4l2-console: successfully initialized video device '%s'\n", dev_path); 83 | printf(" width: %u\n", camera->GetWidth()); 84 | printf(" height: %u\n", camera->GetHeight()); 85 | printf(" depth: %u (bpp)\n", camera->GetPixelDepth()); 86 | 87 | 88 | /* 89 | * start streaming 90 | */ 91 | if( !camera->Open() ) 92 | { 93 | printf("\nv4l2-console: failed to open camera '%s' for streaming\n", dev_path); 94 | return 0; 95 | } 96 | 97 | printf("\nv4l2-console: camera '%s' open for streaming\n", dev_path); 98 | 99 | 100 | while( !signal_recieved ) 101 | { 102 | uint8_t* img = (uint8_t*)camera->Capture(500); 103 | 104 | if( !img ) 105 | { 106 | //printf("got NULL image from camera capture\n"); 107 | continue; 108 | } 109 | else 110 | { 111 | printf("recieved new video frame\n"); 112 | 113 | static int num_frames = 0; 114 | 115 | const int width = camera->GetWidth(); 116 | const int height = camera->GetHeight(); 117 | 118 | QImage qImg(width, height, QImage::Format_RGB32); 119 | 120 | for( int y=0; y < height; y++ ) 121 | { 122 | for( int x=0; x < width; x++ ) 123 | { 124 | const int value = img[y * width + x]; 125 | if( value != 0 ) 126 | printf("%i %i %i\n", x, y, value); 127 | qImg.setPixel(x, y, qRgb(value, value, value)); 128 | } 129 | } 130 | 131 | char output_filename[64]; 132 | sprintf(output_filename, "camera-%u.jpg", num_frames); 133 | 134 | qImg.save(QString(output_filename)); 135 | num_frames++; 136 | } 137 | 138 | } 139 | 140 | printf("\nv4l2-console: un-initializing video device '%s'\n", dev_path); 141 | 142 | 143 | /* 144 | * shutdown the camera device 145 | */ 146 | if( camera != NULL ) 147 | { 148 | delete camera; 149 | camera = NULL; 150 | } 151 | 152 | printf("v4l2-console: video device '%s' has been un-initialized.\n", dev_path); 153 | printf("v4l2-console: this concludes the test of video device '%s'\n", dev_path); 154 | return 0; 155 | } 156 | -------------------------------------------------------------------------------- /utils/camera/v4l2-display/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | file(GLOB v4l2DisplaySources *.cpp) 3 | file(GLOB v4l2DisplayIncludes *.h ) 4 | 5 | add_executable(v4l2-display ${v4l2DisplaySources}) 6 | target_link_libraries(v4l2-display jetson-utils) 7 | -------------------------------------------------------------------------------- /utils/camera/v4l2-display/v4l2-display.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "v4l2Camera.h" 24 | #include "glDisplay.h" 25 | #include "cudaMappedMemory.h" 26 | 27 | #include 28 | 29 | 30 | int main( int argc, char** argv ) 31 | { 32 | printf("v4l2-display\n args (%i): ", argc); 33 | 34 | /* 35 | * verify parameters 36 | */ 37 | for( int i=0; i < argc; i++ ) 38 | printf("%i [%s] ", i, argv[i]); 39 | 40 | printf("\n"); 41 | 42 | if( argc < 2 ) 43 | { 44 | printf("v4l2-display: 0 arguments were supplied.\n"); 45 | printf("usage: v4l2-display \n"); 46 | printf(" ./v4l2-display /dev/video0\n"); 47 | 48 | return 0; 49 | } 50 | 51 | const char* dev_path = argv[1]; 52 | printf("v4l2-display: attempting to initialize video device '%s'\n\n", dev_path); 53 | 54 | 55 | /* 56 | * create the camera device 57 | */ 58 | v4l2Camera* camera = v4l2Camera::Create(dev_path); 59 | 60 | if( !camera ) 61 | { 62 | printf("\nv4l2-display: failed to initialize video device '%s'\n", dev_path); 63 | return 0; 64 | } 65 | 66 | printf("\nv4l2-display: successfully initialized video device '%s'\n", dev_path); 67 | printf(" width: %u\n", camera->GetWidth()); 68 | printf(" height: %u\n", camera->GetHeight()); 69 | printf(" depth: %u (bpp)\n", camera->GetPixelDepth()); 70 | 71 | printf("\nv4l2-display: un-initializing video device '%s'\n", dev_path); 72 | 73 | 74 | /* 75 | * create openGL window 76 | */ 77 | glDisplay* display = glDisplay::Create(); 78 | 79 | if( !display ) 80 | { 81 | printf("\nv4l2-display: failed to create openGL display\n"); 82 | return 0; 83 | } 84 | 85 | glTexture* tex = glTexture::Create(camera->GetWidth(), camera->GetHeight(), GL_LUMINANCE8); 86 | 87 | if( !tex ) 88 | { 89 | printf("v4l2-display: failed to create %ux%u openGL texture\n", camera->GetWidth(), camera->GetHeight()); 90 | return 0; 91 | } 92 | 93 | printf("v4l2-display: initialized %u x %u openGL texture (%u bytes)\n", tex->GetWidth(), tex->GetHeight(), tex->GetSize()); 94 | 95 | 96 | 97 | 98 | /* 99 | * shutdown 100 | */ 101 | if( display != NULL ) 102 | { 103 | delete display; 104 | display = NULL; 105 | } 106 | 107 | if( camera != NULL ) 108 | { 109 | delete camera; 110 | camera = NULL; 111 | } 112 | 113 | printf("v4l2-display: video device '%s' has been un-initialized.\n", dev_path); 114 | printf("v4l2-display: this concludes the test of video device '%s'\n", dev_path); 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /utils/camera/v4l2Camera.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __V4L2_CAPTURE_H 24 | #define __V4L2_CAPTURE_H 25 | 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | 34 | 35 | struct v4l2_mmap 36 | { 37 | struct v4l2_buffer buf; 38 | void* ptr; 39 | }; 40 | 41 | 42 | /** 43 | * Video4Linux2 camera capture streaming. 44 | * @ingroup util 45 | */ 46 | class v4l2Camera 47 | { 48 | public: 49 | /** 50 | * Create V4L2 interface 51 | * @param path Filename of the video device (e.g. /dev/video0) 52 | */ 53 | static v4l2Camera* Create( const char* device_path ); 54 | 55 | /** 56 | * Destructor 57 | */ 58 | ~v4l2Camera(); 59 | 60 | /** 61 | * Start streaming 62 | */ 63 | bool Open(); 64 | 65 | /** 66 | * Stop streaming 67 | */ 68 | bool Close(); 69 | 70 | /** 71 | * Return the next image. 72 | */ 73 | void* Capture( size_t timeout=0 ); 74 | 75 | /** 76 | * Get width, in pixels, of camera image. 77 | */ 78 | inline uint32_t GetWidth() const { return mWidth; } 79 | 80 | /** 81 | * Retrieve height, in pixels, of camera image. 82 | */ 83 | inline uint32_t GetHeight() const { return mHeight; } 84 | 85 | /** 86 | * Return the size in bytes of one line of the image. 87 | */ 88 | inline uint32_t GetPitch() const { return mPitch; } 89 | 90 | /** 91 | * Return the bit depth per pixel. 92 | */ 93 | inline uint32_t GetPixelDepth() const { return mPixelDepth; } 94 | 95 | private: 96 | 97 | v4l2Camera( const char* device_path ); 98 | 99 | bool init(); 100 | bool initCaps(); 101 | bool initFormats(); 102 | bool initStream(); 103 | 104 | bool initUserPtr(); 105 | bool initMMap(); 106 | 107 | int mFD; 108 | int mRequestFormat; 109 | uint32_t mRequestWidth; 110 | uint32_t mRequestHeight; 111 | uint32_t mWidth; 112 | uint32_t mHeight; 113 | uint32_t mPitch; 114 | uint32_t mPixelDepth; 115 | 116 | v4l2_mmap* mBuffersMMap; 117 | size_t mBufferCountMMap; 118 | 119 | std::vector mFormats; 120 | std::string mDevicePath; 121 | }; 122 | 123 | 124 | #endif 125 | 126 | 127 | -------------------------------------------------------------------------------- /utils/commandLine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __COMMAND_LINE_H_ 24 | #define __COMMAND_LINE_H_ 25 | 26 | 27 | /** 28 | * commandLine parser class 29 | * @ingroup util 30 | */ 31 | class commandLine 32 | { 33 | public: 34 | /** 35 | * constructor 36 | */ 37 | commandLine( const int argc, char** argv ); 38 | 39 | 40 | /** 41 | * Checks to see whether the specified flag was included on the 42 | * command line. For example, if argv contained "--foo", then 43 | * GetFlag("foo") would return true. 44 | * 45 | * @returns true, if the flag with argName was found 46 | * false, if the flag with argName was not found 47 | */ 48 | bool GetFlag( const char* argName ); 49 | 50 | 51 | /** 52 | * Get float argument. For example if argv contained "--foo=3.14159", 53 | * then GetInt("foo") would return 3.14159.0f 54 | * 55 | * @returns defaultValue, if the argumentcould not be found. 56 | * Otherwise, returns the value of the argument. 57 | */ 58 | float GetFloat( const char* argName, float defaultValue=0.0f ); 59 | 60 | 61 | /** 62 | * Get integer argument. For example if argv contained "--foo=100", 63 | * then GetInt("foo") would return 100. 64 | * 65 | * @returns defaultValue, if the argument could not be found. 66 | * Otherwise, returns the value of the argument. 67 | */ 68 | int GetInt( const char* argName, int defaultValue=0 ); 69 | 70 | 71 | /** 72 | * Get string argument. For example if argv contained "--foo=bar", 73 | * then GetString("foo") would return "bar". 74 | * 75 | * @returns NULL, if the argument could not be found. 76 | * Otherwise, returns a pointer to the argument value string 77 | * from the argv array. 78 | */ 79 | const char* GetString( const char* argName ); 80 | 81 | 82 | protected: 83 | 84 | int argc; 85 | char** argv; 86 | }; 87 | 88 | 89 | 90 | #endif 91 | 92 | -------------------------------------------------------------------------------- /utils/cuda/cudaFont.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __CUDA_FONT_H__ 24 | #define __CUDA_FONT_H__ 25 | 26 | #include "cudaUtility.h" 27 | 28 | #include 29 | #include 30 | 31 | 32 | /** 33 | * Font overlay rendering using CUDA 34 | * @ingroup util 35 | */ 36 | class cudaFont 37 | { 38 | public: 39 | /** 40 | * Create new CUDA font overlay object using textured fonts 41 | */ 42 | static cudaFont* Create( const char* font_bitmap="fontmapA.png" ); 43 | 44 | /** 45 | * Destructor 46 | */ 47 | ~cudaFont(); 48 | 49 | /** 50 | * Draw font overlay onto image 51 | */ 52 | bool RenderOverlay( float4* input, float4* output, uint32_t width, uint32_t height, 53 | const char* str, int x, int y, const float4& color=make_float4(0, 0, 0, 255)); 54 | 55 | /** 56 | * Draw font overlay onto image 57 | */ 58 | bool RenderOverlay( float4* input, float4* output, uint32_t width, uint32_t height, 59 | const std::vector< std::pair< std::string, int2 > >& text, 60 | const float4& color=make_float4(0.0f, 0.0f, 0.0f, 255.0f)); 61 | 62 | protected: 63 | cudaFont(); 64 | bool init( const char* bitmap_path ); 65 | 66 | float4* mFontMapCPU; 67 | float4* mFontMapGPU; 68 | 69 | int mFontMapWidth; 70 | int mFontMapHeight; 71 | int2 mFontCellSize; 72 | 73 | short4* mCommandCPU; 74 | short4* mCommandGPU; 75 | int mCmdEntries; 76 | 77 | static const uint32_t MaxCommands = 1024; 78 | }; 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /utils/cuda/cudaMappedMemory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __CUDA_MAPPED_MEMORY_H_ 24 | #define __CUDA_MAPPED_MEMORY_H_ 25 | 26 | 27 | #include "cudaUtility.h" 28 | 29 | 30 | /** 31 | * Allocate ZeroCopy mapped memory, shared between CUDA and CPU. 32 | * @ingroup util 33 | */ 34 | inline bool cudaAllocMapped( void** cpuPtr, void** gpuPtr, size_t size ) 35 | { 36 | if( !cpuPtr || !gpuPtr || size == 0 ) 37 | return false; 38 | 39 | //CUDA(cudaSetDeviceFlags(cudaDeviceMapHost)); 40 | 41 | if( CUDA_FAILED(cudaHostAlloc(cpuPtr, size, cudaHostAllocMapped)) ) 42 | return false; 43 | 44 | if( CUDA_FAILED(cudaHostGetDevicePointer(gpuPtr, *cpuPtr, 0)) ) 45 | return false; 46 | 47 | memset(*cpuPtr, 0, size); 48 | printf("[cuda] cudaAllocMapped %zu bytes, CPU %p GPU %p\n", size, *cpuPtr, *gpuPtr); 49 | return true; 50 | } 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /utils/cuda/cudaNormalize.cu: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "cudaNormalize.h" 24 | 25 | 26 | 27 | // gpuNormalize 28 | template 29 | __global__ void gpuNormalize( T* input, T* output, int width, int height, float scaling_factor ) 30 | { 31 | const int x = blockIdx.x * blockDim.x + threadIdx.x; 32 | const int y = blockIdx.y * blockDim.y + threadIdx.y; 33 | 34 | if( x >= width || y >= height ) 35 | return; 36 | 37 | const T px = input[ y * width + x ]; 38 | 39 | output[y*width+x] = make_float4(px.x * scaling_factor, 40 | px.y * scaling_factor, 41 | px.z * scaling_factor, 42 | px.w * scaling_factor); 43 | } 44 | 45 | 46 | // cudaNormalizeRGBA 47 | cudaError_t cudaNormalizeRGBA( float4* input, const float2& input_range, 48 | float4* output, const float2& output_range, 49 | size_t width, size_t height ) 50 | { 51 | if( !input || !output ) 52 | return cudaErrorInvalidDevicePointer; 53 | 54 | if( width == 0 || height == 0 ) 55 | return cudaErrorInvalidValue; 56 | 57 | const float multiplier = output_range.y / input_range.y; 58 | 59 | // launch kernel 60 | const dim3 blockDim(8, 8); 61 | const dim3 gridDim(iDivUp(width,blockDim.x), iDivUp(height,blockDim.y)); 62 | 63 | gpuNormalize<<>>(input, output, width, height, multiplier); 64 | 65 | return CUDA(cudaGetLastError()); 66 | } 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /utils/cuda/cudaNormalize.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __CUDA_NORMALIZE_H__ 24 | #define __CUDA_NORMALIZE_H__ 25 | 26 | 27 | #include "cudaUtility.h" 28 | 29 | 30 | /** 31 | * Rebase the pixel intensities of an image between two scales. 32 | * For example, convert an image with values 0.0-255 to 0.0-1.0. 33 | * @ingroup util 34 | */ 35 | cudaError_t cudaNormalizeRGBA( float4* input, const float2& input_range, 36 | float4* output, const float2& output_range, 37 | size_t width, size_t height ); 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /utils/cuda/cudaOverlay.cu: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "cudaOverlay.h" 24 | 25 | 26 | static inline __device__ __host__ bool eq_less( float a, float b, float epsilon ) 27 | { 28 | return (a > (b - epsilon) && a < (b + epsilon)) ? true : false; 29 | } 30 | 31 | template 32 | __global__ void gpuRectOutlines( T* input, T* output, int width, int height, 33 | float4* rects, int numRects, float4 color ) 34 | { 35 | const int x = blockIdx.x * blockDim.x + threadIdx.x; 36 | const int y = blockIdx.y * blockDim.y + threadIdx.y; 37 | 38 | if( x >= width || y >= height ) 39 | return; 40 | 41 | const T px_in = input[ y * width + x ]; 42 | T px_out = px_in; 43 | 44 | const float fx = x; 45 | const float fy = y; 46 | 47 | const float thick = 10.0f; 48 | const float alpha = color.w / 255.0f; 49 | const float ialph = 1.0f - alpha; 50 | 51 | for( int nr=0; nr < numRects; nr++ ) 52 | { 53 | const float4 r = rects[nr]; 54 | 55 | //printf("%i %i %i %f %f %f %f\n", numRects, x, y, r.x, r.y, r.z, r.w); 56 | 57 | if( fy >= r.y && fy <= r.w /*&& (eq_less(fx, r.x, ep) || eq_less(fx, r.z, ep))*/ ) 58 | { 59 | if( fx >= r.x && fx <= r.z /*&& (eq_less(fy, r.y, ep) || eq_less(fy, r.w, ep))*/ ) 60 | { 61 | //printf("cuda rect %i %i\n", x, y); 62 | 63 | px_out.x = alpha * color.x + ialph * px_out.x; 64 | px_out.y = alpha * color.y + ialph * px_out.y; 65 | px_out.z = alpha * color.z + ialph * px_out.z; 66 | } 67 | } 68 | } 69 | 70 | output[y * width + x] = px_out; 71 | } 72 | 73 | 74 | cudaError_t cudaRectOutlineOverlay( float4* input, float4* output, uint32_t width, uint32_t height, float4* boundingBoxes, int numBoxes, const float4& color ) 75 | { 76 | if( !input || !output || width == 0 || height == 0 || !boundingBoxes || numBoxes == 0 ) 77 | return cudaErrorInvalidValue; 78 | 79 | // launch kernel 80 | const dim3 blockDim(8, 8); 81 | const dim3 gridDim(iDivUp(width,blockDim.x), iDivUp(height,blockDim.y)); 82 | 83 | gpuRectOutlines<<>>(input, output, width, height, boundingBoxes, numBoxes, color); 84 | 85 | return cudaGetLastError(); 86 | } 87 | -------------------------------------------------------------------------------- /utils/cuda/cudaOverlay.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __CUDA_OVERLAY_H__ 24 | #define __CUDA_OVERLAY_H__ 25 | 26 | #include "cudaUtility.h" 27 | 28 | 29 | /** 30 | * cudaRectOutlineOverlay 31 | * @ingroup util 32 | */ 33 | cudaError_t cudaRectOutlineOverlay( float4* input, float4* output, uint32_t width, uint32_t height, float4* boundingBoxes, int numBoxes, const float4& color ); 34 | 35 | 36 | /** 37 | * cudaRectFillOverlay 38 | * @ingroup util 39 | */ 40 | //cudaError_t cudaRectFillOverlay( float4* input, float4* output, uint32_t width, uint32_t height, float4* boundingBoxes, int numBoxes, const float4& color ); 41 | 42 | 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /utils/cuda/cudaRGB.cu: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "cudaRGB.h" 24 | 25 | //------------------------------------------------------------------------------------------------------------------------- 26 | 27 | __global__ void RGBToRGBAf(uchar3* srcImage, 28 | float4* dstImage, 29 | uint32_t width, uint32_t height) 30 | { 31 | int x, y, pixel; 32 | 33 | x = (blockIdx.x * blockDim.x) + threadIdx.x; 34 | y = (blockIdx.y * blockDim.y) + threadIdx.y; 35 | 36 | pixel = y * width + x; 37 | 38 | if (x >= width) 39 | return; 40 | 41 | if (y >= height) 42 | return; 43 | 44 | // printf("cuda thread %i %i %i %i pixel %i \n", x, y, width, height, pixel); 45 | 46 | const float s = 1.0f; 47 | const uchar3 px = srcImage[pixel]; 48 | 49 | dstImage[pixel] = make_float4(px.x * s, px.y * s, px.z * s, 255.0f * s); 50 | } 51 | 52 | cudaError_t cudaRGBToRGBAf( uchar3* srcDev, float4* destDev, size_t width, size_t height ) 53 | { 54 | if( !srcDev || !destDev ) 55 | return cudaErrorInvalidDevicePointer; 56 | 57 | const dim3 blockDim(8,8,1); 58 | const dim3 gridDim(iDivUp(width,blockDim.x), iDivUp(height,blockDim.y), 1); 59 | 60 | RGBToRGBAf<<>>( srcDev, destDev, width, height ); 61 | 62 | return CUDA(cudaGetLastError()); 63 | } 64 | 65 | -------------------------------------------------------------------------------- /utils/cuda/cudaRGB.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __CUDA_RGB_CONVERT_H 24 | #define __CUDA_RGB_CONVERT_H 25 | 26 | 27 | #include "cudaUtility.h" 28 | #include 29 | 30 | 31 | /** 32 | * Convert 8-bit fixed-point RGB image to 32-bit floating-point RGBA image 33 | * @ingroup util 34 | */ 35 | cudaError_t cudaRGBToRGBAf( uchar3* input, float4* output, size_t width, size_t height ); 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /utils/cuda/cudaResize.cu: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "cudaResize.h" 24 | 25 | 26 | 27 | // gpuResample 28 | template 29 | __global__ void gpuResize( float2 scale, T* input, int iWidth, T* output, int oWidth, int oHeight ) 30 | { 31 | const int x = blockIdx.x * blockDim.x + threadIdx.x; 32 | const int y = blockIdx.y * blockDim.y + threadIdx.y; 33 | 34 | if( x >= oWidth || y >= oHeight ) 35 | return; 36 | 37 | const int dx = ((float)x * scale.x); 38 | const int dy = ((float)y * scale.y); 39 | 40 | const T px = input[ dy * iWidth + dx ]; 41 | 42 | output[y*oWidth+x] = px; 43 | } 44 | 45 | 46 | // cudaResize 47 | cudaError_t cudaResize( float* input, size_t inputWidth, size_t inputHeight, 48 | float* output, size_t outputWidth, size_t outputHeight ) 49 | { 50 | if( !input || !output ) 51 | return cudaErrorInvalidDevicePointer; 52 | 53 | if( inputWidth == 0 || outputWidth == 0 || inputHeight == 0 || outputHeight == 0 ) 54 | return cudaErrorInvalidValue; 55 | 56 | const float2 scale = make_float2( float(inputWidth) / float(outputWidth), 57 | float(inputHeight) / float(outputHeight) ); 58 | 59 | // launch kernel 60 | const dim3 blockDim(8, 8); 61 | const dim3 gridDim(iDivUp(outputWidth,blockDim.x), iDivUp(outputHeight,blockDim.y)); 62 | 63 | gpuResize<<>>(scale, input, inputWidth, output, outputWidth, outputHeight); 64 | 65 | return CUDA(cudaGetLastError()); 66 | } 67 | 68 | 69 | // cudaResizeRGBA 70 | cudaError_t cudaResizeRGBA( float4* input, size_t inputWidth, size_t inputHeight, 71 | float4* output, size_t outputWidth, size_t outputHeight ) 72 | { 73 | if( !input || !output ) 74 | return cudaErrorInvalidDevicePointer; 75 | 76 | if( inputWidth == 0 || outputWidth == 0 || inputHeight == 0 || outputHeight == 0 ) 77 | return cudaErrorInvalidValue; 78 | 79 | const float2 scale = make_float2( float(inputWidth) / float(outputWidth), 80 | float(inputHeight) / float(outputHeight) ); 81 | 82 | // launch kernel 83 | const dim3 blockDim(8, 8); 84 | const dim3 gridDim(iDivUp(outputWidth,blockDim.x), iDivUp(outputHeight,blockDim.y)); 85 | 86 | gpuResize<<>>(scale, input, inputWidth, output, outputWidth, outputHeight); 87 | 88 | return CUDA(cudaGetLastError()); 89 | } 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /utils/cuda/cudaResize.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __CUDA_RESIZE_H__ 24 | #define __CUDA_RESIZE_H__ 25 | 26 | 27 | #include "cudaUtility.h" 28 | 29 | 30 | /** 31 | * Function for increasing or decreasing the size of an image on the GPU. 32 | * @ingroup util 33 | */ 34 | cudaError_t cudaResize( float* input, size_t inputWidth, size_t inputHeight, 35 | float* output, size_t outputWidth, size_t outputHeight ); 36 | 37 | 38 | /** 39 | * Function for increasing or decreasing the size of an image on the GPU. 40 | * @ingroup util 41 | */ 42 | cudaError_t cudaResizeRGBA( float4* input, size_t inputWidth, size_t inputHeight, 43 | float4* output, size_t outputWidth, size_t outputHeight ); 44 | 45 | 46 | 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /utils/cuda/cudaUtility.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __CUDA_UTILITY_H_ 24 | #define __CUDA_UTILITY_H_ 25 | 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | 33 | /** 34 | * Execute a CUDA call and print out any errors 35 | * @return the original cudaError_t result 36 | * @ingroup util 37 | */ 38 | #define CUDA(x) cudaCheckError((x), #x, __FILE__, __LINE__) 39 | 40 | /** 41 | * Evaluates to true on success 42 | * @ingroup util 43 | */ 44 | #define CUDA_SUCCESS(x) (CUDA(x) == cudaSuccess) 45 | 46 | /** 47 | * Evaluates to true on failure 48 | * @ingroup util 49 | */ 50 | #define CUDA_FAILED(x) (CUDA(x) != cudaSuccess) 51 | 52 | /** 53 | * Return from the boolean function if CUDA call fails 54 | * @ingroup util 55 | */ 56 | #define CUDA_VERIFY(x) if(CUDA_FAILED(x)) return false; 57 | 58 | /** 59 | * LOG_CUDA string. 60 | * @ingroup util 61 | */ 62 | #define LOG_CUDA "[cuda] " 63 | 64 | /* 65 | * define this if you want all cuda calls to be printed 66 | */ 67 | //#define CUDA_TRACE 68 | 69 | 70 | 71 | /** 72 | * cudaCheckError 73 | * @ingroup util 74 | */ 75 | inline cudaError_t cudaCheckError(cudaError_t retval, const char* txt, const char* file, int line ) 76 | { 77 | #if !defined(CUDA_TRACE) 78 | if( retval == cudaSuccess) 79 | return cudaSuccess; 80 | #endif 81 | 82 | //int activeDevice = -1; 83 | //cudaGetDevice(&activeDevice); 84 | 85 | //Log("[cuda] device %i - %s\n", activeDevice, txt); 86 | 87 | printf(LOG_CUDA "%s\n", txt); 88 | 89 | 90 | if( retval != cudaSuccess ) 91 | { 92 | printf(LOG_CUDA " %s (error %u) (hex 0x%02X)\n", cudaGetErrorString(retval), retval, retval); 93 | printf(LOG_CUDA " %s:%i\n", file, line); 94 | } 95 | 96 | return retval; 97 | } 98 | 99 | 100 | /** 101 | * iDivUp 102 | * @ingroup util 103 | */ 104 | inline __device__ __host__ int iDivUp( int a, int b ) { return (a % b != 0) ? (a / b + 1) : (a / b); } 105 | 106 | 107 | 108 | #endif 109 | -------------------------------------------------------------------------------- /utils/data/fontmapA.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/utils/data/fontmapA.png -------------------------------------------------------------------------------- /utils/data/fontmapB.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/RoboND-DeepRL-Project/fc3809f97edc57f1dc437800ecfb1cf0a67c12da/utils/data/fontmapB.png -------------------------------------------------------------------------------- /utils/display/gl-display-test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | file(GLOB glDisplayTestSources *.cpp) 3 | file(GLOB glDisplayTestIncludes *.h ) 4 | 5 | add_executable(gl-display-test ${glDisplayTestSources}) 6 | target_link_libraries(gl-display-test jetson-utils) 7 | -------------------------------------------------------------------------------- /utils/display/gl-display-test/gl-display-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "glDisplay.h" 24 | #include "glTexture.h" 25 | 26 | #include "cudaFont.h" 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | 33 | #define TEXTURE_WIDTH 1024 34 | #define TEXTURE_HEIGHT 1024 35 | 36 | 37 | bool signal_recieved = false; 38 | 39 | void sig_handler(int signo) 40 | { 41 | if( signo == SIGINT ) 42 | { 43 | printf("received SIGINT\n"); 44 | signal_recieved = true; 45 | } 46 | } 47 | 48 | 49 | int main( int argc, char** argv ) 50 | { 51 | printf("gl-display-test\n args (%i): ", argc); 52 | 53 | for( int i=0; i < argc; i++ ) 54 | printf("%i [%s] ", i, argv[i]); 55 | 56 | printf("\n"); 57 | 58 | 59 | if( signal(SIGINT, sig_handler) == SIG_ERR ) 60 | printf("\ncan't catch SIGINT\n"); 61 | 62 | 63 | /* 64 | * create openGL window 65 | */ 66 | glDisplay* display = glDisplay::Create("NVIDIA OpenGL Display Test"); 67 | 68 | if( !display ) 69 | { 70 | printf("\ngl-display: failed to create openGL display\n"); 71 | return 0; 72 | } 73 | 74 | 75 | /* 76 | * initialize default test texture pattern 77 | */ 78 | const size_t texSize = TEXTURE_WIDTH * TEXTURE_HEIGHT * sizeof(float4); 79 | 80 | float4* texIn = (float4*)malloc(texSize); 81 | 82 | if( !texIn ) 83 | { 84 | printf("failed to allocate texture initialization input\n"); 85 | return 0; 86 | } 87 | 88 | for( uint32_t y=0; y < TEXTURE_HEIGHT; y++ ) 89 | for( uint32_t x=0; x < TEXTURE_WIDTH; x++ ) 90 | texIn[y*TEXTURE_WIDTH+x] = make_float4(0.0f, float(x)/float(TEXTURE_WIDTH), float(y)/float(TEXTURE_HEIGHT), 1.0f); 91 | 92 | 93 | /* 94 | * allocate openGL texture 95 | */ 96 | glTexture* texture = glTexture::Create(TEXTURE_WIDTH, TEXTURE_HEIGHT, GL_RGBA32F_ARB/*GL_RGBA8*/, texIn); 97 | 98 | if( !texture ) 99 | { 100 | printf("gl-display: failed to create openGL texture\n"); 101 | return 0; 102 | } 103 | 104 | 105 | /* 106 | * create font 107 | */ 108 | cudaFont* font = cudaFont::Create(); 109 | 110 | if( !font ) 111 | printf("failed to create cudaFont object\n"); 112 | 113 | 114 | /* 115 | * rendering loop 116 | */ 117 | while( !signal_recieved ) 118 | { 119 | // update display 120 | if( display != NULL ) 121 | { 122 | display->UserEvents(); 123 | display->BeginRender(); 124 | 125 | if( texture != NULL ) 126 | { 127 | void* tex_map = texture->MapCUDA(); 128 | 129 | CUDA(cudaMemset(tex_map, 0, texture->GetSize())); 130 | 131 | if( tex_map != NULL ) 132 | { 133 | if( font != NULL ) 134 | { 135 | char str[256]; 136 | sprintf(str, "AaBbCcDdEeFfGgHhIiJjKkLlMmNn 123456890"); 137 | 138 | font->RenderOverlay((float4*)tex_map, (float4*)tex_map, texture->GetWidth(), texture->GetHeight(), 139 | str, 0, 0, make_float4(0.0f, 0.75f, 1.0f, 255.0f)); 140 | } 141 | 142 | CUDA(cudaDeviceSynchronize()); 143 | texture->Unmap(); 144 | } 145 | 146 | texture->Render(50,50); 147 | } 148 | 149 | display->EndRender(); 150 | } 151 | } 152 | 153 | printf("\ngl-display: un-initializing video device\n"); 154 | 155 | 156 | /* 157 | * close the window 158 | */ 159 | if( display != NULL ) 160 | { 161 | delete display; 162 | display = NULL; 163 | } 164 | 165 | printf("gl-display: video device has been un-initialized.\n"); 166 | printf("gl-display: this concludes the test of the video device.\n"); 167 | return 0; 168 | } 169 | -------------------------------------------------------------------------------- /utils/display/glDisplay.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __GL_VIEWPORT_H__ 24 | #define __GL_VIEWPORT_H__ 25 | 26 | 27 | #include "glUtility.h" 28 | #include "glTexture.h" 29 | 30 | #include 31 | 32 | 33 | /** 34 | * OpenGL display window / video viewer 35 | */ 36 | class glDisplay 37 | { 38 | public: 39 | /** 40 | * Create a new maximized openGL display window. 41 | * @param r default background RGBA color, red component (0.0-1.0f) 42 | * @param g default background RGBA color, green component (0.0-1.0f) 43 | * @param b default background RGBA color, blue component (0.0-1.0f) 44 | * @param a default background RGBA color, alpha component (0.0-1.0f) 45 | */ 46 | static glDisplay* Create( float r=0.05f, float g=0.05f, float b=0.05f, float a=1.0f ); 47 | 48 | /** 49 | * Create a new maximized openGL display window. 50 | * @param title window title bar label string 51 | * @param r default background RGBA color, red component (0.0-1.0f) 52 | * @param g default background RGBA color, green component (0.0-1.0f) 53 | * @param b default background RGBA color, blue component (0.0-1.0f) 54 | * @param a default background RGBA color, alpha component (0.0-1.0f) 55 | */ 56 | static glDisplay* Create( const char* title, float r=0.05f, float g=0.05f, float b=0.05f, float a=1.0f ); 57 | 58 | /** 59 | * Destroy window 60 | */ 61 | ~glDisplay(); 62 | 63 | /** 64 | * Clear window and begin rendering a frame. 65 | */ 66 | void BeginRender(); 67 | 68 | /** 69 | * Finish rendering and refresh / flip the backbuffer. 70 | */ 71 | void EndRender(); 72 | 73 | /** 74 | * Process UI events. 75 | */ 76 | void UserEvents(); 77 | 78 | /** 79 | * UI event handler. 80 | */ 81 | void onEvent( uint msg, int a, int b ); 82 | 83 | /** 84 | * Set the window title string. 85 | */ 86 | void SetTitle( const char* str ); 87 | 88 | /** 89 | * Set the background color. 90 | * @param r background RGBA color, red component (0.0-1.0f) 91 | * @param g background RGBA color, green component (0.0-1.0f) 92 | * @param b background RGBA color, blue component (0.0-1.0f) 93 | * @param a background RGBA color, alpha component (0.0-1.0f) 94 | */ 95 | inline void SetBackgroundColor( float r, float g, float b, float a ) { mBgColor[0] = r; mBgColor[1] = g; mBgColor[2] = b; mBgColor[3] = a; } 96 | 97 | /** 98 | * Get the average frame time (in milliseconds). 99 | */ 100 | inline float GetFPS() { return 1000000000.0f / mAvgTime; } 101 | 102 | protected: 103 | glDisplay(); 104 | 105 | bool initWindow(); 106 | bool initGL(); 107 | 108 | static const int screenIdx = 0; 109 | 110 | Display* mDisplayX; 111 | Screen* mScreenX; 112 | XVisualInfo* mVisualX; 113 | Window mWindowX; 114 | GLXContext mContextGL; 115 | 116 | uint32_t mWidth; 117 | uint32_t mHeight; 118 | 119 | timespec mLastTime; 120 | float mAvgTime; 121 | float mBgColor[4]; 122 | }; 123 | 124 | #endif 125 | 126 | -------------------------------------------------------------------------------- /utils/display/glTexture.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __GL_TEXTURE_H__ 24 | #define __GL_TEXTURE_H__ 25 | 26 | 27 | #include "cudaUtility.h" 28 | #include "cuda_gl_interop.h" 29 | 30 | 31 | /** 32 | * OpenGL texture 33 | */ 34 | class glTexture 35 | { 36 | public: 37 | static glTexture* Create( uint32_t width, uint32_t height, uint32_t format, void* data=NULL ); 38 | ~glTexture(); 39 | 40 | void Render( float x, float y ); 41 | void Render( float x, float y, float width, float height ); 42 | void Render( const float4& rect ); 43 | 44 | inline uint32_t GetID() const { return mID; } 45 | inline uint32_t GetWidth() const { return mWidth; } 46 | inline uint32_t GetHeight() const { return mHeight; } 47 | inline uint32_t GetFormat() const { return mFormat; } 48 | inline uint32_t GetSize() const { return mSize; } 49 | 50 | void* MapCUDA(); 51 | void Unmap(); 52 | 53 | bool UploadCPU( void* data ); 54 | 55 | private: 56 | glTexture(); 57 | bool init(uint32_t width, uint32_t height, uint32_t format, void* data); 58 | 59 | uint32_t mID; 60 | uint32_t mDMA; 61 | uint32_t mWidth; 62 | uint32_t mHeight; 63 | uint32_t mFormat; 64 | uint32_t mSize; 65 | 66 | cudaGraphicsResource* mInteropCUDA; 67 | void* mInteropHost; 68 | void* mInteropDevice; 69 | }; 70 | 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /utils/display/glUtility.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __OPENGL_UTILITY_H 24 | #define __OPENGL_UTILITY_H 25 | 26 | 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | 33 | /** 34 | * LOG_GL printf prefix. 35 | * @ingroup renderGL 36 | */ 37 | #define LOG_GL "[openGL] " 38 | 39 | 40 | 41 | #define GL(x) { x; glCheckError( #x, __FILE__, __LINE__ ); } 42 | #define GL_VERIFY(x) { x; if(glCheckError( #x, __FILE__, __LINE__ )) return false; } 43 | #define GL_VERIFYN(x) { x; if(glCheckError( #x, __FILE__, __LINE__ )) return NULL; } 44 | #define GL_CHECK(msg) { glCheckError(msg, __FILE__, __LINE__); } 45 | 46 | 47 | /** 48 | * openGL error logging macros 49 | * @ingroup renderGL 50 | */ 51 | inline bool glCheckError(const char* msg, const char* file, int line) 52 | { 53 | GLenum err = glGetError(); 54 | 55 | if( err == GL_NO_ERROR ) 56 | return false; 57 | 58 | const char* e = NULL; 59 | 60 | switch(err) 61 | { 62 | case GL_INVALID_ENUM: e = "invalid enum"; break; 63 | case GL_INVALID_VALUE: e = "invalid value"; break; 64 | case GL_INVALID_OPERATION: e = "invalid operation"; break; 65 | case GL_STACK_OVERFLOW: e = "stack overflow"; break; 66 | case GL_STACK_UNDERFLOW: e = "stack underflow"; break; 67 | case GL_OUT_OF_MEMORY: e = "out of memory"; break; 68 | #ifdef GL_TABLE_TOO_LARGE_EXT 69 | case GL_TABLE_TOO_LARGE_EXT: e = "table too large"; break; 70 | #endif 71 | #ifdef GL_TEXTURE_TOO_LARGE_EXT 72 | case GL_TEXTURE_TOO_LARGE_EXT: e = "texture too large"; break; 73 | #endif 74 | default: e = "unknown error"; 75 | } 76 | 77 | printf(LOG_GL "Error %i - '%s'\n", (uint)err, e); 78 | printf(LOG_GL " %s::%i\n", file, line ); 79 | printf(LOG_GL " %s\n", msg ); 80 | 81 | return true; 82 | } 83 | 84 | 85 | /** 86 | * openGL error check + logging 87 | * @ingroup renderGL 88 | */ 89 | inline bool glCheckError(const char* msg) 90 | { 91 | GLenum err = glGetError(); 92 | 93 | if( err == GL_NO_ERROR ) 94 | return false; 95 | 96 | const char* e = NULL; 97 | 98 | switch(err) 99 | { 100 | case GL_INVALID_ENUM: e = "invalid enum"; break; 101 | case GL_INVALID_VALUE: e = "invalid value"; break; 102 | case GL_INVALID_OPERATION: e = "invalid operation"; break; 103 | case GL_STACK_OVERFLOW: e = "stack overflow"; break; 104 | case GL_STACK_UNDERFLOW: e = "stack underflow"; break; 105 | case GL_OUT_OF_MEMORY: e = "out of memory"; break; 106 | #ifdef GL_TABLE_TOO_LARGE_EXT 107 | case GL_TABLE_TOO_LARGE_EXT: e = "table too large"; break; 108 | #endif 109 | #ifdef GL_TEXTURE_TOO_LARGE_EXT 110 | case GL_TEXTURE_TOO_LARGE_EXT: e = "texture too large"; break; 111 | #endif 112 | default: e = "unknown error"; 113 | } 114 | 115 | printf(LOG_GL "%s (error %i - %s)\n", msg, (uint)err, e); 116 | return true; 117 | } 118 | 119 | 120 | 121 | #define GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX 0x9048 122 | #define GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX 0x9049 123 | 124 | 125 | /** 126 | * glPrintFreeMem 127 | * @ingroup renderGL 128 | */ 129 | inline void glPrintFreeMem() 130 | { 131 | GLint total_mem_kb = 0; 132 | GLint cur_avail_mem_kb = 0; 133 | 134 | glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX, &total_mem_kb); 135 | glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb); 136 | 137 | printf("[openGL] GPU memory free %i / %i kb\n", cur_avail_mem_kb, total_mem_kb); 138 | } 139 | 140 | 141 | 142 | #endif 143 | 144 | -------------------------------------------------------------------------------- /utils/input/devInput.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "devInput.h" 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | 34 | // constructor 35 | InputDevices::InputDevices() 36 | { 37 | mKeyboard = NULL; 38 | mJoystick = NULL; 39 | mDebug = false; 40 | } 41 | 42 | 43 | // destructor 44 | InputDevices::~InputDevices() 45 | { 46 | 47 | } 48 | 49 | 50 | // Create 51 | InputDevices* InputDevices::Create() 52 | { 53 | InputDevices* mgr = new InputDevices(); 54 | 55 | mgr->mKeyboard = KeyboardDevice::Create(); 56 | 57 | if( !mgr->mKeyboard ) 58 | return NULL; 59 | 60 | mgr->mJoystick = JoystickDevice::Create(); 61 | 62 | return mgr; 63 | } 64 | 65 | 66 | // Poll 67 | bool InputDevices::Poll( uint32_t timeout ) 68 | { 69 | if( !mKeyboard && !mJoystick ) 70 | return false; 71 | 72 | if( mKeyboard != NULL ) 73 | mKeyboard->Poll(timeout); 74 | 75 | if( mJoystick != NULL ) 76 | mJoystick->Poll(timeout); 77 | 78 | return true; 79 | } 80 | 81 | 82 | // Path used to look for input devices 83 | #define DEV_PATH "/dev/input" 84 | //#define DEV_PATH "/dev/input/by-path" 85 | 86 | 87 | // Filter for the AutoDevProbe scandir on /dev/input. 88 | // @param dir The current directory entry provided by scandir. 89 | // @return Non-zero if the given directory entry starts with "event", or zero otherwise. 90 | static int is_event_device(const struct dirent *dir) 91 | { 92 | return strncmp("event", dir->d_name, 5) == 0; 93 | //if( strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0 ) 94 | // return 0; 95 | 96 | //return 1; 97 | } 98 | 99 | 100 | // Enumerate 101 | void InputDevices::Enumerate( DeviceList& devices ) 102 | { 103 | struct dirent **namelist; 104 | int ndev = scandir(DEV_PATH, &namelist, is_event_device, versionsort); 105 | 106 | if (ndev <= 0) 107 | { 108 | return; 109 | } 110 | 111 | printf("Available devices (%i):\n", ndev); 112 | 113 | for(int i = 0; i < ndev; i++) 114 | { 115 | char fname[64]; 116 | char name[256] = "???"; 117 | 118 | snprintf(fname, sizeof(fname), "%s/%s", DEV_PATH, namelist[i]->d_name); 119 | int fd = open(fname, O_RDONLY); 120 | 121 | if (fd < 0) 122 | continue; 123 | 124 | if( ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0 ) 125 | continue; 126 | 127 | printf("%s: '%s'\n", fname, name); 128 | 129 | close(fd); 130 | free(namelist[i]); 131 | 132 | devices.push_back(std::pair(fname, name)); 133 | } 134 | } 135 | 136 | 137 | // FindPathByName 138 | std::string InputDevices::FindPathByName( const char* name ) 139 | { 140 | if( !name ) 141 | return ""; 142 | 143 | DeviceList list; 144 | Enumerate(list); 145 | 146 | const size_t numDevices = list.size(); 147 | 148 | if( numDevices == 0 ) 149 | return ""; 150 | 151 | for( size_t n=0; n < numDevices; n++ ) 152 | { 153 | if( strcasecmp(name, list[n].second.c_str()) == 0 ) 154 | return list[n].first; 155 | } 156 | 157 | return ""; 158 | } 159 | 160 | 161 | // Debug 162 | void InputDevices::Debug( bool enable ) 163 | { 164 | mDebug = enable; 165 | 166 | if( mKeyboard != NULL ) 167 | mKeyboard->Debug(enable); 168 | 169 | if( mJoystick != NULL ) 170 | mJoystick->Debug(enable); 171 | } 172 | 173 | 174 | 175 | -------------------------------------------------------------------------------- /utils/input/devInput.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __DEV_INPUT_H__ 24 | #define __DEV_INPUT_H__ 25 | 26 | #include "devKeyboard.h" 27 | #include "devJoystick.h" 28 | 29 | #include 30 | #include 31 | 32 | 33 | /** 34 | * Typedef of device pairs 35 | */ 36 | typedef std::vector< std::pair > DeviceList; 37 | 38 | 39 | /** 40 | * Input device manager 41 | */ 42 | class InputDevices 43 | { 44 | public: 45 | /** 46 | * Create device 47 | */ 48 | static InputDevices* Create(); 49 | 50 | /** 51 | * Destructor 52 | */ 53 | ~InputDevices(); 54 | 55 | /** 56 | * Poll the devices for updates 57 | */ 58 | bool Poll( uint32_t timeout=0 ); 59 | 60 | /** 61 | * Retrieve the keyboard device 62 | */ 63 | inline KeyboardDevice* GetKeyboard() const { return mKeyboard; } 64 | 65 | /** 66 | * Retrieve the gamepad device 67 | */ 68 | inline JoystickDevice* GetJoystick() const { return mJoystick; } 69 | 70 | /** 71 | * Scan /dev/input for devices 72 | */ 73 | static void Enumerate( DeviceList& devices ); 74 | 75 | /** 76 | * Find /dev/input path by device name 77 | */ 78 | static std::string FindPathByName( const char* name ); 79 | 80 | /** 81 | * Enable/disable verbose logging 82 | */ 83 | void Debug( bool enabled=true ); 84 | 85 | protected: 86 | // constructor 87 | InputDevices(); 88 | 89 | KeyboardDevice* mKeyboard; 90 | JoystickDevice* mJoystick; 91 | 92 | bool mDebug; 93 | }; 94 | 95 | #endif 96 | 97 | -------------------------------------------------------------------------------- /utils/input/devJoystick.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "devJoystick.h" 24 | #include "devInput.h" 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | // "linux/input-event-codes.h" 31 | 32 | 33 | // constructor 34 | JoystickDevice::JoystickDevice() 35 | { 36 | mFD = -1; 37 | mDebug = false; 38 | 39 | memset(mAxisRaw, 0, sizeof(mAxisRaw)); 40 | memset(mAxisNorm, 0, sizeof(mAxisNorm)); 41 | } 42 | 43 | 44 | // destructor 45 | JoystickDevice::~JoystickDevice() 46 | { 47 | 48 | } 49 | 50 | 51 | // Create 52 | JoystickDevice* JoystickDevice::Create( const char* name ) 53 | { 54 | std::string path = InputDevices::FindPathByName(name); 55 | 56 | if( path.length() == 0 ) 57 | { 58 | printf("joystick -- failed to find path for device '%s'\n", name); 59 | return NULL; 60 | } 61 | 62 | const int fd = open(path.c_str(), O_RDONLY); 63 | 64 | if( fd == -1 ) 65 | { 66 | printf("joystick -- failed to open %s\n", path.c_str()); 67 | return NULL; 68 | } 69 | 70 | JoystickDevice* joy = new JoystickDevice(); 71 | 72 | joy->mFD = fd; 73 | joy->mPath = path; 74 | 75 | printf("joystick -- opened device %s\n", path.c_str()); 76 | return joy; 77 | } 78 | 79 | 80 | // Poll 81 | bool JoystickDevice::Poll( uint32_t timeout ) 82 | { 83 | const uint32_t max_ev = 64; 84 | struct input_event ev[max_ev]; 85 | 86 | fd_set fds; 87 | FD_ZERO(&fds); 88 | FD_SET(mFD, &fds); 89 | 90 | struct timeval tv; 91 | 92 | tv.tv_sec = 0; 93 | tv.tv_usec = timeout*1000; 94 | 95 | const int result = select(mFD + 1, &fds, NULL, NULL, &tv); 96 | 97 | if( result == -1 ) 98 | { 99 | printf("joystick -- select() failed (errno=%i) (%s)\n", errno, strerror(errno)); 100 | return false; 101 | } 102 | else if( result == 0 ) 103 | { 104 | if( mDebug && timeout > 0 ) 105 | printf("joystick -- select() timed out...\n"); 106 | 107 | return false; // timeout, not necessarily an error (TRY_AGAIN) 108 | } 109 | 110 | const int bytesRead = read(mFD, ev, sizeof(struct input_event) * max_ev); 111 | 112 | if( bytesRead < (int)sizeof(struct input_event) ) 113 | { 114 | printf("joystick -- read() expected %d bytes, got %d\n", (int)sizeof(struct input_event), bytesRead); 115 | return false; 116 | } 117 | 118 | const int num_ev = bytesRead / sizeof(struct input_event); 119 | 120 | for( int i = 0; i < num_ev; i++ ) 121 | { 122 | if( ev[i].type == EV_ABS ) 123 | { 124 | if( ev[i].code >= MAX_AXIS ) 125 | continue; 126 | 127 | mAxisRaw[ev[i].code] = ev[i].value; 128 | 129 | if( mDebug ) 130 | printf("joystick -- axis %i, value %i type=%i\n", (int)ev[i].code, ev[i].value, (int)ev[i].type); 131 | } 132 | else if( ev[i].type != 0 ) 133 | { 134 | if( mDebug ) 135 | printf("joystick -- event %i, code %i, value %i\n", (int)ev[i].type, (int)ev[i].code, ev[i].value); 136 | } 137 | } 138 | 139 | return true; 140 | } 141 | 142 | 143 | // Debug 144 | void JoystickDevice::Debug( bool enable ) 145 | { 146 | mDebug = enable; 147 | } 148 | 149 | -------------------------------------------------------------------------------- /utils/input/devJoystick.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __DEV_JOYSTICK_H__ 24 | #define __DEV_JOYSTICK_H__ 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | 32 | /** 33 | * Joystick device 34 | */ 35 | class JoystickDevice 36 | { 37 | public: 38 | /** 39 | * Create device 40 | */ 41 | static JoystickDevice* Create( const char* device="Microntek USB Joystick " ); 42 | 43 | /** 44 | * Destructor 45 | */ 46 | ~JoystickDevice(); 47 | 48 | /** 49 | * Poll the device for updates 50 | */ 51 | bool Poll( uint32_t timeout=0 ); 52 | 53 | /** 54 | * Enable/disable verbose logging 55 | */ 56 | void Debug( bool enabled=true ); 57 | 58 | protected: 59 | // constructor 60 | JoystickDevice(); 61 | 62 | static const int MAX_AXIS = 256; 63 | 64 | float mAxisNorm[MAX_AXIS]; 65 | int mAxisRaw[MAX_AXIS]; 66 | int mFD; 67 | bool mDebug; 68 | 69 | std::string mPath; 70 | }; 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /utils/input/devKeyboard.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include "devKeyboard.h" 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | 31 | // constructor 32 | KeyboardDevice::KeyboardDevice() 33 | { 34 | mFD = -1; 35 | mDebug = false; 36 | 37 | memset(mKeyMap, 0, sizeof(mKeyMap)); 38 | } 39 | 40 | 41 | // destructor 42 | KeyboardDevice::~KeyboardDevice() 43 | { 44 | 45 | } 46 | 47 | 48 | // Create 49 | KeyboardDevice* KeyboardDevice::Create( const char* path ) 50 | { 51 | if( !path ) 52 | return NULL; 53 | 54 | const int fd = open(path, O_RDONLY); 55 | 56 | if( fd == -1 ) 57 | { 58 | printf("keyboard -- failed to open %s\n", path); 59 | return NULL; 60 | } 61 | 62 | KeyboardDevice* kbd = new KeyboardDevice(); 63 | 64 | kbd->mFD = fd; 65 | kbd->mPath = path; 66 | 67 | return kbd; 68 | } 69 | 70 | 71 | // Poll 72 | bool KeyboardDevice::Poll( uint32_t timeout ) 73 | { 74 | const uint32_t max_ev = 64; 75 | struct input_event ev[max_ev]; 76 | 77 | fd_set fds; 78 | FD_ZERO(&fds); 79 | FD_SET(mFD, &fds); 80 | 81 | struct timeval tv; 82 | 83 | tv.tv_sec = 0; 84 | tv.tv_usec = timeout*1000; 85 | 86 | const int result = select(mFD + 1, &fds, NULL, NULL, &tv); 87 | 88 | if( result == -1 ) 89 | { 90 | printf("keyboard -- select() failed (errno=%i) (%s)\n", errno, strerror(errno)); 91 | return false; 92 | } 93 | else if( result == 0 ) 94 | { 95 | if( mDebug && timeout > 0 ) 96 | printf("keyboard -- select() timed out...\n"); 97 | 98 | return false; // timeout, not necessarily an error (TRY_AGAIN) 99 | } 100 | 101 | const int bytesRead = read(mFD, ev, sizeof(struct input_event) * max_ev); 102 | 103 | if( bytesRead < (int)sizeof(struct input_event) ) 104 | { 105 | printf("keyboard -- read() expected %d bytes, got %d\n", (int)sizeof(struct input_event), bytesRead); 106 | return false; 107 | } 108 | 109 | const int num_ev = bytesRead / sizeof(struct input_event); 110 | 111 | for( int i = 0; i < num_ev; i++ ) 112 | { 113 | if( ev[i].type != EV_KEY ) 114 | continue; 115 | 116 | if( ev[i].value < 0 || ev[i].value > 2 ) 117 | continue; 118 | 119 | if( ev[i].code >= MAX_KEYS ) 120 | continue; 121 | 122 | mKeyMap[ev[i].code] = (ev[i].value == 0) ? false : true; 123 | 124 | if( mDebug ) 125 | printf("keyboard -- code %02u value %i\n", ev[i].code, ev[i].value); 126 | } 127 | 128 | return true; 129 | } 130 | 131 | 132 | // KeyDown 133 | bool KeyboardDevice::KeyDown( uint32_t code ) const 134 | { 135 | if( code >= MAX_KEYS ) 136 | return false; 137 | 138 | return mKeyMap[code]; 139 | } 140 | 141 | 142 | // Debug 143 | void KeyboardDevice::Debug( bool enable ) 144 | { 145 | mDebug = enable; 146 | } 147 | 148 | -------------------------------------------------------------------------------- /utils/input/devKeyboard.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __DEV_KEYBOARD_H__ 24 | #define __DEV_KEYBOARD_H__ 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | 34 | /** 35 | * Keyboard device 36 | */ 37 | class KeyboardDevice 38 | { 39 | public: 40 | /** 41 | * Create device 42 | */ 43 | static KeyboardDevice* Create( const char* path="/dev/input/by-path/platform-i8042-serio-0-event-kbd" ); 44 | 45 | /** 46 | * Destructor 47 | */ 48 | ~KeyboardDevice(); 49 | 50 | /** 51 | * Poll the device for updates 52 | */ 53 | bool Poll( uint32_t timeout=0 ); 54 | 55 | /** 56 | * Check if a particular key is pressed 57 | */ 58 | bool KeyDown( uint32_t code ) const; 59 | 60 | /** 61 | * Enable/disable verbose logging 62 | */ 63 | void Debug( bool enabled=true ); 64 | 65 | protected: 66 | // constructor 67 | KeyboardDevice(); 68 | 69 | static const int MAX_KEYS = 256; 70 | 71 | int mKeyMap[MAX_KEYS]; 72 | int mFD; 73 | bool mDebug; 74 | 75 | std::string mPath; 76 | }; 77 | 78 | #endif 79 | 80 | -------------------------------------------------------------------------------- /utils/loadImage.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __IMAGE_LOADER_H_ 24 | #define __IMAGE_LOADER_H_ 25 | 26 | 27 | #include "cudaUtility.h" 28 | 29 | 30 | /** 31 | * Load a color image from disk into CUDA memory with alpha. 32 | * This function loads the image into shared CPU/GPU memory, using the functions from cudaMappedMemory.h 33 | * 34 | * @param filename Path to the image file on disk. 35 | * @param cpu Pointer to CPU buffer allocated containing the image. 36 | * @param gpu Pointer to CUDA device buffer residing on GPU containing image. 37 | * @param width Variable containing width in pixels of the image. 38 | * @param height Variable containing height in pixels of the image. 39 | * 40 | * @ingroup util 41 | */ 42 | bool loadImageRGBA( const char* filename, float4** cpu, float4** gpu, int* width, int* height ); 43 | 44 | 45 | /** 46 | * Save an image to disk 47 | * @ingroup util 48 | */ 49 | bool saveImageRGBA( const char* filename, float4* cpu, int width, int height, float max_pixel=255.0f ); 50 | 51 | 52 | /** 53 | * Load a color image from disk into CUDA memory. 54 | * This function loads the image into shared CPU/GPU memory, using the functions from cudaMappedMemory.h 55 | * 56 | * @param filename Path to the image file on disk. 57 | * @param cpu Pointer to CPU buffer allocated containing the image. 58 | * @param gpu Pointer to CUDA device buffer residing on GPU containing image. 59 | * @param width Variable containing width in pixels of the image. 60 | * @param height Variable containing height in pixels of the image. 61 | * 62 | * @ingroup util 63 | */ 64 | bool loadImageRGB( const char* filename, float3** cpu, float3** gpu, int* width, int* height, const float3& mean=make_float3(0,0,0) ); 65 | 66 | 67 | /** 68 | * Load a color image from disk into CUDA memory. 69 | * This function loads the image into shared CPU/GPU memory, using the functions from cudaMappedMemory.h 70 | * 71 | * @param filename Path to the image file on disk. 72 | * @param cpu Pointer to CPU buffer allocated containing the image. 73 | * @param gpu Pointer to CUDA device buffer residing on GPU containing image. 74 | * @param width Variable containing width in pixels of the image. 75 | * @param height Variable containing height in pixels of the image. 76 | * 77 | * @ingroup util 78 | */ 79 | bool loadImageBGR( const char* filename, float3** cpu, float3** gpu, int* width, int* height, const float3& mean=make_float3(0,0,0) ); 80 | 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /utils/pi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __PI_UTILS_H_ 24 | #define __PI_UTILS_H_ 25 | 26 | 27 | /** 28 | * Constant representing PI 29 | */ 30 | #define PI 3.1415926535897932384626433832795f 31 | 32 | 33 | /** 34 | * Constant representing two PI 35 | */ 36 | #define TWO_PI 6.283185307179586476925286766559f 37 | 38 | 39 | /** 40 | * Constant representing PI over two 41 | */ 42 | #define PI_OVER_TWO 1.5707963267948966192313216916398f; 43 | 44 | 45 | /** 46 | * Constant for converting from degrees to radians 47 | */ 48 | #define DEG_TO_RAD 0.017453292519943295769236907684886f 49 | 50 | 51 | /** 52 | * Constant for converting from radians to degrees 53 | */ 54 | #define RAD_TO_DEG 57.295779513082320876798154814105f 55 | 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /utils/rand.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef __RANDOM_UTILS_H_ 24 | #define __RANDOM_UTILS_H_ 25 | 26 | 27 | #include 28 | #include 29 | 30 | 31 | /** 32 | * Generate a floating-point random number within the specified range 33 | */ 34 | inline float randf( float range_min=0.0f, float range_max=1.0f ) { return ((float(rand()) / RAND_MAX) * (range_max - range_min)) + range_min; } 35 | 36 | 37 | /** 38 | * Generate a random integer within the specified range 39 | */ 40 | inline int rand( int range_min, int range_max ) { const float r = (randf() * (range_max - range_min)) + range_min; return int(r); } 41 | 42 | 43 | /** 44 | * Seed the random number generator based on the system time 45 | */ 46 | inline void srand_time() { srand(time(NULL)); } 47 | 48 | 49 | /** 50 | * Seed the random number generator from an unsigned integer value 51 | * (this standard function implemented by stdlib.h) 52 | */ 53 | //void srand( unsigned int seed ); 54 | 55 | 56 | #endif 57 | 58 | --------------------------------------------------------------------------------