Вы находитесь на странице: 1из 4

#!

/usr/bin/python
# Importe de las librerias requeridas.
import sys
import time
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

# en lugar de pines fisicos


# Utilizar referencias BCM GPIO
#GPIO.setmode(GPIO.BCM)

# Definicion de GPIO usadas


# Pines fisicos 11,15,16,18
# GPIO17,GPIO22,GPIO23,GPIO24
StepPins = [17,22,23,24]

# Set all pins as output


for pin in StepPins:
print "Setup pins"
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)

#Definicion de la secuencia avanzada


# Tal y como se muestra en el datasheet.
Seq = [[1,0,0,1],
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1]]

StepCount = len(Seq)
StepDir = 1 # Setear el ciclo de reloj a 1 o 2

# Lectura de la linea de comandos


if len(sys.argv)>1:
WaitTime = int(sys.argv[1])/float(1000)
else:
WaitTime = 10/float(1000)

def puente_arriba():
# Inicializacion de variables
StepCounter = 0
contador=0;

# Loop
while int(sys.argv[2])>=contador:

print StepCounter,
print Seq[StepCounter]

for pin in range(0,4):


xpin=StepPins[pin]# Get GPIO
if Seq[StepCounter][pin]!=0:
print " Enable GPIO %i" %(xpin)
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)

StepCounter += StepDir

# Si se llega al final de la secuencia


# comenzar de nuevo.
if (StepCounter>=StepCount):
StepCounter = 0
if (StepCounter<0):
StepCounter = StepCount+StepDir

# Esperar antes de moverse de nuevo.


time.sleep(WaitTime)
contador=contador+1

def puente_abajo():
# stepper de regreso
StepDir = -1 # Set to -1 or -2 for anti-clockwise

# inicializar variables.
StepCounter = 0
contador=0;

# Start main loop


while int(sys.argv[2])>=contador:

print StepCounter,
print Seq[StepCounter]

for pin in range(0,4):


xpin=StepPins[pin]# Get GPIO
if Seq[StepCounter][pin]!=0:
print " Enable GPIO %i" %(xpin)
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)

StepCounter += StepDir

# Si se llega al final de la secuencia


# comenzar de nuevo.
if (StepCounter>=StepCount):
StepCounter = 0
if (StepCounter<0):
StepCounter = StepCount+StepDir

# Esperar antes del movimiento.


time.sleep(WaitTime)
contador=contador+1

print "Proyecto Arquitectura"


GPIO_TRIGGER = 26 #Usamos el pin GPIO 25 como TRIGGER
GPIO_ECHO = 16 #Usamos el pin GPIO 7 como ECHO
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) #Configuramos Trigger como salida
GPIO.setup(GPIO_ECHO,GPIO.IN) #Configuramos Echo como entrada
GPIO.output(GPIO_TRIGGER,False) #Ponemos el pin 25 como LOW
validador=0
rojo=10 #GPIO para led rojo
amarillo=9 #GPIO para led amarillo
verde=11 #GPIO para led verde

#Se enciende led verde cuando inica programa


GPIO.setup(verde,GPIO.OUT) #Se coloca GPIO como salida
GPIO.output(verde,0) #Se manda senial 0 a led
GPIO.output(verde,1) #Se manda senial 1 a led para encender

#GPIO para talanquera


GPIO.setup(6,GPIO.OUT) #Ponemos el pin 21 como salida
p = GPIO.PWM(6,50) #Ponemos el pin 21 en modo PWM y enviamos 50 pulsos por
segundo
p.start(25.0) #Enviamos un pulso del 7.5% para centrar el servo

#Se coloca talaquera abierta


p.ChangeDutyCycle(2.5) #Enviamos un pulso del 2.5% para girar el servo hacia la
izquierda

try:
while True: #Iniciamos un loop infinito
GPIO.output(GPIO_TRIGGER,True) #Enviamos un pulso de ultrasonidos
time.sleep(0.00001) #Una pequenia pausa
GPIO.output(GPIO_TRIGGER,False) #Apagamos el pulso
start = time.time() #Guarda el tiempo actual mediante
time.time()
while GPIO.input(GPIO_ECHO)==0: #Mientras el sensor no reciba senial...
start = time.time() #Mantenemos el tiempo actual mediante
time.time()
while GPIO.input(GPIO_ECHO)==1: #Si el sensor recibe senial...
stop = time.time() #Guarda el tiempo actual mediante
time.time() en otra variable
elapsed = stop-start #Obtenemos el tiempo transcurrido entre
envio y recepcion
distance = (elapsed * 34300)/2 #Distancia es igual a tiempo por velocidad
partido por 2 D = (T x V)/2
if distance>40 and validador>0:
validador=0
if distance<=50:
validador+=1
print validador
if distance<=40 and validador==3:
print "Se activa puente"
print "Distancia: "+str(distance)+" stop: "+str(stop)+" start:
"+str(start)+" elapsed: "+str(elapsed) #Devolvemos la distancia
(en centimetros) por pantalla

GPIO.output(verde,0)

GPIO.setup(amarillo,GPIO.OUT)
GPIO.output(amarillo,0)
GPIO.output(amarillo,1)
time.sleep(0.5)
GPIO.output(amarillo,0)
time.sleep(0.5)
GPIO.output(amarillo,1)
time.sleep(0.5)
GPIO.output(amarillo,0)
time.sleep(0.5)
GPIO.output(amarillo,1)
time.sleep(0.5)
GPIO.output(amarillo,0)
time.sleep(0.5)
GPIO.output(amarillo,1)
time.sleep(0.5)
GPIO.output(amarillo,0)

GPIO.setup(rojo,GPIO.OUT)
GPIO.output(rojo,0)
GPIO.output(rojo,1)
#Se cierra la talanquera
p.ChangeDutyCycle(7.0) #Enviamos un pulso del 7.0% para girar el
servo hacia la izquierda
puente_arriba() #Se sube el puente
time.sleep(10) #Se hace una espera para que pase el puente
puente_abajo() #Se baja el puente
GPIO.output(rojo,0)

GPIO.output(verde,1)
#Se abre talanquera
p.ChangeDutyCycle(2.5) #Enviamos un pulso del 2.5% para centrar
el servo de nuevo
time.sleep(3)

validador=0
time.sleep(1) #Pequena pausa para no saturar el
procesador de la Raspberry
except KeyboardInterrupt: #Si el usuario pulsa CONTROL+C...
print "quit" #Avisamos del cierre al usuario
GPIO.cleanup() #Limpiamos los pines GPIO y salimos

Вам также может понравиться