Advertisement
Guest User

Untitled

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