Advertisement
tomateblue

main.py

May 26th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. #!/usr/bin/python
  2. from flask import Flask, request,render_template
  3. from random import shuffle,choice
  4. from time import sleep
  5. from pygal.style import DarkSolarizedStyle
  6. import serial
  7. import pygal
  8.  
  9.  
  10.  
  11. app = Flask(__name__)
  12. try:
  13.  try:   # crie um serial na porta ACM0
  14.    ser = serial.Serial("/dev/ttyACM0",9600)
  15.  except: # crie um serial na porta ACM1
  16.   ser = serial.Serial("/dev/ttyACM1",9600)
  17. except:
  18.   status = False
  19.  
  20.  
  21. def gera_grafico(direita,esquerda,frente):
  22.   bar_chart = pygal.HorizontalBar(width=1200,height=400,style=DarkSolarizedStyle)
  23.   bar_chart.title = "Sensor UltraSom HC-04"
  24.   bar_chart.add('Direita', direita)
  25.   bar_chart.add('Esquanerda',esquerda)
  26.   bar_chart.add('Frente', frente)
  27.   chart = bar_chart.render(is_unicode=True)
  28.   return chart
  29.  
  30.  
  31. def update_values():
  32.   while True:
  33.    sleep(0.1)
  34.    rd = range(100)
  35.    shuffle(rd)
  36.    return gera_grafico(choice(rd),choice(rd),choice(rd))
  37.  
  38. def processos():
  39.   cpu = range(10)
  40.   ram = range(10)
  41.   processos_execucao = range (10)
  42.   return [cpu,ram,processos_execucao]
  43.  
  44.  
  45.  
  46.  
  47. @app.route("/")
  48. def home():
  49.  return render_template("index.html")
  50.  
  51. @app.route("/controle",methods=["GET","POST"])  # controle do robo manualmente
  52. def controle():
  53.  
  54.   if request.method == "POST":
  55.      if request.form['submit'] == "RUN":
  56.          print "RUN ROBOT"
  57.          # send comand RUN to robot
  58.          if status:
  59.            ser.write("w")
  60.          else:
  61.             print "No Send comand "  
  62.      elif request.form['submit'] == "BACK":
  63.          print 'RUN BACK'    
  64.          # send comand BACK to robot
  65.          if status:
  66.            ser.write("s")
  67.          else:
  68.             print "No Send comand "  
  69.          
  70.      elif request.form['submit'] == "LEFT":
  71.          print "RUN LEFT"
  72.          # send comand LEFT to robot          
  73.          if status:
  74.            ser.write("a")
  75.          else:
  76.             print "No Send comand "  
  77.      elif request.form['submit'] == "RIGHT":
  78.          print "RUN RIGHT"
  79.          # send comand LEFT to robot
  80.          if status:
  81.            ser.write("d")
  82.          else:
  83.             print "No Send comand "  
  84.          
  85.      elif request.form['submit'] == "STOP":
  86.          print "STOP ROBOT"
  87.          # send comand LEFT to robot
  88.          if status:
  89.            ser.write("p")
  90.          else:
  91.             print "No Send comand "  
  92.          
  93.   return render_template('controle.html')
  94.  
  95. @app.route("/graficos",methods=["GET","POST"])
  96. def graficos():
  97.   return render_template('graficos.html',chart=update_values())
  98.  
  99. @app.route("/processos")
  100. def processos():
  101.    return render_template('processos.html')
  102.  
  103. @app.route("/camera")
  104. def camera():
  105.  
  106.    return render_template('camera.html')
  107.  
  108. if __name__ == "__main__":
  109.     app.run(host="0.0.0.0",port=80,debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement