Advertisement
silver2row

trying random python w/ Motors!

Feb 2nd, 2021
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.27 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 Motor:
  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 = Motor(dir_pin="P8_18", pwm_pin="P9_16", pwm_freq=1000)
  32. motor2 = Motor(dir_pin="P8_16", pwm_pin="P9_14", pwm_freq=1000)
  33. motor3 = Motor(dir_pin="P8_14", pwm_pin="P8_13", pwm_freq=1000)
  34. motor4 = Motor(dir_pin="P8_26", pwm_pin="P8_19", pwm_freq=1000)
  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.         if add_motors_routeOne == 0:
  59.             PWM.stop("P9_16") # causes many errors like the rest of PWM.stop("Px_xx") for v1 through v4!
  60.         else:
  61.             pass
  62.  
  63.         return homepage(title=state)
  64.  
  65. add_motors_routeOne("0",   0)
  66. add_motors_routeOne('40', 40)
  67. add_motors_routeOne('50', 50)
  68. add_motors_routeOne('60', 60)
  69. add_motors_routeOne('70', 70)
  70. add_motors_routeOne('80', 80)
  71. add_motors_routeOne('90', 90)
  72. add_motors_routeOne('100', 100)
  73.  
  74. add_motors_routeOne('-40', -40)
  75. add_motors_routeOne('-50', -50)
  76. add_motors_routeOne('-60', -60)
  77. add_motors_routeOne('-70', -70)
  78. add_motors_routeOne('-80', -80)
  79. add_motors_routeOne('-90', -90)
  80. add_motors_routeOne('-100', -100)
  81.  
  82. def add_motors_routeTwo(state, v2):
  83.     @app.route("/" + state, endpoint=state)
  84.     def handlerTwo():
  85.         set_motorTwo(v2)
  86.         if add_motors_routeTwo == 0:
  87.             PWM.stop("P9_14")
  88.         else:
  89.             pass
  90.         return homepage(title=state)
  91.  
  92. add_motors_routeTwo("0",   0)
  93. add_motors_routeTwo('41', 41)
  94. add_motors_routeTwo('51', 51)
  95. add_motors_routeTwo('61', 61)
  96. add_motors_routeTwo('71', 71)
  97. add_motors_routeTwo('81', 81)
  98. add_motors_routeTwo('91', 91)
  99.  
  100. add_motors_routeTwo('-41', -41)
  101. add_motors_routeTwo('-51', -51)
  102. add_motors_routeTwo('-61', -61)
  103. add_motors_routeTwo('-71', -71)
  104. add_motors_routeTwo('-81', -81)
  105. add_motors_routeTwo('-91', -91)
  106.  
  107. def add_motors_routeThree(state, v3):
  108.     @app.route("/" + state, endpoint=state)
  109.     def handlerThree():
  110.         set_motorThree(v3)
  111.         if add_motors_routeThree == 0:
  112.             PWM.stop("P8_13")
  113.         return homepage(title=state)
  114.  
  115. add_motors_routeThree("0",   0)
  116. add_motors_routeThree('42', 52)
  117. add_motors_routeThree('52', 62)
  118. add_motors_routeThree('62', 72)
  119. add_motors_routeThree('72', 82)
  120. add_motors_routeThree('82', 92)
  121. add_motors_routeThree('92', 92)
  122.  
  123. add_motors_routeThree('-42', -42)
  124. add_motors_routeThree('-52', -52)
  125. add_motors_routeThree('-62', -62)
  126. add_motors_routeThree('-72', -72)
  127. add_motors_routeThree('-82', -82)
  128. add_motors_routeThree('-92', -92)
  129.  
  130. def add_motors_routeFour(state, v4):
  131.     @app.route("/" + state, endpoint=state)
  132.     def handlerFour():
  133.         set_motorFour(v4)
  134.         if add_motors_routeFour == 0:
  135.             PWM.stop("P8_19")
  136.         else:
  137.             pass
  138.         return homepage(title=state)
  139.  
  140. add_motors_routeFour("0",   0)
  141. add_motors_routeFour('43', 43)
  142. add_motors_routeFour('53', 53)
  143. add_motors_routeFour('63', 63)
  144. add_motors_routeFour('73', 73)
  145. add_motors_routeFour('83', 83)
  146. add_motors_routeFour('93', 93)
  147.  
  148. add_motors_routeFour('-43', -43)
  149. add_motors_routeFour('-53', -53)
  150. add_motors_routeFour('-63', -63)
  151. add_motors_routeFour('-73', -73)
  152. add_motors_routeFour('-83', -83)
  153. add_motors_routeFour('-93', -93)
  154.  
  155. if __name__ == "__main__":
  156.     app.run(host="0.0.0.0", port=5000, debug=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement