Advertisement
LovelessIsma

puenteh_python

Nov 5th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. #! /usr/bin/env python
  2. import RPi.GPIO as GPIO
  3. import os
  4. import time
  5. #Define nombre de las entradas del puente H
  6. ena = 18           
  7. in1 = 23
  8. in2 = 24
  9.  
  10. enb = 19
  11. in3 = 6
  12. in4 = 5
  13. #configura los pines segun el microprocesador Broadcom
  14. GPIO.setmode(GPIO.BCM)
  15. #configura los pines como salidas
  16. GPIO.setup(ena,GPIO.OUT)
  17. GPIO.setup(enb,GPIO.OUT)
  18. GPIO.setup(in1, GPIO.OUT)
  19. GPIO.setup(in2, GPIO.OUT)
  20. GPIO.setup(in3, GPIO.OUT)
  21. GPIO.setup(in4, GPIO.OUT)
  22. #Define las salidas PWM q
  23. pwm_a = GPIO.PWM(ena,500)
  24. pwm_b = GPIO.PWM(enb,500)
  25. #inicializan los PWM con un duty Cicly de cero
  26. pwm_a.start(0)
  27. pwm_b.start(0)
  28. # funciones de sentido de giro de los motores
  29. def  Giro_Favor_Reloj_MotorA():
  30.     GPIO.output(in1,False)
  31.     GPIO.output(in2,True)
  32.  
  33. def Giro_Contra_Reloj_MotorA():
  34.     GPIO.output(in1,True)
  35.     GPIO.output(in2,False)
  36.  
  37. def  Giro_Favor_Reloj_MotorB():
  38.     GPIO.output(in3,False)
  39.     GPIO.output(in4,True)
  40.  
  41. def Giro_Contra_Reloj_MotorB():
  42.     GPIO.output(in3,True)
  43.     GPIO.output(in4,False)
  44. #limpia la pantalla
  45. os.system('clear')
  46. print("Elija motor[A-B], el sentido [F-R] y la velocidad [0-100]")
  47. print("ejemplo 'AF50' MOTOR A Foward a 50%. de velocidad")
  48. print("CTRL-C para salir")
  49. print
  50. try:
  51.     while True:
  52.         cmd = raw_input("inserte el comando ")
  53.         cmd = cmd.lower()
  54.         motor = cmd[0]
  55.         direccion =cmd[1]
  56.         velocidad =cmd[2:5]
  57.  
  58.         if motor == "a":
  59.             if direccion == "f":
  60.                 Giro_Favor_Reloj_MotorA()
  61.                 print("motor A, CW, vel="+velocidad)
  62.             elif direccion== "r":
  63.                 Giro_Contra_Reloj_MotorA()
  64.                 print("motor A, CCW, vel="+velocidad)
  65.             else:
  66.                 print("comando no reconocido")
  67.             pwm_a.ChangeDutyCycle(int(velocidad))
  68.             print
  69.  
  70.         elif motor == "b":
  71.             if direccion == "f":
  72.                 Giro_Favor_Reloj_MotorB()
  73.                 print("motor B, CW, vel="+velocidad)
  74.             elif direccion == "r":
  75.                 Giro_Contra_Reloj_MotorB()
  76.             else:
  77.                 print("comando no reconocido")
  78.             pwm_b.ChangeDutyCycle(int(velocidad))
  79.             print
  80.         else:
  81.             print
  82.             print("comando no reconocido")
  83.             print
  84. except KeyboardInterrupt:
  85.     pwm_a.stop()
  86.     pwm_b.stop()
  87.     GPIO.cleanup()
  88.     os.system('clear')
  89.     print
  90.     print("Programa Terminado por el usuario")
  91.     print
  92.     exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement