-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw.py
70 lines (57 loc) · 1.68 KB
/
raw.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
import random
import os
from flask import Flask, flash, redirect, render_template, request, session, abort, url_for, jsonify, send_from_directory
# from flask_cache_buster import CacheBuster
import time
import string
import json
import krpc
from threading import Timer
conn = krpc.connect(name='Fly-by-wire', address='127.0.0.1')
vessel = conn.space_center.active_vessel
fps = 30
last_update = -1
spf = 1 / fps
def angles_to_control_landscape(angles, raw=False):
if 45 < angles[2] < 90:
angles[2] = -90
elif 0 < angles[2] < 45:
angles[2] = 0
pitch = (- 45 - angles[2])
roll = angles[1]
if not raw:
pitch /= 45
roll /= 90
return pitch, roll
def angles_to_control_portrait(angles, raw=False):
pitch, roll = angles[1], angles[2]
if not raw:
pitch /= 90
roll /= 90
return pitch, roll
app = Flask(__name__)
app.config['SECRET_KEY'] = 'kab9hlasjdb023jlgkab0ksd89p1uaivopwb9012h'
config = {
'extensions': ['.js', '.css', '.csv'],
'hash_size': 10
}
@app.route('/', methods=['GET'])
def index():
return render_template(
'index.html',
)
@app.route('/attitude_update', methods=['POST'])
def attitude_update():
global last_update
if time.time() > last_update + spf:
pitch, roll = angles_to_control_landscape(json.loads(tuple(request.form)[0]))
vessel.control.pitch = pitch
vessel.control.roll = roll
last_update = time.time()
return jsonify({'success': True}, 200, {'ContentType': 'application/json'})
@app.route('/throttle_update', methods=['POST'])
def throttle_update():
vessel.control.throttle = float(json.loads(tuple(request.form)[0])) / 100
return jsonify({'success': True}, 200, {'ContentType': 'application/json'})
if __name__ == '__main__':
app.run(host='0.0.0.0')