简体   繁体   English

Raspberry Pi 连接到电机控制器 - 代码中出现错误

[英]Raspberry Pi connected to Motor Controller- Getting error in code

*Hi, I am getting this code error:
*blink.py:25: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.*
GPIO.setup(ENA1,GPIO.OUT)
Traceback (most recent call last):
File "/home/pi/my_python_programs/blink.py", line 27, in <module>
GPIO.output(IN1,GPIO.LOW)
RuntimeError: The GPIO channel has not been set up as an OUTPUT*

It is in reference to this code I made to control a motor controller from the raspberry pi.这是参考我为从树莓派控制电机 controller 而编写的代码。 Here is my code.这是我的代码。 I am wondering what the error is:我想知道错误是什么:

#ImportedLibraries
import RPi.GPIO as GPIO
from time import sleep
#Motor1
PWR= 17
ENA1 = 33
IN1 = 31
IN2 = 29
GND = 39
#Motor2
PWR = 1
ENA2 = 32
IN3 = 18
IN4 = 16
GND = 34
#SetMode
GPIO.setmode(GPIO.BOARD)
#Motor1
GPIO.setup(ENA1,GPIO.OUT)
PWM1=GPIO.PWM(ENA1,100)
GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.LOW)
#Motor2
GPIO.setup(ENA2,GPIO.OUT)
PWM2=GPIO.PWM(ENA2,100)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.LOW)
PWM1.start(10)

It seems that the setup is not been doing very well, it fails in line 25.似乎设置不是很好,它在第 25 行失败。

If you want to control a motor you will need a H bridge, like L293D or L298, and then use the gpiozero library, look for it in the net.如果你想控制电机,你需要一个 H 桥,比如 L293D 或 L298,然后使用 gpiozero 库,在网上寻找它。

Try to use this code:尝试使用此代码:

from gpiozero import Motor
from time import sleep

motor = Motor(forward=4, backward=14)

while True:
    motor.forward()
    sleep(5)
    motor.backward()
    sleep(5)

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

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