python - 先前定义的函数名,但无法调用
问题描述
我对 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
我不知道如何修复它,纯粹是因为它说“grab”尚未定义,但它在代码的前面被定义为一个函数。以下是愿意帮助我的人的代码:
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]
我真的希望有人能帮助我。我试过把 self 放在一切前面,只把 self 放在变量上,而不把 self 放在任何东西上。我真的很茫然。如果你们中的任何人可以帮助我,那将不胜感激。提前致谢!
解决方案
添加self
为您的方法的参数,然后在从另一个方法调用方法时,像这样调用它们self.grab()
:看看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()
推荐阅读
- python - 如何使用 setup.py 从自托管位置在 zip 文件中安装 python 包?
- java - 在 spring rest Api 中启用 SSL (HTTPS)
- ajax - 使用 ajax 发送 >1 GB 数组缓冲区以制作字符串文件
- python - 比较两个数据框并替换列值
- events - CLI Dart:onPause、onResume、onDone 未按预期启动
- c# - 动态库 (System.Linq.Dynamic) SQL LIKE 运算符
- javascript - Owl-Carousel2 - 设置点数据
- odoo - 如何使用函数在树视图中创建字段?
- c++ - 当试图将数据插入到数据类型结构的向量中时,它返回错误的大小
- apache-kafka - 在 Kafka 中跨分区排序