我是Python的新手,发现了这段代码here。它继续给我这个错误,我很困惑该怎么做。
在on_frame上输入第78行文件“my_first_leap.py” 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 :(得分: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()
答案 1 :(得分: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个空格),它会为你节省很多挫折。