Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Cpp scoped lock #199

Closed
wants to merge 4 commits into from

5 participants

@vokimon

Using safer scoped lock in cpp wrapper (Resource Acquisition is Initialization idiom) and simplifying some methods using early exits which are simpler to do with scoped locks.

RyanGordon and others added some commits
@RyanGordon RyanGordon Committing initial revisions of the LabView Wrapper Signed-off-by: Ry…
…anGordon <rygordon4@gmail.com>
7feae2b
@KaiRitterbusch KaiRitterbusch fix: cmake now skips cpp example according to the BUILD_EXAMPLE option
Signed-off-by: Kai Ritterbusch <kai.ritterbusch@gmx.de>
605ae05
@RyanGordon RyanGordon * Removing lvdemo.c. Only need to build the dll
* Much improved error handling, better and cleaner VI's and wrapper functionality
* Now uses IMAQ for image processing. Much more extensive library for machine vision.

Signed-off-By: Ryan Gordon <rygordon4@gmail.com>
18252c9
David García Garzón Using scoped locks in cpp wrapper
I added scoped locks to the Mutex class which use the RIIA idiom
to ensure that any lock is unlocked whatever the control path you
take. This enables the simplification of some methods by doing
early exists.

Signed-off-by: David García Garzón <david.garcia@barcelonamedia.org>
27319a5
@qdot
Collaborator

Cherry picked 27319a5 into unstable

@piedar
Collaborator

This has been in master for a while.

@piedar piedar closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 23, 2011
  1. @RyanGordon @qdot

    Committing initial revisions of the LabView Wrapper Signed-off-by: Ry…

    RyanGordon authored qdot committed
    …anGordon <rygordon4@gmail.com>
  2. @KaiRitterbusch @qdot

    fix: cmake now skips cpp example according to the BUILD_EXAMPLE option

    KaiRitterbusch authored qdot committed
    Signed-off-by: Kai Ritterbusch <kai.ritterbusch@gmx.de>
  3. @RyanGordon @qdot

    * Removing lvdemo.c. Only need to build the dll

    RyanGordon authored qdot committed
    * Much improved error handling, better and cleaner VI's and wrapper functionality
    * Now uses IMAQ for image processing. Much more extensive library for machine vision.
    
    Signed-off-By: Ryan Gordon <rygordon4@gmail.com>
Commits on Feb 3, 2011
  1. Using scoped locks in cpp wrapper

    David García Garzón authored
    I added scoped locks to the Mutex class which use the RIIA idiom
    to ensure that any lock is unlocked whatever the control path you
    take. This enables the simplification of some methods by doing
    early exists.
    
    Signed-off-by: David García Garzón <david.garcia@barcelonamedia.org>
This page is out of date. Refresh to see the latest.
Showing with 251 additions and 22 deletions.
  1. +7 −0 CMakeLists.txt
  2. +4 −0 wrappers/cpp/CMakeLists.txt
  3. +27 −22 wrappers/cpp/cppview.cpp
  4. +16 −0 wrappers/labview/CMakeLists.txt
  5. +19 −0 wrappers/labview/README
  6. +93 −0 wrappers/labview/libfreenect_lv.c
  7. +57 −0 wrappers/labview/libfreenect_lv.h
  8. BIN  wrappers/labview/lvlib/VIs/example.vi
  9. BIN  wrappers/labview/lvlib/VIs/get depth frame.vi
  10. BIN  wrappers/labview/lvlib/VIs/get image frame.vi
  11. BIN  wrappers/labview/lvlib/VIs/get tilt state mks.vi
  12. BIN  wrappers/labview/lvlib/VIs/get tilt state.vi
  13. BIN  wrappers/labview/lvlib/VIs/get_depth_imaq.vi
  14. BIN  wrappers/labview/lvlib/VIs/get_image_imaq.vi
  15. BIN  wrappers/labview/lvlib/VIs/set led.vi
  16. BIN  wrappers/labview/lvlib/VIs/set tilt degs.vi
  17. BIN  wrappers/labview/lvlib/VIs/stop.vi
  18. BIN  wrappers/labview/lvlib/datatypes/freenect_depth_format.ctl
  19. BIN  wrappers/labview/lvlib/datatypes/freenect_led_options.ctl
  20. BIN  wrappers/labview/lvlib/datatypes/freenect_raw_tilt_state.ctl
  21. BIN  wrappers/labview/lvlib/datatypes/freenect_rgb_24bit.ctl
  22. BIN  wrappers/labview/lvlib/datatypes/freenect_tilt_status_code.ctl
  23. BIN  wrappers/labview/lvlib/datatypes/freenect_video_format.ctl
  24. BIN  wrappers/labview/lvlib/freenect.dll
  25. BIN  wrappers/labview/lvlib/freenect_lv.dll
  26. BIN  wrappers/labview/lvlib/freenect_sync.dll
  27. +28 −0 wrappers/labview/lvlib/libfreenect.lvlib
  28. BIN  wrappers/labview/lvlib/pthreadVC2.dll
View
7 CMakeLists.txt
@@ -55,6 +55,9 @@ OPTION(BUILD_CPP "Build C++ Library (currently header only)" ON)
OPTION(BUILD_CV "Build OpenCV wrapper" OFF)
OPTION(BUILD_AS3_SERVER "Build the Actionscript 3 Server Example" OFF)
OPTION(BUILD_PYTHON "Build Python extension" OFF)
+IF(WIN32)
+ OPTION(BUILD_LABVIEW "Build LabView extension" OFF)
+ENDIF()
IF(PROJECT_OS_LINUX)
OPTION(BUILD_CPACK "Build an RPM or DEB using CPack" OFF)
ENDIF(PROJECT_OS_LINUX)
@@ -137,6 +140,10 @@ IF(BUILD_PYTHON)
add_subdirectory (wrappers/python)
ENDIF()
+IF(BUILD_LABVIEW)
+ add_subdirectory (wrappers/labview)
+ENDIF()
+
######################################################################################
# Extras
######################################################################################
View
4 wrappers/cpp/CMakeLists.txt
@@ -1,6 +1,8 @@
INSTALL(FILES libfreenect.hpp
DESTINATION include)
+IF(BUILD_EXAMPLES)
+
set(CMAKE_C_FLAGS "-Wall")
if (WIN32)
@@ -36,3 +38,5 @@ endif()
install (TARGETS cppview
DESTINATION bin)
+
+ENDIF()
View
49 wrappers/cpp/cppview.cpp
@@ -55,6 +55,21 @@ class Mutex {
void unlock() {
pthread_mutex_unlock( &m_mutex );
}
+
+ class ScopedLock
+ {
+ Mutex & _mutex;
+ public:
+ ScopedLock(Mutex & mutex)
+ : _mutex(mutex)
+ {
+ _mutex.lock();
+ }
+ ~ScopedLock()
+ {
+ _mutex.unlock();
+ }
+ };
private:
pthread_mutex_t m_mutex;
};
@@ -74,15 +89,14 @@ class MyFreenectDevice : public Freenect::FreenectDevice {
//~MyFreenectDevice(){}
// Do not call directly even in child
void VideoCallback(void* _rgb, uint32_t timestamp) {
- m_rgb_mutex.lock();
+ Mutex::ScopedLock lock(m_rgb_mutex);
uint8_t* rgb = static_cast<uint8_t*>(_rgb);
std::copy(rgb, rgb+getVideoBufferSize(), m_buffer_video.begin());
m_new_rgb_frame = true;
- m_rgb_mutex.unlock();
};
// Do not call directly even in child
void DepthCallback(void* _depth, uint32_t timestamp) {
- m_depth_mutex.lock();
+ Mutex::ScopedLock lock(m_depth_mutex);
uint16_t* depth = static_cast<uint16_t*>(_depth);
for( unsigned int i = 0 ; i < FREENECT_FRAME_PIX ; i++) {
int pval = m_gamma[depth[i]];
@@ -126,32 +140,23 @@ class MyFreenectDevice : public Freenect::FreenectDevice {
}
}
m_new_depth_frame = true;
- m_depth_mutex.unlock();
}
bool getRGB(std::vector<uint8_t> &buffer) {
- m_rgb_mutex.lock();
- if(m_new_rgb_frame) {
- buffer.swap(m_buffer_video);
- m_new_rgb_frame = false;
- m_rgb_mutex.unlock();
- return true;
- } else {
- m_rgb_mutex.unlock();
+ Mutex::ScopedLock lock(m_rgb_mutex);
+ if (!m_new_rgb_frame)
return false;
- }
+ buffer.swap(m_buffer_video);
+ m_new_rgb_frame = false;
+ return true;
}
bool getDepth(std::vector<uint8_t> &buffer) {
- m_depth_mutex.lock();
- if(m_new_depth_frame) {
- buffer.swap(m_buffer_depth);
- m_new_depth_frame = false;
- m_depth_mutex.unlock();
- return true;
- } else {
- m_depth_mutex.unlock();
+ Mutex::ScopedLock lock(m_depth_mutex);
+ if (!m_new_depth_frame)
return false;
- }
+ buffer.swap(m_buffer_depth);
+ m_new_depth_frame = false;
+ return true;
}
private:
View
16 wrappers/labview/CMakeLists.txt
@@ -0,0 +1,16 @@
+######################################################################################
+# C Labview Interface
+######################################################################################
+set_source_files_properties(libfreenect_lv.c PROPERTIES LANGUAGE CXX)
+
+add_library (freenect_lv SHARED libfreenect_lv.c)
+set_target_properties (freenect_lv PROPERTIES
+ VERSION ${PROJECT_VER}
+ SOVERSION ${PROJECT_APIVER})
+
+target_link_libraries (freenect_lv freenect freenect_sync)
+
+install (TARGETS freenect_lv
+ DESTINATION "${PROJECT_LIBRARY_INSTALL_DIR}")
+install (FILES "libfreenect_lv.h"
+ DESTINATION ${PROJECT_INCLUDE_INSTALL_DIR})
View
19 wrappers/labview/README
@@ -0,0 +1,19 @@
+General information:
+These drivers are still very very experimental and while they work, they are not 100% stable. There will probably be problems we haven't discovered yet. Please keep this in mind!
+
+Needed first before this works:
+1) Windows machine with a (preferably) new version of LabVIEW (8.6+). Older versions untested.
+2) inf files (driver files) Please see: http://openkinect.org/wiki/Getting_Started_Windows and follow the Driver Installation instructions. You don't need the dependencies or the other instructions unless you actually want to mess with and compile the code that makes up the dlls.
+
+Known issues:
+1) Only 11Bit Depth Format and RGB Format work currently. Support for the others is being added soon.
+2) It is IMPORTANT to make sure the Kinect is turned off (either by pressing the the "stop" button in example.vi or running "stop.vi" or manually removing physical power from the Kinect before working on the code. It's been seen that LabView will close without warning or explanation if these steps are not taken. If LabView crashes when you try and run it, unplug your Kinect physically, wait a few seconds, and plug it back in. Restarting your computer or LabVIEW may also help. Save your work often.
+
+Information:
+- The depth frame image in example.vi uses an arbitrary gray-scale false-color rendering. The raw resultant array can be accessed from the vi "get depth frame.vi" included in libfreenect.lvlib. Please see: http://openkinect.org/wiki/Protocol_Documentation for more information
+- The RGB frame image in example.vi can be directly accessed from the raw resultant array in "get image frame.vi" included in libfreenect.lvlib. Please see: http://openkinect.org/wiki/Imaging_Information for more information
+- The dlls in this project are 32bit compiled with MSVC 2010
+- Only has been tested with 1 Kinect
+- Timing is very important to frame rate speed. Please be considerate to cycles when developing algorithms on top of these drivers
+
+If you have problems, email me at rygordon4@gmail.com or find me on irc.freenode.net #openkinect under the RyanGordon alias.
View
93 wrappers/labview/libfreenect_lv.c
@@ -0,0 +1,93 @@
+/*
+ * This file is part of the OpenKinect Project. http://www.openkinect.org
+ *
+ * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
+ * for details.
+ *
+ * Ryan Gordon <rygordon4@gmail.com>
+ *
+ * This code is licensed to you under the terms of the Apache License, version
+ * 2.0, or, at your option, the terms of the GNU General Public License,
+ * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
+ * or the following URLs:
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.gnu.org/licenses/gpl-2.0.txt
+ *
+ * If you redistribute this file in source form, modified or unmodified, you
+ * may:
+ * 1) Leave this header intact and distribute it under the same terms,
+ * accompanying it with the APACHE20 and GPL20 files, or
+ * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
+ * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
+ * In all cases you must keep the copyright notice intact and include a copy
+ * of the CONTRIB file.
+ *
+ * Binary distributions must follow the binary distribution requirements of
+ * either License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "libfreenect.h"
+#include "../c_sync/libfreenect_sync.h"
+#include "libfreenect_lv.h"
+
+EXPORT short *freenect_lv_get_depth_frame(int index, freenect_depth_format fmt)
+{
+ short *depth = 0;
+ unsigned int timestamp;
+
+ if(freenect_sync_get_depth((void**)&depth, &timestamp, index, fmt) < 0)
+ {
+ return NULL;
+ }
+
+ return depth;
+}
+
+EXPORT char *freenect_lv_get_image_frame(int index, freenect_video_format fmt)
+{
+ char *rgb = 0;
+ unsigned int timestamp;
+
+ if(freenect_sync_get_video((void**)&rgb, &timestamp, index, fmt) < 0)
+ {
+ return NULL;
+ }
+ return rgb;
+}
+
+EXPORT int freenect_lv_set_tilt_degs(int angle, int index)
+{
+ return freenect_sync_set_tilt_degs(angle, index);
+}
+
+EXPORT freenect_raw_tilt_state *freenect_lv_get_tilt_state(int index)
+{
+ freenect_raw_tilt_state *state = 0;
+
+ freenect_sync_get_tilt_state(&state, index);
+
+ return state;
+}
+
+EXPORT void freenect_lv_get_tilt_state_mks(double *dx, double *dy, double *dz, int index)
+{
+ freenect_raw_tilt_state *state = 0;
+
+ freenect_sync_get_tilt_state(&state, index);
+ freenect_get_mks_accel(state, dx, dy, dz);
+}
+
+EXPORT void freenect_lv_set_led(freenect_led_options led, int index)
+{
+ freenect_sync_set_led(led, index);
+}
+
+EXPORT void freenect_lv_stop()
+{
+ freenect_sync_stop();
+}
View
57 wrappers/labview/libfreenect_lv.h
@@ -0,0 +1,57 @@
+/*
+ * This file is part of the OpenKinect Project. http://www.openkinect.org
+ *
+ * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
+ * for details.
+ *
+ * Ryan Gordon <rygordon4@gmail.com>
+ *
+ * This code is licensed to you under the terms of the Apache License, version
+ * 2.0, or, at your option, the terms of the GNU General Public License,
+ * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
+ * or the following URLs:
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.gnu.org/licenses/gpl-2.0.txt
+ *
+ * If you redistribute this file in source form, modified or unmodified, you
+ * may:
+ * 1) Leave this header intact and distribute it under the same terms,
+ * accompanying it with the APACHE20 and GPL20 files, or
+ * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
+ * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
+ * In all cases you must keep the copyright notice intact and include a copy
+ * of the CONTRIB file.
+ *
+ * Binary distributions must follow the binary distribution requirements of
+ * either License.
+ */
+
+#ifndef LIBFREENECT__LV_H
+#define LIBFREENECT_LV_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "libfreenect.h"
+#include "../c_sync/libfreenect_sync.h"
+
+#ifndef _WIN32
+#define EXPORT
+#else
+#define EXPORT extern "C" __declspec(dllexport)
+#endif
+
+EXPORT short *freenect_lv_get_depth_frame(int index, freenect_depth_format fmt);
+EXPORT char *freenect_lv_get_image_frame(int index, freenect_video_format fmt);
+EXPORT int freenect_lv_set_tilt_degs(int angle, int index);
+EXPORT freenect_raw_tilt_state *freenect_lv_get_tilt_state(int index);
+EXPORT void freenect_lv_get_tilt_state_mks(double *dx, double *dy, double *dz, int index);
+EXPORT void freenect_lv_set_led(freenect_led_options led, int index);
+EXPORT void freenect_lv_stop();
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //
View
BIN  wrappers/labview/lvlib/VIs/example.vi
Binary file not shown
View
BIN  wrappers/labview/lvlib/VIs/get depth frame.vi
Binary file not shown
View
BIN  wrappers/labview/lvlib/VIs/get image frame.vi
Binary file not shown
View
BIN  wrappers/labview/lvlib/VIs/get tilt state mks.vi
Binary file not shown
View
BIN  wrappers/labview/lvlib/VIs/get tilt state.vi
Binary file not shown
View
BIN  wrappers/labview/lvlib/VIs/get_depth_imaq.vi
Binary file not shown
View
BIN  wrappers/labview/lvlib/VIs/get_image_imaq.vi
Binary file not shown
View
BIN  wrappers/labview/lvlib/VIs/set led.vi
Binary file not shown
View
BIN  wrappers/labview/lvlib/VIs/set tilt degs.vi
Binary file not shown
View
BIN  wrappers/labview/lvlib/VIs/stop.vi
Binary file not shown
View
BIN  wrappers/labview/lvlib/datatypes/freenect_depth_format.ctl
Binary file not shown
View
BIN  wrappers/labview/lvlib/datatypes/freenect_led_options.ctl
Binary file not shown
View
BIN  wrappers/labview/lvlib/datatypes/freenect_raw_tilt_state.ctl
Binary file not shown
View
BIN  wrappers/labview/lvlib/datatypes/freenect_rgb_24bit.ctl
Binary file not shown
View
BIN  wrappers/labview/lvlib/datatypes/freenect_tilt_status_code.ctl
Binary file not shown
View
BIN  wrappers/labview/lvlib/datatypes/freenect_video_format.ctl
Binary file not shown
View
BIN  wrappers/labview/lvlib/freenect.dll
Binary file not shown
View
BIN  wrappers/labview/lvlib/freenect_lv.dll
Binary file not shown
View
BIN  wrappers/labview/lvlib/freenect_sync.dll
Binary file not shown
View
28 wrappers/labview/lvlib/libfreenect.lvlib
@@ -0,0 +1,28 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<Library LVVersion="8608002">
+ <Property Name="NI.Lib.Icon" Type="Bin">#'#!!A!!!!)!"1!&amp;!!!-!%!!!@````]!!!!"!!%!!!+G!!!*Q(C=\:9\DB."%)&lt;`:4&gt;9E@E'S#*T7&amp;=Q#9%T3ZSALG#=L&lt;2#=LI3$EJ;2%$G!TCR%)(4CD&lt;X&amp;8S&amp;Y?NWYXWA(1M*"!(6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ctl" Type="VI" URL="../datatypes/freenect_depth_format.ctl"/>
+ <Item Name="freenect_led_options.ctl" Type="VI" URL="../datatypes/freenect_led_options.ctl"/>
+ <Item Name="freenect_raw_tilt_state.ctl" Type="VI" URL="../datatypes/freenect_raw_tilt_state.ctl"/>
+ <Item Name="freenect_tilt_status_code.ctl" Type="VI" URL="../datatypes/freenect_tilt_status_code.ctl"/>
+ <Item Name="freenect_video_format.ctl" Type="VI" URL="../datatypes/freenect_video_format.ctl"/>
+ <Item Name="freenect_rgb_24bit.ctl" Type="VI" URL="../datatypes/freenect_rgb_24bit.ctl"/>
+ </Item>
+ <Item Name="dll" Type="Folder">
+ <Item Name="freenect_lv.dll" Type="Document" URL="../freenect_lv.dll"/>
+ <Item Name="get depth frame.vi" Type="VI" URL="../VIs/get depth frame.vi"/>
+ <Item Name="get image frame.vi" Type="VI" URL="../VIs/get image frame.vi"/>
+ <Item Name="get tilt state.vi" Type="VI" URL="../VIs/get tilt state.vi"/>
+ <Item Name="get tilt state mks.vi" Type="VI" URL="../VIs/get tilt state mks.vi"/>
+ <Item Name="set led.vi" Type="VI" URL="../VIs/set led.vi"/>
+ <Item Name="set tilt degs.vi" Type="VI" URL="../VIs/set tilt degs.vi"/>
+ <Item Name="stop.vi" Type="VI" URL="../VIs/stop.vi"/>
+ </Item>
+ <Item Name="imaq" Type="Folder">
+ <Item Name="get_image_imaq.vi" Type="VI" URL="../VIs/get_image_imaq.vi"/>
+ <Item Name="get_depth_imaq.vi" Type="VI" URL="../VIs/get_depth_imaq.vi"/>
+ </Item>
+ <Item Name="example.vi" Type="VI" URL="../VIs/example.vi"/>
+</Library>
View
BIN  wrappers/labview/lvlib/pthreadVC2.dll
Binary file not shown
Something went wrong with that request. Please try again.