Raspberry Pi2中使用后如何释放频道?
How to release channel after use in Raspberry Pi 2?
我是 Raspberry Pi 的新手,我正在尝试通过打开和关闭引脚来让电机工作。它工作正常,但是当我尝试设置引脚时收到这些警告:
test2.py:17: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(pin1,GPIO.OUT)
test2.py:18: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(pin2,GPIO.OUT)
test2.py:19: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(pin3,GPIO.OUT)
它似乎没有引起任何问题,因为电机仍在工作,但如果可能的话,我想摆脱它们。
我想我需要在程序结束时以某种方式释放 pin 通道,但我该怎么做?
有关信息,这是我的完整程序:
import RPi.GPIO as GPIO
import time
import sys
pin1=17
pin2=18
pin3=27
pin4=22
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin1,GPIO.OUT)
GPIO.setup(pin2,GPIO.OUT)
GPIO.setup(pin3,GPIO.OUT)
GPIO.setup(pin4,GPIO.OUT)
Apin1=[0,1,0,0,1]
Apin2=[0,1,1,0,0]
Apin3=[0,0,1,1,0]
Apin4=[0,0,0,1,1]
current=0
target=0
def GO_THERE(target,current):
if current<target:
while current<target:
i=current&2 + 1
GPIO.output(pin1,Apin1[i])
GPIO.output(pin2,Apin2[i])
GPIO.output(pin3,Apin3[i])
GPIO.output(pin4,Apin4[i])
time.sleep(.003)
current= current + 1
else:
while current>target:
i=current&2 + 1
GPIO.output(pin1,Apin1[i])
GPIO.output(pin2,Apin2[i])
GPIO.output(pin3,Apin3[i])
GPIO.output(pin4,Apin4[i])
time.sleep(.003)
current= current - 1
print current,target
return current;
target=4096
current=GO_THERE(target,current)
您应该在程序结束时调用 GPIO.cleanup()
:
import RPi.GPIO as GPIO
# your init code
try:
# your main loop
except KeyboardInterrupt:
# handle ctrl-c
except:
# other exceptions
finally:
GPIO.cleanup()
如 RPi.GPIO basics 3 – How to Exit GPIO programs cleanly, avoid warnings and protect your Pi 所述:
RPi.GPIO provides a built-in function GPIO.cleanup() to clean up all
the ports you’ve used. But be very clear what this does. It only
affects any ports you have set in the current program. It resets any
ports you have used in this program back to input mode. This prevents
damage from, say, a situation where you have a port set HIGH as an
output and you accidentally connect it to GND (LOW), which would
short-circuit the port and possibly fry it. Inputs can handle either
0V (LOW) or 3.3V (HIGH), so it’s safer to leave ports as inputs.
并且GPIO.cleanup()
不会清理所有端口,因为:
If it did clean up all the ports, this would mean you could have major
conflicts between different programs, which might not even be trying
to use the same ports. Clearly, not a desirable situation!
P.S.: 有一个Raspberry Pi StackExchange.
我是 Raspberry Pi 的新手,我正在尝试通过打开和关闭引脚来让电机工作。它工作正常,但是当我尝试设置引脚时收到这些警告:
test2.py:17: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(pin1,GPIO.OUT)
test2.py:18: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(pin2,GPIO.OUT)
test2.py:19: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(pin3,GPIO.OUT)
它似乎没有引起任何问题,因为电机仍在工作,但如果可能的话,我想摆脱它们。
我想我需要在程序结束时以某种方式释放 pin 通道,但我该怎么做?
有关信息,这是我的完整程序:
import RPi.GPIO as GPIO
import time
import sys
pin1=17
pin2=18
pin3=27
pin4=22
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin1,GPIO.OUT)
GPIO.setup(pin2,GPIO.OUT)
GPIO.setup(pin3,GPIO.OUT)
GPIO.setup(pin4,GPIO.OUT)
Apin1=[0,1,0,0,1]
Apin2=[0,1,1,0,0]
Apin3=[0,0,1,1,0]
Apin4=[0,0,0,1,1]
current=0
target=0
def GO_THERE(target,current):
if current<target:
while current<target:
i=current&2 + 1
GPIO.output(pin1,Apin1[i])
GPIO.output(pin2,Apin2[i])
GPIO.output(pin3,Apin3[i])
GPIO.output(pin4,Apin4[i])
time.sleep(.003)
current= current + 1
else:
while current>target:
i=current&2 + 1
GPIO.output(pin1,Apin1[i])
GPIO.output(pin2,Apin2[i])
GPIO.output(pin3,Apin3[i])
GPIO.output(pin4,Apin4[i])
time.sleep(.003)
current= current - 1
print current,target
return current;
target=4096
current=GO_THERE(target,current)
您应该在程序结束时调用 GPIO.cleanup()
:
import RPi.GPIO as GPIO
# your init code
try:
# your main loop
except KeyboardInterrupt:
# handle ctrl-c
except:
# other exceptions
finally:
GPIO.cleanup()
如 RPi.GPIO basics 3 – How to Exit GPIO programs cleanly, avoid warnings and protect your Pi 所述:
RPi.GPIO provides a built-in function GPIO.cleanup() to clean up all the ports you’ve used. But be very clear what this does. It only affects any ports you have set in the current program. It resets any ports you have used in this program back to input mode. This prevents damage from, say, a situation where you have a port set HIGH as an output and you accidentally connect it to GND (LOW), which would short-circuit the port and possibly fry it. Inputs can handle either 0V (LOW) or 3.3V (HIGH), so it’s safer to leave ports as inputs.
并且GPIO.cleanup()
不会清理所有端口,因为:
If it did clean up all the ports, this would mean you could have major conflicts between different programs, which might not even be trying to use the same ports. Clearly, not a desirable situation!
P.S.: 有一个Raspberry Pi StackExchange.