1

I wrote two scripts for a robot with belt drives driven by stepper motors. The script "DUALSTEPPER_LIBARY_002.py" contains a DualStepper Class and some functions like forward, backward etc. This script should run in the background by being imported into another script called "Testlauf_001.py".

When I create an instance of the DualStepper class within script "DUALSTEPPER_LIBARY_002.py" and write some code that uses the functions provided I can execute the code without any problems. Motors move properly back and forth.

As soon as I import script "DUALSTEPPER_LIBARY_002.py" into script "Testlauf_001.py" the show stopps with an error message saying:

Traceback (most recent call last):
  File "/home/pi/Documents/PythonFiles/Stepperbox Amazon/Testlauf_001.py", line 7, in <module>
    antriebsmotoren = DualStepper("antriebsmotoren", 20, 16, 21, 12)
NameError: name 'DualStepper' is not defined

So why? There´s a class called DualStepper. How can it not be defined?

IT WOULD BE VERY KIND IF SOMEONE COULD TELL ME MY MISTKAE. THANK YOU

These are the shortest possible code examples to reproduce the problem:

SCRIPT "DUALSTEPPER_LIBARY_002.py":

import time
import RPi.GPIO as GPIO

class DualStepper:
    def __init__(self, motor_name, direction_pin1, direction_pin2, step_pin1, step_pin2):
        self.motor_name = motor_name
        self.direction_pin1 = direction_pin1
        self.direction_pin2 = direction_pin2
        self.step_pin1 = step_pin1
        self.step_pin2 = step_pin2
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)

    def vorwaerts(self, steps=200, stepdelay=.005, initdelay=.05):
        GPIO.setup(self.direction_pin1, GPIO.OUT)
        GPIO.setup(self.direction_pin2, GPIO.OUT)
        GPIO.setup(self.step_pin1, GPIO.OUT)
        GPIO.setup(self.step_pin2, GPIO.OUT)
        GPIO.output(self.direction_pin1, 1)
        GPIO.output(self.direction_pin2, 0)

        for i in range(steps):
            GPIO.output(self.step_pin1, True)
            GPIO.output(self.step_pin2, True)
            time.sleep(stepdelay)
            GPIO.output(self.step_pin1, False)
            GPIO.output(self.step_pin2, False)
            time.sleep(stepdelay)

            antriebsmotoren = DualStepper("antriebsmotoren", 20, 16, 21, 12)

SCRIPT "Testlauf_001.py":

import time
import RPi.GPIO as GPIO
import DUALSTEPPER_LIBARY_002

for n in range (3):
    antriebsmotoren.vorwaerts(steps=400, stepdelay=.001, initdelay=.05)
    time.sleep(.5)

GPIO.cleanup()
  • where do you import `DualStepper` in script B? and what is `DUALSTEPPER_LIBARY_002`? Please share your project structure – Gabio Apr 25 '20 at 14:40
  • Hello Gabip, "DUALSTEPPER_LIBARY_002" is the name of my script "A". I import it to my second script which is called "Testlauf_001.py" Both scripts are in the same folder. – Kinderdisco Goodtime Apr 25 '20 at 15:04

1 Answers1

0

You should initiliaze the antriebsmotoren object outside of your class definition.

Try this:

Script A:

import time
import RPi.GPIO as GPIO

class DualStepper:
    def __init__(self, motor_name, direction_pin1, direction_pin2, step_pin1, step_pin2):
        self.motor_name = motor_name
        self.direction_pin1 = direction_pin1
        self.direction_pin2 = direction_pin2
        self.step_pin1 = step_pin1
        self.step_pin2 = step_pin2
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)

    def vorwaerts(self, steps=200, stepdelay=.005, initdelay=.05):
        GPIO.setup(self.direction_pin1, GPIO.OUT)
        GPIO.setup(self.direction_pin2, GPIO.OUT)
        GPIO.setup(self.step_pin1, GPIO.OUT)
        GPIO.setup(self.step_pin2, GPIO.OUT)
        GPIO.output(self.direction_pin1, 1)
        GPIO.output(self.direction_pin2, 0)

        for i in range(steps):
            GPIO.output(self.step_pin1, True)
            GPIO.output(self.step_pin2, True)
            time.sleep(stepdelay)
            GPIO.output(self.step_pin1, False)
            GPIO.output(self.step_pin2, False)
            time.sleep(stepdelay)

antriebsmotoren = DualStepper("antriebsmotoren", 20, 16, 21, 12)

Script B:

import time
import RPi.GPIO as GPIO
from DUALSTEPPER_LIBARY_002 import antriebsmotoren

for n in range (3):
    antriebsmotoren.vorwaerts(steps=400, stepdelay=.001, initdelay=.05)
    time.sleep(.5)

GPIO.cleanup()
Gabio
  • 9,126
  • 3
  • 12
  • 32