本文主要是介绍利用树莓派控制步进电机——Python语言,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
步进电机的优点在于它能够被精确定位,正向或反向一次性转动“一步”,并且也能够连续转动。本次实验,我们选用的舵机驱动芯片是ULN2003A,ULN2003 是高耐压、大电流复合晶体管阵列,由七个硅NPN 复合晶体管组成。为了方便起见,我们用了ULN2003A模块,电路连接方式如下图:
#!/usr/bin/env python
#########################################################
# File name: stepMotor.py
# Author: Jason Dai
# Date: 2015/01/26
#########################################################
import RPi.GPIO as GPIO
import timeIN1 = 11 # pin11
IN2 = 12
IN3 = 13
IN4 = 15def setStep(w1, w2, w3, w4):GPIO.output(IN1, w1)GPIO.output(IN2, w2)GPIO.output(IN3, w3)GPIO.output(IN4, w4)def stop():setStep(0, 0, 0, 0)def forward(delay, steps): for i in range(0, steps):setStep(1, 0, 0, 0)time.sleep(delay)setStep(0, 1, 0, 0)time.sleep(delay)setStep(0, 0, 1, 0)time.sleep(delay)setStep(0, 0, 0, 1)time.sleep(delay)def backward(delay, steps): for i in range(0, steps):setStep(0, 0, 0, 1)time.sleep(delay)setStep(0, 0, 1, 0)time.sleep(delay)setStep(0, 1, 0, 0)time.sleep(delay)setStep(1, 0, 0, 0)time.sleep(delay)def setup():GPIO.setwarnings(False)GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical locationGPIO.setup(IN1, GPIO.OUT) # Set pin's mode is outputGPIO.setup(IN2, GPIO.OUT)GPIO.setup(IN3, GPIO.OUT)GPIO.setup(IN4, GPIO.OUT)def loop():while True:print "backward..."backward(0.003, 512) # 512 steps --- 360 angleprint "stop..."stop() # stoptime.sleep(3) # sleep 3sprint "forward..."forward(0.005, 512)print "stop..."stop()time.sleep(3)def destroy():GPIO.cleanup() # Release resourceif __name__ == '__main__': # Program start from heresetup()try:loop()except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child function destroy() will be executed.destroy()
运行程序:
python stepMotor.py
这篇关于利用树莓派控制步进电机——Python语言的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!