Advertisement
silver2row

SONAR, Prabakar, and BBIO

Sep 27th, 2019
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. # w/ help from #beagle on Freenode
  2. # and w/ help from Prabakar on his book "BeagleBone by Example."
  3.  
  4. from flask import Flask, render_template
  5. import Adafruit_BBIO.GPIO as GPIO
  6. import Adafruit_BBIO.PWM as PWM
  7. import time
  8. from rangeI import Range
  9.  
  10. sonar = Range("/dev/ttyS2")
  11.  
  12. class Motor:
  13.     def __init__(self, dir_pin, pwm_pin, pwm_freq):
  14.         self.dir_pin = dir_pin
  15.         self.pwm_pin = pwm_pin
  16.         self.value = 0
  17.  
  18.         PWM.start(pwm_pin, 0, pwm_freq)
  19.         GPIO.setup(dir_pin, GPIO.OUT)
  20.  
  21.     def set(self, value):
  22.         assert -100 <= value <= 100
  23.         if (value < 0) != (self.value < 0):
  24.  
  25.             # changing direction
  26.             PWM.set_duty_cycle(self.pwm_pin, 0)
  27.             GPIO.output(self.dir_pin, value < 0)
  28.  
  29.         PWM.set_duty_cycle(self.pwm_pin, abs(value))
  30.         self.value = value
  31.  
  32. motor1 = Motor(dir_pin="P8_18", pwm_pin="P9_16", pwm_freq=2000)
  33. motor2 = Motor(dir_pin="P8_16", pwm_pin="P9_14", pwm_freq=2000)
  34. motor3 = Motor(dir_pin="P8_14", pwm_pin="P8_13", pwm_freq=2000)
  35. motor4 = Motor(dir_pin="P8_26", pwm_pin="P8_19", pwm_freq=2000)
  36.  
  37. app = Flask(__name__)
  38. @app.route("/")
  39. @app.route("/<state>")
  40. @app.route("/<take>")
  41.  
  42. def Distance(take=None):
  43.  
  44.     distance = sonar.measure()
  45.     print("distance =", distance, "inch")
  46.     time.sleep(2)
  47.  
  48.     if take == distance >= 8:
  49.         motor1.set(0)
  50.         motor2.set(0)
  51.         motor3.set(0)
  52.         motor4.set(0)
  53.         print("Stopping Motors!")
  54.         time.sleep(0.2)
  55.  
  56.     template_data = {
  57.         "title" : take,
  58.     }
  59.     return render_template("Boot.html", **template_data)
  60.  
  61. def updates(state=None):
  62.  
  63.     if state == "F":
  64.         motor1.set(100)
  65.         motor2.set(100)
  66.         motor3.set(100)
  67.         motor4.set(100)
  68.         time.sleep(0.2)
  69.  
  70.     if state == "L":
  71.         motor1.set(15)
  72.         motor2.set(85)
  73.         motor3.set(15)
  74.         motor4.set(85)
  75.         time.sleep(0.2)
  76.  
  77.     if state == "R":
  78.         motor1.set(85)
  79.         motor2.set(15)
  80.         motor3.set(85)
  81.         motor4.set(15)
  82.         time.sleep(0.2)
  83.  
  84.     if state == "S":
  85.         motor1.set(0)
  86.         motor2.set(0)
  87.         motor3.set(0)
  88.         motor4.set(0)
  89.         time.sleep(0.2)
  90.  
  91.     if state == "REV":
  92.         motor1.set(-75)
  93.         motor2.set(-75)
  94.         motor3.set(-75)
  95.         motor4.set(-75)
  96.         time.sleep(0.2)
  97.  
  98.     if state == "REV_L":
  99.         motor1.set(-75)
  100.         motor2.set(-25)
  101.         motor3.set(-75)
  102.         motor4.set(-25)
  103.         time.sleep(0.2)
  104.  
  105.     if state == "REV_R":
  106.         motor1.set(-25)
  107.         motor2.set(-75)
  108.         motor3.set(-25)
  109.         motor4.set(-75)
  110.         time.sleep(0.2)
  111.  
  112.     if state == "SPIN_LEFT":
  113.         motor1.set(100)
  114.         motor2.set(-100)
  115.         motor3.set(100)
  116.         motor4.set(-100)
  117.         time.sleep(0.2)
  118.  
  119.     if state == "SPIN_RIGHT":
  120.         motor1.set(-100)
  121.         motor2.set(100)
  122.         motor3.set(-100)
  123.         motor4.set(100)
  124.         time.sleep(0.2)
  125.  
  126.     template_data = {
  127.         "title" : state,
  128.     }
  129.     return render_template("Boot.html", **template_data)
  130.  
  131. if __name__ == "__main__":
  132.     app.run(host="0.0.0.0", port=5000, debug=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement