0

I am fairly new to Python, though I understand what I need to know. Except this part. I have been trying to test my program on my Raspberry Pi 2B for a while, but no matter what I try to do, it keeps returning this error:

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

I have no idea of how to fix it, purely because it says that "grab" has not been defined, but it was defined as a function earlier in the code. Here is the code for those of you willing to help me:

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]

I seriously hope someone can help me. I have tried putting self in front of everything, only putting self on variables, not putting self on anything. I really am at a loss. If any of you can help me, that would be greatly appreciated. Thanks in advance!

MattChris
  • 397
  • 4
  • 19
  • 1
    That's a lot of code! Please trim it down to a [mcve]. But you seem to not understand how class functions work. – user3483203 Jun 12 '18 at 15:36
  • 1
    add `self` as first argument to your functions. Then I guess in Tester.py you should have someting like `solver = Solve()` and then you should call `grab` with `solver.grab()` – ted Jun 12 '18 at 15:36
  • 1
    Before trying to fix this code, it would be much more helpful for you to read https://docs.python.org/3/tutorial/classes.html – user3483203 Jun 12 '18 at 15:40

1 Answers1

0

Add self as argument to your methods and then when calling a method from another method, call them like this: self.grab(). Take a look at What is the purpose of self? and other docs.

Example:

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()
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45