-1

i am programming a GUI with Qt5 that uses 4 buttons that turn a stepper motor. I just took this setup i found on the internet. Here is my code:

from PyQt5 import QtCore, QtGui, QtWidgets
from time import sleep
import RPi.GPIO as GPIO
import sys

class Ui_Widget(object):

    def __init__(self):
        DIR = 20   # Direction GPIO Pin
        STEP = 21  # Step GPIO Pin
        CW = 1     # Clockwise Rotation
        CCW = 0    # Counterclockwise Rotation
        SPR = 100   # Steps per Revolution (360 / 1.8)    
        M0 = 14    # Microstepping
        M1 = 15    
        M2 = 18
        EN = 2    

        GPIO.setmode(GPIO.BCM)
        GPIO.setup(DIR, GPIO.OUT)
        GPIO.setup(STEP, GPIO.OUT)
        GPIO.output(DIR, CW)

        GPIO.setup(M0, GPIO.OUT)
        GPIO.setup(M1, GPIO.OUT)
        GPIO.setup(M2, GPIO.OUT)
        GPIO.setup(EN, GPIO.OUT)

        GPIO.output(M0, GPIO.LOW)
        GPIO.output(M1, GPIO.HIGH)
        GPIO.output(M2, GPIO.LOW)
        GPIO.output(EN, GPIO.LOW)

        step_count = SPR
        delay = .007 #.0208 

    def setupUi(self, Widget):
        Widget.setObjectName("Widget")
        Widget.resize(685, 491)
        self.horizontalLayoutWidget = QtWidgets.QWidget(Widget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(11, 10, 661, 471))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(11, 11, 11, 11)
        self.horizontalLayout.setSpacing(6)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton_3 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.pushButton_3.setObjectName("pushButton_3")
        self.horizontalLayout.addWidget(self.pushButton_3)
        self.pushButton_4 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.pushButton_4.setObjectName("pushButton_4")
        self.horizontalLayout.addWidget(self.pushButton_4)
        self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout.addWidget(self.pushButton_2)
        self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)

        self.retranslateUi(Widget)
        QtCore.QMetaObject.connectSlotsByName(Widget)

    def retranslateUi(self, Widget):
        _translate = QtCore.QCoreApplication.translate
        Widget.setWindowTitle(_translate("Widget", "Widget"))
        self.pushButton_3.setText(_translate("Widget", "Twice 2 Left"))
        self.pushButton_4.setText(_translate("Widget", "Once 2 Left"))
        self.pushButton_2.setText(_translate("Widget", "Once 2 Right"))
        self.pushButton.setText(_translate("Widget", "Twice 2 Right"))

        self.pushButton_3.clicked.connect(self.ZweiRechts)
        self.pushButton_4.clicked.connect(self.EinsRechts)
        self.pushButton.clicked.connect(self.ZweiLinks)
        self.pushButton_2.clicked.connect(self.EinsLinks)

    def EinsLinks(self):

        GPIO.output(DIR, CCW)
        for x in range(step_count):
            GPIO.output(STEP, GPIO.HIGH)
            sleep(delay)
            GPIO.output(STEP, GPIO.LOW)
            sleep(delay)

    def ZweiLinks(self):

        GPIO.output(DIR, CCW)
        for x in range(step_count*2):
            GPIO.output(STEP, GPIO.HIGH)
            sleep(delay)
            GPIO.output(STEP, GPIO.LOW)
            sleep(delay)

    def EinsRechts(self):

        for x in range(step_count):
            GPIO.output(STEP, GPIO.HIGH)
            sleep(delay)
            GPIO.output(STEP, GPIO.LOW)
            sleep(delay)

    def ZweiRechts(self):

        for x in range(step_count*2):
            GPIO.output(STEP, GPIO.HIGH)
            sleep(delay)
            GPIO.output(STEP, GPIO.LOW)
            sleep(delay)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Ui_Widget()
    w = QtWidgets.QWidget()
    ex.setupUi(w)
    w.show()
    sys.exit(app.exec_())
    GPIO.cleanup()

The error i get when i press the button depends if i use one of the Rechts functions or a Links function. For Rechts function error is:

File "widget_ui.py", line 112, in ZweiRechts
    for x in range(step_count*2):
NameError: name 'step_count' is not defined

For Links functions error is:

File "widget_ui.py", line 86, in EinsLinks
    GPIO.output(DIR, CCW)
NameError: name 'DIR' is not defined

Anyone have a idea why the errors are different? What can i do to prevent this from happening? If you have any questions feel free to ask.

Greets,

Xeno

  • This question is not related to the Pi as your problem is purely with the Python code. – Darth Vader Jan 11 '18 at 18:10
1

This is a general Python question, not specific to the Raspberry Pi.

When using class variables within class functions in Python you need to use self.

So for x in range(self.step_count*2): and GPIO.output(self.DIR, self.CCW).

  • now the error is: Traceback (most recent call last): File "widget_ui.py", line 112, in ZweiRechts for x in range(self.step_count*2): AttributeError: 'Ui_Widget' object has no attribute 'step_count' – Xenoshell Jan 10 '18 at 12:04
  • As @joan mentioned this has nothing to do with RPi. It is a python basic 101. You need to have better understanding on python Class variables and instance variables. Maybe this stack overflow question and answer will help you to have better understanding. – hcheung Jan 10 '18 at 12:39
  • ok i was just missing some self. on some variables that was all. Thanks for the help. Well i should have added that i am doing all the controlling with a raspberry pi. I know its more of a programming issue but i wanted to ask here too – Xenoshell Jan 10 '18 at 12:40
  • @Xenoshell Generally you will get a quicker and better response to general programming questions on a general programming site. That's one of the reasons we may close questions as being off topic. – joan Jan 10 '18 at 12:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.