AntonioVillanueva

Test Ctrl. Robot Raspberry Pi con L293D

Jul 14th, 2021 (edited)
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. ##!/usr/bin/python3
  2. #Antonio Villanueva Segura  
  3. # Test Movimiento robot basado en RaspBerry pi y  L293D
  4. #sudo arp -a | grep raspberry
  5. #sudo nmap -sP 192.168.1.0/24 | grep raspberry
  6.  
  7. import sys
  8.  
  9. import RPi.GPIO as GPIO
  10. from time import sleep
  11. import curses
  12.  
  13. VELOCIDAD=75
  14.  
  15. #GPIO.setmode(GPIO.BOARD) #Pins
  16. GPIO.setmode(GPIO.BCM) #Utilizo la notacion BCM
  17.  
  18. GPIO.setwarnings(False)
  19.  
  20. #L293D
  21. #Izquierda
  22. GPIO.setup(4,GPIO.OUT) #Enable 1    PIN1
  23. GPIO.setup(17,GPIO.OUT) #Input 1    PIN2
  24. GPIO.setup(27,GPIO.OUT) #input 2    PIN7
  25.  
  26. #Derecha
  27. GPIO.setup(18,GPIO.OUT) #Input 4    PIN15  
  28. GPIO.setup(22,GPIO.OUT) #imput 3    PIN10
  29. GPIO.setup(23,GPIO.OUT) #Enable 2   PIN9
  30.  
  31. #Control de los dos motores del robot
  32. def Motores(HABILITA_A=False,MOTOR_A1=False,MOTOR_A2=False,HABILITA_B=False,MOTOR_B1=False,MOTOR_B2=False):
  33.  
  34.     #Test Movimiento motores
  35.     GPIO.output(4, HABILITA_A) #Habilita 1
  36.     GPIO.output(17, MOTOR_A1) #input 1 A1
  37.     GPIO.output(27, MOTOR_A2) #input 2 A2
  38.  
  39.     #Test Movimiento motores
  40.     GPIO.output(23, HABILITA_B) #Habilita 2
  41.     GPIO.output(18, MOTOR_B1) #Input 4 B1
  42.     GPIO.output(22, MOTOR_B2) #Input 3 B2
  43.  
  44.  
  45. Motores(False,False,False,False,False,False) #Arranca con los motores parados
  46.  
  47. #Activacion de motores segun la tecla pulsada
  48. def activa(tecla,VEL):
  49.  
  50.    
  51.     if tecla=="8": #Avance Robot
  52.         Motores(VEL,False,True,VEL,False,True)
  53.         return
  54.        
  55.     if tecla=="2": #Retroceso Robot
  56.         Motores(VEL,True,False,VEL,True,False)     
  57.         return
  58.  
  59.     if tecla=="6": #Avance solo motor 1 Derecha
  60.         Motores(VEL,False,True,False,False,False)
  61.         return
  62.        
  63.     if tecla=="9": #Retroceso motor1  Robot
  64.         Motores(VEL,True,False,False,False,False)
  65.         return
  66.                
  67.     if tecla=="4": #Avance solo motor 2 Izquierda
  68.         Motores(False,False,False,VEL,False,True)
  69.         return
  70.        
  71.     if tecla=="7": #Retroceso motor 2  Robot
  72.         Motores(False,False,False,VEL,True,False)
  73.         return
  74.  
  75.     if tecla=="0": #STOP  Robot
  76.         Motores(False,False,False,False,False,False)
  77.         return
  78.        
  79.     if tecla=="q": #STOP  Robot salir del programa forma limpia curses
  80.         Motores(False,False,False,False,False,False)
  81.         curses.endwin()
  82.         sys.exit(0)    
  83.         return     
  84.        
  85.    
  86.     #Si no se pulsa ninguna tecla se para el robot 
  87.     Motores(False,False,False,False,False,False)       
  88.    
  89.    
  90. #Bucle principal
  91. velocidad=False
  92.    
  93. if __name__ == '__main__':
  94.    
  95.    
  96.     win = curses.initscr() #Inicio de curses
  97.     win.addstr(0, 0, "Lee teclas  ") #Mensaje en pantalla
  98.    
  99.     while True:
  100.         win.timeout(100) #No se bloquea la lectura de tecla en 100
  101.         ch=win.getch() #Lee una tecla
  102.        
  103.         if ch<0: #Si no se pulsa tecla rectifica a un valor 0...
  104.             ch='0'
  105.         else:
  106.             ch=chr(ch)
  107.            
  108.         velocidad=not velocidad #Un pwm al 50% para reducir la velocidad
  109.         activa(ch,velocidad)#Activa los motores segun la tecla pulsada
  110.  
  111.     curses.endwin()
  112.     sys.exit(0)
  113.  
  114.  
  115.  
Add Comment
Please, Sign In to add comment