Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- from PySabertooth import Sabertooth, Range
- from flask import Flask, render_template
- import serial
- from time import sleep
- import decimal
- # on the beaglebone related boards, use a known, working UART port on the headers.
- # For instance, attach P9_21 to S1 on the sabertooth 2 x 12 and attach GND on the sabertooth to GND on your BBB.
- # "/dev/ttyS2" has changed in time. Check for changes...
- saber = Sabertooth("/dev/bone/uart/2", baudrate=9600, timeout=0.1)
- ser = serial.Serial("/dev/bone/uart/1", baudrate=9600, timeout=0.1)
- # start and enable a service, start a cron job, or make an executable .sh file for use when booting into this file.
- app = Flask(__name__)
- @app.route("/")
- @app.route("/<gar>")
- @app.route("/<state>")
- class dire:
- def siri(gar=None):
- try:
- if gar == '6' or gar >= '7':
- data = ser.read_until(b'\r')
- ranger = data[1:-1]
- ranger = bytes.decode(ranger)
- ranger = str(ranger)
- print("distance = ", ranger, "inches away..")
- sleep(3)
- sleep(0.1)
- except serial.SerialException as e:
- print(f"Error opening serial port: {e}")
- raise
- def sab(state=None):
- if state == "F":
- print ("Robot Moving Forward")
- try:
- saber.drive(1, 70)
- saber.drive(2, 70)
- except ranger <= '5':
- print("Accessing AI...")
- saber.drive(1, -5)
- saber.drive(2, -5)
- pass
- if state == "R":
- print ("Robot Turning Right")
- try:
- saber.drive(1, 75)
- saber.drive(2, 25)
- except ranger <= '5':
- print("Accessing AI...")
- saber.drive(1, -5)
- saber.drive(2, -5)
- pass
- if state == "L":
- print ("Robot Turning Left")
- try:
- saber.drive(1, 25)
- saber.drive(2, 75)
- except ranger <= '5':
- print("Accessing AI...")
- saber.drive(1, -5)
- saber.drive(2, -5)
- pass
- if state == "S":
- print ("Robot Stopped")
- try:
- saber.drive(1, 0)
- saber.drive(2, 0)
- except ranger <= '5':
- print("Accessing AI...")
- saber.drive(1, -5)
- saber.drive(2, -5)
- pass
- if state == "START":
- print("Starting from a stop...")
- saber.drive(1, True)
- saber.drive(2, True)
- if state == "REV":
- print("Reversing")
- try:
- saber.drive(1, -55)
- saber.drive(2, -55)
- except ranger <= '5':
- print("Accessing AI...")
- saber.drive(1, 5)
- saber.drive(2, 5)
- pass
- if state == "SPIN":
- print("Loops...")
- saber.drive(1, -55)
- saber.drive(2, 55)
- template_data = {
- "title" : state,
- }
- return render_template("Saber.html", name=(state, gar))
- if __name__=="__main__":
- app.run(host="0.0.0.0", port=5000, debug=True)
- # ERROR HERE - - [26/Feb/2025 22:16:58] "GET / HTTP/1.1" 404 -
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement