Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- from flask import Flask, render_template
- import Adafruit_BBIO.GPIO as GPIO
- import Adafruit_BBIO.PWM as PWM
- from time import sleep
- class Motor:
- def __init__(self, dir_pin, pwm_pin, pwm_freq):
- self.dir_pin = dir_pin
- self.pwm_pin = pwm_pin
- self.value = 0
- PWM.start(pwm_pin, 0, pwm_freq)
- GPIO.setup(dir_pin, GPIO.OUT)
- def set(self, value):
- if value == self.value:
- return
- assert -100 <= value <= 100
- if (value < 0) != (self.value < 0):
- # changing direction
- PWM.set_duty_cycle(self.pwm_pin, 0)
- GPIO.output(self.dir_pin, value < 0)
- PWM.set_duty_cycle(self.pwm_pin, abs(value))
- self.value = value
- motor1 = Motor(dir_pin="P8_18", pwm_pin="P9_16", pwm_freq=1000)
- motor2 = Motor(dir_pin="P8_16", pwm_pin="P9_14", pwm_freq=1000)
- motor3 = Motor(dir_pin="P8_14", pwm_pin="P8_13", pwm_freq=1000)
- motor4 = Motor(dir_pin="P8_26", pwm_pin="P8_19", pwm_freq=1000)
- def set_motorOne(v1):
- motor1.set(v1)
- def set_motorTwo(v2):
- motor2.set(v2)
- def set_motorThree(v3):
- motor3.set(v3)
- def set_motorFour(v4):
- motor4.set(v4)
- app = Flask(__name__)
- @app.route("/")
- def homepage(title="homepage"):
- return render_template("BootGoOne.html", title=title)
- def add_motors_routeOne(state, v1):
- @app.route("/" + state, endpoint=state)
- def handlerOne():
- set_motorOne(v1)
- if add_motors_routeOne == 0:
- PWM.stop("P9_16") # causes many errors like the rest of PWM.stop("Px_xx") for v1 through v4!
- else:
- pass
- return homepage(title=state)
- add_motors_routeOne("0", 0)
- add_motors_routeOne('40', 40)
- add_motors_routeOne('50', 50)
- add_motors_routeOne('60', 60)
- add_motors_routeOne('70', 70)
- add_motors_routeOne('80', 80)
- add_motors_routeOne('90', 90)
- add_motors_routeOne('100', 100)
- add_motors_routeOne('-40', -40)
- add_motors_routeOne('-50', -50)
- add_motors_routeOne('-60', -60)
- add_motors_routeOne('-70', -70)
- add_motors_routeOne('-80', -80)
- add_motors_routeOne('-90', -90)
- add_motors_routeOne('-100', -100)
- def add_motors_routeTwo(state, v2):
- @app.route("/" + state, endpoint=state)
- def handlerTwo():
- set_motorTwo(v2)
- if add_motors_routeTwo == 0:
- PWM.stop("P9_14")
- else:
- pass
- return homepage(title=state)
- add_motors_routeTwo("0", 0)
- add_motors_routeTwo('41', 41)
- add_motors_routeTwo('51', 51)
- add_motors_routeTwo('61', 61)
- add_motors_routeTwo('71', 71)
- add_motors_routeTwo('81', 81)
- add_motors_routeTwo('91', 91)
- add_motors_routeTwo('-41', -41)
- add_motors_routeTwo('-51', -51)
- add_motors_routeTwo('-61', -61)
- add_motors_routeTwo('-71', -71)
- add_motors_routeTwo('-81', -81)
- add_motors_routeTwo('-91', -91)
- def add_motors_routeThree(state, v3):
- @app.route("/" + state, endpoint=state)
- def handlerThree():
- set_motorThree(v3)
- if add_motors_routeThree == 0:
- PWM.stop("P8_13")
- return homepage(title=state)
- add_motors_routeThree("0", 0)
- add_motors_routeThree('42', 52)
- add_motors_routeThree('52', 62)
- add_motors_routeThree('62', 72)
- add_motors_routeThree('72', 82)
- add_motors_routeThree('82', 92)
- add_motors_routeThree('92', 92)
- add_motors_routeThree('-42', -42)
- add_motors_routeThree('-52', -52)
- add_motors_routeThree('-62', -62)
- add_motors_routeThree('-72', -72)
- add_motors_routeThree('-82', -82)
- add_motors_routeThree('-92', -92)
- def add_motors_routeFour(state, v4):
- @app.route("/" + state, endpoint=state)
- def handlerFour():
- set_motorFour(v4)
- if add_motors_routeFour == 0:
- PWM.stop("P8_19")
- else:
- pass
- return homepage(title=state)
- add_motors_routeFour("0", 0)
- add_motors_routeFour('43', 43)
- add_motors_routeFour('53', 53)
- add_motors_routeFour('63', 63)
- add_motors_routeFour('73', 73)
- add_motors_routeFour('83', 83)
- add_motors_routeFour('93', 93)
- add_motors_routeFour('-43', -43)
- add_motors_routeFour('-53', -53)
- add_motors_routeFour('-63', -63)
- add_motors_routeFour('-73', -73)
- add_motors_routeFour('-83', -83)
- add_motors_routeFour('-93', -93)
- if __name__ == "__main__":
- app.run(host="0.0.0.0", port=5000, debug=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement