Advertisement
silver2row

Prabakar and Sonar and @zmatt...

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