-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
150 lines (124 loc) · 4.26 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import json
import time
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
import uvicorn
from dronekit import connect as VehicleConnect, VehicleMode
# constant. this is html page. index.en.html English version.
HTMLPAGE = 'index.jp.html'
app = FastAPI()
app.mount("/static",StaticFiles(directory="static"), name="static")
vehicle = None # global : having vehicle status
#
# sub functions
# python require funtions before useing it.
#
# connect/disconnect to Dronekit object
def connectvehicle(connectstring):
global vehicle
if connectstring == "close":
if vehicle is None:
pass
else:
vehicle.close()
vehicle = None
return "NG"
else:
# all strings except "close", seemed as connectstring
if vehicle is None:
vehicle = VehicleConnect(connectstring, wait_ready=False)
# "wait_rady = True" does not come back.
vehicle.wait_ready('autopilot_version')
else:
# already connected.
return "OK"
#tried to connect request
if vehicle is None:
return "NG"
else:
return "OK"
def setMode(d_mode):
# don't use variable name 'mode' !!!
# flight Mode change.
global vehicle
vehicle.mode = VehicleMode(d_mode)
vehicle.flush()
while not vehicle.mode.name == d_mode:
time.sleep(1)
return
def getStatus(obj):
global vehicle
# get current status data
# vehicle object has complicated structure
# this function simplify it for JSON transfer
obj["type"] = vehicle.version.vehicle_type
obj["loc_g_lat"] = vehicle.location.global_frame.lat
obj["loc_g_lon"] = vehicle.location.global_frame.lon
obj["loc_g_alt"] = vehicle.location.global_frame.alt
obj["att_pitch"] = vehicle.attitude.pitch
obj["att_yaw"] = vehicle.attitude.yaw
obj["att_roll"] = vehicle.attitude.roll
# obj["velocity"] = vehicle.velocity (vx, vy, vz)
obj["gps"] = vehicle.gps_0.fix_type
# obj["gimbal"] = vehicle.gimbal
obj["batt_volt"] = vehicle.battery.voltage
obj["batt_cur"] = vehicle.battery.current
obj["batt_level"] = vehicle.battery.level
obj["ekf_status"] = vehicle.ekf_ok
obj["heartbeat"] = vehicle.last_heartbeat
obj["rngf_dist"] = vehicle.rangefinder.distance
obj["rngf_volt"] = vehicle.rangefinder.voltage
obj["heading"] = vehicle.heading
obj["armable"] = vehicle.is_armable
obj["sys_status"] = vehicle.system_status.state
obj["g_speed"] = vehicle.groundspeed
obj["mode"] = vehicle.mode.name
obj["armed"] = vehicle.armed
return
def tryArm():
global vehicle
if (vehicle is None):
pass
else:
if (vehicle.is_armable):
if vehicle.armed:
vehicle.armed = False
while not vehicle.armed:
time.sleep(1)
else:
vehicle.armed = True
while vehicle.armed:
time.sleep(1)
#
# main web request process
#
@app.get("/")
async def get():
fh = open(HTMLPAGE,'r')
html = fh.read()
return HTMLResponse(content=html, status_code=200)
@app.websocket("/msg")
async def msg(websocket: WebSocket):
await websocket.accept()
try:
while True:
# receive as json, add getting info and send back.
recv_obj = await websocket.receive_json()
key = recv_obj["key"]
if key == "connect":
recv_obj["result"] = connectvehicle(recv_obj["connectstring"])
elif recv_obj["key"] == "mode":
setMode(recv_obj["mode"])
recv_obj["mode"] = vehicle.mode.name
elif recv_obj["key"] == "status":
getStatus(recv_obj)
elif recv_obj["key"] == "arm":
# result is given by status.
tryArm()
await websocket.send_json(recv_obj) # send updated info to Browser
except WebSocketDisconnect:
print("*** unexpected something happens. socket closed. ")
# await websocket.close()
if __name__ == "__main__":
uvicorn.run(app, port=8000)