-
Notifications
You must be signed in to change notification settings - Fork 0
/
sts-webcontrol.py
83 lines (54 loc) · 1.62 KB
/
sts-webcontrol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from flask import Flask, render_template, request, jsonify
import explorerhat as eh
import RPi.GPIO as GPIO
import time
servopos_default = 6.7
# servo setup
GPIO.setmode(GPIO.BCM)
ServoPIN1 = 18
GPIO.setup(ServoPIN1, GPIO.OUT)
servo1 = GPIO.PWM(ServoPIN1, 50)
ServoPIN2 = 15
GPIO.setup(ServoPIN2, GPIO.OUT)
servo2 = GPIO.PWM(ServoPIN2, 50)
servos = [servo1, servo2]
# go to inital centered positions
servos[0].start(servopos_default)
servos[1].start(servopos_default)
time.sleep(0.5)
# stop servo signal to prevent twitching
servos[0].ChangeDutyCycle(0)
servos[1].ChangeDutyCycle(0)
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/servo')
def servo():
# stop motors
eh.motor.one.speed(0)
eh.motor.two.speed(0)
time.sleep(0.1)
servonum = request.args.get('num', 0, type=int)
print("servonum: " + str(servonum))
servopos = request.args.get('pos', 0, type=float)
print("servopos: " + str(servopos))
if servopos != 0:
# set new position
servos[servonum].ChangeDutyCycle(servopos)
time.sleep(0.5)
# stop servo signal to prevent twitching
servos[servonum].ChangeDutyCycle(0)
return render_template('index.html')
@app.route('/drive')
def drive():
motor1 = request.args.get('m1', 0, type=int)
motor2 = request.args.get('m2', 0, type=int)
#print str(motor1) + " " + str(motor2)
#invert motor 1
motor2 *= -1
eh.motor.one.speed(motor1)
eh.motor.two.speed(motor2)
return jsonify(m1=motor1,m2=motor2)
if __name__ == '__main__':
app.run(host="0.0.0.0",port=int("88"))