NameError: глобальное имя 'AZIMUTHAL_LIMIT' не определено

Я очень новичок в Python и нашел этот код здесь. Это продолжает давать мне эту ошибку, и я не понимаю, что делать.

Файл "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()

2 ответа

Отступы в скопированном коде все перепутаны.

Также, 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()

Я могу предложить быстрый обходной путь. Кажется, есть проблема с определениями области действия в вашей программе. Я думаю, что вы должны переместить определения "глобальных",) переменные) за пределы определения класса. Таким образом, доступ к ним не вызовет никаких жалоб.

 ...
 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 пробела), это избавит вас от многих разочарований.

Другие вопросы по тегам