Advertisement
AntonioVillanueva

Simulacion print para el robot rasbperry con threads

Jul 20th, 2021
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1.  
  2. #pip3 install httpserver
  3. import os
  4. import time
  5. import threading
  6. from http.server import BaseHTTPRequestHandler, HTTPServer
  7.  
  8. HOST = '192.168.6.150'#Direccion de la Raspberry Pi
  9. PORT = 8000
  10.  
  11. def velocidad(habilita):
  12.     print ("HABILITA_A",habilita)
  13.    
  14.     time.sleep(1)
  15.     hilo = threading.Thread(target=velocidad, args=(not habilita,))
  16.     hilo.start()   
  17.    
  18.    
  19.  
  20. def setupGPIO():
  21.     print ("Setup Gpio")
  22.    
  23. #Control de los dos motores del robot
  24. def Motores(HABILITA_A=False,MOTOR_A1=False,MOTOR_A2=False,HABILITA_B=False,MOTOR_B1=False,MOTOR_B2=False):
  25.  
  26.   #Simula motores
  27.   print (HABILITA_A,MOTOR_A1,MOTOR_A2,HABILITA_B,MOTOR_B1,MOTOR_B2)
  28.  
  29. class ServidorWeb(BaseHTTPRequestHandler):
  30.     VEL=True
  31.    
  32.     def do_HEAD(self):
  33.         self.send_response(200)
  34.         self.send_header('Content-type', 'text/html')
  35.         self.end_headers()
  36.  
  37.     def _redirect(self, path):
  38.         self.send_response(303)
  39.         self.send_header('Content-type', 'text/html')
  40.         self.send_header('Location', path)
  41.         self.end_headers()
  42.        
  43.     def do_GET(self):
  44.         html = '''
  45.           <html>
  46.            <body style="width:960px; margin: 20px auto;">
  47.           <h1>ICARO</h1>
  48.           <p>  Control Robot </p>
  49.           <form action="/" method="POST">
  50.               CONTROL :
  51.               <input type="submit" name="submit" value="Avanza">
  52.               <input type="submit" name="submit" value="Retrocede">
  53.               <input type="submit" name="submit" value="Derecha">
  54.               <input type="submit" name="submit" value="Izquierda">  
  55.               <input type="submit" name="submit" value="Stop">                                            
  56.              
  57.           </form>
  58.           </body>
  59.           </html>
  60.        '''
  61.         self.do_HEAD()      
  62.         self.wfile.write(html.encode("utf-8"))          
  63.            
  64.  
  65.  
  66.     def do_POST(self):
  67.  
  68.         content_length = int(self.headers['Content-Length'])
  69.         post_data = self.rfile.read(content_length).decode("utf-8")
  70.         post_data = post_data.split("=")[1]
  71.  
  72.         setupGPIO()
  73.  
  74.         #VEL=not VEL #Un pwm al 50% para reducir la velocidad
  75.  
  76.         if post_data == 'Avanza':
  77.             Motores(self.VEL,False,True,self.VEL,False,True)
  78.            
  79.         if post_data == 'Retrocede':
  80.             Motores(self.VEL,True,False,self.VEL,True,False)            
  81.            
  82.         if post_data == 'Derecha':
  83.             Motores(self.VEL,False,True,False,False,False)      
  84.                                    
  85.         if post_data == 'Izquierda':
  86.             Motores(False,False,False,self.VEL,False,True)  
  87.            
  88.         if post_data == 'Stop':
  89.             Motores(False,False,False,False,False,False)                    
  90.  
  91.         self._redirect('/')  # Redirect back to the root url
  92.  
  93.  
  94. # # # # # Main # # # # #
  95.  
  96. if __name__ == '__main__':
  97.     http_server = HTTPServer((HOST, PORT), ServidorWeb)
  98.     print("Server Starts - %s:%s" % (HOST, PORT))
  99.  
  100.  
  101.     hilo = threading.Thread(target=velocidad, args=(False,))
  102.     hilo.start()
  103.  
  104.     try:
  105.         http_server.serve_forever()
  106.     except KeyboardInterrupt:
  107.         http_server.server_close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement