|
本帖最后由 hahabear 于 2013-5-9 16:40 编辑
一、拿到树莓派爬墙N天,终于能控制步进电机,代码也是根据他的修改的
参考帖子 http://www.shumeipai.net/thread-540-1-1.html
二、关于GPIO口的说明
参考地址:http://elinux.org/RPi_Low-level_peripherals#Introduction
三、步进电机使用说明
见附件:“步进电机_28BYJ-48介绍和驱动及c编程.doc”和“28BYJ48电机详细使用说明.pdf”
四、硬件连接
需要步进电机驱动板,拿出已经满是灰尘的51单片机的开发板,上面有ULN2003模块。GPIO23、GPIO24、GPIO25、GPIO22分别控制步进电机的A橙、B黄、C粉、D蓝四根信号线。步进电机电源取自单片机开发板电机驱动电路VCC/5V
五、代码
求教,如果把去掉会提示GPIO口已经在使用。貌似只有GPIO23口是这样。- #!/usr/bin/env python
- import RPi.GPIO as GPIO
- import time
- # 设置GIPO口的标号方式
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- #使用GPIO23,GPIO24,GPIO25,GPIO22
- StepPins = [23,24,25,22]
- #将选择的GPIO口设置为输出
- for pin in StepPins:
- print('SET PINs OUT')
- GPIO.setup(pin, GPIO.OUT)
- GPIO.output(pin, False)
- #设置反转表
- Seq_reversal = []
- Seq_reversal = range(0,4)
- Seq_reversal[0] = [1,0,0,0]
- Seq_reversal[1] = [0,1,0,0]
- Seq_reversal[2] = [0,0,1,0]
- Seq_reversal[3] = [0,0,0,1]
- #设置正转表
- Seq_forward = []
- Seq_forward = range(0,4)
- Seq_forward[0] = [0,0,0,1]
- Seq_forward[1] = [0,0,1,0]
- Seq_forward[2] = [0,1,0,0]
- Seq_forward[3] = [1,0,0,0]
- #反转
- def Reversal():
- global stepcounter
- for pin in range(0,4):
- xpin = StepPins[pin]
- if Seq_reversal[stepcounter][pin] == 1:
- GPIO.output(xpin, True)
- else:
- GPIO.output(xpin, False)
- stepcounter += 1
- if stepcounter >=4 :
- stepcounter = 0
- if stepcounter < 0:
- stepcounter = 4
- time.sleep(0.005)
- #正转
- def Forward():
- global stepcounter
- for pin in range(0,4):
- xpin = StepPins[pin]
- if Seq_forward[stepcounter][pin] == 1:
- GPIO.output(xpin, True)
- else:
- GPIO.output(xpin, False)
- stepcounter += 1
- if stepcounter >=4 :
- stepcounter = 0
- time.sleep(0.005)
- if __name__ == '__main__':
- print('Begin')
- stepcounter = 0
- num = 0
- #反转一圈后,正转一圈,一直循环
- while True:
- if num < 2048:
- print('Reserval')
- print(num)
- Reversal()
- num += 1
- elif num >= 2048 and num < 4096:
- print('Forward')
- print(num)
- Forward()
- num += 1
- elif num >= 4096:
- num = 0
复制代码 步进电机减速比为32. 旋转一圈需要360/(5.625/32)=2048.
|
|