Advertisement
silver2row

Trying Things w/ Python

Jan 31st, 2021
1,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from flask import Flask, render_template
  4. import Adafruit_BBIO.GPIO as GPIO
  5. import Adafruit_BBIO.PWM as PWM
  6. from time import sleep
  7.  
  8. class Motors:
  9.     def __init__(self, dir_pin, pwm_pin, pwm_freq):
  10.         self.dir_pin = dir_pin
  11.         self.pwm_pin = pwm_pin
  12.         self.value = 0
  13.  
  14.         PWM.start(pwm_pin, 0, pwm_freq)
  15.         GPIO.setup(dir_pin, GPIO.OUT)
  16.  
  17.     def set(self, value):
  18.         if value == self.value:
  19.             return
  20.  
  21.         assert -100 <= value <= 100
  22.  
  23.         if (value < 0) != (self.value < 0):
  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 = Motors(dir_pin="P8_18", pwm_pin="P9_16", pwm_freq=1500)
  32. motor2 = Motors(dir_pin="P8_16", pwm_pin="P9_14", pwm_freq=1500)
  33. motor3 = Motors(dir_pin="P8_14", pwm_pin="P8_13", pwm_freq=1500)
  34. motor4 = Motors(dir_pin="P8_26", pwm_pin="P8_19", pwm_freq=1500)
  35.  
  36. def set_motorOne(v1):
  37.     motor1.set(v1)
  38.  
  39. def set_motorTwo(v2):
  40.     motor2.set(v2)
  41.  
  42. def set_motorThree(v3):
  43.     motor3.set(v3)
  44.  
  45. def set_motorFour(v4):
  46.     motor4.set(v4)
  47.  
  48. app = Flask(__name__)
  49.  
  50. @app.route("/")
  51. def homepage(title="homepage"):
  52.     return render_template("BootGoOne.html", title=title)
  53.  
  54. def add_motors_routeOne(state, v1):
  55.     @app.route("/" + state, endpoint=state)
  56.     def handlerOne():
  57.         set_motorOne(v1)
  58.         return homepage(title=state)
  59.  
  60. add_motors_routeOne('0',   0)
  61. add_motors_routeOne('10', 10)
  62. add_motors_routeOne('20', 20)
  63. add_motors_routeOne('30', 30)
  64. add_motors_routeOne('40', 40)
  65. add_motors_routeOne('50', 50)
  66. add_motors_routeOne('60', 60)
  67. add_motors_routeOne('70', 70)
  68. add_motors_routeOne('80', 80)
  69. add_motors_routeOne('90', 90)
  70. add_motors_routeOne('100', 100)
  71.  
  72. add_motors_routeOne('-4',   -4)
  73. add_motors_routeOne('-10', -10)
  74. add_motors_routeOne('-20', -20)
  75. add_motors_routeOne('-30', -30)
  76. add_motors_routeOne('-40', -40)
  77. add_motors_routeOne('-50', -50)
  78. add_motors_routeOne('-60', -60)
  79. add_motors_routeOne('-70', -70)
  80. add_motors_routeOne('-80', -80)
  81. add_motors_routeOne('-90', -90)
  82. add_motors_routeOne('-100', -100)
  83.  
  84. def add_motors_routeTwo(state, v2):
  85.     @app.route("/" + state, endpoint=state)
  86.     def handlerTwo():
  87.         set_motorTwo(v2)
  88.         return homepage(title=state)
  89.  
  90. add_motors_routeTwo('1',   1)
  91. add_motors_routeTwo('11', 11)
  92. add_motors_routeTwo('21', 21)
  93. add_motors_routeTwo('31', 31)
  94. add_motors_routeTwo('41', 41)
  95. add_motors_routeTwo('51', 51)
  96. add_motors_routeTwo('61', 61)
  97. add_motors_routeTwo('71', 71)
  98. add_motors_routeTwo('81', 81)
  99. add_motors_routeTwo('91', 91)
  100.  
  101. add_motors_routeTwo('-1',   -1)
  102. add_motors_routeTwo('-11', -11)
  103. add_motors_routeTwo('-21', -21)
  104. add_motors_routeTwo('-31', -31)
  105. add_motors_routeTwo('-41', -41)
  106. add_motors_routeTwo('-51', -51)
  107. add_motors_routeTwo('-61', -61)
  108. add_motors_routeTwo('-71', -71)
  109. add_motors_routeTwo('-81', -81)
  110. add_motors_routeTwo('-91', -91)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement