我是Python的新手,虽然我明白我需要知道什么。除了这一部分。我一直试图在我的Raspberry Pi 2B上测试我的程序一段时间,但无论我尝试做什么,它都会一直回复这个错误:
Traceback (most recent call last):
File "/home/pi/Tester.py", line 1, in <module>
import Solve
File "/home/pi/Solve.py", line 7, in <module>
class Solve:
File "/home/pi/Solve.py", line 185, in Solve
motions = {"D" : bottomFace(1),
File "/home/pi/Solve.py", line 103, in bottomFace
grab()
NameError: name 'grab' is not defined
我不知道如何解决它,纯粹是因为它说“抓取”尚未定义,但它被定义为代码中较早的函数。以下是您愿意帮助我的代码:
import cv2 as cv
import time
import colorsys
import RPi.GPIO as gpio
import kociemba as solver
class Solve:
def __init__(self):
#create a video stream
self.cap = cv.VideoCapture(0)
self.flipperMotorA = 23
self.flipperMotorB = 24
self.flipperMotorE = 25
self.platformMotorA = 12
self.platformMotorB = 6
self.platformMotorE = 5
#To be passed to the solver code
self.colors = ""
#To store the solution
self.solution=""
self.pixelsToScan = [[50,50],[320,50],[590,50],[50,240],[320,240],[590,240],[50,430],[320,430],[590,430]]
def setup():
#setup the GPIO ports
gpio.setmode(gpio.BCM)
gpio.setup(platformMotorA, gpio.OUT)
gpio.setup(platformMotorB, gpio.OUT)
gpio.setup(platformMotorE, gpio.OUT)
gpio.setup(flipperMotorA, gpio.OUT)
gpio.setup(flipperMotorB, gpio.OUT)
gpio.setup(flipperMotorE, gpio.OUT)
#Turn the platform clockwise
def platformClockwise():
gpio.output(platformMotorA, gpio.HIGH)
gpio.output(platformMotorB, gpio.LOW)
gpio.output(platformMotorE, gpio.HIGH)
time.sleep(.25)
gpio.output(platformMotorA, gpio.LOW)
gpio.output(platformMotorB, gpio.LOW)
gpio.output(platformMotorE, gpio.LOW)
time.sleep(.5)
#Flip the cube's face
def flip():
gpio.output(flipperMotorA, gpio.LOW)
gpio.output(flipperMotorB, gpio.HIGH)
gpio.output(flipperMotorE, gpio.HIGH)
time.sleep(1.25)
gpio.output(flipperMotorA, gpio.HIGH)
gpio.output(flipperMotorB, gpio.LOW)
gpio.output(flipperMotorE, gpio.HIGH)
time.sleep(1.25)
gpio.output(flipperMotorA, gpio.LOW)
gpio.output(flipperMotorB, gpio.LOW)
gpio.output(flipperMotorE, gpio.LOW)
time.sleep(.5)
#Grab the cube to turn one face
def grab():
gpio.output(flipperMotorA, gpio.LOW)
gpio.output(flipperMotorB, gpio.HIGH)
gpio.output(flipperMotorE, gpio.HIGH)
time.sleep(.8)
gpio.output(flipperMotorA, gpio.LOW)
gpio.output(flipperMotorB, gpio.LOW)
gpio.output(flipperMotorE, gpio.LOW)
time.sleep(.5)
#release the cube
def release():
gpio.output(flipperMotorA, gpio.HIGH)
gpio.output(flipperMotorB, gpio.LOW)
gpio.output(flipperMotorE, gpio.HIGH)
time.sleep(.8)
gpio.output(flipperMotorA, gpio.LOW)
gpio.output(flipperMotorB, gpio.LOW)
gpio.output(flipperMotorE, gpio.LOW)
time.sleep(1)
#rotate the bottom face
def bottomFace(amount):
grab()
for i in range(1,amount):
platformClockwise()
release()
#rotate the back face
def backFace(amount):
flip()
grab()
for i in range(1,amount):
platformClockwise()
release()
platformClockwise()
platformClockwise()
flip()
platformClockwise()
platformClockwise()
#rotate the front face
def frontFace(amount):
platformClockwise()
platformClockwise()
flip()
grab()
for i in range(1,amount):
platformClockwise()
release()
platformClockwise()
platformClockwise()
flip()
#rotate the left face
def leftFace(amount):
platformClockwise()
flip()
grab()
for i in range(1,amount):
platformClockwise()
release()
platformClockwise()
platformClockwise()
flip()
platformClockwise()
#rotate the right face
def rightFace(amount):
platformClockwise()
platformClockwise()
platformClockwise()
flip()
grab()
for i in range(1,amount):
platformClockwise()
release()
platformClockwise()
platformClockwise()
flip()
platformClockwise()
platformClockwise()
platformClockwise()
#rotate the top face
def topFace(amount):
flip()
flip()
for i in range(1,amount):
platformClockwise()
flip()
flip()
motions = {"D" : bottomFace(1),
"D2" : bottomFace(2),
"D'" : bottomFace(3),
"B" : backFace(1),
"B2" : backFace(2),
"B'" : backFace(3),
"U" : topFace(1),
"U2" : topFace(2),
"U'" : topFace(3),
"F" : frontFace(1),
"F2" : frontface(2),
"F'" : frontFace(3),
"L" : leftFace(1),
"L2" : leftFace(2),
"L'" : leftFace(3),
"R" : rightFace(1),
"R2" : rightFace(2),
"R'" : rightFace(3)}
#Function for scanning every side of the cube
def scanSides():
setup()
for i in range(0,5): #For each side add the colors to a list
#Get the frame with the cube
rgbimg = cap.read()
#scan each pixel in the list
for a in pixelsToScan:
hue = colorsys.rgb_to_hsv(rgbimg[a[0],a[1],0],rgbimg[a[0],a[1],1],rgbimg[a[0],a[1],2])[0]*360 #get the hue of the pixel
sat = colorsys.rgb_to_hsv(rgbimg[a[0],a[1],0],rgbimg[a[0],a[1],1],rgbimg[a[0],a[1],2])[1]
val = colorsys.rgb_to_hsv(rgbimg[a[0],a[1],0],rgbimg[a[0],a[1],1],rgbimg[a[0],a[1],2])[2]
if sat==0 and val==1:
#WHITE
colors.append("U")
elif hue<15 or hue>=295:
#RED
colors.append("F")
elif hue>=15 and hue<40:
#ORANGE
colors.append("B")
elif hue>=40 and hue<75:
#YELLOW
colors.append("D")
elif hue>=75 and hue<165:
#GREEN
colors.append("L")
elif hue>=165 and hue<295:
#BLUE
colors.append("R")
else:
#BROKEN
colors.append("E")
if i<=2:
platformClockwise()
elif i==3:
flip()
elif i==4:
flip()
flip()
else:
flip()
solution = solver.solve(colors)
for move in solution.split(" "):
#RUN the solution
motions[move]
我真的希望有人可以帮助我。我已经尝试将自我置于一切之前,只将自我置于变量之上,而不是将自我置于任何事物上。我真的很茫然。如果您有任何人可以帮助我,我将不胜感激。提前谢谢!
答案 0 :(得分:0)
将self
作为参数添加到您的方法中,然后在从另一个方法调用方法时,调用它们:self.grab()
。请查看What is the purpose of self?和其他文档。
示例:
def setup(self):
#setup the GPIO ports
gpio.setmode(gpio.BCM)
gpio.setup(self.platformMotorA, gpio.OUT) # notice self.platformMotorA
def grab(self):
gpio.output(self.flipperMotorA, gpio.LOW)
gpio.output(self.flipperMotorB, gpio.HIGH)
gpio.output(self.flipperMotorE, gpio.HIGH)
time.sleep(.8)
gpio.output(self.flipperMotorA, gpio.LOW)
gpio.output(self.flipperMotorB, gpio.LOW)
gpio.output(self.flipperMotorE, gpio.LOW)
time.sleep(.5)
#release the cube
def release(self):
gpio.output(self.flipperMotorA, gpio.HIGH)
gpio.output(self.flipperMotorB, gpio.LOW)
gpio.output(self.flipperMotorE, gpio.HIGH)
time.sleep(.8)
gpio.output(self.flipperMotorA, gpio.LOW)
gpio.output(self.flipperMotorB, gpio.LOW)
gpio.output(self.flipperMotorE, gpio.LOW)
time.sleep(1)
#rotate the bottom face
def bottomFace(self, amount):
self.grab()
for i in range(1, amount):
self.platformClockwise()
self.release()