以下代码对(http://www.cnblogs.com/lucantang/archive/2011/12/15/2288982.html) 稍作修改
1 #include <XnUSB.h>
2 bool _isOpen =false;
4 bool tiltToAngle(int angle)// angle: -27 to 27
5 {
6 XN_USB_DEV_HANDLE m_dev;
7 const XnUSBConnectionString *paths;
8 XnUInt32count;
9 XnStatus res;
10
11 // _isOpen is theglobal camera open status of kinect. If kinect camera is open, we should notcall xnUSBInit() again.
12 if (_isOpen ==false)
13 {
14 res = xnUSBInit();
15 if (res != XN_STATUS_OK) {
16 xnPrintError(res, "xnUSBInit failed");
17 returnfalse;
18 }
19 }
20 // Get device path
21 res = xnUSBEnumerateDevices(0x045E/* VendorID */,0x02B0/*ProductID*/, &paths, &count);
22 if (res != XN_STATUS_OK) {
23 xnPrintError(res, "xnUSBEnumerateDevices failed");
24 return false;
25 }
26 // Open firstfound device
27 res = xnUSBOpenDeviceByPath(paths[0], &m_dev);
28 if (res != XN_STATUS_OK) {
29 xnPrintError(res, "xnUSBOpenDeviceByPath failed");
30 return false;
31 }
32 // Send controlcommand
33 res = xnUSBSendControl(m_dev, XN_USB_CONTROL_TYPE_VENDOR ,0x31, angle,0x00, NULL,0,0);
34 if (res != XN_STATUS_OK) {
35 xnPrintError(res, "xnUSBSendControl failed");
36 return false;
37 }
38 xnUSBCloseDevice(m_dev);
39
40 // Shutdown usb
41 if (_isOpen ==false)
42 {
43 res = xnUSBShutdown();
44 if (res != XN_STATUS_OK) {
45 xnPrintError(res, "xnUSBShutdown failed");
46 returnfalse;
47 }
48 }
49
50 return true;
51 }
52
53 // tiltToAngle(0) : Normal state
54 // tiltToAngle(27) : Up
55 // tiltToAngle(-27) : Down
发现这个旋转角度与底座是否水平放置无关(当选择旋转0时,它总是能够让摄像头水平向前)。这也证实了kinect内部确实存在一个陀螺仪。这也让我想起上篇文章中的提到的unity3D中的Gyroscope类,它不就是陀螺仪么?是不是我把它注释掉,我就不能用陀螺仪设置俯仰角度了呢?
另外发现程序有个bug: direct3D窗口 过一段时间(大约3分钟),就不动了。刷新也停止了,鼠标也不能控制模型了。Mark一下