Advertisement
silver2row

Prabakar and Freenode #beagle BOTS!

Mar 8th, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # w/ help from #beagle on Freenode
  4.  
  5. from flask import Flask, render_template
  6. import Adafruit_BBIO.GPIO as GPIO
  7. import Adafruit_BBIO.PWM as PWM
  8. import time
  9.  
  10. class Motor:
  11.     def __init__(self, dir_pin, pwm_pin, pwm_freq):
  12.         self.dir_pin = dir_pin
  13.         self.pwm_pin = pwm_pin
  14.         self.value = 0
  15.  
  16.         PWM.start(pwm_pin, 0, pwm_freq)
  17.         GPIO.setup(dir_pin, GPIO.OUT)
  18.  
  19.     def set(self, value):
  20.         assert -100 <= value <= 100
  21.         if (value < 0) != (self.value < 0):
  22.             # changing direction
  23.             PWM.set_duty_cycle(self.pwm_pin, 0)
  24.             GPIO.output(self.dir_pin, value < 0)
  25.         PWM.set_duty_cycle(self.pwm_pin, abs(value))
  26.         self.value = value
  27.  
  28. motor1 = Motor(dir_pin="P8_18", pwm_pin="P9_16", pwm_freq=2000)
  29. motor2 = Motor(dir_pin="P8_16", pwm_pin="P9_14", pwm_freq=2000)
  30. motor3 = Motor(dir_pin="P8_14", pwm_pin="P8_13", pwm_freq=2000)
  31. motor4 = Motor(dir_pin="P8_26", pwm_pin="P8_19", pwm_freq=2000)
  32.  
  33. app = Flask(__name__)
  34. @app.route("/")
  35. @app.route("/<state>")
  36.  
  37. def updates(state=None):
  38.     if state == "F":
  39.         motor1.set(100)
  40.         motor2.set(100)
  41.         motor3.set(100)
  42.         motor4.set(100)
  43.         time.sleep(.2)
  44.     if state == "L":
  45.         motor1.set(0)
  46.         motor4.set(85)
  47.         time.sleep(.2)
  48.     if state == "R":
  49.         motor1.set(85)
  50.         motor4.set(0)
  51.         time.sleep(.2)
  52.     if state == "S":
  53.         motor1.set(0)
  54.         motor4.set(0)
  55.         time.sleep(.2)
  56.     if state == "REV":
  57.         motor1.set(-75)
  58.         motor4.set(-75)
  59.         time.sleep(.2)
  60.     if state == "REV_L":
  61.         motor1.set(-75)
  62.         motor4.set(0)
  63.         time.sleep(.2)
  64.     if state == "REV_R":
  65.         motor1.set(0)
  66.         motor4.set(-75)
  67.         time.sleep(.2)
  68.  
  69.     template_data = {
  70.         "title" : state,
  71.     }
  72.     return render_template("boboIV.html", **template_data)
  73.  
  74. if __name__ == "__main__":
  75.     app.run(host="0.0.0.0", port=5000, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement