Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

OS X: iSight exposure locking support

  • Loading branch information...
commit 79c1af37f853abc42198e44ead18bcab2ad76711 1 parent 633dd62
@thp authored
View
6 CMakeLists.txt
@@ -92,14 +92,12 @@ if (PSMOVE_USE_LOCAL_OPENCV)
set(INFO_BUILD_TRACKER "Yes (with local OpenCV)")
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
- find_library(AVFOUNDATION AVFoundation)
find_library(QUARTZCORE QuartzCore)
find_library(APPKIT AppKit)
find_library(QTKIT QTKit)
list(APPEND PSMOVEAPI_TRACKER_REQUIRED_LIBS
opencv_core opencv_highgui opencv_imgproc
libjpeg libpng zlib stdc++
- ${AVFOUNDATION}
${QUARTZCORE}
${APPKIT}
${QTKIT})
@@ -125,6 +123,9 @@ IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
list(APPEND PSMOVEAPI_REQUIRED_LIBS ${COREFOUNDATION})
list(APPEND PSMOVEAPI_REQUIRED_LIBS ${IOBLUETOOTH})
+ find_library(AVFOUNDATION AVFoundation)
+ list(APPEND PSMOVEAPI_REQUIRED_LIBS ${AVFOUNDATION})
+
set(HIDAPI_SRC ${PSMOVEAPI_SOURCE_DIR}/external/hidapi/mac/hid.c)
# Build Universal Binaries for OS X
@@ -134,6 +135,7 @@ IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
${PSMOVEAPI_SOURCE_DIR}/src/platform/psmove_osxsupport.m)
list(APPEND PSMOVEAPI_TRACKER_PLATFORM_SRC
+ ${PSMOVEAPI_SOURCE_DIR}/src/tracker/platform/psmove_osxsupport.m
${PSMOVEAPI_SOURCE_DIR}/src/tracker/platform/camera_control_macosx.c)
ELSEIF(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
list(APPEND PSMOVEAPI_REQUIRED_LIBS setupapi bthprops kernel32 ws2_32 winmm)
View
59 README.osx
@@ -0,0 +1,59 @@
+
+Background information about iSight exposure locking
+====================================================
+
+On Mac OS X, we have problems getting the PS Eye to work reliably with OpenCV.
+Because of that, it makes sense to use other cameras like the iSight camera
+built into most (all?) MacBook (Pro) computers and iMacs. Unfortunately, there
+is no API for controlling the exposure directly. It is possible to lock the
+exposure to a given value so that the exposure isn't continously corrected.
+
+How it works normally (continous autoexposure):
+
+ 1. The camera device determines a good first exposure when started
+ 2. While frames are captured, the exposure is continously modified
+ (e.g. when you hold the controller near the iSight, the exposure is
+ automatically lowered to accomodate the brighter camera image)
+
+How it works with locked exposure:
+
+ 1. The camera device determines a good first exposure when started
+ 2. The exposure set after opening the device will stay the same
+
+So what we need to do is the following:
+
+ 1. Light up the controller to a very bright color
+ 2. Move the controller directly in front of the camera (the sphere
+ can actually touch the iSight camera and "block its sight")
+ 3. Open the camera device (the first exposure is determined here)
+ 4. As soon as the camera device is opened (and the exposure set),
+ lock the exposure so that it doesn't change anymore
+ 5. Move the controller away from the camera, proceed with normal
+ initialization procedure (blinking calibration, etc..)
+
+The call to locking has been integrated into the tracker library, the
+exposure will be automatically locked when the tracker library is started.
+
+The exposure seems to be locked until the next reboot in my experiments,
+but the exposure is determined every time the camera device is opened (but
+will stay locked after opening the device).
+
+Because of this, the calibration procedure on Mac OS X is a bit different
+to other platforms, because we have to light up the controller, put it in
+front of the iSight, open the camera device, wait for a good exposure, and
+only then continue with normal calibration. This has been implemented in
+examples/c/test_tracker.c - the console output will tell you what to do:
+
+ 1. Start the application
+ 2. Move the controller that lights up close to the iSight
+ 3. Press the Move button on this controller
+ 4. Keep the controller close to the iSight while the camera is opened
+ 5. Move the controller away from the iSight and press the Move button
+
+Thanks to Raphaël de Courville who had the initial idea for the exposure
+locking, did some experimentation with it and provided an first draft of the
+implementation that is now integrated in simplified form in the library.
+
+
+Thomas Perl, 2012-10-05
+
View
53 examples/c/test_tracker.c
@@ -40,15 +40,33 @@
#include "psmove.h"
#include "psmove_tracker.h"
+
+void
+wait_for_button(PSMove *move, int button)
+{
+ /* Wait for press */
+ while ((psmove_get_buttons(move) & button) == 0) {
+ psmove_poll(move);
+ psmove_update_leds(move);
+ }
+
+ /* Wait for release */
+ while ((psmove_get_buttons(move) & button) != 0) {
+ psmove_poll(move);
+ psmove_update_leds(move);
+ }
+}
+
+
int main(int arg, char** args) {
int i;
int count = psmove_count_connected();
PSMove* controllers[count];
- printf("%s", "### Trying to init PSMoveTracker...");
- PSMoveTracker* tracker = psmove_tracker_new();
- printf("%s\n", "OK");
printf("### Found %d controllers.\n", count);
+ if (count == 0) {
+ return 1;
+ }
void *frame;
unsigned char r, g, b;
@@ -58,7 +76,35 @@ int main(int arg, char** args) {
printf("Opening controller %d\n", i);
controllers[i] = psmove_connect_by_id(i);
assert(controllers[i] != NULL);
+ }
+
+#ifdef __APPLE__
+ PSMove *move = controllers[0];
+ psmove_set_leds(move, 255, 255, 255);
+ psmove_update_leds(move);
+#endif
+
+#ifdef __APPLE__
+ printf("Cover the iSight camera with the sphere and press the Move button\n");
+ wait_for_button(move, Btn_MOVE);
+ psmove_set_leds(move, 0, 0, 0);
+ psmove_update_leds(move);
+ psmove_set_leds(move, 255, 255, 255);
+ psmove_update_leds(move);
+#endif
+
+ fprintf(stderr, "Trying to init PSMoveTracker...");
+ PSMoveTracker* tracker = psmove_tracker_new();
+ fprintf(stderr, "OK\n");
+#ifdef __APPLE__
+ printf("Move the controller away and press the Move button\n");
+ wait_for_button(move, Btn_MOVE);
+ psmove_set_leds(move, 0, 0, 0);
+ psmove_update_leds(move);
+#endif
+
+ for (i=0; i<count; i++) {
while (1) {
printf("Calibrating controller %d...", i);
fflush(stdout);
@@ -71,7 +117,6 @@ int main(int arg, char** args) {
printf("ERROR - retrying\n");
}
}
-
}
while ((cvWaitKey(1) & 0xFF) != 27) {
View
11 src/tracker/platform/camera_control_macosx.c
@@ -28,7 +28,7 @@
#include "../camera_control.h"
-#include <stdio.h>
+#include "psmove_osxsupport.h"
void
camera_control_set_parameters(CameraControl* cc,
@@ -37,22 +37,19 @@ camera_control_set_parameters(CameraControl* cc,
int wbRed, int wbGreen, int wbBlue,
int contrast, int brightness)
{
- /* TODO: Implement for Mac OS X */
- fprintf(stderr, "<noimpl> camera_control_set_parameters\n");
+ macosx_camera_set_exposure_lock(1);
}
void
camera_control_backup_system_settings(CameraControl* cc,
const char* file)
{
- /* TODO: Implement for Mac OS X */
- fprintf(stderr, "<noimpl> camera_control_backup_system_settings\n");
+ // Do nothing
}
void camera_control_restore_system_settings(CameraControl* cc,
const char* file)
{
- /* TODO: Implement for Mac OS X */
- fprintf(stderr, "<noimpl> camera_control_restore_system_settings\n");
+ // Do nothing
}
View
45 src/tracker/platform/psmove_osxsupport.h
@@ -0,0 +1,45 @@
+
+ /**
+ * PS Move API - An interface for the PS Move Motion Controller
+ * Copyright (c) 2012 Thomas Perl <m@thp.io>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ **/
+
+
+#ifndef PSMOVE_TRACKER_OSXSUPPORT_H
+#define PSMOVE_TRACKER_OSXSUPPORT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int
+macosx_camera_set_exposure_lock(int locked);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
View
97 src/tracker/platform/psmove_osxsupport.m
@@ -0,0 +1,97 @@
+
+ /**
+ * PS Move API - An interface for the PS Move Motion Controller
+ * Copyright (c) 2012 Thomas Perl <m@thp.io>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ **/
+
+#include "psmove_osxsupport.h"
+#include "../../psmove_private.h"
+
+#import <Foundation/NSAutoreleasePool.h>
+#import <AVFoundation/AVFoundation.h>
+
+#define CAMEXPOSURE_DEBUG(msg, ...) \
+ psmove_PRINTF("CAM EXPOSURE", msg, ## __VA_ARGS__)
+
+int
+macosx_camera_set_exposure_lock(int locked)
+{
+ /**
+ * Exposure locking for the iSight camera on Apple computers.
+ * Based on initial experimentations and code by Raphaël de Courville.
+ **/
+
+ int result = 0;
+
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ AVCaptureSession *session = [[AVCaptureSession alloc] init];
+ AVCaptureDevice *device = nil;
+ AVCaptureDeviceInput *input = nil;
+
+ /* Configure the capture session */
+ [session beginConfiguration];
+
+ device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
+ if (!device) {
+ device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeMuxed];
+ }
+
+ input = [AVCaptureDeviceInput deviceInputWithDevice: device error: nil];
+ if (!input) {
+ CAMEXPOSURE_DEBUG("Cannot get AVCaptureDeviceInput");
+ }
+
+ [session addInput: input];
+ [session commitConfiguration];
+
+ /* Lock the device (for configuration), set exposure mode, unlock */
+ if ([device lockForConfiguration: nil]) {
+ AVCaptureExposureMode mode = locked ?
+ AVCaptureExposureModeLocked :
+ AVCaptureExposureModeContinuousAutoExposure;
+
+ if ([device isExposureModeSupported: mode]) {
+ [device setExposureMode: mode];
+ } else {
+ CAMEXPOSURE_DEBUG("Device does not support requested exposure mode");
+ }
+
+ [device unlockForConfiguration];
+ result = 1;
+ } else {
+ CAMEXPOSURE_DEBUG("Cannot lock device for configuration");
+ }
+
+ [session beginConfiguration];
+ [session removeInput:input];
+ [session commitConfiguration];
+
+ [session autorelease];
+
+ [pool release];
+ return result;
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.