use a function imported with a moudle contain multi classes in python

asked 2019-01-22 15:13:06 -0600

stevensu1838 gravatar image

updated 2019-01-22 15:13:19 -0600

0

The question is all about importing a module called leap_interface.py to send.py and using a of the functions defined in leap_interface. I ask this question, because the leap_interface module is a bit complicated. It contains multi classes.

First, I defined function get_extended_fingers(self) in class LeapInterface in leap_interface module as below:

class LeapInterface(Leap.Listener):
def on_init(self, controller):
...
def on_disconnect(self, controller):
...
def on_exit(self, controller):
    print "Exited Leap Motion Controller"
def on_frame(self, controller):
    # Get the most recent frame and report some basic information
    frame = controller.frame()
# Number of extended fingers Steven
def get_extended_fingers(self):
    #print "extendedfingers: %d" % len(extended_finger_list)
    return len(extended_finger_list)

Secondly, I want to use this function as an if condition in the file sender.py:

   if li.get_extended_fingers < 2:
         msg.direction.x = hand_direction_[0]# - initial_pose[0]
         msg.direction.y = hand_direction_[1]
         msg.direction.z = hand_direction_[2]
         print "works1"

   if li.get_extended_fingers > 4:
         msg.ypr.x = hand_yaw_
         msg.ypr.y = hand_pitch_
         msg.ypr.z = hand_roll_
         print "works2"

However, my solution doesn't work properly. It my terminal, it keeps printing works2, no matter how many extended fingers I show in the field of view of my leap motion sensor. Can you please tell me what I did wrong?

leap_interface.py

import sys
import time
import threading
import Leap
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture

class LeapFinger():
    def __init__(self, finger=None):
    self.boneNames = ['metacarpal',
                      'proximal',
                      'intermediate',
                      'distal']
    for boneName in self.boneNames:
        setattr(self, boneName, [0.0, 0.0, 0.0])
    self.tip = [0.0, 0.0, 0.0]

        self.leapBoneNames = [Leap.Bone.TYPE_METACARPAL,
                          Leap.Bone.TYPE_PROXIMAL,
                          Leap.Bone.TYPE_INTERMEDIATE,
                              Leap.Bone.TYPE_DISTAL]

        if finger is not None:
            self.importFinger(finger)

def importFinger(self, finger):
    for boneName in self.boneNames:
        # Get the base of each bone
        bone = finger.bone(getattr(Leap.Bone, 'TYPE_%s' % boneName.upper()))
        setattr(self, boneName, bone.prev_joint.to_float_array())
    # For the tip, get the end of the distal bone
    self.tip = finger.bone(Leap.Bone.TYPE_DISTAL).next_joint.to_float_array()

class LeapInterface(Leap.Listener):
    def on_init(self, controller):
        # These variables as probably not thread safe
    # TODO: Make thread safe ;)
    self.hand           = [0,0,0]
    self.right_hand = False
    self.left_hand = False
    self.hand_direction = [0,0,0]
    self.hand_normal    = [0,0,0]
        self.hand_palm_pos  = [0,0,0]
        # Velocity Steven
    self.hand_palm_vel  = [0,0,0]
    # Finger tip velocity Steven
    self.index_tip_vel  = [0,0,0]
    self.hand_pitch     = 0.0
    self.hand_yaw       = 0.0
    self.hand_roll      = 0.0
    self.fingerNames = ['thumb', 'index', 'middle', 'ring', 'pinky']
    for fingerName in self.fingerNames:
        setattr(self, fingerName, LeapFinger())
    print "Initialized Leap Motion Device"

def on_connect(self, controller):
        print "Connected to Leap Motion Controller"

    # Enable gestures
    controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
    controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
    controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
    controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);

def on_disconnect(self, controller):
    # Note: not dispatched when running in a debugger.
    print "Disconnected Leap Motion"

def on_exit(self, controller):
    print "Exited Leap Motion Controller"

def on_frame(self, controller):
    # Get the most recent frame and report some basic information
    frame = controller.frame()

    '''print "Frame ...
(more)
edit retag flag offensive close merge delete