其它部分的工具(python-setuptools,python-dev)已经在初始化时安装。现在安装GPIO
sudo easy_install RPi.GPIO
需要在root权限下调用。如果不想使用管理权限,也可以用第三方的WiringPi,使用easy_install直接安装。
接上步进电机和驱动板。
板子是V2.0的,内存512M的,使用GPIO图如下:
#!/bin/env python # -*- coding:utf-8 -*- # Revision: # Date: 2012-12-11 # Author: simonzhang # Email: simon-zzm@163.com # Web: www.simonzhang.net # ------------------------------- import time import RPi.GPIO as GPIO #要使用的四个针脚 Pins = [17,18,22,23] #初始化。使用BCM2835标准。具体对应要查说明 #http://elinux.org/RPi_BCM2835_GPIOs GPIO.setmode(GPIO.BCM) for pin in Pins: try: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, False) except: pass # 顺时针旋转矩阵 Seq1 = [] Seq1 = range(0, 4) Seq1[0] = [1,0,0,0] Seq1[1] = [0,1,0,0] Seq1[2] = [0,0,1,0] Seq1[3] = [0,0,0,1] # 逆时针旋转矩阵 Seq2 = [] Seq2 = range(0, 4) Seq2[0] = [0,0,0,1] Seq2[1] = [0,0,1,0] Seq2[2] = [0,1,0,0] Seq2[3] = [1,0,0,0] #初始化步数 setup_count = 0 while 1: for set in range(0, 4): # 小于100步按照逆时针转,100步到200步之间按顺时针转 print "setup %s" % setup_count if 100 < setup_count < 200: l = Seq1[set] else: l = Seq2[set] for ls in range(len(Pins)): if l[ls] == 1: GPIO.output(Pins[ls], True) else: GPIO.output(Pins[ls], False) # 大于200步初始化为0,否则加1 if setup_count < 200: setup_count += 1 else: setup_count = 0 # 需要做一定延迟,最小延迟应该是0.01s time.sleep(0.1)