8赞

5回答

2收藏

[原创] 树莓派/Raspberry Pi控制步进电机(附代码)!!!!

 

Raspberry Pi Raspberry Pi 15389 人阅读 | 5 人回复 | 2012-09-06

本帖最后由 GY@艳 于 2012-10-12 17:20 编辑

原帖由
ukonline2000


本来打算写arduino的代码,后来发现网上有个步进电机的代码,于是用这个代码试了试驱动步进电机,效果不错!







材料:
1.树莓派
2.全功能扩展版
3.步进电机28BYJ-48-5V或者28BYJ-48-12V

使用GPIO0-GPIO3

python代码(这里要感谢matt.hawkins提供的源代码
Stepper Motor.zip (903 Bytes, 下载次数: 117)

复制代码
#-----------------------------------
# Name: Stepper Motor
#
# Author: matt.hawkins
#
# Created: 11/07/2012
# Copyright: (c) matt.hawkins 2012
#-----------------------------------
#!/usr/bin/env python
# Import required libraries
import time
import RPi.GPIO as GPIO
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO signals to use
# GPIO24,GPIO25,GPIO8,GPIO7
StepPins = [17,18,21,22]
# Set all pins as output
for pin in StepPins:
print "Setup pins"
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
# Define some settings
StepCounter = 0
WaitTime = 0
# Define simple sequence
StepCount1 = 4
Seq1 = []
Seq1 = range(0, StepCount1)
Seq1[0] = [1,0,0,0]
Seq1[1] = [0,1,0,0]
Seq1[2] = [0,0,1,0]
Seq1[3] = [0,0,0,1]
# Define advanced sequence
# as shown in manufacturers datasheet
StepCount2 = 8
Seq2 = []
Seq2 = range(0, StepCount2)
Seq2[0] = [1,0,0,0]
Seq2[1] = [1,1,0,0]
Seq2[2] = [0,1,0,0]
Seq2[3] = [0,1,1,0]
Seq2[4] = [0,0,1,0]
Seq2[5] = [0,0,1,1]
Seq2[6] = [0,0,0,1]
Seq2[7] = [1,0,0,1]
# Choose a sequence to use
Seq = Seq1
StepCount = StepCount1
# Start main loop
while 1==1:
for pin in range(0, 4):
xpin = StepPins[pin]
if Seq[StepCounter][pin]!=0:
print " Step %i Enable %i" %(StepCounter,xpin)
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)
StepCounter += 1
# If we reach the end of the sequence
# start again
if (StepCounter==StepCount):
StepCounter = 0
if (StepCounter<0):
StepCounter = StepCount
# Wait before moving on
time.sleep(WaitTime)


爱板  爱板  爱板     https://www.cirmall.com
分享到:
回复

使用道具 举报

回答|共 5 个

倒序浏览

沙发

fengye5340-272571

发表于 2012-9-26 19:15:29 | 只看该作者

资料不错啊,这个串口线很漂亮啊,是随班子带的吗?
板凳

GY@艳

发表于 2012-9-27 08:41:51 | 只看该作者

fengye5340 发表于 2012-9-26 19:15
资料不错啊,这个串口线很漂亮啊,是随班子带的吗?

这个我不知道呢,得问原创了。
爱板  爱板  爱板     https://www.cirmall.com
地板

fengye5340-272571

发表于 2012-9-27 09:50:32 | 只看该作者

GY@艳 发表于 2012-9-27 08:41
这个我不知道呢,得问原创了。

哦,好的,非常感谢!
5#

GY@艳

发表于 2012-9-27 10:15:07 | 只看该作者

fengye5340 发表于 2012-9-27 09:50
哦,好的,非常感谢!

呵呵,小事儿啦
爱板  爱板  爱板     https://www.cirmall.com
6#

小米粒稻草

发表于 2015-6-25 12:54:33 | 只看该作者

支持一下   不错
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /2 下一条