Permalink
Browse files

Preliminary support for Kinect for Windows hardware, at the expense o…

…f LED/motor support for normal devices and messiness. Needs review and more testing, as well as some way to stub out the LED/motor calls.

Signed-off-by: Drew Fisher <drew.m.fisher@gmail.com>
  • Loading branch information...
1 parent 5795655 commit 06e48e5d4328f96bf61613953bab2f45a459a1fb @zarvox zarvox committed Feb 12, 2012
Showing with 56 additions and 59 deletions.
  1. +2 −45 examples/glview.c
  2. +1 −3 examples/hiview.c
  3. +20 −5 src/cameras.c
  4. +9 −0 src/freenect_internal.h
  5. +24 −6 src/usb_libusb10.c
View
@@ -67,7 +67,6 @@ GLuint gl_rgb_tex;
freenect_context *f_ctx;
freenect_device *f_dev;
int freenect_angle = 0;
-int freenect_led;
freenect_video_format requested_format = FREENECT_VIDEO_RGB;
freenect_video_format current_format = FREENECT_VIDEO_RGB;
@@ -179,29 +178,6 @@ void keyPressed(unsigned char key, int x, int y)
freenect_angle = -30;
}
}
- if (key == '1') {
- freenect_set_led(f_dev,LED_GREEN);
- }
- if (key == '2') {
- freenect_set_led(f_dev,LED_RED);
- }
- if (key == '3') {
- freenect_set_led(f_dev,LED_YELLOW);
- }
- if (key == '4') {
- freenect_set_led(f_dev,LED_BLINK_GREEN);
- }
- if (key == '5') {
- // 5 is the same as 4
- freenect_set_led(f_dev,LED_BLINK_GREEN);
- }
- if (key == '6') {
- freenect_set_led(f_dev,LED_BLINK_RED_YELLOW);
- }
- if (key == '0') {
- freenect_set_led(f_dev,LED_OFF);
- }
- freenect_set_tilt_degs(f_dev,freenect_angle);
}
void ReSizeGLScene(int Width, int Height)
@@ -335,10 +311,6 @@ void rgb_cb(freenect_device *dev, void *rgb, uint32_t timestamp)
void *freenect_threadfunc(void *arg)
{
- int accelCount = 0;
-
- freenect_set_tilt_degs(f_dev,freenect_angle);
- freenect_set_led(f_dev,LED_RED);
freenect_set_depth_callback(f_dev, depth_cb);
freenect_set_video_callback(f_dev, rgb_cb);
freenect_set_video_mode(f_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, current_format));
@@ -348,22 +320,7 @@ void *freenect_threadfunc(void *arg)
freenect_start_depth(f_dev);
freenect_start_video(f_dev);
- printf("'w'-tilt up, 's'-level, 'x'-tilt down, '0'-'6'-select LED mode, 'f'-video format\n");
-
while (!die && freenect_process_events(f_ctx) >= 0) {
- //Throttle the text output
- if (accelCount++ >= 2000)
- {
- accelCount = 0;
- freenect_raw_tilt_state* state;
- freenect_update_tilt_state(f_dev);
- state = freenect_get_tilt_state(f_dev);
- double dx,dy,dz;
- freenect_get_mks_accel(state, &dx, &dy, &dz);
- printf("\r raw acceleration: %4d %4d %4d mks acceleration: %4f %4f %4f", state->accelerometer_x, state->accelerometer_y, state->accelerometer_z, dx, dy, dz);
- fflush(stdout);
- }
-
if (requested_format != current_format) {
freenect_stop_video(f_dev);
freenect_set_video_mode(f_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, requested_format));
@@ -411,8 +368,8 @@ int main(int argc, char **argv)
return 1;
}
- freenect_set_log_level(f_ctx, FREENECT_LOG_DEBUG);
- freenect_select_subdevices(f_ctx, (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA));
+ freenect_set_log_level(f_ctx, FREENECT_LOG_SPEW);
+ freenect_select_subdevices(f_ctx, (freenect_device_flags)(FREENECT_DEVICE_CAMERA));
int nr_devices = freenect_num_devices (f_ctx);
printf ("Number of devices found: %d\n", nr_devices);
View
@@ -68,7 +68,6 @@ GLuint gl_rgb_tex;
freenect_context *f_ctx;
freenect_device *f_dev;
-int freenect_led;
freenect_video_format requested_format = FREENECT_VIDEO_RGB;
freenect_video_format current_format = FREENECT_VIDEO_RGB;
@@ -360,7 +359,6 @@ void video_cb(freenect_device *dev, void *rgb, uint32_t timestamp)
void *freenect_threadfunc(void *arg)
{
- freenect_set_led(f_dev,LED_RED);
freenect_set_depth_callback(f_dev, depth_cb);
freenect_set_video_callback(f_dev, video_cb);
freenect_set_video_mode(f_dev, freenect_find_video_mode(current_resolution, current_format));
@@ -436,7 +434,7 @@ int main(int argc, char **argv)
}
freenect_set_log_level(f_ctx, FREENECT_LOG_DEBUG);
- freenect_select_subdevices(f_ctx, (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA));
+ freenect_select_subdevices(f_ctx, FREENECT_DEVICE_CAMERA);
int nr_devices = freenect_num_devices (f_ctx);
printf ("Number of devices found: %d\n", nr_devices);
View
@@ -903,13 +903,27 @@ static int freenect_fetch_zero_plane_info(freenect_device *dev)
uint16_t cmd[5] = {0}; // Offset is the only field in this command, and it's 0
int res;
- res = send_cmd(dev, 0x04, cmd, 10, reply, 322); //OPCODE_GET_FIXED_PARAMS = 4,
- if (res != 322) {
- FN_ERROR("freenect_fetch_zero_plane_info: send_cmd read %d bytes (expected 322)\n", res);
+
+ int expected_len = 0;
+ int struct_offset = 0;
+ switch(dev->hwrev) {
+ case HWREV_XBOX360_0:
+ expected_len = 322;
+ struct_offset = 94;
+ break;
+ case HWREV_K4W_0:
+ expected_len = 334;
+ struct_offset = 94;
+ break;
+ }
+
+ res = send_cmd(dev, 0x04, cmd, 10, reply, expected_len); //OPCODE_GET_FIXED_PARAMS = 4,
+ if (res != expected_len) {
+ FN_ERROR("freenect_fetch_zero_plane_info: send_cmd read %d bytes (expected %d)\n", res, expected_len);
return -1;
}
- memcpy(&(dev->registration.zero_plane_info), reply + 94, sizeof(dev->registration.zero_plane_info));
+ memcpy(&(dev->registration.zero_plane_info), reply + struct_offset, sizeof(dev->registration.zero_plane_info));
union {
uint32_t ui;
float f;
@@ -1261,7 +1275,8 @@ int freenect_set_video_mode(freenect_device* dev, const freenect_frame_mode mode
dev->video_resolution = res;
// Now that we've changed video format and resolution, we need to update
// registration tables.
- freenect_fetch_reg_info(dev);
+ if (res == FREENECT_RESOLUTION_MEDIUM)
+ freenect_fetch_reg_info(dev);
return 0;
}
View
@@ -132,6 +132,13 @@ static inline int32_t fn_le32s(int32_t s)
#define PID_NUI_AUDIO 0x02ad
#define PID_NUI_CAMERA 0x02ae
#define PID_NUI_MOTOR 0x02b0
+#define PID_K4W_CAMERA 0x02bf
+#define PID_K4W_AUDIO 0x02be
+
+typedef enum {
+ HWREV_XBOX360_0 = 0,
+ HWREV_K4W_0 = 1,
+} hardware_revision;
typedef struct {
int running;
@@ -203,6 +210,8 @@ struct _freenect_device {
freenect_device *next;
void *user_data;
+ hardware_revision hwrev;
+
// Cameras
fnusb_dev usb_cam;
fnusb_isoc_stream depth_isoc;
View
@@ -48,7 +48,7 @@ FN_INTERNAL int fnusb_num_devices(fnusb_ctx *ctx)
int r = libusb_get_device_descriptor (devs[i], &desc);
if (r < 0)
continue;
- if (desc.idVendor == VID_MICROSOFT && desc.idProduct == PID_NUI_CAMERA)
+ if (desc.idVendor == VID_MICROSOFT && (desc.idProduct == PID_NUI_CAMERA || desc.idProduct == PID_K4W_CAMERA))
nr++;
}
libusb_free_device_list (devs, 1);
@@ -77,7 +77,7 @@ FN_INTERNAL int fnusb_list_device_attributes(fnusb_ctx *ctx, struct freenect_dev
int r = libusb_get_device_descriptor (devs[i], &desc);
if (r < 0)
continue;
- if (desc.idVendor == VID_MICROSOFT && desc.idProduct == PID_NUI_CAMERA) {
+ if (desc.idVendor == VID_MICROSOFT && (desc.idProduct == PID_NUI_CAMERA || desc.idProduct == PID_K4W_CAMERA)) {
// Verify that a serial number exists to query. If not, don't touch the device.
if (desc.iSerialNumber == 0) {
continue;
@@ -121,6 +121,7 @@ FN_INTERNAL int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx)
int res;
if (!usb_ctx) {
res = libusb_init(&ctx->ctx);
+ libusb_set_debug(ctx->ctx, 3);
if (res >= 0) {
ctx->should_free_ctx = 1;
return 0;
@@ -191,7 +192,7 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
continue;
// Search for the camera
- if ((ctx->enabled_subdevices & FREENECT_DEVICE_CAMERA) && !dev->usb_cam.dev && desc.idProduct == PID_NUI_CAMERA) {
+ if ((ctx->enabled_subdevices & FREENECT_DEVICE_CAMERA) && !dev->usb_cam.dev && (desc.idProduct == PID_NUI_CAMERA || desc.idProduct == PID_K4W_CAMERA)) {
// If the index given by the user matches our camera index
if (nr_cam == index) {
res = libusb_open (devs[i], &dev->usb_cam.dev);
@@ -220,13 +221,30 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
dev->usb_cam.dev = NULL;
break;
}
+ if (desc.idProduct == PID_NUI_CAMERA) {
+ dev->hwrev = HWREV_XBOX360_0;
+ FN_SPEW("Opened Kinect for Xbox360 camera\n");
+ } else if (desc.idProduct == PID_K4W_CAMERA) {
+ dev->hwrev = HWREV_K4W_0;
+ FN_SPEW("Opened Kinect for Windows camera\n");
+ // Set alternate interface setting 1 to enable the two isochronous endpoints
+ res = libusb_set_interface_alt_setting(dev->usb_cam.dev, 0, 1);
+ if (res != 0) {
+ FN_ERROR("Failed to set alternate interface #1 for K4W: %d\n", res);
+ libusb_close(dev->usb_cam.dev);
+ dev->usb_cam.dev = NULL;
+ break;
+ }
+ } else {
+ FN_ERROR("Unknown hardware revision - fix fnusb_open_subdevices()\n");
+ }
} else {
nr_cam++;
}
}
// Search for the motor
- if ((ctx->enabled_subdevices & FREENECT_DEVICE_MOTOR) && !dev->usb_motor.dev && desc.idProduct == PID_NUI_MOTOR) {
+ if ((ctx->enabled_subdevices & FREENECT_DEVICE_MOTOR) && !dev->usb_motor.dev && (desc.idProduct == PID_NUI_MOTOR)) {
// If the index given by the user matches our camera index
if (nr_mot == index) {
res = libusb_open (devs[i], &dev->usb_motor.dev);
@@ -250,7 +268,7 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
#ifdef BUILD_AUDIO
// TODO: check that the firmware has already been loaded; if not, upload firmware.
// Search for the audio
- if ((ctx->enabled_subdevices & FREENECT_DEVICE_AUDIO) && !dev->usb_audio.dev && desc.idProduct == PID_NUI_AUDIO) {
+ if ((ctx->enabled_subdevices & FREENECT_DEVICE_AUDIO) && !dev->usb_audio.dev && (desc.idProduct == PID_NUI_AUDIO || desc.idProduct == PID_K4W_AUDIO)) {
// If the index given by the user matches our audio index
if (nr_audio == index) {
res = libusb_open (devs[i], &dev->usb_audio.dev);
@@ -306,7 +324,7 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
if (r < 0)
continue;
// If this dev is a Kinect audio device, open device, read serial, and compare.
- if (new_dev_desc.idVendor == VID_MICROSOFT && new_dev_desc.idProduct == PID_NUI_AUDIO) {
+ if (new_dev_desc.idVendor == VID_MICROSOFT && (new_dev_desc.idProduct == PID_NUI_AUDIO || new_dev_desc.idProduct == PID_K4W_AUDIO)) {
FN_SPEW("Matched VID/PID!\n");
libusb_device_handle* new_dev_handle;
// Open device

0 comments on commit 06e48e5

Please sign in to comment.