Advertisement
silver2row

WorkIt TwerkIt Bot

Apr 26th, 2020
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. from flask import Flask, render_template
  2. import Adafruit_BBIO.GPIO as GPIO
  3. import Adafruit_BBIO.PWM as PWM
  4. from rangeI import Range
  5.  
  6. sonar = Range("/dev/ttyS2")
  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=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. def set_motors( v1, v2, v3, v4 ):
  37.     motor1.set( v1 )
  38.     motor2.set( v2 )
  39.     motor3.set( v3 )
  40.     motor4.set( v4 )
  41.  
  42.  
  43. app = Flask(__name__)
  44.  
  45. @app.route("/")
  46. def homepage( title="homepage" ):
  47.     return render_template( "BootI.html", title=title )
  48.  
  49. @app.route("/distance")
  50. def distance():
  51.     distance = sonar.measure()
  52.     print("distance =", distance, "inch")
  53.  
  54.     if distance < 8:
  55.         set_motors( 0, 0, 0, 0 )
  56.         print("Stopping Motors!")
  57.        
  58.      return homepage( title="distance" )
  59.  
  60. def add_motors_route( state, v1, v2, v3, v4 ):
  61.     @app.route( "/" + state, endpoint=state )
  62.     def handler():
  63.         set_motors( v1, v2, v3, v4 )
  64.         return homepage( title=state )
  65.  
  66. add_motors_route( "F",           100,  100,  100,  100 )
  67. add_motors_route( "L",            15,   85,   15,   85 )
  68. add_motors_route( "R",            85,   15,   85,   15 )
  69. add_motors_route( "S",             0,    0,    0,    0 )
  70. add_motors_route( "REV",         -75,  -75,  -75,  -75 )
  71. add_motors_route( "REV_L",       -75,  -25,  -75,  -25 )
  72. add_motors_route( "REV_R",       -25,  -75,  -25,  -75 )
  73. add_motors_route( "SPIN_LEFT",   100, -100,  100, -100 )
  74. add_motors_route( "SPIN_RIGHT", -100,  100, -100,  100 )
  75.  
  76. if __name__ == "__main__":
  77.     app.run(host="0.0.0.0", port=5000, debug=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement