python - 树莓派步进电机
问题描述
我是编码新手,需要在我的高级顶点项目中使用 python。我希望这段代码允许我的步进电机上下移动,所以我想输入两个允许这样做的值。此外,我想添加允许代码持续运行的代码,并在我简单地打开机器电源时运行
import RPi.GPIO as GPIO
import time
out1 = 13
out2 = 11
out3 = 15
out4 = 12
i=0
positive=0
negative=0
y=0
GPIO.setmode(GPIO.BOARD)
GPIO.setup(out1,GPIO.OUT)
GPIO.setup(out2,GPIO.OUT)
GPIO.setup(out3,GPIO.OUT)
GPIO.setup(out4,GPIO.OUT)
print "First calibrate by giving some +ve and -ve values....."
try:
while(1):
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
x = input()
if x>0 and x<=400:
for y in range(x,0,-1):
if negative==1:
if i==7:
i=0
else:
i=i+1
y=y+2
negative=0
positive=1
#print((x+1)-y)
if i==0:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.03)
#time.sleep(1)
elif i==1:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.03)
#time.sleep(1)
elif i==2:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.03)
#time.sleep(1)
elif i==3:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.03)
#time.sleep(1)
elif i==4:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.03)
#time.sleep(1)
elif i==5:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.03)
#time.sleep(1)
elif i==6:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.03)
#time.sleep(1)
elif i==7:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.03)
#time.sleep(1)
if i==7:
i=0
continue
i=i+1
elif x<0 and x>=-400:
x=x*-1
for y in range(x,0,-1):
if positive==1:
if i==0:
i=7
else:
i=i-1
y=y+3
positive=0
negative=1
#print((x+1)-y)
if i==0:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.03)
#time.sleep(1)
elif i==1:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.03)
#time.sleep(1)
elif i==2:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.03)
#time.sleep(1)
elif i==3:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.03)
#time.sleep(1)
elif i==4:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.03)
#time.sleep(1)
elif i==5:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.03)
#time.sleep(1)
elif i==6:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.03)
#time.sleep(1)
elif i==7:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.03)
#time.sleep(1)
if i==0:
i=7
continue
i=i-1
except KeyboardInterrupt:
GPIO.cleanup()'
我可以输入我想要的值,步进电机将旋转该值
解决方案
推荐阅读
- php - 在 laravel 中哪里可以找到 Auth::attempt 方法
- json - 如何在 JSON 中创建树结构表示?
- excel - Excel VBA - 如果从 VBS 调用范围方法失败
- javascript - 说明:for-loop 上的 Ajax
- python - 如何转储(和重新加载)django allauth 社交应用程序数据到/从文件
- c# - 当重复调用相同的函数时,编码的 UI 测试会停止而不停止
- reactjs - Mocha 在导入时无法识别 React
- reactjs - 如何将 .Net Core WebAPI 和 React JS 应用程序部署到单个 Docker 容器
- c# - 如何以编程方式放大?
- tensorflow - 使用 tensorflow 训练 mnist 数字数据集时的密集形状错误