Advertisement
AntonioVillanueva

Nuevo servidor Web con Threads para robot Raspberry control velocidad TESTs

Jul 20th, 2021 (edited)
1,696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #Control web para el robot Raspberry
  3.  
  4. import RPi.GPIO as GPIO
  5. import os
  6. from http.server import BaseHTTPRequestHandler, HTTPServer
  7. import time
  8. import threading
  9.  
  10. HOST = '192.168.1.5'#Direccion de la Raspberry Pi
  11. PORT = 8000
  12.  
  13. def setupGPIO():
  14.     GPIO.setmode(GPIO.BCM)
  15.     GPIO.setwarnings(False)
  16.  
  17.     # Inicializa la GPIO conectada al L293D
  18.     #Izquierda
  19.     GPIO.setup(4,GPIO.OUT) #Enable 1    PIN1
  20.     GPIO.setup(17,GPIO.OUT) #Input 1    PIN2
  21.     GPIO.setup(27,GPIO.OUT) #input 2    PIN7
  22.  
  23.     #Derecha
  24.     GPIO.setup(18,GPIO.OUT) #Input 4    PIN15  
  25.     GPIO.setup(22,GPIO.OUT) #imput 3    PIN10
  26.     GPIO.setup(23,GPIO.OUT) #Enable 2   PIN9
  27.  
  28. #Reduce velocidad 50%
  29. def velocidad(habilita):
  30.     #Solo actua con la habilitacion del L293D
  31.    
  32.     GPIO.output(4, habilita) #Habilita 1   
  33.     GPIO.output(23, habilita) #Habilita 2  
  34.    
  35.     time.sleep(0.05)
  36.    
  37.     #Lanza de nuevo este thread para controlar los motores
  38.     hilo = threading.Thread(target=velocidad, args=(not habilita,))
  39.     hilo.start()
  40.     #hilo.join()   
  41.  
  42. #Control de los dos motores del robot
  43. def Motores(HABILITA_A=False,MOTOR_A1=False,MOTOR_A2=False,HABILITA_B=False,MOTOR_B1=False,MOTOR_B2=False):
  44.  
  45.     #Test Movimiento motores
  46.     GPIO.output(4, HABILITA_A) #Habilita 1
  47.     GPIO.output(17, MOTOR_A1) #input 1 A1
  48.     GPIO.output(27, MOTOR_A2) #input 2 A2
  49.  
  50.     #Test Movimiento motores
  51.     GPIO.output(23, HABILITA_B) #Habilita 2
  52.     GPIO.output(18, MOTOR_B1) #Input 4 B1
  53.     GPIO.output(22, MOTOR_B2) #Input 3 B2
  54.  
  55. class ServidorWeb(BaseHTTPRequestHandler):
  56.    
  57.     def do_HEAD(self):
  58.         self.send_response(200)
  59.         self.send_header('Content-type', 'text/html')
  60.         self.end_headers()
  61.  
  62.     def _redirect(self, path):
  63.         self.send_response(303)
  64.         self.send_header('Content-type', 'text/html')
  65.         self.send_header('Location', path)
  66.         self.end_headers()
  67.        
  68.     def do_GET(self):
  69.         html = '''
  70.            <html>
  71.             <body style="width:960px; margin: 20px auto;">
  72.            <h1>ICARO</h1>
  73.            <p>  Control Robot </p>
  74.            <form action="/" method="POST">
  75.                CONTROL :
  76.                <input type="submit" name="submit" value="Avanza">
  77.                <input type="submit" name="submit" value="Retrocede">
  78.                <input type="submit" name="submit" value="Derecha">
  79.                <input type="submit" name="submit" value="Izquierda">  
  80.                <input type="submit" name="submit" value="Stop">                                            
  81.                
  82.            </form>
  83.            </body>
  84.            </html>
  85.         '''
  86.         self.do_HEAD()     
  87.         self.wfile.write(html.encode("utf-8"))                     
  88.  
  89.     def do_POST(self):
  90.  
  91.         content_length = int(self.headers['Content-Length'])
  92.         post_data = self.rfile.read(content_length).decode("utf-8")
  93.         post_data = post_data.split("=")[1]
  94.  
  95.         #VEL=not VEL #Un pwm al 50% para reducir la velocidad
  96.  
  97.         if post_data == 'Avanza':
  98.             Motores(True,False,True,True,False,True)
  99.            
  100.         if post_data == 'Retrocede':
  101.             Motores(True,True,False,True,True,False)           
  102.            
  103.         if post_data == 'Derecha':
  104.             Motores(True,False,True,False,False,False)     
  105.                                    
  106.         if post_data == 'Izquierda':
  107.             Motores(False,False,False,True,False,True) 
  108.            
  109.         if post_data == 'Stop':
  110.             Motores(False,False,False,False,False,False)                   
  111.  
  112.         self._redirect('/')  # Redirect back to the root url
  113.  
  114.  
  115. # # # # # Main # # # # #
  116.  
  117. if __name__ == '__main__':
  118.     http_server = HTTPServer((HOST, PORT), ServidorWeb)
  119.     print("Server Starts - %s:%s" % (HOST, PORT))
  120.    
  121.     setupGPIO() #Configura GPIO raspberry
  122.    
  123.     #Lanza el Thread de control de velocidad de motores
  124.     hilo = threading.Thread(target=velocidad, args=(False,))
  125.     hilo.start()   
  126.  
  127.     try:
  128.         http_server.serve_forever()
  129.     except KeyboardInterrupt:
  130.         http_server.server_close()
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement