00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 """ For backwards compatibility with the old driver files
00018 Will be DELETED in the future """
00019
00020 import sys
00021 import time
00022
00023
00024
00025
00026
00027
00028
00029
00030 import threading
00031 import Leap
00032 from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
00033
00034 class LeapFinger():
00035 def __init__(self, finger=None):
00036 self.boneNames = ['metacarpal',
00037 'proximal',
00038 'intermediate',
00039 'distal']
00040 for boneName in self.boneNames:
00041 setattr(self, boneName, [0.0, 0.0, 0.0])
00042 self.tip = [0.0, 0.0, 0.0]
00043
00044 self.leapBoneNames = [Leap.Bone.TYPE_METACARPAL,
00045 Leap.Bone.TYPE_PROXIMAL,
00046 Leap.Bone.TYPE_INTERMEDIATE,
00047 Leap.Bone.TYPE_DISTAL]
00048
00049 if finger is not None:
00050 self.importFinger(finger)
00051
00052 def importFinger(self, finger):
00053 for boneName in self.boneNames:
00054
00055 bone = finger.bone(getattr(Leap.Bone, 'TYPE_%s' % boneName.upper()))
00056 setattr(self, boneName, bone.prev_joint.to_float_array())
00057
00058 self.tip = finger.bone(Leap.Bone.TYPE_DISTAL).next_joint.to_float_array()
00059
00060
00061
00062 class LeapInterface(Leap.Listener):
00063 def on_init(self, controller):
00064
00065
00066 self.hand = [0,0,0]
00067 self.right_hand = False
00068 self.left_hand = False
00069 self.hand_direction = [0,0,0]
00070 self.hand_normal = [0,0,0]
00071 self.hand_palm_pos = [0,0,0]
00072 self.hand_pitch = 0.0
00073 self.hand_yaw = 0.0
00074 self.hand_roll = 0.0
00075 self.fingerNames = ['thumb', 'index', 'middle', 'ring', 'pinky']
00076 for fingerName in self.fingerNames:
00077 setattr(self, fingerName, LeapFinger())
00078 print "Initialized Leap Motion Device"
00079
00080 def on_connect(self, controller):
00081 print "Connected to Leap Motion Controller"
00082
00083
00084 controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
00085 controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
00086 controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
00087 controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
00088
00089 def on_disconnect(self, controller):
00090
00091 print "Disconnected Leap Motion"
00092
00093 def on_exit(self, controller):
00094 print "Exited Leap Motion Controller"
00095
00096 def on_frame(self, controller):
00097
00098 frame = controller.frame()
00099
00100 print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
00101 frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures()))
00102
00103 if not frame.hands.is_empty:
00104
00105
00106
00107
00108 there_is_right_hand=False
00109 there_is_left_hand=False
00110
00111 for hand in frame.hands:
00112
00113 if hand.is_right:
00114 there_is_right_hand=True
00115 self.right_hand=hand
00116 elif hand.is_left:
00117 there_is_left_hand=True
00118
00119 self.left_hand=hand
00120
00121 if not there_is_right_hand:
00122 self.right_hand=False
00123
00124 if not there_is_left_hand:
00125 self.left_hand=False
00126
00127 self.hand = frame.hands[0]
00128
00129
00130 fingers = self.hand.fingers
00131 if not fingers.is_empty:
00132 for fingerName in self.fingerNames:
00133
00134
00135 finger = fingers.finger_type(getattr(Leap.Finger, 'TYPE_%s' % fingerName.upper()))[0]
00136 getattr(self, fingerName).importFinger(finger)
00137
00138
00139
00140
00141
00142 normal = self.hand.palm_normal
00143 direction = self.hand.direction
00144 pos = self.hand.palm_position
00145
00146 self.hand_direction[0] = direction.x
00147 self.hand_direction[1] = direction.y
00148 self.hand_direction[2] = direction.z
00149 self.hand_normal[0] = normal.x
00150 self.hand_normal[1] = normal.y
00151 self.hand_normal[2] = normal.z
00152 self.hand_palm_pos[0] = pos.x
00153 self.hand_palm_pos[1] = pos.y
00154 self.hand_palm_pos[2] = pos.z
00155 self.hand_pitch = direction.pitch * Leap.RAD_TO_DEG
00156 self.hand_yaw = normal.yaw * Leap.RAD_TO_DEG
00157 self.hand_roll = direction.roll * Leap.RAD_TO_DEG
00158
00159
00160 print "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (self.hand_pitch, self.hand_roll, self.hand_yaw)
00161
00162 '''
00163 # Gestures
00164 for gesture in frame.gestures():
00165 if gesture.type == Leap.Gesture.TYPE_CIRCLE:
00166 circle = CircleGesture(gesture)
00167
00168 # Determine clock direction using the angle between the pointable and the circle normal
00169 if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
00170 clockwiseness = "clockwise"
00171 else:
00172 clockwiseness = "counterclockwise"
00173
00174 # Calculate the angle swept since the last frame
00175 swept_angle = 0
00176 if circle.state != Leap.Gesture.STATE_START:
00177 previous_update = CircleGesture(controller.frame(1).gesture(circle.id))
00178 swept_angle = (circle.progress - previous_update.progress) * 2 * Leap.PI
00179
00180 print "Circle id: %d, %s, progress: %f, radius: %f, angle: %f degrees, %s" % (
00181 gesture.id, self.state_string(gesture.state),
00182 circle.progress, circle.radius, swept_angle * Leap.RAD_TO_DEG, clockwiseness)
00183
00184 if gesture.type == Leap.Gesture.TYPE_SWIPE:
00185 swipe = SwipeGesture(gesture)
00186 print "Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % (
00187 gesture.id, self.state_string(gesture.state),
00188 swipe.position, swipe.direction, swipe.speed)
00189
00190 if gesture.type == Leap.Gesture.TYPE_KEY_TAP:
00191 keytap = KeyTapGesture(gesture)
00192 print "Key Tap id: %d, %s, position: %s, direction: %s" % (
00193 gesture.id, self.state_string(gesture.state),
00194 keytap.position, keytap.direction )
00195
00196 if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP:
00197 screentap = ScreenTapGesture(gesture)
00198 print "Screen Tap id: %d, %s, position: %s, direction: %s" % (
00199 gesture.id, self.state_string(gesture.state),
00200 screentap.position, screentap.direction )
00201
00202 if not (frame.hands.empty and frame.gestures().empty):
00203 print ""
00204
00205 def state_string(self, state):
00206 if state == Leap.Gesture.STATE_START:
00207 return "STATE_START"
00208
00209 if state == Leap.Gesture.STATE_UPDATE:
00210 return "STATE_UPDATE"
00211
00212 if state == Leap.Gesture.STATE_STOP:
00213 return "STATE_STOP"
00214
00215 if state == Leap.Gesture.STATE_INVALID:
00216 return "STATE_INVALID"
00217 '''
00218
00219 def get_hand_direction(self):
00220 return self.hand_direction
00221
00222 def get_hand_normal(self):
00223 return self.hand_normal
00224
00225 def get_hand_palmpos(self):
00226 return self.hand_palm_pos
00227
00228 def get_hand_yaw(self):
00229 return self.hand_yaw
00230
00231 def get_hand_pitch(self):
00232 return self.hand_pitch
00233
00234 def get_hand_roll(self):
00235 return self.hand_roll
00236
00237 def get_finger_point(self, fingerName, fingerPointName):
00238 return getattr(getattr(self, fingerName), fingerPointName)
00239
00240
00241 class Runner(threading.Thread):
00242
00243 def __init__(self,arg=None):
00244 threading.Thread.__init__(self)
00245 self.arg=arg
00246 self.listener = LeapInterface()
00247 self.controller = Leap.Controller()
00248 self.controller.add_listener(self.listener)
00249
00250 def __del__(self):
00251 self.controller.remove_listener(self.listener)
00252
00253 def get_hand_direction(self):
00254 return self.listener.get_hand_direction()
00255
00256 def get_hand_normal(self):
00257 return self.listener.get_hand_normal()
00258
00259 def get_hand_palmpos(self):
00260 return self.listener.get_hand_palmpos()
00261
00262 def get_hand_roll(self):
00263 return self.listener.get_hand_roll()
00264
00265 def get_hand_pitch(self):
00266 return self.listener.get_hand_pitch()
00267
00268 def get_hand_yaw(self):
00269 return self.listener.get_hand_yaw()
00270
00271 def get_finger_point(self, fingerName, fingerPointName):
00272 return self.listener.get_finger_point(fingerName, fingerPointName)
00273
00274 def run (self):
00275 while True:
00276
00277 time.sleep(0.001)
00278