-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14-FlyEarth-Bee.py
240 lines (228 loc) · 5.62 KB
/
14-FlyEarth-Bee.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import sys
import datetime
import time
import math
#import selenium
from selenium import webdriver
#from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
# import Kasper
from kaspersmicrobit import KaspersMicrobit
# Kasper mbit functions
def get_roll(x,y,z):
return math.atan(x/math.sqrt(math.pow(y,2) + math.pow(z,2))) * 180 / math.pi # +ve go right
def get_pitch(x,y,z):
return math.atan(y/math.sqrt(math.pow(x,2) + math.pow(z,2))) * 180 / math.pi # +ve go up
def get_pitch_roll(microbit):
accel = microbit.accelerometer.read()
x = accel.x
y = accel.y
z = accel.z
return [ get_pitch(x,y,z) , get_roll(x,y,z) ]
# global variables
global cmd_string # commands from microbit via serial, 'buttonA/B,pitch,roll'
global port, button, pitch, roll, k, rk # for flying
global i, e, l, n, m # message for logging
global t1, t2 # thresholds for actions
global lm # last move, characters d f b u l L r R
global wsize # window size
global sx, sy, d, s # mouse moves, start x,y drag dist and speed
i = 0 # set initial value
t1 = 20 # threshold 1
t2 = 40 # threshold 2
# create selenium objects)
driver = webdriver.Chrome()
# action accumulates keystrokes which are applied at the next perform()
# and the chain is emptied
action = ActionChains(driver)
# functions for keys
def kd(key):
action.key_down(key)
def ku(key):
action.key_up(key)
# variables for keys
ctrl = Keys.CONTROL
sh = Keys.SHIFT
alt = Keys.ALT
au = Keys.ARROW_UP
ad = Keys.ARROW_DOWN
al = Keys.ARROW_LEFT
ar = Keys.ARROW_RIGHT
pu = Keys.PAGE_UP
pd = Keys.PAGE_DOWN
# start Earth in Chrome
driver.get ("https://earth.google.com/")
# Confirm you are ready to fly
# input("Click through New Look popup") # no longer needed
# press ctrl-i to select .kml file for starting point, from dialog
"""
kd(ctrl)
action.send_keys("i")
ku(ctrl)
action.perform()
action.send_keys("O")
action.perform()
"""
input("Press shift and arrow multiple times to show horizon at top of screen")
"""
#press shift arrow_down 28 times to start 3D view and show horizon at top of screen
kd(sh)
for i in range(28):
kd(ad)
ku(ad)
action.perform()
ku(sh)
"""
with KaspersMicrobit.find_one_microbit() as microbit:
i = 0
l = datetime.datetime.now()
run = True
debug = False
#test_cmds = ("0,0,0","0,30,0","0,30,-30","0,30,30","0,-30,0")
# level up left right down
test = 0
while run :
try:
if debug == False :
# request new data from mbit
res = get_pitch_roll(microbit)
pitch = res[0]
roll = res[1]
#microbit.buttons.on_button_a(press=pressed)
a = microbit.buttons.read_button_a()
b = microbit.buttons.read_button_b()
button = 0
if a > 0 and b == 0:
button = 1 # look up
if a == 0 and b > 0:
button = -1 # look down
if a > 0 and b > 0:
button = 99 # stop Flying"
cmd_string = f"{button},{int(pitch)},{int(roll)}" # simulates prior serial string
except Exception as e:
sys.stdout.write(str(e))
break
else:
if button == 99:
# Stop flying
print("Flight Stopped")
exit()
# start process
m = ""
i += 1
n = datetime.datetime.now()
e = n - l
l = n
lm = ""
# start of bee control
"""
Strategy:
to minimize jerkiness keep keys down until pitch/roll changes
this will fly fast until hover is reached, and all keys up
each go comment will need appropriate key up and down
roll takes priority, left right turn, supersedes pitch
if no roll forward/back or up/down
flying with ku for all moves makes flight jerky
leaving active key down would make flight smoother and quick
"""
# start of roll
if roll > t2:
tm = "R"
#print(cmd_string + " turn right")
match lm:
case "":
kd(sh)
kd(al)
case "r":
ku(sh)
ku(al)
kd(ar)
lm = tm
elif roll > t1:
tm = "r"
match lm:
case "":
kd(ar)
case "R":
ku(sh)
ku(al)
kd(ar)
lm = tm
elif roll < -t2 :
tm = "L"
match lm:
case "":
kd(sh)
kd(ar)
case "l":
kd(sh)
kd(ar)
lm = tm
elif roll < -t1 :
tm = "l"
match lm:
case "":
kd(al)
case "L":
ku(sh)
lm = tm
else :
# hover
lm = ""
# all move keys up
ku(sh)
ku(al)
ku(ar)
ku(pu)
ku(pd)
ku(ad)
ku(au)
# start of pitch
if pitch > t2:
tm = "u"
match lm:
case "":
kd(pd)
case "b":
ku(sh)
lm = tm
elif pitch > t1 :
tm = "b"
match lm:
case "":
kd(ad)
case "u":
ku(ad)
kd(pu)
lm = tm
elif pitch < -t2 :
tm = "d"
match lm:
case "":
kd(pu)
case "f":
ku(pu)
kd(au)
lm = tm
elif pitch < -t1 :
tm = "f"
match lm:
case "" :
kd(au)
case "d" :
ku(pd)
kd(au)
lm = tm
else :
lm = ""
print(cmd_string + " " + lm)
# end of pitch
action.perform()
# time.sleep(1)
# end of process
# end of try get data from mbit
# end of while loop
# end of flight
# end of microbit