Created
September 30, 2017 09:49
-
-
Save hlecuanda/e83174998bc193a25d525d717087a232 to your computer and use it in GitHub Desktop.
Sugerencias para la respuesta en https://es.stackoverflow.com/questions/106206/ejecutar-archivo-python-desde-un-archivo-php/106221#106221
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# −*− coding: UTF−8 −*− | |
# Las dos lineas anteriores se aseguran #1 de localizar el | |
# interpretador para poder usar el script directamente | |
# y #2 de definir la codificacion de la fuente. Solo es | |
# buena educacion, =) | |
#import sys # con argparse y la linea shebang! (#!) ya no lo ocupas | |
import argparse # modulo utilisimo para lineas de comando | |
import RPi.GPIO as GPIO | |
def principal(): | |
''' Secuencia principal a ejecutar cuando se llama desde | |
la linea de comando en shell ''' | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(11, GPIO.OUT) | |
GPIO.setup(12, GPIO.OUT) | |
pwm = GPIO.PWM(12, 100) # (1): pwm asignado pero nunca usado | |
pwm2 = GPIO.PWM(11, 100) | |
pwm.start(0) # (1): Tal vez hacia falta esto? | |
pwm2.start(0) | |
# vel = float(sys.argv[1]) # (6): Esto ya no es necesario, | |
# p1 = int(sys.argv[2]) # puedes hacer referencia al objeto | |
# p2 = int(sys.argv[3]) # args (linea 59) donde las propiedades | |
# p3 = int(sys.argv[4]) # corresponden a tus variables. argparse | |
# ya se encargo de que sean del tipo correcto | |
if args.verbose: | |
print(args.vel) | |
print(args.p1) | |
print(args.p2) | |
print(args.p3) | |
while args.p3 != 2: | |
if args.p1 == 1: # (2): Faltaba indentar tu bloque if para | |
pwm.ChangeDutyCycle(args.vel) # incluirlo en el bucle while | |
pwm2.ChangeDutyCycle(0) | |
elif args.p2 == 1: # (3): Revisa tu logica. la condicion | |
pwm.ChangeDutyCycle(0) # elif esta en una variable distinta al | |
pwm2.ChangeDutyCycle(args.vel) # if original! seguro que asi debe ser? | |
# (4): Este bucle if es infinito, el valor de p3 nunca cambiara! | |
pwm.stop() | |
GPIO.cleanup() | |
print("fin") | |
if __name__ == '__main__': # (5) Recurso idiomatico en python: de esta manera | |
# te aseguras que el programa se pueda ejecutar desde | |
# el shell, o que igual se pueda incluir como modulo | |
# en otro porgrama mas grande, mandando llamar al | |
# metodo principal() | |
parser = argparse.ArgumentParser(description='Mi programa para controlar motores \ | |
con la Raspberry PI') | |
parser.add_argument('vel', type=float, help='velocidad (punto flotante)') | |
parser.add_argument('p1', type=int, help='velocidad (punto flotante)') | |
parser.add_argument('p2', type=int, help='velocidad (punto flotante)') | |
parser.add_argument('p3', type=int, help='velocidad (punto flotante)') | |
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', | |
help='genera salida explicita (opcional)') | |
args = parser.parse_args() | |
principal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment