-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_data.py
154 lines (133 loc) · 4.27 KB
/
sample_data.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
151
152
153
154
from cmath import nan
from re import T
from websocket import create_connection
import sys
import json
import time
import keyboard
import math
import time
import websocket
class Drone:
def __init__(self, id, alt, lat, long, pitch, roll, yaw, velocityX, velocityY, velocityZ):
self.DroneId = id
self.Altitude = alt
self.Latitude = lat
self.Longitude = long
self.Pitch = pitch
self.Roll = roll
self.Yaw = yaw
self.Compass = yaw
self.VelocityX = velocityX
self.VelocityY = velocityY
self.VelocityZ = velocityZ
def translate_in_direction(self, angle, force):
angle = (angle + self.Yaw) % 360
x = force * math.cos(math.radians(angle)) #lat
y = force * math.sin(math.radians(angle)) #long
self.Latitude += x
self.Longitude += y
self.VelocityX = round(1000000*x, 1)
self.VelocityY = round(1000000*y, 1)
def close_connection():
ws.close()
def send_data(ws:websocket):
controll = False
if (len(sys.argv) > 1):
drone.DroneId = sys.argv[1]
input_given = False
previously_pressed = False
sleepTime = 0.1
force = 0.0000001 * (sleepTime / 0.02)
altitudeForce = 0.02 * (sleepTime / 0.02)
while True:
jsonData = json.dumps(drone.__dict__)
ws.send(jsonData)
if keyboard.is_pressed('`'):
if not previously_pressed:
controll = not controll
print(f"Input set to {controll}", flush=True)
previously_pressed = True
time.sleep(sleepTime)
continue
previously_pressed = False
if keyboard.is_pressed('esc'):
ws.close()
break
if not controll:
time.sleep(sleepTime)
continue
input_given = False
if keyboard.is_pressed('d'):
input_given = True
print("D", end=" ")
drone.translate_in_direction(90, force)
drone.Roll = 30
elif keyboard.is_pressed('a'):
input_given = True
print("A", end=" ")
drone.translate_in_direction(-90, force)
drone.Roll = -30
else:
drone.Roll = 0
if keyboard.is_pressed('w'):
input_given = True
print("W", end=" ")
drone.translate_in_direction(0, force)
drone.Pitch = -30
elif keyboard.is_pressed('s'):
input_given = True
print("S", end=" ")
drone.translate_in_direction(180, force)
drone.Pitch = 30
else:
drone.Pitch = 0
if keyboard.is_pressed('shift'):
input_given = True
print("SHIFT", end=" ")
drone.Altitude += altitudeForce
elif keyboard.is_pressed('ctrl'):
input_given = True
print("CTRL", end=" ")
drone.Altitude -= altitudeForce
if keyboard.is_pressed('q'):
input_given = True
print("Q", end=" ")
drone.Yaw = (drone.Yaw - 3) % 360
drone.Compass = drone.Yaw
elif keyboard.is_pressed('e'):
input_given = True
print("E", end=" ")
drone.Yaw = (drone.Yaw + 3) % 360
drone.Compass = drone.Yaw
if input_given:
print(' '*15, end='\r', flush=True)
else:
drone.VelocityY = 0
drone.VelocityX = 0
drone.VelocityZ = 0
time.sleep(sleepTime)
def on_message(vs, msg:str):
if msg.startswith("testdata"):
print(msg)
ws = None
try:
server = '147.229.14.181'
if (len(sys.argv) > 2):
server = sys.argv[2]
ws = create_connection(f"ws://{server}:5555", on_message=on_message)
altitude = 221.4
yaw = 0
latitude = 49.227227662057544
longtitude = 16.59721346994562
pitch = 0
roll = 0
drone = Drone('Drone0', altitude, latitude, longtitude, 0, 0, 0, 0, 0, 0)
except:
print('Failed to connect')
if ws:
try:
send_data(ws)
except KeyboardInterrupt:
ws.close()
sys.exit(0)