2016-03-05 133 views
2

我對Python很新,發現此代碼爲here。它不斷給我這個錯誤,我很困惑該怎麼做。NameError:全局名稱'AZIMUTHAL_LIMIT'未定義

文件 「my_first_leap.py」,線78,在on_frame XPOS_servo = ABS(AZIMUTHAL_LIMIT-normalized_point.x * AZIMUTHAL_LIMIT)NameError:全局名稱 'AZIMUTHAL_LIMIT' 沒有定義

感謝。

# Simple Leap motion program to track the position of your hand and move one servo 
# import the libraries where the LeapMotionSDK is 
import sys 
sys.path.insert(0, "LeapLib/") 

import Leap, thread, time 
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture,  SwipeGesture 
from pyduino import * 


class SampleListener(Leap.Listener): 

oldtime = time.time() 
newtime = time.time() 

# FIXME if servo is not attached to pin 2 
SERVO_PIN = 2 # Azimuthal Servo motor pin 
AZIMUTHAL_LIMIT = 180 # we want our motor to go between 0 and 180 

def on_init(self, controller): 

    # if your arduino was running on a serial port other than '/dev/ttyACM0/' 
    # declare: a = Arduino(serial_port='/dev/ttyXXXX') 
    self.a = Arduino(serial_port='/dev/cu.usbmodem1421') 

    # sleep to ensure ample time for computer to make serial connection 
    time.sleep(3) 

    print "Initialized" 

def on_connect(self, controller): 
    print "Connected" 

    # 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" 

def on_exit(self, controller): 

    # Reset servo position when you stop program 
    self.a.servo_write(self.SERVO_PIN,90) 
    self.a.close() 

    print "Exited" 

def on_frame(self, controller): 

    # we only want to get the position of the hand every so often 
    self.newtime = time.time() 
    if self.newtime-self.oldtime > 0.1: # if difference between times is 10ms 

     # Get the most recent frame and report some basic information 
     frame = controller.frame() 
     interaction_box = frame.interaction_box 

     # print some statistics 
     print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
       frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures())) 

     # Get hands 
     for hand in frame.hands: 

      handType = "Left hand" if hand.is_left else "Right hand" 
      normalized_point = interaction_box.normalize_point(hand.palm_position,True) 

      print " %s, id %d, x-position: %s" % (handType, hand.id, normalized_point.x) 
      print " %s, id %d, y-position: %s" % (handType, hand.id, normalized_point.y) 
      print " %s, id %d, z-position: %s" % (handType, hand.id, normalized_point.z) 

     # FIXME depending on orientation of servo motor 
     # if motor is upright, Leap Device will register a 0 degree angle if hand is all the way to the left 
     XPOS_servo = abs(AZIMUTHAL_LIMIT-normalized_point.x*AZIMUTHAL_LIMIT) 
     print " Servo Angle = %d " %(int(XPOS_servo)) 

     # write the value to servo on arduino 
     self.a.servo_write(self.SERVO_PIN,int(XPOS_servo)) # turn LED on 

     # update the old time 
     self.oldtime = self.newtime 
    else: 
     pass # keep advancing in time until 10 millisecond is reached 


def main(): 

# Create a sample listener and controller 
listener = SampleListener() 
controller = Leap.Controller() 

# Have the sample listener receive events from the controller 
controller.add_listener(listener) 

# Keep this process running until Enter is pressed 
print "Press Enter to quit..." 
try: 
    sys.stdin.readline() 
except KeyboardInterrupt: 
    pass 
finally: 
    # Remove the sample listener when done 
    controller.remove_listener(listener) 

if __name__ == "__main__": 
main() 

編輯:這是改變它後的代碼,它運行但不寫入伺服。

# Simple Leap motion program to track the position of your hand and move one servo 
# import the libraries where the LeapMotionSDK is 
import sys 
sys.path.insert(0, "LeapLib/") 

import Leap, thread, time 
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture,  SwipeGesture 
from pyduino import * 


class SampleListener(Leap.Listener): 
    def on_init(self,controller): 
     # if your arduino was running on a serial port other than  '/dev/ttyACM0/' 
     # declare: a = Arduino(serial_port='/dev/ttyXXXX') 
    self.oldtime = time.time() 
    self.newtime = time.time() 
    # FIXME if servo is not attached to pin 2 
    self.SERVO_PIN = 2 # Azimuthal Servo motor pin 
    self.AZIMUTHAL_LIMIT = 180 # we want our motor to go between 0 and 180 

    self.a = Arduino(serial_port='/dev/cu.usbmodem1421') 
    # sleep to ensure ample time for computer to make serial connection 
    time.sleep(3) 
    print "Initialized" 

def on_connect(self, controller): 
    print "Connected" 
    # 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, *args, **kwargs): 
    # Note: not dispatched when running in a debugger. 
    print "Disconnected" 

def on_exit(self, *args, **kwargs): 
    # Reset servo position when you stop program 
    self.a.servo_write(self.SERVO_PIN,90) 
    self.a.close() 


def on_frame(self, controller): 

    # we only want to get the position of the hand every so often 
    self.newtime = time.time() 
    if self.newtime-self.oldtime > 0.1: # if difference between times is 10ms 
     # Get the most recent frame and report some basic information 
     frame    = controller.frame() 
     interaction_box  = frame.interaction_box 
     normalized_point = None 

     # Get hands 
     for hand in frame.hands: 

      handType = "Left hand" if hand.is_left else "Right hand" 
      print handType 
      normalized_point = interaction_box.normalize_point(hand.palm_position,True) 


     # FIXME depending on orientation of servo motor 
     # if motor is upright, Leap Device will register a 0 degree angle if hand is all the way to the left 
      if normalized_point: 
       XPOS_servo = abs(self.AZIMUTHAL_LIMIT-normalized_point.x*self.AZIMUTHAL_LIMIT) 
      # write the value to servo on arduino 
       self.a.servo_write(self.SERVO_PIN,int(XPOS_servo)) # turn LED on 

     # update the old time 
     self.oldtime = self.newtime 
    else: 
     pass # keep advancing in time until 10 millisecond is reached 


def main(): 
    # Create a sample listener and controller 
    listener = SampleListener() 
    controller = Leap.Controller() 
    # Have the sample listener receive events from the controller 
    controller.add_listener(listener) 
    # Keep this process running until Enter is pressed 
    try: 
     sys.stdin.readline() 
    except KeyboardInterrupt: 
     pass 
    finally: 
     # Remove the sample listener when done 
     controller.remove_listener(listener) 

if __name__ == "__main__": 
    main() 

回答

0

複製代碼中的縮進全部搞砸了。

而且,AZIMUTHAL_LIMIT應該被稱爲<classname>. AZIMUTHAL_LIMIT

示例代碼

>>> class foo: 
...  bar = 1 
...  def __init__(self): 
...    print bar 
... 
>>> foo() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 4, in __init__ 
NameError: global name 'bar' is not defined 
>>> 

這裏看到下同錯誤代碼一點清理,沒有承諾它會工作,我不敢肯定這一飛躍運動的東西是如何工作的,但也許這將幫助您延續:

# Simple Leap motion program to track the position of your hand and move one servo 
# import the libraries where the LeapMotionSDK is 
import sys 
sys.path.insert(0, "LeapLib/") 

import Leap, thread, time 
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture 
from pyduino import * 


class SampleListener(Leap.Listener): 
    def __init__(self): 
     # if your arduino was running on a serial port other than '/dev/ttyACM0/' 
     # declare: a = Arduino(serial_port='/dev/ttyXXXX') 
     self.oldtime = time.time() 
     self.newtime = time.time() 
     # FIXME if servo is not attached to pin 2 
     self.SERVO_PIN = 2 # Azimuthal Servo motor pin 
     self.AZIMUTHAL_LIMIT = 180 # we want our motor to go between 0 and 180 

     self.a = Arduino(serial_port='/dev/cu.usbmodem1421') 
     # sleep to ensure ample time for computer to make serial connection 
     time.sleep(3) 
     print "Initialized" 

    def on_connect(self, controller): 
     print "Connected" 
     # 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, *args, **kwargs): 
     # Note: not dispatched when running in a debugger. 
     print "Disconnected" 

    def on_exit(self, *args, **kwargs): 
     # Reset servo position when you stop program 
     self.a.servo_write(self.SERVO_PIN,90) 
     self.a.close() 


    def on_frame(self, controller): 

     # we only want to get the position of the hand every so often 
     self.newtime = time.time() 
     if self.newtime-self.oldtime > 0.1: # if difference between times is 10ms 
      # Get the most recent frame and report some basic information 
      frame    = controller.frame() 
      interaction_box  = frame.interaction_box 
      normalized_point = None 

      # Get hands 
      for hand in frame.hands: 

       handType = "Left hand" if hand.is_left else "Right hand" 
       print handType 
       normalized_point = interaction_box.normalize_point(hand.palm_position,True) 


      # FIXME depending on orientation of servo motor 
      # if motor is upright, Leap Device will register a 0 degree angle if hand is all the way to the left 
      if normalized_point: 
       XPOS_servo = abs(self.AZIMUTHAL_LIMIT-normalized_point.x*self.AZIMUTHAL_LIMIT) 
       # write the value to servo on arduino 
       self.a.servo_write(self.SERVO_PIN,int(XPOS_servo)) # turn LED on 

      # update the old time 
      self.oldtime = self.newtime 
     else: 
      pass # keep advancing in time until 10 millisecond is reached 


def main(): 
    # Create a sample listener and controller 
    listener = SampleListener() 
    controller = Leap.Controller() 
    # Have the sample listener receive events from the controller 
    controller.add_listener(listener) 
    # Keep this process running until Enter is pressed 
    try: 
     sys.stdin.readline() 
    except KeyboardInterrupt: 
     pass 
    finally: 
     # Remove the sample listener when done 
     controller.remove_listener(listener) 

if __name__ == "__main__": 
    main() 
+0

謝謝!我調整了一下,現在它運行,我可以看到handType,但它不寫入伺服,你知道爲什麼嗎? – Engima

0

我可以建議一個快速的解決方法。程序中的範圍定義似乎存在問題。我認爲你應該移動「全局變量」的定義,)變量)以外的類定義。這樣,訪問它們不會引發任何投訴。

... 
SERVO_PIN = 2 # Azimuthal Servo motor pin 
AZIMUTHAL_LIMIT = 180 # we want our motor to go between 0 and 180 
class SampleListner(Leap.listener): 
... 

另請注意,已經指出了以前的答案......在壓痕遵循的標準(4位),它會爲你節省很多的挫折。

相關問題