Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template
- import Adafruit_BBIO.GPIO as GPIO
- import Adafruit_BBIO.PWM as PWM
- from rangeI import Range
- sonar = Range("/dev/ttyS2")
- 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=2000)
- motor2 = Motor(dir_pin="P8_16", pwm_pin="P9_14", pwm_freq=2000)
- motor3 = Motor(dir_pin="P8_14", pwm_pin="P8_13", pwm_freq=2000)
- motor4 = Motor(dir_pin="P8_26", pwm_pin="P8_19", pwm_freq=2000)
- def set_motors( v1, v2, v3, v4 ):
- motor1.set( v1 )
- motor2.set( v2 )
- motor3.set( v3 )
- motor4.set( v4 )
- app = Flask(__name__)
- @app.route("/")
- def homepage( title="homepage" ):
- return render_template( "BootI.html", title=title )
- @app.route("/distance")
- def distance():
- distance = sonar.measure()
- print("distance =", distance, "inch")
- if distance < 8:
- set_motors( 0, 0, 0, 0 )
- print("Stopping Motors!")
- return homepage( title="distance" )
- def add_motors_route( state, v1, v2, v3, v4 ):
- @app.route( "/" + state, endpoint=state )
- def handler():
- set_motors( v1, v2, v3, v4 )
- return homepage( title=state )
- add_motors_route( "F", 100, 100, 100, 100 )
- add_motors_route( "L", 15, 85, 15, 85 )
- add_motors_route( "R", 85, 15, 85, 15 )
- add_motors_route( "S", 0, 0, 0, 0 )
- add_motors_route( "REV", -75, -75, -75, -75 )
- add_motors_route( "REV_L", -75, -25, -75, -25 )
- add_motors_route( "REV_R", -25, -75, -25, -75 )
- add_motors_route( "SPIN_LEFT", 100, -100, 100, -100 )
- add_motors_route( "SPIN_RIGHT", -100, 100, -100, 100 )
- if __name__ == "__main__":
- app.run(host="0.0.0.0", port=5000, debug=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement