在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基础3所述-如何干净地退出GPIO程序,避免警告并保护您的Pi

RPi.GPIO提供内置函数GPIO.cleanup()来清理您已使用的所有端口。但是要非常清楚这是做什么的。它仅影响您在当前程序中设置的任何端口。它将在此程序中使用的所有端口重设为输入模式。例如,这可以防止因端口设置为高电平而将输出端口设置为高电平而意外将其连接至GND(LOW)的情况,从而导致端口短路并炸掉。输入可以处理0V(LOW)或3.3V(HIGH),因此将端口保留为输入更为安全。

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

如果确实清除了所有端口,这意味着您可能在不同程序之间存在重大冲突,甚至可能不想使用相同的端口。显然,这不是理想的情况!


PS:有一个Raspberry Pi StackExchange

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Raspberry Pi 2 Netflix

来自分类Dev

Raspberry Pi 2 Netflix

来自分类Dev

从Raspberry Slave从Raspberry PI PI SPI读取与wireingPI2?

来自分类Dev

在Raspberry Pi中安装QtQml

来自分类Dev

使用Raspberry Pi作为VPN?

来自分类Dev

在Raspberry Pi中使用Google动态DNS

来自分类Dev

如何在Raspberry Pi中同步时钟?

来自分类Dev

如何使用Raspberry Pi控制灯泡-要求

来自分类Dev

交叉编译Raspberry Pi 2错误

来自分类Dev

Raspberry Pi2上的FreeBSD

来自分类Dev

交叉编译Raspberry Pi 2错误

来自分类Dev

DNXCore ARM在Raspberry Pi 2上发布

来自分类Dev

Raspberry Pi 2上的MongoDb性能

来自分类Dev

如何在Raspberry Pi上运行javafx

来自分类Dev

如何通过SSH访问Raspberry Pi Zero

来自分类Dev

如何在Raspberry Pi上安装FreeRTOS?

来自分类Dev

如何用QEMU模拟Raspberry Pi Raspbian?

来自分类Dev

如何每周重新启动Raspberry PI

来自分类Dev

如何远程访问我的Raspberry Pi?

来自分类Dev

如何找到Raspberry PI B的UBootloader地址?

来自分类Dev

Raspberry PI上NodeJS中的long setTimeout

来自分类Dev

在Raspberry Pi中创建iBeacon服务

来自分类Dev

汇编中从Raspberry Pi的UART RxD读取

来自分类Dev

在Raspberry Pi或Ubuntu中创建RDP网关

来自分类Dev

Debian 中的根目录(Raspberry pi)

来自分类Dev

在Raspberry Pi上使用pygame撕裂屏幕

来自分类Dev

使用Nginx在Raspberry Pi上部署Flask

来自分类Dev

使用spidev的linux(raspberry PI)上的SPI

来自分类Dev

使用Raspberry Pi作为VPN的TOR中继