2015-10-15 55 views
2

我是Raspberry Pi的新手,我试图通过开关引脚来使电机工作。它的正常工作,当我尝试设置引脚但是我得到这些警告:如何在Raspberry Pi 2中使用后发布频道?

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) 

回答

2

你应该在你的程序结束时调用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提供了一个内置函数GPIO.cleanup()来清理所有的 p或者你已经使用过。但要清楚这是干什么的。它只有 影响您在当前程序中设置的任何端口。它会将您在此程序中使用的任何 端口重置为输入模式。这样可以防止 的损坏,比如说将端口设置为高电平作为 输出,并且您意外地将其连接到GND(LOW),这会使端口短路并可能将其油炸。输入可以处理 0V(低)或3.3V(高),因此将端口作为输入更安全。

而且GPIO.cleanup()不清理所有端口,因为:

如果它没有清理干净所有的端口,这意味着你可以有不同的程序之间的主要 冲突,这甚至可能不会尝试 使用相同的端口。显然,不是一个理想的情况!


P.S .:有一个Raspberry Pi StackExchange