Skip to content

Instantly share code, notes, and snippets.

@vgel
Created July 1, 2013 23:01
Show Gist options
  • Save vgel/5905398 to your computer and use it in GitHub Desktop.
Save vgel/5905398 to your computer and use it in GitHub Desktop.
Some kinect scrolling junk
#! /usr/bin/python
from openni import *
import kinect_motor
import uinput
import math
grabbed = False
def setup_uinput():
events = (
uinput.REL_WHEEL,
uinput.REL_X,
uinput.REL_Y,
uinput.BTN_LEFT,
uinput.BTN_RIGHT,
)
return uinput.Device(events)
def uinput_scroll(direction, amount=1):
device.emit(uinput.REL_WHEEL, direction * amount)
# Declare the callbacks
# gesture
def gesture_detected(src, gesture, id, end_point):
global grabbed
if grabbed:
return
grabbed = True
print "Start scrolling!"
hands_generator.start_tracking(end_point)
kinect_motor.set_led_color(kinect_motor.LED_GREEN)
# gesture_detected
def gesture_progress(src, gesture, point, progress): pass
# gesture_progress
points = []
X = 0
Y = 1
Z = 2
def _add_point(p):
points.append(p)
if len(points) > 200:
points.pop(0)
def _map_axis(a):
return map(lambda p: p[a], points)
def _average_axis(axis):
a = _map_axis(axis)
return sum(a) / len(a)
def lastpos(axis):
return points[-2][axis]
def create(src, id, pos, time):
print 'Starts!'
_add_point(pos)
def dist(p1, p2):
return math.sqrt((p1[X] - p2[X])**2 + (p1[Y] - p2[Y])**2)
def update(src, id, pos, time):
#print 'Update', src, id, pos, time
_add_point(pos)
distance = dist(points[-1], points[-2])
if distance > 10 and points[-1][X] - points[-2][X] > 20:
if points[-1][Y] - points[-2][Y] < 10:
print 'Down', distance
uinput_scroll(-1, int(distance / 10))
elif points[-1][Y] - points[-2][Y] > 10:
print 'Up', distance
uinput_scroll(1, int(distance / 10))
#amount = abs((pos[Y] - lastpos(Y)) / threshold[Y])
#uinput_scroll(-1, int(amount))
def destroy(src, id, time):
global grabbed
grabbed = False
print 'Lost track of hand!', time
kinect_motor.set_led_color(kinect_motor.LED_BLINK_YELLOW)
device = setup_uinput()
#kinect_motor.set_motor_angle(-10)
kinect_motor.set_motor_level()
kinect_motor.set_led_color(kinect_motor.LED_BLINK_RED_YELLOW)
context = Context()
context.init()
depth_generator = DepthGenerator()
depth_generator.create(context)
depth_generator.set_resolution_preset(RES_VGA)
depth_generator.fps = 30
#print depth_generator.get_tuple_depth_map()
gesture_generator = GestureGenerator()
gesture_generator.create(context)
gesture_generator.add_gesture('Click')
hands_generator = HandsGenerator()
hands_generator.create(context)
# Register the callbacks
gesture_generator.register_gesture_cb(gesture_detected, gesture_progress)
hands_generator.register_hand_cb(create, update, destroy)
# Start generating
context.start_generating_all()
#for x in depth_generator.point_map:
# print x
print 'Click to start scrolling!'
while True:
context.wait_any_update_all()
# while
import usb
LED_OFF = 0
LED_GREEN = 1
LED_RED = 2
LED_YELLOW = 3
LED_BLINK_YELLOW = 4
LED_BLINK_GREEN = 5
LED_BLINK_RED_YELLOW = 6
_dev = usb.core.find(idVendor=0x045e, idProduct=0x02B0) #The kinect motor device
def set_led_color(color):
_dev.ctrl_transfer(0x40, 0x06, color, 0x0, [])
def set_motor_angle(degree):
_dev.ctrl_transfer(0x40, 0x31, degree, 0x0, [])
def set_motor_up():
set_motor_angle(60)
def set_motor_level():
set_motor_angle(0)
def set_motor_down():
set_motor_angle(-60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment