Translate

martedì 13 maggio 2014

RaspBerry Pi - ed il motorino passo passo
 
Ecco la Seconda Puntata del mio approccio con il Raspberry pi... nulla di eclatante.. un po' di prove di "velocita massima", di passi, mezzi passi nulla di che
un po' di ottimizzazione del codice di esempio (un bel try-catch fa' sempre bene) e come al solito i commenti sono piu del codice :P


allego il codice usato:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

enable_pin = 18
coil_A_1_pin = 4
coil_A_2_pin = 17
coil_B_1_pin = 23
coil_B_2_pin = 24

GPIO.setup(enable_pin, GPIO.OUT)
GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)

GPIO.output(enable_pin, 1)

def forward(delay, steps):  
#mezzo passo 
  for i in range(0, steps):
    setStep(1, 0, 0, 1)
    time.sleep(delay)
    setStep(0, 0, 0, 1)
    time.sleep(delay)
    setStep(0, 0, 1, 1)
    time.sleep(delay)
    setStep(0, 0, 1, 0)
    time.sleep(delay)
    setStep(0, 1, 1, 0)
    time.sleep(delay)
    setStep(0, 1, 0 , 0)
    time.sleep(delay)
    setStep(1, 1, 0, 0)
    time.sleep(delay)
    setStep(1, 0, 0, 0)
    time.sleep(delay)

def backwards(delay, steps): 
#passo intero 
  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 setStep(w1, w2, w3, w4):
  GPIO.output(coil_A_1_pin, w1)
  GPIO.output(coil_A_2_pin, w2)
  GPIO.output(coil_B_1_pin, w3)
  GPIO.output(coil_B_2_pin, w4)

try:
  #here you put your main loop or block of code
  delay = raw_input("Delay between steps (milliseconds)?")

  while int(delay) > 0 :
    steps = raw_input("How many steps forward? ")
    forward(int(delay) / 1000.0, int(steps))
    steps = raw_input("How many steps backwards? ")
    backwards(int(delay) / 1000.0, int(steps))
    delay = raw_input("Delay between steps (milliseconds)?")

except KeyboardInterrupt:
  # here you put any code you want to run before the program
  # exits when you press CTRL+C
  #print "\n", counter # print value of counter
  print "\nuscito per pressione tasti CTRL+C"

except:
  # this catches ALL other exceptions including errors.
  # You won't get any error messages for debugging
  # so only use it once your code is working
  print "Other error or exception occurred!"

finally:
  GPIO.cleanup() # this ensures a clean exit