Advertisement
silver2row

Old Styler Prabakar and Free Spinnin'

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