-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
86 lines (67 loc) · 2.44 KB
/
server.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
83
84
85
86
import json
from flask import *
import model.model as modelobj
from servos import servos
# Flask app configuration
DEBUG = True
SERVO_MIN = 0 # Minimum rotation value for the servo, should be -90 degrees of rotation.
SERVO_MAX = 180 # Maximum rotation value for the servo, should be 90 degrees of rotation.
SERVO_XCENTER = 130
SERVO_YCENTER = 85
MOTOR_VALUE = 105 # Speed setting for motor
FIRING_VALUE = 120 # Servo throw for firing arm
FIRING = False
BAUD_RATE = 9600
SERIAL_PORT = "/dev/ttyS0"
# Initialize flask app
app = Flask(__name__)
app.config.from_object(__name__)
# Setup the real servo when running on a Raspberry Pi
servos = servos.Servos(BAUD_RATE, SERIAL_PORT)
model = modelobj.LaserModel(servos, SERVO_MIN, SERVO_MAX, SERVO_XCENTER, SERVO_YCENTER, FIRING_VALUE, MOTOR_VALUE)
# Main view for rendering the web page
@app.route('/')
def main():
return render_template('main.html', model=model)
# Error handler for API call failures
@app.errorhandler(ValueError)
def valueErrorHandler(error):
return jsonify({'result': error.message}), 500
def successNoResponse():
return jsonify({'result': 'success'}), 204
# API calls used by the web app
@app.route('/set/servo/xaxis/<xaxis>', methods=['PUT'])
def setServoXAxis(xaxis):
model.setaxisx(xaxis)
return successNoResponse()
@app.route('/set/servo/yaxis/<yaxis>', methods=['PUT'])
def setServoYAaxis(yaxis):
model.setaxisy(yaxis)
return successNoResponse()
@app.route('/set/servos/<xaxis>/<yaxis>', methods=['PUT'])
def setServos(xaxis, yaxis):
model.setaxisx(xaxis)
model.setaxisy(yaxis)
return successNoResponse()
@app.route('/get/servos', methods=['GET'])
def getServos():
return jsonify({'xaxis': model.getaxisx(), 'yaxis': model.getaxisy()}), 200
@app.route('/get/calibration', methods=['GET'])
def getCalibration():
return jsonify({'target': model.targetCalibration, 'servo': model.servoCalibration}), 200
@app.route('/set/calibration', methods=['POST'])
def setCalibration():
model.setCalibration(json.loads(request.form['targetCalibration']), json.loads(request.form['servoCalibration']))
return successNoResponse()
@app.route('/target/<int:x>/<int:y>', methods=['PUT'])
def target(x, y):
model.target(x, y)
return successNoResponse()
@app.route('/fire', methods=['GET'])
def fire():
model.fire()
#return successNoResponse()
return successNoResponse()
# Start running the flask app
if __name__ == '__main__':
app.run(host='0.0.0.0')