-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhydra_server.py
145 lines (122 loc) · 5.32 KB
/
hydra_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
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
#!/usr/bin/env python3
"""
* Program: hydra_server.py
* Author: Tomas McMonigal
* Date: 2/15/2020
* Description: Multi-threaded server that accepts incoming connections to send
telemetry data to GUI, CP, and PP programs. Receives telemetry data from
ttyUSB0. To run it provide the port number as a command line parameter
"""
import asyncio
from mavsdk import System
from concurrent.futures import TimeoutError
from mavsdk import (Attitude, OffboardError)
async def echo_server(reader, writer, drone):
while True:
print("waiting on data requests")
marker = 1
try:
data = await asyncio.wait_for(reader.read(100), timeout=10)
if not data: # client has disconnected
print("connection closed by client: closing connection with client")
break
except TimeoutError:
print("Timeout error: closing connection with client")
break
# ******** if data is valid and connection active ****************
data_in = data.decode() # decodes utf-8 only
data_in = data_in.replace('\r\n', '')
if data_in == "latitude":
print("latitude requested")
async for position in drone.telemetry.position():
data_out = str(position.latitude_deg) + '\n'
# data_out = data_out + '\n'
writer.write(data_out.encode())
await writer.drain()
break
elif data_in == "altitude":
async for position in drone.telemetry.position():
print("altitude requested")
data_out = str(position.relative_altitude_m) + '\n'
writer.write(data_out.encode())
await writer.drain()
break
elif data_in == "longitude":
async for position in drone.telemetry.position():
print("longitude requested")
data_out = str(position.longitude_deg) + '\n'
# data_out = data_out + '\n'
writer.write(data_out.encode())
await writer.drain()
break
elif data_in == "battery":
async for battery in drone.telemetry.battery():
print("battery percentage requested")
data_out = str(battery.voltage_v) + '\n'
writer.write(data_out.encode())
await writer.drain()
break
elif data_in == "arm":
print("-- Arming")
await drone.action.arm()
elif data_in == "disarm":
try:
print("-- Disarming")
await drone.action.disarm()
except:
print("error disarming")
elif data_in == "kill":
print("-- Killing")
await drone.action.kill()
elif data_in == "is_armed":
async for is_armed in drone.telemetry.armed():
print("Is_armed requested:", is_armed)
data_out = str(is_armed) + '\n'
writer.write(data_out.encode())
await writer.drain()
break
elif data_in == "flight_mode":
async for flight_mode in drone.telemetry.flight_mode():
print("flight mode requested:", flight_mode)
data_out = str(flight_mode) + '\n'
writer.write(data_out.encode())
await writer.drain()
break
elif data_in == "takeoff":
print("-- Taking off")
await drone.action.takeoff()
elif data_in == "land":
print("-- Landing")
await drone.action.land()
elif data_in == "return":
print("-- Returning home")
await drone.action.return_to_launch()
else:
print("error: data requested not supported, closing connection")
output_string = '|' + data_in + '|'
print(output_string)
writer.close()
async def main(host, port):
drone = System()
await drone.connect(system_address="udp://:14555")
# await drone.connect(system_address="/dev/ttyUSB0")
print("Waiting for drone to connect...")
async for state in drone.core.connection_state():
if state.is_connected:
print(f"Drone discovered with UUID: {state.uuid}")
break
print("Waiting for drone to have a global position estimate...")
async for health in drone.telemetry.health():
if health.is_global_position_ok:
print("Global position estimate ok")
break
print("-- Setting initial setpoint")
await drone.offboard.set_attitude(Attitude(0.0, 0.0, 0.0, 0.0))
print("-- Setting takeoff altitude to 5m")
await drone.action.set_takeoff_altitude(5)
print (await drone.action.get_takeoff_altitude())
print("Ready to accept incoming connections")
server = await asyncio.start_server(lambda r, w: echo_server(r, w, drone), host, port)
async with server:
await server.serve_forever()
asyncio.run(main('127.0.0.1', 5000))