简体   繁体   English

通过一个程序控制RFID RC522和伺服电机

[英]Controlling RFID RC522 and servo motor in one program

I'm working on a project where I'm trying to turn a 5V servo motor (9g) when RFID RC522 detects a card. 我正在一个项目中,当RFID RC522检测到卡时,我试图转动5V伺服电机(9g)。 I'm using a Raspberry Pi 3 B+, Python RPi.GPIO lib and another lib: SimpleMFRC522 for the card reader. 我正在使用Raspberry Pi 3 B +,Python RPi.GPIO库和另一个库:用于读卡器的SimpleMFRC522。

I run into a problem where I can't set pins for the servo because of the SimpleMFRC522. 由于SimpleMFRC522,我遇到了无法设置伺服器引脚的问题。 I'm getting this error: 我收到此错误:

File "test.py", line 39, in <module>
  unlock_cooler()
  File "test.py", line 21, in unlock_cooler
  GPIO.SETUP(7, GPIO.OUT)
AttributeError: 'module' object has no attribute 'SETUP'

Is there any ways to change the GPIO setup and using the servo together with the SimpleMFRC522 lib? 有什么方法可以更改GPIO设置以及将伺服器与SimpleMFRC522库一起使用吗?

#!/usr/bin/env python

import RPi.GPIO as GPIO
import SimpleMFRC522
import re

rfid = 0

def read_RFID():
    reader = SimpleMFRC522.SimpleMFRC522()
    id, text = reader.read()
    clean_text = re.findall('\d+', text)
    match = int(clean_text[0])
    rfid = match
    GPIO.cleanup()


def unlock_cooler():

    GPIO.SETUP(7, GPIO.OUT)
    p = GPIO.PWM(7, 50)

    p.start(2.5)
    p.ChangeDutyCycle(7.5)
    time.sleep(3)
    p.ChangeDutyCycle(2.5)
    time.sleep(1)
    GPIO.cleanup()


read_RFID()
print(rfid)
if rfid == 6:
    unlock_cooler()

GPIO.cleanup()

The setup method is named GPIO.setup() and not GPIO.SETUP() (note the lower-case characters!). 设置方法名为GPIO.setup()而不是GPIO.SETUP() (请注意小写字符!)。

So changing the method unlock_cooler to this should solve the error that you got: 因此,将方法unlock_cooler更改为此可以解决您得到的错误:

def unlock_cooler():
    GPIO.setup(7, GPIO.OUT)
    p = GPIO.PWM(7, 50)
    p.start(2.5)
    p.ChangeDutyCycle(7.5)
    time.sleep(3)
    p.ChangeDutyCycle(2.5)
    time.sleep(1)
    p.stop()
    GPIO.cleanup()

Note that you would probably aslo want to call stop() on the PWM instance. 请注意,您可能还想在PWM实例上调用stop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM