CGR Localization
 All Classes Namespaces Files Functions Variables Macros Pages
accelerometers.c
1 /*
2  * This file is part of the OpenKinect Project. http://www.openkinect.org
3  *
4  * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
5  * for details.
6  *
7  * This code is licensed to you under the terms of the Apache License, version
8  * 2.0, or, at your option, the terms of the GNU General Public License,
9  * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10  * or the following URLs:
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * http://www.gnu.org/licenses/gpl-2.0.txt
13  *
14  * If you redistribute this file in source form, modified or unmodified, you
15  * may:
16  * 1) Leave this header intact and distribute it under the same terms,
17  * accompanying it with the APACHE20 and GPL20 files, or
18  * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19  * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20  * In all cases you must keep the copyright notice intact and include a copy
21  * of the CONTRIB file.
22  *
23  * Binary distributions must follow the binary distribution requirements of
24  * either License.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <math.h>
32 
33 #include "freenect_internal.h"
34 #define GRAVITY 9.80665
35 
36 int freenect_get_raw_accelerometers(freenect_device *dev, int16_t* x, int16_t* y, int16_t* z)
37 {
38  unsigned char buf[10];
39  uint16_t ux, uy, uz;
40  int ret = fnusb_control(&dev->usb_motor, 0xC0, 0x32, 0x0, 0x0, buf, 10);
41  if (ret != 10)
42  printf("Error in accelerometer reading, libusb_control_transfer returned %d\n", ret);
43 
44  ux = ((uint16_t)buf[2] << 8) | buf[3];
45  uy = ((uint16_t)buf[4] << 8) | buf[5];
46  uz = ((uint16_t)buf[6] << 8) | buf[7];
47  *x = (int16_t)ux;
48  *y = (int16_t)uy;
49  *z = (int16_t)uz;
50 
51  return ret;
52 }
53 
54 int freenect_get_mks_accelerometers(freenect_device *dev, double* x, double* y, double* z)
55 {
56  //the documentation for the accelerometer (http://www.kionix.com/Product%20Sheets/KXSD9%20Product%20Brief.pdf)
57  //states there are 819 counts/g
58  int16_t ix, iy, iz;
59  int ret = freenect_get_raw_accelerometers(dev,&ix,&iy,&iz);
60 
61  *x = (double)ix/FREENECT_COUNTS_PER_G*GRAVITY;
62  *y = (double)iy/FREENECT_COUNTS_PER_G*GRAVITY;
63  *z = (double)iz/FREENECT_COUNTS_PER_G*GRAVITY;
64 
65  return ret;
66 }
67 
68