Advertisement
silver2row

ideas for Flask and Python3 and Bots

Feb 26th, 2025
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from PySabertooth import Sabertooth, Range
  4. from flask import Flask, render_template
  5. import serial
  6. from time import sleep
  7. import decimal
  8.  
  9. # on the beaglebone related boards, use a known, working UART port on the headers.
  10.  
  11. # For instance, attach P9_21 to S1 on the sabertooth 2 x 12 and attach GND on the sabertooth to GND on your BBB.
  12. # "/dev/ttyS2" has changed in time. Check for changes...
  13.  
  14. saber = Sabertooth("/dev/bone/uart/2", baudrate=9600, timeout=0.1)
  15. ser = serial.Serial("/dev/bone/uart/1", baudrate=9600, timeout=0.1)
  16.  
  17. # start and enable a service, start a cron job, or make an executable .sh file for use when booting into this file.
  18.  
  19. app = Flask(__name__)
  20. @app.route("/")
  21. @app.route("/<gar>")
  22. @app.route("/<state>")
  23. class dire:
  24.     def siri(gar=None):
  25.         try:
  26.             if gar == '6' or gar >= '7':
  27.                 data = ser.read_until(b'\r')
  28.                 ranger = data[1:-1]
  29.                 ranger = bytes.decode(ranger)
  30.                 ranger = str(ranger)
  31.                 print("distance = ", ranger, "inches away..")
  32.                 sleep(3)
  33.             sleep(0.1)
  34.  
  35.         except serial.SerialException as e:
  36.             print(f"Error opening serial port: {e}")
  37.             raise
  38.  
  39.     def sab(state=None):
  40.         if state == "F":
  41.             print ("Robot Moving Forward")
  42.             try:
  43.                 saber.drive(1, 70)
  44.                 saber.drive(2, 70)
  45.             except ranger <= '5':
  46.                 print("Accessing AI...")
  47.                 saber.drive(1, -5)
  48.                 saber.drive(2, -5)
  49.                 pass
  50.  
  51.         if state == "R":
  52.             print ("Robot Turning Right")
  53.             try:
  54.                 saber.drive(1, 75)
  55.                 saber.drive(2, 25)
  56.             except ranger <= '5':
  57.                 print("Accessing AI...")
  58.                 saber.drive(1, -5)
  59.                 saber.drive(2, -5)
  60.                 pass
  61.  
  62.         if state == "L":
  63.             print ("Robot Turning Left")
  64.             try:
  65.                 saber.drive(1, 25)
  66.                 saber.drive(2, 75)
  67.             except ranger <= '5':
  68.                 print("Accessing AI...")
  69.                 saber.drive(1, -5)
  70.                 saber.drive(2, -5)
  71.                 pass
  72.  
  73.         if state == "S":
  74.             print ("Robot Stopped")
  75.             try:
  76.                 saber.drive(1, 0)
  77.                 saber.drive(2, 0)
  78.             except ranger <= '5':
  79.                 print("Accessing AI...")
  80.                 saber.drive(1, -5)
  81.                 saber.drive(2, -5)
  82.                 pass
  83.  
  84.         if state == "START":
  85.             print("Starting from a stop...")
  86.             saber.drive(1, True)
  87.             saber.drive(2, True)
  88.  
  89.         if state == "REV":
  90.             print("Reversing")
  91.             try:
  92.                 saber.drive(1, -55)
  93.                 saber.drive(2, -55)
  94.             except ranger <= '5':
  95.                 print("Accessing AI...")
  96.                 saber.drive(1, 5)
  97.                 saber.drive(2, 5)
  98.                 pass
  99.  
  100.         if state == "SPIN":
  101.             print("Loops...")
  102.             saber.drive(1, -55)
  103.             saber.drive(2, 55)
  104.  
  105.         template_data = {
  106.             "title" : state,
  107.         }
  108.         return render_template("Saber.html", name=(state, gar))
  109.  
  110.     if __name__=="__main__":
  111.         app.run(host="0.0.0.0", port=5000, debug=True)
  112.  
  113.  
  114.  
  115. # ERROR HERE - - [26/Feb/2025 22:16:58] "GET / HTTP/1.1" 404 -
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement