Created
February 16, 2016 13:52
-
-
Save Phate334/5285fcc38ecd84e6630c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>servo motor control</title> | |
<link rel=stylesheet href="static/css/jquery.mobile-1.4.5.min.css"> | |
<script src="static/js/jquery-1.12.0.min.js"></script> | |
<script src="static/js/jquery.mobile-1.4.5.min.js"></script> | |
</head> | |
<body> | |
<div data-rote="page"> | |
<div data-role="header"> | |
<h1>servo controler</h1> | |
</div> | |
<div data-rote="content"> | |
<form> | |
<h2>servo0</h2> | |
<label for="servo0"></label> | |
<input name="servo0" id="servo0" min="0" max="10" step="0.1" type="range"> | |
</form> | |
<form> | |
<h2>servo1</h2> | |
<label for="servo1"></label> | |
<input name="servo1" id="servo1" min="0" max="10" step="0.1" type="range"> | |
</form> | |
</div> | |
<div data-role="footer"> | |
<h1>PhateW</h1> | |
</div> | |
</div> | |
<script> | |
$(document).ready(function(){ | |
$("#servo0").change(function(){ | |
console.log($("#servo0").val()); | |
$.ajax({url:"/move_servo/0/"+$("#servo0").val()}); | |
}); | |
$("#servo1").change(function(){ | |
console.log($("#servo1").val()); | |
$.ajax({url:"/move_servo/1/"+$("#servo1").val()}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
from flask import Flask, render_template | |
servos = [18, 24] | |
pwms = [] | |
app = Flask(__name__) | |
GPIO.setmode(GPIO.BCM) | |
for s_port in servos: | |
GPIO.setup(s_port, GPIO.OUT) | |
pwm = GPIO.PWM(s_port, 50) | |
pwm.start(0) | |
pwm.ChangeDutyCycle(1) | |
pwm.ChangeDutyCycle(0) | |
pwms.append(pwm) | |
@app.route("/<name>") | |
def page(name): | |
if ".html" not in name: | |
return render_template(".".join([name, "html"])) | |
return render_template(name) | |
@app.route("/move_servo/<servo_num>/<value>") | |
def move_servo(servo_num, value): | |
pwms[int(servo_num)].ChangeDutyCycle(float(value)) | |
return "%d|%.2f"%(int(servo_num), float(value)) | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0") | |
for s in pwms: | |
s.stop() | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment