tiltdemo.c
Go to the documentation of this file.
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  * Andrew Miller <amiller@dappervision.com>
8  *
9  * This code is licensed to you under the terms of the Apache License, version
10  * 2.0, or, at your option, the terms of the GNU General Public License,
11  * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
12  * or the following URLs:
13  * http://www.apache.org/licenses/LICENSE-2.0
14  * http://www.gnu.org/licenses/gpl-2.0.txt
15  *
16  * If you redistribute this file in source form, modified or unmodified, you
17  * may:
18  * 1) Leave this header intact and distribute it under the same terms,
19  * accompanying it with the APACHE20 and GPL20 files, or
20  * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
21  * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
22  * In all cases you must keep the copyright notice intact and include a copy
23  * of the CONTRIB file.
24  *
25  * Binary distributions must follow the binary distribution requirements of
26  * either License.
27  */
28 
29 #include "libfreenect.h"
30 #include "libfreenect_sync.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <time.h>
34 
35 #ifndef _WIN32
36  #include <unistd.h>
37 #else
38  // Microsoft Visual C++ does not provide the <unistd.h> header, but most of
39  // its contents can be found within the <stdint.h> header:
40  #include <stdint.h>
41  // except for the UNIX sleep() function that has to be emulated:
42  #include <windows.h>
43  // http://pubs.opengroup.org/onlinepubs/009695399/functions/sleep.html
44  // According to the link above, the semantics of UNIX sleep() is as follows:
45  // "If sleep() returns because the requested time has elapsed, the value
46  // returned shall be 0. If sleep() returns due to delivery of a signal, the
47  // return value shall be the "unslept" amount (the requested time minus the
48  // time actually slept) in seconds."
49  // The following function does not implement the return semantics, but
50  // will do for now... A proper implementation would require Performance
51  // Counters before and after the forward call to the Windows Sleep()...
52  unsigned sleep(unsigned seconds)
53  {
54  Sleep(seconds*1000); // The Windows Sleep operates on milliseconds
55  return(0);
56  }
57  // Note for MinGW-gcc users: MinGW-gcc also does not provide the UNIX sleep()
58  // function within <unistd.h>, but it does provide usleep(); trivial wrapping
59  // of sleep() aroung usleep() is possible, however the usleep() documentation
60  // (http://docs.hp.com/en/B2355-90682/usleep.2.html) clearly states that:
61  // "The useconds argument must be less than 1,000,000. (...)
62  // (...) The usleep() function may fail if:
63  // [EINVAL]
64  // The time interval specified 1,000,000 or more microseconds."
65  // which means that something like below can be potentially dangerous:
66  // unsigned sleep(unsigned seconds)
67  // {
68  // usleep(seconds*1000000); // The usleep operates on microseconds
69  // return(0);
70  // }
71  // So, it is strongly advised to stick with the _WIN32/_MSC_VER
72  // http://www.xinotes.org/notes/note/439/
73 #endif//_WIN32
74 
75 
76 
77 /* This is a simple demo. It connects to the kinect and plays with the motor,
78  the accelerometers, and the LED. It doesn't do anything with images. And,
79  unlike the other examples, no OpenGL is required!
80 
81  So, this should serve as the reference example for working with the motor,
82  accelerometers, and LEDs. */
83 
84 void no_kinect_quit(void)
85 {
86  printf("Error: Kinect not connected?\n");
87  exit(1);
88 }
89 
90 int main(int argc, char *argv[])
91 {
92  srand(time(0));
93 
94  while (1) {
95  // Pick a random tilt and a random LED state
96  freenect_led_options led = (freenect_led_options) (rand() % 6); // explicit cast
97  int tilt = (rand() % 30)-15;
99  double dx, dy, dz;
100 
101  // Set the LEDs to one of the possible states
102  if (freenect_sync_set_led(led, 0)) no_kinect_quit();
103 
104  // Set the tilt angle (in degrees)
106 
107  // Get the raw accelerometer values and tilt data
109 
110  // Get the processed accelerometer values (calibrated to gravity)
111  freenect_get_mks_accel(state, &dx, &dy, &dz);
112 
113  printf("led[%d] tilt[%d] accel[%lf,%lf,%lf]\n", led, tilt, dx,dy,dz);
114 
115  sleep(3);
116  }
117 }
118 
119 
void freenect_get_mks_accel(freenect_raw_tilt_state *state, double *x, double *y, double *z)
Definition: fakenect.c:227
int freenect_sync_set_tilt_degs(int angle, int index)
int freenect_sync_get_tilt_state(freenect_raw_tilt_state **state, int index)
freenect_led_options
Definition: libfreenect.h:148
int main(int argc, char *argv[])
Definition: tiltdemo.c:90
Data from the tilt motor and accelerometer.
Definition: libfreenect.h:167
capture state
Definition: micview.c:53
int freenect_sync_set_led(freenect_led_options led, int index)
void no_kinect_quit(void)
Definition: tiltdemo.c:84


libfreenect
Author(s): Hector Martin, Josh Blake, Kyle Machulis, OpenKinect community
autogenerated on Thu Jun 6 2019 19:25:38