函数名以前定义过,但未命名

2024-10-01 04:56:28 发布

您现在位置: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]

我真的希望有人能帮我。我试着把自我放在一切的前面,只把自我放在变量上,不把自我放在任何事情上。我真是不知所措。如果你们能帮助我,我将不胜感激。提前谢谢


Tags: theinselfforoutputgpiotimedef
1条回答
网友
1楼 · 发布于 2024-10-01 04:56:28

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()

相关问题 更多 >