Advertisement
AntonioVillanueva

Control Robot Raspberry pi version web

Jul 18th, 2021
1,958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 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.  
  8. HOST = '192.168.1.5'#Direccion de la Raspberry Pi
  9. PORT = 8000
  10.  
  11.  
  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. #Control de los dos motores del robot
  29. def Motores(HABILITA_A=False,MOTOR_A1=False,MOTOR_A2=False,HABILITA_B=False,MOTOR_B1=False,MOTOR_B2=False):
  30.  
  31.     #Test Movimiento motores
  32.     GPIO.output(4, HABILITA_A) #Habilita 1
  33.     GPIO.output(17, MOTOR_A1) #input 1 A1
  34.     GPIO.output(27, MOTOR_A2) #input 2 A2
  35.  
  36.     #Test Movimiento motores
  37.     GPIO.output(23, HABILITA_B) #Habilita 2
  38.     GPIO.output(18, MOTOR_B1) #Input 4 B1
  39.     GPIO.output(22, MOTOR_B2) #Input 3 B2
  40.  
  41. class ServidorWeb(BaseHTTPRequestHandler):
  42.     VEL=True
  43.    
  44.     def do_HEAD(self):
  45.         self.send_response(200)
  46.         self.send_header('Content-type', 'text/html')
  47.         self.end_headers()
  48.  
  49.     def _redirect(self, path):
  50.         self.send_response(303)
  51.         self.send_header('Content-type', 'text/html')
  52.         self.send_header('Location', path)
  53.         self.end_headers()
  54.        
  55.     def do_GET(self):
  56.         html = '''
  57.            <html>
  58.             <body style="width:960px; margin: 20px auto;">
  59.            <h1>ICARO</h1>
  60.            <p>  Control Robot </p>
  61.            <form action="/" method="POST">
  62.                CONTROL :
  63.                <input type="submit" name="submit" value="Avanza">
  64.                <input type="submit" name="submit" value="Retrocede">
  65.                <input type="submit" name="submit" value="Derecha">
  66.                <input type="submit" name="submit" value="Izquierda">  
  67.                <input type="submit" name="submit" value="Stop">                                            
  68.                
  69.            </form>
  70.            </body>
  71.            </html>
  72.         '''
  73.         self.do_HEAD()     
  74.         self.wfile.write(html.encode("utf-8"))         
  75.            
  76.  
  77.  
  78.     def do_POST(self):
  79.  
  80.         content_length = int(self.headers['Content-Length'])
  81.         post_data = self.rfile.read(content_length).decode("utf-8")
  82.         post_data = post_data.split("=")[1]
  83.  
  84.         setupGPIO()
  85.  
  86.         #VEL=not VEL #Un pwm al 50% para reducir la velocidad
  87.  
  88.         if post_data == 'Avanza':
  89.             Motores(self.VEL,False,True,self.VEL,False,True)
  90.            
  91.         if post_data == 'Retrocede':
  92.             Motores(self.VEL,True,False,self.VEL,True,False)           
  93.            
  94.         if post_data == 'Derecha':
  95.             Motores(self.VEL,False,True,False,False,False)     
  96.                                    
  97.         if post_data == 'Izquierda':
  98.             Motores(False,False,False,self.VEL,False,True) 
  99.            
  100.         if post_data == 'Stop':
  101.             Motores(False,False,False,False,False,False)                   
  102.  
  103.         self._redirect('/')  # Redirect back to the root url
  104.  
  105.  
  106. # # # # # Main # # # # #
  107.  
  108. if __name__ == '__main__':
  109.     http_server = HTTPServer((HOST, PORT), ServidorWeb)
  110.     print("Server Starts - %s:%s" % (HOST, PORT))
  111.  
  112.     try:
  113.         http_server.serve_forever()
  114.     except KeyboardInterrupt:
  115.         http_server.server_close()
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement