Skip to content

Commit

Permalink
Move tilt code back into tilt.c and rename device_state -> tilt_state
Browse files Browse the repository at this point in the history
Signed-off-by: Hector Martin <[email protected]>
  • Loading branch information
marcan authored and qdot committed Dec 1, 2010
1 parent c3d165b commit 55a55f0
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 110 deletions.
9 changes: 4 additions & 5 deletions examples/glview.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,10 @@ void *freenect_threadfunc(void *arg)

printf("'w'-tilt up, 's'-level, 'x'-tilt down, '0'-'6'-select LED mode\n");

while(!die && freenect_process_events(f_ctx) >= 0 )
{
freenect_raw_device_state* state;
freenect_update_device_state(f_dev);
state = freenect_get_device_state(f_dev);
while (!die && freenect_process_events(f_ctx) >= 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);
Expand Down
2 changes: 1 addition & 1 deletion fakenect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
######################################################################################
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/utils)
SET(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib/fakenect)
add_library (fakenect SHARED fakenect.c ../src/accel.c)
add_library (fakenect SHARED fakenect.c)
set_target_properties ( fakenect PROPERTIES
VERSION ${PROJECT_VER}
SOVERSION ${PROJECT_APIVER}
Expand Down
2 changes: 1 addition & 1 deletion fakenect/README
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This consists of a "record" program to save dumps from the kinect sensor and a l
Record
./record my_output // NOTE: output directory is created for you

The program takes one argument (the output directory) and saves the acceleration, depth, and rgb data as individual files with names in the form "TYPE-CURRENTIME-TIMESTAMP" where TYPE is either (a)ccel, (d)epth, or (r)gb, TIMESTAMP corresponds to the timestamp associated with the observation (or in the case of accel, the last timestamp seen), and CURRENTTIME corresponds to a floating point version of the time in seconds. The purpose of storing the current time is so that delays can be recreated exactly as they occurred. For RGB and DEPTH the dump is just the entirety of the data provided in PPM and PGM formats respectively (just a 1 line header above the raw dump). For ACCEL, the dump is the 'freenect_raw_device_state'. Only the front part of the file name is used, with the rest left undefined (extension, extra info, etc).
The program takes one argument (the output directory) and saves the acceleration, depth, and rgb data as individual files with names in the form "TYPE-CURRENTIME-TIMESTAMP" where TYPE is either (a)ccel, (d)epth, or (r)gb, TIMESTAMP corresponds to the timestamp associated with the observation (or in the case of accel, the last timestamp seen), and CURRENTTIME corresponds to a floating point version of the time in seconds. The purpose of storing the current time is so that delays can be recreated exactly as they occurred. For RGB and DEPTH the dump is just the entirety of the data provided in PPM and PGM formats respectively (just a 1 line header above the raw dump). For ACCEL, the dump is the 'freenect_raw_tilt_state'. Only the front part of the file name is used, with the rest left undefined (extension, extra info, etc).

A file called INDEX.txt is also output with all of the filenames local to that directory to simplify the format (e.g., no need to read the directory structure).

Expand Down
21 changes: 15 additions & 6 deletions fakenect/fakenect.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@
#include <unistd.h>
#include <time.h>

#define GRAVITY 9.80665

// The dev and ctx are just faked with these numbers

static freenect_device *fake_dev = (freenect_device *)1234;
static freenect_context *fake_ctx = (freenect_context *)5678;
static freenect_depth_cb cur_depth_cb = NULL;
static freenect_rgb_cb cur_rgb_cb = NULL;
static char *input_path = NULL;
static FILE *index_fp = NULL;
static freenect_raw_device_state state = {};
static freenect_raw_tilt_state state = {};
static int already_warned = 0;
static double playback_prev_time = 0.;
static double record_prev_time = 0.;
Expand Down Expand Up @@ -206,17 +209,23 @@ int freenect_process_events(freenect_context *ctx) {
return 0;
}

double freenect_get_tilt_degs(freenect_raw_device_state *state) {
double freenect_get_tilt_degs(freenect_raw_tilt_state *state) {
// NOTE: This is duped from tilt.c, this is the only function we need from there
return ((double)state->tilt_angle) / 2.;
}

freenect_raw_device_state* freenect_get_device_state(freenect_device *dev) {
freenect_raw_tilt_state* freenect_get_tilt_state(freenect_device *dev) {
return &state;
}

// void freenect_get_mks_accel(freenect_raw_device_state *state, double* x, double* y, double* z);
// NOTE: We use use the version of this function from accel.c
void freenect_get_mks_accel(freenect_raw_tilt_state *state, double* x, double* y, double* z)
{
//the documentation for the accelerometer (http://www.kionix.com/Product%20Sheets/KXSD9%20Product%20Brief.pdf)
//states there are 819 counts/g
*x = (double)state->accelerometer_x/FREENECT_COUNTS_PER_G*GRAVITY;
*y = (double)state->accelerometer_y/FREENECT_COUNTS_PER_G*GRAVITY;
*z = (double)state->accelerometer_z/FREENECT_COUNTS_PER_G*GRAVITY;
}

void freenect_set_depth_callback(freenect_device *dev, freenect_depth_cb cb) {
cur_depth_cb = cb;
Expand Down Expand Up @@ -265,5 +274,5 @@ int freenect_stop_depth(freenect_device *dev) {return 0;}
int freenect_stop_rgb(freenect_device *dev) {return 0;}
int freenect_set_tilt_degs(freenect_device *dev, double angle) {return 0;}
int freenect_set_led(freenect_device *dev, freenect_led_options option) {return 0;}
int freenect_update_device_state(freenect_device *dev) {return 0;}
int freenect_update_tilt_state(freenect_device *dev) {return 0;}
void *freenect_get_user(freenect_device *dev) {return NULL;}
6 changes: 3 additions & 3 deletions fakenect/record.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ void dump(char type, uint32_t timestamp, void *data, int data_size) {
void snapshot_accel(freenect_device *dev) {
if (!last_timestamp)
return;
freenect_raw_device_state* state;
freenect_update_device_state(dev);
state = freenect_get_device_state(dev);
freenect_raw_tilt_state* state;
freenect_update_tilt_state(dev);
state = freenect_get_tilt_state(dev);
dump('a', last_timestamp, state, sizeof *state);
}

Expand Down
11 changes: 5 additions & 6 deletions include/libfreenect.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ typedef struct {
int16_t accelerometer_z;
int8_t tilt_angle;
freenect_tilt_status_code tilt_status;
} freenect_raw_device_state;
} freenect_raw_tilt_state;

struct _freenect_context;
typedef struct _freenect_context freenect_context;
Expand Down Expand Up @@ -139,13 +139,12 @@ int freenect_start_rgb(freenect_device *dev);
int freenect_stop_depth(freenect_device *dev);
int freenect_stop_rgb(freenect_device *dev);

int freenect_update_tilt_state(freenect_device *dev);
freenect_raw_tilt_state* freenect_get_tilt_state(freenect_device *dev);
double freenect_get_tilt_degs(freenect_raw_tilt_state *state);
int freenect_set_tilt_degs(freenect_device *dev, double angle);
int freenect_set_led(freenect_device *dev, freenect_led_options option);

int freenect_update_device_state(freenect_device *dev);
freenect_raw_device_state* freenect_get_device_state(freenect_device *dev);
void freenect_get_mks_accel(freenect_raw_device_state *state, double* x, double* y, double* z);
double freenect_get_tilt_degs(freenect_raw_device_state *state);
void freenect_get_mks_accel(freenect_raw_tilt_state *state, double* x, double* y, double* z);

#ifdef __cplusplus
}
Expand Down
8 changes: 4 additions & 4 deletions include/libfreenect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace Freenect {

class FreenectDeviceState : Noncopyable {
friend class FreenectDevice;
FreenectDeviceState(freenect_raw_device_state *_state):
FreenectDeviceState(freenect_raw_tilt_state *_state):
m_state(_state)
{}
public:
Expand All @@ -54,7 +54,7 @@ namespace Freenect {
return freenect_get_tilt_degs(m_state);
}
private:
freenect_raw_device_state *m_state;
freenect_raw_tilt_state *m_state;
};

class FreenectDevice : Noncopyable {
Expand Down Expand Up @@ -89,10 +89,10 @@ namespace Freenect {
if(freenect_set_led(m_dev, _option) != 0) throw std::runtime_error("Cannot set led");
}
void updateState() {
if (freenect_update_device_state(m_dev) != 0) throw std::runtime_error("Cannot update device state");
if (freenect_update_tilt_state(m_dev) != 0) throw std::runtime_error("Cannot update device state");
}
FreenectDeviceState getState() const {
return FreenectDeviceState(freenect_get_device_state(m_dev));
return FreenectDeviceState(freenect_get_tilt_state(m_dev));
}
// Do not call directly even in child
virtual void RGBCallback(freenect_pixel *rgb, uint32_t timestamp) = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include_directories (${CMAKE_CURRENT_SOURCE_DIR})

set(CMAKE_C_FLAGS "-Wall")
include_directories(${LIBUSB_1_INCLUDE_DIRS})
LIST(APPEND SRC core.c tilt.c cameras.c accel.c usb_libusb10.c)
LIST(APPEND SRC core.c tilt.c cameras.c usb_libusb10.c)

add_library (freenect SHARED ${SRC})
set_target_properties ( freenect PROPERTIES
Expand Down
44 changes: 0 additions & 44 deletions src/accel.c

This file was deleted.

29 changes: 0 additions & 29 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,32 +195,3 @@ void fn_log(freenect_context *ctx, freenect_loglevel level, const char *fmt, ...
va_end(ap);
}
}

freenect_raw_device_state* freenect_get_device_state(freenect_device *dev)
{
return &dev->raw_state;
}

int freenect_update_device_state(freenect_device *dev)
{
freenect_context *ctx = dev->parent;
uint8_t buf[10];
uint16_t ux, uy, uz;
int ret = fnusb_control(&dev->usb_motor, 0xC0, 0x32, 0x0, 0x0, buf, 10);
if (ret != 10) {
FN_ERROR("Error in accelerometer reading, libusb_control_transfer returned %d\n", ret);
return ret < 0 ? ret : -1;
}

ux = ((uint16_t)buf[2] << 8) | buf[3];
uy = ((uint16_t)buf[4] << 8) | buf[5];
uz = ((uint16_t)buf[6] << 8) | buf[7];

dev->raw_state.accelerometer_x = (int16_t)ux;
dev->raw_state.accelerometer_y = (int16_t)uy;
dev->raw_state.accelerometer_z = (int16_t)uz;
dev->raw_state.tilt_angle = (int8_t)buf[8];
dev->raw_state.tilt_status = buf[9];

return ret;
}
2 changes: 1 addition & 1 deletion src/freenect_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct _freenect_device {
// Audio
// Motor
fnusb_dev usb_motor;
freenect_raw_device_state raw_state;
freenect_raw_tilt_state raw_state;
};

struct caminit {
Expand Down
40 changes: 39 additions & 1 deletion src/tilt.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@

#define GRAVITY 9.80665

freenect_raw_tilt_state* freenect_get_tilt_state(freenect_device *dev)
{
return &dev->raw_state;
}

int freenect_update_tilt_state(freenect_device *dev)
{
freenect_context *ctx = dev->parent;
uint8_t buf[10];
uint16_t ux, uy, uz;
int ret = fnusb_control(&dev->usb_motor, 0xC0, 0x32, 0x0, 0x0, buf, 10);
if (ret != 10) {
FN_ERROR("Error in accelerometer reading, libusb_control_transfer returned %d\n", ret);
return ret < 0 ? ret : -1;
}

ux = ((uint16_t)buf[2] << 8) | buf[3];
uy = ((uint16_t)buf[4] << 8) | buf[5];
uz = ((uint16_t)buf[6] << 8) | buf[7];

dev->raw_state.accelerometer_x = (int16_t)ux;
dev->raw_state.accelerometer_y = (int16_t)uy;
dev->raw_state.accelerometer_z = (int16_t)uz;
dev->raw_state.tilt_angle = (int8_t)buf[8];
dev->raw_state.tilt_status = buf[9];

return ret;
}

int freenect_set_tilt_degs(freenect_device *dev, double angle)
{
int ret;
Expand All @@ -59,7 +88,16 @@ int freenect_set_led(freenect_device *dev, freenect_led_options option)
return ret;
}

double freenect_get_tilt_degs(freenect_raw_device_state *state)
double freenect_get_tilt_degs(freenect_raw_tilt_state *state)
{
return ((double)state->tilt_angle) / 2.;
}

void freenect_get_mks_accel(freenect_raw_tilt_state *state, double* x, double* y, double* z)
{
//the documentation for the accelerometer (http://www.kionix.com/Product%20Sheets/KXSD9%20Product%20Brief.pdf)
//states there are 819 counts/g
*x = (double)state->accelerometer_x/FREENECT_COUNTS_PER_G*GRAVITY;
*y = (double)state->accelerometer_y/FREENECT_COUNTS_PER_G*GRAVITY;
*z = (double)state->accelerometer_z/FREENECT_COUNTS_PER_G*GRAVITY;
}
16 changes: 8 additions & 8 deletions wrappers/python/freenect.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ cdef extern from "libfreenect.h":
int freenect_stop_rgb(void *dev)
int freenect_set_tilt_degs(void *dev, double angle)
int freenect_set_led(void *dev, int option)
int freenect_update_device_state(void *dev)
void* freenect_get_device_state(void *dev)
int freenect_update_tilt_state(void *dev)
void* freenect_get_tilt_state(void *dev)
void freenect_get_mks_accel(void *state, double* x, double* y, double* z)
double freenect_get_tilt_degs(void *state)

Expand Down Expand Up @@ -125,11 +125,11 @@ def set_tilt_degs(DevPtr dev, float angle):
def set_led(DevPtr dev, int option):
return freenect_set_led(dev._ptr, option)

def update_device_state(DevPtr dev):
return freenect_update_device_state(dev._ptr)
def update_tilt_state(DevPtr dev):
return freenect_update_tilt_state(dev._ptr)

def get_device_state(DevPtr dev):
cdef void* state = freenect_get_device_state(dev._ptr)
def get_tilt_state(DevPtr dev):
cdef void* state = freenect_get_tilt_state(dev._ptr)
cdef StatePtr state_out
state_out = StatePtr()
state_out._ptr = state
Expand All @@ -149,8 +149,8 @@ def get_accel(DevPtr dev):
Returns:
(x, y, z) accelerometer values
"""
update_device_state(dev)
return get_mks_accel(get_device_state(dev))
update_tilt_state(dev)
return get_mks_accel(get_tilt_state(dev))


def get_tilt_degs(StatePtr state):
Expand Down

0 comments on commit 55a55f0

Please sign in to comment.