fresa frambuesa
fresa frambuesa

Reputation: 41

RuntimeWarning with a code for stepper motor

In order to make our motor run, we are trying this code but apparently it has many errors. Can someone help us?

RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)

pinDir = 24
pinStep = 26
numSteps = 200
microPausa = 0.005


GPIO.setup(pinDir,GPIO.OUT)
GPIO.setup(pinStep,GPIO.OUT)

while True:

        GPIO.output(pinDir,0)

        for i in range(0,numSteps):
                GPIO.output(pinStep, True)
                time.sleep(microPausa)
                GPIO.output(pinStep, False)
                time.sleep(microPausa)

        time.sleep(microPausa)

        GPIO.output(pinDir, 1)

        for i in range(0,numSteps):
                GPIO.output(pinStep, True)
                time.sleep(microPausa)
                GPIO.output(pinStep, False)
                time.sleep(microPausa)

GPIO.cleanup()

carlini.py:12: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(pinDir,GPIO.OUT) carlini.py:13: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(pinStep,GPIO.OUT)

Upvotes: 2

Views: 1549

Answers (2)

ben_nuttall
ben_nuttall

Reputation: 929

A RuntimeWarning is just a warning not an error. It will carry on while giving you a warning. If you read it, it actually tells you what the warning is for, and how to prevent it:

This channel is already in use, continuing anyway.

Use GPIO.setwarnings(False) to disable warnings.

This means the GPIO pin you're using has been set up before, and not cleaned up. This is not really a problem for you as you're just re-running your code. You can disable warnings by adding Use GPIO.setwarnings(False) to the top of your file (after GPIO.setmode).

Upvotes: 3

pittix
pittix

Reputation: 171

Did you run it multiple times and stopped it via a keyboard interrupt (like ctrl+c)? If so, the GPIOs are still set.

You may want to use a try/except/finally block, to properly unset the GPIOs:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)

pinDir = 24
pinStep = 26
numSteps = 200
microPausa = 0.005


GPIO.setup(pinDir,GPIO.OUT)
GPIO.setup(pinStep,GPIO.OUT)
try:
    while True:

        GPIO.output(pinDir,0)

        for i in range(0,numSteps):
                GPIO.output(pinStep, True)
                time.sleep(microPausa)
                GPIO.output(pinStep, False)
                time.sleep(microPausa)

        time.sleep(microPausa)

        GPIO.output(pinDir, 1)

        for i in range(0,numSteps):
                GPIO.output(pinStep, True)
                time.sleep(microPausa)
                GPIO.output(pinStep, False)
                time.sleep(microPausa)
except KeyboardInterrupt:
        pass # or print("received a keyboard interrupt, exiting.")
finally:
    GPIO.cleanup()

Mind that the first time you run this script, you will get the same warning (as it comes from previous executions), unless you run the python script

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.cleanup()

Upvotes: 0

Related Questions